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