file_stream.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. const tar = require('../tar');
  3. const gzip = require('../gzip');
  4. const utils = require('../utils');
  5. const stream = require('stream');
  6. const pump = require('pump');
  7. const ready = require('get-ready');
  8. class TgzFileStream extends stream.Transform {
  9. constructor(opts) {
  10. opts = opts || {};
  11. super(opts);
  12. const sourceType = this._sourceType = utils.sourceType(opts.source);
  13. const tarStream = this._tarStream = new tar.FileStream(opts);
  14. opts = utils.clone(opts);
  15. delete opts.source;
  16. const gzipStream = new gzip.FileStream(opts);
  17. gzipStream.on('data', chunk => {
  18. this.push(chunk);
  19. });
  20. gzipStream.on('end', () => this.ready(true));
  21. pump(tarStream, gzipStream, err => {
  22. err && this.emit('error', err);
  23. });
  24. if (sourceType !== 'stream' && sourceType !== undefined) {
  25. this.end();
  26. }
  27. }
  28. _transform(chunk, encoding, callback) {
  29. this._tarStream.write(chunk, encoding, callback);
  30. }
  31. _flush(callback) {
  32. if (this._sourceType === 'stream' || this._sourceType === undefined) {
  33. this._tarStream.end();
  34. }
  35. this.ready(callback);
  36. }
  37. }
  38. ready.mixin(TgzFileStream.prototype);
  39. module.exports = TgzFileStream;