Diffinery

codician/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

33 lines · 1.4 KBRaw History
1import pako from "pako";
2
3/** zlib-deflate (RFC 1950), the format git uses for loose objects and pack entries. */
4export function deflate(data: Uint8Array): Uint8Array {
5 return pako.deflate(data);
6}
7
8export function inflate(data: Uint8Array): Uint8Array {
9 return pako.inflate(data);
10}
11
12/**
13 * Inflate one zlib stream that starts at `offset` inside `data`, and report how
14 * many input bytes it consumed. Pack files concatenate zlib streams without
15 * lengths, so the consumed count is how we find the next entry.
16 */
17export function inflateAt(data: Uint8Array, offset: number, expectedSize?: number): { out: Uint8Array; consumed: number } {
18 const inflator = new pako.Inflate() as pako.Inflate & { ended: boolean };
19 // Feed in bounded windows so a huge trailing buffer is not copied repeatedly.
20 const window = Math.max(64 * 1024, expectedSize ? expectedSize + 1024 : 0);
21 let pos = offset;
22 while (!inflator.ended && pos < data.length) {
23 const end = Math.min(data.length, pos + window);
24 inflator.push(data.subarray(pos, end), false);
25 if (inflator.err) throw new Error(`zlib error at ${offset}: ${inflator.msg}`);
26 pos = end;
27 }
28 if (!inflator.ended) throw new Error(`truncated zlib stream at offset ${offset}`);
29 const strm = (inflator as unknown as { strm: { avail_in: number } }).strm;
30 const consumed = pos - offset - strm.avail_in;
31 const out = inflator.result as Uint8Array;
32 return { out: out ?? new Uint8Array(0), consumed };
33}