@plasius/gpu-shader 0.1.1 → 0.1.2

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/testing.js CHANGED
@@ -74,6 +74,29 @@ function canonicalizeGpuContract(value) {
74
74
  return visit(value, "$root");
75
75
  }
76
76
 
77
+ // src/asset-version.ts
78
+ var ASSET_VERSION_PATTERN = /^[0-9A-Za-z][0-9A-Za-z._-]{0,127}$/u;
79
+ var MUTABLE_VERSION_ALIASES = /* @__PURE__ */ new Set([
80
+ "latest",
81
+ "current",
82
+ "stable",
83
+ "preview",
84
+ "default",
85
+ "production",
86
+ "canary",
87
+ "next",
88
+ "head",
89
+ "main"
90
+ ]);
91
+ var X_WILDCARD_SEGMENT_PATTERN = /^(?:[vV])?[xX](?:\.|-|$)|\.[xX](?:\.|-|$)/u;
92
+ var IMMUTABLE_VERSION_ERROR_MESSAGE = "Immutable asset version must be an exact token up to 128 characters; mutable aliases, ranges, wildcards, and URLs are not allowed.";
93
+ function assertImmutableAssetVersion(value) {
94
+ if (typeof value !== "string" || !ASSET_VERSION_PATTERN.test(value) || MUTABLE_VERSION_ALIASES.has(value.toLowerCase()) || X_WILDCARD_SEGMENT_PATTERN.test(value)) {
95
+ throw new TypeError(IMMUTABLE_VERSION_ERROR_MESSAGE);
96
+ }
97
+ return value;
98
+ }
99
+
77
100
  // src/hash.ts
78
101
  var SHA256_PATTERN = /^[a-f0-9]{64}$/u;
79
102
  function asSha256Hex(value) {
@@ -96,6 +119,15 @@ async function computeSha256(value) {
96
119
 
97
120
  // src/manifest-validation.ts
98
121
  var stages = ["vertex", "fragment", "compute"];
122
+ function detachedJson(value, path2) {
123
+ try {
124
+ return JSON.parse(canonicalizeGpuContract(value));
125
+ } catch (cause) {
126
+ const detail = cause instanceof Error ? cause.message : "Contract snapshot failed.";
127
+ const boundedDetail = [...detail].slice(0, 512).join("");
128
+ throw new TypeError(`${path2} must contain detached JSON contract data: ${boundedDetail}`, { cause });
129
+ }
130
+ }
99
131
  function object(value, path2) {
100
132
  if (typeof value !== "object" || value === null || Array.isArray(value)) throw new TypeError(`${path2} must be an object.`);
101
133
  return value;
@@ -308,7 +340,7 @@ function pipeline(value, path2, moduleIds) {
308
340
  return input;
309
341
  }
310
342
  function parseSerializableGpuPipelineDescriptors(value, moduleIds, path2 = "pipelines") {
311
- const parsed = array(value, path2).map((item, index) => pipeline(item, `${path2}[${index}]`, new Set(moduleIds)));
343
+ const parsed = array(detachedJson(value, path2), path2).map((item, index) => pipeline(item, `${path2}[${index}]`, new Set(moduleIds)));
312
344
  unique(parsed, (item) => item.pipelineId, path2);
313
345
  return freeze(parsed);
314
346
  }
@@ -326,6 +358,11 @@ function issue(code, message, path2) {
326
358
  function object2(value) {
327
359
  return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
328
360
  }
361
+ function freezeJson(value) {
362
+ if (typeof value !== "object" || value === null || Object.isFrozen(value)) return value;
363
+ for (const child of Object.values(value)) freezeJson(child);
364
+ return Object.freeze(value);
365
+ }
329
366
  function exactKeys(value, keys2) {
330
367
  const expected = new Set(keys2);
331
368
  return Object.keys(value).every((key) => expected.has(key)) && keys2.every((key) => key in value);
@@ -333,6 +370,14 @@ function exactKeys(value, keys2) {
333
370
  function safeToken(value) {
334
371
  return typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,159}$/u.test(value) && !value.includes("..");
335
372
  }
373
+ function immutableVersion(value) {
374
+ try {
375
+ assertImmutableAssetVersion(value);
376
+ return true;
377
+ } catch {
378
+ return false;
379
+ }
380
+ }
336
381
  function validSha(value) {
337
382
  try {
338
383
  asSha256Hex(String(value));
@@ -345,8 +390,20 @@ function defineShaderCompileUnit(unit) {
345
390
  return Object.freeze(unit);
346
391
  }
347
392
  function validateCompileUnitInventory(value) {
348
- const envelope = object2(value);
349
- const candidate = envelope && "inventory" in envelope ? object2(value.inventory) : envelope;
393
+ let snapshot;
394
+ try {
395
+ snapshot = JSON.parse(canonicalizeGpuContract(value));
396
+ } catch (cause) {
397
+ return {
398
+ ok: false,
399
+ diagnostics: [issue(
400
+ "invalid-contract",
401
+ cause instanceof Error ? cause.message : "Compile-unit inventory must contain detached JSON data."
402
+ )]
403
+ };
404
+ }
405
+ const envelope = object2(snapshot);
406
+ const candidate = envelope && Object.hasOwn(envelope, "inventory") ? object2(snapshot.inventory) : envelope;
350
407
  const diagnostics = [];
351
408
  if (!candidate || !exactKeys(candidate, ["contractVersion", "fragments", "compileUnits"])) {
352
409
  return { ok: false, diagnostics: [issue("invalid-contract", "Compile-unit inventory has unknown or missing fields.")] };
@@ -477,7 +534,7 @@ function validateCompileUnitInventory(value) {
477
534
  diagnostics.push(issue("invalid-contract", cause instanceof Error ? cause.message : "Compile-unit pipelines are invalid.", `${path2}.pipelines`));
478
535
  }
479
536
  const interfaceRef = object2(unit.interfaceRef);
480
- if (!interfaceRef || !exactKeys(interfaceRef, ["interfaceId", "interfaceVersion", "manifestUri", "manifestSha256", "interfaceAbiHash", "modelAbiHash"]) || !safeToken(interfaceRef.interfaceId) || !safeToken(interfaceRef.interfaceVersion) || typeof interfaceRef.manifestUri !== "string" || !interfaceRef.manifestUri.startsWith("https://") || !validSha(interfaceRef.manifestSha256) || !validSha(interfaceRef.interfaceAbiHash) || !validSha(interfaceRef.modelAbiHash)) {
537
+ if (!interfaceRef || !exactKeys(interfaceRef, ["interfaceId", "interfaceVersion", "manifestUri", "manifestSha256", "interfaceAbiHash", "modelAbiHash"]) || !safeToken(interfaceRef.interfaceId) || !immutableVersion(interfaceRef.interfaceVersion) || typeof interfaceRef.manifestUri !== "string" || !interfaceRef.manifestUri.startsWith("https://") || !validSha(interfaceRef.manifestSha256) || !validSha(interfaceRef.interfaceAbiHash) || !validSha(interfaceRef.modelAbiHash)) {
481
538
  diagnostics.push(issue("invalid-contract", "Compile-unit interfaceRef is invalid.", `${path2}.interfaceRef`));
482
539
  }
483
540
  const fixture = object2(unit.qualificationFixture);
@@ -488,7 +545,7 @@ function validateCompileUnitInventory(value) {
488
545
  for (const fragmentId of fragmentIds) {
489
546
  if (!covered.has(fragmentId)) diagnostics.push(issue("uncovered-fragment", `WGSL fragment ${fragmentId} has no compile unit.`));
490
547
  }
491
- return diagnostics.length > 0 ? { ok: false, diagnostics } : { ok: true, value: candidate };
548
+ return diagnostics.length > 0 ? { ok: false, diagnostics } : { ok: true, value: freezeJson(candidate) };
492
549
  }
493
550
 
494
551
  // src/testing/matrix.ts
@@ -715,6 +772,18 @@ function object4(value, path2) {
715
772
  if (typeof value !== "object" || value === null || Array.isArray(value)) throw new TypeError(`${path2} must be an object.`);
716
773
  return value;
717
774
  }
775
+ function detachedJson2(value, label) {
776
+ try {
777
+ return JSON.parse(canonicalizeGpuContract(value));
778
+ } catch (cause) {
779
+ throw new TypeError(`${label} must contain detached JSON contract data.`, { cause });
780
+ }
781
+ }
782
+ function freezeJson2(value) {
783
+ if (typeof value !== "object" || value === null || Object.isFrozen(value)) return value;
784
+ for (const child of Object.values(value)) freezeJson2(child);
785
+ return Object.freeze(value);
786
+ }
718
787
  function exact2(value, keys2, path2) {
719
788
  const expected = new Set(keys2);
720
789
  if (!Object.keys(value).every((key) => expected.has(key)) || !keys2.every((key) => key in value)) throw new TypeError(`${path2} has unknown or missing fields.`);
@@ -723,6 +792,16 @@ function token2(value, path2) {
723
792
  if (typeof value !== "string" || !TOKEN.test(value) || value.includes("..")) throw new TypeError(`${path2} must be a safe token.`);
724
793
  return value;
725
794
  }
795
+ function immutableVersion2(value, path2) {
796
+ try {
797
+ return assertImmutableAssetVersion(value);
798
+ } catch (cause) {
799
+ throw new TypeError(
800
+ `${path2} must be an immutable asset version: exact token required; mutable aliases, ranges, wildcards, and URLs are not allowed.`,
801
+ { cause }
802
+ );
803
+ }
804
+ }
726
805
  function path(value, label, extensions) {
727
806
  if (typeof value !== "string" || !SAFE_PATH2.test(value) || !extensions.some((extension) => value.endsWith(extension))) {
728
807
  throw new TypeError(`${label} must be a safe data-only bundle path (${extensions.join(", ")}).`);
@@ -857,7 +936,7 @@ function jsonValue(value, label, depth = 0) {
857
936
  }
858
937
  function validateQualificationFixture(value, unit) {
859
938
  try {
860
- const fixture = object4(value, "fixture");
939
+ const fixture = object4(detachedJson2(value, "fixture"), "fixture");
861
940
  exact2(fixture, ["contractVersion", "fixtureId", "resources", "bindGroups", "commands", "layoutProbes", "readbacks", "bounds"], "fixture");
862
941
  if (fixture.contractVersion !== SHADER_QUALIFICATION_FIXTURE_VERSION) throw new TypeError("Unsupported qualification fixture version.");
863
942
  token2(fixture.fixtureId, "fixture.fixtureId");
@@ -1193,14 +1272,14 @@ function validateQualificationFixture(value, unit) {
1193
1272
  }
1194
1273
  unique2(probeIds, "fixture.layoutProbes probe IDs");
1195
1274
  unique2(probeSources, "fixture.layoutProbes model sources");
1196
- return { ok: true, value: fixture };
1275
+ return { ok: true, value: freezeJson2(fixture) };
1197
1276
  } catch (cause) {
1198
1277
  return { ok: false, diagnostics: [{ code: "invalid-contract", severity: "error", message: cause instanceof Error ? cause.message : "Invalid qualification fixture." }] };
1199
1278
  }
1200
1279
  }
1201
1280
  function validateQualificationBundleManifest(value) {
1202
1281
  try {
1203
- const bundle = object4(value, "qualification");
1282
+ const bundle = object4(detachedJson2(value, "qualification"), "qualification");
1204
1283
  exact2(bundle, ["contractVersion", "inventory", "subject", "shaderManifestCorePath", "gpuInterfaceManifest", "modelCompatibilityFixtures", "modules", "fixtures"], "qualification");
1205
1284
  if (bundle.contractVersion !== SHADER_QUALIFICATION_BUNDLE_VERSION) throw new TypeError("Unsupported qualification bundle version.");
1206
1285
  const inventory = validateCompileUnitInventory(bundle.inventory);
@@ -1210,7 +1289,7 @@ function validateQualificationBundleManifest(value) {
1210
1289
  const candidate = object4(subject.shaderManifestCore, "qualification.subject.shaderManifestCore");
1211
1290
  exact2(candidate, ["shaderId", "version", "sha256"], "qualification.subject.shaderManifestCore");
1212
1291
  token2(candidate.shaderId, "qualification.subject.shaderManifestCore.shaderId");
1213
- token2(candidate.version, "qualification.subject.shaderManifestCore.version");
1292
+ immutableVersion2(candidate.version, "qualification.subject.shaderManifestCore.version");
1214
1293
  sha(candidate.sha256, "qualification.subject.shaderManifestCore.sha256");
1215
1294
  path(bundle.shaderManifestCorePath, "qualification.shaderManifestCorePath", [".json"]);
1216
1295
  const interfaceManifest = object4(bundle.gpuInterfaceManifest, "qualification.gpuInterfaceManifest");
@@ -1270,7 +1349,7 @@ function validateQualificationBundleManifest(value) {
1270
1349
  const expectedUnits = [...inventory.value.compileUnits.map((unit) => unit.compileUnitId)].sort();
1271
1350
  if (unitIds.join("\n") !== expectedUnits.join("\n")) throw new TypeError("Subject compile-unit IDs differ from inventory.");
1272
1351
  orderedUniqueTokens(subject.requiredCellIds, "qualification.subject.requiredCellIds");
1273
- return { ok: true, value: bundle };
1352
+ return { ok: true, value: freezeJson2(bundle) };
1274
1353
  } catch (cause) {
1275
1354
  return { ok: false, diagnostics: [{ code: "invalid-contract", severity: "error", message: cause instanceof Error ? cause.message : "Invalid qualification bundle." }] };
1276
1355
  }