// Bytes a vendor's wire carries compressed, as a handler reads them. Web-standard compression streams where the runtime // has them (workerd, the browser, Node), so the kernel's root keeps no static import of Node's zlib — a browser client // imports this module too. Bun has neither `CompressionStream` nor `DecompressionStream` (Bun 1.2.20: // `bun -e "typeof CompressionStream"` prints `undefined`), and a local World is served by Bun, so there the builtin is // reached the way the kernel reaches every builtin: `process.getBuiltinModule`, at call time, never at import (the seam // serve-http.ts's `nodeBuiltin` uses). import { DEFAULT_MAX_DECODED_REQUEST_BODY_SIZE, RequestBodyTooLargeError } from './request-body.ts'; type Decode = (b: Uint8Array, options: { maxOutputLength: number }, callback: (error: (Error & { code?: string }) | null, bytes: Uint8Array) => void) => void; type Zlib = { gzipSync(b: Uint8Array): Uint8Array; gunzipSync(b: Uint8Array): Uint8Array; gunzip: Decode; inflate: Decode; brotliDecompress: Decode; zstdDecompress: Decode; deflateSync(b: Uint8Array): Uint8Array; }; function zlib(what: string): Zlib { const get = (globalThis as { process?: { getBuiltinModule?: (id: string) => unknown } }).process?.getBuiltinModule; if (typeof get !== 'function') throw new Error(`${what}: this runtime has neither the compression streams nor node:zlib (process.getBuiltinModule)`); return get('node:zlib') as Zlib; } async function streamed(bytes: Uint8Array, stream: CompressionStream | DecompressionStream): Promise { const piped = new Response(bytes as unknown as BodyInit).body!.pipeThrough(stream as unknown as TransformStream); return new Uint8Array(await new Response(piped).arrayBuffer()); } /** The bytes of a gzip member (a SARIF upload, a tarball's contents, a gzipped request body). */ export async function gunzip(bytes: Uint8Array): Promise { if (typeof DecompressionStream === 'function') return streamed(bytes, new DecompressionStream('gzip')); return new Uint8Array(zlib('gunzip').gunzipSync(bytes)); } /** Compressed HTTP REQUESTS only: async decoding stops at maxOutputLength during inflation. * Return the decoder's own buffer; the request consumes it as a stream without copying it again. */ export async function decompress(bytes: Uint8Array, coding: 'gzip' | 'deflate' | 'br' | 'zstd', limit = DEFAULT_MAX_DECODED_REQUEST_BODY_SIZE): Promise { const builtin = zlib('decompress'); const decode = { gzip: builtin.gunzip, deflate: builtin.inflate, br: builtin.brotliDecompress, zstd: builtin.zstdDecompress }[coding]; return new Promise((resolve, reject) => { decode(bytes, { maxOutputLength: limit }, (error, body) => { if (error) reject(error.code === 'ERR_BUFFER_TOO_LARGE' ? new RequestBodyTooLargeError() : error); else if (body.byteLength > limit) reject(new RequestBodyTooLargeError()); else resolve(body); }); }); } /** Bytes as a gzip member. */ export async function gzip(bytes: Uint8Array): Promise { if (typeof CompressionStream === 'function') return streamed(bytes, new CompressionStream('gzip')); return new Uint8Array(zlib('gzip').gzipSync(bytes)); } /** Bytes as a zlib (deflate) member: git's object and pack framing. Reading one back is git/inflate.ts, which also * reports how many bytes it consumed, since a packfile's objects sit one after another with no length. */ export async function deflate(bytes: Uint8Array): Promise { if (typeof CompressionStream === 'function') return streamed(bytes, new CompressionStream('deflate')); return new Uint8Array(zlib('deflate').deflateSync(bytes)); }