@stigmer/runner 3.12.9 → 3.14.0-dev.20260910084630

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.
Files changed (56) hide show
  1. package/LICENSE +190 -0
  2. package/dist/.build-fingerprint +1 -1
  3. package/dist/activities/call-agent.d.ts +3 -2
  4. package/dist/activities/call-agent.js +13 -4
  5. package/dist/activities/call-agent.js.map +1 -1
  6. package/dist/activities/discover-mcp-server.js +1 -1
  7. package/dist/activities/execute-cursor/message-translator.d.ts +67 -0
  8. package/dist/activities/execute-cursor/message-translator.js +103 -0
  9. package/dist/activities/execute-cursor/message-translator.js.map +1 -1
  10. package/dist/activities/execute-cursor/prompt-builder.js +13 -1
  11. package/dist/activities/execute-cursor/prompt-builder.js.map +1 -1
  12. package/dist/activities/execute-cursor/turn-boundary.d.ts +16 -1
  13. package/dist/activities/execute-cursor/turn-boundary.js +44 -2
  14. package/dist/activities/execute-cursor/turn-boundary.js.map +1 -1
  15. package/dist/activities/execute-deep-agent/attachment-injector.d.ts +1 -1
  16. package/dist/activities/execute-deep-agent/attachment-injector.js +2 -2
  17. package/dist/activities/execute-deep-agent/attachment-injector.js.map +1 -1
  18. package/dist/client/stigmer-client.js +7 -2
  19. package/dist/client/stigmer-client.js.map +1 -1
  20. package/dist/ipc-protocol-fixtures.js +3 -4
  21. package/dist/ipc-protocol-fixtures.js.map +1 -1
  22. package/dist/ipc-protocol.js +5 -4
  23. package/dist/ipc-protocol.js.map +1 -1
  24. package/dist/runner-manager.js +1 -1
  25. package/dist/runner-manager.js.map +1 -1
  26. package/dist/runner.js +1 -1
  27. package/dist/runner.js.map +1 -1
  28. package/dist/shared/zip-extract.js +1 -1
  29. package/dist/shared/zip-extract.js.map +1 -1
  30. package/package.json +15 -4
  31. package/src/__tests__/ipc-protocol-fixtures.test.ts +3 -4
  32. package/src/__tests__/ipc-protocol.test.ts +3 -2
  33. package/src/activities/call-agent.ts +14 -4
  34. package/src/activities/discover-mcp-server.ts +1 -1
  35. package/src/activities/execute-cursor/__tests__/cursor-generate-image-live.test.ts +147 -0
  36. package/src/activities/execute-cursor/__tests__/skill-resolver.test.ts +1 -1
  37. package/src/activities/execute-cursor/__tests__/turn-boundary.test.ts +153 -1
  38. package/src/activities/execute-cursor/message-translator.ts +115 -0
  39. package/src/activities/execute-cursor/prompt-builder.ts +13 -1
  40. package/src/activities/execute-cursor/turn-boundary.ts +65 -1
  41. package/src/activities/execute-deep-agent/__tests__/attachment-injector.test.ts +3 -2
  42. package/src/activities/execute-deep-agent/attachment-injector.ts +2 -2
  43. package/src/client/stigmer-client.ts +7 -2
  44. package/src/ipc-protocol-fixtures.ts +3 -4
  45. package/src/ipc-protocol.ts +5 -4
  46. package/src/runner-manager.ts +1 -1
  47. package/src/runner.ts +1 -1
  48. package/src/shared/__tests__/skill-mount.test.ts +1 -1
  49. package/src/shared/__tests__/skill-writer.test.ts +1 -1
  50. package/src/shared/__tests__/zip-extract.test.ts +1 -1
  51. package/src/shared/zip-extract.ts +1 -1
  52. package/dist/shared/zip-structure.d.ts +0 -61
  53. package/dist/shared/zip-structure.js +0 -128
  54. package/dist/shared/zip-structure.js.map +0 -1
  55. package/src/__test-utils__/zip-fixtures.ts +0 -206
  56. package/src/shared/zip-structure.ts +0 -181
@@ -1,128 +0,0 @@
1
- /**
2
- * Policy-free structural ZIP parsing: end-of-central-directory (EOCD)
3
- * location plus central-directory walk, producing one record per entry.
4
- *
5
- * Parsing is *central-directory-based* — never a front-to-back walk of
6
- * local file headers. This is a correctness decision, not a style choice
7
- * (issues #450 and #567): local headers of streaming entries
8
- * (general-purpose flag bit 3) carry zeroed sizes, and the only
9
- * local-only way to recover them is scanning the payload for the
10
- * data-descriptor signature — which silently truncates any stored entry
11
- * whose *content* happens to contain those four bytes, and
12
- * desynchronizes every entry after it. The central directory always
13
- * carries the real sizes, and every consumer here holds the complete
14
- * archive bytes, so nothing a local-header walk could offer is needed.
15
- *
16
- * This layer maps bytes to entry records and nothing else. Policy — what
17
- * a structural failure means, which entries are acceptable, how payloads
18
- * are decoded — belongs to the consumers, and they differ on purpose:
19
- *
20
- * - shared/zip-extract.ts (skill artifacts): structural failure is
21
- * NON-FATAL — both editions' push gates validated every artifact
22
- * with a central-directory-based reader before storage, so a defect
23
- * here can only be a truncated or corrupted download.
24
- * - execute-deep-agent/attachment-injector.ts (user attachments):
25
- * structural failure is FAIL-HARD — the input is an untrusted
26
- * upload and nothing upstream vouched for it.
27
- *
28
- * ZIP64 is deliberately unsupported: both consumers cap input far below
29
- * every ZIP64 threshold (skill push gates: 100MB / 10,000 files;
30
- * attachment uploads: 10MB).
31
- *
32
- * Throws plain `Error`s on structural defects (no valid EOCD, a central
33
- * directory or local header record that does not parse, a payload slice
34
- * that runs past the end of the buffer); consumers translate those into
35
- * their own error models.
36
- */
37
- // ─── Format constants ────────────────────────────────────────────────────
38
- const LOCAL_FILE_HEADER_SIG = 0x04034b50;
39
- const CENTRAL_DIRECTORY_SIG = 0x02014b50;
40
- const EOCD_SIG = 0x06054b50;
41
- /** Fixed size of the EOCD record, excluding the variable-length comment. */
42
- export const EOCD_MIN_SIZE = 22;
43
- /** The archive comment length is a u16, so the EOCD sits within the last 64KB + 22 bytes. */
44
- const EOCD_MAX_COMMENT = 0xffff;
45
- const LOCAL_HEADER_SIZE = 30;
46
- const CD_RECORD_SIZE = 46;
47
- // ─── Parsing ─────────────────────────────────────────────────────────────
48
- /**
49
- * Enumerate the archive's entries from its central directory.
50
- *
51
- * Throws on any structural defect: no valid EOCD, a central directory or
52
- * local header record that doesn't parse, or a payload slice that runs
53
- * past the end of the buffer.
54
- */
55
- export function parseZipStructure(data) {
56
- const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
57
- const eocd = findEndOfCentralDirectory(data, view);
58
- const entries = [];
59
- let offset = eocd.centralDirectoryOffset;
60
- for (let i = 0; i < eocd.entryCount; i++) {
61
- if (offset + CD_RECORD_SIZE > data.length || view.getUint32(offset, true) !== CENTRAL_DIRECTORY_SIG) {
62
- throw new Error(`invalid central directory record at offset ${offset}`);
63
- }
64
- const compressionMethod = view.getUint16(offset + 10, true);
65
- const compressedSize = view.getUint32(offset + 20, true);
66
- const uncompressedSize = view.getUint32(offset + 24, true);
67
- const fileNameLength = view.getUint16(offset + 28, true);
68
- const extraFieldLength = view.getUint16(offset + 30, true);
69
- const commentLength = view.getUint16(offset + 32, true);
70
- const localHeaderOffset = view.getUint32(offset + 42, true);
71
- const fileName = new TextDecoder().decode(data.subarray(offset + CD_RECORD_SIZE, offset + CD_RECORD_SIZE + fileNameLength));
72
- // The local header is consulted for one thing only: the payload start.
73
- // Its name/extra field lengths can legitimately differ from the central
74
- // directory's (streaming writers pad the extra field), so the offset
75
- // cannot be derived from CD fields alone.
76
- const dataStart = payloadStart(data, view, localHeaderOffset, fileName);
77
- if (dataStart + compressedSize > data.length) {
78
- throw new Error(`entry "${fileName}" payload runs past end of archive`);
79
- }
80
- entries.push({
81
- name: fileName,
82
- isDirectory: fileName.endsWith("/"),
83
- compressionMethod,
84
- uncompressedSize,
85
- compressedData: data.subarray(dataStart, dataStart + compressedSize),
86
- });
87
- offset += CD_RECORD_SIZE + fileNameLength + extraFieldLength + commentLength;
88
- }
89
- return entries;
90
- }
91
- /**
92
- * Locate and validate the end-of-central-directory record.
93
- *
94
- * Scans backward from the end of the archive across the maximum comment
95
- * span. A signature match alone is not trusted (the four bytes can occur
96
- * inside a trailing comment): the record must also point at an offset
97
- * that actually holds a central directory record, or be a genuinely
98
- * empty archive.
99
- */
100
- function findEndOfCentralDirectory(data, view) {
101
- const scanFloor = Math.max(0, data.length - EOCD_MIN_SIZE - EOCD_MAX_COMMENT);
102
- for (let pos = data.length - EOCD_MIN_SIZE; pos >= scanFloor; pos--) {
103
- if (view.getUint32(pos, true) !== EOCD_SIG)
104
- continue;
105
- const entryCount = view.getUint16(pos + 10, true);
106
- const centralDirectorySize = view.getUint32(pos + 12, true);
107
- const centralDirectoryOffset = view.getUint32(pos + 16, true);
108
- const directoryEndsAtRecord = centralDirectoryOffset + centralDirectorySize <= pos;
109
- const directoryLooksReal = entryCount === 0 ||
110
- (centralDirectoryOffset + 4 <= data.length &&
111
- view.getUint32(centralDirectoryOffset, true) === CENTRAL_DIRECTORY_SIG);
112
- if (directoryEndsAtRecord && directoryLooksReal) {
113
- return { centralDirectoryOffset, entryCount };
114
- }
115
- }
116
- throw new Error("no end-of-central-directory record found");
117
- }
118
- /** Resolve where an entry's payload begins, from its local file header. */
119
- function payloadStart(data, view, localHeaderOffset, fileName) {
120
- if (localHeaderOffset + LOCAL_HEADER_SIZE > data.length ||
121
- view.getUint32(localHeaderOffset, true) !== LOCAL_FILE_HEADER_SIG) {
122
- throw new Error(`entry "${fileName}" has no local file header at offset ${localHeaderOffset}`);
123
- }
124
- const localNameLength = view.getUint16(localHeaderOffset + 26, true);
125
- const localExtraLength = view.getUint16(localHeaderOffset + 28, true);
126
- return localHeaderOffset + LOCAL_HEADER_SIZE + localNameLength + localExtraLength;
127
- }
128
- //# sourceMappingURL=zip-structure.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"zip-structure.js","sourceRoot":"","sources":["../../src/shared/zip-structure.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAoBH,4EAA4E;AAE5E,MAAM,qBAAqB,GAAG,UAAU,CAAC;AACzC,MAAM,qBAAqB,GAAG,UAAU,CAAC;AACzC,MAAM,QAAQ,GAAG,UAAU,CAAC;AAE5B,4EAA4E;AAC5E,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAChC,6FAA6F;AAC7F,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAC7B,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B,4EAA4E;AAE5E;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAgB;IAChD,MAAM,IAAI,GAAG,IAAI,QAAQ,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206 +0,0 @@
1
- /**
2
- * Shared ZIP fixture builder for runner unit tests.
3
- *
4
- * Emits complete, real-shaped archives — local file headers (optionally
5
- * streaming-style, the Go stdlib writer's default), payloads, central
6
- * directory, and EOCD — because that is the only shape that can reach the
7
- * runner: both editions' skill push gates validate artifacts with
8
- * central-directory-based readers (see zip-extract.ts's module doc and
9
- * design record 017). Earlier fixtures emitted local headers only, a
10
- * shape no real ZIP writer produces, and were coupled to the old parser's
11
- * front-to-back walk.
12
- *
13
- * Mirrors the conformance suite's `zipFilesStreaming()`
14
- * (test/conformance/src/support/skills.ts) so unit fixtures and
15
- * cross-edition fixtures share one shape.
16
- *
17
- * Lives in src/__test-utils__/ so `tsc --noEmit` covers it while
18
- * tsconfig.build.json keeps it out of dist/.
19
- */
20
-
21
- import { deflateRawSync } from "node:zlib";
22
-
23
- export interface ZipFixtureFile {
24
- name: string;
25
- /** Text content is UTF-8 encoded; pass bytes directly for binary payloads. */
26
- content: string | Uint8Array;
27
- /** Compression method for the entry. Defaults to stored. */
28
- method?: "stored" | "deflated";
29
- /**
30
- * Emit the entry the way Go's archive/zip does by default: general-purpose
31
- * flag bit 3 set, zeroed sizes in the local header, and a trailing data
32
- * descriptor (with the conventional signature) carrying the real values.
33
- */
34
- streaming?: boolean;
35
- /**
36
- * Lie about the entry's uncompressed size everywhere the writer would
37
- * declare it (local header, data descriptor, central directory). Models a
38
- * crafted archive whose declarations disagree with its actual payload —
39
- * the input class the attachment injector's declared-size enforcement
40
- * exists to reject (issue #567).
41
- */
42
- declaredUncompressedSize?: number;
43
- }
44
-
45
- export interface ZipFixtureOptions {
46
- /** Trailing archive comment appended after the EOCD record. */
47
- comment?: string;
48
- /**
49
- * Drop the central directory and EOCD, leaving only local headers and
50
- * payloads. No real ZIP writer produces this shape — it models a download
51
- * truncated before the archive's index.
52
- */
53
- omitCentralDirectory?: boolean;
54
- }
55
-
56
- /** Build a ZIP archive from path + content pairs. */
57
- export function buildZip(files: ZipFixtureFile[], options?: ZipFixtureOptions): Uint8Array {
58
- const out = new ByteWriter();
59
-
60
- interface WrittenEntry {
61
- nameBytes: Uint8Array;
62
- payload: Uint8Array;
63
- declaredUncompressed: number;
64
- crc: number;
65
- flags: number;
66
- method: number;
67
- localHeaderOffset: number;
68
- }
69
- const written: WrittenEntry[] = [];
70
-
71
- for (const file of files) {
72
- const contentBytes =
73
- typeof file.content === "string" ? new TextEncoder().encode(file.content) : file.content;
74
- const deflated = file.method === "deflated";
75
- const payload = deflated ? new Uint8Array(deflateRawSync(contentBytes)) : contentBytes;
76
- const entry: WrittenEntry = {
77
- nameBytes: new TextEncoder().encode(file.name),
78
- payload,
79
- declaredUncompressed: file.declaredUncompressedSize ?? contentBytes.length,
80
- crc: crc32(contentBytes),
81
- flags: file.streaming ? 0x0008 : 0,
82
- method: deflated ? 8 : 0,
83
- localHeaderOffset: out.length,
84
- };
85
- written.push(entry);
86
-
87
- out.u32(0x04034b50); // local file header signature
88
- out.u16(20); // version needed to extract
89
- out.u16(entry.flags);
90
- out.u16(entry.method);
91
- out.u16(0); // mod time
92
- out.u16(0); // mod date
93
- out.u32(file.streaming ? 0 : entry.crc);
94
- out.u32(file.streaming ? 0 : payload.length);
95
- out.u32(file.streaming ? 0 : entry.declaredUncompressed);
96
- out.u16(entry.nameBytes.length);
97
- out.u16(0); // extra field length
98
- out.raw(entry.nameBytes);
99
- out.raw(payload);
100
-
101
- if (file.streaming) {
102
- out.u32(0x08074b50); // data descriptor signature (Go writes it)
103
- out.u32(entry.crc);
104
- out.u32(payload.length);
105
- out.u32(entry.declaredUncompressed);
106
- }
107
- }
108
-
109
- if (options?.omitCentralDirectory) {
110
- return out.toUint8Array();
111
- }
112
-
113
- const centralDirectoryOffset = out.length;
114
- for (const entry of written) {
115
- out.u32(0x02014b50); // central directory record signature
116
- out.u16(20); // version made by
117
- out.u16(20); // version needed to extract
118
- out.u16(entry.flags);
119
- out.u16(entry.method);
120
- out.u16(0); // mod time
121
- out.u16(0); // mod date
122
- out.u32(entry.crc);
123
- out.u32(entry.payload.length);
124
- out.u32(entry.declaredUncompressed);
125
- out.u16(entry.nameBytes.length);
126
- out.u16(0); // extra field length
127
- out.u16(0); // comment length
128
- out.u16(0); // disk number start
129
- out.u16(0); // internal attributes
130
- out.u32(0); // external attributes
131
- out.u32(entry.localHeaderOffset);
132
- out.raw(entry.nameBytes);
133
- }
134
- const centralDirectorySize = out.length - centralDirectoryOffset;
135
-
136
- const commentBytes = new TextEncoder().encode(options?.comment ?? "");
137
- out.u32(0x06054b50); // EOCD signature
138
- out.u16(0); // disk number
139
- out.u16(0); // disk with central directory
140
- out.u16(written.length); // entries on this disk
141
- out.u16(written.length); // total entries
142
- out.u32(centralDirectorySize);
143
- out.u32(centralDirectoryOffset);
144
- out.u16(commentBytes.length);
145
- out.raw(commentBytes);
146
-
147
- return out.toUint8Array();
148
- }
149
-
150
- // Chunked assembly instead of a number[]-per-byte accumulator: fixtures at
151
- // the injector's 100 MB zip-bomb limit are built from payload-sized chunks,
152
- // which a per-byte spread-push cannot survive (argument-count overflow).
153
- class ByteWriter {
154
- private readonly chunks: Uint8Array[] = [];
155
- private size = 0;
156
-
157
- get length(): number {
158
- return this.size;
159
- }
160
-
161
- u16(v: number): void {
162
- this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff]));
163
- }
164
-
165
- u32(v: number): void {
166
- this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff, (v >>> 16) & 0xff, (v >>> 24) & 0xff]));
167
- }
168
-
169
- raw(b: Uint8Array): void {
170
- this.chunks.push(b);
171
- this.size += b.length;
172
- }
173
-
174
- toUint8Array(): Uint8Array {
175
- const result = new Uint8Array(this.size);
176
- let offset = 0;
177
- for (const chunk of this.chunks) {
178
- result.set(chunk, offset);
179
- offset += chunk.length;
180
- }
181
- return result;
182
- }
183
- }
184
-
185
- // Standard CRC-32 (IEEE 802.3, the ZIP checksum), table-driven so fixtures
186
- // at the injector's 100 MB limit stay cheap to build. Semantically identical
187
- // to the conformance suite's inline bit-loop helper.
188
- const CRC_TABLE = (() => {
189
- const table = new Uint32Array(256);
190
- for (let n = 0; n < 256; n++) {
191
- let c = n;
192
- for (let bit = 0; bit < 8; bit++) {
193
- c = (c >>> 1) ^ (0xedb88320 & -(c & 1));
194
- }
195
- table[n] = c;
196
- }
197
- return table;
198
- })();
199
-
200
- function crc32(data: Uint8Array): number {
201
- let crc = 0xffffffff;
202
- for (let i = 0; i < data.length; i++) {
203
- crc = (crc >>> 8) ^ CRC_TABLE[(crc ^ data[i]!) & 0xff]!;
204
- }
205
- return (crc ^ 0xffffffff) >>> 0;
206
- }
@@ -1,181 +0,0 @@
1
- /**
2
- * Policy-free structural ZIP parsing: end-of-central-directory (EOCD)
3
- * location plus central-directory walk, producing one record per entry.
4
- *
5
- * Parsing is *central-directory-based* — never a front-to-back walk of
6
- * local file headers. This is a correctness decision, not a style choice
7
- * (issues #450 and #567): local headers of streaming entries
8
- * (general-purpose flag bit 3) carry zeroed sizes, and the only
9
- * local-only way to recover them is scanning the payload for the
10
- * data-descriptor signature — which silently truncates any stored entry
11
- * whose *content* happens to contain those four bytes, and
12
- * desynchronizes every entry after it. The central directory always
13
- * carries the real sizes, and every consumer here holds the complete
14
- * archive bytes, so nothing a local-header walk could offer is needed.
15
- *
16
- * This layer maps bytes to entry records and nothing else. Policy — what
17
- * a structural failure means, which entries are acceptable, how payloads
18
- * are decoded — belongs to the consumers, and they differ on purpose:
19
- *
20
- * - shared/zip-extract.ts (skill artifacts): structural failure is
21
- * NON-FATAL — both editions' push gates validated every artifact
22
- * with a central-directory-based reader before storage, so a defect
23
- * here can only be a truncated or corrupted download.
24
- * - execute-deep-agent/attachment-injector.ts (user attachments):
25
- * structural failure is FAIL-HARD — the input is an untrusted
26
- * upload and nothing upstream vouched for it.
27
- *
28
- * ZIP64 is deliberately unsupported: both consumers cap input far below
29
- * every ZIP64 threshold (skill push gates: 100MB / 10,000 files;
30
- * attachment uploads: 10MB).
31
- *
32
- * Throws plain `Error`s on structural defects (no valid EOCD, a central
33
- * directory or local header record that does not parse, a payload slice
34
- * that runs past the end of the buffer); consumers translate those into
35
- * their own error models.
36
- */
37
-
38
- // ─── Entry records ───────────────────────────────────────────────────────
39
-
40
- export interface ZipStructuralEntry {
41
- /** Entry path exactly as recorded in the central directory. */
42
- readonly name: string;
43
- readonly isDirectory: boolean;
44
- readonly compressionMethod: number;
45
- /**
46
- * The central directory's declared uncompressed size — authoritative for
47
- * pre-extraction accounting (e.g. ZIP-bomb budgeting), but still a
48
- * *declaration*: a consumer that distrusts its input must enforce it
49
- * against the actual decompressed output.
50
- */
51
- readonly uncompressedSize: number;
52
- /** The entry's raw payload slice (a view, not a copy) of the archive buffer. */
53
- readonly compressedData: Uint8Array;
54
- }
55
-
56
- // ─── Format constants ────────────────────────────────────────────────────
57
-
58
- const LOCAL_FILE_HEADER_SIG = 0x04034b50;
59
- const CENTRAL_DIRECTORY_SIG = 0x02014b50;
60
- const EOCD_SIG = 0x06054b50;
61
-
62
- /** Fixed size of the EOCD record, excluding the variable-length comment. */
63
- export const EOCD_MIN_SIZE = 22;
64
- /** The archive comment length is a u16, so the EOCD sits within the last 64KB + 22 bytes. */
65
- const EOCD_MAX_COMMENT = 0xffff;
66
-
67
- const LOCAL_HEADER_SIZE = 30;
68
- const CD_RECORD_SIZE = 46;
69
-
70
- // ─── Parsing ─────────────────────────────────────────────────────────────
71
-
72
- /**
73
- * Enumerate the archive's entries from its central directory.
74
- *
75
- * Throws on any structural defect: no valid EOCD, a central directory or
76
- * local header record that doesn't parse, or a payload slice that runs
77
- * past the end of the buffer.
78
- */
79
- export function parseZipStructure(data: Uint8Array): ZipStructuralEntry[] {
80
- const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
81
- const eocd = findEndOfCentralDirectory(data, view);
82
-
83
- const entries: ZipStructuralEntry[] = [];
84
- let offset = eocd.centralDirectoryOffset;
85
-
86
- for (let i = 0; i < eocd.entryCount; i++) {
87
- if (offset + CD_RECORD_SIZE > data.length || view.getUint32(offset, true) !== CENTRAL_DIRECTORY_SIG) {
88
- throw new Error(`invalid central directory record at offset ${offset}`);
89
- }
90
-
91
- const compressionMethod = view.getUint16(offset + 10, true);
92
- const compressedSize = view.getUint32(offset + 20, true);
93
- const uncompressedSize = view.getUint32(offset + 24, true);
94
- const fileNameLength = view.getUint16(offset + 28, true);
95
- const extraFieldLength = view.getUint16(offset + 30, true);
96
- const commentLength = view.getUint16(offset + 32, true);
97
- const localHeaderOffset = view.getUint32(offset + 42, true);
98
-
99
- const fileName = new TextDecoder().decode(
100
- data.subarray(offset + CD_RECORD_SIZE, offset + CD_RECORD_SIZE + fileNameLength),
101
- );
102
-
103
- // The local header is consulted for one thing only: the payload start.
104
- // Its name/extra field lengths can legitimately differ from the central
105
- // directory's (streaming writers pad the extra field), so the offset
106
- // cannot be derived from CD fields alone.
107
- const dataStart = payloadStart(data, view, localHeaderOffset, fileName);
108
- if (dataStart + compressedSize > data.length) {
109
- throw new Error(`entry "${fileName}" payload runs past end of archive`);
110
- }
111
-
112
- entries.push({
113
- name: fileName,
114
- isDirectory: fileName.endsWith("/"),
115
- compressionMethod,
116
- uncompressedSize,
117
- compressedData: data.subarray(dataStart, dataStart + compressedSize),
118
- });
119
-
120
- offset += CD_RECORD_SIZE + fileNameLength + extraFieldLength + commentLength;
121
- }
122
-
123
- return entries;
124
- }
125
-
126
- interface EndOfCentralDirectory {
127
- centralDirectoryOffset: number;
128
- entryCount: number;
129
- }
130
-
131
- /**
132
- * Locate and validate the end-of-central-directory record.
133
- *
134
- * Scans backward from the end of the archive across the maximum comment
135
- * span. A signature match alone is not trusted (the four bytes can occur
136
- * inside a trailing comment): the record must also point at an offset
137
- * that actually holds a central directory record, or be a genuinely
138
- * empty archive.
139
- */
140
- function findEndOfCentralDirectory(data: Uint8Array, view: DataView): EndOfCentralDirectory {
141
- const scanFloor = Math.max(0, data.length - EOCD_MIN_SIZE - EOCD_MAX_COMMENT);
142
-
143
- for (let pos = data.length - EOCD_MIN_SIZE; pos >= scanFloor; pos--) {
144
- if (view.getUint32(pos, true) !== EOCD_SIG) continue;
145
-
146
- const entryCount = view.getUint16(pos + 10, true);
147
- const centralDirectorySize = view.getUint32(pos + 12, true);
148
- const centralDirectoryOffset = view.getUint32(pos + 16, true);
149
-
150
- const directoryEndsAtRecord = centralDirectoryOffset + centralDirectorySize <= pos;
151
- const directoryLooksReal =
152
- entryCount === 0 ||
153
- (centralDirectoryOffset + 4 <= data.length &&
154
- view.getUint32(centralDirectoryOffset, true) === CENTRAL_DIRECTORY_SIG);
155
-
156
- if (directoryEndsAtRecord && directoryLooksReal) {
157
- return { centralDirectoryOffset, entryCount };
158
- }
159
- }
160
-
161
- throw new Error("no end-of-central-directory record found");
162
- }
163
-
164
- /** Resolve where an entry's payload begins, from its local file header. */
165
- function payloadStart(
166
- data: Uint8Array,
167
- view: DataView,
168
- localHeaderOffset: number,
169
- fileName: string,
170
- ): number {
171
- if (
172
- localHeaderOffset + LOCAL_HEADER_SIZE > data.length ||
173
- view.getUint32(localHeaderOffset, true) !== LOCAL_FILE_HEADER_SIG
174
- ) {
175
- throw new Error(`entry "${fileName}" has no local file header at offset ${localHeaderOffset}`);
176
- }
177
-
178
- const localNameLength = view.getUint16(localHeaderOffset + 26, true);
179
- const localExtraLength = view.getUint16(localHeaderOffset + 28, true);
180
- return localHeaderOffset + LOCAL_HEADER_SIZE + localNameLength + localExtraLength;
181
- }