inflator.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { inflateInit, inflate, inflateReset } from "../vendor/pako/lib/zlib/inflate.js";
  2. import ZStream from "../vendor/pako/lib/zlib/zstream.js";
  3. export default class Inflate {
  4. constructor() {
  5. this.strm = new ZStream();
  6. this.chunkSize = 1024 * 10 * 10;
  7. this.strm.output = new Uint8Array(this.chunkSize);
  8. this.windowBits = 5;
  9. inflateInit(this.strm, this.windowBits);
  10. }
  11. inflate(data, flush, expected) {
  12. this.strm.input = data;
  13. this.strm.avail_in = this.strm.input.length;
  14. this.strm.next_in = 0;
  15. this.strm.next_out = 0;
  16. // resize our output buffer if it's too small
  17. // (we could just use multiple chunks, but that would cause an extra
  18. // allocation each time to flatten the chunks)
  19. if (expected > this.chunkSize) {
  20. this.chunkSize = expected;
  21. this.strm.output = new Uint8Array(this.chunkSize);
  22. }
  23. this.strm.avail_out = this.chunkSize;
  24. inflate(this.strm, flush);
  25. return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
  26. }
  27. reset() {
  28. inflateReset(this.strm);
  29. }
  30. }