@noy-db/hub 0.2.0-pre.30 → 0.2.0-pre.31

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 (143) hide show
  1. package/dist/adapter/index.d.ts +9 -0
  2. package/dist/adapter/index.js +14 -0
  3. package/dist/attestation/index.d.ts +1 -1
  4. package/dist/attestation/index.js +3 -3
  5. package/dist/blobs/index.d.ts +3 -3
  6. package/dist/bundle/index.d.ts +3 -3
  7. package/dist/bundle/index.js +1 -1
  8. package/dist/{chunk-Y6YUWZTS.js → chunk-L5HHBOMS.js} +2 -2
  9. package/dist/{chunk-BVKLJBJJ.js → chunk-W5NVNJDX.js} +2 -1
  10. package/dist/{chunk-BVKLJBJJ.js.map → chunk-W5NVNJDX.js.map} +1 -1
  11. package/dist/consent/index.d.ts +2 -2
  12. package/dist/{decrypt-partition-Mhjh6nnG.d.ts → decrypt-partition-CXS5KopB.d.ts} +1 -1
  13. package/dist/derivations/index.d.ts +3 -3
  14. package/dist/{dev-unlock-DJ3IhgY9.d.ts → dev-unlock-CnHTTVea.d.ts} +1 -1
  15. package/dist/guards/index.d.ts +3 -3
  16. package/dist/{hash-DLwkYf1d.d.ts → hash-oc5paYl0.d.ts} +1 -1
  17. package/dist/history/index.d.ts +3 -3
  18. package/dist/i18n/index.d.ts +2 -2
  19. package/dist/{index-CMRIx_zx.d.ts → index-BmhGztUi.d.ts} +1 -1
  20. package/dist/index.d.ts +11 -11
  21. package/dist/index.js +2 -2
  22. package/dist/kernel/index.d.ts +2 -2
  23. package/dist/materialized-views/index.d.ts +3 -3
  24. package/dist/{mime-magic-CacDBwYp.d.ts → mime-magic-JeLKHRng.d.ts} +1 -1
  25. package/dist/{noydb-5MIBD77B.js → noydb-NAU2DTFP.js} +2 -2
  26. package/dist/noydb-NAU2DTFP.js.map +1 -0
  27. package/dist/overlay-views/index.d.ts +3 -3
  28. package/dist/periods/index.d.ts +2 -2
  29. package/dist/session/index.d.ts +3 -3
  30. package/dist/shadow/index.d.ts +2 -2
  31. package/dist/snapshots/index.d.ts +2 -2
  32. package/dist/store/index.d.ts +2 -2
  33. package/dist/sync/index.d.ts +1 -1
  34. package/dist/team/index.d.ts +2 -2
  35. package/dist/{transition-guard-J04-jime.d.ts → transition-guard-Dy3NioQr.d.ts} +1 -1
  36. package/dist/tx/index.d.ts +2 -2
  37. package/dist/{with-materialized-view-DNfzC5lH.d.ts → with-materialized-view-DIVeez0W.d.ts} +1 -1
  38. package/dist/{with-overlayed-view-qk3lbhOd.d.ts → with-overlayed-view-DVZrc5Hb.d.ts} +1 -1
  39. package/dist/{with-rollup-Dd3_ZG4b.d.ts → with-rollup-CbU-O4wP.d.ts} +1 -1
  40. package/package.json +62 -221
  41. package/dist/aggregate/index.cjs +0 -1144
  42. package/dist/aggregate/index.cjs.map +0 -1
  43. package/dist/aggregate/index.d.cts +0 -39
  44. package/dist/attestation/index.cjs +0 -305
  45. package/dist/attestation/index.cjs.map +0 -1
  46. package/dist/attestation/index.d.cts +0 -54
  47. package/dist/blobs/index.cjs +0 -1967
  48. package/dist/blobs/index.cjs.map +0 -1
  49. package/dist/blobs/index.d.cts +0 -48
  50. package/dist/bundle/index.cjs +0 -41045
  51. package/dist/bundle/index.cjs.map +0 -1
  52. package/dist/bundle/index.d.cts +0 -187
  53. package/dist/consent/index.cjs +0 -204
  54. package/dist/consent/index.cjs.map +0 -1
  55. package/dist/consent/index.d.cts +0 -27
  56. package/dist/crdt/index.cjs +0 -152
  57. package/dist/crdt/index.cjs.map +0 -1
  58. package/dist/crdt/index.d.cts +0 -30
  59. package/dist/decrypt-partition-C0iDpbSd.d.cts +0 -558
  60. package/dist/derivations/index.cjs +0 -469
  61. package/dist/derivations/index.cjs.map +0 -1
  62. package/dist/derivations/index.d.cts +0 -74
  63. package/dist/dev-unlock-0Z6yxfS7.d.cts +0 -263
  64. package/dist/discriminant-BN9REW3o.d.cts +0 -60
  65. package/dist/errors-BAWO5Z5a.d.cts +0 -1500
  66. package/dist/forget/index.cjs +0 -43
  67. package/dist/forget/index.cjs.map +0 -1
  68. package/dist/forget/index.d.cts +0 -1
  69. package/dist/guards/index.cjs +0 -455
  70. package/dist/guards/index.cjs.map +0 -1
  71. package/dist/guards/index.d.cts +0 -39
  72. package/dist/hash-Db8ncXGr.d.cts +0 -63
  73. package/dist/history/index.cjs +0 -1245
  74. package/dist/history/index.cjs.map +0 -1
  75. package/dist/history/index.d.cts +0 -65
  76. package/dist/i18n/index.cjs +0 -1280
  77. package/dist/i18n/index.cjs.map +0 -1
  78. package/dist/i18n/index.d.cts +0 -41
  79. package/dist/index-BMmajblo.d.cts +0 -362
  80. package/dist/index-C1SC1EPe.d.cts +0 -1325
  81. package/dist/index-xJEPkvMb.d.cts +0 -93
  82. package/dist/index.cjs +0 -48100
  83. package/dist/index.cjs.map +0 -1
  84. package/dist/index.d.cts +0 -1050
  85. package/dist/indexing/index.cjs +0 -803
  86. package/dist/indexing/index.cjs.map +0 -1
  87. package/dist/indexing/index.d.cts +0 -36
  88. package/dist/kernel/index.cjs +0 -751
  89. package/dist/kernel/index.cjs.map +0 -1
  90. package/dist/kernel/index.d.cts +0 -11
  91. package/dist/lazy-builder-eYZzLEL1.d.cts +0 -304
  92. package/dist/materialized-views/index.cjs +0 -1518
  93. package/dist/materialized-views/index.cjs.map +0 -1
  94. package/dist/materialized-views/index.d.cts +0 -187
  95. package/dist/mime-magic-sdMzsfVK.d.cts +0 -103
  96. package/dist/overlay-views/index.cjs +0 -399
  97. package/dist/overlay-views/index.cjs.map +0 -1
  98. package/dist/overlay-views/index.d.cts +0 -102
  99. package/dist/periods/index.cjs +0 -1041
  100. package/dist/periods/index.cjs.map +0 -1
  101. package/dist/periods/index.d.cts +0 -24
  102. package/dist/predicate-BmhBSPCH.d.cts +0 -267
  103. package/dist/query/index.cjs +0 -3480
  104. package/dist/query/index.cjs.map +0 -1
  105. package/dist/query/index.d.cts +0 -4
  106. package/dist/sealed-record/index.cjs +0 -139
  107. package/dist/sealed-record/index.cjs.map +0 -1
  108. package/dist/sealed-record/index.d.cts +0 -123
  109. package/dist/session/index.cjs +0 -495
  110. package/dist/session/index.cjs.map +0 -1
  111. package/dist/session/index.d.cts +0 -48
  112. package/dist/shadow/index.cjs +0 -133
  113. package/dist/shadow/index.cjs.map +0 -1
  114. package/dist/shadow/index.d.cts +0 -19
  115. package/dist/snapshots/index.cjs +0 -937
  116. package/dist/snapshots/index.cjs.map +0 -1
  117. package/dist/snapshots/index.d.cts +0 -30
  118. package/dist/store/index.cjs +0 -1091
  119. package/dist/store/index.cjs.map +0 -1
  120. package/dist/store/index.d.cts +0 -501
  121. package/dist/strategy-BSxFXGzb.d.cts +0 -110
  122. package/dist/strategy-mJExw4LQ.d.cts +0 -1069
  123. package/dist/sync/index.cjs +0 -1062
  124. package/dist/sync/index.cjs.map +0 -1
  125. package/dist/sync/index.d.cts +0 -45
  126. package/dist/team/index.cjs +0 -2805
  127. package/dist/team/index.cjs.map +0 -1
  128. package/dist/team/index.d.cts +0 -120
  129. package/dist/transition-guard-tWZIsaXe.d.cts +0 -165
  130. package/dist/tx/index.cjs +0 -614
  131. package/dist/tx/index.cjs.map +0 -1
  132. package/dist/tx/index.d.cts +0 -39
  133. package/dist/types-CdVfiQgt.d.cts +0 -15275
  134. package/dist/ulid-DRH25k3y.d.cts +0 -66
  135. package/dist/util/index.cjs +0 -237
  136. package/dist/util/index.cjs.map +0 -1
  137. package/dist/util/index.d.cts +0 -79
  138. package/dist/with-materialized-view-DfgPcvi9.d.cts +0 -27
  139. package/dist/with-overlayed-view-Cvfkx1lg.d.cts +0 -13
  140. package/dist/with-rollup-nIxo7h9z.d.cts +0 -47
  141. /package/dist/{noydb-5MIBD77B.js.map → adapter/index.js.map} +0 -0
  142. /package/dist/{chunk-Y6YUWZTS.js.map → chunk-L5HHBOMS.js.map} +0 -0
  143. /package/dist/{types-DHn9lEP0.d.ts → index-DHn9lEP0.d.ts} +0 -0
@@ -1,469 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/derivations/index.ts
21
- var derivations_exports = {};
22
- __export(derivations_exports, {
23
- DerivationCapExceededError: () => DerivationCapExceededError,
24
- DerivationCycleError: () => DerivationCycleError,
25
- DerivationDepthError: () => DerivationDepthError,
26
- DerivationExecutor: () => DerivationExecutor,
27
- DerivationOutputShapeError: () => DerivationOutputShapeError,
28
- DerivationOutputUnknownError: () => DerivationOutputUnknownError,
29
- DerivationRegistry: () => DerivationRegistry,
30
- withDerivation: () => withDerivation,
31
- withRollup: () => withRollup
32
- });
33
- module.exports = __toCommonJS(derivations_exports);
34
-
35
- // src/errors.ts
36
- var NoydbError = class extends Error {
37
- /** Machine-readable error code. Stable across library versions. */
38
- code;
39
- constructor(code, message) {
40
- super(message);
41
- this.name = "NoydbError";
42
- this.code = code;
43
- }
44
- };
45
- var ValidationError = class extends NoydbError {
46
- constructor(message = "Validation error") {
47
- super("VALIDATION_ERROR", message);
48
- this.name = "ValidationError";
49
- }
50
- };
51
- var DerivationCycleError = class extends NoydbError {
52
- path;
53
- constructor(path) {
54
- super(
55
- "DERIVATION_CYCLE",
56
- `Derivation graph contains a cycle: ${path.join(" \u2192 ")}. Refusing to open vault \u2014 break the cycle before retrying.`
57
- );
58
- this.name = "DerivationCycleError";
59
- this.path = path;
60
- }
61
- };
62
- var DerivationDepthError = class extends NoydbError {
63
- limit;
64
- attempted;
65
- constructor(limit, attempted) {
66
- super(
67
- "DERIVATION_DEPTH",
68
- `Derivation cascade exceeded max depth ${limit} (attempted ${attempted}). Pass lifecycle: { maxDepth: N } to raise the limit if intentional.`
69
- );
70
- this.name = "DerivationDepthError";
71
- this.limit = limit;
72
- this.attempted = attempted;
73
- }
74
- };
75
- var DerivationOutputUnknownError = class extends NoydbError {
76
- collection;
77
- constructor(collection) {
78
- super(
79
- "DERIVATION_OUTPUT_UNKNOWN",
80
- `Derivation output collection "${collection}" is not declared on the vault. Register the collection (e.g. via schema) before registering a derivation that writes to it.`
81
- );
82
- this.name = "DerivationOutputUnknownError";
83
- this.collection = collection;
84
- }
85
- };
86
- var DerivationOutputShapeError = class extends NoydbError {
87
- outputKey;
88
- constructor(outputKey, detail) {
89
- super(
90
- "DERIVATION_OUTPUT_SHAPE",
91
- `Derivation output "${outputKey}" has invalid shape: ${detail}.`
92
- );
93
- this.name = "DerivationOutputShapeError";
94
- this.outputKey = outputKey;
95
- }
96
- };
97
- var DerivationCapExceededError = class extends NoydbError {
98
- outputKey;
99
- returned;
100
- maxFanout;
101
- constructor(outputKey, returned, maxFanout) {
102
- super(
103
- "DERIVATION_CAP_EXCEEDED",
104
- `Derivation array output "${outputKey}" returned ${returned} rows, exceeding maxFanout=${maxFanout}. Raise \`maxFanout\` on the OutputSpec if this fanout is intended (the cap exists to keep dispatch cost bounded).`
105
- );
106
- this.name = "DerivationCapExceededError";
107
- this.outputKey = outputKey;
108
- this.returned = returned;
109
- this.maxFanout = maxFanout;
110
- }
111
- };
112
-
113
- // src/derivations/with-derivation.ts
114
- function withDerivation(spec) {
115
- if (!spec.source || spec.source.length === 0) {
116
- throw new ValidationError("withDerivation: source collection name is required");
117
- }
118
- if (!spec.outputs || Object.keys(spec.outputs).length === 0) {
119
- throw new ValidationError("withDerivation: outputs map must declare at least one output");
120
- }
121
- if (spec.deterministic !== true) {
122
- throw new ValidationError("withDerivation: v1 only supports deterministic derivations");
123
- }
124
- if (typeof spec.derive !== "function") {
125
- throw new ValidationError("withDerivation: derive must be a function");
126
- }
127
- if (spec.sources !== void 0) {
128
- for (const extra of spec.sources) {
129
- if (typeof extra !== "string" || extra.length === 0) {
130
- throw new ValidationError("withDerivation: each entry in sources[] must be a non-empty string");
131
- }
132
- if (extra === spec.source) {
133
- throw new ValidationError(
134
- `withDerivation: sources[] must not contain the primary source "${spec.source}"`
135
- );
136
- }
137
- }
138
- }
139
- if (spec.triggerBy !== void 0) {
140
- for (const t of spec.triggerBy) {
141
- if (typeof t?.collection !== "string" || t.collection.length === 0) {
142
- throw new ValidationError("withDerivation: each triggerBy entry needs a non-empty `collection`");
143
- }
144
- if (t.collection === spec.source) {
145
- throw new ValidationError(
146
- `withDerivation: triggerBy.collection must not equal the source "${spec.source}" (use sources[] for same-id triggers)`
147
- );
148
- }
149
- if (typeof t.on !== "string" || t.on.length === 0) {
150
- throw new ValidationError(
151
- `withDerivation: triggerBy on "${t.collection}" needs a non-empty \`on\` (the FK field on the source)`
152
- );
153
- }
154
- if (t.maxFanout !== void 0 && (!Number.isInteger(t.maxFanout) || t.maxFanout < 1)) {
155
- throw new ValidationError(
156
- `withDerivation: triggerBy maxFanout on "${t.collection}" must be a positive integer (got ${String(t.maxFanout)}).`
157
- );
158
- }
159
- }
160
- }
161
- const lifecycleMode = typeof spec.lifecycle === "string" ? spec.lifecycle : spec.lifecycle.mode;
162
- for (const [outputKey, outputSpec] of Object.entries(spec.outputs)) {
163
- if (outputSpec.shape === "record" && outputSpec.collection === spec.source) {
164
- if (!outputSpec.denorm || outputSpec.denorm.length === 0) {
165
- throw new ValidationError(
166
- `withDerivation: self-write output "${outputKey}" (collection === source "${spec.source}") must declare \`denorm: [...]\` naming the fields it maintains.`
167
- );
168
- }
169
- }
170
- if (outputSpec.shape === "array") {
171
- if (lifecycleMode !== "eager") {
172
- throw new ValidationError(
173
- `withDerivation: shape 'array' supports lifecycle 'eager' only in this release Output "${outputKey}" declared lifecycle '${lifecycleMode}'. Switch to \`lifecycle: "eager"\` or use shape: "record".`
174
- );
175
- }
176
- if (typeof outputSpec.key !== "function") {
177
- throw new ValidationError(
178
- `withDerivation: shape 'array' output "${outputKey}" requires \`key: (out) => string\`.`
179
- );
180
- }
181
- if (outputSpec.maxFanout !== void 0) {
182
- if (!Number.isInteger(outputSpec.maxFanout) || outputSpec.maxFanout < 1) {
183
- throw new ValidationError(
184
- `withDerivation: maxFanout for output "${outputKey}" must be a positive integer (got ${String(outputSpec.maxFanout)}).`
185
- );
186
- }
187
- }
188
- }
189
- }
190
- return {
191
- __noydb_strategy: "derivation",
192
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
193
- spec
194
- };
195
- }
196
-
197
- // src/derivations/with-rollup.ts
198
- function withRollup(config) {
199
- const { from, key, into, field, compute } = config;
200
- if (!from || from.length === 0) {
201
- throw new ValidationError("withRollup: `from` (child collection) is required");
202
- }
203
- if (!into || into.length === 0) {
204
- throw new ValidationError("withRollup: `into` (parent collection) is required");
205
- }
206
- if (from === into) {
207
- throw new ValidationError("withRollup: `from` and `into` must be different collections");
208
- }
209
- if (!key || key.length === 0) {
210
- throw new ValidationError("withRollup: `key` (FK field on the child) is required");
211
- }
212
- if (!field || field.length === 0) {
213
- throw new ValidationError("withRollup: `field` (target field on the parent) is required");
214
- }
215
- if (typeof compute !== "function") {
216
- throw new ValidationError("withRollup: `compute` must be a function");
217
- }
218
- const spec = {
219
- source: into,
220
- // the parent record is what carries the rolled-up field
221
- deterministic: true,
222
- rollup: { from, key, field, compute },
223
- // Synthetic self-write output for registry / cycle bookkeeping. Dispatch
224
- // patches `field` directly (value-equality guarded); the executor is not run.
225
- outputs: { value: { shape: "record", collection: into, denorm: [field] } },
226
- derive: () => ({ value: {} }),
227
- // never invoked for a rollup strategy
228
- lifecycle: "eager"
229
- };
230
- return { __noydb_strategy: "derivation", spec };
231
- }
232
-
233
- // src/derivations/strategy-hash.ts
234
- async function computeStrategyHash(source, outputKeys, derive, sources) {
235
- const canonical = JSON.stringify({
236
- source,
237
- outputs: [...outputKeys].sort(),
238
- derive: derive.toString(),
239
- // Declared sibling sources (#344) — adding/removing a trigger
240
- // collection invalidates cached derived records. Omitted when empty
241
- // so strategies without siblings keep their existing hash.
242
- ...sources?.length ? { sources: [...sources].sort() } : {}
243
- });
244
- const bytes = new TextEncoder().encode(canonical);
245
- const digest = await crypto.subtle.digest("SHA-256", bytes);
246
- return Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join("");
247
- }
248
-
249
- // src/derivations/registry.ts
250
- var DerivationRegistry = class {
251
- _bySource = /* @__PURE__ */ new Map();
252
- _byOutput = /* @__PURE__ */ new Map();
253
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
254
- async register(spec) {
255
- const outputKeys = Object.keys(spec.outputs);
256
- const strategyHash = await computeStrategyHash(spec.source, outputKeys, spec.derive, spec.sources);
257
- const reg = { spec, strategyHash };
258
- const fromSource = this._bySource.get(spec.source);
259
- if (fromSource) fromSource.push(reg);
260
- else this._bySource.set(spec.source, [reg]);
261
- for (const extra of spec.sources ?? []) {
262
- const fromExtra = this._bySource.get(extra);
263
- if (fromExtra) fromExtra.push(reg);
264
- else this._bySource.set(extra, [reg]);
265
- }
266
- for (const t of spec.triggerBy ?? []) {
267
- const fromTrigger = this._bySource.get(t.collection);
268
- if (fromTrigger) fromTrigger.push(reg);
269
- else this._bySource.set(t.collection, [reg]);
270
- }
271
- if (spec.rollup) {
272
- const fromRollup = this._bySource.get(spec.rollup.from);
273
- if (fromRollup) fromRollup.push(reg);
274
- else this._bySource.set(spec.rollup.from, [reg]);
275
- }
276
- for (const key of outputKeys) {
277
- const output = spec.outputs[key];
278
- if (!output) continue;
279
- const outputCollection = output.collection;
280
- const arr = this._byOutput.get(outputCollection);
281
- if (arr) arr.push(reg);
282
- else this._byOutput.set(outputCollection, [reg]);
283
- }
284
- }
285
- strategiesForSource(source) {
286
- return this._bySource.get(source) ?? [];
287
- }
288
- strategiesProducingOutput(collection) {
289
- return this._byOutput.get(collection) ?? [];
290
- }
291
- /**
292
- * All registered strategies as a flat, deduplicated array.
293
- * Each strategy is indexed once per source (not once per output key),
294
- * so iterating `_bySource.values()` naturally yields each strategy
295
- * exactly once per source — deduplication is handled by flattening
296
- * the per-source arrays and collecting into a Set by identity.
297
- *
298
- * Used by `dumpSchema()` / `describeDerivations()` in the introspection
299
- * walker to populate the derivations map.
300
- */
301
- all() {
302
- const seen = /* @__PURE__ */ new Set();
303
- for (const strategies of this._bySource.values()) {
304
- for (const s of strategies) seen.add(s);
305
- }
306
- return [...seen];
307
- }
308
- /**
309
- * Cycle detection over the source → output → … graph. Call after all
310
- * `register()` calls complete (i.e. at vault open). Throws
311
- * `DerivationCycleError` on the first cycle found.
312
- */
313
- validate() {
314
- const visited = /* @__PURE__ */ new Set();
315
- const stack = [];
316
- const visit = (node) => {
317
- if (stack.includes(node)) {
318
- const cycle = stack.slice(stack.indexOf(node)).concat(node);
319
- throw new DerivationCycleError(cycle);
320
- }
321
- if (visited.has(node)) return;
322
- stack.push(node);
323
- const strategies = this._bySource.get(node);
324
- if (strategies) {
325
- for (const s of strategies) {
326
- for (const key of Object.keys(s.spec.outputs)) {
327
- const output = s.spec.outputs[key];
328
- if (!output) continue;
329
- if (output.shape === "record" && output.collection === s.spec.source && output.denorm !== void 0) {
330
- continue;
331
- }
332
- visit(output.collection);
333
- }
334
- }
335
- }
336
- stack.pop();
337
- visited.add(node);
338
- };
339
- for (const src of this._bySource.keys()) visit(src);
340
- }
341
- };
342
-
343
- // src/derivations/executor.ts
344
- var DerivationExecutor = {
345
- /**
346
- * Run `derive` once, validate output shape against the spec, stamp
347
- * `_derivedFrom` onto every output. Returns per-output success or
348
- * failure; throws only for shape mismatches (a contract violation).
349
- */
350
- async run(strategy, source, sourceVersion, strategyHash, ctx) {
351
- const outputs = {};
352
- let derived;
353
- try {
354
- derived = await Promise.resolve(strategy.derive(source, ctx));
355
- } catch (err) {
356
- for (const key of Object.keys(strategy.outputs)) {
357
- outputs[key] = {
358
- kind: "failed",
359
- value: {},
360
- ok: false,
361
- error: err instanceof Error ? err : new Error(String(err))
362
- };
363
- }
364
- return { outputs, failed: true };
365
- }
366
- const meta = {
367
- source: strategy.source,
368
- sourceId: source.id,
369
- sourceVersion,
370
- derivedAt: (/* @__PURE__ */ new Date()).toISOString(),
371
- strategyHash
372
- };
373
- for (const key of Object.keys(strategy.outputs)) {
374
- const outSpec = strategy.outputs[key];
375
- if (!outSpec) continue;
376
- const value = derived[key];
377
- if (outSpec.shape === "array") {
378
- if (value === void 0 || value === null) {
379
- outputs[key] = { kind: "array", ok: true, entries: [] };
380
- continue;
381
- }
382
- if (!Array.isArray(value)) {
383
- throw new DerivationOutputShapeError(
384
- key,
385
- `shape 'array' expects an array, got ${typeof value}`
386
- );
387
- }
388
- const maxFanout = outSpec.maxFanout ?? 64;
389
- if (value.length > maxFanout) {
390
- throw new DerivationCapExceededError(key, value.length, maxFanout);
391
- }
392
- const entries = [];
393
- const seenKeys = /* @__PURE__ */ new Set();
394
- for (let i = 0; i < value.length; i++) {
395
- const row = value[i];
396
- if (row === null || typeof row !== "object") {
397
- throw new DerivationOutputShapeError(
398
- key,
399
- `array member at index ${i} must be a non-null object (got ${row === null ? "null" : typeof row})`
400
- );
401
- }
402
- let derivedKey;
403
- try {
404
- derivedKey = outSpec.key(row);
405
- } catch (err) {
406
- throw new DerivationOutputShapeError(
407
- key,
408
- `key extractor threw on array member at index ${i}: ` + (err instanceof Error ? err.message : String(err))
409
- );
410
- }
411
- if (typeof derivedKey !== "string" || derivedKey.length === 0) {
412
- throw new DerivationOutputShapeError(
413
- key,
414
- `key extractor returned ${typeof derivedKey === "string" ? "empty string" : typeof derivedKey} at index ${i}; expected non-empty string`
415
- );
416
- }
417
- if (seenKeys.has(derivedKey)) {
418
- throw new DerivationOutputShapeError(
419
- key,
420
- `duplicate key "${derivedKey}" in array output (index ${i}); each derived row must have a unique key within a single derive() invocation`
421
- );
422
- }
423
- seenKeys.add(derivedKey);
424
- entries.push({
425
- key: derivedKey,
426
- value: { ...row, _derivedFrom: meta }
427
- });
428
- }
429
- outputs[key] = { kind: "array", ok: true, entries };
430
- continue;
431
- }
432
- if (value === void 0 || value === null) {
433
- if (outSpec.optional === true) {
434
- outputs[key] = { kind: "record", value: {}, ok: true, skipped: true };
435
- continue;
436
- }
437
- throw new DerivationOutputShapeError(
438
- key,
439
- `expected object, got ${value === void 0 ? "undefined" : "null"}`
440
- );
441
- }
442
- if (typeof value !== "object") {
443
- throw new DerivationOutputShapeError(
444
- key,
445
- `expected object, got ${typeof value}`
446
- );
447
- }
448
- outputs[key] = {
449
- kind: "record",
450
- value: { ...value, _derivedFrom: meta },
451
- ok: true
452
- };
453
- }
454
- return { outputs, failed: false };
455
- }
456
- };
457
- // Annotate the CommonJS export names for ESM import in node:
458
- 0 && (module.exports = {
459
- DerivationCapExceededError,
460
- DerivationCycleError,
461
- DerivationDepthError,
462
- DerivationExecutor,
463
- DerivationOutputShapeError,
464
- DerivationOutputUnknownError,
465
- DerivationRegistry,
466
- withDerivation,
467
- withRollup
468
- });
469
- //# sourceMappingURL=index.cjs.map