cache_stream.js 425 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. const { Transform } = require('stream');
  3. class CacheStream extends Transform {
  4. constructor() {
  5. super();
  6. this._cache = [];
  7. }
  8. _transform(chunk, enc, callback) {
  9. const buf = chunk instanceof Buffer ? chunk : Buffer.from(chunk, enc);
  10. this._cache.push(buf);
  11. this.push(buf);
  12. callback();
  13. }
  14. getCache() {
  15. return Buffer.concat(this._cache);
  16. }
  17. }
  18. module.exports = CacheStream;