@copilotkit/runtime 2.0.0-rc.8 → 2.0.0-rc.9

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 (68) hide show
  1. package/README.md +19 -1
  2. package/dist/agent/converters/tanstack.cjs +1 -1
  3. package/dist/agent/converters/tanstack.cjs.map +1 -1
  4. package/dist/agent/converters/tanstack.mjs +1 -1
  5. package/dist/agent/converters/tanstack.mjs.map +1 -1
  6. package/dist/agent/index.cjs +9 -8
  7. package/dist/agent/index.cjs.map +1 -1
  8. package/dist/agent/index.d.cts.map +1 -1
  9. package/dist/agent/index.d.mts.map +1 -1
  10. package/dist/agent/index.mjs +9 -8
  11. package/dist/agent/index.mjs.map +1 -1
  12. package/dist/index.cjs +12 -0
  13. package/dist/index.d.cts +4 -1
  14. package/dist/index.d.mts +4 -1
  15. package/dist/index.mjs +4 -1
  16. package/dist/runtime/core/logger.cjs +1 -4
  17. package/dist/runtime/core/logger.cjs.map +1 -1
  18. package/dist/runtime/core/logger.d.cts.map +1 -1
  19. package/dist/runtime/core/logger.d.mts.map +1 -1
  20. package/dist/runtime/core/logger.mjs +1 -3
  21. package/dist/runtime/core/logger.mjs.map +1 -1
  22. package/dist/runtime/core/package-info.cjs +1 -1
  23. package/dist/runtime/core/package-info.mjs +1 -1
  24. package/dist/runtime/core/runtime.d.mts +1 -1
  25. package/dist/runtime/event-transforms/index.d.cts +4 -0
  26. package/dist/runtime/event-transforms/index.d.mts +4 -0
  27. package/dist/runtime/event-transforms/index.mjs +5 -0
  28. package/dist/runtime/event-transforms/open-generative-ui-middleware.cjs +286 -0
  29. package/dist/runtime/event-transforms/open-generative-ui-middleware.cjs.map +1 -0
  30. package/dist/runtime/event-transforms/open-generative-ui-middleware.d.cts +60 -0
  31. package/dist/runtime/event-transforms/open-generative-ui-middleware.d.cts.map +1 -0
  32. package/dist/runtime/event-transforms/open-generative-ui-middleware.d.mts +60 -0
  33. package/dist/runtime/event-transforms/open-generative-ui-middleware.d.mts.map +1 -0
  34. package/dist/runtime/event-transforms/open-generative-ui-middleware.mjs +283 -0
  35. package/dist/runtime/event-transforms/open-generative-ui-middleware.mjs.map +1 -0
  36. package/dist/runtime/event-transforms/recorded-events.cjs +59 -0
  37. package/dist/runtime/event-transforms/recorded-events.cjs.map +1 -0
  38. package/dist/runtime/event-transforms/recorded-events.d.cts +23 -0
  39. package/dist/runtime/event-transforms/recorded-events.d.cts.map +1 -0
  40. package/dist/runtime/event-transforms/recorded-events.d.mts +23 -0
  41. package/dist/runtime/event-transforms/recorded-events.d.mts.map +1 -0
  42. package/dist/runtime/event-transforms/recorded-events.mjs +58 -0
  43. package/dist/runtime/event-transforms/recorded-events.mjs.map +1 -0
  44. package/dist/runtime/handlers/shared/agent-utils.cjs +4 -3
  45. package/dist/runtime/handlers/shared/agent-utils.cjs.map +1 -1
  46. package/dist/runtime/handlers/shared/agent-utils.mjs +2 -1
  47. package/dist/runtime/handlers/shared/agent-utils.mjs.map +1 -1
  48. package/dist/runtime/index.d.cts +3 -0
  49. package/dist/runtime/index.d.mts +3 -0
  50. package/dist/runtime/intelligence-platform/learning-management.cjs +7 -7
  51. package/dist/runtime/intelligence-platform/learning-management.cjs.map +1 -1
  52. package/dist/runtime/intelligence-platform/learning-management.mjs +7 -7
  53. package/dist/runtime/intelligence-platform/learning-management.mjs.map +1 -1
  54. package/dist/runtime/intelligence-platform/skill-registry/archive.cjs +104 -109
  55. package/dist/runtime/intelligence-platform/skill-registry/archive.cjs.map +1 -1
  56. package/dist/runtime/intelligence-platform/skill-registry/archive.d.cts +8 -2
  57. package/dist/runtime/intelligence-platform/skill-registry/archive.d.cts.map +1 -1
  58. package/dist/runtime/intelligence-platform/skill-registry/archive.d.mts +8 -2
  59. package/dist/runtime/intelligence-platform/skill-registry/archive.d.mts.map +1 -1
  60. package/dist/runtime/intelligence-platform/skill-registry/archive.mjs +104 -109
  61. package/dist/runtime/intelligence-platform/skill-registry/archive.mjs.map +1 -1
  62. package/dist/runtime/intelligence-platform/trajectories.cjs +2 -2
  63. package/dist/runtime/intelligence-platform/trajectories.cjs.map +1 -1
  64. package/dist/runtime/intelligence-platform/trajectories.mjs +2 -2
  65. package/dist/runtime/intelligence-platform/trajectories.mjs.map +1 -1
  66. package/dist/runtime/telemetry/disclosure.cjs.map +1 -1
  67. package/dist/runtime/telemetry/disclosure.mjs.map +1 -1
  68. package/package.json +8 -9
@@ -1,126 +1,121 @@
1
1
  const require_runtime = require('../../../_virtual/_rolldown/runtime.cjs');
2
2
  const require_errors = require('./errors.cjs');
3
- let yauzl = require("yauzl");
3
+ let fflate = require("fflate");
4
4
 
5
5
  //#region src/runtime/intelligence-platform/skill-registry/archive.ts
6
6
  /** Internal corruption bounds, including manifest and directory entries. */
7
7
  const MAX_ARCHIVE_ENTRIES = 1e3;
8
8
  const MAX_SNAPSHOT_BYTES = 32 * 1024 * 1024;
9
+ const LOCAL_HEADER_SIGNATURE = 67324752;
10
+ const CENTRAL_HEADER_SIGNATURE = 33639248;
11
+ const END_OF_CENTRAL_DIRECTORY_SIGNATURE = 101010256;
12
+ const LOCAL_HEADER_SIZE = 30;
13
+ const CENTRAL_HEADER_SIZE = 46;
14
+ const END_OF_CENTRAL_DIRECTORY_SIZE = 22;
15
+ const INFLATE_CHUNK_BYTES = 16 * 1024;
9
16
  function safeRelativePath(path) {
10
17
  return path.length > 0 && !/[\\\x00-\x1f\x7f]/.test(path) && !/^[a-z]:/i.test(path) && path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
11
18
  }
12
- /** Decode only the listed ZIP namespace. No filesystem is involved. */
13
- function readArchive(bytes, signal) {
14
- return new Promise((resolve, reject) => {
15
- let archive;
16
- let stream;
17
- let settled = false;
18
- const finish = (error) => {
19
- if (settled) return;
20
- settled = true;
21
- signal?.removeEventListener("abort", abort);
22
- stream?.destroy();
23
- archive?.close();
24
- if (error !== void 0) reject(error);
25
- else resolve(files);
26
- };
27
- const abort = () => finish(signal?.reason ?? require_errors.invalidSnapshot());
19
+ function equalBytes(left, right) {
20
+ return left.length === right.length && left.every((byte, index) => byte === right[index]);
21
+ }
22
+ /** Inflate raw DEFLATE data, failing as soon as it decodes past `size` bytes. */
23
+ function inflate(data, size) {
24
+ const out = new Uint8Array(size);
25
+ let written = 0;
26
+ const inflater = new fflate.Inflate((chunk) => {
27
+ if (written + chunk.length > size) throw require_errors.invalidSnapshot();
28
+ out.set(chunk, written);
29
+ written += chunk.length;
30
+ });
31
+ let offset = 0;
32
+ do {
33
+ const end = Math.min(offset + INFLATE_CHUNK_BYTES, data.length);
34
+ inflater.push(data.subarray(offset, end), end === data.length);
35
+ offset = end;
36
+ } while (offset < data.length);
37
+ if (written !== size) throw require_errors.invalidSnapshot();
38
+ return out;
39
+ }
40
+ /** Find the end-of-central-directory record; its comment must end the file. */
41
+ function findEndOfCentralDirectory(bytes, view) {
42
+ const last = bytes.length - END_OF_CENTRAL_DIRECTORY_SIZE;
43
+ for (let offset = last; offset >= Math.max(0, last - 65535); offset--) {
44
+ if (view.getUint32(offset, true) !== END_OF_CENTRAL_DIRECTORY_SIGNATURE) continue;
45
+ if (view.getUint16(offset + 20, true) !== last - offset) throw require_errors.invalidSnapshot();
46
+ return offset;
47
+ }
48
+ throw require_errors.invalidSnapshot();
49
+ }
50
+ /**
51
+ * Decode only the listed ZIP namespace. No filesystem is involved.
52
+ *
53
+ * Parses the central directory directly and inflates with fflate, so it runs
54
+ * on Node and on edge runtimes alike. ZIP64, multi-disk, and encrypted
55
+ * archives are rejected: snapshots stay far below the ZIP64 limits.
56
+ */
57
+ async function readArchive(bytes, signal) {
58
+ const throwIfAborted = () => {
59
+ if (signal?.aborted) throw signal.reason ?? require_errors.invalidSnapshot();
60
+ };
61
+ throwIfAborted();
62
+ try {
63
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
64
+ const end = findEndOfCentralDirectory(bytes, view);
65
+ if (view.getUint16(end + 4, true) !== 0) throw require_errors.invalidSnapshot();
66
+ const entryCount = view.getUint16(end + 10, true);
67
+ if (entryCount > MAX_ARCHIVE_ENTRIES) throw require_errors.invalidSnapshot();
28
68
  const files = /* @__PURE__ */ new Map();
29
69
  const names = /* @__PURE__ */ new Set();
30
70
  let decodedBytes = 0;
31
- signal?.addEventListener("abort", abort, { once: true });
32
- if (signal?.aborted) {
33
- abort();
34
- return;
71
+ let header = view.getUint32(end + 16, true);
72
+ for (let index = 0; index < entryCount; index++) {
73
+ throwIfAborted();
74
+ if (header + CENTRAL_HEADER_SIZE > bytes.length || view.getUint32(header, true) !== CENTRAL_HEADER_SIGNATURE) throw require_errors.invalidSnapshot();
75
+ const generalPurposeBitFlag = view.getUint16(header + 8, true);
76
+ const compressionMethod = view.getUint16(header + 10, true);
77
+ const compressedSize = view.getUint32(header + 20, true);
78
+ const uncompressedSize = view.getUint32(header + 24, true);
79
+ const nameLength = view.getUint16(header + 28, true);
80
+ const extraLength = view.getUint16(header + 30, true);
81
+ const commentLength = view.getUint16(header + 32, true);
82
+ const externalFileAttributes = view.getUint32(header + 38, true);
83
+ const local = view.getUint32(header + 42, true);
84
+ const nameStart = header + CENTRAL_HEADER_SIZE;
85
+ header = nameStart + nameLength + extraLength + commentLength;
86
+ if (header > bytes.length) throw require_errors.invalidSnapshot();
87
+ const rawName = bytes.subarray(nameStart, nameStart + nameLength);
88
+ const name = new TextDecoder("utf-8", {
89
+ fatal: true,
90
+ ignoreBOM: true
91
+ }).decode(rawName);
92
+ const directory = name.endsWith("/");
93
+ if (!safeRelativePath(directory ? name.slice(0, -1) : name) || names.has(name)) throw require_errors.invalidSnapshot();
94
+ names.add(name);
95
+ const fileType = externalFileAttributes >>> 16 & 61440;
96
+ if (fileType !== 0 && fileType !== (directory ? 16384 : 32768)) throw require_errors.invalidSnapshot();
97
+ if (generalPurposeBitFlag & 65 || ![0, 8].includes(compressionMethod)) throw require_errors.invalidSnapshot();
98
+ if (compressionMethod === 0 && compressedSize !== uncompressedSize) throw require_errors.invalidSnapshot();
99
+ decodedBytes += uncompressedSize;
100
+ if (decodedBytes > MAX_SNAPSHOT_BYTES || directory && uncompressedSize !== 0) throw require_errors.invalidSnapshot();
101
+ if (local + LOCAL_HEADER_SIZE > bytes.length || view.getUint32(local, true) !== LOCAL_HEADER_SIGNATURE) throw require_errors.invalidSnapshot();
102
+ if (view.getUint16(local + 6, true) !== generalPurposeBitFlag || view.getUint16(local + 8, true) !== compressionMethod) throw require_errors.invalidSnapshot();
103
+ const localNameLength = view.getUint16(local + 26, true);
104
+ const localExtraLength = view.getUint16(local + 28, true);
105
+ const localNameStart = local + LOCAL_HEADER_SIZE;
106
+ const dataStart = localNameStart + localNameLength + localExtraLength;
107
+ if (dataStart + compressedSize > bytes.length || !equalBytes(bytes.subarray(localNameStart, localNameStart + localNameLength), rawName)) throw require_errors.invalidSnapshot();
108
+ if (!(generalPurposeBitFlag & 8) && (view.getUint32(local + 18, true) !== compressedSize || view.getUint32(local + 22, true) !== uncompressedSize)) throw require_errors.invalidSnapshot();
109
+ if (directory) continue;
110
+ const data = bytes.subarray(dataStart, dataStart + compressedSize);
111
+ files.set(name, compressionMethod === 0 ? data.slice() : inflate(data, uncompressedSize));
35
112
  }
36
- (0, yauzl.fromBuffer)(bytes, {
37
- lazyEntries: true,
38
- decodeStrings: false,
39
- validateEntrySizes: true,
40
- autoClose: false
41
- }, (error, zip) => {
42
- if (error) {
43
- finish(require_errors.invalidSnapshot(error));
44
- return;
45
- }
46
- archive = zip;
47
- if (settled) {
48
- zip.close();
49
- return;
50
- }
51
- zip.on("error", (cause) => finish(require_errors.invalidSnapshot(cause)));
52
- zip.on("end", () => finish());
53
- if (zip.entryCount > MAX_ARCHIVE_ENTRIES) {
54
- finish(require_errors.invalidSnapshot());
55
- return;
56
- }
57
- zip.on("entry", (entry) => {
58
- try {
59
- const rawName = entry.fileName;
60
- if (!Buffer.isBuffer(rawName)) throw require_errors.invalidSnapshot();
61
- const name = new TextDecoder("utf-8", {
62
- fatal: true,
63
- ignoreBOM: true
64
- }).decode(rawName);
65
- const directory = name.endsWith("/");
66
- if (!safeRelativePath(directory ? name.slice(0, -1) : name) || names.has(name)) throw require_errors.invalidSnapshot();
67
- names.add(name);
68
- const fileType = entry.externalFileAttributes >>> 16 & 61440;
69
- if (fileType !== 0 && fileType !== (directory ? 16384 : 32768)) throw require_errors.invalidSnapshot();
70
- if (entry.isEncrypted() || ![0, 8].includes(entry.compressionMethod)) throw require_errors.invalidSnapshot();
71
- if (!Number.isSafeInteger(entry.uncompressedSize) || entry.uncompressedSize < 0) throw require_errors.invalidSnapshot();
72
- decodedBytes += entry.uncompressedSize;
73
- if (decodedBytes > MAX_SNAPSHOT_BYTES || directory && entry.uncompressedSize !== 0) throw require_errors.invalidSnapshot();
74
- const local = entry.relativeOffsetOfLocalHeader;
75
- if (local < 0 || local + 30 > bytes.length || bytes.readUInt32LE(local) !== 67324752) throw require_errors.invalidSnapshot();
76
- if (bytes.readUInt16LE(local + 6) !== entry.generalPurposeBitFlag || bytes.readUInt16LE(local + 8) !== entry.compressionMethod) throw require_errors.invalidSnapshot();
77
- const localNameLength = bytes.readUInt16LE(local + 26);
78
- const localExtraLength = bytes.readUInt16LE(local + 28);
79
- if (local + 30 + localNameLength + localExtraLength + entry.compressedSize > bytes.length || !bytes.subarray(local + 30, local + 30 + localNameLength).equals(rawName)) throw require_errors.invalidSnapshot();
80
- if (!(entry.generalPurposeBitFlag & 8) && (bytes.readUInt32LE(local + 18) !== entry.compressedSize || bytes.readUInt32LE(local + 22) !== entry.uncompressedSize)) throw require_errors.invalidSnapshot();
81
- if (directory) {
82
- zip.readEntry();
83
- return;
84
- }
85
- zip.openReadStream(entry, (readError, readStream) => {
86
- if (readError) {
87
- finish(require_errors.invalidSnapshot(readError));
88
- return;
89
- }
90
- stream = readStream;
91
- if (settled) {
92
- readStream.destroy();
93
- return;
94
- }
95
- const chunks = [];
96
- let size = 0;
97
- readStream.on("error", (cause) => finish(require_errors.invalidSnapshot(cause)));
98
- readStream.on("data", (chunk) => {
99
- size += chunk.length;
100
- if (size > entry.uncompressedSize || size > MAX_SNAPSHOT_BYTES) {
101
- finish(require_errors.invalidSnapshot());
102
- return;
103
- }
104
- chunks.push(chunk);
105
- });
106
- readStream.on("end", () => {
107
- if (settled) return;
108
- if (size !== entry.uncompressedSize) {
109
- finish(require_errors.invalidSnapshot());
110
- return;
111
- }
112
- files.set(name, Buffer.concat(chunks, size));
113
- stream = void 0;
114
- zip.readEntry();
115
- });
116
- });
117
- } catch (cause) {
118
- finish(require_errors.invalidSnapshot(cause));
119
- }
120
- });
121
- zip.readEntry();
122
- });
123
- });
113
+ throwIfAborted();
114
+ return files;
115
+ } catch (cause) {
116
+ throwIfAborted();
117
+ throw require_errors.invalidSnapshot(cause);
118
+ }
124
119
  }
125
120
 
126
121
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"archive.cjs","names":["invalidSnapshot"],"sources":["../../../../src/runtime/intelligence-platform/skill-registry/archive.ts"],"sourcesContent":["import { fromBuffer } from \"yauzl\";\nimport type { Entry, ZipFile } from \"yauzl\";\nimport type { Readable } from \"node:stream\";\nimport { invalidSnapshot } from \"./errors\";\n\n/** Internal corruption bounds, including manifest and directory entries. */\nexport const MAX_ARCHIVE_ENTRIES = 1_000;\nexport const MAX_SNAPSHOT_BYTES = 32 * 1024 * 1024;\n\nexport function safeRelativePath(path: string): boolean {\n return (\n path.length > 0 &&\n // Reject control bytes in archive paths, including NUL and DEL.\n // eslint-disable-next-line no-control-regex\n !/[\\\\\\x00-\\x1f\\x7f]/.test(path) &&\n !/^[a-z]:/i.test(path) &&\n path\n .split(\"/\")\n .every((part) => part !== \"\" && part !== \".\" && part !== \"..\")\n );\n}\n\n/** Decode only the listed ZIP namespace. No filesystem is involved. */\nexport function readArchive(\n bytes: Buffer,\n signal?: AbortSignal,\n): Promise<Map<string, Buffer>> {\n return new Promise((resolve, reject) => {\n let archive: ZipFile | undefined;\n let stream: Readable | undefined;\n let settled = false;\n const finish = (error?: unknown) => {\n if (settled) return;\n settled = true;\n signal?.removeEventListener(\"abort\", abort);\n stream?.destroy();\n archive?.close();\n if (error !== undefined) reject(error);\n else resolve(files);\n };\n const abort = () => finish(signal?.reason ?? invalidSnapshot());\n const files = new Map<string, Buffer>();\n const names = new Set<string>();\n let decodedBytes = 0;\n signal?.addEventListener(\"abort\", abort, { once: true });\n if (signal?.aborted) {\n abort();\n return;\n }\n fromBuffer(\n bytes,\n {\n lazyEntries: true,\n decodeStrings: false,\n validateEntrySizes: true,\n autoClose: false,\n },\n (error, zip) => {\n if (error) {\n finish(invalidSnapshot(error));\n return;\n }\n archive = zip;\n if (settled) {\n zip.close();\n return;\n }\n zip.on(\"error\", (cause: unknown) => finish(invalidSnapshot(cause)));\n zip.on(\"end\", () => finish());\n if (zip.entryCount > MAX_ARCHIVE_ENTRIES) {\n finish(invalidSnapshot());\n return;\n }\n zip.on(\"entry\", (entry: Entry) => {\n try {\n // decodeStrings:false preserves raw UTF-8 names instead of normalizing\n // backslashes or consulting alternate Unicode-path extra fields.\n const rawName: unknown = entry.fileName;\n if (!Buffer.isBuffer(rawName)) throw invalidSnapshot();\n const name = new TextDecoder(\"utf-8\", {\n fatal: true,\n ignoreBOM: true,\n }).decode(rawName);\n const directory = name.endsWith(\"/\");\n if (\n !safeRelativePath(directory ? name.slice(0, -1) : name) ||\n names.has(name)\n )\n throw invalidSnapshot();\n names.add(name);\n const fileType = (entry.externalFileAttributes >>> 16) & 0o170000;\n if (\n fileType !== 0 &&\n fileType !== (directory ? 0o040000 : 0o100000)\n )\n throw invalidSnapshot();\n if (\n entry.isEncrypted() ||\n ![0, 8].includes(entry.compressionMethod)\n )\n throw invalidSnapshot();\n if (\n !Number.isSafeInteger(entry.uncompressedSize) ||\n entry.uncompressedSize < 0\n )\n throw invalidSnapshot();\n decodedBytes += entry.uncompressedSize;\n if (\n decodedBytes > MAX_SNAPSHOT_BYTES ||\n (directory && entry.uncompressedSize !== 0)\n )\n throw invalidSnapshot();\n // Require the central directory and the local entry to name the same\n // bytes. ZIP data descriptors may leave local sizes unset.\n const local = entry.relativeOffsetOfLocalHeader;\n if (\n local < 0 ||\n local + 30 > bytes.length ||\n bytes.readUInt32LE(local) !== 0x04034b50\n )\n throw invalidSnapshot();\n if (\n bytes.readUInt16LE(local + 6) !== entry.generalPurposeBitFlag ||\n bytes.readUInt16LE(local + 8) !== entry.compressionMethod\n )\n throw invalidSnapshot();\n const localNameLength = bytes.readUInt16LE(local + 26);\n const localExtraLength = bytes.readUInt16LE(local + 28);\n const dataStart = local + 30 + localNameLength + localExtraLength;\n if (\n dataStart + entry.compressedSize > bytes.length ||\n !bytes\n .subarray(local + 30, local + 30 + localNameLength)\n .equals(rawName)\n )\n throw invalidSnapshot();\n if (\n !(entry.generalPurposeBitFlag & 8) &&\n (bytes.readUInt32LE(local + 18) !== entry.compressedSize ||\n bytes.readUInt32LE(local + 22) !== entry.uncompressedSize)\n )\n throw invalidSnapshot();\n if (directory) {\n zip.readEntry();\n return;\n }\n zip.openReadStream(entry, (readError, readStream) => {\n if (readError) {\n finish(invalidSnapshot(readError));\n return;\n }\n stream = readStream;\n if (settled) {\n readStream.destroy();\n return;\n }\n const chunks: Buffer[] = [];\n let size = 0;\n readStream.on(\"error\", (cause: unknown) =>\n finish(invalidSnapshot(cause)),\n );\n readStream.on(\"data\", (chunk: Buffer) => {\n size += chunk.length;\n if (\n size > entry.uncompressedSize ||\n size > MAX_SNAPSHOT_BYTES\n ) {\n finish(invalidSnapshot());\n return;\n }\n chunks.push(chunk);\n });\n readStream.on(\"end\", () => {\n if (settled) return;\n if (size !== entry.uncompressedSize) {\n finish(invalidSnapshot());\n return;\n }\n files.set(name, Buffer.concat(chunks, size));\n stream = undefined;\n zip.readEntry();\n });\n });\n } catch (cause) {\n finish(invalidSnapshot(cause));\n }\n });\n zip.readEntry();\n },\n );\n });\n}\n"],"mappings":";;;;;;AAMA,MAAa,sBAAsB;AACnC,MAAa,qBAAqB,KAAK,OAAO;AAE9C,SAAgB,iBAAiB,MAAuB;AACtD,Q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B,SAAI,WAAW;AACb,UAAI,WAAW;AACf;;AAEF,SAAI,eAAe,QAAQ,WAAW,eAAe;AACnD,UAAI,WAAW;AACb,cAAOA,+BAAgB,UAAU,CAAC;AAClC;;AAEF,eAAS;AACT,UAAI,SAAS;AACX,kBAAW,SAAS;AACpB;;MAEF,MAAM,SAAmB,EAAE;MAC3B,IAAI,OAAO;AACX,iBAAW,GAAG,UAAU,UACtB,OAAOA,+BAAgB,MAAM,CAAC,CAC/B;AACD,iBAAW,GAAG,SAAS,UAAkB;AACvC,eAAQ,MAAM;AACd,WACE,OAAO,MAAM,oBACb,OAAO,oBACP;AACA,eAAOA,gCAAiB,CAAC;AACzB;;AAEF,cAAO,KAAK,MAAM;QAClB;AACF,iBAAW,GAAG,aAAa;AACzB,WAAI,QAAS;AACb,WAAI,SAAS,MAAM,kBAAkB;AACnC,eAAOA,gCAAiB,CAAC;AACzB;;AAEF,aAAM,IAAI,MAAM,OAAO,OAAO,QAAQ,KAAK,CAAC;AAC5C,gBAAS;AACT,WAAI,WAAW;QACf;OACF;aACK,OAAO;AACd,YAAOA,+BAAgB,MAAM,CAAC;;KAEhC;AACF,OAAI,WAAW;IAElB;GACD"}
1
+ {"version":3,"file":"archive.cjs","names":["Inflate","invalidSnapshot"],"sources":["../../../../src/runtime/intelligence-platform/skill-registry/archive.ts"],"sourcesContent":["import { Inflate } from \"fflate\";\nimport { invalidSnapshot } from \"./errors\";\n\n/** Internal corruption bounds, including manifest and directory entries. */\nexport const MAX_ARCHIVE_ENTRIES = 1_000;\nexport const MAX_SNAPSHOT_BYTES = 32 * 1024 * 1024;\n\nconst LOCAL_HEADER_SIGNATURE = 0x04034b50;\nconst CENTRAL_HEADER_SIGNATURE = 0x02014b50;\nconst END_OF_CENTRAL_DIRECTORY_SIGNATURE = 0x06054b50;\nconst LOCAL_HEADER_SIZE = 30;\nconst CENTRAL_HEADER_SIZE = 46;\nconst END_OF_CENTRAL_DIRECTORY_SIZE = 22;\n// DEFLATE expands at most ~1032:1, so one pushed chunk decodes to ~16 MiB at\n// most before the size check below can stop a bomb.\nconst INFLATE_CHUNK_BYTES = 16 * 1024;\n\nexport function safeRelativePath(path: string): boolean {\n return (\n path.length > 0 &&\n // Reject control bytes in archive paths, including NUL and DEL.\n // eslint-disable-next-line no-control-regex\n !/[\\\\\\x00-\\x1f\\x7f]/.test(path) &&\n !/^[a-z]:/i.test(path) &&\n path\n .split(\"/\")\n .every((part) => part !== \"\" && part !== \".\" && part !== \"..\")\n );\n}\n\nfunction equalBytes(left: Uint8Array, right: Uint8Array): boolean {\n return (\n left.length === right.length &&\n left.every((byte, index) => byte === right[index])\n );\n}\n\n/** Inflate raw DEFLATE data, failing as soon as it decodes past `size` bytes. */\nfunction inflate(data: Uint8Array, size: number): Uint8Array {\n const out = new Uint8Array(size);\n let written = 0;\n const inflater = new Inflate((chunk) => {\n if (written + chunk.length > size) throw invalidSnapshot();\n out.set(chunk, written);\n written += chunk.length;\n });\n let offset = 0;\n do {\n const end = Math.min(offset + INFLATE_CHUNK_BYTES, data.length);\n inflater.push(data.subarray(offset, end), end === data.length);\n offset = end;\n } while (offset < data.length);\n if (written !== size) throw invalidSnapshot();\n return out;\n}\n\n/** Find the end-of-central-directory record; its comment must end the file. */\nfunction findEndOfCentralDirectory(bytes: Uint8Array, view: DataView): number {\n const last = bytes.length - END_OF_CENTRAL_DIRECTORY_SIZE;\n for (let offset = last; offset >= Math.max(0, last - 0xffff); offset--) {\n if (view.getUint32(offset, true) !== END_OF_CENTRAL_DIRECTORY_SIGNATURE)\n continue;\n if (view.getUint16(offset + 20, true) !== last - offset)\n throw invalidSnapshot();\n return offset;\n }\n throw invalidSnapshot();\n}\n\n/**\n * Decode only the listed ZIP namespace. No filesystem is involved.\n *\n * Parses the central directory directly and inflates with fflate, so it runs\n * on Node and on edge runtimes alike. ZIP64, multi-disk, and encrypted\n * archives are rejected: snapshots stay far below the ZIP64 limits.\n */\nexport async function readArchive(\n bytes: Uint8Array,\n signal?: AbortSignal,\n): Promise<Map<string, Uint8Array>> {\n const throwIfAborted = () => {\n if (signal?.aborted) throw signal.reason ?? invalidSnapshot();\n };\n throwIfAborted();\n try {\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);\n const end = findEndOfCentralDirectory(bytes, view);\n if (view.getUint16(end + 4, true) !== 0) throw invalidSnapshot();\n const entryCount = view.getUint16(end + 10, true);\n if (entryCount > MAX_ARCHIVE_ENTRIES) throw invalidSnapshot();\n\n const files = new Map<string, Uint8Array>();\n const names = new Set<string>();\n let decodedBytes = 0;\n let header = view.getUint32(end + 16, true);\n for (let index = 0; index < entryCount; index++) {\n throwIfAborted();\n if (\n header + CENTRAL_HEADER_SIZE > bytes.length ||\n view.getUint32(header, true) !== CENTRAL_HEADER_SIGNATURE\n )\n throw invalidSnapshot();\n const generalPurposeBitFlag = view.getUint16(header + 8, true);\n const compressionMethod = view.getUint16(header + 10, true);\n const compressedSize = view.getUint32(header + 20, true);\n const uncompressedSize = view.getUint32(header + 24, true);\n const nameLength = view.getUint16(header + 28, true);\n const extraLength = view.getUint16(header + 30, true);\n const commentLength = view.getUint16(header + 32, true);\n const externalFileAttributes = view.getUint32(header + 38, true);\n const local = view.getUint32(header + 42, true);\n const nameStart = header + CENTRAL_HEADER_SIZE;\n header = nameStart + nameLength + extraLength + commentLength;\n if (header > bytes.length) throw invalidSnapshot();\n\n // Decode the raw UTF-8 name ourselves instead of normalizing backslashes\n // or consulting alternate Unicode-path extra fields.\n const rawName = bytes.subarray(nameStart, nameStart + nameLength);\n const name = new TextDecoder(\"utf-8\", {\n fatal: true,\n ignoreBOM: true,\n }).decode(rawName);\n const directory = name.endsWith(\"/\");\n if (\n !safeRelativePath(directory ? name.slice(0, -1) : name) ||\n names.has(name)\n )\n throw invalidSnapshot();\n names.add(name);\n const fileType = (externalFileAttributes >>> 16) & 0o170000;\n if (fileType !== 0 && fileType !== (directory ? 0o040000 : 0o100000))\n throw invalidSnapshot();\n // Bit 0 is traditional encryption, bit 6 strong encryption.\n if (generalPurposeBitFlag & 0x41 || ![0, 8].includes(compressionMethod))\n throw invalidSnapshot();\n if (compressionMethod === 0 && compressedSize !== uncompressedSize)\n throw invalidSnapshot();\n decodedBytes += uncompressedSize;\n if (\n decodedBytes > MAX_SNAPSHOT_BYTES ||\n (directory && uncompressedSize !== 0)\n )\n throw invalidSnapshot();\n // Require the central directory and the local entry to name the same\n // bytes. ZIP data descriptors may leave local sizes unset.\n if (\n local + LOCAL_HEADER_SIZE > bytes.length ||\n view.getUint32(local, true) !== LOCAL_HEADER_SIGNATURE\n )\n throw invalidSnapshot();\n if (\n view.getUint16(local + 6, true) !== generalPurposeBitFlag ||\n view.getUint16(local + 8, true) !== compressionMethod\n )\n throw invalidSnapshot();\n const localNameLength = view.getUint16(local + 26, true);\n const localExtraLength = view.getUint16(local + 28, true);\n const localNameStart = local + LOCAL_HEADER_SIZE;\n const dataStart = localNameStart + localNameLength + localExtraLength;\n if (\n dataStart + compressedSize > bytes.length ||\n !equalBytes(\n bytes.subarray(localNameStart, localNameStart + localNameLength),\n rawName,\n )\n )\n throw invalidSnapshot();\n if (\n !(generalPurposeBitFlag & 8) &&\n (view.getUint32(local + 18, true) !== compressedSize ||\n view.getUint32(local + 22, true) !== uncompressedSize)\n )\n throw invalidSnapshot();\n if (directory) continue;\n\n const data = bytes.subarray(dataStart, dataStart + compressedSize);\n files.set(\n name,\n compressionMethod === 0\n ? data.slice()\n : inflate(data, uncompressedSize),\n );\n }\n throwIfAborted();\n return files;\n } catch (cause) {\n throwIfAborted();\n throw invalidSnapshot(cause);\n }\n}\n"],"mappings":";;;;;;AAIA,MAAa,sBAAsB;AACnC,MAAa,qBAAqB,KAAK,OAAO;AAE9C,MAAM,yBAAyB;AAC/B,MAAM,2BAA2B;AACjC,MAAM,qCAAqC;AAC3C,MAAM,oBAAoB;AAC1B,MAAM,sBAAsB;AAC5B,MAAM,gCAAgC;AAGtC,MAAM,sBAAsB,KAAK;AAEjC,SAAgB,iBAAiB,MAAuB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AAO,GACZ,QAAQ,MAAM,iBAAiB,CACpC;;AAEH,kBAAgB;AAChB,SAAO;UACA,OAAO;AACd,kBAAgB;AAChB,QAAMA,+BAAgB,MAAM"}
@@ -3,8 +3,14 @@
3
3
  declare const MAX_ARCHIVE_ENTRIES = 1000;
4
4
  declare const MAX_SNAPSHOT_BYTES: number;
5
5
  declare function safeRelativePath(path: string): boolean;
6
- /** Decode only the listed ZIP namespace. No filesystem is involved. */
7
- declare function readArchive(bytes: Buffer, signal?: AbortSignal): Promise<Map<string, Buffer>>;
6
+ /**
7
+ * Decode only the listed ZIP namespace. No filesystem is involved.
8
+ *
9
+ * Parses the central directory directly and inflates with fflate, so it runs
10
+ * on Node and on edge runtimes alike. ZIP64, multi-disk, and encrypted
11
+ * archives are rejected: snapshots stay far below the ZIP64 limits.
12
+ */
13
+ declare function readArchive(bytes: Uint8Array, signal?: AbortSignal): Promise<Map<string, Uint8Array>>;
8
14
  //#endregion
9
15
  export { MAX_ARCHIVE_ENTRIES, MAX_SNAPSHOT_BYTES, readArchive, safeRelativePath };
10
16
  //# sourceMappingURL=archive.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"archive.d.cts","names":[],"sources":["../../../../src/runtime/intelligence-platform/skill-registry/archive.ts"],"mappings":";;cAMa,mBAAA;AAAA,cACA,kBAAA;AAAA,iBAEG,gBAAA,CAAiB,IAAA;;iBAcjB,WAAA,CACd,KAAA,EAAO,MAAA,EACP,MAAA,GAAS,WAAA,GACR,OAAA,CAAQ,GAAA,SAAY,MAAA"}
1
+ {"version":3,"file":"archive.d.cts","names":[],"sources":["../../../../src/runtime/intelligence-platform/skill-registry/archive.ts"],"mappings":";;cAIa,mBAAA;AAAA,cACA,kBAAA;AAAA,iBAYG,gBAAA,CAAiB,IAAA;;;AAZjC;;;;;iBAuEsB,WAAA,CACpB,KAAA,EAAO,UAAA,EACP,MAAA,GAAS,WAAA,GACR,OAAA,CAAQ,GAAA,SAAY,UAAA"}
@@ -3,8 +3,14 @@
3
3
  declare const MAX_ARCHIVE_ENTRIES = 1000;
4
4
  declare const MAX_SNAPSHOT_BYTES: number;
5
5
  declare function safeRelativePath(path: string): boolean;
6
- /** Decode only the listed ZIP namespace. No filesystem is involved. */
7
- declare function readArchive(bytes: Buffer, signal?: AbortSignal): Promise<Map<string, Buffer>>;
6
+ /**
7
+ * Decode only the listed ZIP namespace. No filesystem is involved.
8
+ *
9
+ * Parses the central directory directly and inflates with fflate, so it runs
10
+ * on Node and on edge runtimes alike. ZIP64, multi-disk, and encrypted
11
+ * archives are rejected: snapshots stay far below the ZIP64 limits.
12
+ */
13
+ declare function readArchive(bytes: Uint8Array, signal?: AbortSignal): Promise<Map<string, Uint8Array>>;
8
14
  //#endregion
9
15
  export { MAX_ARCHIVE_ENTRIES, MAX_SNAPSHOT_BYTES, readArchive, safeRelativePath };
10
16
  //# sourceMappingURL=archive.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"archive.d.mts","names":[],"sources":["../../../../src/runtime/intelligence-platform/skill-registry/archive.ts"],"mappings":";;cAMa,mBAAA;AAAA,cACA,kBAAA;AAAA,iBAEG,gBAAA,CAAiB,IAAA;;iBAcjB,WAAA,CACd,KAAA,EAAO,MAAA,EACP,MAAA,GAAS,WAAA,GACR,OAAA,CAAQ,GAAA,SAAY,MAAA"}
1
+ {"version":3,"file":"archive.d.mts","names":[],"sources":["../../../../src/runtime/intelligence-platform/skill-registry/archive.ts"],"mappings":";;cAIa,mBAAA;AAAA,cACA,kBAAA;AAAA,iBAYG,gBAAA,CAAiB,IAAA;;;AAZjC;;;;;iBAuEsB,WAAA,CACpB,KAAA,EAAO,UAAA,EACP,MAAA,GAAS,WAAA,GACR,OAAA,CAAQ,GAAA,SAAY,UAAA"}
@@ -1,125 +1,120 @@
1
1
  import { invalidSnapshot } from "./errors.mjs";
2
- import { fromBuffer } from "yauzl";
2
+ import { Inflate } from "fflate";
3
3
 
4
4
  //#region src/runtime/intelligence-platform/skill-registry/archive.ts
5
5
  /** Internal corruption bounds, including manifest and directory entries. */
6
6
  const MAX_ARCHIVE_ENTRIES = 1e3;
7
7
  const MAX_SNAPSHOT_BYTES = 32 * 1024 * 1024;
8
+ const LOCAL_HEADER_SIGNATURE = 67324752;
9
+ const CENTRAL_HEADER_SIGNATURE = 33639248;
10
+ const END_OF_CENTRAL_DIRECTORY_SIGNATURE = 101010256;
11
+ const LOCAL_HEADER_SIZE = 30;
12
+ const CENTRAL_HEADER_SIZE = 46;
13
+ const END_OF_CENTRAL_DIRECTORY_SIZE = 22;
14
+ const INFLATE_CHUNK_BYTES = 16 * 1024;
8
15
  function safeRelativePath(path) {
9
16
  return path.length > 0 && !/[\\\x00-\x1f\x7f]/.test(path) && !/^[a-z]:/i.test(path) && path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
10
17
  }
11
- /** Decode only the listed ZIP namespace. No filesystem is involved. */
12
- function readArchive(bytes, signal) {
13
- return new Promise((resolve, reject) => {
14
- let archive;
15
- let stream;
16
- let settled = false;
17
- const finish = (error) => {
18
- if (settled) return;
19
- settled = true;
20
- signal?.removeEventListener("abort", abort);
21
- stream?.destroy();
22
- archive?.close();
23
- if (error !== void 0) reject(error);
24
- else resolve(files);
25
- };
26
- const abort = () => finish(signal?.reason ?? invalidSnapshot());
18
+ function equalBytes(left, right) {
19
+ return left.length === right.length && left.every((byte, index) => byte === right[index]);
20
+ }
21
+ /** Inflate raw DEFLATE data, failing as soon as it decodes past `size` bytes. */
22
+ function inflate(data, size) {
23
+ const out = new Uint8Array(size);
24
+ let written = 0;
25
+ const inflater = new Inflate((chunk) => {
26
+ if (written + chunk.length > size) throw invalidSnapshot();
27
+ out.set(chunk, written);
28
+ written += chunk.length;
29
+ });
30
+ let offset = 0;
31
+ do {
32
+ const end = Math.min(offset + INFLATE_CHUNK_BYTES, data.length);
33
+ inflater.push(data.subarray(offset, end), end === data.length);
34
+ offset = end;
35
+ } while (offset < data.length);
36
+ if (written !== size) throw invalidSnapshot();
37
+ return out;
38
+ }
39
+ /** Find the end-of-central-directory record; its comment must end the file. */
40
+ function findEndOfCentralDirectory(bytes, view) {
41
+ const last = bytes.length - END_OF_CENTRAL_DIRECTORY_SIZE;
42
+ for (let offset = last; offset >= Math.max(0, last - 65535); offset--) {
43
+ if (view.getUint32(offset, true) !== END_OF_CENTRAL_DIRECTORY_SIGNATURE) continue;
44
+ if (view.getUint16(offset + 20, true) !== last - offset) throw invalidSnapshot();
45
+ return offset;
46
+ }
47
+ throw invalidSnapshot();
48
+ }
49
+ /**
50
+ * Decode only the listed ZIP namespace. No filesystem is involved.
51
+ *
52
+ * Parses the central directory directly and inflates with fflate, so it runs
53
+ * on Node and on edge runtimes alike. ZIP64, multi-disk, and encrypted
54
+ * archives are rejected: snapshots stay far below the ZIP64 limits.
55
+ */
56
+ async function readArchive(bytes, signal) {
57
+ const throwIfAborted = () => {
58
+ if (signal?.aborted) throw signal.reason ?? invalidSnapshot();
59
+ };
60
+ throwIfAborted();
61
+ try {
62
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
63
+ const end = findEndOfCentralDirectory(bytes, view);
64
+ if (view.getUint16(end + 4, true) !== 0) throw invalidSnapshot();
65
+ const entryCount = view.getUint16(end + 10, true);
66
+ if (entryCount > MAX_ARCHIVE_ENTRIES) throw invalidSnapshot();
27
67
  const files = /* @__PURE__ */ new Map();
28
68
  const names = /* @__PURE__ */ new Set();
29
69
  let decodedBytes = 0;
30
- signal?.addEventListener("abort", abort, { once: true });
31
- if (signal?.aborted) {
32
- abort();
33
- return;
70
+ let header = view.getUint32(end + 16, true);
71
+ for (let index = 0; index < entryCount; index++) {
72
+ throwIfAborted();
73
+ if (header + CENTRAL_HEADER_SIZE > bytes.length || view.getUint32(header, true) !== CENTRAL_HEADER_SIGNATURE) throw invalidSnapshot();
74
+ const generalPurposeBitFlag = view.getUint16(header + 8, true);
75
+ const compressionMethod = view.getUint16(header + 10, true);
76
+ const compressedSize = view.getUint32(header + 20, true);
77
+ const uncompressedSize = view.getUint32(header + 24, true);
78
+ const nameLength = view.getUint16(header + 28, true);
79
+ const extraLength = view.getUint16(header + 30, true);
80
+ const commentLength = view.getUint16(header + 32, true);
81
+ const externalFileAttributes = view.getUint32(header + 38, true);
82
+ const local = view.getUint32(header + 42, true);
83
+ const nameStart = header + CENTRAL_HEADER_SIZE;
84
+ header = nameStart + nameLength + extraLength + commentLength;
85
+ if (header > bytes.length) throw invalidSnapshot();
86
+ const rawName = bytes.subarray(nameStart, nameStart + nameLength);
87
+ const name = new TextDecoder("utf-8", {
88
+ fatal: true,
89
+ ignoreBOM: true
90
+ }).decode(rawName);
91
+ const directory = name.endsWith("/");
92
+ if (!safeRelativePath(directory ? name.slice(0, -1) : name) || names.has(name)) throw invalidSnapshot();
93
+ names.add(name);
94
+ const fileType = externalFileAttributes >>> 16 & 61440;
95
+ if (fileType !== 0 && fileType !== (directory ? 16384 : 32768)) throw invalidSnapshot();
96
+ if (generalPurposeBitFlag & 65 || ![0, 8].includes(compressionMethod)) throw invalidSnapshot();
97
+ if (compressionMethod === 0 && compressedSize !== uncompressedSize) throw invalidSnapshot();
98
+ decodedBytes += uncompressedSize;
99
+ if (decodedBytes > MAX_SNAPSHOT_BYTES || directory && uncompressedSize !== 0) throw invalidSnapshot();
100
+ if (local + LOCAL_HEADER_SIZE > bytes.length || view.getUint32(local, true) !== LOCAL_HEADER_SIGNATURE) throw invalidSnapshot();
101
+ if (view.getUint16(local + 6, true) !== generalPurposeBitFlag || view.getUint16(local + 8, true) !== compressionMethod) throw invalidSnapshot();
102
+ const localNameLength = view.getUint16(local + 26, true);
103
+ const localExtraLength = view.getUint16(local + 28, true);
104
+ const localNameStart = local + LOCAL_HEADER_SIZE;
105
+ const dataStart = localNameStart + localNameLength + localExtraLength;
106
+ if (dataStart + compressedSize > bytes.length || !equalBytes(bytes.subarray(localNameStart, localNameStart + localNameLength), rawName)) throw invalidSnapshot();
107
+ if (!(generalPurposeBitFlag & 8) && (view.getUint32(local + 18, true) !== compressedSize || view.getUint32(local + 22, true) !== uncompressedSize)) throw invalidSnapshot();
108
+ if (directory) continue;
109
+ const data = bytes.subarray(dataStart, dataStart + compressedSize);
110
+ files.set(name, compressionMethod === 0 ? data.slice() : inflate(data, uncompressedSize));
34
111
  }
35
- fromBuffer(bytes, {
36
- lazyEntries: true,
37
- decodeStrings: false,
38
- validateEntrySizes: true,
39
- autoClose: false
40
- }, (error, zip) => {
41
- if (error) {
42
- finish(invalidSnapshot(error));
43
- return;
44
- }
45
- archive = zip;
46
- if (settled) {
47
- zip.close();
48
- return;
49
- }
50
- zip.on("error", (cause) => finish(invalidSnapshot(cause)));
51
- zip.on("end", () => finish());
52
- if (zip.entryCount > MAX_ARCHIVE_ENTRIES) {
53
- finish(invalidSnapshot());
54
- return;
55
- }
56
- zip.on("entry", (entry) => {
57
- try {
58
- const rawName = entry.fileName;
59
- if (!Buffer.isBuffer(rawName)) throw invalidSnapshot();
60
- const name = new TextDecoder("utf-8", {
61
- fatal: true,
62
- ignoreBOM: true
63
- }).decode(rawName);
64
- const directory = name.endsWith("/");
65
- if (!safeRelativePath(directory ? name.slice(0, -1) : name) || names.has(name)) throw invalidSnapshot();
66
- names.add(name);
67
- const fileType = entry.externalFileAttributes >>> 16 & 61440;
68
- if (fileType !== 0 && fileType !== (directory ? 16384 : 32768)) throw invalidSnapshot();
69
- if (entry.isEncrypted() || ![0, 8].includes(entry.compressionMethod)) throw invalidSnapshot();
70
- if (!Number.isSafeInteger(entry.uncompressedSize) || entry.uncompressedSize < 0) throw invalidSnapshot();
71
- decodedBytes += entry.uncompressedSize;
72
- if (decodedBytes > MAX_SNAPSHOT_BYTES || directory && entry.uncompressedSize !== 0) throw invalidSnapshot();
73
- const local = entry.relativeOffsetOfLocalHeader;
74
- if (local < 0 || local + 30 > bytes.length || bytes.readUInt32LE(local) !== 67324752) throw invalidSnapshot();
75
- if (bytes.readUInt16LE(local + 6) !== entry.generalPurposeBitFlag || bytes.readUInt16LE(local + 8) !== entry.compressionMethod) throw invalidSnapshot();
76
- const localNameLength = bytes.readUInt16LE(local + 26);
77
- const localExtraLength = bytes.readUInt16LE(local + 28);
78
- if (local + 30 + localNameLength + localExtraLength + entry.compressedSize > bytes.length || !bytes.subarray(local + 30, local + 30 + localNameLength).equals(rawName)) throw invalidSnapshot();
79
- if (!(entry.generalPurposeBitFlag & 8) && (bytes.readUInt32LE(local + 18) !== entry.compressedSize || bytes.readUInt32LE(local + 22) !== entry.uncompressedSize)) throw invalidSnapshot();
80
- if (directory) {
81
- zip.readEntry();
82
- return;
83
- }
84
- zip.openReadStream(entry, (readError, readStream) => {
85
- if (readError) {
86
- finish(invalidSnapshot(readError));
87
- return;
88
- }
89
- stream = readStream;
90
- if (settled) {
91
- readStream.destroy();
92
- return;
93
- }
94
- const chunks = [];
95
- let size = 0;
96
- readStream.on("error", (cause) => finish(invalidSnapshot(cause)));
97
- readStream.on("data", (chunk) => {
98
- size += chunk.length;
99
- if (size > entry.uncompressedSize || size > MAX_SNAPSHOT_BYTES) {
100
- finish(invalidSnapshot());
101
- return;
102
- }
103
- chunks.push(chunk);
104
- });
105
- readStream.on("end", () => {
106
- if (settled) return;
107
- if (size !== entry.uncompressedSize) {
108
- finish(invalidSnapshot());
109
- return;
110
- }
111
- files.set(name, Buffer.concat(chunks, size));
112
- stream = void 0;
113
- zip.readEntry();
114
- });
115
- });
116
- } catch (cause) {
117
- finish(invalidSnapshot(cause));
118
- }
119
- });
120
- zip.readEntry();
121
- });
122
- });
112
+ throwIfAborted();
113
+ return files;
114
+ } catch (cause) {
115
+ throwIfAborted();
116
+ throw invalidSnapshot(cause);
117
+ }
123
118
  }
124
119
 
125
120
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"archive.mjs","names":[],"sources":["../../../../src/runtime/intelligence-platform/skill-registry/archive.ts"],"sourcesContent":["import { fromBuffer } from \"yauzl\";\nimport type { Entry, ZipFile } from \"yauzl\";\nimport type { Readable } from \"node:stream\";\nimport { invalidSnapshot } from \"./errors\";\n\n/** Internal corruption bounds, including manifest and directory entries. */\nexport const MAX_ARCHIVE_ENTRIES = 1_000;\nexport const MAX_SNAPSHOT_BYTES = 32 * 1024 * 1024;\n\nexport function safeRelativePath(path: string): boolean {\n return (\n path.length > 0 &&\n // Reject control bytes in archive paths, including NUL and DEL.\n // eslint-disable-next-line no-control-regex\n !/[\\\\\\x00-\\x1f\\x7f]/.test(path) &&\n !/^[a-z]:/i.test(path) &&\n path\n .split(\"/\")\n .every((part) => part !== \"\" && part !== \".\" && part !== \"..\")\n );\n}\n\n/** Decode only the listed ZIP namespace. No filesystem is involved. */\nexport function readArchive(\n bytes: Buffer,\n signal?: AbortSignal,\n): Promise<Map<string, Buffer>> {\n return new Promise((resolve, reject) => {\n let archive: ZipFile | undefined;\n let stream: Readable | undefined;\n let settled = false;\n const finish = (error?: unknown) => {\n if (settled) return;\n settled = true;\n signal?.removeEventListener(\"abort\", abort);\n stream?.destroy();\n archive?.close();\n if (error !== undefined) reject(error);\n else resolve(files);\n };\n const abort = () => finish(signal?.reason ?? invalidSnapshot());\n const files = new Map<string, Buffer>();\n const names = new Set<string>();\n let decodedBytes = 0;\n signal?.addEventListener(\"abort\", abort, { once: true });\n if (signal?.aborted) {\n abort();\n return;\n }\n fromBuffer(\n bytes,\n {\n lazyEntries: true,\n decodeStrings: false,\n validateEntrySizes: true,\n autoClose: false,\n },\n (error, zip) => {\n if (error) {\n finish(invalidSnapshot(error));\n return;\n }\n archive = zip;\n if (settled) {\n zip.close();\n return;\n }\n zip.on(\"error\", (cause: unknown) => finish(invalidSnapshot(cause)));\n zip.on(\"end\", () => finish());\n if (zip.entryCount > MAX_ARCHIVE_ENTRIES) {\n finish(invalidSnapshot());\n return;\n }\n zip.on(\"entry\", (entry: Entry) => {\n try {\n // decodeStrings:false preserves raw UTF-8 names instead of normalizing\n // backslashes or consulting alternate Unicode-path extra fields.\n const rawName: unknown = entry.fileName;\n if (!Buffer.isBuffer(rawName)) throw invalidSnapshot();\n const name = new TextDecoder(\"utf-8\", {\n fatal: true,\n ignoreBOM: true,\n }).decode(rawName);\n const directory = name.endsWith(\"/\");\n if (\n !safeRelativePath(directory ? name.slice(0, -1) : name) ||\n names.has(name)\n )\n throw invalidSnapshot();\n names.add(name);\n const fileType = (entry.externalFileAttributes >>> 16) & 0o170000;\n if (\n fileType !== 0 &&\n fileType !== (directory ? 0o040000 : 0o100000)\n )\n throw invalidSnapshot();\n if (\n entry.isEncrypted() ||\n ![0, 8].includes(entry.compressionMethod)\n )\n throw invalidSnapshot();\n if (\n !Number.isSafeInteger(entry.uncompressedSize) ||\n entry.uncompressedSize < 0\n )\n throw invalidSnapshot();\n decodedBytes += entry.uncompressedSize;\n if (\n decodedBytes > MAX_SNAPSHOT_BYTES ||\n (directory && entry.uncompressedSize !== 0)\n )\n throw invalidSnapshot();\n // Require the central directory and the local entry to name the same\n // bytes. ZIP data descriptors may leave local sizes unset.\n const local = entry.relativeOffsetOfLocalHeader;\n if (\n local < 0 ||\n local + 30 > bytes.length ||\n bytes.readUInt32LE(local) !== 0x04034b50\n )\n throw invalidSnapshot();\n if (\n bytes.readUInt16LE(local + 6) !== entry.generalPurposeBitFlag ||\n bytes.readUInt16LE(local + 8) !== entry.compressionMethod\n )\n throw invalidSnapshot();\n const localNameLength = bytes.readUInt16LE(local + 26);\n const localExtraLength = bytes.readUInt16LE(local + 28);\n const dataStart = local + 30 + localNameLength + localExtraLength;\n if (\n dataStart + entry.compressedSize > bytes.length ||\n !bytes\n .subarray(local + 30, local + 30 + localNameLength)\n .equals(rawName)\n )\n throw invalidSnapshot();\n if (\n !(entry.generalPurposeBitFlag & 8) &&\n (bytes.readUInt32LE(local + 18) !== entry.compressedSize ||\n bytes.readUInt32LE(local + 22) !== entry.uncompressedSize)\n )\n throw invalidSnapshot();\n if (directory) {\n zip.readEntry();\n return;\n }\n zip.openReadStream(entry, (readError, readStream) => {\n if (readError) {\n finish(invalidSnapshot(readError));\n return;\n }\n stream = readStream;\n if (settled) {\n readStream.destroy();\n return;\n }\n const chunks: Buffer[] = [];\n let size = 0;\n readStream.on(\"error\", (cause: unknown) =>\n finish(invalidSnapshot(cause)),\n );\n readStream.on(\"data\", (chunk: Buffer) => {\n size += chunk.length;\n if (\n size > entry.uncompressedSize ||\n size > MAX_SNAPSHOT_BYTES\n ) {\n finish(invalidSnapshot());\n return;\n }\n chunks.push(chunk);\n });\n readStream.on(\"end\", () => {\n if (settled) return;\n if (size !== entry.uncompressedSize) {\n finish(invalidSnapshot());\n return;\n }\n files.set(name, Buffer.concat(chunks, size));\n stream = undefined;\n zip.readEntry();\n });\n });\n } catch (cause) {\n finish(invalidSnapshot(cause));\n }\n });\n zip.readEntry();\n },\n );\n });\n}\n"],"mappings":";;;;;AAMA,MAAa,sBAAsB;AACnC,MAAa,qBAAqB,KAAK,OAAO;AAE9C,SAAgB,iBAAiB,MAAuB;AACtD,QACE,KAAK,SAAS,KAGd,CAAC,oBAAoB,KAAK,KAAK,IAC/B,CAAC,WAAW,KAAK,KAAK,IACtB,KACG,MAAM,IAAI,CACV,OAAO,SAAS,SAAS,MAAM,SAAS,OAAO,SAAS,KAAK;;;AAKpE,SAAgB,YACd,OACA,QAC8B;AAC9B,QAAO,IAAI,SAAS,SAAS,WAAW;EACtC,IAAI;EACJ,IAAI;EACJ,IAAI,UAAU;EACd,MAAM,UAAU,UAAoB;AAClC,OAAI,QAAS;AACb,aAAU;AACV,WAAQ,oBAAoB,SAAS,MAAM;AAC3C,WAAQ,SAAS;AACjB,YAAS,OAAO;AAChB,OAAI,UAAU,OAAW,QAAO,MAAM;OACjC,SAAQ,MAAM;;EAErB,MAAM,cAAc,OAAO,QAAQ,UAAU,iBAAiB,CAAC;EAC/D,MAAM,wBAAQ,IAAI,KAAqB;EACvC,MAAM,wBAAQ,IAAI,KAAa;EAC/B,IAAI,eAAe;AACnB,UAAQ,iBAAiB,SAAS,OAAO,EAAE,MAAM,MAAM,CAAC;AACxD,MAAI,QAAQ,SAAS;AACnB,UAAO;AACP;;AAEF,aACE,OACA;G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GACD"}
1
+ {"version":3,"file":"archive.mjs","names":[],"sources":["../../../../src/runtime/intelligence-platform/skill-registry/archive.ts"],"sourcesContent":["import { Inflate } from \"fflate\";\nimport { invalidSnapshot } from \"./errors\";\n\n/** Internal corruption bounds, including manifest and directory entries. */\nexport const MAX_ARCHIVE_ENTRIES = 1_000;\nexport const MAX_SNAPSHOT_BYTES = 32 * 1024 * 1024;\n\nconst LOCAL_HEADER_SIGNATURE = 0x04034b50;\nconst CENTRAL_HEADER_SIGNATURE = 0x02014b50;\nconst END_OF_CENTRAL_DIRECTORY_SIGNATURE = 0x06054b50;\nconst LOCAL_HEADER_SIZE = 30;\nconst CENTRAL_HEADER_SIZE = 46;\nconst END_OF_CENTRAL_DIRECTORY_SIZE = 22;\n// DEFLATE expands at most ~1032:1, so one pushed chunk decodes to ~16 MiB at\n// most before the size check below can stop a bomb.\nconst INFLATE_CHUNK_BYTES = 16 * 1024;\n\nexport function safeRelativePath(path: string): boolean {\n return (\n path.length > 0 &&\n // Reject control bytes in archive paths, including NUL and DEL.\n // eslint-disable-next-line no-control-regex\n !/[\\\\\\x00-\\x1f\\x7f]/.test(path) &&\n !/^[a-z]:/i.test(path) &&\n path\n .split(\"/\")\n .every((part) => part !== \"\" && part !== \".\" && part !== \"..\")\n );\n}\n\nfunction equalBytes(left: Uint8Array, right: Uint8Array): boolean {\n return (\n left.length === right.length &&\n left.every((byte, index) => byte === right[index])\n );\n}\n\n/** Inflate raw DEFLATE data, failing as soon as it decodes past `size` bytes. */\nfunction inflate(data: Uint8Array, size: number): Uint8Array {\n const out = new Uint8Array(size);\n let written = 0;\n const inflater = new Inflate((chunk) => {\n if (written + chunk.length > size) throw invalidSnapshot();\n out.set(chunk, written);\n written += chunk.length;\n });\n let offset = 0;\n do {\n const end = Math.min(offset + INFLATE_CHUNK_BYTES, data.length);\n inflater.push(data.subarray(offset, end), end === data.length);\n offset = end;\n } while (offset < data.length);\n if (written !== size) throw invalidSnapshot();\n return out;\n}\n\n/** Find the end-of-central-directory record; its comment must end the file. */\nfunction findEndOfCentralDirectory(bytes: Uint8Array, view: DataView): number {\n const last = bytes.length - END_OF_CENTRAL_DIRECTORY_SIZE;\n for (let offset = last; offset >= Math.max(0, last - 0xffff); offset--) {\n if (view.getUint32(offset, true) !== END_OF_CENTRAL_DIRECTORY_SIGNATURE)\n continue;\n if (view.getUint16(offset + 20, true) !== last - offset)\n throw invalidSnapshot();\n return offset;\n }\n throw invalidSnapshot();\n}\n\n/**\n * Decode only the listed ZIP namespace. No filesystem is involved.\n *\n * Parses the central directory directly and inflates with fflate, so it runs\n * on Node and on edge runtimes alike. ZIP64, multi-disk, and encrypted\n * archives are rejected: snapshots stay far below the ZIP64 limits.\n */\nexport async function readArchive(\n bytes: Uint8Array,\n signal?: AbortSignal,\n): Promise<Map<string, Uint8Array>> {\n const throwIfAborted = () => {\n if (signal?.aborted) throw signal.reason ?? invalidSnapshot();\n };\n throwIfAborted();\n try {\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);\n const end = findEndOfCentralDirectory(bytes, view);\n if (view.getUint16(end + 4, true) !== 0) throw invalidSnapshot();\n const entryCount = view.getUint16(end + 10, true);\n if (entryCount > MAX_ARCHIVE_ENTRIES) throw invalidSnapshot();\n\n const files = new Map<string, Uint8Array>();\n const names = new Set<string>();\n let decodedBytes = 0;\n let header = view.getUint32(end + 16, true);\n for (let index = 0; index < entryCount; index++) {\n throwIfAborted();\n if (\n header + CENTRAL_HEADER_SIZE > bytes.length ||\n view.getUint32(header, true) !== CENTRAL_HEADER_SIGNATURE\n )\n throw invalidSnapshot();\n const generalPurposeBitFlag = view.getUint16(header + 8, true);\n const compressionMethod = view.getUint16(header + 10, true);\n const compressedSize = view.getUint32(header + 20, true);\n const uncompressedSize = view.getUint32(header + 24, true);\n const nameLength = view.getUint16(header + 28, true);\n const extraLength = view.getUint16(header + 30, true);\n const commentLength = view.getUint16(header + 32, true);\n const externalFileAttributes = view.getUint32(header + 38, true);\n const local = view.getUint32(header + 42, true);\n const nameStart = header + CENTRAL_HEADER_SIZE;\n header = nameStart + nameLength + extraLength + commentLength;\n if (header > bytes.length) throw invalidSnapshot();\n\n // Decode the raw UTF-8 name ourselves instead of normalizing backslashes\n // or consulting alternate Unicode-path extra fields.\n const rawName = bytes.subarray(nameStart, nameStart + nameLength);\n const name = new TextDecoder(\"utf-8\", {\n fatal: true,\n ignoreBOM: true,\n }).decode(rawName);\n const directory = name.endsWith(\"/\");\n if (\n !safeRelativePath(directory ? name.slice(0, -1) : name) ||\n names.has(name)\n )\n throw invalidSnapshot();\n names.add(name);\n const fileType = (externalFileAttributes >>> 16) & 0o170000;\n if (fileType !== 0 && fileType !== (directory ? 0o040000 : 0o100000))\n throw invalidSnapshot();\n // Bit 0 is traditional encryption, bit 6 strong encryption.\n if (generalPurposeBitFlag & 0x41 || ![0, 8].includes(compressionMethod))\n throw invalidSnapshot();\n if (compressionMethod === 0 && compressedSize !== uncompressedSize)\n throw invalidSnapshot();\n decodedBytes += uncompressedSize;\n if (\n decodedBytes > MAX_SNAPSHOT_BYTES ||\n (directory && uncompressedSize !== 0)\n )\n throw invalidSnapshot();\n // Require the central directory and the local entry to name the same\n // bytes. ZIP data descriptors may leave local sizes unset.\n if (\n local + LOCAL_HEADER_SIZE > bytes.length ||\n view.getUint32(local, true) !== LOCAL_HEADER_SIGNATURE\n )\n throw invalidSnapshot();\n if (\n view.getUint16(local + 6, true) !== generalPurposeBitFlag ||\n view.getUint16(local + 8, true) !== compressionMethod\n )\n throw invalidSnapshot();\n const localNameLength = view.getUint16(local + 26, true);\n const localExtraLength = view.getUint16(local + 28, true);\n const localNameStart = local + LOCAL_HEADER_SIZE;\n const dataStart = localNameStart + localNameLength + localExtraLength;\n if (\n dataStart + compressedSize > bytes.length ||\n !equalBytes(\n bytes.subarray(localNameStart, localNameStart + localNameLength),\n rawName,\n )\n )\n throw invalidSnapshot();\n if (\n !(generalPurposeBitFlag & 8) &&\n (view.getUint32(local + 18, true) !== compressedSize ||\n view.getUint32(local + 22, true) !== uncompressedSize)\n )\n throw invalidSnapshot();\n if (directory) continue;\n\n const data = bytes.subarray(dataStart, dataStart + compressedSize);\n files.set(\n name,\n compressionMethod === 0\n ? data.slice()\n : inflate(data, uncompressedSize),\n );\n }\n throwIfAborted();\n return files;\n } catch (cause) {\n throwIfAborted();\n throw invalidSnapshot(cause);\n }\n}\n"],"mappings":";;;;;AAIA,MAAa,sBAAsB;AACnC,MAAa,qBAAqB,KAAK,OAAO;AAE9C,MAAM,yBAAyB;AAC/B,MAAM,2BAA2B;AACjC,MAAM,qCAAqC;AAC3C,MAAM,oBAAoB;AAC1B,MAAM,sBAAsB;AAC5B,MAAM,gCAAgC;AAGtC,MAAM,sBAAsB,KAAK;AAEjC,SAAgB,iBAAiB,MAAuB;AACtD,QACE,KAAK,SAAS,KAGd,CAAC,oBAAoB,KAAK,KAAK,IAC/B,CAAC,WAAW,KAAK,KAAK,IACtB,KACG,MAAM,IAAI,CACV,OAAO,SAAS,SAAS,MAAM,SAAS,OAAO,SAAS,KAAK;;AAIpE,SAAS,WAAW,MAAkB,OAA4B;AAChE,QACE,KAAK,WAAW,MAAM,UACtB,KAAK,OAAO,MAAM,UAAU,SAAS,MAAM,OAAO;;;AAKtD,SAAS,QAAQ,MAAkB,MAA0B;CAC3D,MAAM,MAAM,IAAI,WAAW,KAAK;CAChC,IAAI,UAAU;CACd,MAAM,WAAW,IAAI,SAAS,UAAU;AACtC,MAAI,UAAU,MAAM,SAAS,KAAM,OAAM,iBAAiB;AAC1D,MAAI,IAAI,OAAO,QAAQ;AACvB,aAAW,MAAM;GACjB;CACF,IAAI,SAAS;AACb,IAAG;EACD,MAAM,MAAM,KAAK,IAAI,SAAS,qBAAqB,KAAK,OAAO;AAC/D,WAAS,KAAK,KAAK,SAAS,QAAQ,IAAI,EAAE,QAAQ,KAAK,OAAO;AAC9D,WAAS;UACF,SAAS,KAAK;AACvB,KAAI,YAAY,KAAM,OAAM,iBAAiB;AAC7C,QAAO;;;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mB,KAAK,UAAU,QAAQ,IAAI,KAAK;GACzD,MAAM,iBAAiB,QAAQ;GAC/B,MAAM,YAAY,iBAAiB,kBAAkB;AACrD,OACE,YAAY,iBAAiB,MAAM,UACnC,CAAC,WACC,MAAM,SAAS,gBAAgB,iBAAiB,gBAAgB,EAChE,QACD,CAED,OAAM,iBAAiB;AACzB,OACE,EAAE,wBAAwB,OACzB,KAAK,UAAU,QAAQ,IAAI,KAAK,KAAK,kBACpC,KAAK,UAAU,QAAQ,IAAI,KAAK,KAAK,kBAEvC,OAAM,iBAAiB;AACzB,OAAI,UAAW;GAEf,MAAM,OAAO,MAAM,SAAS,WAAW,YAAY,eAAe;AAClE,SAAM,IACJ,MACA,sBAAsB,IAClB,KAAK,OAAO,GACZ,QAAQ,MAAM,iBAAiB,CACpC;;AAEH,kBAAgB;AAChB,SAAO;UACA,OAAO;AACd,kBAAgB;AAChB,QAAM,gBAAgB,MAAM"}
@@ -3,7 +3,7 @@ let _copilotkit_shared = require("@copilotkit/shared");
3
3
  let zod = require("zod");
4
4
 
5
5
  //#region src/runtime/intelligence-platform/trajectories.ts
6
- const trajectoryIdSchema = zod.z.string().uuid();
6
+ const trajectoryIdSchema = zod.z.guid();
7
7
  function isValidTrajectoryId(value) {
8
8
  return trajectoryIdSchema.safeParse(value).success;
9
9
  }
@@ -14,7 +14,7 @@ const joinResponseSchema = zod.z.object({
14
14
  const grantSchema = zod.z.object({
15
15
  joinToken: zod.z.string().refine((value) => value.trim().length > 0),
16
16
  realtime: zod.z.object({
17
- clientUrl: zod.z.string().url().refine((value) => {
17
+ clientUrl: zod.z.url().refine((value) => {
18
18
  const url = new URL(value);
19
19
  return (url.protocol === "wss:" || url.protocol === "ws:") && !url.username && !url.password;
20
20
  }),