/** * A ZIP written as a stream: every entry STORED (the bundle's media are * already compressed — EPUB, MP4, JPEG, FLAC — and its text is small), each * entry's CRC-32 worked out while its bytes pass and said after them in a data * descriptor, so no entry is held whole beyond the chunk in hand. * * Stored entries make the archive's length known before a byte is written * (`zipLength`): R2 takes the whole as one fixed-length stream, and a cap is * refused before anything is stored. Plain ZIP (no ZIP64): at most 65,535 * entries and 4 GiB, past which `zipLength` refuses. *//** One file of the archive: its name (`/`-separated, UTF-8), its length, and its bytes. */exportinterfaceZipEntry { name: string; size: number;/** The bytes, whole (a small text file) or as a stream read once. */ bytes: Uint8Array | (() => Promise<ReadableStream<Uint8Array>>);}const LOCAL = 30;const DESCRIPTOR = 16;const CENTRAL = 46;const END = 22;/** Bit 3: sizes and CRC follow the data. Bit 11: the name is UTF-8. */const FLAGS = 0x0808;exportconst ZIP_MAX_ENTRIES = 0xffff;exportconst ZIP_MAX_BYTES = 0xffffffff;const encoder = newTextEncoder();/** The archive's length in bytes, or why it cannot be written as plain ZIP. */exportfunction zipLength(entries: readonly { name: string; size: number }[]): number {if (entries.length > ZIP_MAX_ENTRIES) thrownewRangeError(`${entries.length} files is more than a ZIP holds (${ZIP_MAX_ENTRIES})`);let total = END;for (const e of entries) {const name = encoder.encode(e.name).byteLength; total += LOCAL + name + e.size + DESCRIPTOR + CENTRAL + name; }return total;}const CRC_TABLE = (() => {const table = newUint32Array(256);for (let n = 0; n < 256; n++) {let c = n;for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; table[n] = c >>> 0; }return table;})();/** CRC-32 (IEEE) carried across chunks: start at 0, pass each chunk, read the last answer. */exportfunction crc32(chunk: Uint8Array, crc = 0): number {let c = ~crc >>> 0;for (let i = 0; i < chunk.length; i++) c = CRC_TABLE[(c ^ chunk[i]!) & 0xff]! ^ (c >>> 8);return ~c >>> 0;}/** MS-DOS time and date of an instant, as a ZIP says when its files were made (UTC). */function dosTime(at: Date): { time: number; date: number } {const year = Math.max(1980, at.getUTCFullYear());return { time: (at.getUTCHours() << 11) | (at.getUTCMinutes() << 5) | Math.floor(at.getUTCSeconds() / 2), date: ((year - 1980) << 9) | ((at.getUTCMonth() + 1) << 5) | at.getUTCDate(), };}/** * Write `entries` as one ZIP through `write`, each chunk awaited before the * next is read. A stream that ends short of or past its entry's `size` is * refused: the archive's length was promised. */exportasyncfunction writeZip(entries: readonlyZipEntry[], at: Date, write: (chunk: Uint8Array) => Promise<void>): Promise<void> {const { time, date } = dosTime(at);const central: Uint8Array[] = [];let offset = 0;const emit = async (chunk: Uint8Array): Promise<void> => { offset += chunk.byteLength;await write(chunk); };for (const entry of entries) {const name = encoder.encode(entry.name);const start = offset;const local = newDataView(newArrayBuffer(LOCAL)); local.setUint32(0, 0x04034b50, true); local.setUint16(4, 20, true); local.setUint16(6, FLAGS, true); local.setUint16(8, 0, true); local.setUint16(10, time, true); local.setUint16(12, date, true);// CRC and sizes are zero here and said in the descriptor (bit 3). local.setUint16(26, name.byteLength, true);await emit(newUint8Array(local.buffer));await emit(name);let crc = 0;let size = 0;if (entry.bytes instanceofUint8Array) { crc = crc32(entry.bytes); size = entry.bytes.byteLength;await emit(entry.bytes); } else {const reader = (await entry.bytes()).getReader();for (;;) {const { done, value } = await reader.read();if (done) break; crc = crc32(value, crc); size += value.byteLength;if (size > entry.size) {await reader.cancel();thrownewError(`${entry.name} is longer than the ${entry.size} bytes it was said to be`); }await emit(value); } }if (size !== entry.size) thrownewError(`${entry.name} is ${size} bytes, not the ${entry.size} it was said to be`);const descriptor = newDataView(newArrayBuffer(DESCRIPTOR)); descriptor.setUint32(0, 0x08074b50, true); descriptor.setUint32(4, crc, true); descriptor.setUint32(8, size, true); descriptor.setUint32(12, size, true);await emit(newUint8Array(descriptor.buffer));const record = newDataView(newArrayBuffer(CENTRAL)); record.setUint32(0, 0x02014b50, true); record.setUint16(4, 20, true); record.setUint16(6, 20, true); record.setUint16(8, FLAGS, true); record.setUint16(10, 0, true); record.setUint16(12, time, true); record.setUint16(14, date, true); record.setUint32(16, crc, true); record.setUint32(20, size, true); record.setUint32(24, size, true); record.setUint16(28, name.byteLength, true); record.setUint32(42, start, true); central.push(newUint8Array(record.buffer), name); }const directoryAt = offset;for (const part of central) await emit(part);const end = newDataView(newArrayBuffer(END)); end.setUint32(0, 0x06054b50, true); end.setUint16(8, entries.length, true); end.setUint16(10, entries.length, true); end.setUint32(12, offset - directoryAt, true); end.setUint32(16, directoryAt, true);await emit(newUint8Array(end.buffer));}