The best way to control Claude Code, Codex and more from any computer or phone

main
ts 90 lines 3.41 KB
Raw
1 /**
2 * Deterministically builds minimal, structurally valid zip archives —
3 * enough for CreateApplication's ZIPFILE code validation (which requires a
4 * `.jar` entry inside the uploaded zip) without a real Flink build. The
5 * application is never started in ungated tests; a start with this
6 * placeholder would fail job submission.
7 */
8
9 const crc32 = (data: Uint8Array): number => {
10 let crc = 0xffffffff;
11 for (let i = 0; i < data.length; i++) {
12 crc ^= data[i]!;
13 for (let j = 0; j < 8; j++) {
14 crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1));
15 }
16 }
17 return (crc ^ 0xffffffff) >>> 0;
18 };
19
20 interface ZipEntry {
21 readonly name: string;
22 readonly data: Uint8Array | string;
23 }
24
25 export const makeZip = (entries: ReadonlyArray<ZipEntry>): Uint8Array => {
26 const encoder = new TextEncoder();
27 const chunks: Uint8Array[] = [];
28 const centralChunks: Uint8Array[] = [];
29 let offset = 0;
30
31 for (const entry of entries) {
32 const name = encoder.encode(entry.name);
33 const body = typeof entry.data === "string" ? encoder.encode(entry.data) : entry.data;
34 const crc = crc32(body);
35
36 const localHeader = new Uint8Array(30 + name.length);
37 const localView = new DataView(localHeader.buffer);
38 localView.setUint32(0, 0x04034b50, true); // local file header signature
39 localView.setUint16(4, 20, true); // version needed to extract
40 localView.setUint16(8, 0, true); // compression method: STORED
41 localView.setUint32(14, crc, true);
42 localView.setUint32(18, body.length, true); // compressed size
43 localView.setUint32(22, body.length, true); // uncompressed size
44 localView.setUint16(26, name.length, true);
45 localHeader.set(name, 30);
46
47 const centralHeader = new Uint8Array(46 + name.length);
48 const centralView = new DataView(centralHeader.buffer);
49 centralView.setUint32(0, 0x02014b50, true); // central directory signature
50 centralView.setUint16(4, 20, true); // version made by
51 centralView.setUint16(6, 20, true); // version needed to extract
52 centralView.setUint16(10, 0, true); // compression method: STORED
53 centralView.setUint32(16, crc, true);
54 centralView.setUint32(20, body.length, true);
55 centralView.setUint32(24, body.length, true);
56 centralView.setUint16(28, name.length, true);
57 centralView.setUint32(42, offset, true); // local header offset
58 centralHeader.set(name, 46);
59
60 chunks.push(localHeader, body);
61 centralChunks.push(centralHeader);
62 offset += localHeader.length + body.length;
63 }
64
65 const centralSize = centralChunks.reduce((n, c) => n + c.length, 0);
66 const eocd = new Uint8Array(22);
67 const eocdView = new DataView(eocd.buffer);
68 eocdView.setUint32(0, 0x06054b50, true); // end of central directory
69 eocdView.setUint16(8, entries.length, true); // entries on this disk
70 eocdView.setUint16(10, entries.length, true); // total entries
71 eocdView.setUint32(12, centralSize, true);
72 eocdView.setUint32(16, offset, true);
73
74 const zip = new Uint8Array(offset + centralSize + eocd.length);
75 let cursor = 0;
76 for (const chunk of [...chunks, ...centralChunks, eocd]) {
77 zip.set(chunk, cursor);
78 cursor += chunk.length;
79 }
80 return zip;
81 };
82
83 /**
84 * A zip whose single entry is `app.jar` — itself a structurally valid
85 * (empty) jar with a manifest.
86 */
87 export const makeDummyFlinkCodeZip = (): Uint8Array => {
88 const jar = makeZip([{ name: "META-INF/MANIFEST.MF", data: "Manifest-Version: 1.0\r\n\r\n" }]);
89 return makeZip([{ name: "app.jar", data: jar }]);
90 };