@poncho-ai/cli 0.38.0 → 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/vfs-zip.ts ADDED
@@ -0,0 +1,94 @@
1
+ // Minimal "store" (no-compression) zip writer. Used by the VFS folder-download
2
+ // endpoint. Avoids pulling in a zip dependency.
3
+
4
+ const CRC_TABLE: Uint32Array = (() => {
5
+ const table = new Uint32Array(256);
6
+ for (let i = 0; i < 256; i++) {
7
+ let c = i;
8
+ for (let k = 0; k < 8; k++) c = (c & 1) !== 0 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
9
+ table[i] = c >>> 0;
10
+ }
11
+ return table;
12
+ })();
13
+
14
+ const crc32 = (data: Uint8Array): number => {
15
+ let crc = 0xffffffff;
16
+ for (let i = 0; i < data.length; i++) crc = (CRC_TABLE[(crc ^ data[i]!) & 0xff]! ^ (crc >>> 8)) >>> 0;
17
+ return (crc ^ 0xffffffff) >>> 0;
18
+ };
19
+
20
+ const dosDateTime = (date: Date): { time: number; day: number } => {
21
+ const time = ((date.getHours() & 0x1f) << 11) | ((date.getMinutes() & 0x3f) << 5) | (Math.floor(date.getSeconds() / 2) & 0x1f);
22
+ const year = Math.max(1980, date.getFullYear());
23
+ const day = (((year - 1980) & 0x7f) << 9) | (((date.getMonth() + 1) & 0x0f) << 5) | (date.getDate() & 0x1f);
24
+ return { time, day };
25
+ };
26
+
27
+ export interface ZipEntry {
28
+ /** POSIX-style relative path (forward slashes). */
29
+ name: string;
30
+ content: Uint8Array;
31
+ mtime?: Date;
32
+ }
33
+
34
+ export const buildZip = (entries: ZipEntry[]): Buffer => {
35
+ const local: Buffer[] = [];
36
+ const central: Buffer[] = [];
37
+ let offset = 0;
38
+
39
+ for (const entry of entries) {
40
+ const nameBuf = Buffer.from(entry.name, "utf8");
41
+ const data = Buffer.from(entry.content);
42
+ const crc = crc32(entry.content);
43
+ const { time, day } = dosDateTime(entry.mtime ?? new Date());
44
+
45
+ const lfh = Buffer.alloc(30);
46
+ lfh.writeUInt32LE(0x04034b50, 0);
47
+ lfh.writeUInt16LE(20, 4); // version needed
48
+ lfh.writeUInt16LE(0x0800, 6); // general purpose flags (bit 11 = UTF-8 name)
49
+ lfh.writeUInt16LE(0, 8); // compression method: store
50
+ lfh.writeUInt16LE(time, 10);
51
+ lfh.writeUInt16LE(day, 12);
52
+ lfh.writeUInt32LE(crc, 14);
53
+ lfh.writeUInt32LE(data.length, 18);
54
+ lfh.writeUInt32LE(data.length, 22);
55
+ lfh.writeUInt16LE(nameBuf.length, 26);
56
+ lfh.writeUInt16LE(0, 28);
57
+ local.push(lfh, nameBuf, data);
58
+
59
+ const cdh = Buffer.alloc(46);
60
+ cdh.writeUInt32LE(0x02014b50, 0);
61
+ cdh.writeUInt16LE(20, 4); // version made by
62
+ cdh.writeUInt16LE(20, 6); // version needed
63
+ cdh.writeUInt16LE(0x0800, 8); // flags
64
+ cdh.writeUInt16LE(0, 10); // compression
65
+ cdh.writeUInt16LE(time, 12);
66
+ cdh.writeUInt16LE(day, 14);
67
+ cdh.writeUInt32LE(crc, 16);
68
+ cdh.writeUInt32LE(data.length, 20);
69
+ cdh.writeUInt32LE(data.length, 24);
70
+ cdh.writeUInt16LE(nameBuf.length, 28);
71
+ cdh.writeUInt16LE(0, 30);
72
+ cdh.writeUInt16LE(0, 32);
73
+ cdh.writeUInt16LE(0, 34); // disk number
74
+ cdh.writeUInt16LE(0, 36); // internal attrs
75
+ cdh.writeUInt32LE(0, 38); // external attrs
76
+ cdh.writeUInt32LE(offset, 42);
77
+ central.push(cdh, nameBuf);
78
+
79
+ offset += lfh.length + nameBuf.length + data.length;
80
+ }
81
+
82
+ const centralBuf = Buffer.concat(central);
83
+ const eocd = Buffer.alloc(22);
84
+ eocd.writeUInt32LE(0x06054b50, 0);
85
+ eocd.writeUInt16LE(0, 4); // disk number
86
+ eocd.writeUInt16LE(0, 6); // disk where central dir starts
87
+ eocd.writeUInt16LE(entries.length, 8); // entries on this disk
88
+ eocd.writeUInt16LE(entries.length, 10); // total entries
89
+ eocd.writeUInt32LE(centralBuf.length, 12);
90
+ eocd.writeUInt32LE(offset, 16); // central dir offset
91
+ eocd.writeUInt16LE(0, 20); // comment length
92
+
93
+ return Buffer.concat([...local, centralBuf, eocd]);
94
+ };