@forgeax/engine-assets-runtime 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.
Files changed (71) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +213 -0
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/__tests__/asset-graph-red.integration.test.d.ts +2 -0
  5. package/dist/__tests__/asset-graph-red.integration.test.d.ts.map +1 -0
  6. package/dist/__tests__/asset-kind.test.d.ts +2 -0
  7. package/dist/__tests__/asset-kind.test.d.ts.map +1 -0
  8. package/dist/__tests__/asset-registry-core.integration.test.d.ts +2 -0
  9. package/dist/__tests__/asset-registry-core.integration.test.d.ts.map +1 -0
  10. package/dist/__tests__/asset-registry-public-api.test-d.d.ts +2 -0
  11. package/dist/__tests__/asset-registry-public-api.test-d.d.ts.map +1 -0
  12. package/dist/__tests__/asset-runtime-core-lifecycle.integration.test.d.ts +2 -0
  13. package/dist/__tests__/asset-runtime-core-lifecycle.integration.test.d.ts.map +1 -0
  14. package/dist/__tests__/asset-runtime-snapshot.unit.test.d.ts +2 -0
  15. package/dist/__tests__/asset-runtime-snapshot.unit.test.d.ts.map +1 -0
  16. package/dist/__tests__/catalog-session-red.unit.test.d.ts +2 -0
  17. package/dist/__tests__/catalog-session-red.unit.test.d.ts.map +1 -0
  18. package/dist/__tests__/decode-image-mime-owner.test.d.ts +2 -0
  19. package/dist/__tests__/decode-image-mime-owner.test.d.ts.map +1 -0
  20. package/dist/__tests__/registry-lifecycle-red.integration.test.d.ts +2 -0
  21. package/dist/__tests__/registry-lifecycle-red.integration.test.d.ts.map +1 -0
  22. package/dist/asset-kind.d.ts +4 -0
  23. package/dist/asset-kind.d.ts.map +1 -0
  24. package/dist/catalog-source.d.ts +23 -0
  25. package/dist/catalog-source.d.ts.map +1 -0
  26. package/dist/index.d.ts +5 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.mjs +1240 -0
  29. package/dist/index.mjs.map +1 -0
  30. package/dist/internal/artifact-cache.d.ts +17 -0
  31. package/dist/internal/artifact-cache.d.ts.map +1 -0
  32. package/dist/internal/asset-graph.d.ts +70 -0
  33. package/dist/internal/asset-graph.d.ts.map +1 -0
  34. package/dist/internal/catalog-session.d.ts +60 -0
  35. package/dist/internal/catalog-session.d.ts.map +1 -0
  36. package/dist/internal/decoder-registry.d.ts +19 -0
  37. package/dist/internal/decoder-registry.d.ts.map +1 -0
  38. package/dist/internal/immutable-payload.d.ts +9 -0
  39. package/dist/internal/immutable-payload.d.ts.map +1 -0
  40. package/dist/internal/load-asset.d.ts +31 -0
  41. package/dist/internal/load-asset.d.ts.map +1 -0
  42. package/dist/internal/pack-reader.d.ts +18 -0
  43. package/dist/internal/pack-reader.d.ts.map +1 -0
  44. package/dist/internal/validate-runtime-row.d.ts +7 -0
  45. package/dist/internal/validate-runtime-row.d.ts.map +1 -0
  46. package/dist/internal.d.ts +2 -0
  47. package/dist/internal.d.ts.map +1 -0
  48. package/dist/internal.mjs +15 -0
  49. package/dist/internal.mjs.map +1 -0
  50. package/package.json +63 -0
  51. package/src/__tests__/asset-graph-red.integration.test.ts +113 -0
  52. package/src/__tests__/asset-kind.test.ts +14 -0
  53. package/src/__tests__/asset-registry-core.integration.test.ts +161 -0
  54. package/src/__tests__/asset-registry-public-api.test-d.ts +31 -0
  55. package/src/__tests__/asset-runtime-core-lifecycle.integration.test.ts +79 -0
  56. package/src/__tests__/asset-runtime-snapshot.unit.test.ts +23 -0
  57. package/src/__tests__/catalog-session-red.unit.test.ts +276 -0
  58. package/src/__tests__/decode-image-mime-owner.test.ts +41 -0
  59. package/src/__tests__/registry-lifecycle-red.integration.test.ts +80 -0
  60. package/src/asset-kind.ts +6 -0
  61. package/src/catalog-source.ts +145 -0
  62. package/src/index.ts +22 -0
  63. package/src/internal/artifact-cache.ts +65 -0
  64. package/src/internal/asset-graph.ts +420 -0
  65. package/src/internal/catalog-session.ts +345 -0
  66. package/src/internal/decoder-registry.ts +175 -0
  67. package/src/internal/immutable-payload.ts +20 -0
  68. package/src/internal/load-asset.ts +254 -0
  69. package/src/internal/pack-reader.ts +183 -0
  70. package/src/internal/validate-runtime-row.ts +51 -0
  71. package/src/internal.ts +4 -0
@@ -0,0 +1,345 @@
1
+ import type {
2
+ CatalogDelta,
3
+ CatalogDiagnostic,
4
+ CatalogEntry,
5
+ ResourceRevision,
6
+ } from '@forgeax/engine-types';
7
+ import { type AssetLoadError, err, ok, type Result } from '@forgeax/engine-types';
8
+ import type { CatalogSource } from '../catalog-source.js';
9
+ import { type RuntimeCatalogRow, validateRuntimeRow } from './validate-runtime-row.js';
10
+
11
+ export interface CatalogSessionOptions {
12
+ readonly scopeId?: string;
13
+ readonly generation?: number;
14
+ }
15
+
16
+ export interface CatalogSessionSnapshot {
17
+ readonly scopeId: string;
18
+ readonly generation: number;
19
+ readonly epoch: number;
20
+ readonly revision?: ResourceRevision;
21
+ readonly entries: readonly RuntimeCatalogRow[];
22
+ readonly changed: readonly string[];
23
+ readonly removed: readonly string[];
24
+ readonly diagnostics: readonly CatalogDiagnostic[];
25
+ readonly listenerFailures: number;
26
+ readonly stale: boolean;
27
+ }
28
+
29
+ type SessionResult = Result<CatalogSessionSnapshot, AssetLoadError>;
30
+ type SessionListener = (snapshot: CatalogSessionSnapshot) => void;
31
+
32
+ function runtimeError(
33
+ code: AssetLoadError['code'],
34
+ guid: string,
35
+ detail: Record<string, unknown>,
36
+ ): AssetLoadError {
37
+ if (code === 'catalog-discontinuous') {
38
+ return {
39
+ code,
40
+ expected: 'an ordered catalog revision window',
41
+ hint: 'reconcile the current Catalog before consuming this delta',
42
+ detail: {
43
+ scopeId: String(detail.scopeId ?? 'unknown'),
44
+ expectedGeneration: Number(detail.expectedGeneration ?? 0),
45
+ actualGeneration: Number(detail.actualGeneration ?? 0),
46
+ },
47
+ };
48
+ }
49
+ return {
50
+ code: 'asset-package-invalid',
51
+ expected: 'a verified Catalog source',
52
+ hint: 'repair the producer Catalog and retry with the current publication',
53
+ detail: { guid, reason: String(detail.reason ?? 'catalog source failed') },
54
+ };
55
+ }
56
+
57
+ function freezeSnapshot(snapshot: CatalogSessionSnapshot): CatalogSessionSnapshot {
58
+ return Object.freeze({
59
+ ...snapshot,
60
+ entries: Object.freeze([...snapshot.entries]),
61
+ changed: Object.freeze([...snapshot.changed]),
62
+ removed: Object.freeze([...snapshot.removed]),
63
+ diagnostics: Object.freeze([...snapshot.diagnostics]),
64
+ });
65
+ }
66
+
67
+ function sameEntry(left: CatalogEntry, right: CatalogEntry): boolean {
68
+ return JSON.stringify(left) === JSON.stringify(right);
69
+ }
70
+
71
+ function key(guid: string): string {
72
+ return guid.toLowerCase();
73
+ }
74
+
75
+ export class CatalogSession {
76
+ private readonly source: CatalogSource;
77
+ private readonly scopeId: string;
78
+ private readonly generation: number;
79
+ private readonly entries = new Map<string, RuntimeCatalogRow>();
80
+ private readonly listeners = new Set<SessionListener>();
81
+ private unsubscribe: (() => void) | undefined;
82
+ private baselinePromise: Promise<SessionResult> | undefined;
83
+ private reconcilePromise: Promise<SessionResult> | undefined;
84
+ private pending: CatalogDelta[] = [];
85
+ private currentSnapshot: CatalogSessionSnapshot;
86
+ private revision: ResourceRevision | undefined;
87
+ private diagnostics: CatalogDiagnostic[] = [];
88
+ private listenerFailures = 0;
89
+ private changed = new Set<string>();
90
+ private removed = new Set<string>();
91
+ private epoch = 0;
92
+ private stale = false;
93
+ private staleGeneration = 0;
94
+ private started = false;
95
+ private disposed = false;
96
+
97
+ constructor(source: CatalogSource, options: CatalogSessionOptions = {}) {
98
+ this.source = source;
99
+ this.scopeId = options.scopeId ?? source.expectedScope?.scopeId ?? 'asset-runtime';
100
+ this.generation = options.generation ?? source.expectedScope?.generation ?? 0;
101
+ this.currentSnapshot = freezeSnapshot({
102
+ scopeId: this.scopeId,
103
+ generation: this.generation,
104
+ epoch: 0,
105
+ entries: [],
106
+ changed: [],
107
+ removed: [],
108
+ diagnostics: [],
109
+ listenerFailures: 0,
110
+ stale: false,
111
+ });
112
+ }
113
+
114
+ start(): Promise<SessionResult> {
115
+ if (this.baselinePromise !== undefined) return this.baselinePromise;
116
+ if (this.disposed) return Promise.resolve(err(this.disposedError()));
117
+ this.unsubscribe = this.source.subscribe((delta) => this.receive(delta));
118
+ const promise = this.source
119
+ .enumerate()
120
+ .then((result) => {
121
+ if (!result.ok) {
122
+ this.markStale();
123
+ this.publish();
124
+ return err(runtimeError('asset-package-invalid', '', { reason: result.error.code }));
125
+ }
126
+ this.entries.clear();
127
+ for (const entry of result.value) {
128
+ const validated = validateRuntimeRow(entry);
129
+ if (!validated.ok) {
130
+ this.markStale();
131
+ this.publish();
132
+ return err(validated.error);
133
+ }
134
+ this.entries.set(key(validated.value.guid), validated.value);
135
+ }
136
+ this.started = true;
137
+ this.stale = false;
138
+ this.staleGeneration = this.generation;
139
+ this.diagnostics = [];
140
+ for (const delta of this.pending) this.fold(delta, false);
141
+ this.pending = [];
142
+ this.publish();
143
+ return ok(this.currentSnapshot);
144
+ })
145
+ .catch((cause: unknown) => {
146
+ this.markStale();
147
+ this.addDiagnostic('catalog-degraded-rows', 'Catalog enumeration must resolve a Result');
148
+ this.publish();
149
+ return err(
150
+ runtimeError('asset-package-invalid', '', {
151
+ reason: cause instanceof Error ? cause.message : String(cause),
152
+ }),
153
+ );
154
+ });
155
+ this.baselinePromise = promise;
156
+ void promise.then(
157
+ (result) => {
158
+ if (!result.ok) this.baselinePromise = undefined;
159
+ },
160
+ () => {
161
+ this.baselinePromise = undefined;
162
+ },
163
+ );
164
+ return promise;
165
+ }
166
+
167
+ reconcile(): Promise<SessionResult> {
168
+ if (this.disposed) return Promise.resolve(err(this.disposedError()));
169
+ if (this.reconcilePromise !== undefined) return this.reconcilePromise;
170
+ this.unsubscribe?.();
171
+ this.unsubscribe = undefined;
172
+ this.started = false;
173
+ this.pending = [];
174
+ this.baselinePromise = undefined;
175
+ this.epoch += 1;
176
+ const promise = this.start();
177
+ this.reconcilePromise = promise;
178
+ void promise.then(
179
+ () => {
180
+ if (this.reconcilePromise === promise) this.reconcilePromise = undefined;
181
+ },
182
+ () => {
183
+ if (this.reconcilePromise === promise) this.reconcilePromise = undefined;
184
+ },
185
+ );
186
+ return promise;
187
+ }
188
+
189
+ current(guid: string): RuntimeCatalogRow | undefined {
190
+ return this.entries.get(key(guid));
191
+ }
192
+
193
+ snapshot(): CatalogSessionSnapshot {
194
+ return this.currentSnapshot;
195
+ }
196
+
197
+ discontinuity(): AssetLoadError | undefined {
198
+ if (!this.stale) return undefined;
199
+ return {
200
+ code: 'catalog-discontinuous',
201
+ expected: 'an ordered, authoritative catalog revision window',
202
+ hint: 'reconcile the Catalog source and retry the current publication',
203
+ detail: {
204
+ scopeId: this.scopeId,
205
+ expectedGeneration: this.generation,
206
+ actualGeneration: this.staleGeneration,
207
+ },
208
+ };
209
+ }
210
+
211
+ subscribe(listener: SessionListener): () => void {
212
+ this.listeners.add(listener);
213
+ return () => this.listeners.delete(listener);
214
+ }
215
+
216
+ dispose(): void {
217
+ if (this.disposed) return;
218
+ this.disposed = true;
219
+ this.unsubscribe?.();
220
+ this.unsubscribe = undefined;
221
+ this.pending = [];
222
+ this.listeners.clear();
223
+ }
224
+
225
+ private receive(delta: CatalogDelta): void {
226
+ if (this.disposed) return;
227
+ if (!this.started) {
228
+ this.pending.push(delta);
229
+ return;
230
+ }
231
+ this.fold(delta, true);
232
+ }
233
+
234
+ private fold(delta: CatalogDelta, publish: boolean): void {
235
+ if (
236
+ (delta.scopeId !== undefined && delta.scopeId !== this.scopeId) ||
237
+ (delta.generation !== undefined && delta.generation !== this.generation)
238
+ ) {
239
+ this.markStale(delta.generation);
240
+ this.addDiagnostic('catalog-scope-mismatch', 'delta scope does not match the session');
241
+ if (publish) this.publish();
242
+ return;
243
+ }
244
+ if (delta.authority === 'degraded') {
245
+ this.markStale(delta.generation);
246
+ this.addDiagnostic('catalog-degraded-rows', 'degraded rows are not identity-bearing');
247
+ if (publish) this.publish();
248
+ return;
249
+ }
250
+ if (delta.revisions !== undefined) {
251
+ const baseline = delta.revisions.baseline;
252
+ const current = delta.revisions.current;
253
+ const valid =
254
+ baseline.length === current.length &&
255
+ current.every((point) => {
256
+ const prior = baseline.find((item) => item.rootId === point.rootId);
257
+ return prior !== undefined && point.revision === prior.revision + 1;
258
+ });
259
+ if (!valid) {
260
+ this.markStale(delta.generation);
261
+ this.addDiagnostic('catalog-gap', 'delta revision window is not contiguous');
262
+ if (publish) this.publish();
263
+ return;
264
+ }
265
+ }
266
+ let changed = false;
267
+ for (const entry of [...delta.added, ...delta.changed]) {
268
+ const validated = validateRuntimeRow(entry);
269
+ if (!validated.ok) {
270
+ this.markStale(delta.generation);
271
+ this.addDiagnostic('catalog-degraded-rows', 'delta contains an invalid runtime row');
272
+ if (publish) this.publish();
273
+ return;
274
+ }
275
+ const entryKey = key(validated.value.guid);
276
+ const prior = this.entries.get(entryKey);
277
+ if (prior === undefined || !sameEntry(prior, validated.value)) {
278
+ this.entries.set(entryKey, validated.value);
279
+ this.changed.add(entryKey);
280
+ changed = true;
281
+ if (validated.value.revision !== undefined) this.revision = validated.value.revision;
282
+ }
283
+ }
284
+ for (const guid of delta.removed) {
285
+ const entryKey = key(guid);
286
+ if (this.entries.delete(entryKey)) {
287
+ this.removed.add(entryKey);
288
+ changed = true;
289
+ }
290
+ }
291
+ if (changed) this.epoch += 1;
292
+ if (publish) this.publish();
293
+ }
294
+
295
+ private addDiagnostic(code: CatalogDiagnostic['code'], expected: string): void {
296
+ if (this.diagnostics.some((diagnostic) => diagnostic.code === code)) return;
297
+ this.diagnostics.push({
298
+ code,
299
+ severity: 'blocking',
300
+ expected,
301
+ hint: 'reconcile the Catalog before loading the affected publication',
302
+ authority: 'catalog',
303
+ });
304
+ }
305
+
306
+ private markStale(actualGeneration = this.generation): void {
307
+ this.stale = true;
308
+ this.staleGeneration = actualGeneration;
309
+ this.epoch += 1;
310
+ }
311
+
312
+ private publish(): void {
313
+ this.currentSnapshot = freezeSnapshot({
314
+ scopeId: this.scopeId,
315
+ generation: this.generation,
316
+ epoch: this.epoch,
317
+ ...(this.revision === undefined ? {} : { revision: this.revision }),
318
+ entries: [...this.entries.values()].sort((left, right) =>
319
+ key(left.guid).localeCompare(key(right.guid)),
320
+ ),
321
+ changed: [...this.changed].sort(),
322
+ removed: [...this.removed].sort(),
323
+ diagnostics: this.diagnostics,
324
+ listenerFailures: this.listenerFailures,
325
+ stale: this.stale,
326
+ });
327
+ this.changed.clear();
328
+ this.removed.clear();
329
+ for (const listener of [...this.listeners]) {
330
+ try {
331
+ listener(this.currentSnapshot);
332
+ } catch {
333
+ this.listenerFailures = Math.min(1024, this.listenerFailures + 1);
334
+ this.currentSnapshot = freezeSnapshot({
335
+ ...this.currentSnapshot,
336
+ listenerFailures: this.listenerFailures,
337
+ });
338
+ }
339
+ }
340
+ }
341
+
342
+ private disposedError(): AssetLoadError {
343
+ return runtimeError('asset-runtime-disposed', '', { scopeId: this.scopeId });
344
+ }
345
+ }
@@ -0,0 +1,175 @@
1
+ import type {
2
+ AssetDecoder,
3
+ AssetDecoderInput,
4
+ AssetDecoderLease,
5
+ AssetKind,
6
+ AssetLoadError,
7
+ Result,
8
+ } from '@forgeax/engine-types';
9
+ import { err, ok } from '@forgeax/engine-types';
10
+ import { freezeRuntimePayload } from './immutable-payload.js';
11
+
12
+ interface DecoderEntry {
13
+ readonly identity: symbol;
14
+ readonly decoder: AssetDecoder<unknown>;
15
+ readonly decode: (input: AssetDecoderInput<unknown>) => Promise<Result<unknown, AssetLoadError>>;
16
+ references: number;
17
+ }
18
+
19
+ export interface DecoderRegistryOptions {
20
+ readonly scopeId?: string;
21
+ }
22
+
23
+ export class DecoderRegistry {
24
+ private readonly dispatch = new Map<string, DecoderEntry>();
25
+ private readonly scopeId: string;
26
+ private disposed = false;
27
+
28
+ constructor(options: DecoderRegistryOptions = {}) {
29
+ this.scopeId = options.scopeId ?? 'asset-runtime';
30
+ }
31
+
32
+ install<P, K extends string>(kind: AssetKind<P, K>, decoder: AssetDecoder<P>): AssetDecoderLease {
33
+ if (this.disposed) throw new TypeError('asset runtime decoder registry is disposed');
34
+ const normalizedDecoder = decoder as AssetDecoder<unknown>;
35
+ const existing = this.dispatch.get(kind.kind);
36
+ if (existing !== undefined) {
37
+ if (existing.decoder !== normalizedDecoder) {
38
+ throw new TypeError(`duplicate decoder kind "${kind.kind}"`);
39
+ }
40
+ existing.references += 1;
41
+ return this.createLease(kind.kind, existing);
42
+ }
43
+ const identity = Symbol(kind.kind);
44
+ const entry: DecoderEntry = {
45
+ identity,
46
+ decoder: normalizedDecoder,
47
+ decode: (input) =>
48
+ decoder.decode(input as AssetDecoderInput<P>) as Promise<Result<unknown, AssetLoadError>>,
49
+ references: 1,
50
+ };
51
+ this.dispatch.set(kind.kind, entry);
52
+ return this.createLease(kind.kind, entry);
53
+ }
54
+
55
+ private createLease(kind: string, entry: DecoderEntry): AssetDecoderLease {
56
+ let released = false;
57
+ return {
58
+ kind,
59
+ dispose: () => {
60
+ if (released) return;
61
+ released = true;
62
+ entry.references -= 1;
63
+ if (entry.references === 0 && this.dispatch.get(kind)?.identity === entry.identity) {
64
+ this.dispatch.delete(kind);
65
+ }
66
+ },
67
+ };
68
+ }
69
+
70
+ has<P, K extends string>(kind: AssetKind<P, K>): boolean {
71
+ return this.dispatch.has(kind.kind);
72
+ }
73
+
74
+ load<P, K extends string>(
75
+ kind: AssetKind<P, K>,
76
+ input: AssetDecoderInput<P>,
77
+ ): Promise<Result<P, AssetLoadError>> {
78
+ return this.decode(kind, input);
79
+ }
80
+
81
+ loadByKind(
82
+ kind: string,
83
+ input: AssetDecoderInput<unknown>,
84
+ ): Promise<Result<unknown, AssetLoadError>> {
85
+ if (this.disposed) return Promise.resolve(err(disposedError(this.scopeId)));
86
+ const entry = this.dispatch.get(kind);
87
+ if (entry === undefined) {
88
+ return Promise.resolve(
89
+ err({
90
+ code: 'asset-decoder-missing',
91
+ expected: `an active decoder for kind "${kind}"`,
92
+ hint: 'install the owner decoder lease before loading this kind',
93
+ detail: { kind },
94
+ }),
95
+ );
96
+ }
97
+ return this.decodeEntry(kind, entry, input);
98
+ }
99
+
100
+ async decode<P, K extends string>(
101
+ kind: AssetKind<P, K>,
102
+ input: AssetDecoderInput<P>,
103
+ ): Promise<Result<P, AssetLoadError>> {
104
+ if (this.disposed) return err(disposedError(this.scopeId));
105
+ if (input.signal.aborted) return err(cancelledError(input.envelope.guid));
106
+ const entry = this.dispatch.get(kind.kind);
107
+ if (entry === undefined) {
108
+ return err({
109
+ code: 'asset-decoder-missing',
110
+ expected: `an active decoder for kind "${kind.kind}"`,
111
+ hint: 'install the owner decoder lease before loading this kind',
112
+ detail: { kind: kind.kind },
113
+ });
114
+ }
115
+ return (await this.decodeEntry(kind.kind, entry, input)) as Result<P, AssetLoadError>;
116
+ }
117
+
118
+ private async decodeEntry(
119
+ kind: string,
120
+ entry: DecoderEntry,
121
+ input: AssetDecoderInput<unknown>,
122
+ ): Promise<Result<unknown, AssetLoadError>> {
123
+ if (this.disposed) return err(disposedError(this.scopeId));
124
+ if (input.signal.aborted) return err(cancelledError(input.envelope.guid));
125
+ try {
126
+ const result = await entry.decode(input);
127
+ if (this.disposed) return err(disposedError(this.scopeId));
128
+ if (this.dispatch.get(kind)?.identity !== entry.identity) {
129
+ return err(supersededError(input.envelope.guid, kind));
130
+ }
131
+ if (input.signal.aborted) return err(cancelledError(input.envelope.guid));
132
+ return result.ok ? ok(freezeRuntimePayload(result.value)) : result;
133
+ } catch {
134
+ return err({
135
+ code: 'asset-decode-failed',
136
+ expected: `decoder for kind "${kind}" to return a Result`,
137
+ hint: 'inspect the owner decoder and retry the current publication',
138
+ detail: { guid: input.envelope.guid, kind },
139
+ });
140
+ }
141
+ }
142
+
143
+ dispose(): void {
144
+ if (this.disposed) return;
145
+ this.disposed = true;
146
+ this.dispatch.clear();
147
+ }
148
+ }
149
+
150
+ function cancelledError(guid: string): AssetLoadError {
151
+ return {
152
+ code: 'asset-load-cancelled',
153
+ expected: 'the request AbortSignal to remain live until decode completes',
154
+ hint: 'retry with a live AbortSignal when the request is still needed',
155
+ detail: { guid },
156
+ };
157
+ }
158
+
159
+ function disposedError(scopeId: string): AssetLoadError {
160
+ return {
161
+ code: 'asset-runtime-disposed',
162
+ expected: 'an active asset runtime decoder scope',
163
+ hint: 'obtain a new Registry from the current realm',
164
+ detail: { scopeId },
165
+ };
166
+ }
167
+
168
+ function supersededError(guid: string, kind: string): AssetLoadError {
169
+ return {
170
+ code: 'asset-superseded',
171
+ expected: `the decoder lease for kind "${kind}" to remain current until decode completes`,
172
+ hint: 'retry the current publication after reinstalling its owner decoder',
173
+ detail: { guid, generation: 0 },
174
+ };
175
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Decoder output becomes an owner fact at this boundary. Freeze the ordinary
3
+ * POD graph so a caller cannot mutate the cached payload between loads.
4
+ * Typed-array storage is deliberately left as an opaque byte/vector carrier:
5
+ * JavaScript cannot freeze a non-empty typed array without changing its public
6
+ * engine type, so owners must treat those carriers as read-only by contract.
7
+ */
8
+ export function freezeRuntimePayload<T>(value: T): T {
9
+ const seen = new WeakSet<object>();
10
+ return freeze(value, seen);
11
+ }
12
+
13
+ function freeze<T>(value: T, seen: WeakSet<object>): T {
14
+ if (value === null || typeof value !== 'object') return value;
15
+ if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) return value;
16
+ if (seen.has(value)) return value;
17
+ seen.add(value);
18
+ for (const child of Object.values(value as Record<string, unknown>)) freeze(child, seen);
19
+ return Object.freeze(value);
20
+ }