ZlibHeaderTransformStream.js 681 B

12345678910111213141516171819202122232425262728
  1. "use strict";
  2. import stream from "stream";
  3. class ZlibHeaderTransformStream extends stream.Transform {
  4. __transform(chunk, encoding, callback) {
  5. this.push(chunk);
  6. callback();
  7. }
  8. _transform(chunk, encoding, callback) {
  9. if (chunk.length !== 0) {
  10. this._transform = this.__transform;
  11. // Add Default Compression headers if no zlib headers are present
  12. if (chunk[0] !== 120) { // Hex: 78
  13. const header = Buffer.alloc(2);
  14. header[0] = 120; // Hex: 78
  15. header[1] = 156; // Hex: 9C
  16. this.push(header, encoding);
  17. }
  18. }
  19. this.__transform(chunk, encoding, callback);
  20. }
  21. }
  22. export default ZlibHeaderTransformStream;