uncompress_stream.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. const fs = require('fs');
  3. const utils = require('../utils');
  4. const ready = require('get-ready');
  5. const streamifier = require('streamifier');
  6. const FlushWritable = require('flushwritable');
  7. const GzipUncompressStream = require('../gzip').UncompressStream;
  8. const TarUncompressStream = require('../tar').UncompressStream;
  9. class TgzUncompressStream extends FlushWritable {
  10. constructor(opts) {
  11. opts = opts || {};
  12. super(opts);
  13. const newOpts = utils.clone(opts);
  14. newOpts.source = undefined;
  15. this._gzipStream = new GzipUncompressStream(newOpts)
  16. .on('error', err => this.emit('error', err));
  17. const tarStream = new TarUncompressStream(newOpts)
  18. .on('finish', () => this.ready(true))
  19. .on('entry', this.emit.bind(this, 'entry'))
  20. .on('error', err => this.emit('error', err));
  21. this._gzipStream.pipe(tarStream);
  22. const sourceType = utils.sourceType(opts.source);
  23. if (sourceType === 'file') {
  24. const stream = fs.createReadStream(opts.source, opts.fs);
  25. stream.on('error', err => this.emit('error', err));
  26. stream.pipe(this);
  27. return;
  28. }
  29. if (sourceType === 'buffer') {
  30. const stream = streamifier.createReadStream(opts.source, opts.streamifier);
  31. stream.on('error', err => this.emit('error', err));
  32. stream.pipe(this);
  33. return;
  34. }
  35. if (sourceType === 'stream') {
  36. opts.source.on('error', err => this.emit('error', err));
  37. opts.source.pipe(this);
  38. }
  39. // else: waiting to be piped
  40. }
  41. _write(chunk, encoding, callback) {
  42. this._gzipStream.write(chunk, encoding, callback);
  43. }
  44. _flush(callback) {
  45. this._gzipStream.end();
  46. this.ready(callback);
  47. }
  48. }
  49. ready.mixin(TgzUncompressStream.prototype);
  50. module.exports = TgzUncompressStream;