Skip to content

Commit

Permalink
Changed tabs to spaces.
Browse files Browse the repository at this point in the history
I used to prefer tabs for whatever reason, but it's started to get on my
nerves. Tabs look especially bad when viewing the file on GitHub.
  • Loading branch information
jstuckey committed Mar 29, 2015
1 parent 83d5ccb commit 1c89362
Show file tree
Hide file tree
Showing 5 changed files with 575 additions and 575 deletions.
94 changes: 47 additions & 47 deletions index.js
Expand Up @@ -11,60 +11,60 @@ var PLUGIN_NAME = 'gulp-gzip';

module.exports = function (options) {

// Combine user defined options with default options
var config = utils.merge({ append: true, threshold: false, gzipOptions: {} }, options);
// Combine user defined options with default options
var config = utils.merge({ append: true, threshold: false, gzipOptions: {} }, options);

// Create a through2 object stream. This is our plugin export
var stream = through2.obj(compress);
// Create a through2 object stream. This is our plugin export
var stream = through2.obj(compress);

// Expose the config so we can test it
stream.config = config;
// Expose the config so we can test it
stream.config = config;

function compress(file, enc, done) {
function compress(file, enc, done) {

/*jshint validthis: true */
var self = this;
/*jshint validthis: true */
var self = this;

// Check for empty file
if (file.isNull()) {
// Pass along the empty file to the next plugin
self.push(file);
done();
return;
}
// Check for empty file
if (file.isNull()) {
// Pass along the empty file to the next plugin
self.push(file);
done();
return;
}

// Call when finished with compression
var finished = function(err, contents, wasCompressed) {
if (err) {
var error = new PluginError(PLUGIN_NAME, err, { showStack: true });
self.emit('error', error);
done();
return;
}
// Call when finished with compression
var finished = function(err, contents, wasCompressed) {
if (err) {
var error = new PluginError(PLUGIN_NAME, err, { showStack: true });
self.emit('error', error);
done();
return;
}

if (wasCompressed) {
if (file.contentEncoding) {
file.contentEncoding.push('gzip');
} else {
file.contentEncoding = [ 'gzip' ];
}
if (config.append) {
file.path += '.gz';
}
}
file.contents = contents;
self.push(file);
done();
return;
};
if (wasCompressed) {
if (file.contentEncoding) {
file.contentEncoding.push('gzip');
} else {
file.contentEncoding = [ 'gzip' ];
}
if (config.append) {
file.path += '.gz';
}
}
file.contents = contents;
self.push(file);
done();
return;
};

// Check if file contents is a buffer or a stream
if (file.isBuffer()) {
bufferMode(file.contents, config, finished);
} else {
streamMode(file.contents, config, finished);
}
}
// Check if file contents is a buffer or a stream
if (file.isBuffer()) {
bufferMode(file.contents, config, finished);
} else {
streamMode(file.contents, config, finished);
}
}

return stream;
return stream;
};
54 changes: 27 additions & 27 deletions lib/bufferMode.js
Expand Up @@ -3,34 +3,34 @@ var Readable = require('stream').Readable;
var toArray = require('stream-to-array');

module.exports = function(contents, options, callback) {
// Check if the threshold option is set
// If true, check if the buffer length is greater than the threshold
if (options.threshold && contents.length < options.threshold) {
// File size is smaller than the threshold
// Pass it along to the next plugin without compressing
callback(null, contents, false);
return;
}
// Check if the threshold option is set
// If true, check if the buffer length is greater than the threshold
if (options.threshold && contents.length < options.threshold) {
// File size is smaller than the threshold
// Pass it along to the next plugin without compressing
callback(null, contents, false);
return;
}

// Create a readable stream out of the contents
var rs = new Readable({ objectMode: true });
rs._read = function() {
rs.push(contents);
rs.push(null);
};
// Create a readable stream out of the contents
var rs = new Readable({ objectMode: true });
rs._read = function() {
rs.push(contents);
rs.push(null);
};

// Compress the contents
var gzipStream = zlib.createGzip(options.gzipOptions);
rs.pipe(gzipStream);
// Compress the contents
var gzipStream = zlib.createGzip(options.gzipOptions);
rs.pipe(gzipStream);

// Turn gzip stream back into a buffer
toArray(gzipStream, function (err, chunks) {
if (err) {
callback(err, null, false);
return;
}
// Turn gzip stream back into a buffer
toArray(gzipStream, function (err, chunks) {
if (err) {
callback(err, null, false);
return;
}

callback(null, Buffer.concat(chunks), true);
return;
});
};
callback(null, Buffer.concat(chunks), true);
return;
});
};
58 changes: 29 additions & 29 deletions lib/streamMode.js
Expand Up @@ -3,35 +3,35 @@ var through2 = require('through2');
var toArray = require('stream-to-array');

module.exports = function(contents, options, callback) {
// Check if the threshold option is set
if (options.threshold) {
// Check if the stream contents is less than the threshold
toArray(contents, function (err, chunks) {
if (err) {
callback(err, null, false);
return;
}
// Check if the threshold option is set
if (options.threshold) {
// Check if the stream contents is less than the threshold
toArray(contents, function (err, chunks) {
if (err) {
callback(err, null, false);
return;
}

// Join chunks array into a single buffer
var buffer = Buffer.concat(chunks);
// Join chunks array into a single buffer
var buffer = Buffer.concat(chunks);

// Create a stream to return to the callback
var contentStream = through2();
contentStream.end(buffer);
// Create a stream to return to the callback
var contentStream = through2();
contentStream.end(buffer);

// Check if the stream content length is less than the threshold
if (buffer.length < options.threshold) {
// File does not meet the minimum size requirement for compression
callback(null, contentStream, false);
} else {
// File meets the minimum size requirement for compression
var gzipStream = zlib.createGzip(options.gzipOptions);
callback(null, contentStream.pipe(gzipStream), true);
}
});
} else {
// Compress the file contents
var gzipStream = zlib.createGzip(options.gzipOptions);
callback(null, contents.pipe(gzipStream), true);
}
};
// Check if the stream content length is less than the threshold
if (buffer.length < options.threshold) {
// File does not meet the minimum size requirement for compression
callback(null, contentStream, false);
} else {
// File meets the minimum size requirement for compression
var gzipStream = zlib.createGzip(options.gzipOptions);
callback(null, contentStream.pipe(gzipStream), true);
}
});
} else {
// Compress the file contents
var gzipStream = zlib.createGzip(options.gzipOptions);
callback(null, contents.pipe(gzipStream), true);
}
};
50 changes: 25 additions & 25 deletions lib/utils.js
Expand Up @@ -3,41 +3,41 @@ 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 = {};
if (typeof source === 'undefined') source = {};

Object.keys(source).forEach(function(key) {
if (key === 'threshold') {
target[key] = threshold(source[key]);
} else {
target[key] = source[key];
}
});
Object.keys(source).forEach(function(key) {
if (key === 'threshold') {
target[key] = threshold(source[key]);
} else {
target[key] = source[key];
}
});

return target;
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;
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');
}
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;
return ret;
}

exports.merge = merge;
exports.threshold = threshold;
exports.threshold = threshold;

0 comments on commit 1c89362

Please sign in to comment.