@stigmer/runner 3.12.8 → 3.13.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/dist/.build-fingerprint +1 -1
- package/dist/activities/call-agent.d.ts +3 -2
- package/dist/activities/call-agent.js +13 -4
- package/dist/activities/call-agent.js.map +1 -1
- package/dist/activities/discover-mcp-server.js +1 -1
- package/dist/activities/execute-cursor/fetch-interceptor.js +29 -16
- package/dist/activities/execute-cursor/fetch-interceptor.js.map +1 -1
- package/dist/activities/execute-cursor/index.d.ts +14 -0
- package/dist/activities/execute-cursor/index.js +55 -5
- package/dist/activities/execute-cursor/index.js.map +1 -1
- package/dist/activities/execute-cursor/message-translator.d.ts +15 -9
- package/dist/activities/execute-cursor/message-translator.js +15 -9
- package/dist/activities/execute-cursor/message-translator.js.map +1 -1
- package/dist/activities/execute-deep-agent/attachment-injector.d.ts +1 -1
- package/dist/activities/execute-deep-agent/attachment-injector.js +2 -2
- package/dist/activities/execute-deep-agent/attachment-injector.js.map +1 -1
- package/dist/activities/execute-deep-agent/index.js +9 -0
- package/dist/activities/execute-deep-agent/index.js.map +1 -1
- package/dist/activities/execute-deep-agent/setup.d.ts +10 -1
- package/dist/activities/execute-deep-agent/setup.js +20 -3
- package/dist/activities/execute-deep-agent/setup.js.map +1 -1
- package/dist/client/stigmer-client.js +7 -2
- package/dist/client/stigmer-client.js.map +1 -1
- package/dist/payload-codecs.d.ts +1 -1
- package/dist/payload-codecs.js +6 -4
- package/dist/payload-codecs.js.map +1 -1
- package/dist/runner-manager.js +1 -1
- package/dist/runner-manager.js.map +1 -1
- package/dist/runner.js +1 -1
- package/dist/runner.js.map +1 -1
- package/dist/shared/memory-retrieval.d.ts +126 -0
- package/dist/shared/memory-retrieval.js +293 -0
- package/dist/shared/memory-retrieval.js.map +1 -0
- package/dist/shared/runner-credential-keys.js +2 -1
- package/dist/shared/runner-credential-keys.js.map +1 -1
- package/dist/shared/zip-extract.js +1 -1
- package/dist/shared/zip-extract.js.map +1 -1
- package/package.json +4 -2
- package/src/__tests__/history-encryption-e2e.test.ts +1 -1
- package/src/activities/call-agent.ts +14 -4
- package/src/activities/discover-mcp-server.ts +1 -1
- package/src/activities/execute-cursor/__tests__/build-prompt.test.ts +40 -1
- package/src/activities/execute-cursor/__tests__/fetch-interceptor.test.ts +49 -15
- package/src/activities/execute-cursor/__tests__/skill-resolver.test.ts +1 -1
- package/src/activities/execute-cursor/fetch-interceptor.ts +37 -16
- package/src/activities/execute-cursor/index.ts +58 -5
- package/src/activities/execute-cursor/message-translator.ts +15 -9
- package/src/activities/execute-deep-agent/__tests__/attachment-injector.test.ts +3 -2
- package/src/activities/execute-deep-agent/attachment-injector.ts +2 -2
- package/src/activities/execute-deep-agent/index.ts +10 -0
- package/src/activities/execute-deep-agent/setup.ts +34 -6
- package/src/client/stigmer-client.ts +7 -2
- package/src/payload-codecs.ts +12 -9
- package/src/runner-manager.ts +1 -1
- package/src/runner.ts +1 -1
- package/src/shared/__tests__/memory-retrieval.test.ts +310 -0
- package/src/shared/__tests__/skill-mount.test.ts +1 -1
- package/src/shared/__tests__/skill-writer.test.ts +1 -1
- package/src/shared/__tests__/zip-extract.test.ts +1 -1
- package/src/shared/memory-retrieval.ts +383 -0
- package/src/shared/runner-credential-keys.ts +2 -1
- package/src/shared/zip-extract.ts +1 -1
- package/dist/claimcheck/compressor.d.ts +0 -2
- package/dist/claimcheck/compressor.js +0 -8
- package/dist/claimcheck/compressor.js.map +0 -1
- package/dist/claimcheck/config.d.ts +0 -7
- package/dist/claimcheck/config.js +0 -10
- package/dist/claimcheck/config.js.map +0 -1
- package/dist/claimcheck/index.d.ts +0 -3
- package/dist/claimcheck/index.js +0 -4
- package/dist/claimcheck/index.js.map +0 -1
- package/dist/claimcheck/payload-codec.d.ts +0 -23
- package/dist/claimcheck/payload-codec.js +0 -105
- package/dist/claimcheck/payload-codec.js.map +0 -1
- package/dist/encryption/config.d.ts +0 -64
- package/dist/encryption/config.js +0 -113
- package/dist/encryption/config.js.map +0 -1
- package/dist/encryption/index.d.ts +0 -3
- package/dist/encryption/index.js +0 -3
- package/dist/encryption/index.js.map +0 -1
- package/dist/encryption/payload-codec.d.ts +0 -41
- package/dist/encryption/payload-codec.js +0 -130
- package/dist/encryption/payload-codec.js.map +0 -1
- package/dist/shared/zip-structure.d.ts +0 -61
- package/dist/shared/zip-structure.js +0 -128
- package/dist/shared/zip-structure.js.map +0 -1
- package/src/__test-utils__/zip-fixtures.ts +0 -206
- package/src/__tests__/claimcheck-codec.test.ts +0 -256
- package/src/__tests__/encryption-codec.test.ts +0 -287
- package/src/__tests__/fixtures/encrypted-payload-fixture.json +0 -15
- package/src/claimcheck/compressor.ts +0 -9
- package/src/claimcheck/config.ts +0 -20
- package/src/claimcheck/index.ts +0 -3
- package/src/claimcheck/payload-codec.ts +0 -139
- package/src/encryption/config.ts +0 -161
- package/src/encryption/index.ts +0 -3
- package/src/encryption/payload-codec.ts +0 -152
- package/src/shared/zip-structure.ts +0 -181
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Temporal PayloadCodec that encrypts payloads at rest in workflow
|
|
3
|
-
* history (stigmer-cloud#227).
|
|
4
|
-
*
|
|
5
|
-
* Why: the workflow engine runs inside the Temporal deterministic
|
|
6
|
-
* sandbox, so decrypted execution-context values cross the history
|
|
7
|
-
* boundary in many places — the hydrate activity result, the runtime
|
|
8
|
-
* env passed as an input to every per-task activity, and expression
|
|
9
|
-
* results recorded as local-activity markers. Encrypting at the payload
|
|
10
|
-
* codec layer closes the entire class with one mechanism instead of
|
|
11
|
-
* chasing each crossing.
|
|
12
|
-
*
|
|
13
|
-
* Envelope (cross-SDK contract — the Java decode-only codec in
|
|
14
|
-
* stigmer-cloud's temporal-starter must match it byte-for-byte, pinned
|
|
15
|
-
* by the conformance fixture in __tests__/fixtures/):
|
|
16
|
-
*
|
|
17
|
-
* metadata: encoding = "binary/encrypted"
|
|
18
|
-
* encryption-key-id = <key id that encrypted this payload>
|
|
19
|
-
* data: iv (12 bytes) ‖ AES-256-GCM(ciphertext ‖ tag (16 bytes))
|
|
20
|
-
*
|
|
21
|
-
* The plaintext is the serialized ORIGINAL Payload proto (metadata AND
|
|
22
|
-
* data), so decode restores the payload exactly — including its
|
|
23
|
-
* original encoding — with no side channel.
|
|
24
|
-
*
|
|
25
|
-
* Decode passes through payloads it did not encode. This is what keeps
|
|
26
|
-
* plaintext signals from the Java/Go orchestrators and pre-rollout
|
|
27
|
-
* in-flight histories working with zero migration. Everything else
|
|
28
|
-
* fails closed: unknown key id, missing key id, and ciphertext tampering
|
|
29
|
-
* all throw rather than surfacing bogus payloads.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
33
|
-
import type { Payload, PayloadCodec } from "@temporalio/common";
|
|
34
|
-
// Default-import + destructure, NOT `import { temporal } from …`:
|
|
35
|
-
// @temporalio/proto is CommonJS and its `temporal` export defeats Node's
|
|
36
|
-
// cjs-module-lexer named-export detection, so the named form loads under
|
|
37
|
-
// tsx/vitest (their interop is looser) but crashes plain `node dist/main.js`
|
|
38
|
-
// at startup with "Named export 'temporal' not found" (stigmer/stigmer#399
|
|
39
|
-
// boot regression). Pinned by scripts/verify-dist-boot.mjs in CI.
|
|
40
|
-
import proto from "@temporalio/proto";
|
|
41
|
-
import type { EncryptionKey, PayloadEncryptionConfig } from "./config.js";
|
|
42
|
-
|
|
43
|
-
const { temporal } = proto;
|
|
44
|
-
|
|
45
|
-
const ENCODING_METADATA_KEY = "encoding";
|
|
46
|
-
const ENCRYPTED_ENCODING_VALUE = "binary/encrypted";
|
|
47
|
-
const KEY_ID_METADATA_KEY = "encryption-key-id";
|
|
48
|
-
|
|
49
|
-
/** AES-GCM parameters shared with the Java implementation. */
|
|
50
|
-
const IV_BYTES = 12;
|
|
51
|
-
const AUTH_TAG_BYTES = 16;
|
|
52
|
-
|
|
53
|
-
export class EncryptionPayloadCodec implements PayloadCodec {
|
|
54
|
-
private readonly decryptKeysById: Map<string, Buffer>;
|
|
55
|
-
|
|
56
|
-
constructor(private readonly config: PayloadEncryptionConfig) {
|
|
57
|
-
this.decryptKeysById = new Map([[config.primary.keyId, config.primary.key]]);
|
|
58
|
-
if (config.secondary) {
|
|
59
|
-
this.decryptKeysById.set(config.secondary.keyId, config.secondary.key);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async encode(payloads: Payload[]): Promise<Payload[]> {
|
|
64
|
-
return payloads.map((p) => this.encodePayload(p));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async decode(payloads: Payload[]): Promise<Payload[]> {
|
|
68
|
-
return payloads.map((p) => this.decodePayload(p));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private encodePayload(payload: Payload): Payload {
|
|
72
|
-
// Data-less payloads (binary/null from void results) stay as-is:
|
|
73
|
-
// there is nothing to protect, and the cross-language parents that
|
|
74
|
-
// await our workflows as void must be able to read them without a key.
|
|
75
|
-
if (!payload.data || payload.data.length === 0) {
|
|
76
|
-
return payload;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const plaintext = temporal.api.common.v1.Payload.encode(payload).finish();
|
|
80
|
-
return {
|
|
81
|
-
metadata: {
|
|
82
|
-
[ENCODING_METADATA_KEY]: Buffer.from(ENCRYPTED_ENCODING_VALUE),
|
|
83
|
-
[KEY_ID_METADATA_KEY]: Buffer.from(this.config.primary.keyId),
|
|
84
|
-
},
|
|
85
|
-
data: encrypt(plaintext, this.config.primary),
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private decodePayload(payload: Payload): Payload {
|
|
90
|
-
if (!isEncryptedPayload(payload)) {
|
|
91
|
-
return payload;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const keyIdBytes = payload.metadata?.[KEY_ID_METADATA_KEY];
|
|
95
|
-
if (!keyIdBytes) {
|
|
96
|
-
throw new Error(
|
|
97
|
-
"Encrypted payload is missing its encryption-key-id metadata — refusing to decode",
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
const keyId = Buffer.from(keyIdBytes).toString("utf-8");
|
|
101
|
-
const key = this.decryptKeysById.get(keyId);
|
|
102
|
-
if (!key) {
|
|
103
|
-
throw new Error(
|
|
104
|
-
`Encrypted payload uses unknown key id '${keyId}' — configure it as the ` +
|
|
105
|
-
`primary or secondary payload encryption key (rotation window?)`,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const plaintext = decrypt(payload.data!, key, keyId);
|
|
110
|
-
return temporal.api.common.v1.Payload.decode(plaintext);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function isEncryptedPayload(payload: Payload): boolean {
|
|
115
|
-
const encoding = payload.metadata?.[ENCODING_METADATA_KEY];
|
|
116
|
-
if (!encoding) return false;
|
|
117
|
-
return Buffer.from(encoding).toString("utf-8") === ENCRYPTED_ENCODING_VALUE;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function encrypt(plaintext: Uint8Array, key: EncryptionKey): Buffer {
|
|
121
|
-
const iv = randomBytes(IV_BYTES);
|
|
122
|
-
const cipher = createCipheriv("aes-256-gcm", key.key, iv);
|
|
123
|
-
const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
124
|
-
// Layout iv ‖ ciphertext ‖ tag matches Java's AES/GCM/NoPadding, whose
|
|
125
|
-
// doFinal() output is ciphertext ‖ tag.
|
|
126
|
-
return Buffer.concat([iv, ciphertext, cipher.getAuthTag()]);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function decrypt(data: Uint8Array, key: Buffer, keyId: string): Buffer {
|
|
130
|
-
if (data.length < IV_BYTES + AUTH_TAG_BYTES) {
|
|
131
|
-
throw new Error(
|
|
132
|
-
`Encrypted payload under key id '${keyId}' is truncated (${data.length} bytes)`,
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
const buf = Buffer.from(data);
|
|
136
|
-
const iv = buf.subarray(0, IV_BYTES);
|
|
137
|
-
const ciphertext = buf.subarray(IV_BYTES, buf.length - AUTH_TAG_BYTES);
|
|
138
|
-
const tag = buf.subarray(buf.length - AUTH_TAG_BYTES);
|
|
139
|
-
|
|
140
|
-
const decipher = createDecipheriv("aes-256-gcm", key, iv);
|
|
141
|
-
decipher.setAuthTag(tag);
|
|
142
|
-
try {
|
|
143
|
-
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
144
|
-
} catch {
|
|
145
|
-
// GCM auth failure: tampered ciphertext or a key that does not match
|
|
146
|
-
// its advertised id. Never surface partially decrypted bytes.
|
|
147
|
-
throw new Error(
|
|
148
|
-
`Failed to decrypt payload under key id '${keyId}' — ciphertext is ` +
|
|
149
|
-
`corrupt or the configured key does not match`,
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
@@ -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
|
-
}
|