import { createHash } from 'node:crypto'; export interface ZipStoreEntry { path: string; content: Buffer; } const CRC_TABLE = (() => { const table = new Uint32Array(256); for (let index = 0; index < 256; index += 1) { let value = index; for (let bit = 0; bit < 8; bit += 1) { value = (value & 1) !== 0 ? 0xedb88320 ^ (value >>> 1) : value >>> 1; } table[index] = value >>> 0; } return table; })(); function crc32(buffer: Buffer): number { let crc = 0xffffffff; for (const byte of buffer) crc = CRC_TABLE[(crc ^ byte) & 0xff]! ^ (crc >>> 8); return (crc ^ 0xffffffff) >>> 0; } function normalizedPath(path: string): string { const value = path.replaceAll('\\', '/'); if ( !value || value.startsWith('/') || value.split('/').some((segment) => !segment || segment === '.' || segment === '..') ) throw new Error('zip_store_path_invalid'); return value; } /** Deterministic uncompressed ZIP, sufficient for Hub Skill packages. */ export function createStoredZip(entries: readonly ZipStoreEntry[]): Buffer { const sorted = [...entries] .map((entry) => ({ path: normalizedPath(entry.path), content: Buffer.from(entry.content) })) .sort((left, right) => left.path.localeCompare(right.path)); if (sorted.length === 0 || sorted.length > 0xffff) throw new Error('zip_store_entry_count_invalid'); const localParts: Buffer[] = []; const centralParts: Buffer[] = []; let offset = 0; for (const entry of sorted) { const name = Buffer.from(entry.path, 'utf8'); const checksum = crc32(entry.content); const local = Buffer.alloc(30); local.writeUInt32LE(0x04034b50, 0); local.writeUInt16LE(20, 4); local.writeUInt16LE(0x0800, 6); local.writeUInt16LE(0, 8); local.writeUInt32LE(checksum, 14); local.writeUInt32LE(entry.content.length, 18); local.writeUInt32LE(entry.content.length, 22); local.writeUInt16LE(name.length, 26); localParts.push(local, name, entry.content); const central = Buffer.alloc(46); central.writeUInt32LE(0x02014b50, 0); central.writeUInt16LE(20, 4); central.writeUInt16LE(20, 6); central.writeUInt16LE(0x0800, 8); central.writeUInt16LE(0, 10); central.writeUInt32LE(checksum, 16); central.writeUInt32LE(entry.content.length, 20); central.writeUInt32LE(entry.content.length, 24); central.writeUInt16LE(name.length, 28); central.writeUInt32LE(offset, 42); centralParts.push(central, name); offset += local.length + name.length + entry.content.length; } const centralDirectory = Buffer.concat(centralParts); const end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50, 0); end.writeUInt16LE(sorted.length, 8); end.writeUInt16LE(sorted.length, 10); end.writeUInt32LE(centralDirectory.length, 12); end.writeUInt32LE(offset, 16); return Buffer.concat([...localParts, centralDirectory, end]); } export function zipContentDigest(entries: readonly ZipStoreEntry[]): string { const hash = createHash('sha256'); for (const entry of [...entries].sort((left, right) => left.path.localeCompare(right.path))) { hash.update(normalizedPath(entry.path)); hash.update('\0'); hash.update(entry.content); hash.update('\0'); } return hash.digest('hex'); }