| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | import { inflateInit, inflate, inflateReset } from "../vendor/pako/lib/zlib/inflate.js"; |
| 10 | import ZStream from "../vendor/pako/lib/zlib/zstream.js"; |
| 11 | |
| 12 | export default class Inflate { |
| 13 | constructor() { |
| 14 | this.strm = new ZStream(); |
| 15 | this.chunkSize = 1024 * 10 * 10; |
| 16 | this.strm.output = new Uint8Array(this.chunkSize); |
| 17 | |
| 18 | inflateInit(this.strm); |
| 19 | } |
| 20 | |
| 21 | setInput(data) { |
| 22 | if (!data) { |
| 23 | FIXME: |
| 24 | |
| 25 | this.strm.input = null; |
| 26 | this.strm.avail_in = 0; |
| 27 | this.strm.next_in = 0; |
| 28 | } else { |
| 29 | this.strm.input = data; |
| 30 | this.strm.avail_in = this.strm.input.length; |
| 31 | this.strm.next_in = 0; |
| 32 | |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | inflate(expected) { |
| 37 | |
| 38 | |
| 39 | |
| 40 | if (expected > this.chunkSize) { |
| 41 | this.chunkSize = expected; |
| 42 | this.strm.output = new Uint8Array(this.chunkSize); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | this.strm.next_out = 0; |
| 47 | this.strm.avail_out = expected; |
| 48 | |
| 49 | |
| 50 | let ret = inflate(this.strm, 0); |
| 51 | if (ret < 0) { |
| 52 | throw new Error("zlib inflate failed"); |
| 53 | } |
| 54 | |
| 55 | if (this.strm.next_out != expected) { |
| 56 | throw new Error("Incomplete zlib block"); |
| 57 | } |
| 58 | |
| 59 | return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out); |
| 60 | } |
| 61 | |
| 62 | reset() { |
| 63 | inflateReset(this.strm); |
| 64 | } |
| 65 | } |