@forgeax/engine-pack 0.1.21 → 0.1.24
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/README.md +167 -48
- package/dist/__tests__/inventory-schema.test.d.ts +2 -0
- package/dist/__tests__/inventory-schema.test.d.ts.map +1 -0
- package/dist/__tests__/material-cook-receipt-layer-plan.unit.test.d.ts +2 -0
- package/dist/__tests__/material-cook-receipt-layer-plan.unit.test.d.ts.map +1 -0
- package/dist/__tests__/pack-authoring-gateway.unit.test.d.ts +2 -0
- package/dist/__tests__/pack-authoring-gateway.unit.test.d.ts.map +1 -0
- package/dist/__tests__/pack-authoring.unit.test.d.ts +2 -0
- package/dist/__tests__/pack-authoring.unit.test.d.ts.map +1 -0
- package/dist/__tests__/pack-scanner.unit.test.d.ts +2 -0
- package/dist/__tests__/pack-scanner.unit.test.d.ts.map +1 -0
- package/dist/__tests__/scanner-blacklist.test.d.ts +2 -0
- package/dist/__tests__/scanner-blacklist.test.d.ts.map +1 -0
- package/dist/build.d.ts +9 -4
- package/dist/build.d.ts.map +1 -1
- package/dist/build.mjs +3449 -897
- package/dist/build.mjs.map +1 -1
- package/dist/builtin.mjs +37 -4
- package/dist/builtin.mjs.map +1 -1
- package/dist/catalog-builder.d.ts.map +1 -1
- package/dist/cli-asset.d.ts.map +1 -1
- package/dist/cli-asset.mjs +1361 -845
- package/dist/cli-asset.mjs.map +1 -1
- package/dist/deriveAssetName.d.ts +11 -7
- package/dist/deriveAssetName.d.ts.map +1 -1
- package/dist/evidence/material-cook.d.ts +11 -0
- package/dist/evidence/material-cook.d.ts.map +1 -1
- package/dist/evidence/source-inventory.d.ts.map +1 -1
- package/dist/guid.d.ts +15 -0
- package/dist/guid.d.ts.map +1 -1
- package/dist/guid.mjs +53 -5
- package/dist/guid.mjs.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1297 -562
- package/dist/index.mjs.map +1 -1
- package/dist/inventory/binding.d.ts +6 -0
- package/dist/inventory/binding.d.ts.map +1 -0
- package/dist/inventory/declaration.d.ts +24 -0
- package/dist/inventory/declaration.d.ts.map +1 -0
- package/dist/inventory/index.d.ts +3 -0
- package/dist/inventory/index.d.ts.map +1 -0
- package/dist/inventory/sync.d.ts +5 -0
- package/dist/inventory/sync.d.ts.map +1 -0
- package/dist/material-cook.d.ts +1 -1
- package/dist/material-cook.d.ts.map +1 -1
- package/dist/material-cook.mjs +50 -3
- package/dist/material-cook.mjs.map +1 -1
- package/dist/name.mjs +4 -7
- package/dist/name.mjs.map +1 -1
- package/dist/pack-authoring-node.d.ts +26 -0
- package/dist/pack-authoring-node.d.ts.map +1 -0
- package/dist/pack-authoring-node.mjs +11089 -0
- package/dist/pack-authoring-node.mjs.map +1 -0
- package/dist/pack-authoring.d.ts +230 -0
- package/dist/pack-authoring.d.ts.map +1 -0
- package/dist/pack-authoring.mjs +1137 -0
- package/dist/pack-authoring.mjs.map +1 -0
- package/dist/package-finalizer.d.ts +2 -20
- package/dist/package-finalizer.d.ts.map +1 -1
- package/dist/resolve-asset-source.d.ts +1 -3
- package/dist/resolve-asset-source.d.ts.map +1 -1
- package/dist/resolve-asset-source.mjs +6 -81
- package/dist/resolve-asset-source.mjs.map +1 -1
- package/dist/runtime.mjs +77 -7
- package/dist/runtime.mjs.map +1 -1
- package/dist/scanner.d.ts +18 -7
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.mjs +1164 -687
- package/dist/scanner.mjs.map +1 -1
- package/dist/schema-compiled.d.ts.map +1 -1
- package/dist/schema.mjs +79 -7
- package/dist/schema.mjs.map +1 -1
- package/dist/scriptable-pack-node.d.ts +7 -31
- package/dist/scriptable-pack-node.d.ts.map +1 -1
- package/dist/scriptable-pack-node.mjs +572 -1019
- package/dist/scriptable-pack-node.mjs.map +1 -1
- package/dist/scriptable-pack-worker.mjs +29 -11
- package/dist/scriptable-pack-worker.mjs.map +1 -1
- package/dist/scriptable-pack.d.ts +20 -532
- package/dist/scriptable-pack.d.ts.map +1 -1
- package/dist/scriptable-pack.mjs +1038 -527
- package/dist/scriptable-pack.mjs.map +1 -1
- package/package.json +2 -7
- package/schema/meta.schema.json +1 -1
- package/schema/pack.schema.json +76 -6
- package/src/__tests__/guid-collision.unit.test.ts +18 -9
- package/src/__tests__/inventory-schema.test.ts +109 -0
- package/src/__tests__/material-cook-receipt-layer-plan.unit.test.ts +16 -0
- package/src/__tests__/material-cook-schema.unit.test.ts +62 -0
- package/src/__tests__/pack-authoring-gateway.unit.test.ts +249 -0
- package/src/__tests__/pack-authoring.unit.test.ts +266 -0
- package/src/__tests__/pack-scanner.unit.test.ts +123 -0
- package/src/__tests__/pack.unit.test.ts +1 -238
- package/src/__tests__/package-finalizer.contract.test.ts +55 -0
- package/src/__tests__/resolve-asset-source.test.ts +15 -98
- package/src/__tests__/scanner-blacklist.test.ts +55 -0
- package/src/__tests__/scanner-inventory.contract.test.ts +11 -14
- package/src/__tests__/scanner-inventory.test.ts +23 -10
- package/src/__tests__/scriptable-pack-cli.integration.test.ts +4 -9
- package/src/__tests__/topology.unit.test.ts +23 -0
- package/src/build.ts +28 -18
- package/src/catalog-builder.ts +60 -33
- package/src/cli-asset.ts +68 -83
- package/src/deriveAssetName.ts +14 -13
- package/src/evidence/material-cook.ts +79 -3
- package/src/evidence/source-inventory.ts +34 -21
- package/src/guid.ts +65 -4
- package/src/index.ts +15 -11
- package/src/inventory/binding.ts +25 -0
- package/src/inventory/declaration.ts +168 -0
- package/src/inventory/index.ts +18 -0
- package/src/inventory/sync.ts +22 -0
- package/src/material-cook.ts +1 -0
- package/src/pack-authoring-node.ts +1966 -0
- package/src/pack-authoring.ts +1478 -0
- package/src/package-finalizer.ts +2 -42
- package/src/resolve-asset-source.ts +4 -76
- package/src/scanner.ts +236 -56
- package/src/schema/material-cook.schema.json +4 -1
- package/src/schema-compiled.ts +2 -0
- package/src/scriptable-pack-node.ts +108 -719
- package/src/scriptable-pack-worker.ts +48 -16
- package/src/scriptable-pack.ts +28 -939
- package/dist/.tsbuildinfo +0 -1
- package/dist/__tests__/load-asset-config.test.d.ts +0 -2
- package/dist/__tests__/load-asset-config.test.d.ts.map +0 -1
- package/dist/__tests__/scriptable-pack.test-d.d.ts +0 -2
- package/dist/__tests__/scriptable-pack.test-d.d.ts.map +0 -1
- package/dist/__tests__/scriptable-pack.unit.test.d.ts +0 -2
- package/dist/__tests__/scriptable-pack.unit.test.d.ts.map +0 -1
- package/dist/config.d.ts +0 -6
- package/dist/config.d.ts.map +0 -1
- package/dist/config.mjs +0 -29
- package/dist/config.mjs.map +0 -1
- package/src/__tests__/load-asset-config.test.ts +0 -121
- package/src/__tests__/scriptable-pack.test-d.ts +0 -131
- package/src/__tests__/scriptable-pack.unit.test.ts +0 -789
- package/src/config.ts +0 -36
|
@@ -0,0 +1,1137 @@
|
|
|
1
|
+
import { ok, err } from '@forgeax/engine-types';
|
|
2
|
+
import { sha1 } from '@noble/hashes/legacy.js';
|
|
3
|
+
import { uuidv7obj } from 'uuidv7';
|
|
4
|
+
|
|
5
|
+
// src/pack-authoring.ts
|
|
6
|
+
|
|
7
|
+
// src/errors.ts
|
|
8
|
+
var PackError = class extends Error {
|
|
9
|
+
code;
|
|
10
|
+
expected;
|
|
11
|
+
hint;
|
|
12
|
+
detail;
|
|
13
|
+
constructor(args) {
|
|
14
|
+
super(`[PackError ${args.code}] expected: ${args.expected}; hint: ${args.hint}`);
|
|
15
|
+
this.name = "PackError";
|
|
16
|
+
this.code = args.code;
|
|
17
|
+
this.expected = args.expected;
|
|
18
|
+
this.hint = args.hint;
|
|
19
|
+
this.detail = args.detail;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/guid.ts
|
|
24
|
+
function brand(bytes) {
|
|
25
|
+
return bytes;
|
|
26
|
+
}
|
|
27
|
+
var HEX_BYTE = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
28
|
+
function bytesToDashForm(bytes) {
|
|
29
|
+
const h = HEX_BYTE;
|
|
30
|
+
return `${h[bytes[0]]}${h[bytes[1]]}${h[bytes[2]]}${h[bytes[3]]}-${h[bytes[4]]}${h[bytes[5]]}-${h[bytes[6]]}${h[bytes[7]]}-${h[bytes[8]]}${h[bytes[9]]}-${h[bytes[10]]}${h[bytes[11]]}${h[bytes[12]]}${h[bytes[13]]}${h[bytes[14]]}${h[bytes[15]]}`;
|
|
31
|
+
}
|
|
32
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
33
|
+
var PACK_SOURCE_KEY_RE = /^[a-z0-9][a-z0-9._-]*(\/[a-z0-9][a-z0-9._-]*)*$/;
|
|
34
|
+
function isValidPackSourceKey(value) {
|
|
35
|
+
return typeof value === "string" && PACK_SOURCE_KEY_RE.test(value);
|
|
36
|
+
}
|
|
37
|
+
function isValidAssetGuidString(value) {
|
|
38
|
+
return typeof value === "string" && UUID_RE.test(value);
|
|
39
|
+
}
|
|
40
|
+
function dashFormToBytes(dashForm) {
|
|
41
|
+
const hex = dashForm.replace(/-/g, "");
|
|
42
|
+
const bytes = new Uint8Array(16);
|
|
43
|
+
for (let i = 0; i < 16; i++) {
|
|
44
|
+
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
45
|
+
}
|
|
46
|
+
return bytes;
|
|
47
|
+
}
|
|
48
|
+
function randomUuidBytes() {
|
|
49
|
+
const uuid = uuidv7obj();
|
|
50
|
+
const bytes = new Uint8Array(16);
|
|
51
|
+
bytes.set(uuid.bytes);
|
|
52
|
+
return bytes;
|
|
53
|
+
}
|
|
54
|
+
function packageId(value) {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
var PackageId = {
|
|
58
|
+
parse(dashForm) {
|
|
59
|
+
const parsed = AssetGuid.parse(dashForm);
|
|
60
|
+
return parsed.ok ? { ok: true, value: packageId(parsed.value) } : parsed;
|
|
61
|
+
},
|
|
62
|
+
format(value) {
|
|
63
|
+
return bytesToDashForm(value);
|
|
64
|
+
},
|
|
65
|
+
random() {
|
|
66
|
+
return packageId(randomUuidBytes());
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function derivedGuid(namespace, sourceKey) {
|
|
70
|
+
if (!(namespace instanceof Uint8Array) || namespace.byteLength !== 16) {
|
|
71
|
+
throw new TypeError("AssetGuid.derive requires a 16-byte PackageId");
|
|
72
|
+
}
|
|
73
|
+
if (!isValidPackSourceKey(sourceKey)) {
|
|
74
|
+
const error = new TypeError(
|
|
75
|
+
`AssetGuid.derive received invalid sourceKey ${JSON.stringify(sourceKey)}`
|
|
76
|
+
);
|
|
77
|
+
Object.defineProperty(error, "code", { value: "pack-source-key-invalid" });
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
const name = new TextEncoder().encode(sourceKey);
|
|
81
|
+
const input = new Uint8Array(namespace.byteLength + name.byteLength);
|
|
82
|
+
input.set(namespace, 0);
|
|
83
|
+
input.set(name, namespace.byteLength);
|
|
84
|
+
const digest = sha1(input);
|
|
85
|
+
const result = digest.slice(0, 16);
|
|
86
|
+
result[6] = (result[6] ?? 0) & 15 | 80;
|
|
87
|
+
result[8] = (result[8] ?? 0) & 63 | 128;
|
|
88
|
+
return brand(result);
|
|
89
|
+
}
|
|
90
|
+
var AssetGuid = {
|
|
91
|
+
/**
|
|
92
|
+
* Parse a 36-char RFC 4122 dash-form UUID string into an AssetGuid.
|
|
93
|
+
* Returns Ok(AssetGuid) on success or Err(PackError) with code 'pack-guid-malformed' on failure.
|
|
94
|
+
* Never throws for expected failures (requirements §4.2 / §14 / charter proposition 4).
|
|
95
|
+
*/
|
|
96
|
+
parse(dashForm) {
|
|
97
|
+
if (!isValidAssetGuidString(dashForm)) {
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
error: new PackError({
|
|
101
|
+
code: "pack-guid-malformed",
|
|
102
|
+
expected: "36-char RFC 4122 dash-form UUID",
|
|
103
|
+
hint: "use AssetGuid.random() or a UUIDv7 generator; all GUID fields must be 36-char RFC 4122 dash-form",
|
|
104
|
+
detail: {
|
|
105
|
+
raw: dashForm,
|
|
106
|
+
reason: "expected 36-char RFC 4122 dash-form UUID"
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return { ok: true, value: brand(dashFormToBytes(dashForm)) };
|
|
112
|
+
},
|
|
113
|
+
/**
|
|
114
|
+
* Format an AssetGuid as a 36-char RFC 4122 lowercase dash-form string.
|
|
115
|
+
*/
|
|
116
|
+
format(guid) {
|
|
117
|
+
return bytesToDashForm(guid);
|
|
118
|
+
},
|
|
119
|
+
/**
|
|
120
|
+
* Test byte-by-byte equality between two AssetGuids.
|
|
121
|
+
*/
|
|
122
|
+
equals(a, b) {
|
|
123
|
+
for (let i = 0; i < 16; i++) {
|
|
124
|
+
if (a[i] !== b[i]) return false;
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
},
|
|
128
|
+
/**
|
|
129
|
+
* Mint a new time-ordered UUIDv7 as an AssetGuid.
|
|
130
|
+
* Works in both Node.js and browser environments.
|
|
131
|
+
*/
|
|
132
|
+
random() {
|
|
133
|
+
return brand(randomUuidBytes());
|
|
134
|
+
},
|
|
135
|
+
/** Derive the stable UUIDv5 projection for one Pack subject and sourceKey. */
|
|
136
|
+
derive(namespace, sourceKey) {
|
|
137
|
+
return derivedGuid(namespace, sourceKey);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/pack-authoring.ts
|
|
142
|
+
var PACK_PARAMETER_TYPES = [
|
|
143
|
+
"bool",
|
|
144
|
+
"u32",
|
|
145
|
+
"i32",
|
|
146
|
+
"f32",
|
|
147
|
+
"f64",
|
|
148
|
+
"string",
|
|
149
|
+
"enum",
|
|
150
|
+
"vec2",
|
|
151
|
+
"vec3",
|
|
152
|
+
"vec4",
|
|
153
|
+
"color",
|
|
154
|
+
"asset-guid"
|
|
155
|
+
];
|
|
156
|
+
function authoringError(code, expected, hint, detail = {}, actual) {
|
|
157
|
+
return {
|
|
158
|
+
code,
|
|
159
|
+
expected,
|
|
160
|
+
hint,
|
|
161
|
+
...actual === void 0 ? {} : { actual },
|
|
162
|
+
detail
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function failure(error) {
|
|
166
|
+
return err(error);
|
|
167
|
+
}
|
|
168
|
+
function isRecord(value) {
|
|
169
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
170
|
+
}
|
|
171
|
+
function validateSceneComponents(value) {
|
|
172
|
+
if (value === void 0) return ok(void 0);
|
|
173
|
+
if (!Array.isArray(value)) {
|
|
174
|
+
return failure(
|
|
175
|
+
parameterFailure(
|
|
176
|
+
"$.sceneComponents",
|
|
177
|
+
"an array of component schema records",
|
|
178
|
+
value,
|
|
179
|
+
"sceneComponents is not an array"
|
|
180
|
+
)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
const components = [];
|
|
184
|
+
const names = /* @__PURE__ */ new Set();
|
|
185
|
+
for (const [componentIndex, candidate] of value.entries()) {
|
|
186
|
+
if (!isRecord(candidate) || typeof candidate.name !== "string" || !isRecord(candidate.fields)) {
|
|
187
|
+
return failure(
|
|
188
|
+
parameterFailure(
|
|
189
|
+
`$.sceneComponents[${componentIndex}]`,
|
|
190
|
+
"a component record with name and fields",
|
|
191
|
+
candidate,
|
|
192
|
+
"scene component schema is malformed"
|
|
193
|
+
)
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
if (candidate.name.length === 0 || names.has(candidate.name)) {
|
|
197
|
+
return failure(
|
|
198
|
+
parameterFailure(
|
|
199
|
+
`$.sceneComponents[${componentIndex}].name`,
|
|
200
|
+
"a non-empty unique component name",
|
|
201
|
+
candidate.name,
|
|
202
|
+
"scene component names must be unique"
|
|
203
|
+
)
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
names.add(candidate.name);
|
|
207
|
+
const fields = {};
|
|
208
|
+
for (const [fieldName, field] of Object.entries(candidate.fields)) {
|
|
209
|
+
if (typeof field === "string" && field.length === 0 || typeof field !== "string" && (!isRecord(field) || typeof field.type !== "string" || field.type.length === 0)) {
|
|
210
|
+
return failure(
|
|
211
|
+
parameterFailure(
|
|
212
|
+
`$.sceneComponents[${componentIndex}].fields.${fieldName}`,
|
|
213
|
+
"a field type string or { type: string }",
|
|
214
|
+
field,
|
|
215
|
+
"scene component field schema is malformed"
|
|
216
|
+
)
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
fields[fieldName] = field;
|
|
220
|
+
}
|
|
221
|
+
components.push({ name: candidate.name, fields });
|
|
222
|
+
}
|
|
223
|
+
return ok(components);
|
|
224
|
+
}
|
|
225
|
+
function isPackageId(value) {
|
|
226
|
+
return value instanceof Uint8Array && value.byteLength === 16;
|
|
227
|
+
}
|
|
228
|
+
function cloneValue(value) {
|
|
229
|
+
if (value instanceof Uint8Array) return value.slice();
|
|
230
|
+
if (Array.isArray(value)) return value.map((item) => cloneValue(item));
|
|
231
|
+
return value;
|
|
232
|
+
}
|
|
233
|
+
function sourceKeyFailure(sourceKey, propertyPath = "$.sourceKey") {
|
|
234
|
+
return authoringError(
|
|
235
|
+
"pack-source-key-invalid",
|
|
236
|
+
"a sourceKey matching ^[a-z0-9][a-z0-9._-]*(/[a-z0-9][a-z0-9._-]*)*$",
|
|
237
|
+
"use a stable lower-case semantic key; do not derive it from file paths or output order",
|
|
238
|
+
{ propertyPath, sourceKey },
|
|
239
|
+
typeof sourceKey === "string" ? sourceKey : void 0
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
function parameterFailure(propertyPath, expected, actual, reason) {
|
|
243
|
+
return authoringError(
|
|
244
|
+
"pack-parameter-invalid",
|
|
245
|
+
expected,
|
|
246
|
+
"repair the parameter declaration or the instance values, then inspect and rebuild",
|
|
247
|
+
{ propertyPath, reason, actual: typeof actual === "string" ? actual : JSON.stringify(actual) }
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
function numericType(type) {
|
|
251
|
+
return type === "u32" || type === "i32" || type === "f32" || type === "f64";
|
|
252
|
+
}
|
|
253
|
+
function vectorLength(type) {
|
|
254
|
+
switch (type) {
|
|
255
|
+
case "vec2":
|
|
256
|
+
return 2;
|
|
257
|
+
case "vec3":
|
|
258
|
+
return 3;
|
|
259
|
+
case "vec4":
|
|
260
|
+
case "color":
|
|
261
|
+
return 4;
|
|
262
|
+
default:
|
|
263
|
+
return void 0;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function parameterValueError(parameter, value, propertyPath) {
|
|
267
|
+
const { type } = parameter;
|
|
268
|
+
if (type === "bool" && typeof value !== "boolean") {
|
|
269
|
+
return parameterFailure(propertyPath, "a boolean", value, "bool value has the wrong type");
|
|
270
|
+
}
|
|
271
|
+
if (numericType(type)) {
|
|
272
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
273
|
+
return parameterFailure(
|
|
274
|
+
propertyPath,
|
|
275
|
+
"a finite number",
|
|
276
|
+
value,
|
|
277
|
+
"numeric value is not finite"
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
if ((type === "u32" || type === "i32") && !Number.isInteger(value)) {
|
|
281
|
+
return parameterFailure(
|
|
282
|
+
propertyPath,
|
|
283
|
+
"an integer",
|
|
284
|
+
value,
|
|
285
|
+
"integer parameter received a fraction"
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
if (type === "u32" && (value < 0 || value > 4294967295)) {
|
|
289
|
+
return parameterFailure(
|
|
290
|
+
propertyPath,
|
|
291
|
+
"an unsigned 32-bit integer",
|
|
292
|
+
value,
|
|
293
|
+
"u32 value is outside [0, 2^32 - 1]"
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
if (type === "i32" && (value < -2147483648 || value > 2147483647)) {
|
|
297
|
+
return parameterFailure(
|
|
298
|
+
propertyPath,
|
|
299
|
+
"a signed 32-bit integer",
|
|
300
|
+
value,
|
|
301
|
+
"i32 value is outside [-2^31, 2^31 - 1]"
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
if (type === "f32" && !Number.isFinite(Math.fround(value))) {
|
|
305
|
+
return parameterFailure(
|
|
306
|
+
propertyPath,
|
|
307
|
+
"a finite IEEE-754 32-bit float",
|
|
308
|
+
value,
|
|
309
|
+
"f32 value overflows binary32"
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
if (parameter.minimum !== void 0 && value < parameter.minimum) {
|
|
313
|
+
return parameterFailure(
|
|
314
|
+
propertyPath,
|
|
315
|
+
`a number >= ${parameter.minimum}`,
|
|
316
|
+
value,
|
|
317
|
+
"value is below minimum"
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
if (parameter.maximum !== void 0 && value > parameter.maximum) {
|
|
321
|
+
return parameterFailure(
|
|
322
|
+
propertyPath,
|
|
323
|
+
`a number <= ${parameter.maximum}`,
|
|
324
|
+
value,
|
|
325
|
+
"value is above maximum"
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
return void 0;
|
|
329
|
+
}
|
|
330
|
+
if (type === "string" && typeof value !== "string") {
|
|
331
|
+
return parameterFailure(propertyPath, "a string", value, "string parameter has the wrong type");
|
|
332
|
+
}
|
|
333
|
+
if (type === "enum") {
|
|
334
|
+
if (typeof value !== "string") {
|
|
335
|
+
return parameterFailure(
|
|
336
|
+
propertyPath,
|
|
337
|
+
"one of the declared enum strings",
|
|
338
|
+
value,
|
|
339
|
+
"enum value has the wrong type"
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
if (parameter.values === void 0 || !parameter.values.includes(value)) {
|
|
343
|
+
return parameterFailure(
|
|
344
|
+
propertyPath,
|
|
345
|
+
"one of the declared enum values",
|
|
346
|
+
value,
|
|
347
|
+
"enum value is not declared"
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
return void 0;
|
|
351
|
+
}
|
|
352
|
+
const length = vectorLength(type);
|
|
353
|
+
if (length !== void 0) {
|
|
354
|
+
if (!Array.isArray(value) || value.length !== length || value.some((component) => typeof component !== "number" || !Number.isFinite(component))) {
|
|
355
|
+
return parameterFailure(
|
|
356
|
+
propertyPath,
|
|
357
|
+
`a finite numeric vector with ${length} components`,
|
|
358
|
+
value,
|
|
359
|
+
"vector value has the wrong shape"
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
if (type === "color" && value.some((component) => component < 0 || component > 1)) {
|
|
363
|
+
return parameterFailure(
|
|
364
|
+
propertyPath,
|
|
365
|
+
"an RGBA color with components in [0, 1]",
|
|
366
|
+
value,
|
|
367
|
+
"color component is outside [0, 1]"
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
return void 0;
|
|
371
|
+
}
|
|
372
|
+
if (type === "asset-guid") {
|
|
373
|
+
const validString = typeof value === "string" && isValidAssetGuidString(value);
|
|
374
|
+
if (!(value instanceof Uint8Array && value.byteLength === 16) && !validString) {
|
|
375
|
+
return parameterFailure(
|
|
376
|
+
propertyPath,
|
|
377
|
+
"a 16-byte AssetGuid or UUID string",
|
|
378
|
+
value,
|
|
379
|
+
"asset GUID value is malformed"
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
return void 0;
|
|
383
|
+
}
|
|
384
|
+
return parameterFailure(
|
|
385
|
+
propertyPath,
|
|
386
|
+
"a supported Pack parameter type",
|
|
387
|
+
value,
|
|
388
|
+
"unknown parameter type"
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
function normalizeParameterValue(parameter, value, propertyPath) {
|
|
392
|
+
const error = parameterValueError(parameter, value, propertyPath);
|
|
393
|
+
if (error !== void 0) return failure(error);
|
|
394
|
+
if (parameter.type === "asset-guid" && typeof value === "string") {
|
|
395
|
+
const parsed = AssetGuid.parse(value);
|
|
396
|
+
if (!parsed.ok) {
|
|
397
|
+
return failure(
|
|
398
|
+
parameterFailure(
|
|
399
|
+
propertyPath,
|
|
400
|
+
"a valid AssetGuid UUID string",
|
|
401
|
+
value,
|
|
402
|
+
"asset GUID parser rejected the value"
|
|
403
|
+
)
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
return ok(parsed.value);
|
|
407
|
+
}
|
|
408
|
+
return ok(cloneValue(value));
|
|
409
|
+
}
|
|
410
|
+
function validateParameterDefinitions(parameters, propertyPath = "$.parameters") {
|
|
411
|
+
if (!Array.isArray(parameters) || parameters.length === 0) {
|
|
412
|
+
return failure(
|
|
413
|
+
parameterFailure(
|
|
414
|
+
propertyPath,
|
|
415
|
+
"a non-empty parameter descriptor array",
|
|
416
|
+
parameters,
|
|
417
|
+
"parameters must be explicit and non-empty"
|
|
418
|
+
)
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
const names = /* @__PURE__ */ new Set();
|
|
422
|
+
const normalized = [];
|
|
423
|
+
for (const [index, candidate] of parameters.entries()) {
|
|
424
|
+
const path = `${propertyPath}[${index}]`;
|
|
425
|
+
if (!isRecord(candidate))
|
|
426
|
+
return failure(
|
|
427
|
+
parameterFailure(
|
|
428
|
+
path,
|
|
429
|
+
"a parameter descriptor object",
|
|
430
|
+
candidate,
|
|
431
|
+
"descriptor is not an object"
|
|
432
|
+
)
|
|
433
|
+
);
|
|
434
|
+
const name = candidate.name;
|
|
435
|
+
if (typeof name !== "string" || !/^[A-Za-z][A-Za-z0-9_.-]*$/.test(name)) {
|
|
436
|
+
return failure(
|
|
437
|
+
parameterFailure(
|
|
438
|
+
`${path}.name`,
|
|
439
|
+
"a unique identifier-like parameter name",
|
|
440
|
+
name,
|
|
441
|
+
"parameter name is invalid"
|
|
442
|
+
)
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
if (names.has(name))
|
|
446
|
+
return failure(
|
|
447
|
+
parameterFailure(
|
|
448
|
+
`${path}.name`,
|
|
449
|
+
"a unique parameter name",
|
|
450
|
+
name,
|
|
451
|
+
"parameter name is duplicated"
|
|
452
|
+
)
|
|
453
|
+
);
|
|
454
|
+
names.add(name);
|
|
455
|
+
const unknown = Object.keys(candidate).find(
|
|
456
|
+
(key) => !["name", "type", "default", "minimum", "maximum", "values", "kind"].includes(key)
|
|
457
|
+
);
|
|
458
|
+
if (unknown !== void 0) {
|
|
459
|
+
return failure(
|
|
460
|
+
parameterFailure(
|
|
461
|
+
`${path}.${unknown}`,
|
|
462
|
+
"only name, type, default, minimum, maximum, values, and kind fields",
|
|
463
|
+
candidate[unknown],
|
|
464
|
+
"parameter descriptors are a closed authoring schema"
|
|
465
|
+
)
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
const type = candidate.type;
|
|
469
|
+
if (typeof type !== "string" || !PACK_PARAMETER_TYPES.includes(type)) {
|
|
470
|
+
return failure(
|
|
471
|
+
parameterFailure(
|
|
472
|
+
`${path}.type`,
|
|
473
|
+
PACK_PARAMETER_TYPES.join(" | "),
|
|
474
|
+
type,
|
|
475
|
+
"parameter type is not supported"
|
|
476
|
+
)
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
const typed = {
|
|
480
|
+
...candidate,
|
|
481
|
+
name,
|
|
482
|
+
type
|
|
483
|
+
};
|
|
484
|
+
if (typed.values !== void 0) {
|
|
485
|
+
if (typed.type !== "enum" || !Array.isArray(typed.values) || typed.values.length === 0 || typed.values.some((item) => typeof item !== "string") || new Set(typed.values).size !== typed.values.length) {
|
|
486
|
+
return failure(
|
|
487
|
+
parameterFailure(
|
|
488
|
+
`${path}.values`,
|
|
489
|
+
"unique non-empty strings for enum",
|
|
490
|
+
typed.values,
|
|
491
|
+
"enum values are malformed"
|
|
492
|
+
)
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
} else if (typed.type === "enum") {
|
|
496
|
+
return failure(
|
|
497
|
+
parameterFailure(
|
|
498
|
+
`${path}.values`,
|
|
499
|
+
"a non-empty enum choices array",
|
|
500
|
+
typed.values,
|
|
501
|
+
"enum has no choices"
|
|
502
|
+
)
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
if (typed.kind !== void 0 && (typed.type !== "asset-guid" || typeof typed.kind !== "string")) {
|
|
506
|
+
return failure(
|
|
507
|
+
parameterFailure(
|
|
508
|
+
`${path}.kind`,
|
|
509
|
+
"a kind constraint only on asset-guid parameters",
|
|
510
|
+
typed.kind,
|
|
511
|
+
"kind is misplaced"
|
|
512
|
+
)
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
for (const field of ["minimum", "maximum"]) {
|
|
516
|
+
const bound = typed[field];
|
|
517
|
+
if (bound !== void 0 && (typeof bound !== "number" || !Number.isFinite(bound))) {
|
|
518
|
+
return failure(
|
|
519
|
+
parameterFailure(
|
|
520
|
+
`${path}.${field}`,
|
|
521
|
+
"a finite numeric bound",
|
|
522
|
+
bound,
|
|
523
|
+
"numeric bound is invalid"
|
|
524
|
+
)
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
if (!numericType(typed.type) && (typed.minimum !== void 0 || typed.maximum !== void 0)) {
|
|
529
|
+
return failure(
|
|
530
|
+
parameterFailure(
|
|
531
|
+
path,
|
|
532
|
+
"minimum/maximum only on numeric parameters",
|
|
533
|
+
typed,
|
|
534
|
+
"numeric bounds are not meaningful for this parameter type"
|
|
535
|
+
)
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
if (typed.minimum !== void 0 && typed.maximum !== void 0 && typed.minimum > typed.maximum) {
|
|
539
|
+
return failure(
|
|
540
|
+
parameterFailure(path, "minimum <= maximum", typed, "numeric bounds are reversed")
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
const defaultResult = normalizeParameterValue(typed, typed.default, `${path}.default`);
|
|
544
|
+
if (!defaultResult.ok) return defaultResult;
|
|
545
|
+
normalized.push({
|
|
546
|
+
...typed,
|
|
547
|
+
default: defaultResult.value,
|
|
548
|
+
...typed.values === void 0 ? {} : { values: Object.freeze([...typed.values]) }
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
return ok(Object.freeze(normalized));
|
|
552
|
+
}
|
|
553
|
+
function definePackageId(value) {
|
|
554
|
+
if (isPackageId(value)) return value.slice();
|
|
555
|
+
const parsed = PackageId.parse(value);
|
|
556
|
+
if (!parsed.ok) {
|
|
557
|
+
const error = authoringError(
|
|
558
|
+
"pack-package-id-invalid",
|
|
559
|
+
"a 36-character RFC 4122 UUID packageId",
|
|
560
|
+
"use PackageId.random() or repair the packageId literal before loading the Pack",
|
|
561
|
+
{ value },
|
|
562
|
+
value
|
|
563
|
+
);
|
|
564
|
+
throw Object.assign(new TypeError(error.hint), error);
|
|
565
|
+
}
|
|
566
|
+
return parsed.value;
|
|
567
|
+
}
|
|
568
|
+
function cloneDefinition(definition) {
|
|
569
|
+
const parameters = "parameters" in definition ? Object.freeze(
|
|
570
|
+
definition.parameters.map(
|
|
571
|
+
(parameter) => Object.freeze({
|
|
572
|
+
...parameter,
|
|
573
|
+
default: cloneValue(parameter.default),
|
|
574
|
+
...parameter.values === void 0 ? {} : { values: Object.freeze(parameter.values.map((value) => cloneValue(value))) }
|
|
575
|
+
})
|
|
576
|
+
)
|
|
577
|
+
) : void 0;
|
|
578
|
+
const sceneComponents = definition.sceneComponents === void 0 ? void 0 : Object.freeze(
|
|
579
|
+
definition.sceneComponents.map(
|
|
580
|
+
(component) => Object.freeze({
|
|
581
|
+
name: component.name,
|
|
582
|
+
fields: Object.freeze({ ...component.fields })
|
|
583
|
+
})
|
|
584
|
+
)
|
|
585
|
+
);
|
|
586
|
+
return Object.freeze({
|
|
587
|
+
...definition,
|
|
588
|
+
packageId: definition.packageId.slice(),
|
|
589
|
+
...parameters === void 0 ? {} : { parameters },
|
|
590
|
+
...sceneComponents === void 0 ? {} : { sceneComponents }
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
function definePack(definition) {
|
|
594
|
+
const validated = validatePackDefinition(definition);
|
|
595
|
+
if (!validated.ok) throw Object.assign(new TypeError(validated.error.hint), validated.error);
|
|
596
|
+
return cloneDefinition(
|
|
597
|
+
validated.value
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
function projectScriptablePackMeta(definition, sourcePath) {
|
|
601
|
+
return {
|
|
602
|
+
schemaVersion: "2.0.0",
|
|
603
|
+
kind: "scriptable-pack-source",
|
|
604
|
+
packageId: PackageId.format(definition.packageId),
|
|
605
|
+
source: sourcePath,
|
|
606
|
+
..."parameters" in definition ? {
|
|
607
|
+
parameters: definition.parameters.map((parameter) => ({
|
|
608
|
+
name: parameter.name,
|
|
609
|
+
type: parameter.type,
|
|
610
|
+
default: jsonParameterValue(parameter.default),
|
|
611
|
+
...parameter.minimum === void 0 ? {} : { minimum: parameter.minimum },
|
|
612
|
+
...parameter.maximum === void 0 ? {} : { maximum: parameter.maximum },
|
|
613
|
+
...parameter.values === void 0 ? {} : { values: parameter.values },
|
|
614
|
+
...parameter.kind === void 0 ? {} : { kind: parameter.kind }
|
|
615
|
+
}))
|
|
616
|
+
} : {}
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
function jsonParameterValue(value) {
|
|
620
|
+
return value instanceof Uint8Array ? AssetGuid.format(value) : value;
|
|
621
|
+
}
|
|
622
|
+
function validatePackDefinition(value, sourcePath) {
|
|
623
|
+
if (!isRecord(value))
|
|
624
|
+
return failure(
|
|
625
|
+
parameterFailure("$", "a Pack definition object", value, "definition is not an object")
|
|
626
|
+
);
|
|
627
|
+
const unknown = Object.keys(value).find(
|
|
628
|
+
(key) => !["schemaVersion", "packageId", "name", "parameters", "sceneComponents", "build"].includes(
|
|
629
|
+
key
|
|
630
|
+
)
|
|
631
|
+
);
|
|
632
|
+
if (unknown !== void 0) {
|
|
633
|
+
return failure(
|
|
634
|
+
parameterFailure(
|
|
635
|
+
`$.${unknown}`,
|
|
636
|
+
"only schemaVersion, packageId, name, parameters, sceneComponents, and build fields",
|
|
637
|
+
value[unknown],
|
|
638
|
+
"legacy static output declarations and external asset tables are not part of v2 authoring"
|
|
639
|
+
)
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
if (value.schemaVersion !== "2.0.0") {
|
|
643
|
+
return failure(
|
|
644
|
+
parameterFailure(
|
|
645
|
+
"$.schemaVersion",
|
|
646
|
+
"the literal '2.0.0'",
|
|
647
|
+
value.schemaVersion,
|
|
648
|
+
"authoring source schema is unsupported"
|
|
649
|
+
)
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
if (!isPackageId(value.packageId)) {
|
|
653
|
+
return failure(
|
|
654
|
+
authoringError(
|
|
655
|
+
"pack-package-id-invalid",
|
|
656
|
+
"a 16-byte PackageId",
|
|
657
|
+
"use definePackageId() for the stable source identity",
|
|
658
|
+
{ sourcePath, propertyPath: "$.packageId" },
|
|
659
|
+
typeof value.packageId === "string" ? value.packageId : void 0
|
|
660
|
+
)
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
if (value.name !== void 0 && typeof value.name !== "string") {
|
|
664
|
+
return failure(
|
|
665
|
+
parameterFailure("$.name", "a string when present", value.name, "display name is malformed")
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
if (typeof value.build !== "function") {
|
|
669
|
+
return failure(
|
|
670
|
+
parameterFailure("$.build", "a build function", value.build, "build is not callable")
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
const sceneComponents = validateSceneComponents(value.sceneComponents);
|
|
674
|
+
if (!sceneComponents.ok) return sceneComponents;
|
|
675
|
+
const hasParameters = Object.hasOwn(value, "parameters");
|
|
676
|
+
if (!hasParameters) {
|
|
677
|
+
return ok(
|
|
678
|
+
Object.freeze({
|
|
679
|
+
schemaVersion: "2.0.0",
|
|
680
|
+
packageId: value.packageId,
|
|
681
|
+
...value.name === void 0 ? {} : { name: value.name },
|
|
682
|
+
...sceneComponents.value === void 0 ? {} : { sceneComponents: sceneComponents.value },
|
|
683
|
+
build: value.build
|
|
684
|
+
})
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
const parameters = validateParameterDefinitions(value.parameters);
|
|
688
|
+
if (!parameters.ok) return parameters;
|
|
689
|
+
return ok(
|
|
690
|
+
Object.freeze({
|
|
691
|
+
schemaVersion: "2.0.0",
|
|
692
|
+
packageId: value.packageId,
|
|
693
|
+
...value.name === void 0 ? {} : { name: value.name },
|
|
694
|
+
...sceneComponents.value === void 0 ? {} : { sceneComponents: sceneComponents.value },
|
|
695
|
+
parameters: parameters.value,
|
|
696
|
+
build: value.build
|
|
697
|
+
})
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
function hasPackParameters(value) {
|
|
701
|
+
return isRecord(value) && Array.isArray(value.parameters) && value.parameters.length > 0;
|
|
702
|
+
}
|
|
703
|
+
function resolvePackParameterValues(definition, overrides = {}, inheritedValues) {
|
|
704
|
+
if (!isRecord(overrides)) {
|
|
705
|
+
return failure(
|
|
706
|
+
parameterFailure(
|
|
707
|
+
"$.values",
|
|
708
|
+
"an object of sparse parameter overrides",
|
|
709
|
+
overrides,
|
|
710
|
+
"values is not an object"
|
|
711
|
+
)
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
const descriptors = new Map(
|
|
715
|
+
definition.parameters.map((parameter) => [parameter.name, parameter])
|
|
716
|
+
);
|
|
717
|
+
for (const name of Object.keys(overrides)) {
|
|
718
|
+
if (!descriptors.has(name)) {
|
|
719
|
+
return failure(
|
|
720
|
+
parameterFailure(
|
|
721
|
+
`$.values.${name}`,
|
|
722
|
+
"a declared parameter name",
|
|
723
|
+
name,
|
|
724
|
+
"instance contains an unknown parameter"
|
|
725
|
+
)
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
const values = {};
|
|
730
|
+
for (const parameter of definition.parameters) {
|
|
731
|
+
const raw = Object.hasOwn(overrides, parameter.name) ? overrides[parameter.name] : inheritedValues?.[parameter.name] ?? parameter.default;
|
|
732
|
+
const normalized = normalizeParameterValue(parameter, raw, `$.values.${parameter.name}`);
|
|
733
|
+
if (!normalized.ok) return normalized;
|
|
734
|
+
values[parameter.name] = normalized.value;
|
|
735
|
+
}
|
|
736
|
+
return ok(Object.freeze(values));
|
|
737
|
+
}
|
|
738
|
+
function jsonError(propertyPath, expected, actual, reason) {
|
|
739
|
+
return failure(
|
|
740
|
+
authoringError(
|
|
741
|
+
"pack-parameter-invalid",
|
|
742
|
+
expected,
|
|
743
|
+
"repair the v3 pack.json authoring data, then rerun scan or rebuild",
|
|
744
|
+
{
|
|
745
|
+
propertyPath,
|
|
746
|
+
reason,
|
|
747
|
+
actual: typeof actual === "string" ? actual : JSON.stringify(actual)
|
|
748
|
+
}
|
|
749
|
+
)
|
|
750
|
+
);
|
|
751
|
+
}
|
|
752
|
+
function parsePackageId(value, propertyPath) {
|
|
753
|
+
if (typeof value !== "string") {
|
|
754
|
+
return failure(
|
|
755
|
+
authoringError(
|
|
756
|
+
"pack-package-id-invalid",
|
|
757
|
+
"a UUID string",
|
|
758
|
+
"repair packageId in the authoring file",
|
|
759
|
+
{ propertyPath, value }
|
|
760
|
+
)
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
const parsed = PackageId.parse(value);
|
|
764
|
+
if (!parsed.ok) {
|
|
765
|
+
return failure(
|
|
766
|
+
authoringError(
|
|
767
|
+
"pack-package-id-invalid",
|
|
768
|
+
"a 36-character RFC 4122 UUID",
|
|
769
|
+
"repair packageId or use the authoring gateway to mint one",
|
|
770
|
+
{ propertyPath, value },
|
|
771
|
+
value
|
|
772
|
+
)
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
return ok(parsed.value);
|
|
776
|
+
}
|
|
777
|
+
function parsePackJsonAsset(value, sourceKey) {
|
|
778
|
+
if (!isRecord(value))
|
|
779
|
+
return jsonError(
|
|
780
|
+
`$.assets.${sourceKey}`,
|
|
781
|
+
"an asset entry object",
|
|
782
|
+
value,
|
|
783
|
+
"asset entry is not an object"
|
|
784
|
+
);
|
|
785
|
+
const allowed = /* @__PURE__ */ new Set(["kind", "payload", "refs", "name", "artifacts"]);
|
|
786
|
+
const unknown = Object.keys(value).find((key) => !allowed.has(key));
|
|
787
|
+
if (unknown !== void 0)
|
|
788
|
+
return jsonError(
|
|
789
|
+
`$.assets.${sourceKey}.${unknown}`,
|
|
790
|
+
"no GUID or extra authoring field",
|
|
791
|
+
value[unknown],
|
|
792
|
+
"direct v3 entries derive identity from their object key"
|
|
793
|
+
);
|
|
794
|
+
if (typeof value.kind !== "string" || value.kind.length === 0)
|
|
795
|
+
return jsonError(
|
|
796
|
+
`$.assets.${sourceKey}.kind`,
|
|
797
|
+
"a non-empty asset kind",
|
|
798
|
+
value.kind,
|
|
799
|
+
"asset kind is missing"
|
|
800
|
+
);
|
|
801
|
+
if (!isRecord(value.payload))
|
|
802
|
+
return jsonError(
|
|
803
|
+
`$.assets.${sourceKey}.payload`,
|
|
804
|
+
"a JSON object payload",
|
|
805
|
+
value.payload,
|
|
806
|
+
"payload must be a plain object"
|
|
807
|
+
);
|
|
808
|
+
if (!Array.isArray(value.refs) || value.refs.some((ref) => typeof ref !== "string" || !isValidAssetGuidString(ref))) {
|
|
809
|
+
return jsonError(
|
|
810
|
+
`$.assets.${sourceKey}.refs`,
|
|
811
|
+
"an array of UUID references",
|
|
812
|
+
value.refs,
|
|
813
|
+
"references must already be real AssetGuids"
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
if (value.name !== void 0 && (typeof value.name !== "string" || value.name.length === 0))
|
|
817
|
+
return jsonError(
|
|
818
|
+
`$.assets.${sourceKey}.name`,
|
|
819
|
+
"a non-empty display name when present",
|
|
820
|
+
value.name,
|
|
821
|
+
"asset name is malformed"
|
|
822
|
+
);
|
|
823
|
+
if (value.artifacts !== void 0 && !isRecord(value.artifacts))
|
|
824
|
+
return jsonError(
|
|
825
|
+
`$.assets.${sourceKey}.artifacts`,
|
|
826
|
+
"an artifact descriptor object when present",
|
|
827
|
+
value.artifacts,
|
|
828
|
+
"artifacts are not an object"
|
|
829
|
+
);
|
|
830
|
+
const base = {
|
|
831
|
+
kind: value.kind,
|
|
832
|
+
payload: value.payload,
|
|
833
|
+
refs: Object.freeze([...value.refs]),
|
|
834
|
+
...value.name === void 0 ? {} : { name: value.name }
|
|
835
|
+
};
|
|
836
|
+
if (value.artifacts === void 0) return ok(base);
|
|
837
|
+
const withArtifacts = {
|
|
838
|
+
...base,
|
|
839
|
+
artifacts: value.artifacts
|
|
840
|
+
};
|
|
841
|
+
return ok(withArtifacts);
|
|
842
|
+
}
|
|
843
|
+
function parsePackSourceJson(value) {
|
|
844
|
+
if (!isRecord(value))
|
|
845
|
+
return jsonError("$", "a v3 pack.json object", value, "document is not an object");
|
|
846
|
+
if (value.schemaVersion !== "3.0.0")
|
|
847
|
+
return jsonError(
|
|
848
|
+
"$.schemaVersion",
|
|
849
|
+
"the literal '3.0.0'",
|
|
850
|
+
value.schemaVersion,
|
|
851
|
+
"document schema is not v3"
|
|
852
|
+
);
|
|
853
|
+
const unknown = Object.keys(value).find(
|
|
854
|
+
(key) => !["schemaVersion", "packageId", "assets", "parent", "values"].includes(key)
|
|
855
|
+
);
|
|
856
|
+
if (unknown !== void 0)
|
|
857
|
+
return jsonError(
|
|
858
|
+
`$.${unknown}`,
|
|
859
|
+
"no extra top-level authoring fields",
|
|
860
|
+
value[unknown],
|
|
861
|
+
"keep the v3 document to packageId+assets or packageId+parent+values"
|
|
862
|
+
);
|
|
863
|
+
const packageResult = parsePackageId(value.packageId, "$.packageId");
|
|
864
|
+
if (!packageResult.ok) return packageResult;
|
|
865
|
+
const hasAssets = Object.hasOwn(value, "assets");
|
|
866
|
+
const hasParent = Object.hasOwn(value, "parent");
|
|
867
|
+
const hasValues = Object.hasOwn(value, "values");
|
|
868
|
+
if (hasAssets && (hasParent || hasValues))
|
|
869
|
+
return jsonError(
|
|
870
|
+
"$",
|
|
871
|
+
"assets or parent+values, never both",
|
|
872
|
+
value,
|
|
873
|
+
"direct and instance branches are mutually exclusive"
|
|
874
|
+
);
|
|
875
|
+
if (hasAssets) {
|
|
876
|
+
if (!isRecord(value.assets))
|
|
877
|
+
return jsonError(
|
|
878
|
+
"$.assets",
|
|
879
|
+
"a sourceKey to asset object",
|
|
880
|
+
value.assets,
|
|
881
|
+
"direct assets are not an object"
|
|
882
|
+
);
|
|
883
|
+
const assets = {};
|
|
884
|
+
for (const [sourceKey, entry] of Object.entries(value.assets)) {
|
|
885
|
+
if (!isValidPackSourceKey(sourceKey))
|
|
886
|
+
return failure(sourceKeyFailure(sourceKey, `$.assets.${sourceKey}`));
|
|
887
|
+
const parsed = parsePackJsonAsset(entry, sourceKey);
|
|
888
|
+
if (!parsed.ok) return parsed;
|
|
889
|
+
assets[sourceKey] = parsed.value;
|
|
890
|
+
}
|
|
891
|
+
return ok({
|
|
892
|
+
format: "direct",
|
|
893
|
+
schemaVersion: "3.0.0",
|
|
894
|
+
packageId: packageResult.value,
|
|
895
|
+
assets: Object.freeze(assets)
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
if (!hasParent || !hasValues || hasAssets)
|
|
899
|
+
return jsonError(
|
|
900
|
+
"$",
|
|
901
|
+
"parent and values for an instance document",
|
|
902
|
+
value,
|
|
903
|
+
"instance branch is incomplete"
|
|
904
|
+
);
|
|
905
|
+
const parentResult = parsePackageId(value.parent, "$.parent");
|
|
906
|
+
if (!parentResult.ok) return parentResult;
|
|
907
|
+
if (!isRecord(value.values))
|
|
908
|
+
return jsonError(
|
|
909
|
+
"$.values",
|
|
910
|
+
"a sparse parameter object",
|
|
911
|
+
value.values,
|
|
912
|
+
"instance values are not an object"
|
|
913
|
+
);
|
|
914
|
+
return ok({
|
|
915
|
+
format: "instance",
|
|
916
|
+
schemaVersion: "3.0.0",
|
|
917
|
+
packageId: packageResult.value,
|
|
918
|
+
parent: parentResult.value,
|
|
919
|
+
values: Object.freeze({ ...value.values })
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
function projectDirectPackJson(value) {
|
|
923
|
+
const parsed = "format" in value ? ok(value) : parsePackSourceJson(value);
|
|
924
|
+
if (!parsed.ok) return parsed;
|
|
925
|
+
if (parsed.value.format !== "direct") {
|
|
926
|
+
return failure(
|
|
927
|
+
authoringError(
|
|
928
|
+
"pack-parameter-invalid",
|
|
929
|
+
"a direct v3 pack.json",
|
|
930
|
+
"projectDirectPackJson accepts the direct branch only",
|
|
931
|
+
{ observed: parsed.value.format }
|
|
932
|
+
)
|
|
933
|
+
);
|
|
934
|
+
}
|
|
935
|
+
const assets = [];
|
|
936
|
+
for (const [sourceKey, entry] of Object.entries(parsed.value.assets).sort(
|
|
937
|
+
([left], [right]) => left.localeCompare(right)
|
|
938
|
+
)) {
|
|
939
|
+
assets.push({
|
|
940
|
+
...entry,
|
|
941
|
+
guid: AssetGuid.format(AssetGuid.derive(parsed.value.packageId, sourceKey)),
|
|
942
|
+
sourceKey
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
return ok({ packageId: PackageId.format(parsed.value.packageId), assets: Object.freeze(assets) });
|
|
946
|
+
}
|
|
947
|
+
function subjectId(subject) {
|
|
948
|
+
return PackageId.format(subject.packageId).toLowerCase();
|
|
949
|
+
}
|
|
950
|
+
async function resolvePackParameterInheritance(subject, readParent) {
|
|
951
|
+
const visited = /* @__PURE__ */ new Set();
|
|
952
|
+
async function visit(current, path) {
|
|
953
|
+
const currentId = subjectId(current);
|
|
954
|
+
if (visited.has(currentId) || path.includes(currentId)) {
|
|
955
|
+
return failure(
|
|
956
|
+
authoringError(
|
|
957
|
+
"pack-parent-cycle",
|
|
958
|
+
"a finite acyclic Pack parent chain",
|
|
959
|
+
"inspect parentChain and repair the first repeated packageId",
|
|
960
|
+
{ packageId: currentId, parentChain: [...path, currentId] }
|
|
961
|
+
)
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
visited.add(currentId);
|
|
965
|
+
const nextPath = [...path, currentId];
|
|
966
|
+
if (current.format === "direct") {
|
|
967
|
+
return failure(
|
|
968
|
+
authoringError(
|
|
969
|
+
"pack-parent-has-no-parameters",
|
|
970
|
+
"a ScriptablePack source with parameters as the parent",
|
|
971
|
+
"direct packs cannot be instance parents; point the instance at a ScriptablePack source with parameters",
|
|
972
|
+
{ packageId: currentId }
|
|
973
|
+
)
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
if (current.format === "source") {
|
|
977
|
+
const descriptors2 = validateParameterDefinitions(current.parameters, "$.parameters");
|
|
978
|
+
if (!descriptors2.ok) return descriptors2;
|
|
979
|
+
if (descriptors2.value.length === 0) {
|
|
980
|
+
return failure(
|
|
981
|
+
authoringError(
|
|
982
|
+
"pack-parent-has-no-parameters",
|
|
983
|
+
"the parent source to declare a non-empty parameter list",
|
|
984
|
+
"use clone for an independent zero-parameter Pack instead of creating an empty instance",
|
|
985
|
+
{ packageId: currentId }
|
|
986
|
+
)
|
|
987
|
+
);
|
|
988
|
+
}
|
|
989
|
+
const definition2 = {
|
|
990
|
+
packageId: current.packageId,
|
|
991
|
+
parameters: descriptors2.value};
|
|
992
|
+
const values2 = resolvePackParameterValues(definition2);
|
|
993
|
+
if (!values2.ok) return values2;
|
|
994
|
+
return ok({
|
|
995
|
+
packageId: current.packageId,
|
|
996
|
+
rootPackageId: current.packageId,
|
|
997
|
+
values: values2.value,
|
|
998
|
+
parentChain: [],
|
|
999
|
+
parameters: descriptors2.value
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
const parent = await readParent(current.parent);
|
|
1003
|
+
if (parent === void 0) {
|
|
1004
|
+
return failure(
|
|
1005
|
+
authoringError(
|
|
1006
|
+
"pack-parent-not-found",
|
|
1007
|
+
"the parent packageId to resolve in the source index",
|
|
1008
|
+
"inspect the parent locator or recreate the instance from a live ScriptablePack with parameters",
|
|
1009
|
+
{ packageId: currentId, parent: PackageId.format(current.parent) }
|
|
1010
|
+
)
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
if (PackageId.format(parent.packageId).toLowerCase() !== PackageId.format(current.parent).toLowerCase()) {
|
|
1014
|
+
return failure(
|
|
1015
|
+
authoringError(
|
|
1016
|
+
"pack-parent-not-found",
|
|
1017
|
+
"the parent reader to return the requested packageId",
|
|
1018
|
+
"repair the Source Index locator and retry inheritance resolution",
|
|
1019
|
+
{
|
|
1020
|
+
packageId: currentId,
|
|
1021
|
+
requestedParent: PackageId.format(current.parent),
|
|
1022
|
+
observedParent: PackageId.format(parent.packageId)
|
|
1023
|
+
}
|
|
1024
|
+
)
|
|
1025
|
+
);
|
|
1026
|
+
}
|
|
1027
|
+
const resolvedParent = await visit(parent, nextPath);
|
|
1028
|
+
if (!resolvedParent.ok) return resolvedParent;
|
|
1029
|
+
const descriptors = resolvedParent.value.parameters;
|
|
1030
|
+
const definition = {
|
|
1031
|
+
packageId: resolvedParent.value.rootPackageId,
|
|
1032
|
+
parameters: descriptors};
|
|
1033
|
+
const values = resolvePackParameterValues(
|
|
1034
|
+
definition,
|
|
1035
|
+
current.values,
|
|
1036
|
+
resolvedParent.value.values
|
|
1037
|
+
);
|
|
1038
|
+
if (!values.ok) return values;
|
|
1039
|
+
return ok({
|
|
1040
|
+
packageId: current.packageId,
|
|
1041
|
+
rootPackageId: resolvedParent.value.rootPackageId,
|
|
1042
|
+
values: values.value,
|
|
1043
|
+
parentChain: [PackageId.format(current.parent), ...resolvedParent.value.parentChain],
|
|
1044
|
+
parameters: descriptors
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
return visit(subject, []);
|
|
1048
|
+
}
|
|
1049
|
+
var resolvePackInheritance = resolvePackParameterInheritance;
|
|
1050
|
+
var PACK_AUTHORING_OPERATION_IDS = [
|
|
1051
|
+
"asset.list",
|
|
1052
|
+
"asset.inspect",
|
|
1053
|
+
"asset.resolve",
|
|
1054
|
+
"asset.verify",
|
|
1055
|
+
"asset-source.create",
|
|
1056
|
+
"asset-source.clone",
|
|
1057
|
+
"asset-source.create-instance",
|
|
1058
|
+
"asset-source.apply-values",
|
|
1059
|
+
"asset-source.rebuild",
|
|
1060
|
+
"asset-source.cold-cook"
|
|
1061
|
+
];
|
|
1062
|
+
function createPackAuthoringGateway(port) {
|
|
1063
|
+
const requests = /* @__PURE__ */ new Map();
|
|
1064
|
+
return {
|
|
1065
|
+
execute(operation) {
|
|
1066
|
+
if (typeof operation.requestId !== "string" || operation.requestId.trim().length === 0) {
|
|
1067
|
+
return Promise.resolve(
|
|
1068
|
+
failure(
|
|
1069
|
+
authoringError(
|
|
1070
|
+
"pack-source-revision-conflict",
|
|
1071
|
+
"a non-empty caller-minted requestId",
|
|
1072
|
+
"mint a new requestId and retry",
|
|
1073
|
+
{ requestId: operation.requestId }
|
|
1074
|
+
)
|
|
1075
|
+
)
|
|
1076
|
+
);
|
|
1077
|
+
}
|
|
1078
|
+
let fingerprint;
|
|
1079
|
+
try {
|
|
1080
|
+
fingerprint = JSON.stringify(operation);
|
|
1081
|
+
} catch (cause) {
|
|
1082
|
+
return Promise.resolve(
|
|
1083
|
+
failure(
|
|
1084
|
+
authoringError(
|
|
1085
|
+
"pack-parameter-invalid",
|
|
1086
|
+
"a JSON-serialisable authoring operation",
|
|
1087
|
+
"remove non-serialisable operation fields and retry with a new requestId",
|
|
1088
|
+
{
|
|
1089
|
+
requestId: operation.requestId,
|
|
1090
|
+
cause: cause instanceof Error ? cause.message : String(cause)
|
|
1091
|
+
}
|
|
1092
|
+
)
|
|
1093
|
+
)
|
|
1094
|
+
);
|
|
1095
|
+
}
|
|
1096
|
+
const previous = requests.get(operation.requestId);
|
|
1097
|
+
if (previous !== void 0) {
|
|
1098
|
+
if (previous.fingerprint === fingerprint) return previous.result;
|
|
1099
|
+
return Promise.resolve(
|
|
1100
|
+
failure(
|
|
1101
|
+
authoringError(
|
|
1102
|
+
"pack-source-revision-conflict",
|
|
1103
|
+
"one immutable operation per requestId",
|
|
1104
|
+
"read the original result or mint a new requestId",
|
|
1105
|
+
{ requestId: operation.requestId }
|
|
1106
|
+
)
|
|
1107
|
+
)
|
|
1108
|
+
);
|
|
1109
|
+
}
|
|
1110
|
+
const result = Promise.resolve().then(() => port.execute(operation)).catch(
|
|
1111
|
+
(cause) => failure(
|
|
1112
|
+
authoringError(
|
|
1113
|
+
"pack-parameter-invalid",
|
|
1114
|
+
"the Pack authoring port to return a structured Result",
|
|
1115
|
+
"repair the gateway port failure, then retry with a new requestId",
|
|
1116
|
+
{
|
|
1117
|
+
requestId: operation.requestId,
|
|
1118
|
+
cause: cause instanceof Error ? cause.message : String(cause)
|
|
1119
|
+
}
|
|
1120
|
+
)
|
|
1121
|
+
)
|
|
1122
|
+
);
|
|
1123
|
+
requests.set(operation.requestId, { fingerprint, result });
|
|
1124
|
+
return result;
|
|
1125
|
+
}
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
var PACK_AUTHORING_OPERATION_DESCRIPTORS = PACK_AUTHORING_OPERATION_IDS.map((id) => ({
|
|
1129
|
+
id,
|
|
1130
|
+
domain: id.startsWith("asset-source.") ? "session" : "asset",
|
|
1131
|
+
readOnly: id.startsWith("asset."),
|
|
1132
|
+
requiresRevision: !id.startsWith("asset.") && id !== "asset-source.create"
|
|
1133
|
+
}));
|
|
1134
|
+
|
|
1135
|
+
export { PACK_AUTHORING_OPERATION_DESCRIPTORS, PACK_AUTHORING_OPERATION_IDS, PACK_PARAMETER_TYPES, createPackAuthoringGateway, definePack, definePackageId, hasPackParameters, parsePackSourceJson, projectDirectPackJson, projectScriptablePackMeta, resolvePackInheritance, resolvePackParameterInheritance, resolvePackParameterValues, validatePackDefinition };
|
|
1136
|
+
//# sourceMappingURL=pack-authoring.mjs.map
|
|
1137
|
+
//# sourceMappingURL=pack-authoring.mjs.map
|