Spaces:
Sleeping
Sleeping
File size: 1,132 Bytes
05a77ff |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
var spawn = require('child_process').spawn,
util = require('util'),
ResponseStream = require('../../lib').ResponseStream;
/**
* Accepts a writable stream, i.e. fs.WriteStream, and returns a StreamStack
* whose 'write()' calls are transparently sent to a 'gzip' process before
* being written to the target stream.
*/
var GzipEncode = module.exports = function GzipEncode(options) {
ResponseStream.call(this, options);
if (compression) {
process.assert(compression >= 1 && compression <= 9);
this.compression = compression;
}
this.on('pipe', this.encode);
}
util.inherits(GzipEncode, ResponseStream);
GzipEncode.prototype.encode = function (source) {
this.source = source;
};
GzipEncode.prototype.pipe = function (dest) {
if (!this.source) {
throw new Error('GzipEncode is only pipeable once it has been piped to');
}
this.encoder = spawn('gzip', ['-'+this.compression]);
this.encoder.stdout.pipe(dest);
this.encoder.stdin.pipe(this.source);
};
inherits(GzipEncoderStack, StreamStack);
exports.GzipEncoderStack = GzipEncoderStack;
GzipEncoderStack.prototype.compression = 6; |