@mnmal-ai/substrate 1.0.0-alpha.17 → 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.
- package/README.md +4 -4
- package/dist/{boot-store-DNU0UfX7.js → boot-store-DL2Wkzun.js} +207 -258
- package/dist/{bridge-5VHZiH9o.d.ts → bridge-BhiJKwfF.d.ts} +1 -1
- package/dist/{cli-CwWUYUMv.js → cli-BYyPsfdY.js} +193 -31
- package/dist/client.browser.js +2478 -0
- package/dist/client.d.ts +181 -164
- package/dist/client.js +5 -5
- package/dist/{errors-DHgT7Fa8.d.ts → errors-CYOaWZ8l.d.ts} +1 -1
- package/dist/{index-DKJXQ031.d.ts → index-rW7fu2Af.d.ts} +3 -11
- package/dist/{node-version-CxqcqECU.js → node-version-BlYrbY2I.js} +8 -1
- package/dist/pack.d.ts +59 -11
- package/dist/pack.js +2 -2
- package/dist/{pg-edges-store-CSJ7sG2l.js → pg-edges-store-B0Krqoac.js} +1 -1
- package/dist/{plugin-Dk9RC3LH.js → plugin-Dk9h__NU.js} +2 -2
- package/dist/proposals-bootstrap.js +2 -2
- package/dist/proposals.d.ts +2 -2
- package/dist/proposals.js +1 -1
- package/dist/recall-bootstrap.js +2 -2
- package/dist/recall.d.ts +1 -1
- package/dist/recall.js +2 -2
- package/dist/{repl-BNnKFG3y.js → repl-D6_CBsue.js} +1 -1
- package/dist/{row-DHxnCnhA.d.ts → row-CfsHSEfB.d.ts} +4 -4
- package/dist/{run-NmwNmVUD.js → run-BJMgCcCC.js} +18 -9
- package/dist/{schema-B5WBO8yh.d.ts → schema-laGLMvMC.d.ts} +15 -40
- package/dist/{script-C6DSa_Na.js → script-DgXaJY1Y.js} +12 -2
- package/dist/script-runner.js +78 -0
- package/dist/sdk.d.ts +9 -8
- package/dist/sdk.js +3 -3
- package/dist/{src-CdbUzLhB.js → src-B6nsPVmD.js} +201 -25
- package/dist/{src-DV6PcXym.js → src-B_oTGi3X.js} +648 -845
- package/dist/{src-DWrFdHrY.js → src-BcigMQdR.js} +7 -4
- package/dist/{src-DR_hUj2p.js → src-D3ACIp1y.js} +1 -1
- package/dist/{src-DwynOVMk.js → src-Dlmuq32R.js} +3 -3
- package/dist/{src-7lp1y4HK.js → src-DreZjBk7.js} +3 -3
- package/dist/{src-BHh6UZ7L.js → src-oO0DtS6y.js} +431 -949
- package/dist/substrate-admin.js +2 -2
- package/dist/substrate.js +2 -2
- package/dist/testing.d.ts +13 -47
- package/dist/testing.js +6 -167
- package/dist/{ws-BL9nSELO.d.ts → ws-BT8kL1IF.d.ts} +4 -4
- package/package.json +21 -23
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
2
|
-
import { A as expandFlat, D as isActiveAdminKeyRow, F as parseGrantEntries, I as decodeList, L as encodeList, M as grantEntryError, P as intersectGrants, S as and, T as SYSTEM_NAMESPACE, d as confersAnywhere, f as coversNamespace, g as checkUpdatable, h as checkRevocable, l as requireNamespace, m as grantedIn, o as denyVerbs, p as grantOf, s as requireCapability, t as applyRowFilters, u as allCapabilities, v as grantEntriesConfer, w as or } from "./src-
|
|
3
|
-
import {
|
|
2
|
+
import { A as expandFlat, D as isActiveAdminKeyRow, F as parseGrantEntries, I as decodeList, L as encodeList, M as grantEntryError, P as intersectGrants, S as and, T as SYSTEM_NAMESPACE, d as confersAnywhere, f as coversNamespace, g as checkUpdatable, h as checkRevocable, l as requireNamespace, m as grantedIn, o as denyVerbs, p as grantOf, s as requireCapability, t as applyRowFilters, u as allCapabilities, v as grantEntriesConfer, w as or } from "./src-D3ACIp1y.js";
|
|
3
|
+
import { B as isInt32, F as isColumnField, G as EXPR_AGGREGATES, H as stringifyJson, I as naturalOrderOf, J as DEFAULT_FILTER_WRITE_LIMITS, K as EXPR_FUNCTIONS, L as formatQualifiedKey, M as positionValue, N as withData, O as encodeEnvelope, R as parseQualifiedKey, V as parseInt64, W as DATE_UNITS, Y as assertNever, a as FRAMES_JSON_MIME, b as MUTATION_ACK_FIELDS, c as HEARTBEAT_FRAME, d as framesEncodingFromAccept, j as packEnvelopeParts, k as encodePositional, o as FRAMES_PACK_MIME, q as EXPR_LIMITS, s as FrameStreamError, u as encodeStreamFrame, x as mutationReturns, z as isInt16 } from "./src-B6nsPVmD.js";
|
|
4
4
|
import { n as acceptsPack, t as PACK_MIME } from "./content-type-C7T28xNb.js";
|
|
5
5
|
import { readFileSync } from "node:fs";
|
|
6
6
|
import { createCipheriv, createDecipheriv, createHash, createHmac, createPrivateKey, createPublicKey, createVerify, hkdfSync, randomBytes, randomUUID, sign, timingSafeEqual, verify } from "node:crypto";
|
|
@@ -727,7 +727,7 @@ function normaliseVocabWhere(where, fieldDefs) {
|
|
|
727
727
|
//#endregion
|
|
728
728
|
//#region ../../packages/substrate-types/src/ws.ts
|
|
729
729
|
/**
|
|
730
|
-
* Terminal-cause vocabulary (docs/
|
|
730
|
+
* Terminal-cause vocabulary (docs/stream-contract.md). The overload
|
|
731
731
|
* token is BOTH the `code` of the terminal `error` frame and the `reason` of
|
|
732
732
|
* the following `end` frame — one token per condition, no second spelling.
|
|
733
733
|
*/
|
|
@@ -1087,7 +1087,7 @@ function rejectionDetail(reason) {
|
|
|
1087
1087
|
}
|
|
1088
1088
|
/**
|
|
1089
1089
|
* The parts of a Key row a verification relied on. A string, so it survives
|
|
1090
|
-
*
|
|
1090
|
+
* a JSON round trip on the identity.
|
|
1091
1091
|
*/
|
|
1092
1092
|
function keyRecordDigest(alg, publicKey, namespaces, capabilities) {
|
|
1093
1093
|
return createHash("sha256").update(JSON.stringify([
|
|
@@ -1200,14 +1200,27 @@ function nestedFields(sel) {
|
|
|
1200
1200
|
function projectEnvelope(envelope, frame) {
|
|
1201
1201
|
const data = {};
|
|
1202
1202
|
for (const [key, value] of Object.entries(envelope.data)) {
|
|
1203
|
-
const
|
|
1204
|
-
data[key] =
|
|
1203
|
+
const project = rowProjector(envelope, frame, key);
|
|
1204
|
+
data[key] = Array.isArray(value) ? value.map(project) : project(value, 0);
|
|
1205
1205
|
}
|
|
1206
1206
|
return {
|
|
1207
1207
|
...envelope,
|
|
1208
1208
|
data
|
|
1209
1209
|
};
|
|
1210
1210
|
}
|
|
1211
|
+
/**
|
|
1212
|
+
* How to project row `i` of `envelope.data[key]`. A batched mutate's element
|
|
1213
|
+
* `i` is frame `i`'s result, so it takes frame `i`'s own `fields`.
|
|
1214
|
+
*/
|
|
1215
|
+
function rowProjector(envelope, frame, key) {
|
|
1216
|
+
const entry = frame[key];
|
|
1217
|
+
if (Array.isArray(entry)) return (row, i) => {
|
|
1218
|
+
const fields = entry[i]?.fields;
|
|
1219
|
+
return fields === void 0 ? row : projectValue(row, fields);
|
|
1220
|
+
};
|
|
1221
|
+
const fields = projectionFields(envelope, frame, key);
|
|
1222
|
+
return (row) => fields === void 0 ? row : projectValue(row, fields);
|
|
1223
|
+
}
|
|
1211
1224
|
/** The selection `envelope.data[key]` is projected through, or undefined when it passes through whole. */
|
|
1212
1225
|
function projectionFields(envelope, frame, key) {
|
|
1213
1226
|
const entry = frame[key];
|
|
@@ -1217,7 +1230,7 @@ function projectionFields(envelope, frame, key) {
|
|
|
1217
1230
|
return entry.fields;
|
|
1218
1231
|
}
|
|
1219
1232
|
/**
|
|
1220
|
-
*
|
|
1233
|
+
* Frame analog of `projectEnvelope` — applies the same row-trimming to the
|
|
1221
1234
|
* `data` payload of a `WsResponseFrame` using the per-key `fieldsByKey`
|
|
1222
1235
|
* map the route layer captures at subscribe / stream time.
|
|
1223
1236
|
*
|
|
@@ -1311,10 +1324,9 @@ function* jsonParts(envelope, frame) {
|
|
|
1311
1324
|
sep = ",";
|
|
1312
1325
|
let dsep = "";
|
|
1313
1326
|
for (const [key, value] of Object.entries(envelope.data)) {
|
|
1314
|
-
const
|
|
1315
|
-
const project = (x) => fields === void 0 ? x : projectValue(x, fields);
|
|
1327
|
+
const project = rowProjector(envelope, frame, key);
|
|
1316
1328
|
if (!Array.isArray(value) || value.length <= ROWS_PER_CHUNK) {
|
|
1317
|
-
const s = stringifyJson(project(value));
|
|
1329
|
+
const s = stringifyJson(Array.isArray(value) ? value.map(project) : project(value, 0));
|
|
1318
1330
|
if (s === void 0) continue;
|
|
1319
1331
|
yield `${dsep}${JSON.stringify(key)}:${s}`;
|
|
1320
1332
|
dsep = ",";
|
|
@@ -1323,7 +1335,7 @@ function* jsonParts(envelope, frame) {
|
|
|
1323
1335
|
yield `${dsep}${JSON.stringify(key)}:[`;
|
|
1324
1336
|
dsep = ",";
|
|
1325
1337
|
for (let i = 0; i < value.length; i += ROWS_PER_CHUNK) {
|
|
1326
|
-
const chunk = stringifyJson(value.slice(i, i + ROWS_PER_CHUNK).map(project));
|
|
1338
|
+
const chunk = stringifyJson(value.slice(i, i + ROWS_PER_CHUNK).map((row, j) => project(row, i + j)));
|
|
1327
1339
|
yield `${i === 0 ? "" : ","}${chunk.slice(1, -1)}`;
|
|
1328
1340
|
}
|
|
1329
1341
|
yield "]";
|
|
@@ -1549,7 +1561,7 @@ function buildEncodeCtx(engine, frame, opts) {
|
|
|
1549
1561
|
//#region ../../packages/substrate-compose/src/routes/whoami.ts
|
|
1550
1562
|
/**
|
|
1551
1563
|
* The caller-facing projection of an `Identity`: `{ id, grants }`. Shared with
|
|
1552
|
-
* the
|
|
1564
|
+
* the `whoami` frame.
|
|
1553
1565
|
*/
|
|
1554
1566
|
function describeIdentity(identity) {
|
|
1555
1567
|
return {
|
|
@@ -1610,7 +1622,7 @@ var HttpBridgeClient = class {
|
|
|
1610
1622
|
constructor(opts) {
|
|
1611
1623
|
this.name = opts.name;
|
|
1612
1624
|
this.baseUrl = opts.baseUrl.endsWith("/") ? opts.baseUrl.slice(0, -1) : opts.baseUrl;
|
|
1613
|
-
this.#fetch = opts.fetch
|
|
1625
|
+
this.#fetch = opts.fetch;
|
|
1614
1626
|
this.#intervalMs = opts.healthCheckIntervalMs ?? DEFAULT_HEALTH_INTERVAL_MS;
|
|
1615
1627
|
this.#auth = opts.auth;
|
|
1616
1628
|
this.#timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
@@ -7197,6 +7209,15 @@ function partitionMutateByTxScope(singleFrameKeys, parsedByKey, datasets) {
|
|
|
7197
7209
|
* short-form numeric ids legitimately miss and are excluded.
|
|
7198
7210
|
*/
|
|
7199
7211
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
7212
|
+
/**
|
|
7213
|
+
* Whether row-filter obligations are already settled for `entry`'s result. A
|
|
7214
|
+
* by-filter mutation's resolver filters the rows it writes, and a delete
|
|
7215
|
+
* returns `{ id, deleted }`, which carries no row values to filter: running a
|
|
7216
|
+
* row predicate on it drops a committed delete to null.
|
|
7217
|
+
*/
|
|
7218
|
+
function resolverFiltered(entry) {
|
|
7219
|
+
return entry.filterTarget !== void 0 || entry.verb === "delete";
|
|
7220
|
+
}
|
|
7200
7221
|
function shapeResult(input) {
|
|
7201
7222
|
const { envelope, key, entry, ctx, result, metric } = input;
|
|
7202
7223
|
const decision = input.resolverFiltered === true ? {
|
|
@@ -7288,12 +7309,17 @@ function hashContent(types, mutations) {
|
|
|
7288
7309
|
mutations
|
|
7289
7310
|
})));
|
|
7290
7311
|
}
|
|
7312
|
+
/** A create or update over a declared type, not a struct of its own (updateMany's `{ count, ids }`). */
|
|
7313
|
+
function writesRows(entry) {
|
|
7314
|
+
return (entry.verb === "create" || entry.verb === "update") && entry.returns?.kind === "struct" && entry.fields !== void 0 && Object.keys(entry.fields).length > 0;
|
|
7315
|
+
}
|
|
7291
7316
|
function publishSchema(registry) {
|
|
7292
7317
|
const types = {};
|
|
7293
7318
|
const mutations = {};
|
|
7294
7319
|
for (const [name, entry] of registry.all()) if (entry.kind === "mutation") mutations[name] = {
|
|
7295
7320
|
params: entry.params,
|
|
7296
|
-
returns: entry.returns ?? UNKNOWN_RETURNS
|
|
7321
|
+
returns: entry.returns ?? UNKNOWN_RETURNS,
|
|
7322
|
+
...writesRows(entry) ? { writesRows: true } : {}
|
|
7297
7323
|
};
|
|
7298
7324
|
else types[name] = entry.orderedBy !== void 0 ? {
|
|
7299
7325
|
fields: entry.fields,
|
|
@@ -9769,7 +9795,7 @@ var Engine = class {
|
|
|
9769
9795
|
decision,
|
|
9770
9796
|
result,
|
|
9771
9797
|
metric: this.#metric,
|
|
9772
|
-
resolverFiltered: registryEntry
|
|
9798
|
+
resolverFiltered: resolverFiltered(registryEntry)
|
|
9773
9799
|
});
|
|
9774
9800
|
return false;
|
|
9775
9801
|
}
|
|
@@ -10136,7 +10162,7 @@ var Engine = class {
|
|
|
10136
10162
|
decision: p.decision,
|
|
10137
10163
|
result: rows[j] ?? null,
|
|
10138
10164
|
metric: this.#metric,
|
|
10139
|
-
resolverFiltered: p.registryEntry
|
|
10165
|
+
resolverFiltered: resolverFiltered(p.registryEntry)
|
|
10140
10166
|
});
|
|
10141
10167
|
const shaped = slotResultOf(scratch, key);
|
|
10142
10168
|
results[i] = shaped;
|
|
@@ -11117,6 +11143,15 @@ function rejectUnknownParams(resolverName, typeName, params, allowed) {
|
|
|
11117
11143
|
});
|
|
11118
11144
|
}
|
|
11119
11145
|
/**
|
|
11146
|
+
* Refuse a caller-supplied value for a `readonly` field, on create and update
|
|
11147
|
+
* alike. shortId is the main case: the engine allocates it.
|
|
11148
|
+
*
|
|
11149
|
+
* @throws ResolverFrameError `readonly_field` naming the first such field
|
|
11150
|
+
*/
|
|
11151
|
+
function assertNoReadonlyFields(params, fieldDefs) {
|
|
11152
|
+
for (const [name, def] of Object.entries(fieldDefs)) if (def.kind === "scalar" && def.readonly === true && name in params) throw new ResolverFrameError("readonly_field", `Cannot set read-only field '${name}' (engine-allocated)`, { field: name });
|
|
11153
|
+
}
|
|
11154
|
+
/**
|
|
11120
11155
|
* Assert that the caller has not supplied any server-controlled fields.
|
|
11121
11156
|
*
|
|
11122
11157
|
* Tier-0: `_metadata` is rejected explicitly — the resolver's spread ordering
|
|
@@ -11470,58 +11505,6 @@ function listResolver(typeName, dataset, options = {}) {
|
|
|
11470
11505
|
};
|
|
11471
11506
|
}
|
|
11472
11507
|
|
|
11473
|
-
//#endregion
|
|
11474
|
-
//#region ../../packages/substrate-sdk/src/autocrud/autocrud-purge.ts
|
|
11475
|
-
/**
|
|
11476
|
-
* autoCRUD `purge<T>` resolver. Spec: §5.1 (`purge<T>({ id })` — optional
|
|
11477
|
-
* admin-only hard-delete), §4.5 (`purge` on nonexistent row returns
|
|
11478
|
-
* `{ deleted: 0 }` without error).
|
|
11479
|
-
*
|
|
11480
|
-
* Purge is hard-delete: it bypasses the `_deletedAt` tombstone lifecycle and
|
|
11481
|
-
* removes the row outright. Downstream edges observe the row as MISSING on
|
|
11482
|
-
* subsequent reads; the sweep's non-bridged MISSING branch treats this as
|
|
11483
|
-
* deleted and fans policy per §6.3.
|
|
11484
|
-
*
|
|
11485
|
-
* Admin gate: this resolver does NOT embed a capability check. The gate is a
|
|
11486
|
-
* `Policy` composed at install time — see `installAutoCrud(..., { purgeable: true })`
|
|
11487
|
-
* which attaches `requireCapability(adminCapability)` by default. Callers can
|
|
11488
|
-
* override with any policy.
|
|
11489
|
-
*
|
|
11490
|
-
* Kill switch: assertHardDeleteAllowed() is the first thing — refusal
|
|
11491
|
-
* short-circuits before any state read.
|
|
11492
|
-
*/
|
|
11493
|
-
const KNOWN_PURGE_PARAMS = /* @__PURE__ */ new Set(["id"]);
|
|
11494
|
-
function purgeResolver(typeName, dataset) {
|
|
11495
|
-
return async (_ctx, args) => {
|
|
11496
|
-
rejectUnknownParams("purge", typeName, args.params, KNOWN_PURGE_PARAMS);
|
|
11497
|
-
assertHardDeleteAllowed();
|
|
11498
|
-
const ds = args.dataset ?? dataset;
|
|
11499
|
-
const id = coerceId(args.params?.id);
|
|
11500
|
-
if (id === void 0) throw new ResolverFrameError("missing_id", `purgeResolver('${typeName}'): params.id is required`, { field: "id" });
|
|
11501
|
-
const { rows: existing } = await ds.read(typeName, {
|
|
11502
|
-
id,
|
|
11503
|
-
includeDeleted: true
|
|
11504
|
-
});
|
|
11505
|
-
const preRow = existing[0];
|
|
11506
|
-
const { deleted } = await ds.purge(typeName, [id]);
|
|
11507
|
-
const purged = deleted > 0;
|
|
11508
|
-
if (purged && args.onWrite !== void 0) {
|
|
11509
|
-
const event = {
|
|
11510
|
-
ns: ds.namespace,
|
|
11511
|
-
type: typeName,
|
|
11512
|
-
id,
|
|
11513
|
-
operation: "delete",
|
|
11514
|
-
...preRow !== void 0 ? { row: preRow } : {}
|
|
11515
|
-
};
|
|
11516
|
-
args.onWrite(event);
|
|
11517
|
-
}
|
|
11518
|
-
return {
|
|
11519
|
-
id,
|
|
11520
|
-
purged
|
|
11521
|
-
};
|
|
11522
|
-
};
|
|
11523
|
-
}
|
|
11524
|
-
|
|
11525
11508
|
//#endregion
|
|
11526
11509
|
//#region ../../packages/substrate-sdk/src/delete.ts
|
|
11527
11510
|
async function deleteRow(args) {
|
|
@@ -11590,7 +11573,7 @@ function prepareCreateRow(rawParams, fields) {
|
|
|
11590
11573
|
const params = dropUndefinedParams(rawParams ?? {});
|
|
11591
11574
|
if (fields) {
|
|
11592
11575
|
assertNoClientServerFields(params, fields, "create");
|
|
11593
|
-
|
|
11576
|
+
assertNoReadonlyFields(params, fields);
|
|
11594
11577
|
Object.assign(params, normaliseVocabValues(params, fields));
|
|
11595
11578
|
assertEnumValues(params, fields);
|
|
11596
11579
|
assertNoUnknownFields(params, fields, "create");
|
|
@@ -11670,6 +11653,7 @@ function prepareUpdate(typeName, rawParams, fields) {
|
|
|
11670
11653
|
function preparePatch(rawParams, fields) {
|
|
11671
11654
|
if (fields) assertNoClientServerFields(rawParams ?? {}, fields, "update");
|
|
11672
11655
|
const sent = dropUndefinedParams(rawParams ?? {});
|
|
11656
|
+
if (fields) assertNoReadonlyFields(sent, fields);
|
|
11673
11657
|
const params = fields !== void 0 ? normaliseVocabValues(sent, fields) : sent;
|
|
11674
11658
|
if ("_deletedAt" in params) throw new ReservedField("_deletedAt");
|
|
11675
11659
|
if (fields) {
|
|
@@ -12275,8 +12259,65 @@ function updateResolver(typeName, dataset, edges, options = {}) {
|
|
|
12275
12259
|
return singleFrame(updateBatchResolver(typeName, dataset, edges, options));
|
|
12276
12260
|
}
|
|
12277
12261
|
/** `delete<T>`: the batch resolver with one element. */
|
|
12278
|
-
function deleteResolver(typeName, dataset, edges) {
|
|
12279
|
-
return singleFrame(deleteBatchResolver(typeName, dataset, edges));
|
|
12262
|
+
function deleteResolver(typeName, dataset, edges, options = {}) {
|
|
12263
|
+
return singleFrame(deleteBatchResolver(typeName, dataset, edges, options));
|
|
12264
|
+
}
|
|
12265
|
+
|
|
12266
|
+
//#endregion
|
|
12267
|
+
//#region ../../packages/substrate-sdk/src/autocrud/autocrud-purge.ts
|
|
12268
|
+
/**
|
|
12269
|
+
* autoCRUD `purge<T>` resolver. Spec: §5.1 (`purge<T>({ id })` — optional
|
|
12270
|
+
* admin-only hard-delete), §4.5 (`purge` on nonexistent row returns
|
|
12271
|
+
* `{ deleted: 0 }` without error).
|
|
12272
|
+
*
|
|
12273
|
+
* Purge is hard-delete: it bypasses the `_deletedAt` tombstone lifecycle and
|
|
12274
|
+
* removes the row outright. Downstream edges observe the row as MISSING on
|
|
12275
|
+
* subsequent reads; the sweep's non-bridged MISSING branch treats this as
|
|
12276
|
+
* deleted and fans policy per §6.3.
|
|
12277
|
+
*
|
|
12278
|
+
* Admin gate: this resolver does NOT embed a capability check. The gate is a
|
|
12279
|
+
* `Policy` composed at install time — see `installAutoCrud(..., { purgeable: true })`
|
|
12280
|
+
* which attaches `requireCapability(adminCapability)` by default. Callers can
|
|
12281
|
+
* override with any policy.
|
|
12282
|
+
*
|
|
12283
|
+
* Kill switch: assertHardDeleteAllowed() is the first thing — refusal
|
|
12284
|
+
* short-circuits before any state read.
|
|
12285
|
+
*/
|
|
12286
|
+
const KNOWN_PURGE_PARAMS = /* @__PURE__ */ new Set(["id"]);
|
|
12287
|
+
/** With `policy`, a row its row filters hide is left in place and answers `purged: false`. */
|
|
12288
|
+
function purgeResolver(typeName, dataset, policy) {
|
|
12289
|
+
return async (ctx, args) => {
|
|
12290
|
+
rejectUnknownParams("purge", typeName, args.params, KNOWN_PURGE_PARAMS);
|
|
12291
|
+
assertHardDeleteAllowed();
|
|
12292
|
+
const ds = args.dataset ?? dataset;
|
|
12293
|
+
const id = coerceId(args.params?.id);
|
|
12294
|
+
if (id === void 0) throw new ResolverFrameError("missing_id", `purgeResolver('${typeName}'): params.id is required`, { field: "id" });
|
|
12295
|
+
const { rows: existing } = await ds.read(typeName, {
|
|
12296
|
+
id,
|
|
12297
|
+
includeDeleted: true
|
|
12298
|
+
});
|
|
12299
|
+
const preRow = existing[0];
|
|
12300
|
+
if (guardRowWrites(ctx, policy, [args], [preRow ?? null])[0]) return {
|
|
12301
|
+
id,
|
|
12302
|
+
purged: false
|
|
12303
|
+
};
|
|
12304
|
+
const { deleted } = await ds.purge(typeName, [id]);
|
|
12305
|
+
const purged = deleted > 0;
|
|
12306
|
+
if (purged && args.onWrite !== void 0) {
|
|
12307
|
+
const event = {
|
|
12308
|
+
ns: ds.namespace,
|
|
12309
|
+
type: typeName,
|
|
12310
|
+
id,
|
|
12311
|
+
operation: "delete",
|
|
12312
|
+
...preRow !== void 0 ? { row: preRow } : {}
|
|
12313
|
+
};
|
|
12314
|
+
args.onWrite(event);
|
|
12315
|
+
}
|
|
12316
|
+
return {
|
|
12317
|
+
id,
|
|
12318
|
+
purged
|
|
12319
|
+
};
|
|
12320
|
+
};
|
|
12280
12321
|
}
|
|
12281
12322
|
|
|
12282
12323
|
//#endregion
|
|
@@ -12643,7 +12684,7 @@ function installAutoCrud(registry, typeName, dataset, edges, options = {}) {
|
|
|
12643
12684
|
const create = singleFrame(createBatch);
|
|
12644
12685
|
const update = singleFrame(updateBatch);
|
|
12645
12686
|
const del = singleFrame(deleteBatch);
|
|
12646
|
-
const purge = purgeResolver(typeName, dataset);
|
|
12687
|
+
const purge = purgeResolver(typeName, dataset, policy);
|
|
12647
12688
|
const cap = capOf(typeName);
|
|
12648
12689
|
const stillOwn = (key, own) => () => {
|
|
12649
12690
|
const get = registry.get?.bind(registry);
|
|
@@ -16891,8 +16932,7 @@ function wrapPluginRouterWithSystemActor(router, actor) {
|
|
|
16891
16932
|
post: (path, h) => router.post(path, w(h)),
|
|
16892
16933
|
put: (path, h) => router.put(path, w(h)),
|
|
16893
16934
|
delete: (path, h) => router.delete(path, w(h)),
|
|
16894
|
-
patch: (path, h) => router.patch(path, w(h))
|
|
16895
|
-
websocket: (path, h) => router.websocket(path, h)
|
|
16935
|
+
patch: (path, h) => router.patch(path, w(h))
|
|
16896
16936
|
};
|
|
16897
16937
|
}
|
|
16898
16938
|
/**
|
|
@@ -16937,87 +16977,11 @@ function withAuth(handler, cfg, required, onAnonymousRead) {
|
|
|
16937
16977
|
};
|
|
16938
16978
|
}
|
|
16939
16979
|
/**
|
|
16940
|
-
* Cap on frames buffered before a plugin WS handler's `onMessage` registers
|
|
16941
|
-
* (the early-frame race — see `wrapWsHandler`). An unauthenticated peer that
|
|
16942
|
-
* never reaches a registered listener, whether rejected outright or simply
|
|
16943
|
-
* connected to a handler that never calls `onMessage`, must not be able to
|
|
16944
|
-
* grow this queue without bound while the socket stays open.
|
|
16945
|
-
*/
|
|
16946
|
-
const WS_EARLY_FRAME_QUEUE_LIMIT = 64;
|
|
16947
|
-
/**
|
|
16948
|
-
* Authenticate a plugin WebSocket route and hand the inner handler a request
|
|
16949
|
-
* carrying `identity` when the caller presented a valid bearer token. A plugin
|
|
16950
|
-
* serving its own frame protocol at `/substrate/v1/plugins/<name>/ws` gets neither the
|
|
16951
|
-
* binary's handshake auth nor the engine's per-frame gate.
|
|
16952
|
-
*
|
|
16953
|
-
* Mirrors `withAuth`'s mode handling: an absent token is rejected (1008)
|
|
16954
|
-
* unless `cfg.mode === 'attribute'`, in which case the
|
|
16955
|
-
* caller proceeds anonymous, with any caller-supplied `identity` stripped —
|
|
16956
|
-
* the same defence-in-depth `withAuth` applies on its own anonymous path.
|
|
16957
|
-
* A token that is present but invalid is always rejected, regardless of
|
|
16958
|
-
* mode: presenting a bad credential is an error, never a downgrade to
|
|
16959
|
-
* anonymous. Which frames are privileged, and what capability each
|
|
16960
|
-
* requires, stays the plugin's call — this wrapper only resolves identity.
|
|
16961
|
-
*/
|
|
16962
|
-
function wrapWsHandler(handler, cfg) {
|
|
16963
|
-
return async (req, conn) => {
|
|
16964
|
-
const queued = [];
|
|
16965
|
-
const listeners = [];
|
|
16966
|
-
let rejected = false;
|
|
16967
|
-
function rejectAndClose(reason) {
|
|
16968
|
-
rejected = true;
|
|
16969
|
-
queued.length = 0;
|
|
16970
|
-
conn.close(1008, reason);
|
|
16971
|
-
}
|
|
16972
|
-
conn.onMessage((b) => {
|
|
16973
|
-
if (rejected) return;
|
|
16974
|
-
if (listeners.length > 0) {
|
|
16975
|
-
for (const cb of listeners) cb(b);
|
|
16976
|
-
return;
|
|
16977
|
-
}
|
|
16978
|
-
queued.push(b);
|
|
16979
|
-
if (queued.length > WS_EARLY_FRAME_QUEUE_LIMIT) rejectAndClose("frame backlog");
|
|
16980
|
-
});
|
|
16981
|
-
const token = extractBearer(req.headers.authorization);
|
|
16982
|
-
if (token === null) {
|
|
16983
|
-
if (cfg.mode !== "attribute") {
|
|
16984
|
-
rejectAndClose("unauthorized");
|
|
16985
|
-
return;
|
|
16986
|
-
}
|
|
16987
|
-
const { identity: _identity, ...anonReq } = req;
|
|
16988
|
-
req = anonReq;
|
|
16989
|
-
} else {
|
|
16990
|
-
const verdict = authenticateBearer(token, cfg);
|
|
16991
|
-
if (!verdict.ok) {
|
|
16992
|
-
rejectAndClose("unauthorized");
|
|
16993
|
-
return;
|
|
16994
|
-
}
|
|
16995
|
-
req = {
|
|
16996
|
-
...req,
|
|
16997
|
-
identity: verdict.identity
|
|
16998
|
-
};
|
|
16999
|
-
const stop = watchCredential(verdict.identity, cfg, (lapse) => conn.close(1008, lapse));
|
|
17000
|
-
conn.onClose(stop);
|
|
17001
|
-
}
|
|
17002
|
-
if (rejected) return;
|
|
17003
|
-
const shim = {
|
|
17004
|
-
...conn,
|
|
17005
|
-
onMessage: (cb) => {
|
|
17006
|
-
while (queued.length > 0) for (const b of queued.splice(0)) cb(b);
|
|
17007
|
-
listeners.push(cb);
|
|
17008
|
-
}
|
|
17009
|
-
};
|
|
17010
|
-
await handler(req, shim);
|
|
17011
|
-
};
|
|
17012
|
-
}
|
|
17013
|
-
/**
|
|
17014
16980
|
* Wrap a `PluginRouter` so every registered route runs through `withAuth`
|
|
17015
16981
|
* with a capability inferred from the HTTP method — GET reads, everything
|
|
17016
16982
|
* else mutates. An authenticated caller must hold that capability in one of
|
|
17017
16983
|
* `namespaces` (the plugin's declared namespaces); a plugin that declares
|
|
17018
|
-
* none accepts the capability held in any namespace.
|
|
17019
|
-
* `wrapWsHandler`, which resolves identity but leaves the
|
|
17020
|
-
* privileged/anonymous decision to the plugin.
|
|
16984
|
+
* none accepts the capability held in any namespace.
|
|
17021
16985
|
*/
|
|
17022
16986
|
function wrapPluginRouter(router, cfg, namespaces = []) {
|
|
17023
16987
|
const gate = (method, h) => {
|
|
@@ -17035,32 +16999,20 @@ function wrapPluginRouter(router, cfg, namespaces = []) {
|
|
|
17035
16999
|
post: (path, h) => router.post(path, gate("POST", h)),
|
|
17036
17000
|
put: (path, h) => router.put(path, gate("PUT", h)),
|
|
17037
17001
|
delete: (path, h) => router.delete(path, gate("DELETE", h)),
|
|
17038
|
-
patch: (path, h) => router.patch(path, gate("PATCH", h))
|
|
17039
|
-
websocket: (path, h) => router.websocket(path, wrapWsHandler(h, cfg))
|
|
17002
|
+
patch: (path, h) => router.patch(path, gate("PATCH", h))
|
|
17040
17003
|
};
|
|
17041
17004
|
}
|
|
17042
17005
|
|
|
17043
17006
|
//#endregion
|
|
17044
17007
|
//#region ../../packages/substrate-compose/src/compose/guards.ts
|
|
17045
|
-
/**
|
|
17046
|
-
* Floor for the SSE stream-ticket HMAC key. 16 chars is the shortest length
|
|
17047
|
-
* that isn't trivially brute-forced offline — an attacker can test candidate
|
|
17048
|
-
* keys against any ticket they observe, with no server round-trip to rate-limit.
|
|
17049
|
-
*/
|
|
17050
|
-
const MIN_STREAM_TICKET_SECRET_LEN = 16;
|
|
17051
17008
|
function resolveBootGuards(opts) {
|
|
17052
|
-
const streamTicketSecret = opts.streamTicketSecret?.trim();
|
|
17053
|
-
if (opts.auth !== void 0 && (streamTicketSecret === void 0 || streamTicketSecret.length < MIN_STREAM_TICKET_SECRET_LEN)) throw new Error(`compose: \`streamTicketSecret\` must be at least ${MIN_STREAM_TICKET_SECRET_LEN} characters when auth is enabled — it signs the SSE stream tickets that are the only authentication on GET /substrate/v1/stream and /substrate/v1/subscribe, so a short, empty, or whitespace-only key is a forgeable one (set SUBSTRATE_STREAM_TICKET_SECRET for the binary)`);
|
|
17054
17009
|
const systemActor = opts.systemActor?.trim();
|
|
17055
17010
|
if (opts.auth === void 0 && (systemActor === void 0 || systemActor.length === 0)) throw new Error("compose: `systemActor` is required when auth is disabled — with no bearer there is nothing to attribute a write to, and Substrate no longer defaults to `system:anonymous`. Declare who this deployment writes as, e.g. systemActor: 'local-dev' (SUBSTRATE_SYSTEM_ACTOR for the binary). Enable auth instead if callers should be attributed individually.");
|
|
17056
|
-
return {
|
|
17057
|
-
|
|
17058
|
-
|
|
17059
|
-
|
|
17060
|
-
|
|
17061
|
-
grants: { "*": ["read", "mutate"] }
|
|
17062
|
-
} : null
|
|
17063
|
-
};
|
|
17011
|
+
return { systemIdentity: systemActor !== void 0 && systemActor.length > 0 ? {
|
|
17012
|
+
kind: "system",
|
|
17013
|
+
id: systemActor,
|
|
17014
|
+
grants: { "*": ["read", "mutate"] }
|
|
17015
|
+
} : null };
|
|
17064
17016
|
}
|
|
17065
17017
|
|
|
17066
17018
|
//#endregion
|
|
@@ -17507,13 +17459,8 @@ const failed = (err) => ({
|
|
|
17507
17459
|
|
|
17508
17460
|
//#endregion
|
|
17509
17461
|
//#region ../../packages/substrate-compose/src/routes/connection-limit.ts
|
|
17510
|
-
/** Most
|
|
17462
|
+
/** Most long-lived responses one client may hold open at once. */
|
|
17511
17463
|
const MAX_CONNECTIONS_PER_CLIENT = 64;
|
|
17512
|
-
/**
|
|
17513
|
-
* Most subscribe/stream sessions one client may hold open at once, across
|
|
17514
|
-
* every WS socket and SSE stream. Each one is engine work on every write.
|
|
17515
|
-
*/
|
|
17516
|
-
const MAX_SESSIONS_PER_CLIENT = 1024;
|
|
17517
17464
|
function createConnectionLimiter(max = 64) {
|
|
17518
17465
|
const open = /* @__PURE__ */ new Map();
|
|
17519
17466
|
return { acquire(identity, clientIp) {
|
|
@@ -17611,10 +17558,11 @@ function healthRoute(deps) {
|
|
|
17611
17558
|
* POST /substrate/v1/mutate — accept a frame, dispatch to the engine, return envelope.
|
|
17612
17559
|
*
|
|
17613
17560
|
* Under `Accept: application/vnd.substrate.pack`, mutation responses are positionally
|
|
17614
|
-
* encoded
|
|
17615
|
-
*
|
|
17616
|
-
*
|
|
17617
|
-
*
|
|
17561
|
+
* encoded by each mutation's published `MutationSchema.returns` (§5.2).
|
|
17562
|
+
*
|
|
17563
|
+
* A mutation answers by the frame's `fields` on both wires, and a create or
|
|
17564
|
+
* update that names none answers `{ id, shortId }`: JSON projects the result,
|
|
17565
|
+
* pack encodes it by `returns` narrowed the same way (`mutationReturns`).
|
|
17618
17566
|
*/
|
|
17619
17567
|
function mutateRoute(engine, trustedProxies, authEnabled) {
|
|
17620
17568
|
return async (req) => {
|
|
@@ -17627,24 +17575,32 @@ function mutateRoute(engine, trustedProxies, authEnabled) {
|
|
|
17627
17575
|
ctx: buildContext(req, trustedProxies, authEnabled),
|
|
17628
17576
|
...req.signal !== void 0 ? { signal: req.signal } : {}
|
|
17629
17577
|
});
|
|
17578
|
+
const schemas = engine.federatedSchema();
|
|
17630
17579
|
const returnsByKey = {};
|
|
17631
17580
|
const currentNsByKey = {};
|
|
17632
|
-
|
|
17581
|
+
const projectBy = { ...frame };
|
|
17582
|
+
for (const [key, frameEntry] of Object.entries(frame)) {
|
|
17633
17583
|
const parsed = parseQualifiedKey(key);
|
|
17634
17584
|
if (parsed === null) continue;
|
|
17635
|
-
const
|
|
17636
|
-
|
|
17637
|
-
|
|
17638
|
-
|
|
17639
|
-
|
|
17640
|
-
|
|
17641
|
-
}
|
|
17642
|
-
return serializeEnvelope(req, envelope, engine,
|
|
17585
|
+
const mutation = schemas[parsed.ns]?.mutations[parsed.name];
|
|
17586
|
+
if (mutation === void 0) continue;
|
|
17587
|
+
currentNsByKey[key] = parsed.ns;
|
|
17588
|
+
const shapeOf = (f) => mutationReturns(mutation, f.fields);
|
|
17589
|
+
returnsByKey[key] = Array.isArray(frameEntry) ? frameEntry.map(shapeOf) : shapeOf(frameEntry);
|
|
17590
|
+
if (mutation.writesRows === true) projectBy[key] = Array.isArray(frameEntry) ? frameEntry.map(withAckFields) : withAckFields(frameEntry);
|
|
17591
|
+
}
|
|
17592
|
+
return serializeEnvelope(req, envelope, engine, projectBy, {
|
|
17643
17593
|
returnsByKey,
|
|
17644
17594
|
currentNsByKey
|
|
17645
17595
|
});
|
|
17646
17596
|
};
|
|
17647
17597
|
}
|
|
17598
|
+
function withAckFields(frameEntry) {
|
|
17599
|
+
return frameEntry.fields === void 0 ? {
|
|
17600
|
+
...frameEntry,
|
|
17601
|
+
fields: MUTATION_ACK_FIELDS
|
|
17602
|
+
} : frameEntry;
|
|
17603
|
+
}
|
|
17648
17604
|
|
|
17649
17605
|
//#endregion
|
|
17650
17606
|
//#region ../../packages/substrate-compose/src/routes/onboard.ts
|
|
@@ -17704,7 +17660,7 @@ const HTTP_INSTRUCTIONS = {
|
|
|
17704
17660
|
"`authEnabled: false` means this deployment verifies nothing, which is NOT the same as you being anonymous",
|
|
17705
17661
|
"USE IT BEFORE CONCLUDING A CREDENTIAL WORKS: on a reads-open stack an authenticated read and an anonymous one return byte-identical results, so a successful query is never evidence of identity",
|
|
17706
17662
|
"the grant is returned AS RECORDED — a wildcard stays a `\"*\"` key in `grants` rather than expanding into every namespace",
|
|
17707
|
-
"also available as the MCP tool `whoami
|
|
17663
|
+
"also available as the MCP tool `whoami`"
|
|
17708
17664
|
]
|
|
17709
17665
|
},
|
|
17710
17666
|
federatedSchema: {
|
|
@@ -17713,80 +17669,51 @@ const HTTP_INSTRUCTIONS = {
|
|
|
17713
17669
|
notes: [
|
|
17714
17670
|
"returns `Record<namespace, Schema>` for every active namespace",
|
|
17715
17671
|
"cache at cold-start + rebuild when a response signals a federatedVersion mismatch",
|
|
17716
|
-
"build the
|
|
17672
|
+
"build the pack nsIndex map from sorted(federatedSchema keys)"
|
|
17717
17673
|
]
|
|
17718
17674
|
}
|
|
17719
17675
|
};
|
|
17720
17676
|
/**
|
|
17721
|
-
* The live subscription surfaces:
|
|
17722
|
-
*
|
|
17677
|
+
* The live subscription surfaces: one POST per subscription or stream, the
|
|
17678
|
+
* response body a sequence of length-prefixed frames.
|
|
17723
17679
|
*
|
|
17724
17680
|
* Every string here is a claim about the wire, held to the real routes by
|
|
17725
|
-
* `onboard-wire-shapes.test.ts`.
|
|
17726
|
-
* defended if that test composes a server in that mode.
|
|
17681
|
+
* `onboard-wire-shapes.test.ts`.
|
|
17727
17682
|
*/
|
|
17728
|
-
const
|
|
17683
|
+
const STREAM_INSTRUCTIONS = {
|
|
17729
17684
|
subscribe: {
|
|
17730
|
-
|
|
17731
|
-
path: "/substrate/v1/
|
|
17732
|
-
|
|
17733
|
-
|
|
17734
|
-
* Structured rather than prose: each field is one cell of the
|
|
17735
|
-
* {mode} × {token} matrix, so `onboard-wire-shapes.test.ts` can read a cell
|
|
17736
|
-
* and drive that cell.
|
|
17737
|
-
*/
|
|
17738
|
-
authByMode: {
|
|
17739
|
-
required: "DEFAULT. A tokenless upgrade receives one { id:\"\", kind:\"error\", errors:[{ code:\"unauthorized\" }] } frame and is closed with code 4001",
|
|
17740
|
-
attribute: "A tokenless upgrade is ADMITTED and runs anonymously, matching the HTTP routes under this mode — send `{ id, op:\"whoami\" }` and `identity` comes back null.",
|
|
17741
|
-
invalidToken: "Closed 4001 in BOTH modes. This is the distinction to design against rather than the mode: `attribute` relaxes \"you sent nothing\", never \"what you sent is broken\", so a caller that always sends a token sees identical behaviour either way"
|
|
17742
|
-
},
|
|
17685
|
+
method: "POST",
|
|
17686
|
+
path: "/substrate/v1/subscribe",
|
|
17687
|
+
accept: "application/vnd.substrate.frames+json",
|
|
17688
|
+
body: "{\"<ns>/<TypeName>\":{\"params\":{\"where\":{}},\"fields\":{\"id\":true}}}",
|
|
17743
17689
|
notes: [
|
|
17744
|
-
"
|
|
17745
|
-
"
|
|
17690
|
+
"send `Accept: application/vnd.substrate.frames+json` (or `+pack`, see `substratePack`); any other Accept is refused 406 with code `not_acceptable`",
|
|
17691
|
+
"the response body is a sequence of frames: a 4-byte big-endian length, then that many bytes of payload. A zero-length frame is a heartbeat; skip it",
|
|
17692
|
+
"the first frame is { id, kind:\"snapshot\", data:{ \"<ns>/<TypeName>\": rows }, shapes }, then kind:\"add\" | \"update\" | \"remove\" (a remove carries `removedId`, no `data`)",
|
|
17746
17693
|
"PARAMS ARE NARROWER THAN QUERY: subscribe accepts only `where` and `orderBy`. `limit` is a POST /substrate/v1/query param and is refused here with code `unknown_param` — the allowlist is in the message",
|
|
17747
|
-
"multi-key subscribe IS supported (unlike stream)
|
|
17748
|
-
"
|
|
17749
|
-
"`{ id, op:\"whoami\" }` answers { id, kind:\"whoami\", data:{ authEnabled, identity } } without opening a session — ask it on the socket rather than over HTTP when the socket is what you are asking about",
|
|
17750
|
-
"subscriptions are connection-scoped — cancelled automatically on disconnect",
|
|
17694
|
+
"multi-key subscribe IS supported (unlike stream)",
|
|
17695
|
+
"one subscription per response: close the response to cancel it",
|
|
17751
17696
|
"virtual namespaces cannot be subscribed: a `recall/*` key is refused with code `subscription_unsupported_frame` (one-shot query/mutate only)",
|
|
17752
|
-
"
|
|
17753
|
-
"
|
|
17697
|
+
"auth is the same as POST /substrate/v1/query: the route needs `read`",
|
|
17698
|
+
"failures BEFORE the stream opens are ordinary HTTP status codes; once bytes are on the wire the status is spent and errors arrive in-band as { kind:\"error\", errors } frames",
|
|
17699
|
+
"a server shutting down sends { kind:\"end\", reason:\"server_restart\" } before closing: subscribe again for a fresh snapshot"
|
|
17754
17700
|
]
|
|
17755
17701
|
},
|
|
17756
17702
|
stream: {
|
|
17757
|
-
|
|
17758
|
-
path: "/substrate/v1/
|
|
17759
|
-
|
|
17703
|
+
method: "POST",
|
|
17704
|
+
path: "/substrate/v1/stream",
|
|
17705
|
+
accept: "application/vnd.substrate.frames+json",
|
|
17706
|
+
body: "{\"<ns>/<TypeName>\":{\"params\":{},\"fields\":{\"id\":true}}}",
|
|
17760
17707
|
notes: [
|
|
17761
17708
|
"single op-key only — two keys are refused with code `stream_multi_key`",
|
|
17762
|
-
"
|
|
17709
|
+
"one { id, kind:\"chunk\", data:{ \"<ns>/<TypeName>\": <entity> } } frame per chunk",
|
|
17763
17710
|
"terminates with { id, kind:\"end\", final:true } — errors terminate too, no partial-error-then-resume",
|
|
17764
17711
|
"streaming needs a resolver returning an async iterable; autoCRUD types are not streamable and answer code `not_streamable`"
|
|
17765
17712
|
]
|
|
17766
17713
|
},
|
|
17767
17714
|
substratePack: {
|
|
17768
|
-
|
|
17769
|
-
|
|
17770
|
-
notes: [
|
|
17771
|
-
"advertise `Sec-WebSocket-Protocol: substrate-pack` at handshake to negotiate MessagePack frames with positionally-encoded `data`",
|
|
17772
|
-
"the negotiation is SYMMETRIC: on a pack socket your REQUEST frames must be MessagePack too — sending JSON text there is refused with code `invalid_frame`",
|
|
17773
|
-
"positional `data` decodes against the field selection you sent, in declared field order — see @mnmal-ai/substrate-pack (encodeWsFrame / decodeWsEnvelope)"
|
|
17774
|
-
]
|
|
17775
|
-
},
|
|
17776
|
-
sse: {
|
|
17777
|
-
transport: "sse",
|
|
17778
|
-
paths: [
|
|
17779
|
-
"/substrate/v1/subscribe",
|
|
17780
|
-
"/substrate/v1/stream",
|
|
17781
|
-
"/substrate/v1/stream/ticket"
|
|
17782
|
-
],
|
|
17783
|
-
notes: [
|
|
17784
|
-
"same two engine operations as WS, one session per HTTP response — cancellation IS disconnection, so there is no `cancel` op and no session id",
|
|
17785
|
-
"POST /substrate/v1/subscribe (or /substrate/v1/stream) with the frame object as the BODY — the qualified-frame-keyed payload, exactly as POST /substrate/v1/query takes it",
|
|
17786
|
-
"GET the same paths with ?frame=<url-encoded JSON> for EventSource, which cannot send an Authorization header: mint a ticket first with POST /substrate/v1/stream/ticket (body = the same frame) and pass ?ticket=<ticket>. A ticketless GET is 401 `ticket_missing`; the ticket is bound to that exact frame",
|
|
17787
|
-
"THE EVENT ENVELOPE DIFFERS FROM WS: SSE emits `event: <kind>` with a per-frame payload — snapshot/add/update carry { key, rows } or { key, row }, remove carries { removedId }, end carries { reason }, error carries { code, message }. WS instead sends one object per frame carrying `data` keyed by the frame key",
|
|
17788
|
-
"failures BEFORE the stream opens are ordinary HTTP status codes; once bytes are on the wire the status is spent and errors arrive in-band as `event: error`"
|
|
17789
|
-
]
|
|
17715
|
+
accept: "application/vnd.substrate.frames+pack",
|
|
17716
|
+
notes: ["send `Accept: application/vnd.substrate.frames+pack` on subscribe or stream for MessagePack frames with positionally-encoded `data`; the request body stays JSON", "positional `data` decodes against the field selection you sent, in declared field order — see @mnmal-ai/substrate-pack (decodeStreamFrames / decodeWsEnvelope)"]
|
|
17790
17717
|
}
|
|
17791
17718
|
};
|
|
17792
17719
|
const CODE_MODE_INSTRUCTIONS = {
|
|
@@ -17884,10 +17811,9 @@ function buildOnboardBody(deps) {
|
|
|
17884
17811
|
schemaForType: HTTP_INSTRUCTIONS.schemaForType,
|
|
17885
17812
|
federatedSchema: HTTP_INSTRUCTIONS.federatedSchema,
|
|
17886
17813
|
whoami: HTTP_INSTRUCTIONS.whoami,
|
|
17887
|
-
subscribe:
|
|
17888
|
-
stream:
|
|
17889
|
-
substratePack:
|
|
17890
|
-
sse: WS_INSTRUCTIONS.sse,
|
|
17814
|
+
subscribe: STREAM_INSTRUCTIONS.subscribe,
|
|
17815
|
+
stream: STREAM_INSTRUCTIONS.stream,
|
|
17816
|
+
substratePack: STREAM_INSTRUCTIONS.substratePack,
|
|
17891
17817
|
codeMode: CODE_MODE_INSTRUCTIONS,
|
|
17892
17818
|
plugins: buildPluginContributions(deps.coordinators),
|
|
17893
17819
|
pluginRoutes: buildPluginRoutes(deps.coordinators)
|
|
@@ -18179,84 +18105,242 @@ function mutationTargetsType(name, def, type, fields) {
|
|
|
18179
18105
|
}
|
|
18180
18106
|
|
|
18181
18107
|
//#endregion
|
|
18182
|
-
//#region ../../packages/substrate-compose/src/routes/
|
|
18108
|
+
//#region ../../packages/substrate-compose/src/routes/demand-transport.ts
|
|
18109
|
+
const DEFAULT_HEARTBEAT_MS = 2e4;
|
|
18183
18110
|
/**
|
|
18184
|
-
*
|
|
18185
|
-
*
|
|
18186
|
-
*
|
|
18187
|
-
*
|
|
18188
|
-
*
|
|
18189
|
-
|
|
18190
|
-
|
|
18191
|
-
const SSE_HEADERS = {
|
|
18192
|
-
"content-type": "text/event-stream",
|
|
18193
|
-
"cache-control": "no-store",
|
|
18194
|
-
"x-accel-buffering": "no"
|
|
18195
|
-
};
|
|
18196
|
-
const SSE_HEARTBEAT = ": keepalive\n\n";
|
|
18197
|
-
/** Browser default is 3s — too eager when every reconnect costs a snapshot. */
|
|
18198
|
-
function sseRetry(ms) {
|
|
18199
|
-
return `retry: ${ms}\n\n`;
|
|
18200
|
-
}
|
|
18111
|
+
* How long a data frame may wait for the consumer to read before the stream is
|
|
18112
|
+
* cut off. Generous on purpose: a park also waits on every buffer between here
|
|
18113
|
+
* and a slow client (a 1 KB/s reader parks ~64 s), and dead peers are caught
|
|
18114
|
+
* by the server's HTTP/2 pings, so this only has to catch a live peer that
|
|
18115
|
+
* stopped reading.
|
|
18116
|
+
*/
|
|
18117
|
+
const DEFAULT_STALL_MS = 3e5;
|
|
18201
18118
|
/**
|
|
18202
|
-
*
|
|
18203
|
-
*
|
|
18204
|
-
|
|
18205
|
-
|
|
18206
|
-
|
|
18207
|
-
|
|
18208
|
-
|
|
18209
|
-
|
|
18210
|
-
|
|
18211
|
-
|
|
18212
|
-
|
|
18213
|
-
|
|
18214
|
-
|
|
18215
|
-
|
|
18216
|
-
|
|
18217
|
-
|
|
18218
|
-
|
|
18219
|
-
|
|
18220
|
-
|
|
18221
|
-
|
|
18222
|
-
|
|
18223
|
-
|
|
18224
|
-
|
|
18225
|
-
const
|
|
18226
|
-
|
|
18227
|
-
|
|
18228
|
-
|
|
18229
|
-
|
|
18230
|
-
|
|
18231
|
-
|
|
18232
|
-
|
|
18119
|
+
* A response body paced to consumer demand, carrying one session's frames in
|
|
18120
|
+
* whatever wire encoding `encode` gives.
|
|
18121
|
+
*/
|
|
18122
|
+
function createDemandTransport(opts) {
|
|
18123
|
+
const heartbeatMs = opts.heartbeatMs ?? DEFAULT_HEARTBEAT_MS;
|
|
18124
|
+
const stallMs = opts.stallMs ?? 3e5;
|
|
18125
|
+
let controller = null;
|
|
18126
|
+
let heartbeat = null;
|
|
18127
|
+
let closed = false;
|
|
18128
|
+
/**
|
|
18129
|
+
* Resolvers of parked `emit` calls. Must be a Set, not a single slot: a
|
|
18130
|
+
* multi-key subscribe drains each key on its own task sharing one `onFrame`,
|
|
18131
|
+
* so two `emit`s can be in flight at once, and a single slot would strand
|
|
18132
|
+
* the first one's drain task.
|
|
18133
|
+
*/
|
|
18134
|
+
const demand = /* @__PURE__ */ new Set();
|
|
18135
|
+
const stopHeartbeat = () => {
|
|
18136
|
+
if (heartbeat !== null) {
|
|
18137
|
+
clearInterval(heartbeat);
|
|
18138
|
+
heartbeat = null;
|
|
18139
|
+
}
|
|
18140
|
+
};
|
|
18141
|
+
/** Wake every parked `emit`, not just one — see the `demand` docblock. */
|
|
18142
|
+
const releaseDemand = () => {
|
|
18143
|
+
for (const resolve of demand) resolve();
|
|
18144
|
+
demand.clear();
|
|
18145
|
+
};
|
|
18146
|
+
/**
|
|
18147
|
+
* Shared by every teardown path (write failure, close, consumer cancel) so
|
|
18148
|
+
* none of them can leave the heartbeat ticking or an `emit` parked forever;
|
|
18149
|
+
* a parked `emit` strands the engine's producer loop.
|
|
18150
|
+
*/
|
|
18151
|
+
const teardown = () => {
|
|
18152
|
+
closed = true;
|
|
18153
|
+
stopHeartbeat();
|
|
18154
|
+
releaseDemand();
|
|
18155
|
+
};
|
|
18156
|
+
const write = (bytes) => {
|
|
18157
|
+
if (closed || controller === null) return;
|
|
18158
|
+
try {
|
|
18159
|
+
controller.enqueue(bytes);
|
|
18160
|
+
} catch {
|
|
18161
|
+
teardown();
|
|
18162
|
+
controller = null;
|
|
18163
|
+
}
|
|
18164
|
+
};
|
|
18165
|
+
/** The consumer stopped reading with data waiting: error the body and cancel the session. */
|
|
18166
|
+
const stall = () => {
|
|
18167
|
+
if (closed) return;
|
|
18168
|
+
const c = controller;
|
|
18169
|
+
teardown();
|
|
18170
|
+
controller = null;
|
|
18171
|
+
try {
|
|
18172
|
+
c?.error(/* @__PURE__ */ new Error("consumer_stalled"));
|
|
18173
|
+
} catch {}
|
|
18174
|
+
opts.onStall?.();
|
|
18175
|
+
opts.onCancel?.();
|
|
18176
|
+
};
|
|
18177
|
+
return {
|
|
18178
|
+
transport: {
|
|
18179
|
+
emit: async (frame, fieldsByKey) => {
|
|
18180
|
+
if (closed) return;
|
|
18181
|
+
write(opts.encode(frame, fieldsByKey));
|
|
18182
|
+
if (frame.kind === "end" || frame.kind === "error") return;
|
|
18183
|
+
if (controller !== null && (controller.desiredSize ?? 1) <= 0) await new Promise((resolve) => {
|
|
18184
|
+
const timer = setTimeout(stall, stallMs);
|
|
18185
|
+
timer.unref?.();
|
|
18186
|
+
demand.add(() => {
|
|
18187
|
+
clearTimeout(timer);
|
|
18188
|
+
resolve();
|
|
18189
|
+
});
|
|
18190
|
+
});
|
|
18191
|
+
},
|
|
18192
|
+
close: (_reason) => {
|
|
18193
|
+
if (closed) return;
|
|
18194
|
+
teardown();
|
|
18195
|
+
try {
|
|
18196
|
+
controller?.close();
|
|
18197
|
+
} catch {}
|
|
18198
|
+
controller = null;
|
|
18199
|
+
}
|
|
18200
|
+
},
|
|
18201
|
+
body: new ReadableStream({
|
|
18202
|
+
start(c) {
|
|
18203
|
+
controller = c;
|
|
18204
|
+
heartbeat = setInterval(() => write(opts.heartbeat), heartbeatMs);
|
|
18205
|
+
heartbeat.unref?.();
|
|
18206
|
+
},
|
|
18207
|
+
pull() {
|
|
18208
|
+
releaseDemand();
|
|
18209
|
+
},
|
|
18210
|
+
cancel() {
|
|
18211
|
+
teardown();
|
|
18212
|
+
controller = null;
|
|
18213
|
+
opts.onCancel?.();
|
|
18214
|
+
}
|
|
18215
|
+
}),
|
|
18216
|
+
writeFrame: (frame) => write(opts.encode(frame, void 0))
|
|
18233
18217
|
};
|
|
18234
|
-
return frame.sequence !== void 0 ? {
|
|
18235
|
-
...base,
|
|
18236
|
-
sequence: frame.sequence
|
|
18237
|
-
} : base;
|
|
18238
18218
|
}
|
|
18239
18219
|
|
|
18240
18220
|
//#endregion
|
|
18241
|
-
//#region ../../packages/substrate-compose/src/routes/
|
|
18221
|
+
//#region ../../packages/substrate-compose/src/routes/pack-frame.ts
|
|
18242
18222
|
/**
|
|
18243
|
-
*
|
|
18244
|
-
*
|
|
18223
|
+
* Produce a wire-ready frame object — for substrate-pack, positionally encode
|
|
18224
|
+
* `data` against the per-key FieldInput + the engine's federated schema. Remove
|
|
18225
|
+
* frames (no `data`) and error/end frames pass through untouched.
|
|
18245
18226
|
*/
|
|
18246
|
-
function
|
|
18247
|
-
if (frame.
|
|
18227
|
+
function encodeDataForPack(frame, fieldsByKey, engine) {
|
|
18228
|
+
if (frame.data === void 0 || fieldsByKey === void 0) return frame;
|
|
18229
|
+
const schemas = engine.federatedSchema();
|
|
18230
|
+
const encoded = {};
|
|
18231
|
+
for (const [key, value] of Object.entries(frame.data)) {
|
|
18232
|
+
const parsed = parseQualifiedKey(key);
|
|
18233
|
+
if (parsed === null) {
|
|
18234
|
+
encoded[key] = value;
|
|
18235
|
+
continue;
|
|
18236
|
+
}
|
|
18237
|
+
const { ns, name } = parsed;
|
|
18238
|
+
const fields = fieldsByKey.get(key);
|
|
18239
|
+
const schema = schemas[ns];
|
|
18240
|
+
if (fields === void 0 || schema === void 0) {
|
|
18241
|
+
encoded[key] = value;
|
|
18242
|
+
continue;
|
|
18243
|
+
}
|
|
18244
|
+
const rawShape = frame.shapes?.[key];
|
|
18245
|
+
encoded[key] = encodeByShape(value, fields, schema, name, rawShape === "single" || rawShape === "list" ? rawShape : void 0, {
|
|
18246
|
+
schemas,
|
|
18247
|
+
currentNs: ns
|
|
18248
|
+
});
|
|
18249
|
+
}
|
|
18250
|
+
return {
|
|
18251
|
+
...frame,
|
|
18252
|
+
data: encoded
|
|
18253
|
+
};
|
|
18254
|
+
}
|
|
18255
|
+
function encodeByShape(value, fields, schema, typeName, shape, ctx) {
|
|
18256
|
+
if (value === null || value === void 0) return null;
|
|
18257
|
+
if (shape === "list") {
|
|
18258
|
+
if (!Array.isArray(value)) return value;
|
|
18259
|
+
return value.map((row) => encodePositional(row, fields, schema, typeName, ctx));
|
|
18260
|
+
}
|
|
18261
|
+
return encodePositional(value, fields, schema, typeName, ctx);
|
|
18262
|
+
}
|
|
18263
|
+
|
|
18264
|
+
//#endregion
|
|
18265
|
+
//#region ../../packages/substrate-compose/src/routes/frames-transport.ts
|
|
18266
|
+
function framesHeaders(mode) {
|
|
18267
|
+
return {
|
|
18268
|
+
"content-type": mode === "pack" ? FRAMES_PACK_MIME : FRAMES_JSON_MIME,
|
|
18269
|
+
"cache-control": "no-store",
|
|
18270
|
+
"x-accel-buffering": "no"
|
|
18271
|
+
};
|
|
18272
|
+
}
|
|
18273
|
+
function createFramesTransport(opts) {
|
|
18274
|
+
const { mode, engine } = opts;
|
|
18275
|
+
let tooLarge = false;
|
|
18276
|
+
const payload = (frame, fieldsByKey) => mode === "pack" ? encodeDataForPack(frame, fieldsByKey, engine) : projectWsFrame(frame, fieldsByKey);
|
|
18277
|
+
const encode = (frame, fieldsByKey) => {
|
|
18278
|
+
try {
|
|
18279
|
+
return encodeStreamFrame(payload(frame, fieldsByKey), mode);
|
|
18280
|
+
} catch (err) {
|
|
18281
|
+
if (!(err instanceof FrameStreamError)) throw err;
|
|
18282
|
+
tooLarge = true;
|
|
18283
|
+
const error = encodeStreamFrame({
|
|
18284
|
+
id: frame.id,
|
|
18285
|
+
kind: "error",
|
|
18286
|
+
errors: [{
|
|
18287
|
+
code: err.code,
|
|
18288
|
+
message: err.message
|
|
18289
|
+
}]
|
|
18290
|
+
}, mode);
|
|
18291
|
+
const end = encodeStreamFrame({
|
|
18292
|
+
id: frame.id,
|
|
18293
|
+
kind: "end",
|
|
18294
|
+
reason: err.code,
|
|
18295
|
+
final: true
|
|
18296
|
+
}, mode);
|
|
18297
|
+
const out = new Uint8Array(error.length + end.length);
|
|
18298
|
+
out.set(error);
|
|
18299
|
+
out.set(end, error.length);
|
|
18300
|
+
return out;
|
|
18301
|
+
}
|
|
18302
|
+
};
|
|
18303
|
+
const inner = createDemandTransport({
|
|
18304
|
+
encode,
|
|
18305
|
+
heartbeat: HEARTBEAT_FRAME,
|
|
18306
|
+
...opts.heartbeatMs !== void 0 ? { heartbeatMs: opts.heartbeatMs } : {},
|
|
18307
|
+
...opts.onCancel !== void 0 ? { onCancel: opts.onCancel } : {},
|
|
18308
|
+
...opts.onStall !== void 0 ? { onStall: opts.onStall } : {}
|
|
18309
|
+
});
|
|
18310
|
+
return {
|
|
18311
|
+
...inner,
|
|
18312
|
+
transport: {
|
|
18313
|
+
emit: async (frame, fieldsByKey) => {
|
|
18314
|
+
if (tooLarge) return;
|
|
18315
|
+
const sent = inner.transport.emit(frame, fieldsByKey);
|
|
18316
|
+
if (tooLarge) opts.onTooLarge();
|
|
18317
|
+
await sent;
|
|
18318
|
+
},
|
|
18319
|
+
close: inner.transport.close
|
|
18320
|
+
}
|
|
18321
|
+
};
|
|
18322
|
+
}
|
|
18323
|
+
|
|
18324
|
+
//#endregion
|
|
18325
|
+
//#region ../../packages/substrate-compose/src/routes/stream-session.ts
|
|
18326
|
+
/**
|
|
18327
|
+
* The frame key an engine registration error blames, or undefined when the
|
|
18328
|
+
* error isn't attributable to one key.
|
|
18329
|
+
*/
|
|
18330
|
+
function erroredKey(frame) {
|
|
18331
|
+
if (frame.kind !== "error") return void 0;
|
|
18248
18332
|
return frame.errors?.[0]?.path?.[0];
|
|
18249
18333
|
}
|
|
18250
18334
|
/**
|
|
18251
18335
|
* Close reason → the token the engine records as
|
|
18252
18336
|
* `engine_subscription_closed_<reason>`. Mapped here rather than per transport
|
|
18253
18337
|
* so metric names are identical whichever transport opens the session.
|
|
18254
|
-
* Pinned by apps/substrate/test/
|
|
18338
|
+
* Pinned by apps/substrate/test/stream-teardown-contract.test.ts.
|
|
18255
18339
|
*/
|
|
18256
18340
|
const ENGINE_CANCEL_REASON = {
|
|
18257
18341
|
complete: "cancel",
|
|
18258
18342
|
cancelled: "cancel",
|
|
18259
|
-
client_disconnect: "
|
|
18343
|
+
client_disconnect: "client_disconnect",
|
|
18260
18344
|
shutdown: "shutdown"
|
|
18261
18345
|
};
|
|
18262
18346
|
var StreamSession = class StreamSession {
|
|
@@ -18342,214 +18426,13 @@ var StreamSession = class StreamSession {
|
|
|
18342
18426
|
};
|
|
18343
18427
|
|
|
18344
18428
|
//#endregion
|
|
18345
|
-
//#region ../../packages/substrate-compose/src/routes/stream-
|
|
18346
|
-
/**
|
|
18347
|
-
* Stateless stream tickets. Spec §3.
|
|
18348
|
-
*
|
|
18349
|
-
* `EventSource` cannot set request headers, so Substrate's signed-request auth
|
|
18350
|
-
* can't reach the browser SSE path. A ticket is minted by a NORMALLY-SIGNED
|
|
18351
|
-
* POST and then carried in the query string.
|
|
18352
|
-
*
|
|
18353
|
-
* Stateless by design: no store, no TTL sweeper, and therefore no
|
|
18354
|
-
* mint-on-instance-A/connect-to-instance-B failure the moment a second
|
|
18355
|
-
* instance exists. Signed with a SERVER-held secret (symmetric HMAC is
|
|
18356
|
-
* sufficient — only the issuing server verifies) so the Key registry
|
|
18357
|
-
* stays out of a request-path hot loop. Rotating the secret invalidates
|
|
18358
|
-
* outstanding tickets, which is the revocation escape hatch statelessness
|
|
18359
|
-
* otherwise gives up; immaterial at a ~60s TTL.
|
|
18360
|
-
*/
|
|
18361
|
-
/** Chosen so an EventSource reconnect lands inside the window. */
|
|
18362
|
-
const TICKET_TTL_MS = 6e4;
|
|
18363
|
-
/**
|
|
18364
|
-
* Order-independent hash of a frame. Binding the ticket to this prevents
|
|
18365
|
-
* replaying a ticket minted for one frame onto a different (e.g. broader) one.
|
|
18366
|
-
*
|
|
18367
|
-
* HMAC rather than a plain `createHash('sha256')` — but `'substrate-frame-hash'`
|
|
18368
|
-
* is a DOMAIN-SEPARATION constant, not a secret, and doesn't need protecting.
|
|
18369
|
-
* The frame hash only needs collision resistance (plain SHA-256 gives that
|
|
18370
|
-
* too); its integrity against tampering is already covered by the outer
|
|
18371
|
-
* ticket signature in `sign`, which is keyed on the real server secret.
|
|
18372
|
-
*/
|
|
18373
|
-
function hashFrame(frame) {
|
|
18374
|
-
return createHmac("sha256", "substrate-frame-hash").update(canonicalJson(frame)).digest("base64url");
|
|
18375
|
-
}
|
|
18376
|
-
/** Deterministic JSON with lexicographically sorted keys at every depth. */
|
|
18377
|
-
function canonicalJson(value) {
|
|
18378
|
-
if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
|
|
18379
|
-
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
|
18380
|
-
return `{${Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
|
|
18381
|
-
}
|
|
18382
|
-
function sign$1(secret, body) {
|
|
18383
|
-
return createHmac("sha256", secret).update(body).digest("base64url");
|
|
18384
|
-
}
|
|
18385
|
-
function mintStreamTicket(secret, payload, nowMs) {
|
|
18386
|
-
const full = {
|
|
18387
|
-
...payload,
|
|
18388
|
-
exp: nowMs + TICKET_TTL_MS
|
|
18389
|
-
};
|
|
18390
|
-
const body = Buffer.from(JSON.stringify(full)).toString("base64url");
|
|
18391
|
-
return `${body}.${sign$1(secret, body)}`;
|
|
18392
|
-
}
|
|
18393
|
-
/** Full structural check of a decoded ticket body. See `verifyStreamTicket`. */
|
|
18394
|
-
function isTicketPayload(value) {
|
|
18395
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
18396
|
-
if (!("exp" in value) || typeof value.exp !== "number") return false;
|
|
18397
|
-
if (!("frameHash" in value) || typeof value.frameHash !== "string") return false;
|
|
18398
|
-
if (!("capabilities" in value)) return false;
|
|
18399
|
-
const caps = value.capabilities;
|
|
18400
|
-
if (!Array.isArray(caps) || caps.some((c) => typeof c !== "string")) return false;
|
|
18401
|
-
if (!("identity" in value)) return false;
|
|
18402
|
-
const identity = value.identity;
|
|
18403
|
-
if (identity === null) return true;
|
|
18404
|
-
return typeof identity === "object" && !Array.isArray(identity) && "id" in identity && typeof identity.id === "string";
|
|
18405
|
-
}
|
|
18406
|
-
function verifyStreamTicket(secret, ticket, frame, nowMs) {
|
|
18407
|
-
const dot = ticket.indexOf(".");
|
|
18408
|
-
if (dot <= 0) return {
|
|
18409
|
-
ok: false,
|
|
18410
|
-
code: "ticket_invalid"
|
|
18411
|
-
};
|
|
18412
|
-
const body = ticket.slice(0, dot);
|
|
18413
|
-
const provided = ticket.slice(dot + 1);
|
|
18414
|
-
const expected = sign$1(secret, body);
|
|
18415
|
-
const a = Buffer.from(provided);
|
|
18416
|
-
const b = Buffer.from(expected);
|
|
18417
|
-
if (a.length !== b.length || !timingSafeEqual(a, b)) return {
|
|
18418
|
-
ok: false,
|
|
18419
|
-
code: "ticket_invalid"
|
|
18420
|
-
};
|
|
18421
|
-
let parsed;
|
|
18422
|
-
try {
|
|
18423
|
-
parsed = JSON.parse(Buffer.from(body, "base64url").toString("utf8"));
|
|
18424
|
-
} catch {
|
|
18425
|
-
return {
|
|
18426
|
-
ok: false,
|
|
18427
|
-
code: "ticket_invalid"
|
|
18428
|
-
};
|
|
18429
|
-
}
|
|
18430
|
-
if (!isTicketPayload(parsed)) return {
|
|
18431
|
-
ok: false,
|
|
18432
|
-
code: "ticket_invalid"
|
|
18433
|
-
};
|
|
18434
|
-
const payload = parsed;
|
|
18435
|
-
if (nowMs > payload.exp) return {
|
|
18436
|
-
ok: false,
|
|
18437
|
-
code: "ticket_expired"
|
|
18438
|
-
};
|
|
18439
|
-
let expectedFrameHash;
|
|
18440
|
-
try {
|
|
18441
|
-
expectedFrameHash = hashFrame(frame);
|
|
18442
|
-
} catch {
|
|
18443
|
-
return {
|
|
18444
|
-
ok: false,
|
|
18445
|
-
code: "ticket_invalid"
|
|
18446
|
-
};
|
|
18447
|
-
}
|
|
18448
|
-
if (payload.frameHash !== expectedFrameHash) return {
|
|
18449
|
-
ok: false,
|
|
18450
|
-
code: "ticket_frame_mismatch"
|
|
18451
|
-
};
|
|
18452
|
-
return {
|
|
18453
|
-
ok: true,
|
|
18454
|
-
payload
|
|
18455
|
-
};
|
|
18456
|
-
}
|
|
18457
|
-
|
|
18458
|
-
//#endregion
|
|
18459
|
-
//#region ../../packages/substrate-compose/src/routes/sse.ts
|
|
18460
|
-
const DEFAULT_HEARTBEAT_MS = 2e4;
|
|
18461
|
-
const DEFAULT_RETRY_MS = 5e3;
|
|
18462
|
-
function createSseTransport(opts) {
|
|
18463
|
-
const encoder = new TextEncoder();
|
|
18464
|
-
const heartbeatMs = opts.heartbeatMs ?? DEFAULT_HEARTBEAT_MS;
|
|
18465
|
-
let controller = null;
|
|
18466
|
-
let heartbeat = null;
|
|
18467
|
-
let closed = false;
|
|
18468
|
-
/**
|
|
18469
|
-
* Resolvers of parked `emit` calls. Must be a Set, not a single slot: a
|
|
18470
|
-
* multi-key subscribe drains each key on its own task sharing one `onFrame`,
|
|
18471
|
-
* so two `emit`s can be in flight at once, and a single slot would strand
|
|
18472
|
-
* the first one's drain task.
|
|
18473
|
-
*/
|
|
18474
|
-
const demand = /* @__PURE__ */ new Set();
|
|
18475
|
-
const stopHeartbeat = () => {
|
|
18476
|
-
if (heartbeat !== null) {
|
|
18477
|
-
clearInterval(heartbeat);
|
|
18478
|
-
heartbeat = null;
|
|
18479
|
-
}
|
|
18480
|
-
};
|
|
18481
|
-
/** Wake every parked `emit`, not just one — see the `demand` docblock. */
|
|
18482
|
-
const releaseDemand = () => {
|
|
18483
|
-
for (const resolve of demand) resolve();
|
|
18484
|
-
demand.clear();
|
|
18485
|
-
};
|
|
18486
|
-
/**
|
|
18487
|
-
* Shared by every teardown path (write failure, close, consumer cancel) so
|
|
18488
|
-
* none of them can leave the heartbeat ticking or an `emit` parked forever;
|
|
18489
|
-
* a parked `emit` strands the engine's producer loop.
|
|
18490
|
-
*/
|
|
18491
|
-
const teardown = () => {
|
|
18492
|
-
closed = true;
|
|
18493
|
-
stopHeartbeat();
|
|
18494
|
-
releaseDemand();
|
|
18495
|
-
};
|
|
18496
|
-
const write = (text) => {
|
|
18497
|
-
if (closed || controller === null) return;
|
|
18498
|
-
try {
|
|
18499
|
-
controller.enqueue(encoder.encode(text));
|
|
18500
|
-
} catch {
|
|
18501
|
-
teardown();
|
|
18502
|
-
controller = null;
|
|
18503
|
-
}
|
|
18504
|
-
};
|
|
18505
|
-
return {
|
|
18506
|
-
transport: {
|
|
18507
|
-
emit: async (frame, fieldsByKey) => {
|
|
18508
|
-
if (closed) return;
|
|
18509
|
-
write(sseEvent(frame, fieldsByKey));
|
|
18510
|
-
if (frame.kind === "end" || frame.kind === "error") return;
|
|
18511
|
-
if (controller !== null && (controller.desiredSize ?? 1) <= 0) await new Promise((resolve) => {
|
|
18512
|
-
demand.add(resolve);
|
|
18513
|
-
});
|
|
18514
|
-
},
|
|
18515
|
-
close: (_reason) => {
|
|
18516
|
-
if (closed) return;
|
|
18517
|
-
teardown();
|
|
18518
|
-
try {
|
|
18519
|
-
controller?.close();
|
|
18520
|
-
} catch {}
|
|
18521
|
-
controller = null;
|
|
18522
|
-
}
|
|
18523
|
-
},
|
|
18524
|
-
body: new ReadableStream({
|
|
18525
|
-
start(c) {
|
|
18526
|
-
controller = c;
|
|
18527
|
-
write(sseRetry(opts.retryMs ?? DEFAULT_RETRY_MS));
|
|
18528
|
-
heartbeat = setInterval(() => write(SSE_HEARTBEAT), heartbeatMs);
|
|
18529
|
-
heartbeat.unref?.();
|
|
18530
|
-
},
|
|
18531
|
-
pull() {
|
|
18532
|
-
releaseDemand();
|
|
18533
|
-
},
|
|
18534
|
-
cancel() {
|
|
18535
|
-
teardown();
|
|
18536
|
-
controller = null;
|
|
18537
|
-
opts.onCancel?.();
|
|
18538
|
-
}
|
|
18539
|
-
}),
|
|
18540
|
-
writeFrame: (frame) => write(sseEvent(frame, void 0))
|
|
18541
|
-
};
|
|
18542
|
-
}
|
|
18543
|
-
/** The capability a stream ticket must carry, and the one the mint route grants. */
|
|
18544
|
-
const STREAM_CAPABILITY = "read";
|
|
18429
|
+
//#region ../../packages/substrate-compose/src/routes/stream-routes.ts
|
|
18545
18430
|
/**
|
|
18546
18431
|
* Shape validation: an object whose every VALUE is a plain object. Inner
|
|
18547
18432
|
* `params`/`fields` bags stay opaque — the engine validates those.
|
|
18548
18433
|
*
|
|
18549
18434
|
* The values must be checked here: `StreamSession` reads `entry.fields` off
|
|
18550
18435
|
* each one, and a throw after the transport opens leaks its heartbeat timer.
|
|
18551
|
-
* The WS route uses it too, where that throw would escape the message
|
|
18552
|
-
* listener and exit the process.
|
|
18553
18436
|
*
|
|
18554
18437
|
* Unlike `validateFrame` in `helpers.ts`, array values (the §5.13
|
|
18555
18438
|
* batched-mutate shape) are rejected: streams cannot carry them, so this
|
|
@@ -18569,65 +18452,19 @@ function parseFrameJson(raw) {
|
|
|
18569
18452
|
}
|
|
18570
18453
|
return isFrameRecord(parsed) ? parsed : null;
|
|
18571
18454
|
}
|
|
18572
|
-
/**
|
|
18573
|
-
* Resolve the frame + identity for a stream request.
|
|
18574
|
-
*
|
|
18575
|
-
* GET is the EventSource path: the frame rides the query string and auth rides
|
|
18576
|
-
* a ticket, because EventSource can set neither a body nor headers. POST is the
|
|
18577
|
-
* node/MCP path: frame in the body, auth already applied upstream by `gate`.
|
|
18578
|
-
*
|
|
18579
|
-
* Both paths feed the SAME `JSON.parse`, so a frame's value types are identical
|
|
18580
|
-
* either way — a ticket minted from a POST body verifies against the same frame
|
|
18581
|
-
* arriving via GET. Never coerce query values to strings.
|
|
18582
|
-
*/
|
|
18583
|
-
function parseStreamRequest(req, secret, nowMs) {
|
|
18584
|
-
const frame = parseFrameJson(req.method === "POST" ? req.body?.toString("utf8") ?? "" : req.query.frame ?? "");
|
|
18585
|
-
if (frame === null) return {
|
|
18586
|
-
ok: false,
|
|
18587
|
-
status: 400,
|
|
18588
|
-
code: "invalid_frame"
|
|
18589
|
-
};
|
|
18590
|
-
if (req.method === "POST") return {
|
|
18591
|
-
ok: true,
|
|
18592
|
-
frame,
|
|
18593
|
-
identity: req.identity ?? null
|
|
18594
|
-
};
|
|
18595
|
-
const ticket = req.query.ticket;
|
|
18596
|
-
if (ticket === void 0 || ticket === "") return {
|
|
18597
|
-
ok: false,
|
|
18598
|
-
status: 401,
|
|
18599
|
-
code: "ticket_missing"
|
|
18600
|
-
};
|
|
18601
|
-
const verdict = verifyStreamTicket(secret, ticket, frame, nowMs);
|
|
18602
|
-
if (!verdict.ok) return {
|
|
18603
|
-
ok: false,
|
|
18604
|
-
status: 401,
|
|
18605
|
-
code: verdict.code
|
|
18606
|
-
};
|
|
18607
|
-
if (!verdict.payload.capabilities.includes(STREAM_CAPABILITY)) return {
|
|
18608
|
-
ok: false,
|
|
18609
|
-
status: 401,
|
|
18610
|
-
code: "ticket_missing_capability"
|
|
18611
|
-
};
|
|
18612
|
-
return {
|
|
18613
|
-
ok: true,
|
|
18614
|
-
frame,
|
|
18615
|
-
identity: verdict.payload.identity
|
|
18616
|
-
};
|
|
18617
|
-
}
|
|
18618
18455
|
/** Errors BEFORE the stream opens are ordinary HTTP; after, they're in-band. */
|
|
18619
|
-
function errorResponse(status, code) {
|
|
18456
|
+
function errorResponse(status, code, message = code) {
|
|
18620
18457
|
return jsonResponse(status, { errors: [{
|
|
18621
|
-
message
|
|
18458
|
+
message,
|
|
18622
18459
|
code
|
|
18623
18460
|
}] });
|
|
18624
18461
|
}
|
|
18625
|
-
|
|
18462
|
+
const NOT_ACCEPTABLE = `streams answer ${FRAMES_JSON_MIME} or ${FRAMES_PACK_MIME}; send one in Accept`;
|
|
18463
|
+
function registerStreamRoutes(server, engine, trustedProxies, gate, metric, auth, connections) {
|
|
18626
18464
|
/**
|
|
18627
|
-
* Every
|
|
18628
|
-
*
|
|
18629
|
-
*
|
|
18630
|
-
* so without this `server.stop()` would wait on it forever.
|
|
18465
|
+
* Every stream response currently held open, for the graceful-shutdown walk.
|
|
18466
|
+
* A subscribe never self-ends, so without this `server.stop()` would wait on
|
|
18467
|
+
* it forever.
|
|
18631
18468
|
*/
|
|
18632
18469
|
const live = /* @__PURE__ */ new Set();
|
|
18633
18470
|
/**
|
|
@@ -18646,21 +18483,31 @@ function registerSseRoutes(server, engine, trustedProxies, secret, gate, metric,
|
|
|
18646
18483
|
});
|
|
18647
18484
|
const open = (mode) => async (req) => {
|
|
18648
18485
|
if (shuttingDown) return errorResponse(503, "server_shutting_down");
|
|
18649
|
-
const
|
|
18650
|
-
if (
|
|
18486
|
+
const frames = framesEncodingFromAccept(req.headers.accept);
|
|
18487
|
+
if (frames === void 0) return errorResponse(406, "not_acceptable", NOT_ACCEPTABLE);
|
|
18488
|
+
const frame = parseFrameJson(req.body?.toString("utf8") ?? "");
|
|
18489
|
+
if (frame === null) return errorResponse(400, "invalid_frame");
|
|
18490
|
+
const parsed = {
|
|
18491
|
+
frame,
|
|
18492
|
+
identity: req.identity ?? null
|
|
18493
|
+
};
|
|
18651
18494
|
const lapsed = auth === void 0 ? null : credentialLapse(parsed.identity, auth);
|
|
18652
18495
|
if (lapsed !== null) return errorResponse(401, lapsed);
|
|
18653
18496
|
const clientIp = extractClientIp(req, trustedProxies);
|
|
18654
18497
|
const release = connections === void 0 ? () => {} : connections.acquire(auth === void 0 ? null : parsed.identity, clientIp);
|
|
18655
18498
|
if (release === null) return errorResponse(429, "too_many_connections");
|
|
18656
|
-
const releaseSlot = sessionCap === void 0 ? () => {} : sessionCap.acquire(auth === void 0 ? null : parsed.identity, clientIp);
|
|
18657
|
-
if (releaseSlot === null) {
|
|
18658
|
-
release();
|
|
18659
|
-
return errorResponse(429, "too_many_sessions");
|
|
18660
|
-
}
|
|
18661
18499
|
let session;
|
|
18662
|
-
const sessionId = `
|
|
18663
|
-
const { transport, body, writeFrame } =
|
|
18500
|
+
const sessionId = `frames-${mode}`;
|
|
18501
|
+
const { transport, body, writeFrame } = createFramesTransport({
|
|
18502
|
+
mode: frames,
|
|
18503
|
+
engine,
|
|
18504
|
+
onCancel: () => session?.cancel("client_disconnect"),
|
|
18505
|
+
onStall: () => metric("stream_consumer_stalled", {
|
|
18506
|
+
mode,
|
|
18507
|
+
wire: frames
|
|
18508
|
+
}),
|
|
18509
|
+
onTooLarge: () => session?.cancel("cancelled")
|
|
18510
|
+
});
|
|
18664
18511
|
/**
|
|
18665
18512
|
* This response's entry in the shutdown registry. Closures over
|
|
18666
18513
|
* `session` rather than a field holding it, so the record can be built
|
|
@@ -18691,10 +18538,6 @@ function registerSseRoutes(server, engine, trustedProxies, secret, gate, metric,
|
|
|
18691
18538
|
let stopWatch = () => {};
|
|
18692
18539
|
/** Set while `start` is in flight. */
|
|
18693
18540
|
let starting;
|
|
18694
|
-
const freeSlots = () => {
|
|
18695
|
-
release();
|
|
18696
|
-
releaseSlot();
|
|
18697
|
-
};
|
|
18698
18541
|
const watched = {
|
|
18699
18542
|
emit: async (frame, fieldsByKey) => {
|
|
18700
18543
|
if (registering) {
|
|
@@ -18705,8 +18548,8 @@ function registerSseRoutes(server, engine, trustedProxies, secret, gate, metric,
|
|
|
18705
18548
|
},
|
|
18706
18549
|
close: (reason) => {
|
|
18707
18550
|
live.delete(record);
|
|
18708
|
-
if (starting === void 0)
|
|
18709
|
-
else starting.then(
|
|
18551
|
+
if (starting === void 0) release();
|
|
18552
|
+
else starting.then(release, release);
|
|
18710
18553
|
stopWatch();
|
|
18711
18554
|
transport.close(reason);
|
|
18712
18555
|
}
|
|
@@ -18722,7 +18565,7 @@ function registerSseRoutes(server, engine, trustedProxies, secret, gate, metric,
|
|
|
18722
18565
|
}, watched);
|
|
18723
18566
|
} catch (err) {
|
|
18724
18567
|
watched.close("complete");
|
|
18725
|
-
metric("
|
|
18568
|
+
metric("stream_start_failed", {
|
|
18726
18569
|
mode,
|
|
18727
18570
|
keys: Object.keys(parsed.frame ?? {}),
|
|
18728
18571
|
error: err instanceof Error ? err.message : String(err)
|
|
@@ -18738,7 +18581,7 @@ function registerSseRoutes(server, engine, trustedProxies, secret, gate, metric,
|
|
|
18738
18581
|
starting = started;
|
|
18739
18582
|
await started;
|
|
18740
18583
|
} catch (err) {
|
|
18741
|
-
metric("
|
|
18584
|
+
metric("stream_start_failed", {
|
|
18742
18585
|
mode,
|
|
18743
18586
|
keys: Object.keys(parsed.frame ?? {}),
|
|
18744
18587
|
error: err instanceof Error ? err.message : String(err)
|
|
@@ -18759,376 +18602,18 @@ function registerSseRoutes(server, engine, trustedProxies, secret, gate, metric,
|
|
|
18759
18602
|
if (mode === "subscribe" && keys.every((k) => failedKeys.has(k))) session.cancel("complete");
|
|
18760
18603
|
return {
|
|
18761
18604
|
status: 200,
|
|
18762
|
-
headers:
|
|
18605
|
+
headers: framesHeaders(frames),
|
|
18763
18606
|
body
|
|
18764
18607
|
};
|
|
18765
18608
|
};
|
|
18766
|
-
|
|
18767
|
-
|
|
18768
|
-
server.route("POST", path, gate(open(mode), "read"));
|
|
18769
|
-
}
|
|
18770
|
-
server.route("POST", "/substrate/v1/stream/ticket", gate(async (req) => {
|
|
18771
|
-
const frame = parseFrameJson(req.body?.toString("utf8") ?? "");
|
|
18772
|
-
if (frame === null) return errorResponse(400, "invalid_frame");
|
|
18773
|
-
const ticket = mintStreamTicket(secret, {
|
|
18774
|
-
identity: req.identity ?? null,
|
|
18775
|
-
capabilities: [STREAM_CAPABILITY],
|
|
18776
|
-
frameHash: hashFrame(frame)
|
|
18777
|
-
}, Date.now());
|
|
18778
|
-
return jsonResponse(200, {
|
|
18779
|
-
ticket,
|
|
18780
|
-
expiresIn: TICKET_TTL_MS / 1e3
|
|
18781
|
-
});
|
|
18782
|
-
}, "read"));
|
|
18783
|
-
}
|
|
18784
|
-
|
|
18785
|
-
//#endregion
|
|
18786
|
-
//#region ../../packages/substrate-compose/src/routes/ws.ts
|
|
18787
|
-
/**
|
|
18788
|
-
* Detect the negotiated subprotocol from the upgrade request. @fastify/websocket
|
|
18789
|
-
* surfaces the accepted subprotocol in `sec-websocket-protocol`; the client's
|
|
18790
|
-
* offered list arrives in the request headers the same way. We treat "the
|
|
18791
|
-
* header mentions `substrate-pack`" as opt-in — the caller's responsibility is to
|
|
18792
|
-
* advertise it at handshake.
|
|
18793
|
-
*/
|
|
18794
|
-
function usesSubstratePack(req) {
|
|
18795
|
-
const raw = req.headers["sec-websocket-protocol"];
|
|
18796
|
-
if (raw === void 0) return false;
|
|
18797
|
-
for (const clause of raw.split(",")) if (clause.trim() === "substrate-pack") return true;
|
|
18798
|
-
return false;
|
|
18799
|
-
}
|
|
18800
|
-
function parseRequestFrame(buf, mode) {
|
|
18801
|
-
try {
|
|
18802
|
-
const value = decodeWsEnvelope(buf, mode);
|
|
18803
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) return null;
|
|
18804
|
-
if (!isPlainRequestData(value)) return null;
|
|
18805
|
-
const v = value;
|
|
18806
|
-
if (typeof v.id !== "string") return null;
|
|
18807
|
-
if (v.op !== "subscribe" && v.op !== "stream" && v.op !== "cancel" && v.op !== "whoami") return null;
|
|
18808
|
-
if (v.frame !== void 0 && !isFrameRecord(v.frame)) return null;
|
|
18809
|
-
return value;
|
|
18810
|
-
} catch {
|
|
18811
|
-
return null;
|
|
18812
|
-
}
|
|
18813
|
-
}
|
|
18814
|
-
function serializeFrame(frame, mode, fieldsByKey, engine) {
|
|
18815
|
-
if (mode === "json") return stringifyJson(projectWsFrame(frame, fieldsByKey));
|
|
18816
|
-
const encoded = encodeDataForPack(frame, fieldsByKey, engine);
|
|
18817
|
-
const wire = encodeWsFrame(encoded, mode);
|
|
18818
|
-
return typeof wire === "string" ? wire : Buffer.from(wire);
|
|
18819
|
-
}
|
|
18820
|
-
/**
|
|
18821
|
-
* Produce a wire-ready frame object — for substrate-pack, positionally encode
|
|
18822
|
-
* `data` against the per-key FieldInput + the engine's federated schema. Remove
|
|
18823
|
-
* frames (no `data`) and error/end frames pass through untouched.
|
|
18824
|
-
*/
|
|
18825
|
-
function encodeDataForPack(frame, fieldsByKey, engine) {
|
|
18826
|
-
if (frame.data === void 0 || fieldsByKey === void 0) return frame;
|
|
18827
|
-
const schemas = engine.federatedSchema();
|
|
18828
|
-
const encoded = {};
|
|
18829
|
-
for (const [key, value] of Object.entries(frame.data)) {
|
|
18830
|
-
const parsed = parseQualifiedKey(key);
|
|
18831
|
-
if (parsed === null) {
|
|
18832
|
-
encoded[key] = value;
|
|
18833
|
-
continue;
|
|
18834
|
-
}
|
|
18835
|
-
const { ns, name } = parsed;
|
|
18836
|
-
const fields = fieldsByKey.get(key);
|
|
18837
|
-
const schema = schemas[ns];
|
|
18838
|
-
if (fields === void 0 || schema === void 0) {
|
|
18839
|
-
encoded[key] = value;
|
|
18840
|
-
continue;
|
|
18841
|
-
}
|
|
18842
|
-
const rawShape = frame.shapes?.[key];
|
|
18843
|
-
encoded[key] = encodeByShape(value, fields, schema, name, rawShape === "single" || rawShape === "list" ? rawShape : void 0, {
|
|
18844
|
-
schemas,
|
|
18845
|
-
currentNs: ns
|
|
18846
|
-
});
|
|
18847
|
-
}
|
|
18848
|
-
return {
|
|
18849
|
-
...frame,
|
|
18850
|
-
data: encoded
|
|
18851
|
-
};
|
|
18852
|
-
}
|
|
18853
|
-
function encodeByShape(value, fields, schema, typeName, shape, ctx) {
|
|
18854
|
-
if (value === null || value === void 0) return null;
|
|
18855
|
-
if (shape === "list") {
|
|
18856
|
-
if (!Array.isArray(value)) return value;
|
|
18857
|
-
return value.map((row) => encodePositional(row, fields, schema, typeName, ctx));
|
|
18858
|
-
}
|
|
18859
|
-
return encodePositional(value, fields, schema, typeName, ctx);
|
|
18860
|
-
}
|
|
18861
|
-
/**
|
|
18862
|
-
* Most logical sessions one socket may hold open. Each holds an engine
|
|
18863
|
-
* subscription, so an unbounded count is unbounded server work per socket.
|
|
18864
|
-
*/
|
|
18865
|
-
const MAX_SESSIONS_PER_SOCKET = 1024;
|
|
18866
|
-
function registerWsRoute(server, engine, trustedProxies, metric, auth, systemIdentity = null, connections, sessionCap) {
|
|
18867
|
-
server.websocket("/substrate/v1/ws", async (req, conn) => {
|
|
18868
|
-
const wsClientIp = extractClientIp(req, trustedProxies);
|
|
18869
|
-
let wsIdentity = auth === void 0 ? systemIdentity ?? void 0 : void 0;
|
|
18870
|
-
if (auth !== void 0) {
|
|
18871
|
-
const token = extractBearer(req.headers.authorization);
|
|
18872
|
-
if (!(auth.mode === "attribute" && token === null)) {
|
|
18873
|
-
const verdict = token === null ? null : authenticateBearer(token, auth);
|
|
18874
|
-
if (verdict === null || !verdict.ok) {
|
|
18875
|
-
const reason = verdict?.reason ?? null;
|
|
18876
|
-
const errorBody = reason === null ? {
|
|
18877
|
-
message: "unauthorized",
|
|
18878
|
-
code: "unauthorized"
|
|
18879
|
-
} : {
|
|
18880
|
-
message: `unauthorized: ${reason}`,
|
|
18881
|
-
code: "unauthorized",
|
|
18882
|
-
reason
|
|
18883
|
-
};
|
|
18884
|
-
try {
|
|
18885
|
-
conn.send(JSON.stringify({
|
|
18886
|
-
id: "",
|
|
18887
|
-
kind: "error",
|
|
18888
|
-
errors: [errorBody]
|
|
18889
|
-
}));
|
|
18890
|
-
} catch {}
|
|
18891
|
-
conn.close(4001, reason === null ? "unauthorized" : `unauthorized: ${reason}`);
|
|
18892
|
-
return;
|
|
18893
|
-
}
|
|
18894
|
-
wsIdentity = verdict.identity;
|
|
18895
|
-
}
|
|
18896
|
-
}
|
|
18897
|
-
const release = connections === void 0 ? () => {} : connections.acquire(auth === void 0 ? null : wsIdentity, wsClientIp);
|
|
18898
|
-
if (release === null) {
|
|
18899
|
-
try {
|
|
18900
|
-
conn.send(JSON.stringify({
|
|
18901
|
-
id: "",
|
|
18902
|
-
kind: "error",
|
|
18903
|
-
errors: [{
|
|
18904
|
-
message: "too many connections",
|
|
18905
|
-
code: "too_many_connections"
|
|
18906
|
-
}]
|
|
18907
|
-
}));
|
|
18908
|
-
} catch {}
|
|
18909
|
-
conn.close(1013, "too_many_connections");
|
|
18910
|
-
return;
|
|
18911
|
-
}
|
|
18912
|
-
const mode = usesSubstratePack(req) ? "pack" : "json";
|
|
18913
|
-
/**
|
|
18914
|
-
* One socket carries many logical sessions, so multiplexing lives here
|
|
18915
|
-
* rather than in `StreamSession`. `isStream` rides alongside because the
|
|
18916
|
-
* cancel acknowledgement differs by mode (see the cancel handler) and the
|
|
18917
|
-
* session deliberately doesn't expose its own mode.
|
|
18918
|
-
*/
|
|
18919
|
-
const sessions = /* @__PURE__ */ new Map();
|
|
18920
|
-
/** Raw send for frames that belong to no session (parse errors, cancel acks). */
|
|
18921
|
-
const sendBare = (frame) => {
|
|
18922
|
-
try {
|
|
18923
|
-
conn.send(serializeFrame(frame, mode, void 0, engine));
|
|
18924
|
-
} catch {}
|
|
18925
|
-
};
|
|
18926
|
-
/** `owner` lets a late close from a replaced session leave its successor's entry alone. */
|
|
18927
|
-
const transportFor = (id, owner, releaseSlot, startOf) => ({
|
|
18928
|
-
emit: async (frame, fieldsByKey) => {
|
|
18929
|
-
const wire = serializeFrame(frame, mode, fieldsByKey, engine);
|
|
18930
|
-
try {
|
|
18931
|
-
conn.send(wire);
|
|
18932
|
-
} catch {
|
|
18933
|
-
return;
|
|
18934
|
-
}
|
|
18935
|
-
if (frame.kind !== "end" && frame.kind !== "error" && conn.drain !== void 0) await conn.drain();
|
|
18936
|
-
},
|
|
18937
|
-
close: () => {
|
|
18938
|
-
const starting = startOf();
|
|
18939
|
-
if (starting === void 0) releaseSlot();
|
|
18940
|
-
else starting.then(releaseSlot, releaseSlot);
|
|
18941
|
-
if (sessions.get(id)?.session === owner()) sessions.delete(id);
|
|
18942
|
-
}
|
|
18943
|
-
});
|
|
18944
|
-
/**
|
|
18945
|
-
* Register BEFORE dispatching to the engine: a `cancel` frame can arrive in
|
|
18946
|
-
* the same synchronous batch as the `subscribe`/`stream` that precedes it
|
|
18947
|
-
* (memory transports invoke message listeners inline), and a session that
|
|
18948
|
-
* isn't in the map yet is a session that cancel cannot find.
|
|
18949
|
-
*/
|
|
18950
|
-
function openSession(request, sessionMode) {
|
|
18951
|
-
if (sessions.has(request.id)) {
|
|
18952
|
-
sendBare({
|
|
18953
|
-
id: request.id,
|
|
18954
|
-
kind: "error",
|
|
18955
|
-
errors: [{
|
|
18956
|
-
message: `session id '${request.id}' is already open`,
|
|
18957
|
-
code: "duplicate_id"
|
|
18958
|
-
}]
|
|
18959
|
-
});
|
|
18960
|
-
return;
|
|
18961
|
-
}
|
|
18962
|
-
if (sessions.size >= 1024) {
|
|
18963
|
-
sendBare({
|
|
18964
|
-
id: request.id,
|
|
18965
|
-
kind: "error",
|
|
18966
|
-
errors: [{
|
|
18967
|
-
message: `at most ${MAX_SESSIONS_PER_SOCKET} sessions per socket`,
|
|
18968
|
-
code: "too_many_sessions"
|
|
18969
|
-
}]
|
|
18970
|
-
});
|
|
18971
|
-
return;
|
|
18972
|
-
}
|
|
18973
|
-
const releaseSlot = sessionCap === void 0 ? () => {} : sessionCap.acquire(auth === void 0 ? null : wsIdentity, wsClientIp);
|
|
18974
|
-
if (releaseSlot === null) {
|
|
18975
|
-
sendBare({
|
|
18976
|
-
id: request.id,
|
|
18977
|
-
kind: "error",
|
|
18978
|
-
errors: [{
|
|
18979
|
-
message: `at most ${MAX_SESSIONS_PER_CLIENT} sessions per client`,
|
|
18980
|
-
code: "too_many_sessions"
|
|
18981
|
-
}]
|
|
18982
|
-
});
|
|
18983
|
-
return;
|
|
18984
|
-
}
|
|
18985
|
-
const keys = Object.keys(request.frame ?? {});
|
|
18986
|
-
const failedKeys = /* @__PURE__ */ new Set();
|
|
18987
|
-
let registering = true;
|
|
18988
|
-
/** Set while `start` is in flight. */
|
|
18989
|
-
let starting;
|
|
18990
|
-
const transport = transportFor(request.id, () => session, releaseSlot, () => starting);
|
|
18991
|
-
const session = StreamSession.create({
|
|
18992
|
-
id: request.id,
|
|
18993
|
-
mode: sessionMode,
|
|
18994
|
-
frame: request.frame ?? {},
|
|
18995
|
-
identity: wsIdentity ?? null,
|
|
18996
|
-
headers: req.headers,
|
|
18997
|
-
...wsClientIp !== void 0 ? { clientIp: wsClientIp } : {}
|
|
18998
|
-
}, {
|
|
18999
|
-
emit: (frame, fieldsByKey) => {
|
|
19000
|
-
if (registering) {
|
|
19001
|
-
const key = erroredKey(frame);
|
|
19002
|
-
if (key !== void 0) failedKeys.add(key);
|
|
19003
|
-
}
|
|
19004
|
-
return transport.emit(frame, fieldsByKey);
|
|
19005
|
-
},
|
|
19006
|
-
close: transport.close
|
|
19007
|
-
});
|
|
19008
|
-
sessions.set(request.id, {
|
|
19009
|
-
session,
|
|
19010
|
-
isStream: sessionMode === "stream"
|
|
19011
|
-
});
|
|
19012
|
-
const started = session.start(engine).finally(() => {
|
|
19013
|
-
starting = void 0;
|
|
19014
|
-
});
|
|
19015
|
-
starting = started;
|
|
19016
|
-
started.then(() => {
|
|
19017
|
-
registering = false;
|
|
19018
|
-
if (sessionMode === "subscribe" && keys.every((k) => failedKeys.has(k))) session.cancel("complete");
|
|
19019
|
-
}, (err) => {
|
|
19020
|
-
metric("ws_stream_start_failed", {
|
|
19021
|
-
mode: sessionMode,
|
|
19022
|
-
keys: Object.keys(request.frame ?? {}),
|
|
19023
|
-
error: err instanceof Error ? err.message : String(err)
|
|
19024
|
-
});
|
|
19025
|
-
releaseSlot();
|
|
19026
|
-
if (sessions.get(request.id)?.session === session) sessions.delete(request.id);
|
|
19027
|
-
sendBare({
|
|
19028
|
-
id: request.id,
|
|
19029
|
-
kind: "error",
|
|
19030
|
-
errors: [{
|
|
19031
|
-
message: "stream_start_failed",
|
|
19032
|
-
code: "stream_start_failed"
|
|
19033
|
-
}]
|
|
19034
|
-
});
|
|
19035
|
-
sendBare({
|
|
19036
|
-
id: request.id,
|
|
19037
|
-
kind: "end",
|
|
19038
|
-
final: true
|
|
19039
|
-
});
|
|
19040
|
-
});
|
|
19041
|
-
}
|
|
19042
|
-
conn.onMessage((buf) => {
|
|
19043
|
-
const request = parseRequestFrame(buf, mode);
|
|
19044
|
-
if (!request) {
|
|
19045
|
-
sendBare({
|
|
19046
|
-
id: "",
|
|
19047
|
-
kind: "error",
|
|
19048
|
-
errors: [{
|
|
19049
|
-
message: "invalid frame",
|
|
19050
|
-
code: "invalid_frame"
|
|
19051
|
-
}]
|
|
19052
|
-
});
|
|
19053
|
-
return;
|
|
19054
|
-
}
|
|
19055
|
-
if (request.op === "whoami") {
|
|
19056
|
-
const principal = wsIdentity === void 0 ? null : describeIdentity(wsIdentity);
|
|
19057
|
-
sendBare({
|
|
19058
|
-
id: request.id,
|
|
19059
|
-
kind: "whoami",
|
|
19060
|
-
data: {
|
|
19061
|
-
authEnabled: auth !== void 0,
|
|
19062
|
-
identity: auth === void 0 ? null : principal
|
|
19063
|
-
}
|
|
19064
|
-
});
|
|
19065
|
-
return;
|
|
19066
|
-
}
|
|
19067
|
-
if (request.op === "subscribe") {
|
|
19068
|
-
openSession(request, "subscribe");
|
|
19069
|
-
return;
|
|
19070
|
-
}
|
|
19071
|
-
if (request.op === "stream") {
|
|
19072
|
-
openSession(request, "stream");
|
|
19073
|
-
return;
|
|
19074
|
-
}
|
|
19075
|
-
if (request.op === "cancel") {
|
|
19076
|
-
const record = sessions.get(request.id);
|
|
19077
|
-
if (record === void 0) {
|
|
19078
|
-
sendBare({
|
|
19079
|
-
id: request.id,
|
|
19080
|
-
kind: "end",
|
|
19081
|
-
final: true
|
|
19082
|
-
});
|
|
19083
|
-
return;
|
|
19084
|
-
}
|
|
19085
|
-
record.session.cancel("cancelled");
|
|
19086
|
-
sessions.delete(request.id);
|
|
19087
|
-
if (!record.isStream) sendBare({
|
|
19088
|
-
id: request.id,
|
|
19089
|
-
kind: "end",
|
|
19090
|
-
final: true
|
|
19091
|
-
});
|
|
19092
|
-
}
|
|
19093
|
-
});
|
|
19094
|
-
const stopWatch = watchCredential(wsIdentity, auth, (lapse) => {
|
|
19095
|
-
for (const [id, record] of sessions) {
|
|
19096
|
-
sendBare({
|
|
19097
|
-
id,
|
|
19098
|
-
kind: "end",
|
|
19099
|
-
reason: lapse,
|
|
19100
|
-
final: true
|
|
19101
|
-
});
|
|
19102
|
-
record.session.cancel("cancelled");
|
|
19103
|
-
}
|
|
19104
|
-
sessions.clear();
|
|
19105
|
-
conn.close(4001, lapse);
|
|
19106
|
-
});
|
|
19107
|
-
conn.onClose(() => {
|
|
19108
|
-
release();
|
|
19109
|
-
stopWatch();
|
|
19110
|
-
for (const record of sessions.values()) record.session.cancel("client_disconnect");
|
|
19111
|
-
sessions.clear();
|
|
19112
|
-
});
|
|
19113
|
-
conn.onShutdown?.((reason) => {
|
|
19114
|
-
for (const [id, record] of sessions) {
|
|
19115
|
-
sendBare({
|
|
19116
|
-
id,
|
|
19117
|
-
kind: "end",
|
|
19118
|
-
reason,
|
|
19119
|
-
final: true
|
|
19120
|
-
});
|
|
19121
|
-
record.session.cancel("shutdown");
|
|
19122
|
-
}
|
|
19123
|
-
sessions.clear();
|
|
19124
|
-
});
|
|
19125
|
-
});
|
|
18609
|
+
server.route("POST", "/substrate/v1/stream", gate(open("stream"), "read"));
|
|
18610
|
+
server.route("POST", "/substrate/v1/subscribe", gate(open("subscribe"), "read"));
|
|
19126
18611
|
}
|
|
19127
18612
|
|
|
19128
18613
|
//#endregion
|
|
19129
18614
|
//#region ../../packages/substrate-compose/src/compose/routes.ts
|
|
19130
18615
|
async function registerCoreRoutes(args) {
|
|
19131
|
-
const { server, engine, gate, trustedProxies, auth,
|
|
18616
|
+
const { server, engine, gate, trustedProxies, auth, version, stats, routeEventMetric, substrateDataset, substrateKeyRegistry, datasets, edges, namespaces, coordinatorPlugins, systemIdentity, routeMetric, bridges, registerRateLimiter, identityDirectory, adminCapability, configReport, storageReport, promoteFields, fieldWarnings, poolStats } = args;
|
|
19132
18617
|
server.route("GET", "/health", healthRoute({
|
|
19133
18618
|
version: version ?? "0.0.0",
|
|
19134
18619
|
startedAt: Date.now(),
|
|
@@ -19150,8 +18635,7 @@ async function registerCoreRoutes(args) {
|
|
|
19150
18635
|
server.route("POST", "/substrate/v1/query", gate(queryRoute(engine, trustedProxies, auth !== void 0, args.pageLimits), "read"));
|
|
19151
18636
|
server.route("POST", "/substrate/v1/mutate", gate(mutateRoute(engine, trustedProxies, auth !== void 0), "mutate"));
|
|
19152
18637
|
const connections = createConnectionLimiter();
|
|
19153
|
-
|
|
19154
|
-
registerSseRoutes(server, engine, trustedProxies, streamTicketSecret !== void 0 && streamTicketSecret.length > 0 ? streamTicketSecret : "substrate-dev-insecure-stream-secret", gate, routeEventMetric, auth, connections, sessionCap);
|
|
18638
|
+
registerStreamRoutes(server, engine, trustedProxies, gate, routeEventMetric, auth, connections);
|
|
19155
18639
|
server.observeRequests?.((o) => stats.recordRequest(o.op, o.ok, o.durationMs));
|
|
19156
18640
|
server.route("GET", "/substrate/v1/metrics", gate(metricsRouteHandler(stats), "read"));
|
|
19157
18641
|
server.route("GET", "/substrate/v1/errors", gate(errorsRouteHandler(stats, {
|
|
@@ -19263,7 +18747,6 @@ async function registerCoreRoutes(args) {
|
|
|
19263
18747
|
server.route("POST", "/substrate/v1/_system/promote-fields", gate(adminStoreWide(promoteFieldsRoute(promoteFields)), "admin"));
|
|
19264
18748
|
server.route("POST", "/substrate/v1/_system/register", registerRoute(substrateDataset, substrateKeyRegistry, rateLimiter, { trustedProxies }));
|
|
19265
18749
|
server.route("GET", "/substrate/v1/whoami", whoamiRoute(auth));
|
|
19266
|
-
registerWsRoute(server, engine, trustedProxies, routeEventMetric, auth, systemIdentity, connections, sessionCap);
|
|
19267
18750
|
for (const plugin of coordinatorPlugins) {
|
|
19268
18751
|
if (plugin.routes === void 0) continue;
|
|
19269
18752
|
const rawRouter = withRouterRequestContext(server.createPluginRouter({ name: plugin.name }), trustedProxies, auth !== void 0);
|
|
@@ -19287,8 +18770,7 @@ function withRouterRequestContext(router, trustedProxies, authEnabled) {
|
|
|
19287
18770
|
post: (path, h) => router.post(path, w(h)),
|
|
19288
18771
|
put: (path, h) => router.put(path, w(h)),
|
|
19289
18772
|
delete: (path, h) => router.delete(path, w(h)),
|
|
19290
|
-
patch: (path, h) => router.patch(path, w(h))
|
|
19291
|
-
websocket: (path, h) => router.websocket(path, h)
|
|
18773
|
+
patch: (path, h) => router.patch(path, w(h))
|
|
19292
18774
|
};
|
|
19293
18775
|
}
|
|
19294
18776
|
|
|
@@ -19415,8 +18897,9 @@ async function compose(opts) {
|
|
|
19415
18897
|
}
|
|
19416
18898
|
}
|
|
19417
18899
|
async function composeInner(opts, cleanup) {
|
|
19418
|
-
const {
|
|
19419
|
-
|
|
18900
|
+
const { systemIdentity } = resolveBootGuards(opts);
|
|
18901
|
+
const cursorSealSecret = opts.cursorSealSecret?.trim();
|
|
18902
|
+
if (cursorSealSecret !== void 0 && cursorSealSecret.length > 0) setCursorSealSecret(cursorSealSecret);
|
|
19420
18903
|
const edges = opts.edges ?? new MemoryEdgesStore({ now: () => /* @__PURE__ */ new Date() });
|
|
19421
18904
|
const bridges = opts.bridges ?? NO_BRIDGES;
|
|
19422
18905
|
const datasets = new MemoryDatasetRegistry({ factory: (ns) => {
|
|
@@ -19575,7 +19058,6 @@ async function composeInner(opts, cleanup) {
|
|
|
19575
19058
|
trustedProxies,
|
|
19576
19059
|
pageLimits: opts.pageLimits ?? DEFAULT_PAGE_LIMITS,
|
|
19577
19060
|
auth,
|
|
19578
|
-
streamTicketSecret,
|
|
19579
19061
|
version: opts.version,
|
|
19580
19062
|
stats,
|
|
19581
19063
|
routeEventMetric,
|
|
@@ -19882,4 +19364,4 @@ function kernelMigrationTracking(sql) {
|
|
|
19882
19364
|
}
|
|
19883
19365
|
|
|
19884
19366
|
//#endregion
|
|
19885
|
-
export { publishSchema as $, METADATA_FILTER_FIELDS as $t, isReservedAutoCrudField as A, isAuthCause as An, encodeCursor as At,
|
|
19367
|
+
export { publishSchema as $, METADATA_FILTER_FIELDS as $t, isReservedAutoCrudField as A, isAuthCause as An, encodeCursor as At, createResolver as B, parseJsonBody as Bt, makeCtx as C, WALKS_IN_MUTATION as Cn, walkAggExprType as Ct, RESERVED_AUTOCRUD_FIELDS as D, WALK_BUDGET_EXCEEDED as Dn, endsWithEscape as Dt, selectAll as E, WALKS_UNSUPPORTED_STORE as En, LIKE_TRAILING_ESCAPE as Et, createResolverWithEdges as F, ROW_METADATA_FIELDS as Fn, HttpBridgeClient as Ft, listResolver as G, WS_REASON_SUBSCRIPTION_OVERLOADED as Gt, updateResolver as H, signHs256 as Ht, installAutoCrud as I, assertNoRepeatedIds as In, JSON_HEADERS as It, resolveIdentity as J, normaliseVocabWhere as Jt, getResolver as K, canonicalVocabValue as Kt, deleteManyResolver as L, canonicalize as Ln, buildContext as Lt, field as M, isResolverFrameError as Mn, fullBuiltinOperators as Mt, AutoCrudNameCollisionError as N, isRestrictViolation as Nn, applyExtensionResultMeta as Nt, RESERVED_AUTOCRUD_FIELD_NAMES as O, WALK_UNSUPPORTED_REF as On, CursorDecodeError as Ot, autoCrudOptionsOf as P, CLIENT_SUPPLIED_SERVER_FIELD as Pn, fieldRequested as Pt, loadPlugins as Q, METADATA_AUTHOR_FIELDS as Qt, updateManyResolver as R, BootstrapConfigError as Rn, error400 as Rt, EngineQueryError as S, VALUE_OUT_OF_RANGE as Sn, metadataExprType as St, queryOne as T, WALKS_NEED_PARTITIONED as Tn, isJsonPathPredicate as Tt, applyUpdatePatch as U, WS_CODE_SUBSCRIPTION_UNSUPPORTED_FRAME as Ut, deleteResolver as V, parseAuthConfig as Vt, deleteRow as W, WS_REASON_SERVER_RESTART as Wt, resolveEntityRow as X, vocabImplication as Xt, resolveEntityId as Y, storedVocabSpellings as Yt, MemoryEdgesStore as Z, RegistryBase as Zt, AUDIT_EVENT_TYPE as _, ReservedField as _n, getExtensionOperator as _t, composeFromManifests as a, isIsoTimestamp as an, forEachWalkHop as at, assertOrderedBy as b, STORE_BUSY as bn, unregisterExtensionOperator as bt, MemoryDataset as c, validateHardDeleteEnv as cn, preResolveExtensionOperatorsDetailed as ct, uuidFromName as d, FILTER_TOO_BROAD as dn, canRaise as dt, normaliseMetadataOrderBy as en, effectiveFilterOperators as et, emitFieldEvents as f, MissingReferentError as fn, isDateField as ft, buildAuditRow as g, ROW_POLICY_BLOCKS_WALK as gn, refNames as gt, SYSTEM_TYPE_DEFS as h, REGEX_BUDGET_EXCEEDED as hn, isRefOperand as ht, PluginLoadError as i, isImpossibleIsoTimestamp as in, saveSweepState as it, reservedMetadataFieldMessage as j, isBatchElementError as jn, normalizeOrderBy as jt, RESERVED_METADATA_FIELD_NAMES as k, filterTooBroad as kn, decodeCursor as kt, planServesShape as l, BatchElementError as ln, aggParts as lt, runInviteSweep as m, QUERY_TIMEOUT as mn, groupConjuncts as mt, kernelMigrationTracking as n, normaliseWalkNode as nn, loadSweepState as nt, DEFAULT_NAMESPACE as o, normalizeIsoTimestamp as on, sharedPicks as ot, migrateFieldValues as p, NamespaceMismatch as pn, bindingWalks as pt, UnattributedWriteError as q, normaliseVocabValues as qt, mountKernelRoute as r, createKeyedMutex as rn, runSweep as rt, compose as s, assertHardDeleteAllowed as sn, windowGroupBy as st, composeKernelPlugins as t, normaliseMetadataWhere as tn, applyObligations as tt, isStoreProfileName as u, BridgeUnreachableError as un, aggType as ut, INVITE_TYPE as v, ResolverFrameError as vn, listExtensionOperators as vt, queryAll as w, WALKS_IN_SUBSCRIPTION as wn, assertJsonPathPredicate as wt, defineNamespace as x, UnsupportedFilterError as xn, exprTypeOfField as xt, assertIndexDecl as y, RestrictViolation as yn, registerExtensionOperator as yt, purgeResolver as z, defineBootstrap as zn, jsonResponse as zt };
|