1import { gzipSync } from "node:zlib";23export type TarFile = { path: string; bytes: Uint8Array; executable?: boolean };45678const STRIPPED = new Set([9 "koh.build",10 "kepr.build",11 "Cargo.lock",12 "package-lock.json",13 "yarn.lock",14 "pnpm-lock.yaml",15 "bun.lockb",16 "go.sum",17 "LOG.md",18 "CONTINUITY.md",19 "AGENTS.md",20 ".kohignore",21 ".gitignore",22]);2324export function isStripped(path: string): boolean {25 return STRIPPED.has(path) || path === ".agent" || path.startsWith(".agent/");26}2728const encoder = new TextEncoder();29const BLOCK = 512;30313233343536export function buildTarball(37 root: string,38 files: TarFile[],39 mode?: number,40): Uint8Array<ArrayBuffer> {41 const entries = files42 .filter((file) => file.path !== "" && !isStripped(file.path))43 .map((file) => ({ ...file, name: encoder.encode(`${root}/${file.path}`) }))44 .sort((a, b) => Buffer.compare(a.name, b.name));45 const blocks: Uint8Array[] = [header(encoder.encode(root), 0, true)];46 for (const entry of entries) {47 blocks.push(48 header(entry.name, entry.bytes.length, false, mode ?? (entry.executable ? 0o755 : 0o644)),49 );50 blocks.push(entry.bytes, new Uint8Array((BLOCK - (entry.bytes.length % BLOCK)) % BLOCK));51 }52 blocks.push(new Uint8Array(2 * BLOCK));53 const gzip = gzipSync(Buffer.concat(blocks), { level: 6 });54 55 gzip[9] = 3;56 return gzip;57}585960616263export function streamTarball(64 root: string,65 files: { path: string; full: string }[],66 mode: number,67): ReadableStream<Uint8Array> {68 const entries = files69 .filter((file) => file.path !== "" && !isStripped(file.path))70 .map((file) => ({ ...file, name: encoder.encode(`${root}/${file.path}`) }))71 .sort((a, b) => Buffer.compare(a.name, b.name));72 const gzip = new CompressionStream("gzip");73 void (async () => {74 const writer = gzip.writable.getWriter();75 try {76 await writer.write(header(encoder.encode(root), 0, true));77 for (const entry of entries) {78 const file = Bun.file(entry.full);79 await writer.write(header(entry.name, file.size, false, mode));80 for await (const chunk of file.stream()) await writer.write(chunk);81 await writer.write(new Uint8Array((BLOCK - (file.size % BLOCK)) % BLOCK));82 }83 await writer.write(new Uint8Array(2 * BLOCK));84 await writer.close();85 } catch (error) {86 await writer.abort(error);87 }88 })();89 return gzip.readable;90}91929394function header(95 name: Uint8Array,96 size: number,97 isDir: boolean,98 mode = 0o644,99): Uint8Array<ArrayBuffer> {100 const h = new Uint8Array(BLOCK);101 const maxName = isDir ? 98 : 99;102 let base = name;103 if (name.length > maxName) {104 const split = splitAt(name, maxName);105 if (split === null)106 throw new Error(`path too long for a tarball: ${new TextDecoder().decode(name)}`);107 h.set(name.subarray(0, split), 345);108 base = name.subarray(split + 1);109 }110 h.set(base);111 if (isDir) h[base.length] = 0x2f;112 const ascii = (text: string, at: number) => h.set(encoder.encode(text), at);113 ascii(isDir ? "0000755\0" : `${mode.toString(8).padStart(7, "0")}\0`, 100);114 ascii("0000000\0", 108);115 ascii("0000000\0", 116);116 if (!isDir) ascii(`${size.toString(8).padStart(11, "0")}\0`, 124);117 ascii("00000000000\0", 136);118 ascii(" ", 148);119 ascii(isDir ? "5" : "0", 156);120 ascii("ustar\0", 257);121 ascii("00", 263);122 const sum = h.reduce((total, byte) => total + byte, 0);123 ascii(`${sum.toString(8).padStart(6, "0")}\0 `, 148);124 return h;125}126127128function splitAt(path: Uint8Array, maxName: number): number | null {129 const min = path.length - maxName - 1;130 for (let i = Math.min(path.length - 1, 154); i >= Math.max(min, 0); i--) {131 if (path[i] === 0x2f) return i;132 }133 return null;134}