@mnmal-ai/substrate 1.0.0-alpha.10 → 1.0.0-alpha.12
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 +4 -2
- package/dist/{boot-store-BVqfFxh1.js → boot-store-DHv9H1Gr.js} +170 -69
- package/dist/{bridge-Cp4PaIYO.d.ts → bridge-DQyQ6Qhn.d.ts} +1 -1
- package/dist/{cli-hSCBywhE.js → cli-_UNdneXr.js} +44 -24
- package/dist/client.d.ts +3 -4
- package/dist/client.js +3 -3
- package/dist/{errors-7mycvNGq.d.ts → errors-xP9Mo7Q1.d.ts} +38 -4
- package/dist/{index-DgSfwx7M.d.ts → index-baF5A5jU.d.ts} +1 -1
- package/dist/pack.d.ts +2 -2
- package/dist/pack.js +1 -1
- package/dist/{pg-edges-store-Lxsl5ZnK.js → pg-edges-store-B4FSN9yp.js} +72 -9
- package/dist/{plugin-CtwOINCM.js → plugin-BLm5IRdX.js} +2 -2
- package/dist/proposals-bootstrap.js +2 -2
- package/dist/proposals.d.ts +2 -3
- package/dist/proposals.js +1 -1
- package/dist/recall-bootstrap.js +2 -2
- package/dist/recall.d.ts +10 -11
- package/dist/recall.js +2 -2
- package/dist/{repl-DqfHelK7.js → repl-DUJGBwie.js} +25 -25
- package/dist/{row-BKKCXIG6.d.ts → row-Cu_iRhcR.d.ts} +57 -10
- package/dist/{run-B5dTbZ3g.js → run-BpsTNvdI.js} +539 -145
- package/dist/{dataset-CejF-Fr_.d.ts → schema-oXfQyo0s.d.ts} +2455 -1589
- package/dist/sdk.d.ts +108 -34
- package/dist/sdk.js +4 -4
- package/dist/{src-CqT-QWZ_.js → src-Bew_JvTo.js} +32 -16
- package/dist/{src-Da0t6si5.js → src-Bi3NhEsT.js} +1 -1
- package/dist/{src-5SJ5GEIM.js → src-C4t1Gy-m.js} +4844 -1367
- package/dist/{src-7CUSMK66.js → src-CHND7-Pm.js} +7 -3
- package/dist/src-CSe2tJDf.js +6940 -0
- package/dist/{src-Bi849yhn.js → src-CzhKLsOV.js} +175 -91
- package/dist/{src-hZb8wfvg.js → src-DWqzrm3O.js} +400 -284
- package/dist/substrate-admin.js +1 -1
- package/dist/substrate.js +1 -1
- package/dist/testing.d.ts +255 -90
- package/dist/testing.js +6 -23
- package/dist/{ws-ByZyHk7m.d.ts → ws-Dx3A2B4U.d.ts} +2 -3
- package/package.json +20 -20
- package/dist/schema-DoSc-4ig.d.ts +0 -470
- package/dist/src-CyvV7dCK.js +0 -4333
|
@@ -11,6 +11,13 @@ function assertNever(x) {
|
|
|
11
11
|
throw new Error(`unhandled variant: ${JSON.stringify(x)}`);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region ../../packages/substrate-types/src/dataset.ts
|
|
16
|
+
const DEFAULT_FILTER_WRITE_LIMITS = Object.freeze({
|
|
17
|
+
maxMatch: 1e4,
|
|
18
|
+
idsCap: 1e3
|
|
19
|
+
});
|
|
20
|
+
|
|
14
21
|
//#endregion
|
|
15
22
|
//#region ../../packages/substrate-types/src/qualified-key.ts
|
|
16
23
|
function parseQualifiedKey(key) {
|
|
@@ -31,6 +38,16 @@ function formatQualifiedKey(namespace, name) {
|
|
|
31
38
|
|
|
32
39
|
//#endregion
|
|
33
40
|
//#region ../../packages/substrate-types/src/schema.ts
|
|
41
|
+
/**
|
|
42
|
+
* True when the field's value is a typed column: a scalar other than `json` that
|
|
43
|
+
* the server does not own, or a crossRef. An explicit `null` on such a field
|
|
44
|
+
* reads back as absent. A json, struct or list field keeps its `null`, and a
|
|
45
|
+
* readonly scalar such as `shortId` is the server's.
|
|
46
|
+
*/
|
|
47
|
+
function isColumnField(def) {
|
|
48
|
+
if (def.kind === "crossRef") return true;
|
|
49
|
+
return def.kind === "scalar" && def.type !== "json" && def.readonly !== true;
|
|
50
|
+
}
|
|
34
51
|
/** Natural order of a type that declares none: newest first. */
|
|
35
52
|
const DEFAULT_ORDERED_BY = Object.freeze({ createdAt: "desc" });
|
|
36
53
|
/** The declared natural order, or {@link DEFAULT_ORDERED_BY}. */
|
|
@@ -40,6 +57,33 @@ function naturalOrderOf(schema) {
|
|
|
40
57
|
|
|
41
58
|
//#endregion
|
|
42
59
|
//#region ../../packages/substrate-pack/src/_plan-cache.ts
|
|
60
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
61
|
+
/** The slot for one selection entry, or undefined for a plain field. */
|
|
62
|
+
function walkSlot(name, sel, fieldMap) {
|
|
63
|
+
if (!isObj(sel)) return void 0;
|
|
64
|
+
if (name === "_agg") return {
|
|
65
|
+
kind: "agg",
|
|
66
|
+
names: Object.keys(sel)
|
|
67
|
+
};
|
|
68
|
+
const walk = sel._walk;
|
|
69
|
+
if (!isObj(walk)) return void 0;
|
|
70
|
+
let type;
|
|
71
|
+
if (typeof walk.in === "string") type = /^[^/]+\/([^./]+)\./.exec(walk.in)?.[1];
|
|
72
|
+
else if (typeof walk.out === "string") {
|
|
73
|
+
const def = Object.hasOwn(fieldMap, walk.out) ? fieldMap[walk.out] : void 0;
|
|
74
|
+
if (def?.kind === "crossRef") type = def.type;
|
|
75
|
+
}
|
|
76
|
+
if (type === void 0) return void 0;
|
|
77
|
+
const fields = isObj(sel.fields) ? sel.fields : {};
|
|
78
|
+
return {
|
|
79
|
+
kind: "nested",
|
|
80
|
+
type,
|
|
81
|
+
fields: {
|
|
82
|
+
id: true,
|
|
83
|
+
...fields
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
43
87
|
/**
|
|
44
88
|
* Lookup-or-build the per-(fields, fieldMap) plan. O(1) on hit; O(F) on miss
|
|
45
89
|
* for F selected fields.
|
|
@@ -54,14 +98,29 @@ function getRowPlan$2(cache, fields, fieldMap) {
|
|
|
54
98
|
if (cached !== void 0) return cached;
|
|
55
99
|
const entries = Object.entries(fields);
|
|
56
100
|
const plan = new Array(entries.length);
|
|
101
|
+
let nested = false;
|
|
57
102
|
for (let i = 0; i < entries.length; i++) {
|
|
58
103
|
const pair = entries[i];
|
|
59
|
-
|
|
104
|
+
const def = Object.hasOwn(fieldMap, pair[0]) ? fieldMap[pair[0]] : void 0;
|
|
105
|
+
const slot = def === void 0 ? walkSlot(pair[0], pair[1], fieldMap) : void 0;
|
|
106
|
+
if (slot?.kind === "nested") nested = true;
|
|
107
|
+
plan[i] = slot === void 0 ? [
|
|
108
|
+
pair[0],
|
|
109
|
+
pair[1],
|
|
110
|
+
def
|
|
111
|
+
] : [
|
|
60
112
|
pair[0],
|
|
61
113
|
pair[1],
|
|
62
|
-
|
|
114
|
+
def,
|
|
115
|
+
slot
|
|
63
116
|
];
|
|
64
117
|
}
|
|
118
|
+
if (nested) plan.push([
|
|
119
|
+
"_truncated",
|
|
120
|
+
true,
|
|
121
|
+
void 0,
|
|
122
|
+
{ kind: "truncated" }
|
|
123
|
+
]);
|
|
65
124
|
perFieldMap.set(fieldMap, plan);
|
|
66
125
|
return plan;
|
|
67
126
|
}
|
|
@@ -113,70 +172,69 @@ function resolveFieldMap$2(schema, typeName, path, makeError) {
|
|
|
113
172
|
}
|
|
114
173
|
|
|
115
174
|
//#endregion
|
|
116
|
-
//#region ../../packages/substrate-pack/src/
|
|
175
|
+
//#region ../../packages/substrate-pack/src/encoder.ts
|
|
117
176
|
/**
|
|
118
|
-
* Positional
|
|
177
|
+
* Positional encoder. Spec: §7.0, §7.5.
|
|
119
178
|
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* (§7.
|
|
179
|
+
* Strips field names from the wire. For a selected FieldInput, emits a tuple
|
|
180
|
+
* of values in iteration order; the decoder reconstructs the named object
|
|
181
|
+
* using the same FieldInput plus the schema cached at client init. The win is
|
|
182
|
+
* parse time (§7.5) — msgpackr handles named objects fine, but positional
|
|
183
|
+
* arrays skip the key lookup per cell, which compounds across thousand-row
|
|
184
|
+
* responses.
|
|
124
185
|
*
|
|
125
|
-
*
|
|
126
|
-
* `
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
186
|
+
* Contract: `encodePositional` is shape-blind. Callers decide single-vs-list
|
|
187
|
+
* via `PackEnvelope.shapes[key]` (§7.2) and pass either a row or an array
|
|
188
|
+
* of rows; the encoder emits the corresponding tuple or array-of-tuples. It does
|
|
189
|
+
* NOT infer list-vs-single from the value shape.
|
|
190
|
+
*
|
|
191
|
+
* Mutation responses use `encodePositionalFromFieldDef` — mutations don't
|
|
192
|
+
* carry a client-sent `FieldInput`, so the FieldDef declared at registration
|
|
193
|
+
* (MutationSchema.returns) drives the encoding directly.
|
|
130
194
|
*/
|
|
131
195
|
/**
|
|
132
|
-
* @internal — error thrown by the positional
|
|
133
|
-
* Exported, but treat as package-private
|
|
134
|
-
* major bump.
|
|
196
|
+
* @internal — error thrown by the positional encoder on malformed input.
|
|
197
|
+
* Exported, but treat as package-private.
|
|
135
198
|
*/
|
|
136
|
-
var
|
|
137
|
-
name = "
|
|
199
|
+
var PackEncodeError = class extends Error {
|
|
200
|
+
name = "PackEncodeError";
|
|
138
201
|
path;
|
|
139
202
|
constructor(message, path) {
|
|
140
203
|
super(`${message} at ${path.length === 0 ? "<root>" : path.join(".")}`);
|
|
141
204
|
this.path = path.slice();
|
|
142
205
|
}
|
|
143
206
|
};
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
*/
|
|
150
|
-
var SchemaDriftError = class extends Error {
|
|
151
|
-
envelopeSchemaVersion;
|
|
152
|
-
expectedSchemaVersion;
|
|
153
|
-
name = "SchemaDriftError";
|
|
154
|
-
constructor(envelopeSchemaVersion, expectedSchemaVersion) {
|
|
155
|
-
super(`schema drift: envelope schemaVersion '${envelopeSchemaVersion}' does not match expected '${expectedSchemaVersion}'`);
|
|
156
|
-
this.envelopeSchemaVersion = envelopeSchemaVersion;
|
|
157
|
-
this.expectedSchemaVersion = expectedSchemaVersion;
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
function decodePositional(encoded, fields, schema, typeName, ctx) {
|
|
161
|
-
if (encoded === null || encoded === void 0) return null;
|
|
162
|
-
return decodeValueByArity(encoded, fields, resolveFieldMap$1(schema, typeName, [typeName]), schema, [typeName], ctx);
|
|
207
|
+
function encodePositional(value, fields, schema, typeName, ctx) {
|
|
208
|
+
if (value === null || value === void 0) return null;
|
|
209
|
+
const plan = getRowPlan$1(fields, resolveFieldMap$1(schema, typeName, [typeName]));
|
|
210
|
+
if (Array.isArray(value)) return value.map((row) => encodeRowWithPlan(row, plan, schema, [typeName], ctx));
|
|
211
|
+
return encodeRowWithPlan(value, plan, schema, [typeName], ctx);
|
|
163
212
|
}
|
|
164
213
|
/**
|
|
165
|
-
*
|
|
166
|
-
* `
|
|
214
|
+
* Encode a mutation response against its declared `FieldDef` (no client-sent
|
|
215
|
+
* FieldInput). Symmetric to `encodePositional` but driven entirely by the
|
|
216
|
+
* schema descriptor.
|
|
167
217
|
*
|
|
168
|
-
*
|
|
218
|
+
* - `scalar` → value passes through (nullable tolerated)
|
|
219
|
+
* - `struct` → positional tuple over `def.fields` iteration order
|
|
220
|
+
* - `list` → value is an array; each element encoded against `def.of`
|
|
221
|
+
* - `crossRef` → value is expected to be an id scalar (eq-on-id; we don't
|
|
222
|
+
* recurse into the target type — mutation returns don't embed sub-rows)
|
|
223
|
+
*
|
|
224
|
+
* @internal — used by `encodeEnvelope` for mutation returns. Treat as
|
|
169
225
|
* package-private.
|
|
170
226
|
*/
|
|
171
|
-
function
|
|
172
|
-
if (
|
|
173
|
-
return
|
|
227
|
+
function encodePositionalFromFieldDef(value, def, schema, ctx, path = []) {
|
|
228
|
+
if (value === null || value === void 0) return null;
|
|
229
|
+
return encodeFieldDef(value, def, schema, path, ctx);
|
|
174
230
|
}
|
|
175
|
-
function
|
|
176
|
-
const raw = unpack(Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes));
|
|
177
|
-
if (ctx.expectedSchemaVersion !== void 0 && raw.schemaVersion !== ctx.expectedSchemaVersion) throw new SchemaDriftError(raw.schemaVersion, ctx.expectedSchemaVersion);
|
|
231
|
+
function encodeEnvelope(envelope, ctx) {
|
|
178
232
|
const rewritten = {};
|
|
179
|
-
for (const [key, value] of Object.entries(
|
|
233
|
+
for (const [key, value] of Object.entries(envelope.data)) {
|
|
234
|
+
if (isAggShape(envelope.shapes[key])) {
|
|
235
|
+
rewritten[key] = value;
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
180
238
|
const returns = ctx.returnsByKey?.[key];
|
|
181
239
|
if (returns !== void 0) {
|
|
182
240
|
const currentNs = ctx.currentNsByKey[key];
|
|
@@ -185,7 +243,7 @@ function decodeEnvelope(bytes, ctx) {
|
|
|
185
243
|
rewritten[key] = value;
|
|
186
244
|
continue;
|
|
187
245
|
}
|
|
188
|
-
rewritten[key] =
|
|
246
|
+
rewritten[key] = encodePositionalForShape(value, returns, schema, envelope.shapes[key], {
|
|
189
247
|
schemas: ctx.schemas,
|
|
190
248
|
currentNs
|
|
191
249
|
});
|
|
@@ -203,182 +261,199 @@ function decodeEnvelope(bytes, ctx) {
|
|
|
203
261
|
rewritten[key] = value;
|
|
204
262
|
continue;
|
|
205
263
|
}
|
|
206
|
-
|
|
207
|
-
rewritten[key] = decodeByShape(value, fields, schema, typeName, Array.isArray(rawShape) ? void 0 : rawShape, {
|
|
264
|
+
rewritten[key] = encodePositional(value, fields, schema, typeName, {
|
|
208
265
|
schemas: ctx.schemas,
|
|
209
266
|
currentNs
|
|
210
267
|
});
|
|
211
268
|
}
|
|
212
269
|
const out = {
|
|
213
|
-
schemaVersion:
|
|
270
|
+
schemaVersion: envelope.schemaVersion,
|
|
214
271
|
data: rewritten,
|
|
215
|
-
shapes:
|
|
272
|
+
shapes: envelope.shapes
|
|
216
273
|
};
|
|
217
|
-
if (
|
|
218
|
-
if (
|
|
219
|
-
if (
|
|
220
|
-
if (
|
|
221
|
-
if (
|
|
222
|
-
return out;
|
|
274
|
+
if (envelope._meta !== void 0) out._meta = envelope._meta;
|
|
275
|
+
if (envelope._pagination !== void 0) out._pagination = envelope._pagination;
|
|
276
|
+
if (envelope._completeness !== void 0) out._completeness = envelope._completeness;
|
|
277
|
+
if (envelope._diagnostics !== void 0) out._diagnostics = envelope._diagnostics;
|
|
278
|
+
if (envelope.errors !== void 0) out.errors = envelope.errors;
|
|
279
|
+
return Buffer.from(pack(out));
|
|
223
280
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (shape === "list") {
|
|
228
|
-
if (!Array.isArray(value)) throw new PackDecodeError("expected array for list shape", [typeName]);
|
|
229
|
-
return value.map((elem) => decodeRowWithPlan(elem, plan, schema, [typeName], ctx));
|
|
230
|
-
}
|
|
231
|
-
return decodeRowWithPlan(value, plan, schema, [typeName], ctx);
|
|
281
|
+
/** A shape whose value is a `{ count }` or `{ groups }` map, not rows. */
|
|
282
|
+
function isAggShape(shape) {
|
|
283
|
+
return shape === "count" || shape === "groups";
|
|
232
284
|
}
|
|
233
|
-
|
|
285
|
+
/**
|
|
286
|
+
* Encode a single envelope value against its declared `returns` FieldDef,
|
|
287
|
+
* honouring the envelope's single-vs-list shape for that key. `single` encodes
|
|
288
|
+
* one row; `list` encodes an array of rows element-by-element.
|
|
289
|
+
*
|
|
290
|
+
* Batched mutate (§5.13): when the shape is `Array<'single' | 'list'>`, the
|
|
291
|
+
* value is itself `Array<unknown>` (one per-frame slot); each slot encodes
|
|
292
|
+
* against the same FieldDef using its own per-frame shape.
|
|
293
|
+
*/
|
|
294
|
+
function encodePositionalForShape(value, def, schema, shape, ctx) {
|
|
234
295
|
if (value === null || value === void 0) return null;
|
|
235
296
|
if (Array.isArray(shape)) {
|
|
236
|
-
if (!Array.isArray(value)) throw new
|
|
237
|
-
return value.map((elem, idx) =>
|
|
297
|
+
if (!Array.isArray(value)) throw new PackEncodeError("expected array for batched shape", []);
|
|
298
|
+
return value.map((elem, idx) => {
|
|
299
|
+
const perFrameShape = shape[idx];
|
|
300
|
+
return encodePositionalForShape(elem, def, schema, perFrameShape, ctx);
|
|
301
|
+
});
|
|
238
302
|
}
|
|
239
303
|
if (shape === "list") {
|
|
240
|
-
if (!Array.isArray(value)) throw new
|
|
241
|
-
return value.map((elem, idx) =>
|
|
304
|
+
if (!Array.isArray(value)) throw new PackEncodeError("expected array for list shape", []);
|
|
305
|
+
return value.map((elem, idx) => encodePositionalFromFieldDef(elem, def, schema, ctx, [`[${idx}]`]));
|
|
242
306
|
}
|
|
243
|
-
return
|
|
307
|
+
return encodePositionalFromFieldDef(value, def, schema, ctx);
|
|
244
308
|
}
|
|
245
309
|
function resolveFieldMap$1(schema, typeName, path) {
|
|
246
|
-
return resolveFieldMap$2(schema, typeName, path, (message, p) => new
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Decide whether `value` is a single tuple or a list of tuples based on its
|
|
250
|
-
* shape plus the FieldInput — used only by the top-level `decodePositional`
|
|
251
|
-
* helper (the envelope decoder uses `shapes[key]` directly). Heuristic:
|
|
252
|
-
* - null → null
|
|
253
|
-
* - Array where the first element is itself an array → list of rows
|
|
254
|
-
* - Array otherwise → single row (tuple)
|
|
255
|
-
*
|
|
256
|
-
* This heuristic is the ambiguity §7.2 calls out, so callers SHOULD go through
|
|
257
|
-
* `decodeEnvelope` to use explicit shape metadata.
|
|
258
|
-
*/
|
|
259
|
-
function decodeValueByArity(value, fields, fieldMap, schema, path, ctx) {
|
|
260
|
-
if (value === null || value === void 0) return null;
|
|
261
|
-
if (!Array.isArray(value)) throw new PackDecodeError("expected array (tuple) for positional row", path);
|
|
262
|
-
const plan = getRowPlan$1(fields, fieldMap);
|
|
263
|
-
if (value.length > 0 && Array.isArray(value[0])) return value.map((elem) => decodeRowWithPlan(elem, plan, schema, path, ctx));
|
|
264
|
-
return decodeRowWithPlan(value, plan, schema, path, ctx);
|
|
310
|
+
return resolveFieldMap$2(schema, typeName, path, (message, p) => new PackEncodeError(message, p));
|
|
265
311
|
}
|
|
266
|
-
const
|
|
312
|
+
const ENCODE_PLAN_CACHE = /* @__PURE__ */ new WeakMap();
|
|
267
313
|
function getRowPlan$1(fields, fieldMap) {
|
|
268
|
-
return getRowPlan$2(
|
|
314
|
+
return getRowPlan$2(ENCODE_PLAN_CACHE, fields, fieldMap);
|
|
269
315
|
}
|
|
270
|
-
function
|
|
271
|
-
if (
|
|
272
|
-
if (
|
|
273
|
-
const
|
|
316
|
+
function encodeRowWithPlan(row, plan, schema, path, ctx) {
|
|
317
|
+
if (row === null || row === void 0) return null;
|
|
318
|
+
if (typeof row !== "object" || Array.isArray(row)) throw new PackEncodeError(`expected object row, got ${Array.isArray(row) ? "array" : typeof row}`, path);
|
|
319
|
+
const rowObj = row;
|
|
274
320
|
const len = plan.length;
|
|
321
|
+
const tuple = new Array(len);
|
|
275
322
|
for (let i = 0; i < len; i++) {
|
|
276
323
|
const entry = plan[i];
|
|
277
324
|
const field = entry[0];
|
|
278
325
|
const selection = entry[1];
|
|
279
326
|
const def = entry[2];
|
|
280
|
-
const
|
|
327
|
+
const value = Object.hasOwn(rowObj, field) ? rowObj[field] : void 0;
|
|
281
328
|
path.push(field);
|
|
282
|
-
|
|
329
|
+
const slot = entry[3];
|
|
330
|
+
tuple[i] = slot === void 0 ? encodeField(value, selection, def, schema, path, ctx) : encodeWalkSlot(value, slot, schema, path, ctx);
|
|
283
331
|
path.pop();
|
|
284
332
|
}
|
|
285
|
-
return
|
|
333
|
+
return tuple;
|
|
286
334
|
}
|
|
287
|
-
function
|
|
288
|
-
if (
|
|
289
|
-
|
|
290
|
-
|
|
335
|
+
function encodeWalkSlot(value, slot, schema, path, ctx) {
|
|
336
|
+
if (value === null || value === void 0) return null;
|
|
337
|
+
switch (slot.kind) {
|
|
338
|
+
case "truncated": return value;
|
|
339
|
+
case "agg": {
|
|
340
|
+
if (typeof value !== "object" || Array.isArray(value)) throw new PackEncodeError("expected object for _agg", path);
|
|
341
|
+
const aggs = value;
|
|
342
|
+
return slot.names.map((n) => coerceScalar(Object.hasOwn(aggs, n) ? aggs[n] : null));
|
|
343
|
+
}
|
|
344
|
+
case "nested": {
|
|
345
|
+
if (!Array.isArray(value)) throw new PackEncodeError("expected array for nested selection", path);
|
|
346
|
+
const plan = getRowPlan$1(slot.fields, resolveFieldMap$1(schema, slot.type, path));
|
|
347
|
+
return value.map((row, idx) => {
|
|
348
|
+
path.push(`[${idx}]`);
|
|
349
|
+
const out = encodeRowWithPlan(row, plan, schema, path, ctx);
|
|
350
|
+
path.pop();
|
|
351
|
+
return out;
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
default: return assertNever(slot);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
function encodeField(value, selection, def, schema, path, ctx) {
|
|
358
|
+
if (value === null || value === void 0) return null;
|
|
359
|
+
if (def === void 0) return selection === true ? coerceScalar(value) : value;
|
|
360
|
+
if (selection === true) return encodeLeaf(value, def, path);
|
|
291
361
|
switch (def.kind) {
|
|
292
|
-
case "scalar": throw new
|
|
293
|
-
case "struct": return
|
|
294
|
-
case "list": return
|
|
295
|
-
case "crossRef": return
|
|
362
|
+
case "scalar": throw new PackEncodeError(`field is a scalar (${def.type}); cannot select sub-fields`, path);
|
|
363
|
+
case "struct": return encodeStruct(value, selection, def, schema, path, ctx);
|
|
364
|
+
case "list": return encodeList(value, selection, def, schema, path, ctx);
|
|
365
|
+
case "crossRef": return encodeCrossRef(value, selection, def, schema, path, ctx);
|
|
296
366
|
default: return assertNever(def);
|
|
297
367
|
}
|
|
298
368
|
}
|
|
299
|
-
function
|
|
369
|
+
function encodeLeaf(value, def, path) {
|
|
300
370
|
switch (def.kind) {
|
|
301
|
-
case "scalar": return
|
|
371
|
+
case "scalar": return coerceScalar(value);
|
|
302
372
|
case "list":
|
|
303
|
-
if (!Array.isArray(
|
|
304
|
-
return
|
|
373
|
+
if (!Array.isArray(value)) throw new PackEncodeError("expected array for list field", path);
|
|
374
|
+
return value.map((v) => coerceScalar(v));
|
|
305
375
|
case "struct":
|
|
306
|
-
case "crossRef": return
|
|
376
|
+
case "crossRef": return value;
|
|
307
377
|
default: return assertNever(def);
|
|
308
378
|
}
|
|
309
379
|
}
|
|
310
|
-
function
|
|
311
|
-
return
|
|
380
|
+
function encodeStruct(value, selection, def, schema, path, ctx) {
|
|
381
|
+
return encodeRowWithPlan(value, getRowPlan$1(selection, def.fields), schema, path, ctx);
|
|
312
382
|
}
|
|
313
|
-
function
|
|
314
|
-
if (!Array.isArray(
|
|
383
|
+
function encodeList(value, selection, def, schema, path, ctx) {
|
|
384
|
+
if (!Array.isArray(value)) throw new PackEncodeError("expected array for list field", path);
|
|
315
385
|
const inner = def.of;
|
|
316
386
|
const innerPlan = inner.kind === "struct" ? getRowPlan$1(selection, inner.fields) : void 0;
|
|
317
387
|
const out = [];
|
|
318
|
-
for (let idx = 0; idx <
|
|
319
|
-
const elem =
|
|
388
|
+
for (let idx = 0; idx < value.length; idx++) {
|
|
389
|
+
const elem = value[idx];
|
|
320
390
|
if (elem === null || elem === void 0) {
|
|
321
391
|
out.push(null);
|
|
322
392
|
continue;
|
|
323
393
|
}
|
|
324
394
|
path.push(`[${idx}]`);
|
|
325
|
-
out.push(
|
|
395
|
+
out.push(encodeListElement(elem, selection, inner, schema, path, ctx, innerPlan));
|
|
326
396
|
path.pop();
|
|
327
397
|
}
|
|
328
398
|
return out;
|
|
329
399
|
}
|
|
330
|
-
function
|
|
400
|
+
function encodeListElement(elem, selection, inner, schema, elemPath, ctx, structPlan) {
|
|
331
401
|
switch (inner.kind) {
|
|
332
|
-
case "scalar": throw new
|
|
333
|
-
case "struct": return
|
|
334
|
-
case "list": return
|
|
335
|
-
case "crossRef": return
|
|
402
|
+
case "scalar": throw new PackEncodeError(`list element is scalar (${inner.type}); cannot select sub-fields`, elemPath);
|
|
403
|
+
case "struct": return encodeRowWithPlan(elem, structPlan ?? getRowPlan$1(selection, inner.fields), schema, elemPath, ctx);
|
|
404
|
+
case "list": return encodeList(elem, selection, inner, schema, elemPath, ctx);
|
|
405
|
+
case "crossRef": return encodeCrossRef(elem, selection, inner, schema, elemPath, ctx);
|
|
336
406
|
default: return assertNever(inner);
|
|
337
407
|
}
|
|
338
408
|
}
|
|
339
|
-
function
|
|
340
|
-
if (def.ns === ctx.currentNs) return
|
|
409
|
+
function encodeCrossRef(value, selection, def, schema, path, ctx) {
|
|
410
|
+
if (def.ns === ctx.currentNs) return encodeRowWithPlan(value, getRowPlan$1(selection, resolveFieldMap$1(schema, def.type, path)), schema, path, ctx);
|
|
341
411
|
const schemas = ctx.schemas;
|
|
342
|
-
if (schemas === void 0) throw new
|
|
412
|
+
if (schemas === void 0) throw new PackEncodeError(`cross-namespace crossRef '${def.ns}/${def.type}' requires ctx.schemas`, path);
|
|
343
413
|
const targetSchema = schemas[def.ns];
|
|
344
|
-
if (targetSchema === void 0) throw new
|
|
345
|
-
return
|
|
414
|
+
if (targetSchema === void 0) throw new PackEncodeError(`cross-namespace crossRef target namespace '${def.ns}' not in ctx.schemas`, path);
|
|
415
|
+
return encodeRowWithPlan(value, getRowPlan$1(selection, resolveFieldMap$1(targetSchema, def.type, path)), targetSchema, path, {
|
|
346
416
|
...ctx,
|
|
347
417
|
currentNs: def.ns
|
|
348
418
|
});
|
|
349
419
|
}
|
|
350
|
-
function
|
|
420
|
+
function encodeFieldDef(value, def, schema, path, ctx) {
|
|
351
421
|
if (value === null || value === void 0) return null;
|
|
352
422
|
switch (def.kind) {
|
|
353
|
-
case "scalar": return value;
|
|
354
|
-
case "struct": return
|
|
355
|
-
case "list": return
|
|
356
|
-
case "crossRef": return value;
|
|
423
|
+
case "scalar": return coerceScalar(value);
|
|
424
|
+
case "struct": return encodeStructFromDef(value, def, schema, path, ctx);
|
|
425
|
+
case "list": return encodeListFromDef(value, def, schema, path, ctx);
|
|
426
|
+
case "crossRef": return coerceScalar(value);
|
|
357
427
|
default: return assertNever(def);
|
|
358
428
|
}
|
|
359
429
|
}
|
|
360
|
-
const
|
|
430
|
+
const ENCODE_FIELDDEF_PLAN_CACHE = /* @__PURE__ */ new WeakMap();
|
|
361
431
|
function getFieldDefPlan$1(fields) {
|
|
362
|
-
return getFieldDefPlan$2(
|
|
432
|
+
return getFieldDefPlan$2(ENCODE_FIELDDEF_PLAN_CACHE, fields);
|
|
363
433
|
}
|
|
364
|
-
function
|
|
365
|
-
if (
|
|
366
|
-
const
|
|
434
|
+
function encodeStructFromDef(value, def, schema, path, ctx) {
|
|
435
|
+
if (typeof value !== "object" || Array.isArray(value)) throw new PackEncodeError(`expected object for struct, got ${Array.isArray(value) ? "array" : typeof value}`, path);
|
|
436
|
+
const rowObj = value;
|
|
367
437
|
const plan = getFieldDefPlan$1(def.fields);
|
|
368
438
|
const len = plan.length;
|
|
439
|
+
const tuple = new Array(len);
|
|
369
440
|
for (let i = 0; i < len; i++) {
|
|
370
441
|
const entry = plan[i];
|
|
371
442
|
const fieldName = entry[0];
|
|
372
443
|
const sub = entry[1];
|
|
373
|
-
const cell =
|
|
444
|
+
const cell = Object.hasOwn(rowObj, fieldName) ? rowObj[fieldName] : void 0;
|
|
445
|
+
if (cell === void 0) {
|
|
446
|
+
tuple[i] = null;
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
374
449
|
path.push(fieldName);
|
|
375
|
-
|
|
450
|
+
tuple[i] = encodeFieldDef(cell, sub, schema, path, ctx);
|
|
376
451
|
path.pop();
|
|
377
452
|
}
|
|
378
|
-
return
|
|
453
|
+
return tuple;
|
|
379
454
|
}
|
|
380
|
-
function
|
|
381
|
-
if (!Array.isArray(value)) throw new
|
|
455
|
+
function encodeListFromDef(value, def, schema, path, ctx) {
|
|
456
|
+
if (!Array.isArray(value)) throw new PackEncodeError("expected array for list field", path);
|
|
382
457
|
const out = [];
|
|
383
458
|
for (let idx = 0; idx < value.length; idx++) {
|
|
384
459
|
const elem = value[idx];
|
|
@@ -387,72 +462,88 @@ function decodeListFromDef(value, def, schema, path, ctx) {
|
|
|
387
462
|
continue;
|
|
388
463
|
}
|
|
389
464
|
path.push(`[${idx}]`);
|
|
390
|
-
out.push(
|
|
465
|
+
out.push(encodeFieldDef(elem, def.of, schema, path, ctx));
|
|
391
466
|
path.pop();
|
|
392
467
|
}
|
|
393
468
|
return out;
|
|
394
469
|
}
|
|
470
|
+
/**
|
|
471
|
+
* Identity today — msgpackr round-trips Dates natively. Kept as the single
|
|
472
|
+
* choke point for any future scalar normalisation.
|
|
473
|
+
*/
|
|
474
|
+
function coerceScalar(value) {
|
|
475
|
+
return value;
|
|
476
|
+
}
|
|
395
477
|
|
|
396
478
|
//#endregion
|
|
397
|
-
//#region ../../packages/substrate-pack/src/
|
|
479
|
+
//#region ../../packages/substrate-pack/src/decoder.ts
|
|
398
480
|
/**
|
|
399
|
-
* Positional
|
|
400
|
-
*
|
|
401
|
-
* Strips field names from the wire. For a selected FieldInput, emits a tuple
|
|
402
|
-
* of values in iteration order; the decoder reconstructs the named object
|
|
403
|
-
* using the same FieldInput plus the schema cached at client init. The win is
|
|
404
|
-
* parse time (§7.5) — msgpackr handles named objects fine, but positional
|
|
405
|
-
* arrays skip the key lookup per cell, which compounds across thousand-row
|
|
406
|
-
* responses.
|
|
481
|
+
* Positional decoder. Spec: §7.0, §7.2.
|
|
407
482
|
*
|
|
408
|
-
*
|
|
409
|
-
*
|
|
410
|
-
*
|
|
411
|
-
*
|
|
483
|
+
* Mirror of `encoder.ts`. Rebuilds named objects from positional tuples using
|
|
484
|
+
* the same `FieldInput` the client originally sent plus the schema cached at
|
|
485
|
+
* init. Single-vs-list dispatch is driven by `PackEnvelope.shapes[key]`
|
|
486
|
+
* (§7.2) — no inference from the tuple shape.
|
|
412
487
|
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
488
|
+
* Schema drift (§7.1) is detected here when the caller passes
|
|
489
|
+
* `expectedSchemaVersion`: the decoder throws `SchemaDriftError` before
|
|
490
|
+
* decoding any data value, so drift fails loudly instead of corrupting row
|
|
491
|
+
* shapes. Refetch-and-retry is the caller's job. Without
|
|
492
|
+
* `expectedSchemaVersion` no check is made.
|
|
416
493
|
*/
|
|
417
494
|
/**
|
|
418
|
-
* @internal — error thrown by the positional
|
|
419
|
-
* Exported, but treat as package-private
|
|
495
|
+
* @internal — error thrown by the positional decoder on malformed input.
|
|
496
|
+
* Exported, but treat as package-private; signature may change without a
|
|
497
|
+
* major bump.
|
|
420
498
|
*/
|
|
421
|
-
var
|
|
422
|
-
name = "
|
|
499
|
+
var PackDecodeError = class extends Error {
|
|
500
|
+
name = "PackDecodeError";
|
|
423
501
|
path;
|
|
424
502
|
constructor(message, path) {
|
|
425
503
|
super(`${message} at ${path.length === 0 ? "<root>" : path.join(".")}`);
|
|
426
504
|
this.path = path.slice();
|
|
427
505
|
}
|
|
428
506
|
};
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
507
|
+
/**
|
|
508
|
+
* §7.1 loud-failure. Thrown by `decodeEnvelope` when the caller supplied an
|
|
509
|
+
* `expectedSchemaVersion` that disagrees with the envelope's declared version.
|
|
510
|
+
* Carries both sides of the mismatch so the caller can decide whether to
|
|
511
|
+
* refetch the federated schema and retry or surface the error.
|
|
512
|
+
*/
|
|
513
|
+
var SchemaDriftError = class extends Error {
|
|
514
|
+
envelopeSchemaVersion;
|
|
515
|
+
expectedSchemaVersion;
|
|
516
|
+
name = "SchemaDriftError";
|
|
517
|
+
constructor(envelopeSchemaVersion, expectedSchemaVersion) {
|
|
518
|
+
super(`schema drift: envelope schemaVersion '${envelopeSchemaVersion}' does not match expected '${expectedSchemaVersion}'`);
|
|
519
|
+
this.envelopeSchemaVersion = envelopeSchemaVersion;
|
|
520
|
+
this.expectedSchemaVersion = expectedSchemaVersion;
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
function decodePositional(encoded, fields, schema, typeName, ctx) {
|
|
524
|
+
if (encoded === null || encoded === void 0) return null;
|
|
525
|
+
return decodeValueByArity(encoded, fields, resolveFieldMap(schema, typeName, [typeName]), schema, [typeName], ctx);
|
|
434
526
|
}
|
|
435
527
|
/**
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
* schema descriptor.
|
|
439
|
-
*
|
|
440
|
-
* - `scalar` → value passes through (nullable tolerated)
|
|
441
|
-
* - `struct` → positional tuple over `def.fields` iteration order
|
|
442
|
-
* - `list` → value is an array; each element encoded against `def.of`
|
|
443
|
-
* - `crossRef` → value is expected to be an id scalar (eq-on-id; we don't
|
|
444
|
-
* recurse into the target type — mutation returns don't embed sub-rows)
|
|
528
|
+
* Decode a mutation response against its declared `FieldDef`. Symmetric to
|
|
529
|
+
* `encodePositionalFromFieldDef`.
|
|
445
530
|
*
|
|
446
|
-
* @internal — used by `
|
|
531
|
+
* @internal — used by `decodeEnvelope` for mutation returns. Treat as
|
|
447
532
|
* package-private.
|
|
448
533
|
*/
|
|
449
|
-
function
|
|
450
|
-
if (
|
|
451
|
-
return
|
|
534
|
+
function decodePositionalFromFieldDef(encoded, def, schema, ctx, path = []) {
|
|
535
|
+
if (encoded === null || encoded === void 0) return null;
|
|
536
|
+
return decodeFieldDef(encoded, def, schema, path, ctx);
|
|
452
537
|
}
|
|
453
|
-
function
|
|
538
|
+
function decodeEnvelope(bytes, ctx) {
|
|
539
|
+
const raw = unpack(Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes));
|
|
540
|
+
if (ctx.expectedSchemaVersion !== void 0 && raw.schemaVersion !== ctx.expectedSchemaVersion) throw new SchemaDriftError(raw.schemaVersion, ctx.expectedSchemaVersion);
|
|
454
541
|
const rewritten = {};
|
|
455
|
-
for (const [key, value] of Object.entries(
|
|
542
|
+
for (const [key, value] of Object.entries(raw.data)) {
|
|
543
|
+
if (isAggShape(raw.shapes[key])) {
|
|
544
|
+
rewritten[key] = value;
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
456
547
|
const returns = ctx.returnsByKey?.[key];
|
|
457
548
|
if (returns !== void 0) {
|
|
458
549
|
const currentNs = ctx.currentNsByKey[key];
|
|
@@ -461,7 +552,7 @@ function encodeEnvelope(envelope, ctx) {
|
|
|
461
552
|
rewritten[key] = value;
|
|
462
553
|
continue;
|
|
463
554
|
}
|
|
464
|
-
rewritten[key] =
|
|
555
|
+
rewritten[key] = decodeByShapeFromDef(value, returns, schema, raw.shapes[key], {
|
|
465
556
|
schemas: ctx.schemas,
|
|
466
557
|
currentNs
|
|
467
558
|
});
|
|
@@ -479,172 +570,204 @@ function encodeEnvelope(envelope, ctx) {
|
|
|
479
570
|
rewritten[key] = value;
|
|
480
571
|
continue;
|
|
481
572
|
}
|
|
482
|
-
|
|
573
|
+
const rawShape = raw.shapes[key];
|
|
574
|
+
rewritten[key] = decodeByShape(value, fields, schema, typeName, Array.isArray(rawShape) || isAggShape(rawShape) ? void 0 : rawShape, {
|
|
483
575
|
schemas: ctx.schemas,
|
|
484
576
|
currentNs
|
|
485
577
|
});
|
|
486
578
|
}
|
|
487
579
|
const out = {
|
|
488
|
-
schemaVersion:
|
|
580
|
+
schemaVersion: raw.schemaVersion,
|
|
489
581
|
data: rewritten,
|
|
490
|
-
shapes:
|
|
582
|
+
shapes: raw.shapes
|
|
491
583
|
};
|
|
492
|
-
if (
|
|
493
|
-
if (
|
|
494
|
-
if (
|
|
495
|
-
if (
|
|
496
|
-
if (
|
|
497
|
-
return
|
|
584
|
+
if (raw._meta !== void 0) out._meta = raw._meta;
|
|
585
|
+
if (raw._pagination !== void 0) out._pagination = raw._pagination;
|
|
586
|
+
if (raw._completeness !== void 0) out._completeness = raw._completeness;
|
|
587
|
+
if (raw._diagnostics !== void 0) out._diagnostics = raw._diagnostics;
|
|
588
|
+
if (raw.errors !== void 0) out.errors = raw.errors;
|
|
589
|
+
return out;
|
|
498
590
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
function
|
|
591
|
+
function decodeByShape(value, fields, schema, typeName, shape, ctx) {
|
|
592
|
+
if (value === null || value === void 0) return null;
|
|
593
|
+
const plan = getRowPlan(fields, resolveFieldMap(schema, typeName, [typeName]));
|
|
594
|
+
if (shape === "list") {
|
|
595
|
+
if (!Array.isArray(value)) throw new PackDecodeError("expected array for list shape", [typeName]);
|
|
596
|
+
return value.map((elem) => decodeRowWithPlan(elem, plan, schema, [typeName], ctx));
|
|
597
|
+
}
|
|
598
|
+
return decodeRowWithPlan(value, plan, schema, [typeName], ctx);
|
|
599
|
+
}
|
|
600
|
+
function decodeByShapeFromDef(value, def, schema, shape, ctx) {
|
|
509
601
|
if (value === null || value === void 0) return null;
|
|
510
602
|
if (Array.isArray(shape)) {
|
|
511
|
-
if (!Array.isArray(value)) throw new
|
|
512
|
-
return value.map((elem, idx) =>
|
|
513
|
-
const perFrameShape = shape[idx];
|
|
514
|
-
return encodePositionalForShape(elem, def, schema, perFrameShape, ctx);
|
|
515
|
-
});
|
|
603
|
+
if (!Array.isArray(value)) throw new PackDecodeError("expected array for batched shape", []);
|
|
604
|
+
return value.map((elem, idx) => decodeByShapeFromDef(elem, def, schema, shape[idx], ctx));
|
|
516
605
|
}
|
|
517
606
|
if (shape === "list") {
|
|
518
|
-
if (!Array.isArray(value)) throw new
|
|
519
|
-
return value.map((elem, idx) =>
|
|
607
|
+
if (!Array.isArray(value)) throw new PackDecodeError("expected array for list shape", []);
|
|
608
|
+
return value.map((elem, idx) => decodePositionalFromFieldDef(elem, def, schema, ctx, [`[${idx}]`]));
|
|
520
609
|
}
|
|
521
|
-
return
|
|
610
|
+
return decodePositionalFromFieldDef(value, def, schema, ctx);
|
|
522
611
|
}
|
|
523
612
|
function resolveFieldMap(schema, typeName, path) {
|
|
524
|
-
return resolveFieldMap$2(schema, typeName, path, (message, p) => new
|
|
613
|
+
return resolveFieldMap$2(schema, typeName, path, (message, p) => new PackDecodeError(message, p));
|
|
525
614
|
}
|
|
526
|
-
|
|
615
|
+
/**
|
|
616
|
+
* Decide whether `value` is a single tuple or a list of tuples based on its
|
|
617
|
+
* shape plus the FieldInput — used only by the top-level `decodePositional`
|
|
618
|
+
* helper (the envelope decoder uses `shapes[key]` directly). Heuristic:
|
|
619
|
+
* - null → null
|
|
620
|
+
* - Array where the first element is itself an array → list of rows
|
|
621
|
+
* - Array otherwise → single row (tuple)
|
|
622
|
+
*
|
|
623
|
+
* This heuristic is the ambiguity §7.2 calls out, so callers SHOULD go through
|
|
624
|
+
* `decodeEnvelope` to use explicit shape metadata.
|
|
625
|
+
*/
|
|
626
|
+
function decodeValueByArity(value, fields, fieldMap, schema, path, ctx) {
|
|
627
|
+
if (value === null || value === void 0) return null;
|
|
628
|
+
if (!Array.isArray(value)) throw new PackDecodeError("expected array (tuple) for positional row", path);
|
|
629
|
+
const plan = getRowPlan(fields, fieldMap);
|
|
630
|
+
if (value.length > 0 && Array.isArray(value[0])) return value.map((elem) => decodeRowWithPlan(elem, plan, schema, path, ctx));
|
|
631
|
+
return decodeRowWithPlan(value, plan, schema, path, ctx);
|
|
632
|
+
}
|
|
633
|
+
const DECODE_PLAN_CACHE = /* @__PURE__ */ new WeakMap();
|
|
527
634
|
function getRowPlan(fields, fieldMap) {
|
|
528
|
-
return getRowPlan$2(
|
|
635
|
+
return getRowPlan$2(DECODE_PLAN_CACHE, fields, fieldMap);
|
|
529
636
|
}
|
|
530
|
-
function
|
|
531
|
-
if (
|
|
532
|
-
if (
|
|
533
|
-
const
|
|
637
|
+
function decodeRowWithPlan(tuple, plan, schema, path, ctx) {
|
|
638
|
+
if (tuple === null || tuple === void 0) return null;
|
|
639
|
+
if (!Array.isArray(tuple)) throw new PackDecodeError("expected array (tuple) for row", path);
|
|
640
|
+
const out = {};
|
|
534
641
|
const len = plan.length;
|
|
535
|
-
const tuple = new Array(len);
|
|
536
642
|
for (let i = 0; i < len; i++) {
|
|
537
643
|
const entry = plan[i];
|
|
538
644
|
const field = entry[0];
|
|
539
645
|
const selection = entry[1];
|
|
540
646
|
const def = entry[2];
|
|
541
|
-
const
|
|
647
|
+
const cell = tuple[i] ?? null;
|
|
648
|
+
const slot = entry[3];
|
|
649
|
+
if (slot?.kind === "truncated" && cell === null) continue;
|
|
542
650
|
path.push(field);
|
|
543
|
-
|
|
651
|
+
out[field] = slot === void 0 ? decodeField(cell, selection, def, schema, path, ctx) : decodeWalkSlot(cell, slot, schema, path, ctx);
|
|
544
652
|
path.pop();
|
|
545
653
|
}
|
|
546
|
-
return
|
|
654
|
+
return out;
|
|
547
655
|
}
|
|
548
|
-
function
|
|
549
|
-
if (
|
|
550
|
-
|
|
551
|
-
|
|
656
|
+
function decodeWalkSlot(cell, slot, schema, path, ctx) {
|
|
657
|
+
if (cell === null || cell === void 0) return null;
|
|
658
|
+
switch (slot.kind) {
|
|
659
|
+
case "truncated": return cell;
|
|
660
|
+
case "agg":
|
|
661
|
+
if (!Array.isArray(cell)) throw new PackDecodeError("expected array for _agg", path);
|
|
662
|
+
return Object.fromEntries(slot.names.map((n, i) => [n, cell[i] ?? null]));
|
|
663
|
+
case "nested": {
|
|
664
|
+
if (!Array.isArray(cell)) throw new PackDecodeError("expected array for nested selection", path);
|
|
665
|
+
const plan = getRowPlan(slot.fields, resolveFieldMap(schema, slot.type, path));
|
|
666
|
+
return cell.map((tuple, idx) => {
|
|
667
|
+
path.push(`[${idx}]`);
|
|
668
|
+
const out = decodeRowWithPlan(tuple, plan, schema, path, ctx);
|
|
669
|
+
path.pop();
|
|
670
|
+
return out;
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
default: return assertNever(slot);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
function decodeField(cell, selection, def, schema, path, ctx) {
|
|
677
|
+
if (cell === null || cell === void 0) return null;
|
|
678
|
+
if (def === void 0) return cell;
|
|
679
|
+
if (selection === true) return decodeLeaf(cell, def, path);
|
|
552
680
|
switch (def.kind) {
|
|
553
|
-
case "scalar": throw new
|
|
554
|
-
case "struct": return
|
|
555
|
-
case "list": return
|
|
556
|
-
case "crossRef": return
|
|
681
|
+
case "scalar": throw new PackDecodeError(`field is a scalar (${def.type}); cannot select sub-fields`, path);
|
|
682
|
+
case "struct": return decodeStruct(cell, selection, def, schema, path, ctx);
|
|
683
|
+
case "list": return decodeList(cell, selection, def, schema, path, ctx);
|
|
684
|
+
case "crossRef": return decodeCrossRef(cell, selection, def, schema, path, ctx);
|
|
557
685
|
default: return assertNever(def);
|
|
558
686
|
}
|
|
559
687
|
}
|
|
560
|
-
function
|
|
688
|
+
function decodeLeaf(cell, def, path) {
|
|
561
689
|
switch (def.kind) {
|
|
562
|
-
case "scalar": return
|
|
690
|
+
case "scalar": return cell;
|
|
563
691
|
case "list":
|
|
564
|
-
if (!Array.isArray(
|
|
565
|
-
return
|
|
692
|
+
if (!Array.isArray(cell)) throw new PackDecodeError("expected array for list leaf", path);
|
|
693
|
+
return cell;
|
|
566
694
|
case "struct":
|
|
567
|
-
case "crossRef": return
|
|
695
|
+
case "crossRef": return cell;
|
|
568
696
|
default: return assertNever(def);
|
|
569
697
|
}
|
|
570
698
|
}
|
|
571
|
-
function
|
|
572
|
-
return
|
|
699
|
+
function decodeStruct(cell, selection, def, schema, path, ctx) {
|
|
700
|
+
return decodeRowWithPlan(cell, getRowPlan(selection, def.fields), schema, path, ctx);
|
|
573
701
|
}
|
|
574
|
-
function
|
|
575
|
-
if (!Array.isArray(
|
|
702
|
+
function decodeList(cell, selection, def, schema, path, ctx) {
|
|
703
|
+
if (!Array.isArray(cell)) throw new PackDecodeError("expected array for list field", path);
|
|
576
704
|
const inner = def.of;
|
|
577
705
|
const innerPlan = inner.kind === "struct" ? getRowPlan(selection, inner.fields) : void 0;
|
|
578
706
|
const out = [];
|
|
579
|
-
for (let idx = 0; idx <
|
|
580
|
-
const elem =
|
|
707
|
+
for (let idx = 0; idx < cell.length; idx++) {
|
|
708
|
+
const elem = cell[idx];
|
|
581
709
|
if (elem === null || elem === void 0) {
|
|
582
710
|
out.push(null);
|
|
583
711
|
continue;
|
|
584
712
|
}
|
|
585
713
|
path.push(`[${idx}]`);
|
|
586
|
-
out.push(
|
|
714
|
+
out.push(decodeListElement(elem, selection, inner, schema, path, ctx, innerPlan));
|
|
587
715
|
path.pop();
|
|
588
716
|
}
|
|
589
717
|
return out;
|
|
590
718
|
}
|
|
591
|
-
function
|
|
719
|
+
function decodeListElement(elem, selection, inner, schema, elemPath, ctx, structPlan) {
|
|
592
720
|
switch (inner.kind) {
|
|
593
|
-
case "scalar": throw new
|
|
594
|
-
case "struct": return
|
|
595
|
-
case "list": return
|
|
596
|
-
case "crossRef": return
|
|
721
|
+
case "scalar": throw new PackDecodeError(`list element is scalar (${inner.type}); cannot select sub-fields`, elemPath);
|
|
722
|
+
case "struct": return decodeRowWithPlan(elem, structPlan ?? getRowPlan(selection, inner.fields), schema, elemPath, ctx);
|
|
723
|
+
case "list": return decodeList(elem, selection, inner, schema, elemPath, ctx);
|
|
724
|
+
case "crossRef": return decodeCrossRef(elem, selection, inner, schema, elemPath, ctx);
|
|
597
725
|
default: return assertNever(inner);
|
|
598
726
|
}
|
|
599
727
|
}
|
|
600
|
-
function
|
|
601
|
-
if (def.ns === ctx.currentNs) return
|
|
728
|
+
function decodeCrossRef(cell, selection, def, schema, path, ctx) {
|
|
729
|
+
if (def.ns === ctx.currentNs) return decodeRowWithPlan(cell, getRowPlan(selection, resolveFieldMap(schema, def.type, path)), schema, path, ctx);
|
|
602
730
|
const schemas = ctx.schemas;
|
|
603
|
-
if (schemas === void 0) throw new
|
|
731
|
+
if (schemas === void 0) throw new PackDecodeError(`cross-namespace crossRef '${def.ns}/${def.type}' requires ctx.schemas`, path);
|
|
604
732
|
const targetSchema = schemas[def.ns];
|
|
605
|
-
if (targetSchema === void 0) throw new
|
|
606
|
-
return
|
|
733
|
+
if (targetSchema === void 0) throw new PackDecodeError(`cross-namespace crossRef target namespace '${def.ns}' not in ctx.schemas`, path);
|
|
734
|
+
return decodeRowWithPlan(cell, getRowPlan(selection, resolveFieldMap(targetSchema, def.type, path)), targetSchema, path, {
|
|
607
735
|
...ctx,
|
|
608
736
|
currentNs: def.ns
|
|
609
737
|
});
|
|
610
738
|
}
|
|
611
|
-
function
|
|
739
|
+
function decodeFieldDef(value, def, schema, path, ctx) {
|
|
612
740
|
if (value === null || value === void 0) return null;
|
|
613
741
|
switch (def.kind) {
|
|
614
|
-
case "scalar": return
|
|
615
|
-
case "struct": return
|
|
616
|
-
case "list": return
|
|
617
|
-
case "crossRef": return
|
|
742
|
+
case "scalar": return value;
|
|
743
|
+
case "struct": return decodeStructFromDef(value, def, schema, path, ctx);
|
|
744
|
+
case "list": return decodeListFromDef(value, def, schema, path, ctx);
|
|
745
|
+
case "crossRef": return value;
|
|
618
746
|
default: return assertNever(def);
|
|
619
747
|
}
|
|
620
748
|
}
|
|
621
|
-
const
|
|
749
|
+
const DECODE_FIELDDEF_PLAN_CACHE = /* @__PURE__ */ new WeakMap();
|
|
622
750
|
function getFieldDefPlan(fields) {
|
|
623
|
-
return getFieldDefPlan$2(
|
|
751
|
+
return getFieldDefPlan$2(DECODE_FIELDDEF_PLAN_CACHE, fields);
|
|
624
752
|
}
|
|
625
|
-
function
|
|
626
|
-
if (
|
|
627
|
-
const
|
|
753
|
+
function decodeStructFromDef(value, def, schema, path, ctx) {
|
|
754
|
+
if (!Array.isArray(value)) throw new PackDecodeError("expected array (tuple) for struct", path);
|
|
755
|
+
const out = {};
|
|
628
756
|
const plan = getFieldDefPlan(def.fields);
|
|
629
757
|
const len = plan.length;
|
|
630
|
-
const tuple = new Array(len);
|
|
631
758
|
for (let i = 0; i < len; i++) {
|
|
632
759
|
const entry = plan[i];
|
|
633
760
|
const fieldName = entry[0];
|
|
634
761
|
const sub = entry[1];
|
|
635
|
-
const cell =
|
|
636
|
-
if (cell === void 0) {
|
|
637
|
-
tuple[i] = null;
|
|
638
|
-
continue;
|
|
639
|
-
}
|
|
762
|
+
const cell = value[i] ?? null;
|
|
640
763
|
path.push(fieldName);
|
|
641
|
-
|
|
764
|
+
out[fieldName] = decodeFieldDef(cell, sub, schema, path, ctx);
|
|
642
765
|
path.pop();
|
|
643
766
|
}
|
|
644
|
-
return
|
|
767
|
+
return out;
|
|
645
768
|
}
|
|
646
|
-
function
|
|
647
|
-
if (!Array.isArray(value)) throw new
|
|
769
|
+
function decodeListFromDef(value, def, schema, path, ctx) {
|
|
770
|
+
if (!Array.isArray(value)) throw new PackDecodeError("expected array for list field", path);
|
|
648
771
|
const out = [];
|
|
649
772
|
for (let idx = 0; idx < value.length; idx++) {
|
|
650
773
|
const elem = value[idx];
|
|
@@ -653,18 +776,11 @@ function encodeListFromDef(value, def, schema, path, ctx) {
|
|
|
653
776
|
continue;
|
|
654
777
|
}
|
|
655
778
|
path.push(`[${idx}]`);
|
|
656
|
-
out.push(
|
|
779
|
+
out.push(decodeFieldDef(elem, def.of, schema, path, ctx));
|
|
657
780
|
path.pop();
|
|
658
781
|
}
|
|
659
782
|
return out;
|
|
660
783
|
}
|
|
661
|
-
/**
|
|
662
|
-
* Identity today — msgpackr round-trips Dates natively. Kept as the single
|
|
663
|
-
* choke point for any future scalar normalisation.
|
|
664
|
-
*/
|
|
665
|
-
function coerceScalar(value) {
|
|
666
|
-
return value;
|
|
667
|
-
}
|
|
668
784
|
|
|
669
785
|
//#endregion
|
|
670
786
|
//#region ../../packages/substrate-pack/src/stream-chunk.ts
|
|
@@ -845,4 +961,4 @@ function applyDecodePlanInPlace(envelope, plan) {
|
|
|
845
961
|
const WS_SUBPROTOCOL_SUBSTRATE_PACK = "substrate-pack";
|
|
846
962
|
|
|
847
963
|
//#endregion
|
|
848
|
-
export {
|
|
964
|
+
export { formatQualifiedKey as C, assertNever as E, naturalOrderOf as S, DEFAULT_FILTER_WRITE_LIMITS as T, encodeEnvelope as _, buildDecodePlan as a, DEFAULT_ORDERED_BY as b, isPlainRequestData as c, PackDecodeError as d, SchemaDriftError as f, PackEncodeError as g, decodePositionalFromFieldDef as h, applyDecodePlanInPlace as i, decodeStreamChunk as l, decodePositional as m, packFrame as n, decodeWsEnvelope as o, decodeEnvelope as p, unpackFrame as r, encodeWsFrame as s, WS_SUBPROTOCOL_SUBSTRATE_PACK as t, encodeStreamChunk as u, encodePositional as v, parseQualifiedKey as w, isColumnField as x, encodePositionalFromFieldDef as y };
|