You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

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