@mnmal-ai/substrate 1.0.0-alpha.18 → 1.0.0-alpha.19

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 (40) hide show
  1. package/README.md +4 -4
  2. package/dist/{boot-store-C8KrACZ5.js → boot-store-DL2Wkzun.js} +191 -268
  3. package/dist/{bridge-5VHZiH9o.d.ts → bridge-BhiJKwfF.d.ts} +1 -1
  4. package/dist/{cli-zZ7nHhY5.js → cli-BYyPsfdY.js} +25 -28
  5. package/dist/client.browser.js +2478 -0
  6. package/dist/client.d.ts +176 -164
  7. package/dist/client.js +5 -5
  8. package/dist/{errors-DHgT7Fa8.d.ts → errors-CYOaWZ8l.d.ts} +1 -1
  9. package/dist/{index-DKJXQ031.d.ts → index-rW7fu2Af.d.ts} +3 -11
  10. package/dist/{node-version-CxqcqECU.js → node-version-BlYrbY2I.js} +8 -1
  11. package/dist/pack.d.ts +59 -11
  12. package/dist/pack.js +2 -2
  13. package/dist/{pg-edges-store-D7etFxNK.js → pg-edges-store-B0Krqoac.js} +1 -1
  14. package/dist/{plugin-DcoJGEMM.js → plugin-Dk9h__NU.js} +2 -2
  15. package/dist/proposals-bootstrap.js +2 -2
  16. package/dist/proposals.d.ts +2 -2
  17. package/dist/proposals.js +1 -1
  18. package/dist/recall-bootstrap.js +2 -2
  19. package/dist/recall.d.ts +1 -1
  20. package/dist/recall.js +2 -2
  21. package/dist/{repl-SY8cLOZl.js → repl-D6_CBsue.js} +1 -1
  22. package/dist/{row-DHxnCnhA.d.ts → row-CfsHSEfB.d.ts} +4 -4
  23. package/dist/{run-Cr-53N3F.js → run-BJMgCcCC.js} +18 -9
  24. package/dist/{schema-B5WBO8yh.d.ts → schema-laGLMvMC.d.ts} +15 -40
  25. package/dist/{script-B-32wZGY.js → script-DgXaJY1Y.js} +1 -1
  26. package/dist/sdk.d.ts +6 -6
  27. package/dist/sdk.js +3 -3
  28. package/dist/{src-CdbUzLhB.js → src-B6nsPVmD.js} +201 -25
  29. package/dist/{src-DV6PcXym.js → src-B_oTGi3X.js} +648 -845
  30. package/dist/{src-BcuotITf.js → src-BcigMQdR.js} +7 -4
  31. package/dist/{src-DR_hUj2p.js → src-D3ACIp1y.js} +1 -1
  32. package/dist/{src-B4dTiEWw.js → src-Dlmuq32R.js} +3 -3
  33. package/dist/{src-DZfgQ2gR.js → src-DreZjBk7.js} +3 -3
  34. package/dist/{src-D55T3ZcP.js → src-oO0DtS6y.js} +313 -887
  35. package/dist/substrate-admin.js +2 -2
  36. package/dist/substrate.js +2 -2
  37. package/dist/testing.d.ts +13 -47
  38. package/dist/testing.js +6 -167
  39. package/dist/{ws-BL9nSELO.d.ts → ws-BT8kL1IF.d.ts} +4 -4
  40. package/package.json +21 -23
@@ -470,14 +470,22 @@ function encodePositionalForShape(value, def, schema, shape, ctx) {
470
470
  if (!Array.isArray(value)) throw new PackEncodeError("expected array for batched shape", []);
471
471
  return value.map((elem, idx) => {
472
472
  const perFrameShape = shape[idx];
473
- return encodePositionalForShape(elem, def, schema, perFrameShape, ctx);
473
+ return encodePositionalForShape(elem, frameDef(def, idx), schema, perFrameShape, ctx);
474
474
  });
475
475
  }
476
- if (shape === "list") {
476
+ const one = frameDef(def, 0);
477
+ if (shape === "list" && one.kind !== "list") {
477
478
  if (!Array.isArray(value)) throw new PackEncodeError("expected array for list shape", []);
478
- return value.map((elem, idx) => encodePositionalFromFieldDef(elem, def, schema, ctx, [`[${idx}]`]));
479
+ return value.map((elem, idx) => encodePositionalFromFieldDef(elem, one, schema, ctx, [`[${idx}]`]));
479
480
  }
480
- return encodePositionalFromFieldDef(value, def, schema, ctx);
481
+ return encodePositionalFromFieldDef(value, one, schema, ctx);
482
+ }
483
+ /** Frame `idx`'s return shape: its own entry for a batched key, else the key's one shape. */
484
+ function frameDef(def, idx) {
485
+ if (!Array.isArray(def)) return def;
486
+ const one = def[idx];
487
+ if (one === void 0) throw new PackEncodeError(`no return shape for frame ${idx}`, []);
488
+ return one;
481
489
  }
482
490
  function resolveFieldMap$1(schema, typeName, path) {
483
491
  return resolveFieldMap$2(schema, typeName, path, (message, p) => new PackEncodeError(message, p));
@@ -709,7 +717,7 @@ function decodePositionalFromFieldDef(encoded, def, schema, ctx, path = []) {
709
717
  return decodeFieldDef(encoded, def, schema, path, ctx);
710
718
  }
711
719
  function decodeEnvelope(bytes, ctx) {
712
- const raw = unpack(Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes));
720
+ const raw = unpack(bytes);
713
721
  if (ctx.expectedSchemaVersion !== void 0 && raw.schemaVersion !== ctx.expectedSchemaVersion) throw new SchemaDriftError(raw.schemaVersion, ctx.expectedSchemaVersion);
714
722
  const rewritten = {};
715
723
  for (const [key, value] of Object.entries(raw.data)) {
@@ -774,13 +782,14 @@ function decodeByShapeFromDef(value, def, schema, shape, ctx) {
774
782
  if (value === null || value === void 0) return null;
775
783
  if (Array.isArray(shape)) {
776
784
  if (!Array.isArray(value)) throw new PackDecodeError("expected array for batched shape", []);
777
- return value.map((elem, idx) => decodeByShapeFromDef(elem, def, schema, shape[idx], ctx));
785
+ return value.map((elem, idx) => decodeByShapeFromDef(elem, frameDef(def, idx), schema, shape[idx], ctx));
778
786
  }
779
- if (shape === "list") {
787
+ const one = frameDef(def, 0);
788
+ if (shape === "list" && one.kind !== "list") {
780
789
  if (!Array.isArray(value)) throw new PackDecodeError("expected array for list shape", []);
781
- return value.map((elem, idx) => decodePositionalFromFieldDef(elem, def, schema, ctx, [`[${idx}]`]));
790
+ return value.map((elem, idx) => decodePositionalFromFieldDef(elem, one, schema, ctx, [`[${idx}]`]));
782
791
  }
783
- return decodePositionalFromFieldDef(value, def, schema, ctx);
792
+ return decodePositionalFromFieldDef(value, one, schema, ctx);
784
793
  }
785
794
  function resolveFieldMap(schema, typeName, path) {
786
795
  return resolveFieldMap$2(schema, typeName, path, (message, p) => new PackDecodeError(message, p));
@@ -955,6 +964,43 @@ function decodeListFromDef(value, def, schema, path, ctx) {
955
964
  return out;
956
965
  }
957
966
 
967
+ //#endregion
968
+ //#region ../../packages/substrate-pack/src/mutation-returns.ts
969
+ /** What a row-writing mutation answers when the frame names no `fields`. */
970
+ const MUTATION_ACK_FIELDS = {
971
+ id: true,
972
+ shortId: true
973
+ };
974
+ /** The `returns` a mutation's result is positioned by, given the frame's `fields` (`true` selects everything). */
975
+ function mutationReturns(mutation, fields) {
976
+ if (fields === true) return mutation.returns;
977
+ const selection = fields ?? (mutation.writesRows === true ? MUTATION_ACK_FIELDS : void 0);
978
+ return selection === void 0 ? mutation.returns : narrow(mutation.returns, selection);
979
+ }
980
+ /**
981
+ * `def` narrowed to the selection, in declared order: a list narrows its
982
+ * element, and a nested selection narrows a nested struct, as the JSON
983
+ * projection recurses. A selected field the struct does not declare is
984
+ * dropped, as the JSON projection drops a field the row does not carry.
985
+ */
986
+ function narrow(def, selection) {
987
+ if (def.kind === "list") return {
988
+ ...def,
989
+ of: narrow(def.of, selection)
990
+ };
991
+ if (def.kind !== "struct") return def;
992
+ const fields = {};
993
+ for (const [name, field] of Object.entries(def.fields)) {
994
+ const sub = selection[name];
995
+ if (sub === void 0) continue;
996
+ fields[name] = typeof sub === "object" && sub !== null ? narrow(field, sub) : field;
997
+ }
998
+ return {
999
+ ...def,
1000
+ fields
1001
+ };
1002
+ }
1003
+
958
1004
  //#endregion
959
1005
  //#region ../../packages/substrate-pack/src/stream-chunk.ts
960
1006
  /**
@@ -983,7 +1029,7 @@ function encodeStreamChunk(input) {
983
1029
  * `_meta` round-trip unchanged.
984
1030
  */
985
1031
  function decodeStreamChunk(bytes, input) {
986
- const raw = unpack(Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes));
1032
+ const raw = unpack(bytes);
987
1033
  const decoded = decodePositional(raw.data, input.fields, input.schema, input.typeName, {
988
1034
  currentNs: input.currentNs,
989
1035
  ...input.schemas !== void 0 ? { schemas: input.schemas } : {}
@@ -1003,25 +1049,24 @@ function decodeStreamChunk(bytes, input) {
1003
1049
  //#endregion
1004
1050
  //#region ../../packages/substrate-pack/src/wire.ts
1005
1051
  /**
1006
- * WS-frame wire codec. Spec §8.5.
1052
+ * Frame wire codec. Spec §8.5.
1007
1053
  *
1008
- * Centralizes JSON-vs-pack envelope handling for WebSocket transports plus the
1054
+ * Centralizes JSON-vs-pack envelope handling for frame streams plus the
1009
1055
  * precomputed decode plan that turns positional `data` tuples back into named
1010
1056
  * objects. The plan is built once per subscription, so per-frame work is
1011
1057
  * O(plan entries). Shared by substrate-client and the perf harness so both
1012
1058
  * measure the same codepath.
1013
1059
  */
1014
1060
  /**
1015
- * Encode an outbound WS frame. Pack mode returns msgpack bytes; JSON mode
1016
- * returns stringified text. The `mode` argument is the wire-format discriminant
1017
- * — call sites can route the result to the right `WebSocket.send` overload via
1018
- * a typed `typeof wire === 'string'` branch.
1061
+ * Encode an outbound frame. Pack mode returns msgpack bytes; JSON mode
1062
+ * returns stringified text. The `mode` argument is the wire-format discriminant;
1063
+ * callers branch on `typeof wire === 'string'`.
1019
1064
  */
1020
1065
  function encodeWsFrame(value, mode) {
1021
1066
  return mode === "pack" ? pack(value) : stringifyJson(value);
1022
1067
  }
1023
1068
  /**
1024
- * Decode an inbound WS message envelope (the outer `{ id, kind, data?, ... }`
1069
+ * Decode an inbound frame envelope (the outer `{ id, kind, data?, ... }`
1025
1070
  * shape — positional `data` reconstruction is a separate concern handled by
1026
1071
  * `applyDecodePlanInPlace`).
1027
1072
  *
@@ -1037,7 +1082,7 @@ function encodeWsFrame(value, mode) {
1037
1082
  function decodeWsEnvelope(raw, mode) {
1038
1083
  if (mode === "pack") return inbound.unpack(toBytes(raw));
1039
1084
  if (typeof raw === "string") return JSON.parse(raw);
1040
- return JSON.parse(Buffer.from(toBytes(raw)).toString("utf8"));
1085
+ return JSON.parse(new TextDecoder().decode(toBytes(raw)));
1041
1086
  }
1042
1087
  /**
1043
1088
  * Inbound frames come from untrusted peers: structured-clone references (and
@@ -1069,7 +1114,7 @@ function isPlainRequestData(value, depth = 0) {
1069
1114
  function toBytes(raw) {
1070
1115
  if (raw instanceof Uint8Array) return raw;
1071
1116
  if (raw instanceof ArrayBuffer) return new Uint8Array(raw);
1072
- return Buffer.from(raw);
1117
+ return new Uint8Array(raw);
1073
1118
  }
1074
1119
  /**
1075
1120
  * Build the decode plan from a frame spec map + federated schemas. Called
@@ -1125,13 +1170,144 @@ function applyDecodePlanInPlace(envelope, plan) {
1125
1170
  }
1126
1171
 
1127
1172
  //#endregion
1128
- //#region ../../packages/substrate-pack/src/index.ts
1173
+ //#region ../../packages/substrate-pack/src/stream-frames.ts
1174
+ /**
1175
+ * Length-prefixed stream frames: the body of an HTTP/2 subscription or stream.
1176
+ *
1177
+ * Each frame is a big-endian u32 payload length, then the payload: UTF-8 JSON
1178
+ * or msgpack, exactly what `encodeWsFrame` produces, so
1179
+ * the positional pack decode plan applies unchanged. A zero-length frame is a
1180
+ * heartbeat and carries nothing.
1181
+ */
1182
+ const FRAMES_JSON_MIME = "application/vnd.substrate.frames+json";
1183
+ const FRAMES_PACK_MIME = "application/vnd.substrate.frames+pack";
1184
+ /** Largest payload a decoder accepts unless told otherwise. */
1185
+ const DEFAULT_MAX_FRAME_BYTES = 16777216;
1186
+ /** A frame of length 0: keeps an idle stream alive, and decoders skip it. */
1187
+ const HEARTBEAT_FRAME = /* @__PURE__ */ new Uint8Array(4);
1188
+ var FrameStreamError = class extends Error {
1189
+ code;
1190
+ constructor(code, message) {
1191
+ super(message);
1192
+ this.name = "FrameStreamError";
1193
+ this.code = code;
1194
+ }
1195
+ };
1196
+ /** A little over three of the server's 20 s heartbeats. */
1197
+ const DEFAULT_STREAM_IDLE_MS = 65e3;
1129
1198
  /**
1130
- * WebSocket subprotocol advertised in `Sec-WebSocket-Protocol` at handshake
1131
- * time. Connections negotiating this subprotocol receive msgpack frames with
1132
- * positionally-encoded `data`; anything else falls back to JSON.
1199
+ * `body`, failing with `stream_idle` when a read waits `ms` for bytes,
1200
+ * heartbeats included. It times only reads the consumer is waiting on, so a
1201
+ * slow consumer never trips it; a connection that died without closing does.
1202
+ * The underlying reader is cancelled on timeout, which settles its pending read.
1133
1203
  */
1134
- const WS_SUBPROTOCOL_SUBSTRATE_PACK = "substrate-pack";
1204
+ function withIdleTimeout(body, ms) {
1205
+ const reader = body.getReader();
1206
+ return new ReadableStream({
1207
+ async pull(controller) {
1208
+ let timer;
1209
+ const idle = new Promise((_, reject) => {
1210
+ timer = setTimeout(() => reject(new FrameStreamError("stream_idle", `the stream sent nothing for ${ms} ms`)), ms);
1211
+ });
1212
+ try {
1213
+ const r = await Promise.race([reader.read(), idle]);
1214
+ if (r.done) controller.close();
1215
+ else controller.enqueue(r.value);
1216
+ } catch (err) {
1217
+ reader.cancel().catch(() => {});
1218
+ controller.error(err);
1219
+ } finally {
1220
+ clearTimeout(timer);
1221
+ }
1222
+ },
1223
+ cancel(reason) {
1224
+ return reader.cancel(reason);
1225
+ }
1226
+ }, { highWaterMark: 0 });
1227
+ }
1228
+ /** The frames encoding an `Accept` header asks for, or undefined when it asks for none. */
1229
+ function framesEncodingFromAccept(accept) {
1230
+ if (accept === void 0) return void 0;
1231
+ for (const clause of accept.split(",")) {
1232
+ const [type = "", ...params] = clause.split(";");
1233
+ if (params.some((p) => /^\s*q\s*=\s*0(\.0*)?\s*$/i.test(p))) continue;
1234
+ const mime = type.trim().toLowerCase();
1235
+ if (mime === "application/vnd.substrate.frames+pack") return "pack";
1236
+ if (mime === "application/vnd.substrate.frames+json") return "json";
1237
+ }
1238
+ }
1239
+ const utf8 = new TextEncoder();
1240
+ /**
1241
+ * One frame: u32 BE length, then `value` as JSON or msgpack.
1242
+ *
1243
+ * @throws FrameStreamError `frame_too_large` when the payload is over `maxFrameBytes`,
1244
+ * which a decoder with the same limit would refuse
1245
+ */
1246
+ function encodeStreamFrame(value, mode, maxFrameBytes = DEFAULT_MAX_FRAME_BYTES) {
1247
+ const raw = encodeWsFrame(value, mode);
1248
+ const payload = typeof raw === "string" ? utf8.encode(raw) : raw;
1249
+ if (payload.length > maxFrameBytes) throw new FrameStreamError("frame_too_large", `frame of ${payload.length} bytes exceeds the ${maxFrameBytes}-byte limit`);
1250
+ const out = new Uint8Array(4 + payload.length);
1251
+ new DataView(out.buffer).setUint32(0, payload.length, false);
1252
+ out.set(payload, 4);
1253
+ return out;
1254
+ }
1255
+ /**
1256
+ * The payloads of a frame stream, in order, heartbeats skipped. A length above
1257
+ * `maxFrameBytes` throws `frame_too_large` before any of that payload is read
1258
+ * or allocated; a body that ends inside a frame throws `truncated_frame`.
1259
+ */
1260
+ async function* decodeStreamFrames(body, opts = {}) {
1261
+ const max = opts.maxFrameBytes ?? 16777216;
1262
+ if (!Number.isInteger(max) || max < 0) throw new RangeError(`maxFrameBytes must be a non-negative integer, got ${max}`);
1263
+ let buf = /* @__PURE__ */ new Uint8Array(4096);
1264
+ let start = 0;
1265
+ let end = 0;
1266
+ const append = (chunk) => {
1267
+ if (end + chunk.length > buf.length) {
1268
+ const live = end - start;
1269
+ if (live + chunk.length > buf.length) {
1270
+ const grown = new Uint8Array(Math.max(buf.length * 2, live + chunk.length));
1271
+ grown.set(buf.subarray(start, end), 0);
1272
+ buf = grown;
1273
+ } else buf.copyWithin(0, start, end);
1274
+ start = 0;
1275
+ end = live;
1276
+ }
1277
+ buf.set(chunk, end);
1278
+ end += chunk.length;
1279
+ };
1280
+ const source = typeof body[Symbol.asyncIterator] === "function" ? body : readableToIterable(body);
1281
+ for await (const chunk of source) {
1282
+ append(chunk);
1283
+ while (end - start >= 4) {
1284
+ const len = new DataView(buf.buffer, buf.byteOffset + start, 4).getUint32(0, false);
1285
+ if (len > max) throw new FrameStreamError("frame_too_large", `frame of ${len} bytes exceeds the ${max}-byte limit`);
1286
+ if (end - start < 4 + len) break;
1287
+ if (len > 0) yield buf.slice(start + 4, start + 4 + len);
1288
+ start += 4 + len;
1289
+ }
1290
+ }
1291
+ if (end - start > 0) throw new FrameStreamError("truncated_frame", `stream ended inside a frame (${end - start} bytes left over)`);
1292
+ }
1293
+ /** A ReadableStream without async iteration (Safari), cancelled when the consumer stops early. */
1294
+ async function* readableToIterable(stream) {
1295
+ const reader = stream.getReader();
1296
+ let finished = false;
1297
+ try {
1298
+ for (;;) {
1299
+ const { done, value } = await reader.read();
1300
+ if (done) {
1301
+ finished = true;
1302
+ return;
1303
+ }
1304
+ yield value;
1305
+ }
1306
+ } finally {
1307
+ if (!finished) await reader.cancel().catch(() => {});
1308
+ reader.releaseLock();
1309
+ }
1310
+ }
1135
1311
 
1136
1312
  //#endregion
1137
- export { parseInt64 as A, DEFAULT_ORDERED_BY as C, parseQualifiedKey as D, formatQualifiedKey as E, EXPR_FUNCTIONS as F, EXPR_LIMITS as I, DEFAULT_FILTER_WRITE_LIMITS as L, stringifyJsonExact as M, DATE_UNITS as N, isInt16 as O, EXPR_AGGREGATES as P, assertNever as R, withData as S, naturalOrderOf as T, encodeEnvelope as _, buildDecodePlan as a, packEnvelopeParts as b, isPlainRequestData as c, PackDecodeError as d, SchemaDriftError as f, PackEncodeError as g, decodePositionalFromFieldDef as h, applyDecodePlanInPlace as i, stringifyJson as j, isInt32 as k, 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, isColumnField as w, positionValue as x, encodePositionalFromFieldDef as y };
1313
+ export { encodePositionalFromFieldDef as A, isInt32 as B, SchemaDriftError as C, PackEncodeError as D, decodePositionalFromFieldDef as E, isColumnField as F, EXPR_AGGREGATES as G, stringifyJson as H, naturalOrderOf as I, DEFAULT_FILTER_WRITE_LIMITS as J, EXPR_FUNCTIONS as K, formatQualifiedKey as L, positionValue as M, withData as N, encodeEnvelope as O, DEFAULT_ORDERED_BY as P, parseQualifiedKey as R, PackDecodeError as S, decodePositional as T, stringifyJsonExact as U, parseInt64 as V, DATE_UNITS as W, assertNever as Y, isPlainRequestData as _, FRAMES_JSON_MIME as a, MUTATION_ACK_FIELDS as b, HEARTBEAT_FRAME as c, framesEncodingFromAccept as d, withIdleTimeout as f, encodeWsFrame as g, decodeWsEnvelope as h, DEFAULT_STREAM_IDLE_MS as i, packEnvelopeParts as j, encodePositional as k, decodeStreamFrames as l, buildDecodePlan as m, unpackFrame as n, FRAMES_PACK_MIME as o, applyDecodePlanInPlace as p, EXPR_LIMITS as q, DEFAULT_MAX_FRAME_BYTES as r, FrameStreamError as s, packFrame as t, encodeStreamFrame as u, decodeStreamChunk as v, decodeEnvelope as w, mutationReturns as x, encodeStreamChunk as y, isInt16 as z };