@mnmal-ai/substrate 1.0.0-alpha.2 → 1.0.0-alpha.20
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 +64 -14
- package/dist/boot-store-BaDqbBS-.js +4062 -0
- package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-BhiJKwfF.d.ts} +2 -87
- package/dist/cli-RRFphMtQ.js +3366 -0
- package/dist/client.browser.js +2485 -0
- package/dist/client.d.ts +273 -135
- package/dist/client.js +6 -6
- package/dist/cluster-worker.js +0 -1
- package/dist/{content-type-C7T28xNb.js → content-type-wLljot5M.js} +0 -1
- package/dist/{env-utils-CGNazUNt.js → env-utils-s25Z1YSA.js} +0 -1
- package/dist/{errors-D4vKuwz8.d.ts → errors-CYOaWZ8l.d.ts} +44 -4
- package/dist/{fake-DUVtKVSE.js → fake-DdXtb-m8.js} +0 -1
- package/dist/{index-D7mSOFlM.d.ts → index-ZKeYFHDD.d.ts} +7 -11
- package/dist/msgpackr-Dq18g6Ds.js +16 -0
- package/dist/node-version-BtK_Nq6z.js +32 -0
- package/dist/pack.d.ts +75 -11
- package/dist/pack.js +4 -4
- package/dist/pg-edges-store-C-IRv-O9.js +443 -0
- package/dist/{plugin-QuJN9pG_.js → plugin-MSbSZ6Al.js} +132 -8
- package/dist/proposals-bootstrap.js +6 -5
- package/dist/proposals.config.schema.json +5 -0
- package/dist/proposals.d.ts +17 -5
- package/dist/proposals.default-config.json +2 -1
- package/dist/proposals.js +2 -3
- package/dist/recall-bootstrap.js +5 -4
- package/dist/recall.config.schema.json +6 -0
- package/dist/recall.d.ts +35 -6
- package/dist/recall.js +2 -3
- package/dist/{repl-C52wyfMs.js → repl-Ob5OyQqj.js} +65 -33
- package/dist/{row-ByLFE2Wx.d.ts → row-CfsHSEfB.d.ts} +92 -12
- package/dist/run-B438GGJK.js +2435 -0
- package/dist/{plugin-t2g2wX0b.d.ts → schema-laGLMvMC.d.ts} +1287 -63
- package/dist/script-A9dxMRZA.js +143 -0
- package/dist/script-runner.js +77 -0
- package/dist/sdk.d.ts +227 -38
- package/dist/sdk.js +5 -110
- package/dist/semver-D_zdq0hi.js +17 -0
- package/dist/{src-DMzc3HfW.js → src-BNdhH_Hx.js} +745 -851
- package/dist/{src-CG7giuHr.js → src-BSL2yyVy.js} +779 -75
- package/dist/{src-BLgbLq2k.js → src-B_kHkbsr.js} +780 -330
- package/dist/{src-DWm0iA88.js → src-De9j33jv.js} +188 -87
- package/dist/src-DkxrpQOz.js +7709 -0
- package/dist/{row-DZ0Fnpc6.js → src-DmiUWOLp.js} +2 -1
- package/dist/{src-CHp3QyqB.js → src-S84ef4e3.js} +9177 -3377
- package/dist/substrate-admin.js +17 -782
- package/dist/substrate.js +4 -5487
- package/dist/testing.d.ts +730 -53
- package/dist/testing.js +20 -163
- package/dist/{ws-C72iVS5T.d.ts → ws-BT8kL1IF.d.ts} +4 -5
- package/package.json +26 -26
- package/dist/dataset-BQ8egGl5.js +0 -1535
- package/dist/schema-g4FQLr0V.d.ts +0 -433
- package/dist/src-CXKnqeTW.js +0 -2339
|
@@ -1,10 +1,315 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
1
|
+
import { c as makeTokenProvider, i as DEFAULT_CAPABILITIES, n as trimTrailingSlashes } from "./env-utils-s25Z1YSA.js";
|
|
2
|
+
import { D as isActiveAdminKeyRow, O as capabilitiesFor, T as SYSTEM_NAMESPACE } from "./src-DmiUWOLp.js";
|
|
3
|
+
import { N as parseQualifiedKey, S as decodeEnvelope, d as applyDecodePlanInPlace, f as buildDecodePlan, i as FRAMES_PACK_MIME, n as DEFAULT_STREAM_IDLE_MS, p as decodeWsEnvelope, r as FRAMES_JSON_MIME, s as decodeStreamFrames, u as withIdleTimeout, x as SchemaDriftError, y as mutationReturns } from "./src-B_kHkbsr.js";
|
|
4
|
+
import http2 from "node:http2";
|
|
5
5
|
import { signal } from "@preact/signals-core";
|
|
6
|
-
import { WebSocket } from "ws";
|
|
7
6
|
|
|
7
|
+
//#region ../../packages/substrate-client/src/transport/h2-fetch.ts
|
|
8
|
+
/**
|
|
9
|
+
* A fetch-shaped function over `node:http2`: the transport seam's HTTP/2
|
|
10
|
+
* adapter for Node and Bun (Todo #1850, Decision #625).
|
|
11
|
+
*
|
|
12
|
+
* One `ClientHttp2Session` per origin, opened on first use and shared by every
|
|
13
|
+
* request to it; concurrent first calls share the pending connect. A session is
|
|
14
|
+
* dropped on GOAWAY, close or error, and the next request opens a new one. An
|
|
15
|
+
* idle session is unref'd, so a short-lived process exits without `close()`.
|
|
16
|
+
* `http:` origins use HTTP/2 with prior knowledge (h2c); `https:` origins verify
|
|
17
|
+
* the server against `ca`, or the process's default trust (which includes
|
|
18
|
+
* NODE_EXTRA_CA_CERTS). Certificate checks are never turned off.
|
|
19
|
+
*
|
|
20
|
+
* Imports node builtins only.
|
|
21
|
+
*/
|
|
22
|
+
const DEFAULT_CONNECT_TIMEOUT_MS = 1e4;
|
|
23
|
+
/** Request headers HTTP/2 forbids (RFC 9113 §8.2.2); fetch callers may still set them. */
|
|
24
|
+
const CONNECTION_HEADERS = /* @__PURE__ */ new Set([
|
|
25
|
+
"connection",
|
|
26
|
+
"keep-alive",
|
|
27
|
+
"transfer-encoding",
|
|
28
|
+
"upgrade",
|
|
29
|
+
"proxy-connection",
|
|
30
|
+
"host"
|
|
31
|
+
]);
|
|
32
|
+
function abortError(signal) {
|
|
33
|
+
const reason = signal.reason;
|
|
34
|
+
if (reason instanceof Error && reason.name === "AbortError") return reason;
|
|
35
|
+
return new DOMException("This operation was aborted", "AbortError");
|
|
36
|
+
}
|
|
37
|
+
function toHeaderRecord(init) {
|
|
38
|
+
const out = {};
|
|
39
|
+
new Headers(init).forEach((value, name) => {
|
|
40
|
+
if (!CONNECTION_HEADERS.has(name)) out[name] = value;
|
|
41
|
+
});
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
function toBodyBytes(body) {
|
|
45
|
+
if (body === void 0 || body === null) return void 0;
|
|
46
|
+
if (typeof body === "string" || body instanceof Uint8Array) return body;
|
|
47
|
+
if (body instanceof ArrayBuffer) return new Uint8Array(body);
|
|
48
|
+
if (ArrayBuffer.isView(body)) return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
|
49
|
+
throw new TypeError("createH2Fetch: only string, ArrayBuffer and typed-array bodies are supported");
|
|
50
|
+
}
|
|
51
|
+
function createH2Fetch(opts = {}) {
|
|
52
|
+
const connectTimeoutMs = opts.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS;
|
|
53
|
+
const pool = /* @__PURE__ */ new Map();
|
|
54
|
+
/** Every session opened, connected or not, so close() can end them all. */
|
|
55
|
+
const sessions = /* @__PURE__ */ new Set();
|
|
56
|
+
function connect(origin) {
|
|
57
|
+
const existing = pool.get(origin);
|
|
58
|
+
if (existing !== void 0) return existing;
|
|
59
|
+
const pending = new Promise((resolve, reject) => {
|
|
60
|
+
const session = http2.connect(origin, origin.startsWith("https:") && opts.ca !== void 0 ? { ca: opts.ca } : {});
|
|
61
|
+
sessions.add(session);
|
|
62
|
+
session.once("close", () => sessions.delete(session));
|
|
63
|
+
const pooled = {
|
|
64
|
+
session,
|
|
65
|
+
holds: 0
|
|
66
|
+
};
|
|
67
|
+
let connected = false;
|
|
68
|
+
const drop = () => {
|
|
69
|
+
if (pool.get(origin) === pending) pool.delete(origin);
|
|
70
|
+
};
|
|
71
|
+
const fail = (err) => {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
drop();
|
|
74
|
+
if (!connected) reject(err);
|
|
75
|
+
};
|
|
76
|
+
const timer = setTimeout(() => {
|
|
77
|
+
session.destroy();
|
|
78
|
+
fail(/* @__PURE__ */ new Error(`connect to ${origin} timed out after ${connectTimeoutMs} ms`));
|
|
79
|
+
}, connectTimeoutMs);
|
|
80
|
+
timer.unref();
|
|
81
|
+
session.once("connect", () => {
|
|
82
|
+
clearTimeout(timer);
|
|
83
|
+
connected = true;
|
|
84
|
+
session.unref();
|
|
85
|
+
resolve(pooled);
|
|
86
|
+
});
|
|
87
|
+
session.on("error", fail);
|
|
88
|
+
session.on("goaway", drop);
|
|
89
|
+
session.on("close", () => fail(/* @__PURE__ */ new Error(`connection to ${origin} closed before it opened`)));
|
|
90
|
+
});
|
|
91
|
+
pending.catch(() => {});
|
|
92
|
+
pool.set(origin, pending);
|
|
93
|
+
return pending;
|
|
94
|
+
}
|
|
95
|
+
/** Forget `pooled` if it is still the origin's session, so the next request opens a new one. */
|
|
96
|
+
function forget(origin, pooled) {
|
|
97
|
+
const current = pool.get(origin);
|
|
98
|
+
if (current === void 0) return;
|
|
99
|
+
current.then((p) => {
|
|
100
|
+
if (p === pooled && pool.get(origin) === current) pool.delete(origin);
|
|
101
|
+
}, () => {});
|
|
102
|
+
}
|
|
103
|
+
const fetchH2 = async (input, init = {}) => {
|
|
104
|
+
const signal = init.signal ?? void 0;
|
|
105
|
+
if (signal?.aborted) throw abortError(signal);
|
|
106
|
+
const url = new URL(input);
|
|
107
|
+
const body = toBodyBytes(init.body);
|
|
108
|
+
const headers = {
|
|
109
|
+
...toHeaderRecord(init.headers),
|
|
110
|
+
":method": (init.method ?? "GET").toUpperCase(),
|
|
111
|
+
":path": `${url.pathname}${url.search}`
|
|
112
|
+
};
|
|
113
|
+
for (let attempt = 0;; attempt++) {
|
|
114
|
+
const pooled = await untilAborted(connect(url.origin), signal);
|
|
115
|
+
const outcome = await send(pooled, headers, body, init, signal);
|
|
116
|
+
if (outcome.kind === "response") return outcome.response;
|
|
117
|
+
forget(url.origin, pooled);
|
|
118
|
+
if (outcome.kind === "retry" && attempt === 0) continue;
|
|
119
|
+
throw outcome.error;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
return Object.assign(fetchH2, { async close() {
|
|
123
|
+
pool.clear();
|
|
124
|
+
await Promise.all([...sessions].map((session) => {
|
|
125
|
+
if (session.connecting) {
|
|
126
|
+
session.destroy();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
return session.closed ? void 0 : new Promise((r) => session.close(() => r()));
|
|
130
|
+
}));
|
|
131
|
+
} });
|
|
132
|
+
}
|
|
133
|
+
/** Keep the session ref'd while anyone waits on it; an idle or unread one lets the process exit. */
|
|
134
|
+
function hold(p) {
|
|
135
|
+
if (p.holds++ === 0) p.session.ref();
|
|
136
|
+
}
|
|
137
|
+
function release(p) {
|
|
138
|
+
if (--p.holds === 0 && !p.session.destroyed) p.session.unref();
|
|
139
|
+
}
|
|
140
|
+
function untilAborted(p, signal) {
|
|
141
|
+
if (signal === void 0) return p;
|
|
142
|
+
return new Promise((resolve, reject) => {
|
|
143
|
+
const onAbort = () => reject(abortError(signal));
|
|
144
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
145
|
+
p.then((v) => {
|
|
146
|
+
signal.removeEventListener("abort", onAbort);
|
|
147
|
+
resolve(v);
|
|
148
|
+
}, (e) => {
|
|
149
|
+
signal.removeEventListener("abort", onAbort);
|
|
150
|
+
reject(e);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const RETRYABLE_CODES = /* @__PURE__ */ new Set(["ERR_HTTP2_GOAWAY_SESSION", "ERR_HTTP2_INVALID_SESSION"]);
|
|
155
|
+
function send(pooled, headers, body, init, signal) {
|
|
156
|
+
return new Promise((resolve) => {
|
|
157
|
+
let stream;
|
|
158
|
+
try {
|
|
159
|
+
stream = pooled.session.request(headers, { endStream: body === void 0 });
|
|
160
|
+
} catch (err) {
|
|
161
|
+
const code = err.code;
|
|
162
|
+
resolve({
|
|
163
|
+
kind: code !== void 0 && RETRYABLE_CODES.has(code) ? "retry" : "error",
|
|
164
|
+
error: err
|
|
165
|
+
});
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
hold(pooled);
|
|
169
|
+
let settled = false;
|
|
170
|
+
const settle = (o) => {
|
|
171
|
+
if (settled) return;
|
|
172
|
+
settled = true;
|
|
173
|
+
release(pooled);
|
|
174
|
+
resolve(o);
|
|
175
|
+
};
|
|
176
|
+
const onAbort = () => {
|
|
177
|
+
stream.close(http2.constants.NGHTTP2_CANCEL);
|
|
178
|
+
settle({
|
|
179
|
+
kind: "error",
|
|
180
|
+
error: abortError(signal)
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
184
|
+
stream.once("close", () => {
|
|
185
|
+
signal?.removeEventListener("abort", onAbort);
|
|
186
|
+
const refused = stream.rstCode === http2.constants.NGHTTP2_REFUSED_STREAM;
|
|
187
|
+
settle({
|
|
188
|
+
kind: refused ? "retry" : "error",
|
|
189
|
+
error: /* @__PURE__ */ new Error(`stream closed before a response (code ${stream.rstCode})`)
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
stream.once("error", (err) => settle({
|
|
193
|
+
kind: "error",
|
|
194
|
+
error: err
|
|
195
|
+
}));
|
|
196
|
+
stream.once("response", (h) => {
|
|
197
|
+
const status = Number(h[":status"]);
|
|
198
|
+
const responseHeaders = new Headers();
|
|
199
|
+
for (const [name, value] of Object.entries(h)) {
|
|
200
|
+
if (name.startsWith(":") || value === void 0) continue;
|
|
201
|
+
for (const v of Array.isArray(value) ? value : [String(value)]) responseHeaders.append(name, v);
|
|
202
|
+
}
|
|
203
|
+
if (status >= 300 && status < 400 && responseHeaders.has("location") && init.redirect !== "manual") {
|
|
204
|
+
stream.close(http2.constants.NGHTTP2_CANCEL);
|
|
205
|
+
settle({
|
|
206
|
+
kind: "error",
|
|
207
|
+
error: /* @__PURE__ */ new TypeError(`redirect refused: ${status} to ${responseHeaders.get("location")}`)
|
|
208
|
+
});
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const nullBody = status === 204 || status === 205 || status === 304 || headers[":method"] === "HEAD";
|
|
212
|
+
let response;
|
|
213
|
+
try {
|
|
214
|
+
response = new Response(nullBody ? null : webBody(stream, pooled, signal), {
|
|
215
|
+
status,
|
|
216
|
+
headers: responseHeaders
|
|
217
|
+
});
|
|
218
|
+
} catch (err) {
|
|
219
|
+
stream.close(http2.constants.NGHTTP2_CANCEL);
|
|
220
|
+
settle({
|
|
221
|
+
kind: "error",
|
|
222
|
+
error: err
|
|
223
|
+
});
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (nullBody) stream.resume();
|
|
227
|
+
settle({
|
|
228
|
+
kind: "response",
|
|
229
|
+
response
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
if (body !== void 0) stream.end(body);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* The response body as a web stream with backpressure. The session stays ref'd
|
|
237
|
+
* only while the reader is waiting for data, so a body nobody reads lets the
|
|
238
|
+
* process exit. An unread body still holds its HTTP/2 stream open until the
|
|
239
|
+
* server finishes it: read or cancel every body. Cancelling resets the stream.
|
|
240
|
+
* Built by hand rather than with `Readable.toWeb`, which aborts h2 bodies early under Bun.
|
|
241
|
+
*/
|
|
242
|
+
function webBody(stream, pooled, signal) {
|
|
243
|
+
let done = false;
|
|
244
|
+
let holding = false;
|
|
245
|
+
const setHold = (on) => {
|
|
246
|
+
if (on === holding) return;
|
|
247
|
+
holding = on;
|
|
248
|
+
if (on) hold(pooled);
|
|
249
|
+
else release(pooled);
|
|
250
|
+
};
|
|
251
|
+
const finish = () => {
|
|
252
|
+
done = true;
|
|
253
|
+
setHold(false);
|
|
254
|
+
};
|
|
255
|
+
return new ReadableStream({
|
|
256
|
+
start(controller) {
|
|
257
|
+
stream.on("data", (chunk) => {
|
|
258
|
+
if (done) return;
|
|
259
|
+
controller.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
260
|
+
if ((controller.desiredSize ?? 1) <= 0) {
|
|
261
|
+
stream.pause();
|
|
262
|
+
setHold(false);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
stream.once("end", () => {
|
|
266
|
+
if (done) return;
|
|
267
|
+
finish();
|
|
268
|
+
if (signal?.aborted) controller.error(abortError(signal));
|
|
269
|
+
else controller.close();
|
|
270
|
+
});
|
|
271
|
+
stream.once("error", (err) => {
|
|
272
|
+
if (done) return;
|
|
273
|
+
finish();
|
|
274
|
+
controller.error(signal?.aborted ? abortError(signal) : err);
|
|
275
|
+
});
|
|
276
|
+
stream.once("close", () => {
|
|
277
|
+
if (done) return;
|
|
278
|
+
finish();
|
|
279
|
+
controller.error(signal?.aborted ? abortError(signal) : /* @__PURE__ */ new Error(`stream closed before its end (code ${stream.rstCode})`));
|
|
280
|
+
});
|
|
281
|
+
},
|
|
282
|
+
pull() {
|
|
283
|
+
if (done) return;
|
|
284
|
+
setHold(true);
|
|
285
|
+
stream.resume();
|
|
286
|
+
},
|
|
287
|
+
cancel() {
|
|
288
|
+
finish();
|
|
289
|
+
stream.close(http2.constants.NGHTTP2_CANCEL);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region ../../packages/substrate-client/src/transport/platform-fetch.ts
|
|
296
|
+
/**
|
|
297
|
+
* The default behind `substrateFetch` in Node and Bun: one process-wide HTTP/2
|
|
298
|
+
* session pool, opened on first use. Browser builds swap in
|
|
299
|
+
* `platform-fetch.browser.ts`.
|
|
300
|
+
*/
|
|
301
|
+
let pool;
|
|
302
|
+
const platformFetch = (input, init) => {
|
|
303
|
+
pool ??= createH2Fetch();
|
|
304
|
+
return pool(input, init);
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
//#endregion
|
|
308
|
+
//#region ../../packages/substrate-client/src/transport/default-fetch.ts
|
|
309
|
+
let current = platformFetch;
|
|
310
|
+
const substrateFetch = (input, init) => current(input, init);
|
|
311
|
+
|
|
312
|
+
//#endregion
|
|
8
313
|
//#region ../../packages/substrate-client/src/admin/diagnose.ts
|
|
9
314
|
/**
|
|
10
315
|
* The library behind `substrate-admin doctor` and substrate-tui's `/doctor` — diagnoses
|
|
@@ -27,6 +332,7 @@ const CHECK_ADMIN = "admin-capability";
|
|
|
27
332
|
const CHECK_FLOOR = "admin-floor";
|
|
28
333
|
const CHECK_NAMESPACES = "namespaces";
|
|
29
334
|
const CHECK_DIR = "key-directory";
|
|
335
|
+
const CHECK_STORAGE = "storage";
|
|
30
336
|
/** Every check downstream of a failure, so a skip is never silent. */
|
|
31
337
|
const DOWNSTREAM = {
|
|
32
338
|
[CHECK_REACHABLE]: [
|
|
@@ -36,21 +342,28 @@ const DOWNSTREAM = {
|
|
|
36
342
|
CHECK_ADMIN,
|
|
37
343
|
CHECK_FLOOR,
|
|
38
344
|
CHECK_NAMESPACES,
|
|
39
|
-
CHECK_DIR
|
|
345
|
+
CHECK_DIR,
|
|
346
|
+
CHECK_STORAGE
|
|
40
347
|
],
|
|
41
348
|
[CHECK_CREDENTIAL]: [
|
|
42
349
|
CHECK_IDENTITY,
|
|
43
350
|
CHECK_ADMIN,
|
|
44
351
|
CHECK_FLOOR,
|
|
45
352
|
CHECK_NAMESPACES,
|
|
46
|
-
CHECK_DIR
|
|
353
|
+
CHECK_DIR,
|
|
354
|
+
CHECK_STORAGE
|
|
47
355
|
],
|
|
48
356
|
[CHECK_IDENTITY]: [
|
|
49
357
|
CHECK_ADMIN,
|
|
50
358
|
CHECK_FLOOR,
|
|
51
|
-
CHECK_DIR
|
|
359
|
+
CHECK_DIR,
|
|
360
|
+
CHECK_STORAGE
|
|
361
|
+
],
|
|
362
|
+
[CHECK_ADMIN]: [
|
|
363
|
+
CHECK_FLOOR,
|
|
364
|
+
CHECK_DIR,
|
|
365
|
+
CHECK_STORAGE
|
|
52
366
|
],
|
|
53
|
-
[CHECK_ADMIN]: [CHECK_FLOOR, CHECK_DIR],
|
|
54
367
|
[CHECK_FLOOR]: [CHECK_DIR]
|
|
55
368
|
};
|
|
56
369
|
var Checks = class {
|
|
@@ -276,7 +589,8 @@ const DIAGNOSE_CHECKS = [
|
|
|
276
589
|
CHECK_ADMIN,
|
|
277
590
|
CHECK_FLOOR,
|
|
278
591
|
CHECK_NAMESPACES,
|
|
279
|
-
CHECK_DIR
|
|
592
|
+
CHECK_DIR,
|
|
593
|
+
CHECK_STORAGE
|
|
280
594
|
];
|
|
281
595
|
/**
|
|
282
596
|
* Runs every check against `opts.baseUrl`, in dependency order. A failure
|
|
@@ -284,7 +598,7 @@ const DIAGNOSE_CHECKS = [
|
|
|
284
598
|
*/
|
|
285
599
|
async function diagnose(opts) {
|
|
286
600
|
const checks = new Checks();
|
|
287
|
-
const fetchImpl = opts.fetch ??
|
|
601
|
+
const fetchImpl = opts.fetch ?? substrateFetch;
|
|
288
602
|
const adminCapability = opts.adminCapability ?? "admin";
|
|
289
603
|
const baseUrl = trimTrailingSlashes(opts.baseUrl);
|
|
290
604
|
let identityId = null;
|
|
@@ -350,7 +664,8 @@ async function diagnose(opts) {
|
|
|
350
664
|
for (const name of [
|
|
351
665
|
CHECK_ADMIN,
|
|
352
666
|
CHECK_FLOOR,
|
|
353
|
-
CHECK_DIR
|
|
667
|
+
CHECK_DIR,
|
|
668
|
+
CHECK_STORAGE
|
|
354
669
|
]) checks.skip(name, "not asked — this identity was not expected to be an admin");
|
|
355
670
|
return done();
|
|
356
671
|
}
|
|
@@ -364,6 +679,16 @@ async function diagnose(opts) {
|
|
|
364
679
|
return done();
|
|
365
680
|
}
|
|
366
681
|
checks.ok(CHECK_ADMIN, `the grant confers '${adminCapability}' on ${SYSTEM_NAMESPACE} — this identity can administer`);
|
|
682
|
+
const config = await getJson(`${baseUrl}/substrate/v1/config`, fetchImpl, { authorization });
|
|
683
|
+
const storage = "error" in config ? void 0 : config.body.storage;
|
|
684
|
+
if ("error" in config || config.status !== 200) checks.skip(CHECK_STORAGE, `not asked — /substrate/v1/config ${"error" in config ? `could not be reached: ${config.error}` : `answered HTTP ${config.status}`}`);
|
|
685
|
+
else if (storage === void 0) checks.skip(CHECK_STORAGE, "not asked — the server reports no storage section (memory store)");
|
|
686
|
+
else {
|
|
687
|
+
const warnings = Array.isArray(storage.warnings) ? storage.warnings.map(String) : [];
|
|
688
|
+
const info = Array.isArray(storage.info) ? storage.info.map((l) => `\n${String(l)}`).join("") : "";
|
|
689
|
+
if (warnings.length === 0) checks.ok(CHECK_STORAGE, `${String(storage.layout)} layout, no warnings${info}`);
|
|
690
|
+
else checks.no(CHECK_STORAGE, `${String(storage.layout)} layout, ${warnings.length} warning(s):\n${warnings.join("\n")}${info}`, "Each warning names what to move or drop; indexes are never dropped automatically.");
|
|
691
|
+
}
|
|
367
692
|
const floor = await listLiveKeys(baseUrl, authorization, fetchImpl);
|
|
368
693
|
if ("error" in floor) {
|
|
369
694
|
checks.no(CHECK_FLOOR, `could not read ${SYSTEM_NAMESPACE}/Key: ${floor.error}`, "The identity is admin, so this is a route or store problem rather than a permission one — read `journalctl -u substrate-binary`.");
|
|
@@ -903,7 +1228,7 @@ const SURVIVABLE_END_REASONS = /* @__PURE__ */ new Set([
|
|
|
903
1228
|
* A delta that does not change the set (e.g. an `update` whose row has no id) is
|
|
904
1229
|
* not re-yielded. Cancellation: breaking the consuming `for await` (or calling
|
|
905
1230
|
* `.return()`) propagates to the source iterable's `return()` through the inner
|
|
906
|
-
* `for await`, cancelling the underlying
|
|
1231
|
+
* `for await`, cancelling the underlying subscription.
|
|
907
1232
|
*/
|
|
908
1233
|
async function* watchRows(events, qualifiedKey) {
|
|
909
1234
|
let rows = [];
|
|
@@ -1050,7 +1375,11 @@ var CacheStore = class {
|
|
|
1050
1375
|
|
|
1051
1376
|
//#endregion
|
|
1052
1377
|
//#region ../../packages/substrate-client/src/diagnostics.ts
|
|
1053
|
-
const HANDLED = /* @__PURE__ */ new Set([
|
|
1378
|
+
const HANDLED = /* @__PURE__ */ new Set([
|
|
1379
|
+
"unknownProjectionFields",
|
|
1380
|
+
"filterValidationSkipped",
|
|
1381
|
+
"plan"
|
|
1382
|
+
]);
|
|
1054
1383
|
/**
|
|
1055
1384
|
* Reduce one envelope entry to a diagnostic, or `undefined` when it holds
|
|
1056
1385
|
* nothing worth reporting — an entry present but empty, or one whose only
|
|
@@ -1116,69 +1445,282 @@ function createDefaultDiagnosticsSink() {
|
|
|
1116
1445
|
seen.add(dedupeKey);
|
|
1117
1446
|
const parts = [];
|
|
1118
1447
|
if (fields.length > 0) parts.push(`projected field(s) the type does not declare: ${fields.join(", ")}. The read succeeded — these are null on EVERY row, which is indistinguishable from a declared field that is always null. Check for a typo, or a substrate that has not restarted with your schema yet.`);
|
|
1119
|
-
if (filterValidationSkipped !== void 0) parts.push(`the where-clause was not schema-validated (${filterValidationSkipped}), so
|
|
1448
|
+
if (filterValidationSkipped !== void 0) parts.push(`the where-clause was not schema-validated (${filterValidationSkipped}), so its operators and operands were never checked against the field kinds. Results are still correct — the substrate refuses a predicate it cannot honour — but a malformed filter fails later and less clearly.`);
|
|
1120
1449
|
if (unrecognized.length > 0) parts.push(`the substrate reported diagnostic(s) this client cannot read: ${unrecognized.join(", ")}. Upgrade @mnmal-ai/substrate-client to surface them.`);
|
|
1121
1450
|
console.warn(`[substrate-client] ${opKey}: ${parts.join(" Also, ")} (Pass \`onDiagnostics\` to SubstrateClient to route this elsewhere.)`);
|
|
1122
1451
|
};
|
|
1123
1452
|
}
|
|
1124
1453
|
|
|
1125
1454
|
//#endregion
|
|
1126
|
-
//#region ../../packages/substrate-client/src/
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
if (data.length === 0) return null;
|
|
1137
|
-
const raw = data.join("\n");
|
|
1138
|
-
let parsed;
|
|
1139
|
-
try {
|
|
1140
|
-
parsed = JSON.parse(raw);
|
|
1141
|
-
} catch (err) {
|
|
1142
|
-
throw new NetworkError(`malformed SSE data for event '${event}': ${raw.slice(0, 100)}`, err);
|
|
1455
|
+
//#region ../../packages/substrate-client/src/reconnect.ts
|
|
1456
|
+
var ReconnectScheduler = class {
|
|
1457
|
+
#base;
|
|
1458
|
+
#max;
|
|
1459
|
+
#jitter;
|
|
1460
|
+
#attempt = 0;
|
|
1461
|
+
constructor(opts) {
|
|
1462
|
+
this.#base = opts.baseDelayMs;
|
|
1463
|
+
this.#max = opts.maxDelayMs;
|
|
1464
|
+
this.#jitter = opts.jitter;
|
|
1143
1465
|
}
|
|
1144
|
-
|
|
1145
|
-
const
|
|
1146
|
-
|
|
1466
|
+
nextDelay() {
|
|
1467
|
+
const raw = Math.min(this.#base * 2 ** this.#attempt, this.#max);
|
|
1468
|
+
this.#attempt += 1;
|
|
1469
|
+
if (this.#jitter === 0) return raw;
|
|
1470
|
+
const spread = raw * this.#jitter;
|
|
1471
|
+
return raw + (Math.random() * 2 - 1) * spread;
|
|
1472
|
+
}
|
|
1473
|
+
reset() {
|
|
1474
|
+
this.#attempt = 0;
|
|
1475
|
+
}
|
|
1476
|
+
/**
|
|
1477
|
+
* Resolve after `nextDelay()` ms. Callers must use this serially — the
|
|
1478
|
+
* `#attempt` counter is shared across calls, so overlapping `schedule()`
|
|
1479
|
+
* invocations will produce incorrect delays. The stream reconnect loop awaits
|
|
1480
|
+
* each `schedule()` before firing the next attempt.
|
|
1481
|
+
*/
|
|
1482
|
+
async schedule() {
|
|
1483
|
+
const d = this.nextDelay();
|
|
1484
|
+
await new Promise((resolve) => setTimeout(resolve, d));
|
|
1485
|
+
}
|
|
1486
|
+
};
|
|
1487
|
+
|
|
1488
|
+
//#endregion
|
|
1489
|
+
//#region ../../packages/substrate-client/src/transport/frame-event.ts
|
|
1490
|
+
/** Server response frames as the subscribe and stream iterables yield them. */
|
|
1491
|
+
const DEFAULT_RECONNECT = {
|
|
1492
|
+
baseDelayMs: 1e3,
|
|
1493
|
+
maxDelayMs: 3e4,
|
|
1494
|
+
jitter: .1
|
|
1495
|
+
};
|
|
1496
|
+
const DEFAULT_OVERLOAD_RESUBSCRIBE = {
|
|
1497
|
+
enabled: true,
|
|
1498
|
+
maxAttempts: 3,
|
|
1499
|
+
baseDelayMs: 1e3,
|
|
1500
|
+
maxDelayMs: 3e4,
|
|
1501
|
+
jitter: .1,
|
|
1502
|
+
healthyResetMs: 6e4
|
|
1503
|
+
};
|
|
1504
|
+
/** Convert a server frame to a SubscribeEvent, or null if unrecognised. */
|
|
1505
|
+
function msgToEvent(msg) {
|
|
1506
|
+
switch (msg.kind) {
|
|
1507
|
+
case "snapshot":
|
|
1508
|
+
case "add":
|
|
1509
|
+
case "update": return {
|
|
1510
|
+
kind: msg.kind,
|
|
1511
|
+
data: msg.data,
|
|
1512
|
+
...msg.shapes !== void 0 ? { shapes: msg.shapes } : {}
|
|
1513
|
+
};
|
|
1514
|
+
case "chunk": return {
|
|
1515
|
+
kind: "chunk",
|
|
1516
|
+
data: msg.data,
|
|
1517
|
+
...msg.shapes !== void 0 ? { shapes: msg.shapes } : {},
|
|
1518
|
+
...msg.sequence !== void 0 ? { sequence: msg.sequence } : {}
|
|
1519
|
+
};
|
|
1520
|
+
case "remove": return {
|
|
1521
|
+
kind: "remove",
|
|
1522
|
+
removedId: msg.removedId ?? ""
|
|
1523
|
+
};
|
|
1524
|
+
case "end": return {
|
|
1525
|
+
kind: "end",
|
|
1526
|
+
...msg.final !== void 0 ? { final: msg.final } : {},
|
|
1527
|
+
...msg.reason !== void 0 ? { reason: msg.reason } : {}
|
|
1528
|
+
};
|
|
1529
|
+
case "error": {
|
|
1530
|
+
const first = (msg.errors ?? [{
|
|
1531
|
+
message: "unknown error",
|
|
1532
|
+
code: "unknown"
|
|
1533
|
+
}])[0] ?? {
|
|
1534
|
+
message: "unknown error",
|
|
1535
|
+
code: "unknown"
|
|
1536
|
+
};
|
|
1537
|
+
throw new SubscribeError(first.message, first.code ?? "unknown");
|
|
1538
|
+
}
|
|
1539
|
+
default: return null;
|
|
1147
1540
|
}
|
|
1148
|
-
return {
|
|
1149
|
-
event,
|
|
1150
|
-
data: parsed
|
|
1151
|
-
};
|
|
1152
1541
|
}
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1542
|
+
|
|
1543
|
+
//#endregion
|
|
1544
|
+
//#region ../../packages/substrate-client/src/transport/frames.ts
|
|
1545
|
+
/**
|
|
1546
|
+
* Subscribe and stream over length-prefixed frame streams: one POST per
|
|
1547
|
+
* subscription, its response body a sequence of frames (substrate-pack's
|
|
1548
|
+
* stream-frames codec). Each subscription owns its request, so cancelling one
|
|
1549
|
+
* aborts only that request.
|
|
1550
|
+
*
|
|
1551
|
+
* A subscription survives the following, re-subscribing for a fresh
|
|
1552
|
+
* snapshot: a dropped connection (after backoff), `end { server_restart }`
|
|
1553
|
+
* (at once), `subscription_overloaded` (after the overload delay, within its
|
|
1554
|
+
* budget) and a lapsed credential when `getToken` can mint a new one. A 401 or
|
|
1555
|
+
* 403 is terminal. A stream is never re-sent, since that would replay its chunks.
|
|
1556
|
+
*/
|
|
1557
|
+
/** `NetworkError.code` when the server does not answer with frame streams. */
|
|
1558
|
+
const FRAMES_UNSUPPORTED = "frames_unsupported";
|
|
1559
|
+
/** `NetworkError.code` when a stream was cut short by a server restart. */
|
|
1560
|
+
const STREAM_INTERRUPTED = "stream_interrupted";
|
|
1561
|
+
const OVERLOADED = "subscription_overloaded";
|
|
1562
|
+
const SERVER_RESTART = "server_restart";
|
|
1563
|
+
const LAPSES = /* @__PURE__ */ new Set(["credential_expired", "credential_changed"]);
|
|
1564
|
+
function openFrameStream(http, path, frame, opts) {
|
|
1565
|
+
return { [Symbol.asyncIterator]: () => {
|
|
1566
|
+
const ctrl = new AbortController();
|
|
1567
|
+
const gen = run(http, path, frame, opts, opts.signal === void 0 ? ctrl.signal : anySignal([ctrl.signal, opts.signal]));
|
|
1568
|
+
return {
|
|
1569
|
+
next: () => gen.next(),
|
|
1570
|
+
return: async () => {
|
|
1571
|
+
ctrl.abort();
|
|
1572
|
+
await gen.return(void 0);
|
|
1573
|
+
return {
|
|
1574
|
+
value: void 0,
|
|
1575
|
+
done: true
|
|
1576
|
+
};
|
|
1577
|
+
}
|
|
1578
|
+
};
|
|
1579
|
+
} };
|
|
1580
|
+
}
|
|
1581
|
+
async function* run(http, path, frame, opts, signal) {
|
|
1582
|
+
const subscribe = path === "/substrate/v1/subscribe";
|
|
1583
|
+
const reconnect = {
|
|
1584
|
+
...DEFAULT_RECONNECT,
|
|
1585
|
+
...opts.reconnect
|
|
1586
|
+
};
|
|
1587
|
+
const backoff = new ReconnectScheduler(reconnect);
|
|
1588
|
+
const overload = {
|
|
1589
|
+
...DEFAULT_OVERLOAD_RESUBSCRIBE,
|
|
1590
|
+
...opts.overload
|
|
1591
|
+
};
|
|
1592
|
+
let overloadAttempts = 0;
|
|
1593
|
+
let lastOverloadAt = null;
|
|
1594
|
+
let opened = false;
|
|
1595
|
+
let busyRetries = 0;
|
|
1596
|
+
const accept = opts.wire === "pack" ? FRAMES_PACK_MIME : FRAMES_JSON_MIME;
|
|
1597
|
+
for (;;) {
|
|
1598
|
+
let body;
|
|
1599
|
+
try {
|
|
1600
|
+
const res = await http.raw(path, {
|
|
1601
|
+
method: "POST",
|
|
1602
|
+
body: frame,
|
|
1603
|
+
accept,
|
|
1604
|
+
signal
|
|
1605
|
+
});
|
|
1606
|
+
const type = res.headers.get("content-type") ?? "";
|
|
1607
|
+
if (!type.startsWith(accept)) {
|
|
1608
|
+
await res.body?.cancel();
|
|
1609
|
+
throw new NetworkError(`POST ${path} answered '${type}', not ${accept}: the server predates frame streams`, null, {
|
|
1610
|
+
status: res.status,
|
|
1611
|
+
code: FRAMES_UNSUPPORTED
|
|
1612
|
+
});
|
|
1169
1613
|
}
|
|
1614
|
+
if (res.body === null) throw new NetworkError(`POST ${path}: response has no body`, null);
|
|
1615
|
+
body = res.body;
|
|
1616
|
+
} catch (err) {
|
|
1617
|
+
if (signal.aborted) return;
|
|
1618
|
+
if (!opened && isBusy(err) && busyRetries < BUSY_RETRIES) {
|
|
1619
|
+
busyRetries++;
|
|
1620
|
+
if (!await sleep(backoff.nextDelay(), signal)) return;
|
|
1621
|
+
continue;
|
|
1622
|
+
}
|
|
1623
|
+
if (err instanceof AuthError || !opened || !subscribe || refused(err)) throw err;
|
|
1624
|
+
if (!await sleep(backoff.nextDelay(), signal)) return;
|
|
1625
|
+
continue;
|
|
1626
|
+
}
|
|
1627
|
+
opened = true;
|
|
1628
|
+
const openedAt = Date.now();
|
|
1629
|
+
let next;
|
|
1630
|
+
try {
|
|
1631
|
+
const idleMs = opts.idleMs ?? 65e3;
|
|
1632
|
+
for await (const payload of decodeStreamFrames(idleMs > 0 ? withIdleTimeout(body, idleMs) : body)) {
|
|
1633
|
+
const msg = decodeFrame(payload, opts.wire);
|
|
1634
|
+
if (Date.now() - openedAt >= reconnect.maxDelayMs) backoff.reset();
|
|
1635
|
+
if (subscribe && msg.kind === "error" && msg.errors?.[0]?.code === OVERLOADED && overload.enabled) {
|
|
1636
|
+
const now = Date.now();
|
|
1637
|
+
if (lastOverloadAt !== null && now - lastOverloadAt > overload.healthyResetMs) overloadAttempts = 0;
|
|
1638
|
+
lastOverloadAt = now;
|
|
1639
|
+
if (overloadAttempts < overload.maxAttempts) {
|
|
1640
|
+
overloadAttempts += 1;
|
|
1641
|
+
next = { delayMs: jittered(overload, overloadAttempts) };
|
|
1642
|
+
break;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
if (!subscribe && msg.kind === "end" && msg.reason === SERVER_RESTART) throw new NetworkError(`POST ${path}: the server restarted mid-stream`, null, { code: STREAM_INTERRUPTED });
|
|
1646
|
+
const event = msgToEvent(msg);
|
|
1647
|
+
if (event === null) continue;
|
|
1648
|
+
yield event;
|
|
1649
|
+
if (msg.kind === "end") {
|
|
1650
|
+
next = subscribe && (msg.reason === SERVER_RESTART || msg.reason !== void 0 && LAPSES.has(msg.reason) && opts.canRefreshToken) ? { delayMs: 0 } : "stop";
|
|
1651
|
+
break;
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
} catch (err) {
|
|
1655
|
+
if (signal.aborted) return;
|
|
1656
|
+
if (err instanceof SubscribeError || err instanceof NetworkError && err.code === "stream_interrupted") throw err;
|
|
1657
|
+
if (!subscribe) throw err instanceof NetworkError ? err : new NetworkError(`POST ${path}: the stream broke before its end frame`, err);
|
|
1170
1658
|
}
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1659
|
+
if (next === "stop") return;
|
|
1660
|
+
if (next === void 0) {
|
|
1661
|
+
if (signal.aborted) return;
|
|
1662
|
+
if (!subscribe) throw new NetworkError(`POST ${path}: stream ended without an end frame`, null);
|
|
1663
|
+
next = { delayMs: backoff.nextDelay() };
|
|
1176
1664
|
}
|
|
1177
|
-
|
|
1178
|
-
await reader.cancel().catch(() => {});
|
|
1179
|
-
reader.releaseLock();
|
|
1665
|
+
if (!await sleep(next.delayMs, signal)) return;
|
|
1180
1666
|
}
|
|
1181
1667
|
}
|
|
1668
|
+
/** Retries of a 429 before the first response, after which it surfaces. */
|
|
1669
|
+
const BUSY_RETRIES = 3;
|
|
1670
|
+
function isBusy(err) {
|
|
1671
|
+
return err instanceof NetworkError && err.status === 429;
|
|
1672
|
+
}
|
|
1673
|
+
/**
|
|
1674
|
+
* A refusal that retrying will not change: a server without frame streams, or a
|
|
1675
|
+
* 4xx other than 408 and 429 (the frame no longer validates, say).
|
|
1676
|
+
*/
|
|
1677
|
+
function refused(err) {
|
|
1678
|
+
if (!(err instanceof NetworkError)) return false;
|
|
1679
|
+
if (err.code === "frames_unsupported") return true;
|
|
1680
|
+
const status = err.status;
|
|
1681
|
+
return status !== void 0 && status >= 400 && status < 500 && status !== 408 && status !== 429;
|
|
1682
|
+
}
|
|
1683
|
+
/**
|
|
1684
|
+
* Aborts when any of `signals` does. `AbortSignal.any` holds no listener on its
|
|
1685
|
+
* sources, so finished subscriptions leave nothing behind on a shared signal; the
|
|
1686
|
+
* fallback, for browsers before 2024, does.
|
|
1687
|
+
*/
|
|
1688
|
+
function anySignal(signals) {
|
|
1689
|
+
if (typeof AbortSignal.any === "function") return AbortSignal.any(signals);
|
|
1690
|
+
const ctrl = new AbortController();
|
|
1691
|
+
for (const s of signals) {
|
|
1692
|
+
if (s.aborted) {
|
|
1693
|
+
ctrl.abort(s.reason);
|
|
1694
|
+
break;
|
|
1695
|
+
}
|
|
1696
|
+
s.addEventListener("abort", () => ctrl.abort(s.reason), { once: true });
|
|
1697
|
+
}
|
|
1698
|
+
return ctrl.signal;
|
|
1699
|
+
}
|
|
1700
|
+
function decodeFrame(payload, wire) {
|
|
1701
|
+
const value = wire === "pack" ? decodeWsEnvelope(payload, "pack") : JSON.parse(new TextDecoder().decode(payload));
|
|
1702
|
+
if (value === null || typeof value !== "object" || typeof value.kind !== "string") throw new NetworkError("malformed frame in subscription stream", null);
|
|
1703
|
+
return value;
|
|
1704
|
+
}
|
|
1705
|
+
function jittered(cfg, attempt) {
|
|
1706
|
+
const raw = Math.min(cfg.baseDelayMs * 2 ** (attempt - 1), cfg.maxDelayMs);
|
|
1707
|
+
return cfg.jitter === 0 ? raw : raw + (Math.random() * 2 - 1) * raw * cfg.jitter;
|
|
1708
|
+
}
|
|
1709
|
+
/** Resolves true after `ms`, or false as soon as `signal` aborts. */
|
|
1710
|
+
function sleep(ms, signal) {
|
|
1711
|
+
if (signal.aborted) return Promise.resolve(false);
|
|
1712
|
+
return new Promise((resolve) => {
|
|
1713
|
+
const timer = setTimeout(() => {
|
|
1714
|
+
signal.removeEventListener("abort", onAbort);
|
|
1715
|
+
resolve(true);
|
|
1716
|
+
}, ms);
|
|
1717
|
+
const onAbort = () => {
|
|
1718
|
+
clearTimeout(timer);
|
|
1719
|
+
resolve(false);
|
|
1720
|
+
};
|
|
1721
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1722
|
+
});
|
|
1723
|
+
}
|
|
1182
1724
|
|
|
1183
1725
|
//#endregion
|
|
1184
1726
|
//#region ../../packages/substrate-client/src/nl.ts
|
|
@@ -1189,11 +1731,31 @@ function createNlApi(t) {
|
|
|
1189
1731
|
const res = await t.raw("/substrate/v1/nl", {
|
|
1190
1732
|
method: "POST",
|
|
1191
1733
|
body: req,
|
|
1192
|
-
accept:
|
|
1734
|
+
accept: FRAMES_JSON_MIME,
|
|
1193
1735
|
...opts.signal !== void 0 ? { signal: opts.signal } : {}
|
|
1194
1736
|
});
|
|
1195
|
-
|
|
1196
|
-
|
|
1737
|
+
const type = res.headers.get("content-type") ?? "";
|
|
1738
|
+
if (!type.startsWith("application/vnd.substrate.frames+json") || res.body === null) {
|
|
1739
|
+
await res.body?.cancel();
|
|
1740
|
+
throw new NetworkError(`POST /substrate/v1/nl answered '${type}', not ${FRAMES_JSON_MIME}: the server predates frame streams`, null, {
|
|
1741
|
+
status: res.status,
|
|
1742
|
+
code: FRAMES_UNSUPPORTED
|
|
1743
|
+
});
|
|
1744
|
+
}
|
|
1745
|
+
const utf8 = new TextDecoder();
|
|
1746
|
+
for await (const payload of decodeStreamFrames(res.body)) {
|
|
1747
|
+
let ev;
|
|
1748
|
+
try {
|
|
1749
|
+
ev = JSON.parse(utf8.decode(payload));
|
|
1750
|
+
} catch (err) {
|
|
1751
|
+
throw new NetworkError("malformed frame in the NL stream", err);
|
|
1752
|
+
}
|
|
1753
|
+
if (ev.event === "error") {
|
|
1754
|
+
const e = ev.data;
|
|
1755
|
+
throw new NetworkError(`${e.code ?? "error"}: ${e.message ?? ""}`, ev.data);
|
|
1756
|
+
}
|
|
1757
|
+
yield ev;
|
|
1758
|
+
}
|
|
1197
1759
|
},
|
|
1198
1760
|
async askJson(req, opts = {}) {
|
|
1199
1761
|
return await (await t.raw("/substrate/v1/nl", {
|
|
@@ -1280,7 +1842,7 @@ var HttpTransport = class {
|
|
|
1280
1842
|
constructor(opts) {
|
|
1281
1843
|
if (opts.token !== void 0 && opts.getToken !== void 0) throw new Error("HttpTransport: provide `token` OR `getToken`, not both");
|
|
1282
1844
|
this.#endpoint = trimTrailingSlashes(opts.endpoint);
|
|
1283
|
-
this.#fetch = (opts.fetch ??
|
|
1845
|
+
this.#fetch = (opts.fetch ?? substrateFetch).bind(globalThis);
|
|
1284
1846
|
this.#token = opts.token;
|
|
1285
1847
|
this.#getToken = opts.getToken;
|
|
1286
1848
|
}
|
|
@@ -1396,718 +1958,6 @@ async function errorInfo(res) {
|
|
|
1396
1958
|
return info;
|
|
1397
1959
|
}
|
|
1398
1960
|
|
|
1399
|
-
//#endregion
|
|
1400
|
-
//#region ../../packages/substrate-client/src/reconnect.ts
|
|
1401
|
-
var ReconnectScheduler = class {
|
|
1402
|
-
#base;
|
|
1403
|
-
#max;
|
|
1404
|
-
#jitter;
|
|
1405
|
-
#attempt = 0;
|
|
1406
|
-
constructor(opts) {
|
|
1407
|
-
this.#base = opts.baseDelayMs;
|
|
1408
|
-
this.#max = opts.maxDelayMs;
|
|
1409
|
-
this.#jitter = opts.jitter;
|
|
1410
|
-
}
|
|
1411
|
-
nextDelay() {
|
|
1412
|
-
const raw = Math.min(this.#base * 2 ** this.#attempt, this.#max);
|
|
1413
|
-
this.#attempt += 1;
|
|
1414
|
-
if (this.#jitter === 0) return raw;
|
|
1415
|
-
const spread = raw * this.#jitter;
|
|
1416
|
-
return raw + (Math.random() * 2 - 1) * spread;
|
|
1417
|
-
}
|
|
1418
|
-
reset() {
|
|
1419
|
-
this.#attempt = 0;
|
|
1420
|
-
}
|
|
1421
|
-
/**
|
|
1422
|
-
* Resolve after `nextDelay()` ms. Callers must use this serially — the
|
|
1423
|
-
* `#attempt` counter is shared across calls, so overlapping `schedule()`
|
|
1424
|
-
* invocations will produce incorrect delays. The WS reconnect loop awaits
|
|
1425
|
-
* each `schedule()` before firing the next attempt.
|
|
1426
|
-
*/
|
|
1427
|
-
async schedule() {
|
|
1428
|
-
const d = this.nextDelay();
|
|
1429
|
-
await new Promise((resolve) => setTimeout(resolve, d));
|
|
1430
|
-
}
|
|
1431
|
-
};
|
|
1432
|
-
|
|
1433
|
-
//#endregion
|
|
1434
|
-
//#region ../../packages/substrate-client/src/transport/multiplex.ts
|
|
1435
|
-
/**
|
|
1436
|
-
* MultiplexedSocket — low-level WS multiplexer.
|
|
1437
|
-
*
|
|
1438
|
-
* One raw WebSocket; outbound frames get a client-generated `id`; inbound
|
|
1439
|
-
* frames with a matching `id` route to the right pending handler.
|
|
1440
|
-
*
|
|
1441
|
-
* Subscribe/stream ops stay pending across multiple messages (they receive
|
|
1442
|
-
* snapshot/add/update/remove/chunk/end/error frames). Query/mutate are not
|
|
1443
|
-
* handled here — those go via HTTP.
|
|
1444
|
-
*
|
|
1445
|
-
* Encoding: JSON-over-WS by default. Pass `usePack: true` to send/receive
|
|
1446
|
-
* msgpack-encoded frames matching the `substrate-pack` subprotocol negotiated
|
|
1447
|
-
* one level up by `WsTransport`. Under pack, `data` values arrive as
|
|
1448
|
-
* positional tuples — the higher layer (`SubstrateClient`) is responsible for
|
|
1449
|
-
* the schema-driven positional → named decode; the multiplexer only
|
|
1450
|
-
* handles wire-format framing.
|
|
1451
|
-
*
|
|
1452
|
-
* Works with both node-ws WebSocket and browser native WebSocket via the
|
|
1453
|
-
* shared `addEventListener` interface (node-ws v8+ supports it).
|
|
1454
|
-
*/
|
|
1455
|
-
/** Terminal response kinds — once received the pending entry is cleared. */
|
|
1456
|
-
const TERMINAL_KINDS = /* @__PURE__ */ new Set(["end", "error"]);
|
|
1457
|
-
var MultiplexedSocket = class {
|
|
1458
|
-
#ws;
|
|
1459
|
-
#mode;
|
|
1460
|
-
#pending = /* @__PURE__ */ new Map();
|
|
1461
|
-
#seq = 0;
|
|
1462
|
-
#closing = false;
|
|
1463
|
-
/**
|
|
1464
|
-
* Accept either a node `ws` WebSocket or a browser WebSocket — both expose
|
|
1465
|
-
* `addEventListener`. The constructor wires up message and close handling.
|
|
1466
|
-
*
|
|
1467
|
-
* `usePack` is set by `WsTransport` after the `substrate-pack` subprotocol
|
|
1468
|
-
* has been negotiated at handshake; once true the socket exclusively
|
|
1469
|
-
* speaks msgpack frames.
|
|
1470
|
-
*/
|
|
1471
|
-
constructor(ws, opts = {}) {
|
|
1472
|
-
this.#ws = ws;
|
|
1473
|
-
this.#mode = opts.usePack === true ? "pack" : "json";
|
|
1474
|
-
ws.addEventListener("message", (ev) => this.#onMessage(ev));
|
|
1475
|
-
ws.addEventListener("close", (ev) => this.#onClose(ev));
|
|
1476
|
-
}
|
|
1477
|
-
nextId() {
|
|
1478
|
-
this.#seq += 1;
|
|
1479
|
-
return `c${this.#seq}`;
|
|
1480
|
-
}
|
|
1481
|
-
/**
|
|
1482
|
-
* Serialise + send `frame` (which must already include an `id` field) and
|
|
1483
|
-
* register `pending` as the handler for responses bearing that id.
|
|
1484
|
-
* Returns the frame id for caller bookkeeping.
|
|
1485
|
-
*/
|
|
1486
|
-
send(frame, pending) {
|
|
1487
|
-
const id = frame.id ?? this.nextId();
|
|
1488
|
-
const frameWithId = id === frame.id ? frame : {
|
|
1489
|
-
...frame,
|
|
1490
|
-
id
|
|
1491
|
-
};
|
|
1492
|
-
this.#pending.set(id, pending);
|
|
1493
|
-
try {
|
|
1494
|
-
this.#sendWire(encodeWsFrame(frameWithId, this.#mode));
|
|
1495
|
-
} catch (_err) {
|
|
1496
|
-
this.#pending.delete(id);
|
|
1497
|
-
pending.onClose("send failed");
|
|
1498
|
-
}
|
|
1499
|
-
return id;
|
|
1500
|
-
}
|
|
1501
|
-
/**
|
|
1502
|
-
* Send a cancel frame for a given subscription id and remove the pending
|
|
1503
|
-
* handler. The server will emit a terminal `end` frame; since we delete the
|
|
1504
|
-
* pending entry here, that frame is silently dropped.
|
|
1505
|
-
*/
|
|
1506
|
-
cancel(id) {
|
|
1507
|
-
this.#pending.delete(id);
|
|
1508
|
-
try {
|
|
1509
|
-
this.#sendWire(encodeWsFrame({
|
|
1510
|
-
id,
|
|
1511
|
-
op: "cancel"
|
|
1512
|
-
}, this.#mode));
|
|
1513
|
-
} catch {}
|
|
1514
|
-
}
|
|
1515
|
-
/**
|
|
1516
|
-
* Push wire bytes to the socket. The branch exists because `WebSocket.send`'s
|
|
1517
|
-
* overloads don't accept the union. Binary frames are copied into a fresh
|
|
1518
|
-
* `Uint8Array` so the backing buffer is an `ArrayBuffer`, not the
|
|
1519
|
-
* `ArrayBufferLike` msgpackr's `Buffer` is typed with — avoiding a cast.
|
|
1520
|
-
*/
|
|
1521
|
-
#sendWire(wire) {
|
|
1522
|
-
if (typeof wire === "string") this.#ws.send(wire);
|
|
1523
|
-
else this.#ws.send(new Uint8Array(wire));
|
|
1524
|
-
}
|
|
1525
|
-
close() {
|
|
1526
|
-
if (this.#closing) return;
|
|
1527
|
-
this.#closing = true;
|
|
1528
|
-
try {
|
|
1529
|
-
this.#ws.close();
|
|
1530
|
-
} catch {}
|
|
1531
|
-
}
|
|
1532
|
-
#onMessage(ev) {
|
|
1533
|
-
let msg;
|
|
1534
|
-
try {
|
|
1535
|
-
msg = decodeWsEnvelope(ev.data, this.#mode);
|
|
1536
|
-
} catch {
|
|
1537
|
-
return;
|
|
1538
|
-
}
|
|
1539
|
-
const pending = this.#pending.get(msg.id);
|
|
1540
|
-
if (!pending) return;
|
|
1541
|
-
if (TERMINAL_KINDS.has(msg.kind)) this.#pending.delete(msg.id);
|
|
1542
|
-
pending.onMessage(msg);
|
|
1543
|
-
}
|
|
1544
|
-
#onClose(ev) {
|
|
1545
|
-
const reason = this.#closing ? "transport_closed" : ev.code === 1012 ? "server_restart" : `ws closed (code ${ev.code})`;
|
|
1546
|
-
const snapshot = [...this.#pending.entries()];
|
|
1547
|
-
this.#pending.clear();
|
|
1548
|
-
for (const [, pending] of snapshot) pending.onClose(reason);
|
|
1549
|
-
}
|
|
1550
|
-
};
|
|
1551
|
-
|
|
1552
|
-
//#endregion
|
|
1553
|
-
//#region ../../packages/substrate-client/src/transport/ws.ts
|
|
1554
|
-
/**
|
|
1555
|
-
* WsTransport — high-level WebSocket transport for Substrate subscriptions.
|
|
1556
|
-
*
|
|
1557
|
-
* Manages one raw socket per `(endpoint, auth)` pair via MultiplexedSocket.
|
|
1558
|
-
* Exposes `subscribe` and `stream` as AsyncIterables; `cancel` is handled
|
|
1559
|
-
* transparently via the iterator's `return()` method.
|
|
1560
|
-
*
|
|
1561
|
-
* Protocol:
|
|
1562
|
-
* - No hello/handshake: send subscribe/stream frames immediately on open.
|
|
1563
|
-
* - WS handles: subscribe, stream, cancel only. Query/mutate use HTTP.
|
|
1564
|
-
* - Auth: Bearer token in Authorization header on the HTTP upgrade request.
|
|
1565
|
-
* Node-ws supports custom headers; the WHATWG/browser WebSocket does not,
|
|
1566
|
-
* so under Node node-ws is always preferred even when a global WebSocket
|
|
1567
|
-
* exists. True browsers cannot authenticate the WS without a
|
|
1568
|
-
* header-capable `webSocket` implementation.
|
|
1569
|
-
* - Response kinds: snapshot / add / update / remove / chunk / end / error.
|
|
1570
|
-
* - Terminal kinds for cleanup: 'end' and 'error'.
|
|
1571
|
-
*
|
|
1572
|
-
* Reconnect: when the socket closes unexpectedly, ReconnectScheduler backs
|
|
1573
|
-
* off and re-opens. Active subscriptions are resubscribed on the new socket —
|
|
1574
|
-
* the consumer's AsyncIterable keeps flowing (snapshot arrives again from the
|
|
1575
|
-
* server's perspective; the consumer sees it as a new event). A transport that
|
|
1576
|
-
* has never connected does not retry: its first failure rejects `next()`.
|
|
1577
|
-
*/
|
|
1578
|
-
/**
|
|
1579
|
-
* Terminal-cause tokens from the WS teardown contract
|
|
1580
|
-
* (docs/ws-subscription-contract.md). Local literals, not substrate-types
|
|
1581
|
-
* imports: they are stable wire contract, like the 1012 close code.
|
|
1582
|
-
*/
|
|
1583
|
-
const REASON_SUBSCRIPTION_OVERLOADED = "subscription_overloaded";
|
|
1584
|
-
const REASON_SERVER_RESTART = "server_restart";
|
|
1585
|
-
/**
|
|
1586
|
-
* Pick the WebSocket implementation for a connection.
|
|
1587
|
-
*
|
|
1588
|
-
* Priority: injected > node-ws under Node > global (browser). Node ≥22 ships
|
|
1589
|
-
* a global WebSocket that cannot carry headers, so preferring it would
|
|
1590
|
-
* silently drop the bearer.
|
|
1591
|
-
*
|
|
1592
|
-
* Throws when a token is configured but the only available implementation
|
|
1593
|
-
* cannot carry headers (a true browser): connecting anonymously to an
|
|
1594
|
-
* auth-gated server would loop on 4001 instead.
|
|
1595
|
-
*/
|
|
1596
|
-
function selectWebSocketImpl(opts) {
|
|
1597
|
-
if (opts.injected !== void 0) return {
|
|
1598
|
-
impl: opts.injected,
|
|
1599
|
-
supportsHeaders: true
|
|
1600
|
-
};
|
|
1601
|
-
if (opts.isNode) return {
|
|
1602
|
-
impl: opts.nodeWs,
|
|
1603
|
-
supportsHeaders: true
|
|
1604
|
-
};
|
|
1605
|
-
if (opts.globalWs !== void 0) {
|
|
1606
|
-
if (opts.hasToken) throw new NetworkError("WsTransport: a bearer token is configured but the only available WebSocket implementation (the WHATWG/browser global) cannot carry the Authorization header. Supply a header-capable implementation via the `webSocket` option.", null);
|
|
1607
|
-
return {
|
|
1608
|
-
impl: opts.globalWs,
|
|
1609
|
-
supportsHeaders: false
|
|
1610
|
-
};
|
|
1611
|
-
}
|
|
1612
|
-
throw new NetworkError("WsTransport: no WebSocket implementation available", null);
|
|
1613
|
-
}
|
|
1614
|
-
const DEFAULT_RECONNECT = {
|
|
1615
|
-
baseDelayMs: 1e3,
|
|
1616
|
-
maxDelayMs: 3e4,
|
|
1617
|
-
jitter: .1
|
|
1618
|
-
};
|
|
1619
|
-
const DEFAULT_OVERLOAD_RESUBSCRIBE = {
|
|
1620
|
-
enabled: true,
|
|
1621
|
-
maxAttempts: 3,
|
|
1622
|
-
baseDelayMs: 1e3,
|
|
1623
|
-
maxDelayMs: 3e4,
|
|
1624
|
-
jitter: .1,
|
|
1625
|
-
healthyResetMs: 6e4
|
|
1626
|
-
};
|
|
1627
|
-
var WsTransport = class {
|
|
1628
|
-
#endpoint;
|
|
1629
|
-
#token;
|
|
1630
|
-
#getToken;
|
|
1631
|
-
#webSocket;
|
|
1632
|
-
#reconnectOpts;
|
|
1633
|
-
#encoding;
|
|
1634
|
-
#overloadResubscribe;
|
|
1635
|
-
/** Current multiplexed socket, or null if not yet opened / closed. */
|
|
1636
|
-
#mux = null;
|
|
1637
|
-
/** Promise that resolves when the socket is open and ready. */
|
|
1638
|
-
#openPromise = null;
|
|
1639
|
-
/** Set when close() is called — no more reconnects. */
|
|
1640
|
-
#destroyed = false;
|
|
1641
|
-
/** Active subscriptions keyed by their logical frame id. */
|
|
1642
|
-
#activeSubs = /* @__PURE__ */ new Map();
|
|
1643
|
-
/** Guard: prevents multiple concurrent #reconnectActiveSubs invocations. */
|
|
1644
|
-
#reconnecting = false;
|
|
1645
|
-
/**
|
|
1646
|
-
* Close code captured from the most recent unintentional socket close.
|
|
1647
|
-
* Read once by the next `#openLoop` and cleared. When it equals 1012
|
|
1648
|
-
* (W3C "Service Restart") the loop skips its first scheduled backoff —
|
|
1649
|
-
* the server is intentionally bouncing and we'd rather pay one wasted
|
|
1650
|
-
* connect attempt than 1+s of jittered delay.
|
|
1651
|
-
*/
|
|
1652
|
-
#lastCloseCode = null;
|
|
1653
|
-
/**
|
|
1654
|
-
* Backoff for closes that come AFTER an accepted upgrade (1013 overload,
|
|
1655
|
-
* 4001 on a lapsed credential). It lives on the transport, not the open
|
|
1656
|
-
* loop, so repeated churn grows the delay; a socket that delivers a frame
|
|
1657
|
-
* and outlives `maxDelayMs` proves healthy and resets it.
|
|
1658
|
-
*/
|
|
1659
|
-
#churn;
|
|
1660
|
-
/**
|
|
1661
|
-
* Retry forever only once a socket has opened. Before that, a failure is
|
|
1662
|
-
* most likely a wrong endpoint or a broken token, so it surfaces instead.
|
|
1663
|
-
*/
|
|
1664
|
-
#hasOpened = false;
|
|
1665
|
-
constructor(opts) {
|
|
1666
|
-
if (opts.token !== void 0 && opts.getToken !== void 0) throw new Error("WsTransport: provide `token` OR `getToken`, not both");
|
|
1667
|
-
this.#endpoint = opts.endpoint;
|
|
1668
|
-
this.#token = opts.token;
|
|
1669
|
-
this.#getToken = opts.getToken;
|
|
1670
|
-
this.#webSocket = opts.webSocket;
|
|
1671
|
-
this.#reconnectOpts = {
|
|
1672
|
-
...DEFAULT_RECONNECT,
|
|
1673
|
-
...opts.reconnect
|
|
1674
|
-
};
|
|
1675
|
-
this.#churn = new ReconnectScheduler(this.#reconnectOpts);
|
|
1676
|
-
this.#encoding = opts.encoding ?? "json";
|
|
1677
|
-
this.#overloadResubscribe = {
|
|
1678
|
-
...DEFAULT_OVERLOAD_RESUBSCRIBE,
|
|
1679
|
-
...opts.overloadResubscribe
|
|
1680
|
-
};
|
|
1681
|
-
}
|
|
1682
|
-
/**
|
|
1683
|
-
* Subscribe to a live query. Returns an AsyncIterable of SubscribeEvents.
|
|
1684
|
-
* Calling `break` or `return()` on the iterator sends a cancel frame.
|
|
1685
|
-
*/
|
|
1686
|
-
subscribe(req) {
|
|
1687
|
-
return this.#makeIterable({
|
|
1688
|
-
...req,
|
|
1689
|
-
op: "subscribe"
|
|
1690
|
-
});
|
|
1691
|
-
}
|
|
1692
|
-
/** Stream a one-shot resolver. Returns chunk events followed by an end event. */
|
|
1693
|
-
stream(req) {
|
|
1694
|
-
return this.#makeIterable({
|
|
1695
|
-
...req,
|
|
1696
|
-
op: "stream"
|
|
1697
|
-
});
|
|
1698
|
-
}
|
|
1699
|
-
/** Gracefully shut down. Cancels all active subscriptions and closes the socket. */
|
|
1700
|
-
async close() {
|
|
1701
|
-
this.#destroyed = true;
|
|
1702
|
-
for (const [, sub] of this.#activeSubs) {
|
|
1703
|
-
sub.done = true;
|
|
1704
|
-
if (sub.retryTimer !== null) {
|
|
1705
|
-
clearTimeout(sub.retryTimer);
|
|
1706
|
-
sub.retryTimer = null;
|
|
1707
|
-
}
|
|
1708
|
-
if (sub.waiter) {
|
|
1709
|
-
sub.waiter(null);
|
|
1710
|
-
sub.waiter = null;
|
|
1711
|
-
}
|
|
1712
|
-
}
|
|
1713
|
-
this.#activeSubs.clear();
|
|
1714
|
-
if (this.#mux) {
|
|
1715
|
-
this.#mux.close();
|
|
1716
|
-
this.#mux = null;
|
|
1717
|
-
}
|
|
1718
|
-
this.#openPromise = null;
|
|
1719
|
-
}
|
|
1720
|
-
/** Ensure a connected MultiplexedSocket is available. */
|
|
1721
|
-
async #ensureOpen() {
|
|
1722
|
-
if (this.#mux !== null) return this.#mux;
|
|
1723
|
-
if (this.#openPromise !== null) return this.#openPromise;
|
|
1724
|
-
this.#openPromise = this.#openLoop();
|
|
1725
|
-
return this.#openPromise;
|
|
1726
|
-
}
|
|
1727
|
-
/**
|
|
1728
|
-
* Open loop: try to open a socket; on failure, backoff and retry.
|
|
1729
|
-
* Runs until connected or #destroyed.
|
|
1730
|
-
*/
|
|
1731
|
-
async #openLoop() {
|
|
1732
|
-
const scheduler = new ReconnectScheduler(this.#reconnectOpts);
|
|
1733
|
-
const wasGracefulRestart = this.#lastCloseCode === 1012;
|
|
1734
|
-
const churned = this.#lastCloseCode === 1013 || this.#lastCloseCode === 4001;
|
|
1735
|
-
this.#lastCloseCode = null;
|
|
1736
|
-
if (churned) {
|
|
1737
|
-
await this.#churn.schedule();
|
|
1738
|
-
if (this.#destroyed) throw new NetworkError("WsTransport destroyed", null);
|
|
1739
|
-
}
|
|
1740
|
-
let firstFailure = true;
|
|
1741
|
-
while (true) {
|
|
1742
|
-
if (this.#destroyed) throw new NetworkError("WsTransport destroyed", null);
|
|
1743
|
-
try {
|
|
1744
|
-
const mux = await this.#openOnce();
|
|
1745
|
-
scheduler.reset();
|
|
1746
|
-
this.#hasOpened = true;
|
|
1747
|
-
this.#mux = mux;
|
|
1748
|
-
this.#openPromise = null;
|
|
1749
|
-
return mux;
|
|
1750
|
-
} catch (err) {
|
|
1751
|
-
if (this.#destroyed) throw new NetworkError("WsTransport destroyed", err);
|
|
1752
|
-
if (!this.#hasOpened) {
|
|
1753
|
-
this.#openPromise = null;
|
|
1754
|
-
throw err;
|
|
1755
|
-
}
|
|
1756
|
-
if (wasGracefulRestart && firstFailure) {
|
|
1757
|
-
firstFailure = false;
|
|
1758
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1759
|
-
} else {
|
|
1760
|
-
firstFailure = false;
|
|
1761
|
-
await scheduler.schedule();
|
|
1762
|
-
}
|
|
1763
|
-
if (this.#destroyed) throw new NetworkError("WsTransport destroyed", null);
|
|
1764
|
-
}
|
|
1765
|
-
}
|
|
1766
|
-
}
|
|
1767
|
-
/** Open one WebSocket connection and resolve when it's ready. */
|
|
1768
|
-
async #openOnce() {
|
|
1769
|
-
const token = this.#token ?? (this.#getToken ? await this.#getToken() : void 0);
|
|
1770
|
-
const subprotocols = this.#encoding === "pack" ? [WS_SUBPROTOCOL_SUBSTRATE_PACK] : void 0;
|
|
1771
|
-
const ws = await new Promise((resolve, reject) => {
|
|
1772
|
-
let rawWs;
|
|
1773
|
-
const { impl, supportsHeaders } = selectWebSocketImpl({
|
|
1774
|
-
injected: this.#webSocket,
|
|
1775
|
-
isNode: typeof process !== "undefined" && process.versions?.node !== void 0,
|
|
1776
|
-
globalWs: globalThis.WebSocket,
|
|
1777
|
-
nodeWs: WebSocket,
|
|
1778
|
-
hasToken: token !== void 0
|
|
1779
|
-
});
|
|
1780
|
-
if (supportsHeaders) {
|
|
1781
|
-
const headers = {};
|
|
1782
|
-
if (token !== void 0) headers.authorization = `Bearer ${token}`;
|
|
1783
|
-
rawWs = new impl(this.#endpoint, subprotocols, { headers });
|
|
1784
|
-
} else rawWs = subprotocols !== void 0 ? new impl(this.#endpoint, subprotocols) : new impl(this.#endpoint);
|
|
1785
|
-
if (this.#encoding === "pack") try {
|
|
1786
|
-
rawWs.binaryType = "arraybuffer";
|
|
1787
|
-
} catch {}
|
|
1788
|
-
const onOpen = () => {
|
|
1789
|
-
rawWs.removeEventListener("error", onError);
|
|
1790
|
-
resolve(rawWs);
|
|
1791
|
-
};
|
|
1792
|
-
const onError = (ev) => {
|
|
1793
|
-
rawWs.removeEventListener("open", onOpen);
|
|
1794
|
-
reject(new NetworkError(`WS connect failed: ${this.#endpoint}`, ev));
|
|
1795
|
-
};
|
|
1796
|
-
rawWs.addEventListener("open", onOpen);
|
|
1797
|
-
rawWs.addEventListener("error", onError);
|
|
1798
|
-
});
|
|
1799
|
-
const mux = new MultiplexedSocket(ws, { usePack: this.#encoding === "pack" });
|
|
1800
|
-
const openedAt = Date.now();
|
|
1801
|
-
let delivered = false;
|
|
1802
|
-
ws.addEventListener("message", () => {
|
|
1803
|
-
delivered = true;
|
|
1804
|
-
}, { once: true });
|
|
1805
|
-
ws.addEventListener("close", (ev) => {
|
|
1806
|
-
if (this.#destroyed) return;
|
|
1807
|
-
const healthy = delivered && Date.now() - openedAt >= this.#reconnectOpts.maxDelayMs;
|
|
1808
|
-
if (healthy) this.#churn.reset();
|
|
1809
|
-
if (ev.code === 4001 && !(healthy && this.#canRefreshToken())) {
|
|
1810
|
-
if (this.#mux === mux) this.#mux = null;
|
|
1811
|
-
this.#failActiveSubs(new NetworkError(`WS closed 4001 unauthorized by ${this.#endpoint} — bearer rejected (wrong/expired token, or none configured against an auth-gated server)`, ev));
|
|
1812
|
-
return;
|
|
1813
|
-
}
|
|
1814
|
-
if (this.#mux === mux) {
|
|
1815
|
-
this.#mux = null;
|
|
1816
|
-
this.#lastCloseCode = ev.code;
|
|
1817
|
-
}
|
|
1818
|
-
this.#reconnectActiveSubs().catch(() => {});
|
|
1819
|
-
});
|
|
1820
|
-
return mux;
|
|
1821
|
-
}
|
|
1822
|
-
/** Only a provider can mint a fresh token; a fixed `token` would be refused again. */
|
|
1823
|
-
#canRefreshToken() {
|
|
1824
|
-
return this.#token === void 0 && this.#getToken !== void 0;
|
|
1825
|
-
}
|
|
1826
|
-
/** Terminate every active sub with `err` — delivered so next() throws. */
|
|
1827
|
-
#failActiveSubs(err) {
|
|
1828
|
-
for (const [, sub] of this.#activeSubs) {
|
|
1829
|
-
if (sub.done) continue;
|
|
1830
|
-
sub.done = true;
|
|
1831
|
-
if (sub.retryTimer !== null) {
|
|
1832
|
-
clearTimeout(sub.retryTimer);
|
|
1833
|
-
sub.retryTimer = null;
|
|
1834
|
-
}
|
|
1835
|
-
const payload = {
|
|
1836
|
-
ok: false,
|
|
1837
|
-
err
|
|
1838
|
-
};
|
|
1839
|
-
if (sub.waiter) {
|
|
1840
|
-
const w = sub.waiter;
|
|
1841
|
-
sub.waiter = null;
|
|
1842
|
-
w(payload);
|
|
1843
|
-
} else sub.queue.push(payload);
|
|
1844
|
-
}
|
|
1845
|
-
this.#activeSubs.clear();
|
|
1846
|
-
}
|
|
1847
|
-
/** Re-subscribe all active subs after a reconnect. */
|
|
1848
|
-
async #reconnectActiveSubs() {
|
|
1849
|
-
if (this.#reconnecting) return;
|
|
1850
|
-
if (this.#destroyed) return;
|
|
1851
|
-
this.#reconnecting = true;
|
|
1852
|
-
try {
|
|
1853
|
-
if (this.#activeSubs.size === 0) return;
|
|
1854
|
-
let mux;
|
|
1855
|
-
try {
|
|
1856
|
-
mux = await this.#ensureOpen();
|
|
1857
|
-
} catch {
|
|
1858
|
-
for (const [, sub] of this.#activeSubs) {
|
|
1859
|
-
sub.done = true;
|
|
1860
|
-
if (sub.retryTimer !== null) {
|
|
1861
|
-
clearTimeout(sub.retryTimer);
|
|
1862
|
-
sub.retryTimer = null;
|
|
1863
|
-
}
|
|
1864
|
-
if (sub.waiter) {
|
|
1865
|
-
sub.waiter(null);
|
|
1866
|
-
sub.waiter = null;
|
|
1867
|
-
}
|
|
1868
|
-
}
|
|
1869
|
-
this.#activeSubs.clear();
|
|
1870
|
-
return;
|
|
1871
|
-
}
|
|
1872
|
-
for (const [, sub] of this.#activeSubs) {
|
|
1873
|
-
if (sub.done) continue;
|
|
1874
|
-
if (sub.retryTimer !== null) {
|
|
1875
|
-
clearTimeout(sub.retryTimer);
|
|
1876
|
-
sub.retryTimer = null;
|
|
1877
|
-
}
|
|
1878
|
-
this.#doSubscribe(mux, sub);
|
|
1879
|
-
}
|
|
1880
|
-
} finally {
|
|
1881
|
-
this.#reconnecting = false;
|
|
1882
|
-
}
|
|
1883
|
-
}
|
|
1884
|
-
/** Send a subscribe/stream frame on the mux and wire up the Pending handler. */
|
|
1885
|
-
#doSubscribe(mux, sub) {
|
|
1886
|
-
const pending = {
|
|
1887
|
-
onMessage: (msg) => {
|
|
1888
|
-
if (sub.done) return;
|
|
1889
|
-
const action = this.#classifyTerminal(msg, sub);
|
|
1890
|
-
if (action === "suppress") return;
|
|
1891
|
-
let payload;
|
|
1892
|
-
try {
|
|
1893
|
-
if (msgToEvent(msg) === null) return;
|
|
1894
|
-
payload = {
|
|
1895
|
-
ok: true,
|
|
1896
|
-
msg
|
|
1897
|
-
};
|
|
1898
|
-
} catch (err) {
|
|
1899
|
-
payload = {
|
|
1900
|
-
ok: false,
|
|
1901
|
-
err: err instanceof Error ? err : new Error(String(err))
|
|
1902
|
-
};
|
|
1903
|
-
sub.done = true;
|
|
1904
|
-
}
|
|
1905
|
-
if (action === "terminal" && (msg.kind === "end" || msg.kind === "error")) sub.done = true;
|
|
1906
|
-
if (sub.waiter) {
|
|
1907
|
-
const w = sub.waiter;
|
|
1908
|
-
sub.waiter = null;
|
|
1909
|
-
w(payload);
|
|
1910
|
-
} else sub.queue.push(payload);
|
|
1911
|
-
},
|
|
1912
|
-
onClose: (_reason) => {
|
|
1913
|
-
if (sub.done) return;
|
|
1914
|
-
if (this.#destroyed) {
|
|
1915
|
-
sub.done = true;
|
|
1916
|
-
if (sub.waiter) {
|
|
1917
|
-
sub.waiter(null);
|
|
1918
|
-
sub.waiter = null;
|
|
1919
|
-
}
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
};
|
|
1923
|
-
const outFrame = {
|
|
1924
|
-
id: sub.socketId,
|
|
1925
|
-
op: sub.frame.op,
|
|
1926
|
-
frame: sub.frame.frame
|
|
1927
|
-
};
|
|
1928
|
-
mux.send(outFrame, pending);
|
|
1929
|
-
}
|
|
1930
|
-
/**
|
|
1931
|
-
* Classify a frame per the teardown contract (substrate repo
|
|
1932
|
-
* docs/ws-subscription-contract.md) BEFORE msgToEvent, which throws on
|
|
1933
|
-
* error frames.
|
|
1934
|
-
*
|
|
1935
|
-
* - 'suppress': consumed by the retry machinery; never reaches the consumer.
|
|
1936
|
-
* Overload error frames with budget left schedule a resubscribe here; the
|
|
1937
|
-
* trailing `end { reason: 'subscription_overloaded' }` of a suppressed
|
|
1938
|
-
* teardown is swallowed too.
|
|
1939
|
-
* - 'survive': delivered to the consumer but NOT terminal — server_restart
|
|
1940
|
-
* end frames; the sub stays live so the socket-close reconnect path
|
|
1941
|
-
* resubscribes it.
|
|
1942
|
-
* - 'terminal': mark done on delivery.
|
|
1943
|
-
* - 'data': everything else.
|
|
1944
|
-
*/
|
|
1945
|
-
#classifyTerminal(msg, sub) {
|
|
1946
|
-
const cfg = this.#overloadResubscribe;
|
|
1947
|
-
if (msg.kind === "error") {
|
|
1948
|
-
if (msg.errors?.[0]?.code !== REASON_SUBSCRIPTION_OVERLOADED || !cfg.enabled) return "terminal";
|
|
1949
|
-
const now = Date.now();
|
|
1950
|
-
if (sub.lastOverloadAt !== null && now - sub.lastOverloadAt > cfg.healthyResetMs) sub.overloadAttempts = 0;
|
|
1951
|
-
sub.lastOverloadAt = now;
|
|
1952
|
-
if (sub.overloadAttempts >= cfg.maxAttempts) return "terminal";
|
|
1953
|
-
sub.overloadAttempts += 1;
|
|
1954
|
-
this.#scheduleOverloadRetry(sub);
|
|
1955
|
-
return "suppress";
|
|
1956
|
-
}
|
|
1957
|
-
if (msg.kind === "end") {
|
|
1958
|
-
if (msg.reason === REASON_SUBSCRIPTION_OVERLOADED && cfg.enabled && sub.retryTimer !== null) return "suppress";
|
|
1959
|
-
if (msg.reason === REASON_SERVER_RESTART) return "survive";
|
|
1960
|
-
if ((msg.reason === "credential_expired" || msg.reason === "credential_changed") && this.#canRefreshToken()) return "survive";
|
|
1961
|
-
return "terminal";
|
|
1962
|
-
}
|
|
1963
|
-
return "data";
|
|
1964
|
-
}
|
|
1965
|
-
/** Backoff then re-send the subscribe frame for `sub` on the live socket. */
|
|
1966
|
-
#scheduleOverloadRetry(sub) {
|
|
1967
|
-
const cfg = this.#overloadResubscribe;
|
|
1968
|
-
const raw = Math.min(cfg.baseDelayMs * 2 ** (sub.overloadAttempts - 1), cfg.maxDelayMs);
|
|
1969
|
-
const delay = cfg.jitter === 0 ? raw : raw + (Math.random() * 2 - 1) * raw * cfg.jitter;
|
|
1970
|
-
sub.retryTimer = setTimeout(() => {
|
|
1971
|
-
sub.retryTimer = null;
|
|
1972
|
-
if (sub.done || this.#destroyed) return;
|
|
1973
|
-
this.#ensureOpen().then((mux) => {
|
|
1974
|
-
if (!sub.done) this.#doSubscribe(mux, sub);
|
|
1975
|
-
}).catch(() => {});
|
|
1976
|
-
}, delay);
|
|
1977
|
-
}
|
|
1978
|
-
#makeIterable(frame) {
|
|
1979
|
-
const logicalId = frame.id;
|
|
1980
|
-
const transport = this;
|
|
1981
|
-
return { [Symbol.asyncIterator]() {
|
|
1982
|
-
const sub = {
|
|
1983
|
-
frame,
|
|
1984
|
-
socketId: logicalId,
|
|
1985
|
-
queue: [],
|
|
1986
|
-
waiter: null,
|
|
1987
|
-
done: false,
|
|
1988
|
-
overloadAttempts: 0,
|
|
1989
|
-
lastOverloadAt: null,
|
|
1990
|
-
retryTimer: null
|
|
1991
|
-
};
|
|
1992
|
-
transport.#activeSubs.set(logicalId, sub);
|
|
1993
|
-
transport.#ensureOpen().then((mux) => {
|
|
1994
|
-
if (!sub.done) transport.#doSubscribe(mux, sub);
|
|
1995
|
-
}).catch((err) => {
|
|
1996
|
-
if (sub.done) return;
|
|
1997
|
-
sub.done = true;
|
|
1998
|
-
transport.#activeSubs.delete(logicalId);
|
|
1999
|
-
const payload = {
|
|
2000
|
-
ok: false,
|
|
2001
|
-
err: err instanceof Error ? err : new NetworkError(String(err), err)
|
|
2002
|
-
};
|
|
2003
|
-
if (sub.waiter) {
|
|
2004
|
-
const w = sub.waiter;
|
|
2005
|
-
sub.waiter = null;
|
|
2006
|
-
w(payload);
|
|
2007
|
-
} else sub.queue.push(payload);
|
|
2008
|
-
});
|
|
2009
|
-
return {
|
|
2010
|
-
async next() {
|
|
2011
|
-
while (true) {
|
|
2012
|
-
if (sub.queue.length > 0) {
|
|
2013
|
-
const payload = sub.queue.shift();
|
|
2014
|
-
if (!payload.ok) {
|
|
2015
|
-
sub.done = true;
|
|
2016
|
-
transport.#activeSubs.delete(logicalId);
|
|
2017
|
-
throw payload.err;
|
|
2018
|
-
}
|
|
2019
|
-
const ev = msgToEvent(payload.msg);
|
|
2020
|
-
if (ev === null) continue;
|
|
2021
|
-
return {
|
|
2022
|
-
value: ev,
|
|
2023
|
-
done: false
|
|
2024
|
-
};
|
|
2025
|
-
}
|
|
2026
|
-
if (sub.done) return {
|
|
2027
|
-
value: void 0,
|
|
2028
|
-
done: true
|
|
2029
|
-
};
|
|
2030
|
-
const payload = await new Promise((resolve) => {
|
|
2031
|
-
sub.waiter = resolve;
|
|
2032
|
-
});
|
|
2033
|
-
if (payload === null) return {
|
|
2034
|
-
value: void 0,
|
|
2035
|
-
done: true
|
|
2036
|
-
};
|
|
2037
|
-
if (!payload.ok) {
|
|
2038
|
-
sub.done = true;
|
|
2039
|
-
transport.#activeSubs.delete(logicalId);
|
|
2040
|
-
throw payload.err;
|
|
2041
|
-
}
|
|
2042
|
-
const ev = msgToEvent(payload.msg);
|
|
2043
|
-
if (ev !== null) return {
|
|
2044
|
-
value: ev,
|
|
2045
|
-
done: false
|
|
2046
|
-
};
|
|
2047
|
-
}
|
|
2048
|
-
},
|
|
2049
|
-
async return() {
|
|
2050
|
-
if (!sub.done) {
|
|
2051
|
-
sub.done = true;
|
|
2052
|
-
if (sub.retryTimer !== null) {
|
|
2053
|
-
clearTimeout(sub.retryTimer);
|
|
2054
|
-
sub.retryTimer = null;
|
|
2055
|
-
}
|
|
2056
|
-
transport.#activeSubs.delete(logicalId);
|
|
2057
|
-
if (transport.#mux) transport.#mux.cancel(sub.socketId);
|
|
2058
|
-
if (sub.waiter) {
|
|
2059
|
-
sub.waiter(null);
|
|
2060
|
-
sub.waiter = null;
|
|
2061
|
-
}
|
|
2062
|
-
}
|
|
2063
|
-
return {
|
|
2064
|
-
value: void 0,
|
|
2065
|
-
done: true
|
|
2066
|
-
};
|
|
2067
|
-
}
|
|
2068
|
-
};
|
|
2069
|
-
} };
|
|
2070
|
-
}
|
|
2071
|
-
};
|
|
2072
|
-
/** Convert an InboundMessage to a SubscribeEvent, or null if unrecognised. */
|
|
2073
|
-
function msgToEvent(msg) {
|
|
2074
|
-
switch (msg.kind) {
|
|
2075
|
-
case "snapshot":
|
|
2076
|
-
case "add":
|
|
2077
|
-
case "update": return {
|
|
2078
|
-
kind: msg.kind,
|
|
2079
|
-
data: msg.data,
|
|
2080
|
-
...msg.shapes !== void 0 ? { shapes: msg.shapes } : {}
|
|
2081
|
-
};
|
|
2082
|
-
case "chunk": return {
|
|
2083
|
-
kind: "chunk",
|
|
2084
|
-
data: msg.data,
|
|
2085
|
-
...msg.shapes !== void 0 ? { shapes: msg.shapes } : {},
|
|
2086
|
-
...msg.sequence !== void 0 ? { sequence: msg.sequence } : {}
|
|
2087
|
-
};
|
|
2088
|
-
case "remove": return {
|
|
2089
|
-
kind: "remove",
|
|
2090
|
-
removedId: msg.removedId ?? ""
|
|
2091
|
-
};
|
|
2092
|
-
case "end": return {
|
|
2093
|
-
kind: "end",
|
|
2094
|
-
...msg.final !== void 0 ? { final: msg.final } : {},
|
|
2095
|
-
...msg.reason !== void 0 ? { reason: msg.reason } : {}
|
|
2096
|
-
};
|
|
2097
|
-
case "error": {
|
|
2098
|
-
const first = (msg.errors ?? [{
|
|
2099
|
-
message: "unknown error",
|
|
2100
|
-
code: "unknown"
|
|
2101
|
-
}])[0] ?? {
|
|
2102
|
-
message: "unknown error",
|
|
2103
|
-
code: "unknown"
|
|
2104
|
-
};
|
|
2105
|
-
throw new SubscribeError(first.message, first.code ?? "unknown");
|
|
2106
|
-
}
|
|
2107
|
-
default: return null;
|
|
2108
|
-
}
|
|
2109
|
-
}
|
|
2110
|
-
|
|
2111
1961
|
//#endregion
|
|
2112
1962
|
//#region ../../packages/substrate-client/src/wire/pack.ts
|
|
2113
1963
|
function isPackMarker(v) {
|
|
@@ -2124,11 +1974,11 @@ function decodeJsonEnvelope(env) {
|
|
|
2124
1974
|
//#endregion
|
|
2125
1975
|
//#region ../../packages/substrate-client/src/client.ts
|
|
2126
1976
|
/**
|
|
2127
|
-
* SubstrateClient — the public facade over the HTTP and
|
|
1977
|
+
* SubstrateClient — the public facade over the HTTP and frame-stream transports, wire
|
|
2128
1978
|
* decoding and schema-drift handling.
|
|
2129
1979
|
*
|
|
2130
|
-
* Routing is protocol-mandated: query and mutate
|
|
2131
|
-
*
|
|
1980
|
+
* Routing is protocol-mandated: query and mutate are plain HTTP requests;
|
|
1981
|
+
* subscribe and stream are frame streams (one POST each, response held open).
|
|
2132
1982
|
*
|
|
2133
1983
|
* Drift detection is lazy-seeded: the client cannot replicate the server's
|
|
2134
1984
|
* federated schema-version hash, so the first envelope's `schemaVersion` seeds
|
|
@@ -2137,21 +1987,19 @@ function decodeJsonEnvelope(env) {
|
|
|
2137
1987
|
*/
|
|
2138
1988
|
var SubstrateClient = class {
|
|
2139
1989
|
#http;
|
|
2140
|
-
#ws = null;
|
|
2141
1990
|
#nl;
|
|
2142
1991
|
#admin;
|
|
2143
|
-
#
|
|
2144
|
-
#
|
|
2145
|
-
|
|
2146
|
-
#
|
|
1992
|
+
#streamWire;
|
|
1993
|
+
#canRefreshToken;
|
|
1994
|
+
/** Aborted by close(), ending every open subscription and stream. */
|
|
1995
|
+
#closing = new AbortController();
|
|
2147
1996
|
#reconnectOpts;
|
|
1997
|
+
#streamIdleMs;
|
|
2148
1998
|
#overloadResubscribeOpts;
|
|
2149
|
-
#webSocketImpl;
|
|
2150
1999
|
#schemaCache = null;
|
|
2151
2000
|
#schemaFetch = null;
|
|
2152
2001
|
#driftCachedVersion = null;
|
|
2153
2002
|
#closed = false;
|
|
2154
|
-
#wsReqSeq = 0;
|
|
2155
2003
|
#cache;
|
|
2156
2004
|
#onDiagnostics;
|
|
2157
2005
|
constructor(opts) {
|
|
@@ -2162,17 +2010,15 @@ var SubstrateClient = class {
|
|
|
2162
2010
|
...opts.getToken !== void 0 ? { getToken: opts.getToken } : {},
|
|
2163
2011
|
...opts.fetch !== void 0 ? { fetch: opts.fetch } : {}
|
|
2164
2012
|
});
|
|
2165
|
-
this.#
|
|
2166
|
-
this.#
|
|
2167
|
-
this.#wsGetToken = opts.getToken;
|
|
2168
|
-
this.#wsEncoding = opts.wsEncoding ?? "json";
|
|
2013
|
+
this.#streamWire = opts.streamWire ?? "json";
|
|
2014
|
+
this.#canRefreshToken = opts.token === void 0 && opts.getToken !== void 0;
|
|
2169
2015
|
this.#reconnectOpts = opts.reconnect;
|
|
2016
|
+
this.#streamIdleMs = opts.streamIdleMs;
|
|
2170
2017
|
this.#overloadResubscribeOpts = opts.overloadResubscribe;
|
|
2171
|
-
this.#webSocketImpl = opts.webSocket;
|
|
2172
2018
|
this.#onDiagnostics = opts.onDiagnostics ?? createDefaultDiagnosticsSink();
|
|
2173
2019
|
this.#cache = new CacheStore({ idleGcMs: 5e3 });
|
|
2174
2020
|
}
|
|
2175
|
-
/** Natural-language ask (
|
|
2021
|
+
/** Natural-language ask (frame stream or JSON) and the served namespace pack. Requires the substrate-nl plugin. */
|
|
2176
2022
|
get nl() {
|
|
2177
2023
|
this.#nl ??= createNlApi(this.#http);
|
|
2178
2024
|
return this.#nl;
|
|
@@ -2220,17 +2066,28 @@ var SubstrateClient = class {
|
|
|
2220
2066
|
});
|
|
2221
2067
|
return this.#decode(raw, frame, wire);
|
|
2222
2068
|
}
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2069
|
+
/** A live query: a snapshot, then deltas. `opts.wire` and `opts.signal` apply. */
|
|
2070
|
+
subscribe(frame, opts = {}) {
|
|
2071
|
+
return this.#frameStream("/substrate/v1/subscribe", frame, opts);
|
|
2072
|
+
}
|
|
2073
|
+
/** A one-shot streamed resolver: chunks, then `end`. `opts.wire` and `opts.signal` apply. */
|
|
2074
|
+
stream(frame, opts = {}) {
|
|
2075
|
+
return this.#frameStream("/substrate/v1/stream", frame, opts);
|
|
2228
2076
|
}
|
|
2229
|
-
|
|
2077
|
+
#frameStream(path, frame, opts) {
|
|
2230
2078
|
this.#ensureOpen();
|
|
2231
|
-
const
|
|
2232
|
-
const
|
|
2233
|
-
|
|
2079
|
+
const body = flatFrame(frame);
|
|
2080
|
+
const wire = opts.wire ?? this.#streamWire;
|
|
2081
|
+
const signal = opts.signal === void 0 ? this.#closing.signal : anySignal([this.#closing.signal, opts.signal]);
|
|
2082
|
+
const inner = openFrameStream(this.#http, path, body, {
|
|
2083
|
+
wire,
|
|
2084
|
+
canRefreshToken: this.#canRefreshToken,
|
|
2085
|
+
signal,
|
|
2086
|
+
...this.#reconnectOpts !== void 0 ? { reconnect: this.#reconnectOpts } : {},
|
|
2087
|
+
...this.#streamIdleMs !== void 0 ? { idleMs: this.#streamIdleMs } : {},
|
|
2088
|
+
...this.#overloadResubscribeOpts !== void 0 ? { overload: this.#overloadResubscribeOpts } : {}
|
|
2089
|
+
});
|
|
2090
|
+
return wire === "pack" ? this.#wrapPackDecode(inner, body) : inner;
|
|
2234
2091
|
}
|
|
2235
2092
|
/**
|
|
2236
2093
|
* Live typed-row view over a single-key subscription. Subscribes under the
|
|
@@ -2241,7 +2098,7 @@ var SubstrateClient = class {
|
|
|
2241
2098
|
*
|
|
2242
2099
|
* Completes when the subscription ends; throws on an error frame — the same
|
|
2243
2100
|
* contract as `subscribe`. Breaking the consuming `for await` cancels the
|
|
2244
|
-
* underlying
|
|
2101
|
+
* underlying subscription. Reconnect / overload-resubscribe are handled by
|
|
2245
2102
|
* `subscribe`; each fresh snapshot atomically replaces the row-set.
|
|
2246
2103
|
*
|
|
2247
2104
|
* Single-qualified-key only: multi-key frames throw; use `subscribe` for
|
|
@@ -2281,22 +2138,7 @@ var SubstrateClient = class {
|
|
|
2281
2138
|
async close() {
|
|
2282
2139
|
if (this.#closed) return;
|
|
2283
2140
|
this.#closed = true;
|
|
2284
|
-
|
|
2285
|
-
await this.#ws.close();
|
|
2286
|
-
this.#ws = null;
|
|
2287
|
-
}
|
|
2288
|
-
}
|
|
2289
|
-
#getWs() {
|
|
2290
|
-
if (this.#ws === null) this.#ws = new WsTransport({
|
|
2291
|
-
endpoint: this.#wsEndpoint,
|
|
2292
|
-
encoding: this.#wsEncoding,
|
|
2293
|
-
...this.#wsToken !== void 0 ? { token: this.#wsToken } : {},
|
|
2294
|
-
...this.#wsGetToken !== void 0 ? { getToken: this.#wsGetToken } : {},
|
|
2295
|
-
...this.#reconnectOpts !== void 0 ? { reconnect: this.#reconnectOpts } : {},
|
|
2296
|
-
...this.#overloadResubscribeOpts !== void 0 ? { overloadResubscribe: this.#overloadResubscribeOpts } : {},
|
|
2297
|
-
...this.#webSocketImpl !== void 0 ? { webSocket: this.#webSocketImpl } : {}
|
|
2298
|
-
});
|
|
2299
|
-
return this.#ws;
|
|
2141
|
+
this.#closing.abort();
|
|
2300
2142
|
}
|
|
2301
2143
|
async #refetchSchema() {
|
|
2302
2144
|
const schema = await this.#http.federatedSchema();
|
|
@@ -2328,31 +2170,7 @@ var SubstrateClient = class {
|
|
|
2328
2170
|
return env;
|
|
2329
2171
|
}
|
|
2330
2172
|
/**
|
|
2331
|
-
*
|
|
2332
|
-
*
|
|
2333
|
-
* The id MUST be unique per call: a key-derived id lets a re-subscribe race
|
|
2334
|
-
* the old sub's terminal frame and silently kill the new one. The type-key
|
|
2335
|
-
* prefix is only for readable logs. WsTransport reuses the id verbatim on
|
|
2336
|
-
* reconnect, so it is stable for the subscription's lifetime.
|
|
2337
|
-
*/
|
|
2338
|
-
#frameToWsRequest(frame) {
|
|
2339
|
-
this.#wsReqSeq += 1;
|
|
2340
|
-
const id = `${Object.keys(frame).sort().join("|")}#${this.#wsReqSeq}`;
|
|
2341
|
-
const wsFrame = {};
|
|
2342
|
-
for (const [key, entry] of Object.entries(frame)) {
|
|
2343
|
-
const single = Array.isArray(entry) ? entry[0] : entry;
|
|
2344
|
-
wsFrame[key] = {
|
|
2345
|
-
params: single?.params ?? {},
|
|
2346
|
-
fields: single?.fields ?? {}
|
|
2347
|
-
};
|
|
2348
|
-
}
|
|
2349
|
-
return {
|
|
2350
|
-
id,
|
|
2351
|
-
frame: wsFrame
|
|
2352
|
-
};
|
|
2353
|
-
}
|
|
2354
|
-
/**
|
|
2355
|
-
* Wrap a WS event stream so each `data`-bearing frame (snapshot/add/update/
|
|
2173
|
+
* Wrap a frame event stream so each `data`-bearing frame (snapshot/add/update/
|
|
2356
2174
|
* chunk) gets its positional tuple values reconstructed into named-object
|
|
2357
2175
|
* rows using the cached federated schema. Errors from the inner stream
|
|
2358
2176
|
* (transport drop, server error frame) propagate untouched.
|
|
@@ -2403,9 +2221,11 @@ var SubstrateClient = class {
|
|
|
2403
2221
|
const { ns, name } = parsed;
|
|
2404
2222
|
currentNsByKey[key] = ns;
|
|
2405
2223
|
typeByKey[key] = name;
|
|
2406
|
-
const
|
|
2407
|
-
if (
|
|
2408
|
-
|
|
2224
|
+
const mutation = schemas[ns]?.mutations[name];
|
|
2225
|
+
if (mutation !== void 0) {
|
|
2226
|
+
const shapeOf = (fields) => mutationReturns(mutation, fields);
|
|
2227
|
+
returnsByKey[key] = Array.isArray(frameEntry) ? frameEntry.map((f) => shapeOf(f.fields)) : shapeOf(entry?.fields);
|
|
2228
|
+
} else fieldsByKey[key] = entry?.fields ?? {};
|
|
2409
2229
|
}
|
|
2410
2230
|
return {
|
|
2411
2231
|
fieldsByKey,
|
|
@@ -2416,6 +2236,17 @@ var SubstrateClient = class {
|
|
|
2416
2236
|
};
|
|
2417
2237
|
}
|
|
2418
2238
|
};
|
|
2239
|
+
function flatFrame(frame) {
|
|
2240
|
+
const flat = {};
|
|
2241
|
+
for (const [key, entry] of Object.entries(frame)) {
|
|
2242
|
+
const single = Array.isArray(entry) ? entry[0] : entry;
|
|
2243
|
+
flat[key] = {
|
|
2244
|
+
params: single?.params ?? {},
|
|
2245
|
+
fields: single?.fields ?? {}
|
|
2246
|
+
};
|
|
2247
|
+
}
|
|
2248
|
+
return flat;
|
|
2249
|
+
}
|
|
2419
2250
|
|
|
2420
2251
|
//#endregion
|
|
2421
2252
|
//#region ../../packages/substrate-client/src/token-target.ts
|
|
@@ -2439,7 +2270,7 @@ function tokenAudience(baseUrl) {
|
|
|
2439
2270
|
throw new Error(`refusing to send a credential to ${url.origin}: use https, or plain http only to a loopback host`);
|
|
2440
2271
|
}
|
|
2441
2272
|
/** `fetchImpl` with redirects refused: a redirect would carry the credential to wherever it points. */
|
|
2442
|
-
function noRedirectFetch(fetchImpl =
|
|
2273
|
+
function noRedirectFetch(fetchImpl = substrateFetch) {
|
|
2443
2274
|
return (input, init) => fetchImpl(input, {
|
|
2444
2275
|
...init,
|
|
2445
2276
|
redirect: "error"
|
|
@@ -2447,4 +2278,67 @@ function noRedirectFetch(fetchImpl = fetch) {
|
|
|
2447
2278
|
}
|
|
2448
2279
|
|
|
2449
2280
|
//#endregion
|
|
2450
|
-
|
|
2281
|
+
//#region ../../packages/substrate-client/src/from-env.ts
|
|
2282
|
+
/**
|
|
2283
|
+
* The capabilities a client asks for, from `SUBSTRATE_AGENT_CAPABILITIES` (csv),
|
|
2284
|
+
* defaulting to `substrate-policy`'s default.
|
|
2285
|
+
*
|
|
2286
|
+
* Not `ADMIN_CAPABILITIES`: a dataset-registry key intersects the claim, but a
|
|
2287
|
+
* file-registry key takes it verbatim, so a wider default would promote every
|
|
2288
|
+
* agent on such a deployment to admin.
|
|
2289
|
+
*/
|
|
2290
|
+
function resolveAgentCapabilities(env) {
|
|
2291
|
+
const raw = env.SUBSTRATE_AGENT_CAPABILITIES;
|
|
2292
|
+
if (raw === void 0 || raw.trim().length === 0) return DEFAULT_CAPABILITIES;
|
|
2293
|
+
const parsed = raw.split(",").map((c) => c.trim()).filter((c) => c.length > 0);
|
|
2294
|
+
return parsed.length > 0 ? parsed : DEFAULT_CAPABILITIES;
|
|
2295
|
+
}
|
|
2296
|
+
/**
|
|
2297
|
+
* Builds a client from `SUBSTRATE_URL` and `SUBSTRATE_AGENT_KEY_ID` +
|
|
2298
|
+
* `SUBSTRATE_AGENT_KEY_PATH`, signing every request as that kid.
|
|
2299
|
+
*
|
|
2300
|
+
* Redirects are refused and the token's audience is the endpoint's origin.
|
|
2301
|
+
*
|
|
2302
|
+
* @throws Error when `SUBSTRATE_URL` is unset or unsafe for a credential, when
|
|
2303
|
+
* only one of the key pair is set, when the key cannot be read, or when no
|
|
2304
|
+
* key is set and `allowAnonymous` is not.
|
|
2305
|
+
*/
|
|
2306
|
+
function createClientFromEnv(opts = {}) {
|
|
2307
|
+
const { env = process.env, allowAnonymous = false, fetch: fetchImpl, ...rest } = opts;
|
|
2308
|
+
const endpoint = trimTrailingSlashes(env.SUBSTRATE_URL ?? "");
|
|
2309
|
+
if (endpoint === "") throw new Error("SUBSTRATE_URL is required");
|
|
2310
|
+
const keyPath = env.SUBSTRATE_AGENT_KEY_PATH ?? "";
|
|
2311
|
+
const kid = env.SUBSTRATE_AGENT_KEY_ID ?? "";
|
|
2312
|
+
if (keyPath === "" !== (kid === "")) throw new Error(`SUBSTRATE_AGENT_KEY_PATH and SUBSTRATE_AGENT_KEY_ID must be set together; only ${keyPath !== "" ? "SUBSTRATE_AGENT_KEY_PATH" : "SUBSTRATE_AGENT_KEY_ID"} is set`);
|
|
2313
|
+
const base = {
|
|
2314
|
+
...rest,
|
|
2315
|
+
endpoint,
|
|
2316
|
+
fetch: noRedirectFetch(fetchImpl)
|
|
2317
|
+
};
|
|
2318
|
+
if (kid === "") {
|
|
2319
|
+
if (!allowAnonymous) throw new Error("no signing key: set SUBSTRATE_AGENT_KEY_ID and SUBSTRATE_AGENT_KEY_PATH, or pass allowAnonymous");
|
|
2320
|
+
return {
|
|
2321
|
+
client: new SubstrateClient(base),
|
|
2322
|
+
endpoint,
|
|
2323
|
+
kid: null
|
|
2324
|
+
};
|
|
2325
|
+
}
|
|
2326
|
+
const getToken = makeTokenProvider({
|
|
2327
|
+
keyPath,
|
|
2328
|
+
kid
|
|
2329
|
+
}, {
|
|
2330
|
+
capabilities: resolveAgentCapabilities(env),
|
|
2331
|
+
audience: tokenAudience(endpoint)
|
|
2332
|
+
});
|
|
2333
|
+
return {
|
|
2334
|
+
client: new SubstrateClient({
|
|
2335
|
+
...base,
|
|
2336
|
+
getToken
|
|
2337
|
+
}),
|
|
2338
|
+
endpoint,
|
|
2339
|
+
kid
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2343
|
+
//#endregion
|
|
2344
|
+
export { diagnose as C, createH2Fetch as E, DIAGNOSE_CHECKS as S, substrateFetch as T, SKIPPABLE_STEPS as _, SubstrateClient as a, planOnboarding as b, reduceSubscribeRows as c, NetworkError as d, SubscribeError as f, ONBOARD_STEPS as g, CURRENT_MARK as h, tokenAudience as i, watchRows as l, BACKFILL_CHUNK as m, resolveAgentCapabilities as n, collectDiagnostics as o, AdminBackfillError as p, noRedirectFetch as r, createDefaultDiagnosticsSink as s, createClientFromEnv as t, AuthError as u, STEP_MARKS as v, reconcileIdentities as w, requiredOutstanding as x, currentStep as y };
|