| 1 | import pako from "pako"; |
| 2 | |
| 3 | /** zlib-deflate (RFC 1950), the format git uses for loose objects and pack entries. */ |
| 4 | export function deflate(data: Uint8Array): Uint8Array { |
| 5 | return pako.deflate(data); |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * The same zlib data as `deflate`, piece by piece as it is produced, so compressing a large object |
| 10 | * holds it and a few small chunks, not a second copy of it compressed. Each chunk is new. |
| 11 | */ |
| 12 | export function* deflateChunks(data: Uint8Array): Generator<Uint8Array> { |
| 13 | const d = new pako.Deflate(); |
| 14 | let out: Uint8Array[] = []; |
| 15 | d.onData = (chunk) => void out.push(chunk as Uint8Array); |
| 16 | const step = 256 * 1024; |
| 17 | for (let at = 0; ; at += step) { |
| 18 | const last = at + step >= data.length; |
| 19 | d.push(data.subarray(at, Math.min(data.length, at + step)), last); |
| 20 | if (d.err) throw new Error(`zlib error: ${d.msg}`); |
| 21 | yield* out; |
| 22 | out = []; |
| 23 | if (last) return; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // zlib only: pako also takes gzip unless `windowBits` is given, and git can't read gzip. |
| 28 | const ZLIB = { windowBits: 15 }; |
| 29 | |
| 30 | export function inflate(data: Uint8Array): Uint8Array { |
| 31 | return pako.inflate(data, ZLIB); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Inflate zlib data known to hold exactly `size` bytes, into one buffer of that size: no chunks |
| 36 | * joined at the end, so it never needs twice the object. |
| 37 | */ |
| 38 | export function inflateSized(data: Uint8Array, size: number): Uint8Array { |
| 39 | const inflator = new pako.Inflate(ZLIB) as pako.Inflate & { ended: boolean }; |
| 40 | const out = new Uint8Array(size); |
| 41 | let produced = 0; |
| 42 | inflator.onData = (chunk) => { |
| 43 | const c = chunk as Uint8Array; |
| 44 | if (produced + c.length > size) throw new Error(`object is larger than its recorded size (${size} bytes)`); |
| 45 | out.set(c, produced); |
| 46 | produced += c.length; |
| 47 | }; |
| 48 | inflator.push(data, true); |
| 49 | if (inflator.err) throw new Error(`zlib error: ${inflator.msg}`); |
| 50 | if (!inflator.ended || produced !== size) throw new Error(`object inflated to ${produced} bytes, not its recorded ${size}`); |
| 51 | return out; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Inflate one zlib stream that starts at `offset` inside `data`, and report how |
| 56 | * many input bytes it consumed. Pack files concatenate zlib streams without |
| 57 | * lengths, so the consumed count is how we find the next entry. |
| 58 | */ |
| 59 | export function inflateAt(data: Uint8Array, offset: number, expectedSize?: number): { out: Uint8Array; consumed: number } { |
| 60 | const inflator = new pako.Inflate(ZLIB) as pako.Inflate & { ended: boolean }; |
| 61 | if (expectedSize !== undefined) { |
| 62 | // Stop as soon as the output passes the size the pack declared, so a small zlib stream |
| 63 | // can't be made to inflate to gigabytes. |
| 64 | const keep = inflator.onData.bind(inflator); |
| 65 | let produced = 0; |
| 66 | inflator.onData = (chunk) => { |
| 67 | produced += (chunk as Uint8Array).length; |
| 68 | if (produced > expectedSize) throw new Error(`object at ${offset} is larger than its declared size (${expectedSize} bytes)`); |
| 69 | keep(chunk); |
| 70 | }; |
| 71 | } |
| 72 | // Feed in bounded windows so a huge trailing buffer is not copied repeatedly. |
| 73 | const window = Math.max(64 * 1024, expectedSize ? expectedSize + 1024 : 0); |
| 74 | let pos = offset; |
| 75 | while (!inflator.ended && pos < data.length) { |
| 76 | const end = Math.min(data.length, pos + window); |
| 77 | inflator.push(data.subarray(pos, end), false); |
| 78 | if (inflator.err) throw new Error(`zlib error at ${offset}: ${inflator.msg}`); |
| 79 | pos = end; |
| 80 | } |
| 81 | if (!inflator.ended) throw new Error(`truncated zlib stream at offset ${offset}`); |
| 82 | const strm = (inflator as unknown as { strm: { avail_in: number } }).strm; |
| 83 | const consumed = pos - offset - strm.avail_in; |
| 84 | const out = inflator.result as Uint8Array; |
| 85 | return { out: out ?? new Uint8Array(0), consumed }; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Like `inflateAt`, but hands each inflated chunk to `onChunk` instead of keeping it, so an object |
| 90 | * much larger than memory allows can be hashed or scanned. Returns the input bytes consumed and the |
| 91 | * bytes produced. |
| 92 | */ |
| 93 | export function inflateEach(data: Uint8Array, offset: number, onChunk: (chunk: Uint8Array) => void, expectedSize?: number): { consumed: number; produced: number } { |
| 94 | const inflator = new pako.Inflate({ ...ZLIB, chunkSize: 256 * 1024 }) as pako.Inflate & { ended: boolean }; |
| 95 | let produced = 0; |
| 96 | inflator.onData = (chunk) => { |
| 97 | const c = chunk as Uint8Array; |
| 98 | produced += c.length; |
| 99 | if (expectedSize !== undefined && produced > expectedSize) throw new Error(`object at ${offset} is larger than its declared size (${expectedSize} bytes)`); |
| 100 | onChunk(c); |
| 101 | }; |
| 102 | const window = 1024 * 1024; |
| 103 | let pos = offset; |
| 104 | while (!inflator.ended && pos < data.length) { |
| 105 | const end = Math.min(data.length, pos + window); |
| 106 | inflator.push(data.subarray(pos, end), false); |
| 107 | if (inflator.err) throw new Error(`zlib error at ${offset}: ${inflator.msg}`); |
| 108 | pos = end; |
| 109 | } |
| 110 | if (!inflator.ended) throw new Error(`truncated zlib stream at offset ${offset}`); |
| 111 | const strm = (inflator as unknown as { strm: { avail_in: number } }).strm; |
| 112 | return { consumed: pos - offset - strm.avail_in, produced }; |
| 113 | } |