inflate-stream.js 967 B

123456789101112131415161718192021222324252627282930313233343536
  1. var Inflate = require('pako').Inflate;
  2. var Binary = require('bodec').Binary;
  3. // Byte oriented inflate stream. Wrapper for pako's Inflate.
  4. //
  5. // var inf = inflate();
  6. // inf.write(byte) -> more - Write a byte to inflate's state-machine.
  7. // Returns true if more data is expected.
  8. // inf.recycle() - Reset the internal state machine.
  9. // inf.flush() -> data - Flush the output as a binary buffer.
  10. //
  11. module.exports = function inflateStream() {
  12. var inf = new Inflate();
  13. var b = new Uint8Array(1);
  14. var empty = new Binary(0);
  15. return {
  16. write: write,
  17. recycle: recycle,
  18. flush: Binary === Uint8Array ? flush : flushConvert
  19. };
  20. function write(byte) {
  21. b[0] = byte;
  22. inf.push(b);
  23. return !inf.ended;
  24. }
  25. function recycle() { inf = new Inflate(); }
  26. function flush() { return inf.result || empty; }
  27. function flushConvert() {
  28. return inf.result ? new Binary(inf.result) : empty;
  29. }
  30. };