@leitwerk-dev/worker-protocol 0.1.9 → 0.2.1
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/{chunk-DVN32ANP.js → chunk-VR7BWTD7.js} +5 -1
- package/dist/chunk-VR7BWTD7.js.map +1 -0
- package/dist/docker-config.d.ts +39 -0
- package/dist/docker-config.d.ts.map +1 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +529 -10
- package/dist/index.js.map +1 -1
- package/dist/ipc-codec.d.ts +22 -0
- package/dist/ipc-codec.d.ts.map +1 -1
- package/dist/ipc-decode.d.ts +7 -0
- package/dist/ipc-decode.d.ts.map +1 -1
- package/dist/ipc-messages.d.ts +339 -25
- package/dist/ipc-messages.d.ts.map +1 -1
- package/dist/pi-resource-bundle.d.ts +13 -2
- package/dist/pi-resource-bundle.d.ts.map +1 -1
- package/dist/pi-resource-manifest.d.ts +35 -1
- package/dist/pi-resource-manifest.d.ts.map +1 -1
- package/dist/testing.d.ts +3 -0
- package/dist/testing.d.ts.map +1 -1
- package/dist/worker-api-version.d.ts +2 -1
- package/dist/worker-api-version.d.ts.map +1 -1
- package/dist/worker-ipc-transport.d.ts +7 -0
- package/dist/worker-ipc-transport.d.ts.map +1 -1
- package/dist/worker-result-image-public.d.ts +2 -0
- package/dist/worker-result-image-public.d.ts.map +1 -0
- package/dist/worker-result-image-public.js +7 -0
- package/dist/worker-result-image-public.js.map +1 -0
- package/dist/worker-result-image.d.ts +22 -0
- package/dist/worker-result-image.d.ts.map +1 -1
- package/dist/worker-result-image.js +28 -54
- package/dist/worker-result-image.js.map +1 -1
- package/dist/worker-session-snapshot.d.ts +5 -0
- package/dist/worker-session-snapshot.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/docker-config.ts +50 -0
- package/src/index.ts +63 -5
- package/src/ipc-codec.ts +40 -6
- package/src/ipc-decode.ts +17 -2
- package/src/ipc-messages.ts +368 -32
- package/src/pi-resource-bundle.ts +13 -2
- package/src/pi-resource-manifest.ts +35 -1
- package/src/testing.ts +8 -0
- package/src/worker-api-version.ts +2 -1
- package/src/worker-ipc-transport.ts +7 -0
- package/src/worker-result-image-public.ts +1 -0
- package/src/worker-result-image.ts +25 -1
- package/src/worker-session-snapshot.ts +5 -0
- package/dist/chunk-DVN32ANP.js.map +0 -1
- package/dist/ipc-codec.js +0 -48
- package/dist/ipc-codec.js.map +0 -1
- package/dist/ipc-decode.js +0 -72
- package/dist/ipc-decode.js.map +0 -1
- package/dist/ipc-messages.js +0 -14
- package/dist/ipc-messages.js.map +0 -1
- package/dist/pi-resource-bundle.js +0 -139
- package/dist/pi-resource-bundle.js.map +0 -1
- package/dist/pi-resource-manifest.js +0 -101
- package/dist/pi-resource-manifest.js.map +0 -1
- package/dist/testing.js +0 -33
- package/dist/testing.js.map +0 -1
- package/dist/worker-api-version.js +0 -9
- package/dist/worker-api-version.js.map +0 -1
- package/dist/worker-ipc-transport.js +0 -8
- package/dist/worker-ipc-transport.js.map +0 -1
- package/dist/worker-session-snapshot.js +0 -8
- package/dist/worker-session-snapshot.js.map +0 -1
|
@@ -3,18 +3,27 @@ import path from "node:path";
|
|
|
3
3
|
|
|
4
4
|
const BLOCK_SIZE = 512;
|
|
5
5
|
|
|
6
|
+
/** @internal */
|
|
6
7
|
export interface PiResourceFile {
|
|
8
|
+
/** @internal */
|
|
7
9
|
path: string;
|
|
10
|
+
/** @internal */
|
|
8
11
|
content: Uint8Array;
|
|
9
12
|
}
|
|
10
13
|
|
|
14
|
+
/** @internal */
|
|
11
15
|
export interface PiResourceBundle {
|
|
16
|
+
/** @internal */
|
|
12
17
|
digest: string;
|
|
18
|
+
/** @internal */
|
|
13
19
|
bytes: Uint8Array;
|
|
14
20
|
}
|
|
15
21
|
|
|
22
|
+
/** @internal */
|
|
16
23
|
export interface ExtractedPiResourceFile {
|
|
24
|
+
/** @internal */
|
|
17
25
|
path: string;
|
|
26
|
+
/** @internal */
|
|
18
27
|
content: Uint8Array;
|
|
19
28
|
}
|
|
20
29
|
|
|
@@ -22,6 +31,7 @@ function fail(message: string): never {
|
|
|
22
31
|
throw new Error(`Invalid Pi resource bundle: ${message}`);
|
|
23
32
|
}
|
|
24
33
|
|
|
34
|
+
/** @internal */
|
|
25
35
|
export function validateResourceRelativePath(value: string): string {
|
|
26
36
|
if (!value || path.posix.isAbsolute(value) || value.includes("\\")) {
|
|
27
37
|
fail(`unsafe path '${value}'`);
|
|
@@ -69,11 +79,12 @@ function padding(size: number): Buffer {
|
|
|
69
79
|
return Buffer.alloc(length);
|
|
70
80
|
}
|
|
71
81
|
|
|
82
|
+
/** @internal */
|
|
72
83
|
export function sha256Digest(bytes: Uint8Array): string {
|
|
73
84
|
return createHash("sha256").update(bytes).digest("hex");
|
|
74
85
|
}
|
|
75
86
|
|
|
76
|
-
/** Creates a deterministic, uncompressed ustar archive containing regular files only. */
|
|
87
|
+
/** Creates a deterministic, uncompressed ustar archive containing regular files only. @internal */
|
|
77
88
|
export function createCanonicalPiResourceBundle(
|
|
78
89
|
files: readonly PiResourceFile[],
|
|
79
90
|
): PiResourceBundle {
|
|
@@ -120,7 +131,7 @@ function allZero(block: Uint8Array): boolean {
|
|
|
120
131
|
return block.every((byte) => byte === 0);
|
|
121
132
|
}
|
|
122
133
|
|
|
123
|
-
/** Parses only the narrow regular-file ustar subset emitted by this module. */
|
|
134
|
+
/** Parses only the narrow regular-file ustar subset emitted by this module. @internal */
|
|
124
135
|
export function verifyCanonicalPiResourceBundle(
|
|
125
136
|
bytes: Uint8Array,
|
|
126
137
|
expectedDigest?: string,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
2
|
import { validateResourceRelativePath } from "./pi-resource-bundle.js";
|
|
3
3
|
|
|
4
|
+
/** @internal */
|
|
4
5
|
export const PI_RESOURCE_SNAPSHOT_SCHEMA_VERSION = 1;
|
|
5
6
|
|
|
6
7
|
const nonBlankStringSchema = v.pipe(
|
|
@@ -11,59 +12,89 @@ const jsonObjectSchema = v.record(v.string(), v.unknown());
|
|
|
11
12
|
const positiveSafeIntegerSchema = v.pipe(v.number(), v.safeInteger(), v.minValue(1));
|
|
12
13
|
const nonNegativeSafeIntegerSchema = v.pipe(v.number(), v.safeInteger(), v.minValue(0));
|
|
13
14
|
|
|
15
|
+
/** @internal */
|
|
14
16
|
export const piResourceManifestSchema = v.strictObject({
|
|
17
|
+
/** @internal */
|
|
15
18
|
schemaVersion: v.literal(PI_RESOURCE_SNAPSHOT_SCHEMA_VERSION),
|
|
19
|
+
/** @internal */
|
|
16
20
|
model: v.strictObject({
|
|
21
|
+
/** @internal */
|
|
17
22
|
profileId: nonBlankStringSchema,
|
|
23
|
+
/** @internal */
|
|
18
24
|
providerId: nonBlankStringSchema,
|
|
25
|
+
/** @internal */
|
|
19
26
|
modelId: nonBlankStringSchema,
|
|
27
|
+
/** @internal */
|
|
20
28
|
thinkingLevel: nonBlankStringSchema,
|
|
21
29
|
}),
|
|
30
|
+
/** @internal */
|
|
22
31
|
providerOptions: v.record(v.string(), nonBlankStringSchema),
|
|
32
|
+
/** @internal */
|
|
23
33
|
providerWorkerConfig: v.nullable(
|
|
24
34
|
v.strictObject({
|
|
35
|
+
/** @internal */
|
|
25
36
|
version: positiveSafeIntegerSchema,
|
|
37
|
+
/** @internal */
|
|
26
38
|
value: jsonObjectSchema,
|
|
27
39
|
}),
|
|
28
40
|
),
|
|
41
|
+
/** @internal */
|
|
29
42
|
declaredCredentialPaths: v.array(nonBlankStringSchema),
|
|
43
|
+
/** @internal */
|
|
30
44
|
compatibility: v.strictObject({
|
|
45
|
+
/** @internal */
|
|
31
46
|
workerApiVersion: nonBlankStringSchema,
|
|
47
|
+
/** @internal */
|
|
32
48
|
piVersion: nonBlankStringSchema,
|
|
49
|
+
/** @internal */
|
|
33
50
|
protocolVersion: v.optional(nonBlankStringSchema),
|
|
34
51
|
}),
|
|
52
|
+
/** @internal */
|
|
35
53
|
provenance: v.array(
|
|
36
54
|
v.strictObject({
|
|
55
|
+
/** @internal */
|
|
37
56
|
kind: v.picklist(["extension", "skill", "prompt", "generated"]),
|
|
57
|
+
/** @internal */
|
|
38
58
|
ownerExtensionId: v.nullable(nonBlankStringSchema),
|
|
59
|
+
/** @internal */
|
|
39
60
|
packageName: v.nullable(nonBlankStringSchema),
|
|
61
|
+
/** @internal */
|
|
40
62
|
snapshotPath: nonBlankStringSchema,
|
|
63
|
+
/** @internal */
|
|
41
64
|
sha256: v.pipe(v.string(), v.regex(/^[a-f0-9]{64}$/)),
|
|
65
|
+
/** @internal */
|
|
42
66
|
size: nonNegativeSafeIntegerSchema,
|
|
43
67
|
}),
|
|
44
68
|
),
|
|
45
69
|
});
|
|
46
70
|
|
|
71
|
+
/** @internal */
|
|
47
72
|
type DeepReadonly<T> = T extends readonly unknown[]
|
|
48
73
|
? { readonly [Index in keyof T]: DeepReadonly<T[Index]> }
|
|
49
74
|
: T extends object
|
|
50
75
|
? { readonly [Key in keyof T]: DeepReadonly<T[Key]> }
|
|
51
76
|
: T;
|
|
52
77
|
|
|
78
|
+
/** @internal */
|
|
53
79
|
export type PiResourceManifest = DeepReadonly<v.InferOutput<typeof piResourceManifestSchema>>;
|
|
80
|
+
/** @internal */
|
|
54
81
|
export type PiResourceSnapshotModel = PiResourceManifest["model"];
|
|
82
|
+
/** @internal */
|
|
55
83
|
export type PiResourceSnapshotWorkerConfig = NonNullable<
|
|
56
84
|
PiResourceManifest["providerWorkerConfig"]
|
|
57
85
|
>;
|
|
86
|
+
/** @internal */
|
|
58
87
|
export type PiResourceSnapshotCompatibility = PiResourceManifest["compatibility"];
|
|
88
|
+
/** @internal */
|
|
59
89
|
export type PiResourceFileProvenance = PiResourceManifest["provenance"][number];
|
|
90
|
+
/** @internal */
|
|
60
91
|
export type PiResourceProvenanceKind = PiResourceFileProvenance["kind"];
|
|
61
92
|
|
|
62
93
|
function invalidManifest(message: string, cause?: unknown): never {
|
|
63
94
|
throw new Error(`Invalid managed Pi resource snapshot: ${message}`, { cause });
|
|
64
95
|
}
|
|
65
96
|
|
|
66
|
-
/** Parses and validates the complete generated.json contract. */
|
|
97
|
+
/** Parses and validates the complete generated.json contract. @internal */
|
|
67
98
|
export function parsePiResourceManifest(raw: unknown): PiResourceManifest {
|
|
68
99
|
let manifest: PiResourceManifest;
|
|
69
100
|
try {
|
|
@@ -100,6 +131,7 @@ export function parsePiResourceManifest(raw: unknown): PiResourceManifest {
|
|
|
100
131
|
|
|
101
132
|
type JsonRecord = Record<string, unknown>;
|
|
102
133
|
|
|
134
|
+
/** @internal */
|
|
103
135
|
export function canonicalizeJson(value: unknown, location = "value"): unknown {
|
|
104
136
|
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
105
137
|
if (typeof value === "number") {
|
|
@@ -121,10 +153,12 @@ export function canonicalizeJson(value: unknown, location = "value"): unknown {
|
|
|
121
153
|
);
|
|
122
154
|
}
|
|
123
155
|
|
|
156
|
+
/** @internal */
|
|
124
157
|
export function canonicalJsonStringify(value: unknown, location = "value"): string {
|
|
125
158
|
return JSON.stringify(canonicalizeJson(value, location));
|
|
126
159
|
}
|
|
127
160
|
|
|
161
|
+
/** @internal */
|
|
128
162
|
export function canonicalJsonEqual(left: unknown, right: unknown): boolean {
|
|
129
163
|
return canonicalJsonStringify(left) === canonicalJsonStringify(right);
|
|
130
164
|
}
|
package/src/testing.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { ConfigSnapshot } from "@leitwerk-dev/protocol/config-snapshot";
|
|
2
2
|
|
|
3
|
+
/** @internal */
|
|
4
|
+
export async function flushAsyncWork(iterations = 30): Promise<void> {
|
|
5
|
+
for (let i = 0; i < iterations; i++) {
|
|
6
|
+
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** @internal */
|
|
3
11
|
export function createTestConfigSnapshot(): ConfigSnapshot {
|
|
4
12
|
return {
|
|
5
13
|
workers: {
|
|
@@ -1,7 +1,14 @@
|
|
|
1
|
+
/** @internal */
|
|
1
2
|
export const WORKER_INSTANCE_ID_ENV = "LEITWERK_INSTANCE_ID";
|
|
3
|
+
/** @internal */
|
|
2
4
|
export const WORKER_ID_ENV = "LEITWERK_WORKER_ID";
|
|
5
|
+
/** @internal */
|
|
3
6
|
export const WORKER_IPC_SERVER_URL_ENV = "LEITWERK_SERVER_URL";
|
|
7
|
+
/** @internal */
|
|
4
8
|
export const WORKER_IPC_CONNECT_TOKEN_ENV = "LEITWERK_WORKER_CONNECT_TOKEN";
|
|
9
|
+
/** @internal */
|
|
5
10
|
export const WORKER_IPC_RECONNECT_ENV = "LEITWERK_WORKER_RECONNECT";
|
|
11
|
+
/** @internal */
|
|
6
12
|
export const WORKER_SNAPSHOT_TOKEN_ENV = "LEITWERK_WORKER_SNAPSHOT_TOKEN";
|
|
13
|
+
/** @internal */
|
|
7
14
|
export const WORKER_SERVER_EPOCH_ENV = "LEITWERK_SERVER_EPOCH";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { parseManagedResultImagePath } from "./worker-result-image.js";
|
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
2
|
|
|
3
|
+
/** @internal */
|
|
3
4
|
export const WORKER_RESULT_IMAGE_WORKER_ID_HEADER = "x-leitwerk-worker-id";
|
|
5
|
+
/** @internal */
|
|
4
6
|
export const RESULT_IMAGE_MIME_TYPES = ["image/png", "image/jpeg", "image/webp"] as const;
|
|
7
|
+
/** @internal */
|
|
5
8
|
export const RESULT_IMAGE_MAX_SIZE_BYTES = 10 * 1024 * 1024;
|
|
9
|
+
/** @internal */
|
|
6
10
|
export const RESULT_IMAGE_STORAGE_SEGMENT_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
11
|
+
/** @internal */
|
|
7
12
|
export const RESULT_IMAGE_ID_PATTERN = /^img_[A-Za-z0-9]+\.(png|jpg|webp)$/;
|
|
13
|
+
/** @internal */
|
|
8
14
|
export type ResultImageMimeType = (typeof RESULT_IMAGE_MIME_TYPES)[number];
|
|
9
15
|
|
|
16
|
+
/** @internal */
|
|
10
17
|
export function isResultImageStorageSegment(value: string): boolean {
|
|
11
18
|
return RESULT_IMAGE_STORAGE_SEGMENT_PATTERN.test(value);
|
|
12
19
|
}
|
|
13
20
|
|
|
14
|
-
|
|
21
|
+
/** @internal */
|
|
22
|
+
export function parseResultImageId(value: string): {
|
|
23
|
+
/** @internal */
|
|
24
|
+
extension: "png" | "jpg" | "webp";
|
|
25
|
+
} | null {
|
|
15
26
|
const extension = RESULT_IMAGE_ID_PATTERN.exec(value)?.[1];
|
|
16
27
|
return extension === "png" || extension === "jpg" || extension === "webp" ? { extension } : null;
|
|
17
28
|
}
|
|
@@ -20,23 +31,31 @@ function assertResultImageStorageSegment(value: string): void {
|
|
|
20
31
|
if (!isResultImageStorageSegment(value)) throw new Error("Invalid result image storage id");
|
|
21
32
|
}
|
|
22
33
|
|
|
34
|
+
/** @internal */
|
|
23
35
|
export const workerResultImageUploadResponseSchema = v.object({
|
|
36
|
+
/** @internal */
|
|
24
37
|
imageId: v.string(),
|
|
38
|
+
/** @internal */
|
|
25
39
|
url: v.string(),
|
|
40
|
+
/** @internal */
|
|
26
41
|
mimeType: v.picklist(RESULT_IMAGE_MIME_TYPES),
|
|
42
|
+
/** @internal */
|
|
27
43
|
byteSize: v.number(),
|
|
28
44
|
});
|
|
29
45
|
|
|
46
|
+
/** @internal */
|
|
30
47
|
export type WorkerResultImageUploadResponse = v.InferOutput<
|
|
31
48
|
typeof workerResultImageUploadResponseSchema
|
|
32
49
|
>;
|
|
33
50
|
|
|
51
|
+
/** @internal */
|
|
34
52
|
export function buildWorkerResultImageUploadPath(instanceId: string, turnRecordId: string): string {
|
|
35
53
|
assertResultImageStorageSegment(instanceId);
|
|
36
54
|
assertResultImageStorageSegment(turnRecordId);
|
|
37
55
|
return `/internal/workers/${instanceId}/turn-records/${turnRecordId}/result-images`;
|
|
38
56
|
}
|
|
39
57
|
|
|
58
|
+
/** @internal */
|
|
40
59
|
export function buildManagedResultImagePath(
|
|
41
60
|
instanceId: string,
|
|
42
61
|
turnRecordId: string,
|
|
@@ -51,9 +70,13 @@ export function buildManagedResultImagePath(
|
|
|
51
70
|
const MANAGED_RESULT_IMAGE_PATH =
|
|
52
71
|
/^\/api\/processes\/([^/]+)\/turn-records\/([^/]+)\/result-images\/([^/]+)$/;
|
|
53
72
|
|
|
73
|
+
/** @internal */
|
|
54
74
|
export function parseManagedResultImagePath(value: string): {
|
|
75
|
+
/** @internal */
|
|
55
76
|
instanceId: string;
|
|
77
|
+
/** @internal */
|
|
56
78
|
turnRecordId: string;
|
|
79
|
+
/** @internal */
|
|
57
80
|
imageId: string;
|
|
58
81
|
} | null {
|
|
59
82
|
const match = MANAGED_RESULT_IMAGE_PATH.exec(value);
|
|
@@ -70,6 +93,7 @@ export function parseManagedResultImagePath(value: string): {
|
|
|
70
93
|
: null;
|
|
71
94
|
}
|
|
72
95
|
|
|
96
|
+
/** @internal */
|
|
73
97
|
export function parseWorkerResultImageUploadResponse(
|
|
74
98
|
value: unknown,
|
|
75
99
|
): WorkerResultImageUploadResponse | null {
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
/** @internal */
|
|
1
2
|
export const WORKER_SESSION_SNAPSHOT_CONTENT_TYPE = "application/x-ndjson";
|
|
3
|
+
/** @internal */
|
|
2
4
|
export const WORKER_SESSION_SNAPSHOT_WORKER_ID_HEADER = "x-leitwerk-worker-id";
|
|
5
|
+
/** @internal */
|
|
3
6
|
export const WORKER_SESSION_SNAPSHOT_REASON_HEADER = "x-leitwerk-session-snapshot-reason";
|
|
7
|
+
/** @internal */
|
|
4
8
|
export const WORKER_SESSION_SNAPSHOT_TURN_RECORD_ID_HEADER = "x-leitwerk-turn-record-id";
|
|
9
|
+
/** @internal */
|
|
5
10
|
export function buildWorkerSessionSnapshotPath(instanceId: string): string {
|
|
6
11
|
return `/internal/workers/${encodeURIComponent(instanceId)}/session-snapshot`;
|
|
7
12
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/worker-result-image.ts"],"sourcesContent":["import * as v from \"valibot\";\n\nexport const WORKER_RESULT_IMAGE_WORKER_ID_HEADER = \"x-leitwerk-worker-id\";\nexport const RESULT_IMAGE_MIME_TYPES = [\"image/png\", \"image/jpeg\", \"image/webp\"] as const;\nexport const RESULT_IMAGE_MAX_SIZE_BYTES = 10 * 1024 * 1024;\nexport const RESULT_IMAGE_STORAGE_SEGMENT_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;\nexport const RESULT_IMAGE_ID_PATTERN = /^img_[A-Za-z0-9]+\\.(png|jpg|webp)$/;\nexport type ResultImageMimeType = (typeof RESULT_IMAGE_MIME_TYPES)[number];\n\nexport function isResultImageStorageSegment(value: string): boolean {\n\treturn RESULT_IMAGE_STORAGE_SEGMENT_PATTERN.test(value);\n}\n\nexport function parseResultImageId(value: string): { extension: \"png\" | \"jpg\" | \"webp\" } | null {\n\tconst extension = RESULT_IMAGE_ID_PATTERN.exec(value)?.[1];\n\treturn extension === \"png\" || extension === \"jpg\" || extension === \"webp\" ? { extension } : null;\n}\n\nfunction assertResultImageStorageSegment(value: string): void {\n\tif (!isResultImageStorageSegment(value)) throw new Error(\"Invalid result image storage id\");\n}\n\nexport const workerResultImageUploadResponseSchema = v.object({\n\timageId: v.string(),\n\turl: v.string(),\n\tmimeType: v.picklist(RESULT_IMAGE_MIME_TYPES),\n\tbyteSize: v.number(),\n});\n\nexport type WorkerResultImageUploadResponse = v.InferOutput<\n\ttypeof workerResultImageUploadResponseSchema\n>;\n\nexport function buildWorkerResultImageUploadPath(instanceId: string, turnRecordId: string): string {\n\tassertResultImageStorageSegment(instanceId);\n\tassertResultImageStorageSegment(turnRecordId);\n\treturn `/internal/workers/${instanceId}/turn-records/${turnRecordId}/result-images`;\n}\n\nexport function buildManagedResultImagePath(\n\tinstanceId: string,\n\tturnRecordId: string,\n\timageId: string,\n): string {\n\tassertResultImageStorageSegment(instanceId);\n\tassertResultImageStorageSegment(turnRecordId);\n\tif (!parseResultImageId(imageId)) throw new Error(\"Invalid result image id\");\n\treturn `/api/processes/${instanceId}/turn-records/${turnRecordId}/result-images/${imageId}`;\n}\n\nconst MANAGED_RESULT_IMAGE_PATH =\n\t/^\\/api\\/processes\\/([^/]+)\\/turn-records\\/([^/]+)\\/result-images\\/([^/]+)$/;\n\nexport function parseManagedResultImagePath(value: string): {\n\tinstanceId: string;\n\tturnRecordId: string;\n\timageId: string;\n} | null {\n\tconst match = MANAGED_RESULT_IMAGE_PATH.exec(value);\n\tconst instanceId = match?.[1];\n\tconst turnRecordId = match?.[2];\n\tconst imageId = match?.[3];\n\treturn instanceId &&\n\t\tturnRecordId &&\n\t\timageId &&\n\t\tisResultImageStorageSegment(instanceId) &&\n\t\tisResultImageStorageSegment(turnRecordId) &&\n\t\tparseResultImageId(imageId)\n\t\t? { instanceId, turnRecordId, imageId }\n\t\t: null;\n}\n\nexport function parseWorkerResultImageUploadResponse(\n\tvalue: unknown,\n): WorkerResultImageUploadResponse | null {\n\tconst parsed = v.safeParse(workerResultImageUploadResponseSchema, value);\n\treturn parsed.success ? parsed.output : null;\n}\n"],"mappings":";AAAA,YAAY,OAAO;AAEZ,IAAM,uCAAuC;AAC7C,IAAM,0BAA0B,CAAC,aAAa,cAAc,YAAY;AACxE,IAAM,8BAA8B,KAAK,OAAO;AAChD,IAAM,uCAAuC;AAC7C,IAAM,0BAA0B;AAGhC,SAAS,4BAA4B,OAAwB;AACnE,SAAO,qCAAqC,KAAK,KAAK;AACvD;AAEO,SAAS,mBAAmB,OAA6D;AAC/F,QAAM,YAAY,wBAAwB,KAAK,KAAK,IAAI,CAAC;AACzD,SAAO,cAAc,SAAS,cAAc,SAAS,cAAc,SAAS,EAAE,UAAU,IAAI;AAC7F;AAEA,SAAS,gCAAgC,OAAqB;AAC7D,MAAI,CAAC,4BAA4B,KAAK,EAAG,OAAM,IAAI,MAAM,iCAAiC;AAC3F;AAEO,IAAM,wCAA0C,SAAO;AAAA,EAC7D,SAAW,SAAO;AAAA,EAClB,KAAO,SAAO;AAAA,EACd,UAAY,WAAS,uBAAuB;AAAA,EAC5C,UAAY,SAAO;AACpB,CAAC;AAMM,SAAS,iCAAiC,YAAoB,cAA8B;AAClG,kCAAgC,UAAU;AAC1C,kCAAgC,YAAY;AAC5C,SAAO,qBAAqB,UAAU,iBAAiB,YAAY;AACpE;AAEO,SAAS,4BACf,YACA,cACA,SACS;AACT,kCAAgC,UAAU;AAC1C,kCAAgC,YAAY;AAC5C,MAAI,CAAC,mBAAmB,OAAO,EAAG,OAAM,IAAI,MAAM,yBAAyB;AAC3E,SAAO,kBAAkB,UAAU,iBAAiB,YAAY,kBAAkB,OAAO;AAC1F;AAEA,IAAM,4BACL;AAEM,SAAS,4BAA4B,OAInC;AACR,QAAM,QAAQ,0BAA0B,KAAK,KAAK;AAClD,QAAM,aAAa,QAAQ,CAAC;AAC5B,QAAM,eAAe,QAAQ,CAAC;AAC9B,QAAM,UAAU,QAAQ,CAAC;AACzB,SAAO,cACN,gBACA,WACA,4BAA4B,UAAU,KACtC,4BAA4B,YAAY,KACxC,mBAAmB,OAAO,IACxB,EAAE,YAAY,cAAc,QAAQ,IACpC;AACJ;AAEO,SAAS,qCACf,OACyC;AACzC,QAAM,SAAW,YAAU,uCAAuC,KAAK;AACvE,SAAO,OAAO,UAAU,OAAO,SAAS;AACzC;","names":[]}
|
package/dist/ipc-codec.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { parseSchema } from "@leitwerk-dev/protocol";
|
|
2
|
-
import * as v from "valibot";
|
|
3
|
-
export const IPC_PROTOCOL_VERSION = "leitwerk/worker-ipc/v1";
|
|
4
|
-
export const IPC_ENVELOPE_SCHEMA = v.object({
|
|
5
|
-
protocol: v.literal(IPC_PROTOCOL_VERSION),
|
|
6
|
-
messageId: v.string(),
|
|
7
|
-
correlationId: v.optional(v.string()),
|
|
8
|
-
type: v.string(),
|
|
9
|
-
instanceId: v.string(),
|
|
10
|
-
workerId: v.string(),
|
|
11
|
-
sentAt: v.string(),
|
|
12
|
-
payload: v.unknown(),
|
|
13
|
-
});
|
|
14
|
-
export function validateEnvelope(msg) {
|
|
15
|
-
if (typeof msg === "object" &&
|
|
16
|
-
msg !== null &&
|
|
17
|
-
"protocol" in msg &&
|
|
18
|
-
typeof msg.protocol === "string" &&
|
|
19
|
-
msg.protocol !== IPC_PROTOCOL_VERSION) {
|
|
20
|
-
return {
|
|
21
|
-
ok: false,
|
|
22
|
-
error: `Worker IPC protocol '${msg.protocol}' is incompatible with expected protocol '${IPC_PROTOCOL_VERSION}'`,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
const envelope = parseSchema(IPC_ENVELOPE_SCHEMA, msg, "ipc envelope");
|
|
26
|
-
return envelope.ok
|
|
27
|
-
? { ok: true, envelope: envelope.value }
|
|
28
|
-
: { ok: false, error: envelope.error };
|
|
29
|
-
}
|
|
30
|
-
export function serializeMessage(envelope) {
|
|
31
|
-
return JSON.stringify(envelope);
|
|
32
|
-
}
|
|
33
|
-
export function deserializeMessage(line) {
|
|
34
|
-
const trimmed = line.length > 0 && line.charCodeAt(line.length - 1) === 0x0d ? line.slice(0, -1) : line;
|
|
35
|
-
let parsed;
|
|
36
|
-
try {
|
|
37
|
-
parsed = JSON.parse(trimmed);
|
|
38
|
-
}
|
|
39
|
-
catch {
|
|
40
|
-
return { ok: false, error: "invalid JSON" };
|
|
41
|
-
}
|
|
42
|
-
const validated = validateEnvelope(parsed);
|
|
43
|
-
if (!validated.ok) {
|
|
44
|
-
return { ok: false, error: validated.error };
|
|
45
|
-
}
|
|
46
|
-
return { ok: true, message: validated.envelope };
|
|
47
|
-
}
|
|
48
|
-
//# sourceMappingURL=ipc-codec.js.map
|
package/dist/ipc-codec.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"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,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC;AACzC,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAgC,QAAmB;IAClF,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,kBAAkB,CACjC,IAAY;IAEZ,MAAM,OAAO,GACZ,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACzF,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACJ,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAY,CAAC;IACzC,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC;IAC7C,CAAC;IACD,MAAM,SAAS,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC3C,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,CAAC;QACnB,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,CAAC;IAC9C,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC,QAAQ,EAAE,CAAC;AAClD,CAAC"}
|
package/dist/ipc-decode.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { validateEnvelope } from "./ipc-codec.js";
|
|
2
|
-
const SERVER_TO_WORKER_TYPES = [
|
|
3
|
-
"worker.start",
|
|
4
|
-
"worker.turn_start_accepted",
|
|
5
|
-
"worker.turn_terminal_recorded",
|
|
6
|
-
"worker.credential_update_accepted",
|
|
7
|
-
"worker.question_response",
|
|
8
|
-
"worker.integration_tool_result",
|
|
9
|
-
"input.batch",
|
|
10
|
-
"worker.stop",
|
|
11
|
-
"worker.abort_turn",
|
|
12
|
-
];
|
|
13
|
-
const WORKER_TO_SERVER_TYPES = [
|
|
14
|
-
"worker.hello",
|
|
15
|
-
"worker.credential_update",
|
|
16
|
-
"worker.ready",
|
|
17
|
-
"worker.heartbeat",
|
|
18
|
-
"worker.state",
|
|
19
|
-
"worker.input_consumed",
|
|
20
|
-
"worker.event",
|
|
21
|
-
"worker.turn_started",
|
|
22
|
-
"worker.question_requested",
|
|
23
|
-
"worker.integration_tool_request",
|
|
24
|
-
"worker.integration_tool_cancel",
|
|
25
|
-
"worker.turn_outcome",
|
|
26
|
-
"worker.turn_failed",
|
|
27
|
-
"worker.lifecycle_parked",
|
|
28
|
-
"worker.cleanup_started",
|
|
29
|
-
"worker.cleanup_completed",
|
|
30
|
-
"worker.failed",
|
|
31
|
-
];
|
|
32
|
-
function hasMessageType(allowed, value) {
|
|
33
|
-
return allowed.includes(value);
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Validate the generic IPC envelope and narrow it to the correct typed message
|
|
37
|
-
* union member for the given direction.
|
|
38
|
-
*
|
|
39
|
-
* This intentionally stops at the message discriminant boundary: it validates
|
|
40
|
-
* envelope structure + known message type, but does not deeply validate the
|
|
41
|
-
* payload shape. Payload fields are trusted to come from same-version
|
|
42
|
-
* leitwerk components, so malformed payloads from a buggy sender can still
|
|
43
|
-
* pass through as typed data.
|
|
44
|
-
*/
|
|
45
|
-
function decodeKnownMessage(msg, allowed, direction) {
|
|
46
|
-
const validated = validateEnvelope(msg);
|
|
47
|
-
if (!validated.ok) {
|
|
48
|
-
return { ok: false, error: validated.error };
|
|
49
|
-
}
|
|
50
|
-
if (!hasMessageType(allowed, validated.envelope.type)) {
|
|
51
|
-
return {
|
|
52
|
-
ok: false,
|
|
53
|
-
error: `unexpected ${direction} message type: ${validated.envelope.type}`,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
return { ok: true, message: validated.envelope };
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Decode a parsed envelope coming from the server side of the IPC channel.
|
|
60
|
-
* Only envelope structure + message type are runtime-validated.
|
|
61
|
-
*/
|
|
62
|
-
export function decodeServerToWorkerMessage(msg) {
|
|
63
|
-
return decodeKnownMessage(msg, SERVER_TO_WORKER_TYPES, "server-to-worker");
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Decode a parsed envelope coming from the worker side of the IPC channel.
|
|
67
|
-
* Only envelope structure + message type are runtime-validated.
|
|
68
|
-
*/
|
|
69
|
-
export function decodeWorkerToServerMessage(msg) {
|
|
70
|
-
return decodeKnownMessage(msg, WORKER_TO_SERVER_TYPES, "worker-to-server");
|
|
71
|
-
}
|
|
72
|
-
//# sourceMappingURL=ipc-decode.js.map
|
package/dist/ipc-decode.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ipc-decode.js","sourceRoot":"","sources":["../src/ipc-decode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAOlD,MAAM,sBAAsB,GAAG;IAC9B,cAAc;IACd,4BAA4B;IAC5B,+BAA+B;IAC/B,mCAAmC;IACnC,0BAA0B;IAC1B,gCAAgC;IAChC,aAAa;IACb,aAAa;IACb,mBAAmB;CACyC,CAAC;AAE9D,MAAM,sBAAsB,GAAG;IAC9B,cAAc;IACd,0BAA0B;IAC1B,cAAc;IACd,kBAAkB;IAClB,cAAc;IACd,uBAAuB;IACvB,cAAc;IACd,qBAAqB;IACrB,2BAA2B;IAC3B,iCAAiC;IACjC,gCAAgC;IAChC,qBAAqB;IACrB,oBAAoB;IACpB,yBAAyB;IACzB,wBAAwB;IACxB,0BAA0B;IAC1B,eAAe;CAC6C,CAAC;AAE9D,SAAS,cAAc,CACtB,OAAyB,EACzB,KAAa;IAEb,OAAQ,OAA6B,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,kBAAkB,CAC1B,GAAY,EACZ,OAAoC,EACpC,SAAiB;IAEjB,MAAM,SAAS,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;IACxC,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,CAAC;QACnB,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,CAAC;IAC9C,CAAC;IACD,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACvD,OAAO;YACN,EAAE,EAAE,KAAK;YACT,KAAK,EAAE,cAAc,SAAS,kBAAkB,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE;SACzE,CAAC;IACH,CAAC;IACD,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC,QAA+B,EAAE,CAAC;AACzE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,2BAA2B,CAAC,GAAY;IACvD,OAAO,kBAAkB,CAAC,GAAG,EAAE,sBAAsB,EAAE,kBAAkB,CAAC,CAAC;AAC5E,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,2BAA2B,CAAC,GAAY;IACvD,OAAO,kBAAkB,CAAC,GAAG,EAAE,sBAAsB,EAAE,kBAAkB,CAAC,CAAC;AAC5E,CAAC"}
|
package/dist/ipc-messages.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { IPC_PROTOCOL_VERSION } from "./ipc-codec.js";
|
|
2
|
-
export function createIpcMessage(args) {
|
|
3
|
-
return {
|
|
4
|
-
protocol: IPC_PROTOCOL_VERSION,
|
|
5
|
-
messageId: args.messageId,
|
|
6
|
-
correlationId: args.correlationId,
|
|
7
|
-
type: args.type,
|
|
8
|
-
instanceId: args.instanceId,
|
|
9
|
-
workerId: args.workerId,
|
|
10
|
-
sentAt: args.sentAt ?? new Date().toISOString(),
|
|
11
|
-
payload: args.payload,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=ipc-messages.js.map
|
package/dist/ipc-messages.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ipc-messages.js","sourceRoot":"","sources":["../src/ipc-messages.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,oBAAoB,EAAoB,MAAM,gBAAgB,CAAC;AAuZxE,MAAM,UAAU,gBAAgB,CAC/B,IAAiE;IAEjE,OAAO;QACN,QAAQ,EAAE,oBAAoB;QAC9B,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,aAAa,EAAE,IAAI,CAAC,aAAa;QACjC,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,UAAU,EAAE,IAAI,CAAC,UAAU;QAC3B,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QAC/C,OAAO,EAAE,IAAI,CAAC,OAAO;KACT,CAAC;AACf,CAAC"}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
const BLOCK_SIZE = 512;
|
|
4
|
-
function fail(message) {
|
|
5
|
-
throw new Error(`Invalid Pi resource bundle: ${message}`);
|
|
6
|
-
}
|
|
7
|
-
export function validateResourceRelativePath(value) {
|
|
8
|
-
if (!value || path.posix.isAbsolute(value) || value.includes("\\")) {
|
|
9
|
-
fail(`unsafe path '${value}'`);
|
|
10
|
-
}
|
|
11
|
-
const normalized = path.posix.normalize(value);
|
|
12
|
-
if (normalized === "." || normalized === ".." || normalized.startsWith("../")) {
|
|
13
|
-
fail(`unsafe path '${value}'`);
|
|
14
|
-
}
|
|
15
|
-
return normalized;
|
|
16
|
-
}
|
|
17
|
-
function writeString(buffer, offset, length, value) {
|
|
18
|
-
const encoded = Buffer.from(value, "utf8");
|
|
19
|
-
if (encoded.length > length)
|
|
20
|
-
fail(`header value too long: ${value}`);
|
|
21
|
-
encoded.copy(buffer, offset);
|
|
22
|
-
}
|
|
23
|
-
function writeOctal(buffer, offset, length, value) {
|
|
24
|
-
const encoded = value.toString(8).padStart(length - 1, "0");
|
|
25
|
-
if (encoded.length > length - 1)
|
|
26
|
-
fail(`numeric header value too large: ${value}`);
|
|
27
|
-
writeString(buffer, offset, length - 1, encoded);
|
|
28
|
-
buffer[offset + length - 1] = 0;
|
|
29
|
-
}
|
|
30
|
-
function header(name, size) {
|
|
31
|
-
if (Buffer.byteLength(name) > 100)
|
|
32
|
-
fail(`path exceeds canonical ustar name limit: ${name}`);
|
|
33
|
-
const result = Buffer.alloc(BLOCK_SIZE);
|
|
34
|
-
writeString(result, 0, 100, name);
|
|
35
|
-
writeOctal(result, 100, 8, 0o644);
|
|
36
|
-
writeOctal(result, 108, 8, 0);
|
|
37
|
-
writeOctal(result, 116, 8, 0);
|
|
38
|
-
writeOctal(result, 124, 12, size);
|
|
39
|
-
writeOctal(result, 136, 12, 0);
|
|
40
|
-
result.fill(0x20, 148, 156);
|
|
41
|
-
result[156] = "0".charCodeAt(0);
|
|
42
|
-
writeString(result, 257, 6, "ustar");
|
|
43
|
-
writeString(result, 263, 2, "00");
|
|
44
|
-
const sum = result.reduce((total, byte) => total + byte, 0);
|
|
45
|
-
writeString(result, 148, 8, `${sum.toString(8).padStart(6, "0")}\0 `);
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
function padding(size) {
|
|
49
|
-
const length = (BLOCK_SIZE - (size % BLOCK_SIZE)) % BLOCK_SIZE;
|
|
50
|
-
return Buffer.alloc(length);
|
|
51
|
-
}
|
|
52
|
-
export function sha256Digest(bytes) {
|
|
53
|
-
return createHash("sha256").update(bytes).digest("hex");
|
|
54
|
-
}
|
|
55
|
-
/** Creates a deterministic, uncompressed ustar archive containing regular files only. */
|
|
56
|
-
export function createCanonicalPiResourceBundle(files) {
|
|
57
|
-
const seen = new Set();
|
|
58
|
-
const normalized = files.map((file) => {
|
|
59
|
-
const resourcePath = validateResourceRelativePath(file.path);
|
|
60
|
-
if (seen.has(resourcePath))
|
|
61
|
-
fail(`duplicate path '${resourcePath}'`);
|
|
62
|
-
seen.add(resourcePath);
|
|
63
|
-
return { path: resourcePath, content: Buffer.from(file.content) };
|
|
64
|
-
});
|
|
65
|
-
normalized.sort((left, right) => Buffer.compare(Buffer.from(left.path), Buffer.from(right.path)));
|
|
66
|
-
const chunks = [];
|
|
67
|
-
for (const file of normalized) {
|
|
68
|
-
chunks.push(header(file.path, file.content.length), file.content, padding(file.content.length));
|
|
69
|
-
}
|
|
70
|
-
chunks.push(Buffer.alloc(BLOCK_SIZE * 2));
|
|
71
|
-
const bytes = Buffer.concat(chunks);
|
|
72
|
-
return { bytes, digest: sha256Digest(bytes) };
|
|
73
|
-
}
|
|
74
|
-
function readNullString(block, start, length) {
|
|
75
|
-
const end = block.subarray(start, start + length).indexOf(0);
|
|
76
|
-
return Buffer.from(block.subarray(start, end < 0 ? start + length : start + end)).toString("utf8");
|
|
77
|
-
}
|
|
78
|
-
function readOctal(block, start, length) {
|
|
79
|
-
const raw = readNullString(block, start, length).trim();
|
|
80
|
-
if (!/^[0-7]*$/.test(raw))
|
|
81
|
-
fail("non-octal numeric field");
|
|
82
|
-
return raw ? Number.parseInt(raw, 8) : 0;
|
|
83
|
-
}
|
|
84
|
-
function verifyChecksum(block) {
|
|
85
|
-
const expected = readOctal(block, 148, 8);
|
|
86
|
-
let actual = 0;
|
|
87
|
-
for (let index = 0; index < BLOCK_SIZE; index++) {
|
|
88
|
-
actual += index >= 148 && index < 156 ? 32 : block[index];
|
|
89
|
-
}
|
|
90
|
-
if (actual !== expected)
|
|
91
|
-
fail("header checksum mismatch");
|
|
92
|
-
}
|
|
93
|
-
function allZero(block) {
|
|
94
|
-
return block.every((byte) => byte === 0);
|
|
95
|
-
}
|
|
96
|
-
/** Parses only the narrow regular-file ustar subset emitted by this module. */
|
|
97
|
-
export function verifyCanonicalPiResourceBundle(bytes, expectedDigest) {
|
|
98
|
-
if (expectedDigest && sha256Digest(bytes) !== expectedDigest)
|
|
99
|
-
fail("content digest mismatch");
|
|
100
|
-
if (bytes.length < BLOCK_SIZE * 2 || bytes.length % BLOCK_SIZE !== 0) {
|
|
101
|
-
fail("invalid archive length");
|
|
102
|
-
}
|
|
103
|
-
const files = [];
|
|
104
|
-
const paths = new Set();
|
|
105
|
-
let offset = 0;
|
|
106
|
-
while (offset < bytes.length) {
|
|
107
|
-
const block = bytes.subarray(offset, offset + BLOCK_SIZE);
|
|
108
|
-
if (allZero(block)) {
|
|
109
|
-
if (!allZero(bytes.subarray(offset + BLOCK_SIZE, offset + BLOCK_SIZE * 2))) {
|
|
110
|
-
fail("missing end blocks");
|
|
111
|
-
}
|
|
112
|
-
if (offset + BLOCK_SIZE * 2 !== bytes.length)
|
|
113
|
-
fail("data after end blocks");
|
|
114
|
-
return files;
|
|
115
|
-
}
|
|
116
|
-
verifyChecksum(block);
|
|
117
|
-
if (readNullString(block, 257, 6) !== "ustar")
|
|
118
|
-
fail("unsupported tar format");
|
|
119
|
-
const type = readNullString(block, 156, 1);
|
|
120
|
-
if (type !== "" && type !== "0")
|
|
121
|
-
fail(`unsupported entry type '${type}'`);
|
|
122
|
-
if (readNullString(block, 157, 100) || readNullString(block, 345, 155)) {
|
|
123
|
-
fail("links and ustar path prefixes are unsupported");
|
|
124
|
-
}
|
|
125
|
-
const name = validateResourceRelativePath(readNullString(block, 0, 100));
|
|
126
|
-
if (paths.has(name))
|
|
127
|
-
fail(`duplicate path '${name}'`);
|
|
128
|
-
paths.add(name);
|
|
129
|
-
const size = readOctal(block, 124, 12);
|
|
130
|
-
const contentStart = offset + BLOCK_SIZE;
|
|
131
|
-
const contentEnd = contentStart + size;
|
|
132
|
-
if (contentEnd > bytes.length)
|
|
133
|
-
fail("truncated file content");
|
|
134
|
-
files.push({ path: name, content: bytes.slice(contentStart, contentEnd) });
|
|
135
|
-
offset = contentEnd + ((BLOCK_SIZE - (size % BLOCK_SIZE)) % BLOCK_SIZE);
|
|
136
|
-
}
|
|
137
|
-
fail("missing end blocks");
|
|
138
|
-
}
|
|
139
|
-
//# sourceMappingURL=pi-resource-bundle.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pi-resource-bundle.js","sourceRoot":"","sources":["../src/pi-resource-bundle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAAM,UAAU,GAAG,GAAG,CAAC;AAiBvB,SAAS,IAAI,CAAC,OAAe;IAC5B,MAAM,IAAI,KAAK,CAAC,+BAA+B,OAAO,EAAE,CAAC,CAAC;AAC3D,CAAC;AAED,MAAM,UAAU,4BAA4B,CAAC,KAAa;IACzD,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACpE,IAAI,CAAC,gBAAgB,KAAK,GAAG,CAAC,CAAC;IAChC,CAAC;IACD,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC/C,IAAI,UAAU,KAAK,GAAG,IAAI,UAAU,KAAK,IAAI,IAAI,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QAC/E,IAAI,CAAC,gBAAgB,KAAK,GAAG,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,UAAU,CAAC;AACnB,CAAC;AAED,SAAS,WAAW,CAAC,MAAc,EAAE,MAAc,EAAE,MAAc,EAAE,KAAa;IACjF,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC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