// @generated by scripts/build-runtime.mjs; do not edit. // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader. import { snapshotSelectionInclude } from "./chunk-YQ6UW7IF.ts"; // src/snapshot-codec.ts import { Readable, Writable } from "node:stream"; import { pipeline } from "node:stream/promises"; import { promisify } from "node:util"; import { createGunzip, gzip } from "node:zlib"; var VERSION = 1; var MAX_DECOMPRESSED_SNAPSHOT_BYTES = 512 * 1024 * 1024; var gzipAsync = promisify(gzip); async function encodeSnapshot(snapshot) { snapshotSelectionInclude(snapshot); return gzipAsync(Buffer.from(JSON.stringify(snapshot), "utf8")); } async function decodeSnapshot(buffer, options = {}) { throwIfAborted(options.signal); const limit = options.maxOutputLength ?? MAX_DECOMPRESSED_SNAPSHOT_BYTES; const chunks = []; let total = 0; const sink = new Writable({ write(chunk, _encoding, callback) { total += chunk.byteLength; if (total > limit) { callback(new Error(`Decompressed snapshot exceeds the ${limit}-byte limit.`)); return; } chunks.push(Buffer.from(chunk)); callback(); } }); await pipeline(Readable.from([buffer]), createGunzip(), sink, { signal: options.signal }); throwIfAborted(options.signal); const parsed = JSON.parse(Buffer.concat(chunks, total).toString("utf8")); if (parsed.version !== VERSION || !Array.isArray(parsed.files)) { throw new Error("Unsupported snapshot format."); } snapshotSelectionInclude(parsed); return parsed; } function throwIfAborted(signal) { if (!signal?.aborted) return; throw signal.reason instanceof Error ? signal.reason : new DOMException("The operation was aborted", "AbortError"); } export { encodeSnapshot, decodeSnapshot }; //# sourceMappingURL=chunk-HGV24P4T.ts.map