aboutsummaryrefslogtreecommitdiff
path: root/node_modules/gulp-gzip/lib
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-10-10 03:43:44 +0200
committerFlorian Dold <florian.dold@gmail.com>2016-10-10 03:43:44 +0200
commitabd94a7f5a50f43c797a11b53549ae48fff667c3 (patch)
treeab8ed457f65cdd72e13e0571d2975729428f1551 /node_modules/gulp-gzip/lib
parenta0247c6a3fd6a09a41a7e35a3441324c4dcb58be (diff)
downloadwallet-core-abd94a7f5a50f43c797a11b53549ae48fff667c3.tar.xz
add node_modules to address #4364
Diffstat (limited to 'node_modules/gulp-gzip/lib')
-rwxr-xr-xnode_modules/gulp-gzip/lib/compress.js88
-rwxr-xr-xnode_modules/gulp-gzip/lib/utils.js43
2 files changed, 131 insertions, 0 deletions
diff --git a/node_modules/gulp-gzip/lib/compress.js b/node_modules/gulp-gzip/lib/compress.js
new file mode 100755
index 000000000..db04c4c23
--- /dev/null
+++ b/node_modules/gulp-gzip/lib/compress.js
@@ -0,0 +1,88 @@
+var zlib = require('zlib');
+var Readable = require('stream').Readable;
+var toArray = require('stream-to-array');
+
+function convertContentsToBuffer(contents, callback) {
+ if (contents instanceof Buffer) {
+ callback(null, contents);
+ } else {
+ toArray(contents, function (err, chunks) {
+ if (err) {
+ callback(err, null);
+ return;
+ }
+
+ callback(null, Buffer.concat(chunks));
+ });
+ }
+}
+
+function convertContentsToStream(contents, callback) {
+ if (contents instanceof Readable) {
+ callback(null, contents);
+ } else {
+ var rs = new Readable({ objectMode: true });
+ rs._read = function() {
+ rs.push(contents);
+ rs.push(null);
+ };
+ callback(null, rs);
+ }
+}
+
+module.exports = function(originalContents, options, callback) {
+
+ convertContentsToBuffer(originalContents, function(err, contentsAsBuffer) {
+ if (err) {
+ callback(err, null, false);
+ return;
+ }
+
+ var originalContentLength = contentsAsBuffer.length;
+
+ // Check if the threshold option is set
+ // If true, check if the buffer length is greater than the threshold
+ if (options.threshold && originalContentLength < options.threshold) {
+ // File size is smaller than the threshold
+ // Pass it along to the next plugin without compressing
+ if (originalContents instanceof Buffer) {
+ callback(null, contentsAsBuffer, false);
+ } else {
+ convertContentsToStream(contentsAsBuffer, function(err, contentsAsStream) {
+ callback(null, contentsAsStream, false);
+ });
+ }
+ return;
+ }
+
+ convertContentsToStream(contentsAsBuffer, function(err, contentsAsStream) {
+ if (err) {
+ callback(err, null, false);
+ return;
+ }
+
+ // Compress the contents
+ var gzipStream = zlib.createGzip(options.gzipOptions);
+ contentsAsStream.pipe(gzipStream);
+
+ convertContentsToBuffer(gzipStream, function(err, compressedContentsAsBuffer) {
+ if (err) {
+ callback(err, null, false);
+ return;
+ }
+
+ if (options.skipGrowingFiles && compressedContentsAsBuffer.length >= originalContentLength) {
+ callback(null, originalContents, false);
+ } else {
+ if (originalContents instanceof Buffer) {
+ callback(null, compressedContentsAsBuffer, true);
+ } else {
+ convertContentsToStream(compressedContentsAsBuffer, function(err, compressedContentsStream) {
+ callback(null, compressedContentsStream, true);
+ });
+ }
+ }
+ });
+ });
+ });
+};
diff --git a/node_modules/gulp-gzip/lib/utils.js b/node_modules/gulp-gzip/lib/utils.js
new file mode 100755
index 000000000..5658e1ca1
--- /dev/null
+++ b/node_modules/gulp-gzip/lib/utils.js
@@ -0,0 +1,43 @@
+var bytes = require('bytes');
+
+// Merge source object with target object while handling threshold option
+// Used to merge user defined plugin options with default options
+function merge(target, source) {
+ if (typeof source === 'undefined') source = {};
+
+ Object.keys(source).forEach(function(key) {
+ if (key === 'threshold') {
+ target[key] = threshold(source[key]);
+ } else {
+ target[key] = source[key];
+ }
+ });
+
+ return target;
+}
+
+// Parse the threshold plugin option
+// Specifies the minimum file size that will be compressed
+// Can be a string, number, or boolean
+function threshold(obj) {
+ var ret;
+
+ switch (typeof obj) {
+ case 'string':
+ ret = bytes(obj) < 150 ? 150 : bytes(obj);
+ break;
+ case 'number':
+ ret = obj < 150 ? 150 : obj;
+ break;
+ case 'boolean':
+ ret = obj === false ? false : 150;
+ break;
+ default:
+ throw new Error('threshold must be String|Number|Boolean');
+ }
+
+ return ret;
+}
+
+exports.merge = merge;
+exports.threshold = threshold;