@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.cjs CHANGED
@@ -112,6 +112,29 @@ function canonicalizeGpuContract(value) {
112
112
  return visit(value, "$root");
113
113
  }
114
114
 
115
+ // src/asset-version.ts
116
+ var ASSET_VERSION_PATTERN = /^[0-9A-Za-z][0-9A-Za-z._-]{0,127}$/u;
117
+ var MUTABLE_VERSION_ALIASES = /* @__PURE__ */ new Set([
118
+ "latest",
119
+ "current",
120
+ "stable",
121
+ "preview",
122
+ "default",
123
+ "production",
124
+ "canary",
125
+ "next",
126
+ "head",
127
+ "main"
128
+ ]);
129
+ var X_WILDCARD_SEGMENT_PATTERN = /^(?:[vV])?[xX](?:\.|-|$)|\.[xX](?:\.|-|$)/u;
130
+ 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.";
131
+ function assertImmutableAssetVersion(value) {
132
+ if (typeof value !== "string" || !ASSET_VERSION_PATTERN.test(value) || MUTABLE_VERSION_ALIASES.has(value.toLowerCase()) || X_WILDCARD_SEGMENT_PATTERN.test(value)) {
133
+ throw new TypeError(IMMUTABLE_VERSION_ERROR_MESSAGE);
134
+ }
135
+ return value;
136
+ }
137
+
115
138
  // src/hash.ts
116
139
  var SHA256_PATTERN = /^[a-f0-9]{64}$/u;
117
140
  function asSha256Hex(value) {
@@ -134,6 +157,15 @@ async function computeSha256(value) {
134
157
 
135
158
  // src/manifest-validation.ts
136
159
  var stages = ["vertex", "fragment", "compute"];
160
+ function detachedJson(value, path2) {
161
+ try {
162
+ return JSON.parse(canonicalizeGpuContract(value));
163
+ } catch (cause) {
164
+ const detail = cause instanceof Error ? cause.message : "Contract snapshot failed.";
165
+ const boundedDetail = [...detail].slice(0, 512).join("");
166
+ throw new TypeError(`${path2} must contain detached JSON contract data: ${boundedDetail}`, { cause });
167
+ }
168
+ }
137
169
  function object(value, path2) {
138
170
  if (typeof value !== "object" || value === null || Array.isArray(value)) throw new TypeError(`${path2} must be an object.`);
139
171
  return value;
@@ -346,7 +378,7 @@ function pipeline(value, path2, moduleIds) {
346
378
  return input;
347
379
  }
348
380
  function parseSerializableGpuPipelineDescriptors(value, moduleIds, path2 = "pipelines") {
349
- const parsed = array(value, path2).map((item, index) => pipeline(item, `${path2}[${index}]`, new Set(moduleIds)));
381
+ const parsed = array(detachedJson(value, path2), path2).map((item, index) => pipeline(item, `${path2}[${index}]`, new Set(moduleIds)));
350
382
  unique(parsed, (item) => item.pipelineId, path2);
351
383
  return freeze(parsed);
352
384
  }
@@ -364,6 +396,11 @@ function issue(code, message, path2) {
364
396
  function object2(value) {
365
397
  return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
366
398
  }
399
+ function freezeJson(value) {
400
+ if (typeof value !== "object" || value === null || Object.isFrozen(value)) return value;
401
+ for (const child of Object.values(value)) freezeJson(child);
402
+ return Object.freeze(value);
403
+ }
367
404
  function exactKeys(value, keys2) {
368
405
  const expected = new Set(keys2);
369
406
  return Object.keys(value).every((key) => expected.has(key)) && keys2.every((key) => key in value);
@@ -371,6 +408,14 @@ function exactKeys(value, keys2) {
371
408
  function safeToken(value) {
372
409
  return typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,159}$/u.test(value) && !value.includes("..");
373
410
  }
411
+ function immutableVersion(value) {
412
+ try {
413
+ assertImmutableAssetVersion(value);
414
+ return true;
415
+ } catch {
416
+ return false;
417
+ }
418
+ }
374
419
  function validSha(value) {
375
420
  try {
376
421
  asSha256Hex(String(value));
@@ -383,8 +428,20 @@ function defineShaderCompileUnit(unit) {
383
428
  return Object.freeze(unit);
384
429
  }
385
430
  function validateCompileUnitInventory(value) {
386
- const envelope = object2(value);
387
- const candidate = envelope && "inventory" in envelope ? object2(value.inventory) : envelope;
431
+ let snapshot;
432
+ try {
433
+ snapshot = JSON.parse(canonicalizeGpuContract(value));
434
+ } catch (cause) {
435
+ return {
436
+ ok: false,
437
+ diagnostics: [issue(
438
+ "invalid-contract",
439
+ cause instanceof Error ? cause.message : "Compile-unit inventory must contain detached JSON data."
440
+ )]
441
+ };
442
+ }
443
+ const envelope = object2(snapshot);
444
+ const candidate = envelope && Object.hasOwn(envelope, "inventory") ? object2(snapshot.inventory) : envelope;
388
445
  const diagnostics = [];
389
446
  if (!candidate || !exactKeys(candidate, ["contractVersion", "fragments", "compileUnits"])) {
390
447
  return { ok: false, diagnostics: [issue("invalid-contract", "Compile-unit inventory has unknown or missing fields.")] };
@@ -515,7 +572,7 @@ function validateCompileUnitInventory(value) {
515
572
  diagnostics.push(issue("invalid-contract", cause instanceof Error ? cause.message : "Compile-unit pipelines are invalid.", `${path2}.pipelines`));
516
573
  }
517
574
  const interfaceRef = object2(unit.interfaceRef);
518
- 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)) {
575
+ 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)) {
519
576
  diagnostics.push(issue("invalid-contract", "Compile-unit interfaceRef is invalid.", `${path2}.interfaceRef`));
520
577
  }
521
578
  const fixture = object2(unit.qualificationFixture);
@@ -526,7 +583,7 @@ function validateCompileUnitInventory(value) {
526
583
  for (const fragmentId of fragmentIds) {
527
584
  if (!covered.has(fragmentId)) diagnostics.push(issue("uncovered-fragment", `WGSL fragment ${fragmentId} has no compile unit.`));
528
585
  }
529
- return diagnostics.length > 0 ? { ok: false, diagnostics } : { ok: true, value: candidate };
586
+ return diagnostics.length > 0 ? { ok: false, diagnostics } : { ok: true, value: freezeJson(candidate) };
530
587
  }
531
588
 
532
589
  // src/testing/matrix.ts
@@ -753,6 +810,18 @@ function object4(value, path2) {
753
810
  if (typeof value !== "object" || value === null || Array.isArray(value)) throw new TypeError(`${path2} must be an object.`);
754
811
  return value;
755
812
  }
813
+ function detachedJson2(value, label) {
814
+ try {
815
+ return JSON.parse(canonicalizeGpuContract(value));
816
+ } catch (cause) {
817
+ throw new TypeError(`${label} must contain detached JSON contract data.`, { cause });
818
+ }
819
+ }
820
+ function freezeJson2(value) {
821
+ if (typeof value !== "object" || value === null || Object.isFrozen(value)) return value;
822
+ for (const child of Object.values(value)) freezeJson2(child);
823
+ return Object.freeze(value);
824
+ }
756
825
  function exact2(value, keys2, path2) {
757
826
  const expected = new Set(keys2);
758
827
  if (!Object.keys(value).every((key) => expected.has(key)) || !keys2.every((key) => key in value)) throw new TypeError(`${path2} has unknown or missing fields.`);
@@ -761,6 +830,16 @@ function token2(value, path2) {
761
830
  if (typeof value !== "string" || !TOKEN.test(value) || value.includes("..")) throw new TypeError(`${path2} must be a safe token.`);
762
831
  return value;
763
832
  }
833
+ function immutableVersion2(value, path2) {
834
+ try {
835
+ return assertImmutableAssetVersion(value);
836
+ } catch (cause) {
837
+ throw new TypeError(
838
+ `${path2} must be an immutable asset version: exact token required; mutable aliases, ranges, wildcards, and URLs are not allowed.`,
839
+ { cause }
840
+ );
841
+ }
842
+ }
764
843
  function path(value, label, extensions) {
765
844
  if (typeof value !== "string" || !SAFE_PATH2.test(value) || !extensions.some((extension) => value.endsWith(extension))) {
766
845
  throw new TypeError(`${label} must be a safe data-only bundle path (${extensions.join(", ")}).`);
@@ -895,7 +974,7 @@ function jsonValue(value, label, depth = 0) {
895
974
  }
896
975
  function validateQualificationFixture(value, unit) {
897
976
  try {
898
- const fixture = object4(value, "fixture");
977
+ const fixture = object4(detachedJson2(value, "fixture"), "fixture");
899
978
  exact2(fixture, ["contractVersion", "fixtureId", "resources", "bindGroups", "commands", "layoutProbes", "readbacks", "bounds"], "fixture");
900
979
  if (fixture.contractVersion !== SHADER_QUALIFICATION_FIXTURE_VERSION) throw new TypeError("Unsupported qualification fixture version.");
901
980
  token2(fixture.fixtureId, "fixture.fixtureId");
@@ -1231,14 +1310,14 @@ function validateQualificationFixture(value, unit) {
1231
1310
  }
1232
1311
  unique2(probeIds, "fixture.layoutProbes probe IDs");
1233
1312
  unique2(probeSources, "fixture.layoutProbes model sources");
1234
- return { ok: true, value: fixture };
1313
+ return { ok: true, value: freezeJson2(fixture) };
1235
1314
  } catch (cause) {
1236
1315
  return { ok: false, diagnostics: [{ code: "invalid-contract", severity: "error", message: cause instanceof Error ? cause.message : "Invalid qualification fixture." }] };
1237
1316
  }
1238
1317
  }
1239
1318
  function validateQualificationBundleManifest(value) {
1240
1319
  try {
1241
- const bundle = object4(value, "qualification");
1320
+ const bundle = object4(detachedJson2(value, "qualification"), "qualification");
1242
1321
  exact2(bundle, ["contractVersion", "inventory", "subject", "shaderManifestCorePath", "gpuInterfaceManifest", "modelCompatibilityFixtures", "modules", "fixtures"], "qualification");
1243
1322
  if (bundle.contractVersion !== SHADER_QUALIFICATION_BUNDLE_VERSION) throw new TypeError("Unsupported qualification bundle version.");
1244
1323
  const inventory = validateCompileUnitInventory(bundle.inventory);
@@ -1248,7 +1327,7 @@ function validateQualificationBundleManifest(value) {
1248
1327
  const candidate = object4(subject.shaderManifestCore, "qualification.subject.shaderManifestCore");
1249
1328
  exact2(candidate, ["shaderId", "version", "sha256"], "qualification.subject.shaderManifestCore");
1250
1329
  token2(candidate.shaderId, "qualification.subject.shaderManifestCore.shaderId");
1251
- token2(candidate.version, "qualification.subject.shaderManifestCore.version");
1330
+ immutableVersion2(candidate.version, "qualification.subject.shaderManifestCore.version");
1252
1331
  sha(candidate.sha256, "qualification.subject.shaderManifestCore.sha256");
1253
1332
  path(bundle.shaderManifestCorePath, "qualification.shaderManifestCorePath", [".json"]);
1254
1333
  const interfaceManifest = object4(bundle.gpuInterfaceManifest, "qualification.gpuInterfaceManifest");
@@ -1308,7 +1387,7 @@ function validateQualificationBundleManifest(value) {
1308
1387
  const expectedUnits = [...inventory.value.compileUnits.map((unit) => unit.compileUnitId)].sort();
1309
1388
  if (unitIds.join("\n") !== expectedUnits.join("\n")) throw new TypeError("Subject compile-unit IDs differ from inventory.");
1310
1389
  orderedUniqueTokens(subject.requiredCellIds, "qualification.subject.requiredCellIds");
1311
- return { ok: true, value: bundle };
1390
+ return { ok: true, value: freezeJson2(bundle) };
1312
1391
  } catch (cause) {
1313
1392
  return { ok: false, diagnostics: [{ code: "invalid-contract", severity: "error", message: cause instanceof Error ? cause.message : "Invalid qualification bundle." }] };
1314
1393
  }