@mnmal-ai/substrate 0.0.0 → 1.0.0-alpha.0
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/LICENSE +202 -0
- package/README.md +118 -1
- package/dist/agent-token-BIboWOU0.js +200 -0
- package/dist/client.d.ts +782 -0
- package/dist/client.js +7 -0
- package/dist/cluster-worker.js +35 -0
- package/dist/content-type-C7T28xNb.js +25 -0
- package/dist/dataset-BXkus75W.js +1527 -0
- package/dist/env-utils-B7SGmoeJ.js +21 -0
- package/dist/errors-D4vKuwz8.d.ts +130 -0
- package/dist/fake-DUVtKVSE.js +36 -0
- package/dist/index-D7mSOFlM.d.ts +69 -0
- package/dist/pack.d.ts +192 -0
- package/dist/pack.js +5 -0
- package/dist/plugin-j1jt1l3n.js +3844 -0
- package/dist/plugin-t2g2wX0b.d.ts +1568 -0
- package/dist/proposals-bootstrap.js +37 -0
- package/dist/proposals.config.schema.json +20 -0
- package/dist/proposals.d.ts +118 -0
- package/dist/proposals.default-config.json +4 -0
- package/dist/proposals.js +4 -0
- package/dist/recall-bootstrap.js +451 -0
- package/dist/recall.config.schema.json +232 -0
- package/dist/recall.d.ts +1981 -0
- package/dist/recall.default-config.json +4 -0
- package/dist/recall.js +5 -0
- package/dist/repl-C52wyfMs.js +2213 -0
- package/dist/row-ByLFE2Wx.d.ts +719 -0
- package/dist/row-DK7lPZVe.js +732 -0
- package/dist/runtime-metrics-DwpDJ8vz.d.ts +159 -0
- package/dist/schema-g4FQLr0V.d.ts +433 -0
- package/dist/sdk.d.ts +694 -0
- package/dist/sdk.js +112 -0
- package/dist/src-C4DzaIQZ.js +837 -0
- package/dist/src-CrEgiMJQ.js +1015 -0
- package/dist/src-CzxvmhFn.js +2327 -0
- package/dist/src-D88LvCX8.js +1797 -0
- package/dist/src-D91yij6C.js +13523 -0
- package/dist/src-DuopV8u7.js +2408 -0
- package/dist/substrate-admin.js +808 -0
- package/dist/substrate.js +5477 -0
- package/dist/testing.d.ts +1240 -0
- package/dist/testing.js +702 -0
- package/dist/types-8c0gtnd2.d.ts +35 -0
- package/dist/ws-C72iVS5T.d.ts +143 -0
- package/package.json +117 -4
package/dist/testing.js
ADDED
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
2
|
+
import { N as MemoryEdgesStore, P as loadPlugins, at as WS_REASON_SERVER_RESTART, c as MemoryDataset, rt as signHs256, s as compose } from "./src-D91yij6C.js";
|
|
3
|
+
import { t as FakeEmbedder } from "./fake-DUVtKVSE.js";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import postgres from "postgres";
|
|
6
|
+
|
|
7
|
+
//#region ../../packages/substrate-dataset-pg/src/testing.ts
|
|
8
|
+
/**
|
|
9
|
+
* Safety rails for pg integration tests. Shared by every package whose suites
|
|
10
|
+
* run destructive setup (TRUNCATE / DELETE / DDL) against whatever database
|
|
11
|
+
* `POSTGRES_URL` names.
|
|
12
|
+
*
|
|
13
|
+
* Imported via the `@mnmal-ai/substrate-dataset-pg/testing` subpath, so every
|
|
14
|
+
* package shares one tested copy rather than hand-copying it.
|
|
15
|
+
*
|
|
16
|
+
* Independent checks:
|
|
17
|
+
*
|
|
18
|
+
* 1. `assertTestDatabase` — the database NAME must end `_test` or `_dev`, so a
|
|
19
|
+
* stray `POSTGRES_URL` cannot point a destructive suite at real data.
|
|
20
|
+
*
|
|
21
|
+
* 2. `assertNotSuperuser` — the CREDENTIAL must not be a superuser. The name
|
|
22
|
+
* check says nothing about what the role can reach, and a superuser
|
|
23
|
+
* bypasses every grant that would otherwise contain it.
|
|
24
|
+
*
|
|
25
|
+
* 3. `assertCannotWriteOutsideTestDatabases` — the credential must not be able
|
|
26
|
+
* to damage any non-test database. Check 2 implies this only while
|
|
27
|
+
* production runs as a superuser; this asks postgres directly.
|
|
28
|
+
*/
|
|
29
|
+
function assertTestDatabase(url, opts) {
|
|
30
|
+
if (url === void 0 || url === "") return;
|
|
31
|
+
let dbName;
|
|
32
|
+
try {
|
|
33
|
+
dbName = new URL(url).pathname.replace(/^\//, "");
|
|
34
|
+
} catch {
|
|
35
|
+
throw new Error(`POSTGRES_URL is not a valid URL: ${url}`);
|
|
36
|
+
}
|
|
37
|
+
if (dbName === "") throw new Error(`POSTGRES_URL must include a database name: ${url}`);
|
|
38
|
+
if (!isTestDatabaseName(dbName)) throw new Error(`Refusing to run ${opts.packageName} pg tests against database "${dbName}". These suites run destructive TRUNCATEs / DELETEs / DDL. POSTGRES_URL must name a database ending in \`_test\` or \`_dev\` (e.g. \`substrate_test\`).`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Returns whether the superuser probe's rows say `on`. Throws on a missing
|
|
42
|
+
* row rather than returning `false`: an unanswered probe is not evidence of
|
|
43
|
+
* safety.
|
|
44
|
+
*/
|
|
45
|
+
function interpretSuperuserProbe(rows) {
|
|
46
|
+
const su = rows[0]?.su;
|
|
47
|
+
if (su === void 0) throw new Error("superuser probe returned no row — refusing to treat an unanswerable probe as a safe one");
|
|
48
|
+
return su === "on";
|
|
49
|
+
}
|
|
50
|
+
/** Default probe: ask postgres whether the connected role is a superuser. */
|
|
51
|
+
async function probeIsSuperuser(url) {
|
|
52
|
+
const sql = postgres(url, {
|
|
53
|
+
max: 1,
|
|
54
|
+
onnotice: () => {}
|
|
55
|
+
});
|
|
56
|
+
try {
|
|
57
|
+
return interpretSuperuserProbe(await sql`SELECT current_setting('is_superuser') AS su`);
|
|
58
|
+
} finally {
|
|
59
|
+
await sql.end({ timeout: 5 }).catch(() => {});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function assertNotSuperuser(url, opts) {
|
|
63
|
+
if (url === void 0 || url === "") return;
|
|
64
|
+
if (!await (opts.probe ?? probeIsSuperuser)(url)) return;
|
|
65
|
+
throw new Error(`Refusing to run ${opts.packageName} pg tests with a SUPERUSER credential. These suites are destructive, and a superuser bypasses every grant that would otherwise contain them — including the ones protecting production. Connect as a least-privilege role instead, e.g. \`substrate_test\`:
|
|
66
|
+
POSTGRES_URL='postgres://substrate_test:<password>@127.0.0.1:5433/substrate_test'
|
|
67
|
+
Provision it as a superuser: CREATE ROLE substrate_test LOGIN PASSWORD '<password>'; CREATE DATABASE substrate_test OWNER substrate_test;`);
|
|
68
|
+
}
|
|
69
|
+
function isTestDatabaseName(name) {
|
|
70
|
+
return /_test$|_dev$/i.test(name);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A connection refused for one of these reasons is a definitive "cannot reach",
|
|
74
|
+
* which is the answer the guard wants. Anything else is an unanswered probe and
|
|
75
|
+
* must not be read as safe.
|
|
76
|
+
* 42501 insufficient_privilege — CONNECT revoked
|
|
77
|
+
* 28000 / 28P01 — pg_hba has no entry, or authentication failed
|
|
78
|
+
* 3D000 — the database was dropped between listing and connecting
|
|
79
|
+
*/
|
|
80
|
+
const UNREACHABLE_CODES = /* @__PURE__ */ new Set([
|
|
81
|
+
"42501",
|
|
82
|
+
"28000",
|
|
83
|
+
"28P01",
|
|
84
|
+
"3D000"
|
|
85
|
+
]);
|
|
86
|
+
function withDatabase(url, database) {
|
|
87
|
+
const parsed = new URL(url);
|
|
88
|
+
parsed.pathname = `/${encodeURIComponent(database)}`;
|
|
89
|
+
return parsed.toString();
|
|
90
|
+
}
|
|
91
|
+
async function probeReach(url) {
|
|
92
|
+
const findings = [];
|
|
93
|
+
const home = postgres(url, {
|
|
94
|
+
max: 1,
|
|
95
|
+
onnotice: () => {}
|
|
96
|
+
});
|
|
97
|
+
let others;
|
|
98
|
+
try {
|
|
99
|
+
others = await home`
|
|
100
|
+
SELECT datname, pg_has_role(current_user, datdba, 'USAGE') AS owns
|
|
101
|
+
FROM pg_database WHERE datallowconn ORDER BY datname
|
|
102
|
+
`;
|
|
103
|
+
} finally {
|
|
104
|
+
await home.end({ timeout: 5 }).catch(() => {});
|
|
105
|
+
}
|
|
106
|
+
for (const { datname, owns } of others) {
|
|
107
|
+
if (isTestDatabaseName(datname)) continue;
|
|
108
|
+
if (owns) {
|
|
109
|
+
findings.push({
|
|
110
|
+
database: datname,
|
|
111
|
+
via: "owns-database"
|
|
112
|
+
});
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const sql = postgres(withDatabase(url, datname), {
|
|
116
|
+
max: 1,
|
|
117
|
+
connect_timeout: 5,
|
|
118
|
+
onnotice: () => {}
|
|
119
|
+
});
|
|
120
|
+
try {
|
|
121
|
+
const rows = await sql`
|
|
122
|
+
SELECT n.nspname || '.' || c.relname AS rel
|
|
123
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
124
|
+
WHERE c.relkind IN ('r', 'p')
|
|
125
|
+
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
|
|
126
|
+
AND n.nspname NOT LIKE 'pg\\_toast%'
|
|
127
|
+
AND (pg_has_role(current_user, c.relowner, 'USAGE')
|
|
128
|
+
OR has_table_privilege(c.oid, 'INSERT, UPDATE, DELETE, TRUNCATE'))
|
|
129
|
+
ORDER BY 1 LIMIT 5
|
|
130
|
+
`;
|
|
131
|
+
if (rows.length > 0) findings.push({
|
|
132
|
+
database: datname,
|
|
133
|
+
via: "writable-relation",
|
|
134
|
+
relations: rows.map((r) => r.rel)
|
|
135
|
+
});
|
|
136
|
+
} catch (err) {
|
|
137
|
+
const code = err.code;
|
|
138
|
+
if (typeof code === "string" && UNREACHABLE_CODES.has(code)) continue;
|
|
139
|
+
throw new Error(`reach probe could not answer for database "${datname}" — refusing to treat an unanswerable probe as a safe one: ${String(err)}`);
|
|
140
|
+
} finally {
|
|
141
|
+
await sql.end({ timeout: 5 }).catch(() => {});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return findings;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Throws if the credential in `url` owns, or can write to, anything in a
|
|
148
|
+
* database whose name is not a test name. No-op when `url` is unset.
|
|
149
|
+
*
|
|
150
|
+
* Deliberately not a CONNECT check: postgres grants CONNECT to PUBLIC, so a
|
|
151
|
+
* correctly scoped test role can still connect to production.
|
|
152
|
+
*/
|
|
153
|
+
async function assertCannotWriteOutsideTestDatabases(url, opts) {
|
|
154
|
+
if (url === void 0 || url === "") return;
|
|
155
|
+
const message = reachFailureMessage(await (opts.probe ?? probeReach)(url), opts.packageName);
|
|
156
|
+
if (message !== void 0) throw new Error(message);
|
|
157
|
+
}
|
|
158
|
+
function reachFailureMessage(findings, packageName) {
|
|
159
|
+
if (findings.length === 0) return void 0;
|
|
160
|
+
return `Refusing to run ${packageName} pg tests: the POSTGRES_URL credential can damage databases that are not test databases. These suites are destructive, so the credential must be one that can only reach test data. A production credential looks exactly like this.
|
|
161
|
+
${findings.map((f) => f.via === "owns-database" ? ` - ${f.database}: the role OWNS this database` : ` - ${f.database}: the role can write ${(f.relations ?? []).join(", ")}`).join("\n")}\nConnect as a least-privilege role that owns only the test database, e.g. \`substrate_test\`. Provision it as a superuser: CREATE ROLE substrate_test LOGIN PASSWORD '<password>'; CREATE DATABASE substrate_test OWNER substrate_test;`;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Writes one stderr line when `url` is unset, saying this package's pg-gated
|
|
165
|
+
* suites will skip — otherwise a skip looks the same as a pass in the gate.
|
|
166
|
+
*
|
|
167
|
+
* Call it from `vitest.config.ts` (evaluated once per run), not `setupFiles`
|
|
168
|
+
* (once per file). It uses `process.stderr.write` because vitest swallows
|
|
169
|
+
* `console.warn` when every file skips.
|
|
170
|
+
*/
|
|
171
|
+
function announcePgGating(url, opts) {
|
|
172
|
+
if (url !== void 0 && url !== "") return;
|
|
173
|
+
(opts.warn ?? ((m) => void process.stderr.write(`${m}\n`)))(`[pg-gated] ${opts.packageName}: POSTGRES_URL is unset — this package's pg suites will SKIP, not run. Set it to a database whose name ends in _test to exercise them.`);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Prefix for every per-package test schema.
|
|
177
|
+
*/
|
|
178
|
+
const TEST_SCHEMA_PREFIX = "substrate_test_";
|
|
179
|
+
/**
|
|
180
|
+
* Returns the schema a package's pg-gated suites own.
|
|
181
|
+
*
|
|
182
|
+
* Deterministic rather than per-run, so the schema persists between runs and
|
|
183
|
+
* migrations stay incremental.
|
|
184
|
+
*/
|
|
185
|
+
function pgTestSchemaName(packageName) {
|
|
186
|
+
const slug = packageName.toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "");
|
|
187
|
+
return `${TEST_SCHEMA_PREFIX}${slug}`.slice(0, 63);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Returns `url` with `search_path` pinned to this package's schema.
|
|
191
|
+
*
|
|
192
|
+
* A startup parameter, not `SET search_path`: with a pool, a `SET` reaches
|
|
193
|
+
* only one connection. `public` stays on the path because the `vector` type
|
|
194
|
+
* lives there.
|
|
195
|
+
*
|
|
196
|
+
* The query string is built by hand because postgres.js needs the space as
|
|
197
|
+
* `%20`; `URLSearchParams` would emit `+`, which postgres rejects.
|
|
198
|
+
*/
|
|
199
|
+
function pgTestSchemaUrl(url, packageName) {
|
|
200
|
+
const wanted = `-c search_path=${pgTestSchemaName(packageName)},public`;
|
|
201
|
+
const parsed = new URL(url);
|
|
202
|
+
if (parsed.searchParams.get("options") === wanted) return url;
|
|
203
|
+
const params = [...parsed.searchParams.entries()].filter(([key]) => key !== "options");
|
|
204
|
+
params.push(["options", wanted]);
|
|
205
|
+
parsed.search = params.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join("&");
|
|
206
|
+
return parsed.toString();
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Returns a warning naming substrate relations left in `public`, or undefined if
|
|
210
|
+
* there are none.
|
|
211
|
+
*
|
|
212
|
+
* With `public` on the search_path, an unqualified name missing from the
|
|
213
|
+
* package schema resolves to `public`'s copy, so leftovers there are unsafe.
|
|
214
|
+
* They are named rather than dropped: this code does not own them.
|
|
215
|
+
*/
|
|
216
|
+
function publicLeftoverWarning(names, packageName) {
|
|
217
|
+
if (names.length === 0) return void 0;
|
|
218
|
+
return `[pg-gated] ${packageName}: \`public\` still holds ${names.length} substrate relation(s) — ${names.join(", ")}. These are leftovers from before the pg suites moved into per-package schemas. They are not read by anything now, but while they exist an unqualified name that is missing from the package schema can still resolve to them. Drop them once:
|
|
219
|
+
psql "\$POSTGRES_URL" -c 'DROP TABLE IF EXISTS ${names.join(", ")} CASCADE'`;
|
|
220
|
+
}
|
|
221
|
+
async function findPublicLeftoversOnce(url) {
|
|
222
|
+
const sql = postgres(url, {
|
|
223
|
+
max: 1,
|
|
224
|
+
onnotice: () => {}
|
|
225
|
+
});
|
|
226
|
+
try {
|
|
227
|
+
return (await sql`
|
|
228
|
+
SELECT c.relname FROM pg_class c
|
|
229
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
230
|
+
WHERE n.nspname = 'public' AND c.relkind = 'r' AND c.relname LIKE 'substrate\\_%'
|
|
231
|
+
ORDER BY c.relname
|
|
232
|
+
`).map((r) => r.relname);
|
|
233
|
+
} finally {
|
|
234
|
+
await sql.end({ timeout: 5 }).catch(() => {});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
async function createSchemaOnce(url, schema) {
|
|
238
|
+
const sql = postgres(url, {
|
|
239
|
+
max: 1,
|
|
240
|
+
onnotice: () => {}
|
|
241
|
+
});
|
|
242
|
+
try {
|
|
243
|
+
await sql.unsafe(`CREATE SCHEMA IF NOT EXISTS "${schema}"`);
|
|
244
|
+
} finally {
|
|
245
|
+
await sql.end({ timeout: 5 }).catch(() => {});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Creates this package's test schema and returns the URL that reaches it, or
|
|
250
|
+
* `undefined` when there is no `POSTGRES_URL`. Warns about leftovers in `public`.
|
|
251
|
+
*
|
|
252
|
+
* Packages run concurrently and their suites drop and re-migrate the shared
|
|
253
|
+
* tables, so each needs its own schema. A schema rather than a database,
|
|
254
|
+
* because the least-privilege `substrate_test` role has no CREATEDB.
|
|
255
|
+
*
|
|
256
|
+
* Call from `globalSetup`, so forked workers inherit the rewritten URL.
|
|
257
|
+
*/
|
|
258
|
+
async function preparePgTestSchema(url, opts) {
|
|
259
|
+
if (url === void 0 || url === "") return void 0;
|
|
260
|
+
const schema = pgTestSchemaName(opts.packageName);
|
|
261
|
+
await (opts.createSchema ?? createSchemaOnce)(url, schema);
|
|
262
|
+
const warning = publicLeftoverWarning(await (opts.findPublicLeftovers ?? findPublicLeftoversOnce)(url), opts.packageName);
|
|
263
|
+
if (warning !== void 0) (opts.warn ?? ((m) => void process.stderr.write(`${m}\n`)))(warning);
|
|
264
|
+
return pgTestSchemaUrl(url, opts.packageName);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region ../../packages/substrate-server-memory/src/memory-ws-connection.ts
|
|
269
|
+
/**
|
|
270
|
+
* In-process WebSocket pair. One end is the server-side `WsConnection`
|
|
271
|
+
* handed to the user's `WsHandler`; the other is a `MemoryWsClient` returned
|
|
272
|
+
* to test code so it can drive the connection without opening a socket.
|
|
273
|
+
*
|
|
274
|
+
* Per §4.8 transport symmetry, upgrade metadata (path, headers, query, params)
|
|
275
|
+
* rides on `SubstrateRequest`, not on `WsConnection`. The pair only needs an
|
|
276
|
+
* `id` — the caller (`MemorySubstrateServer`) builds the `SubstrateRequest` separately.
|
|
277
|
+
*
|
|
278
|
+
* Frames are normalised to Buffer on delivery so handlers see the same shape
|
|
279
|
+
* they would over a real WS (the `WsConnection.onMessage` signature is
|
|
280
|
+
* `(frame: Buffer) => void`). `send()` accepts Buffer / Uint8Array / string on
|
|
281
|
+
* both sides and normalises before dispatch.
|
|
282
|
+
*
|
|
283
|
+
* Close semantics:
|
|
284
|
+
* - First close from either side transitions the pair to closed and fires
|
|
285
|
+
* both sides' onClose callbacks synchronously with the provided code.
|
|
286
|
+
* - Subsequent close() calls are no-ops (no duplicate events).
|
|
287
|
+
* - send() on a closed endpoint throws a named error — callers that race
|
|
288
|
+
* a close against a send should handle this, mirroring real WS behaviour
|
|
289
|
+
* where the underlying socket may have shut down mid-write.
|
|
290
|
+
*/
|
|
291
|
+
/**
|
|
292
|
+
* @internal — thrown when sending on a closed in-memory WS pair. Treat as
|
|
293
|
+
* package-private.
|
|
294
|
+
*/
|
|
295
|
+
var WsClosedError = class extends Error {
|
|
296
|
+
constructor() {
|
|
297
|
+
super("WebSocket is closed");
|
|
298
|
+
this.name = "WsClosedError";
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
function toBuffer(frame) {
|
|
302
|
+
if (typeof frame === "string") return Buffer.from(frame, "utf8");
|
|
303
|
+
if (Buffer.isBuffer(frame)) return frame;
|
|
304
|
+
return Buffer.from(frame);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* @internal — paired in-memory WS endpoints used by `MemorySubstrateServer`
|
|
308
|
+
* for inject-style WS testing. Treat as package-private.
|
|
309
|
+
*/
|
|
310
|
+
function createMemoryWsPair() {
|
|
311
|
+
const serverSide = {
|
|
312
|
+
messageListeners: [],
|
|
313
|
+
closeListeners: []
|
|
314
|
+
};
|
|
315
|
+
const clientSide = {
|
|
316
|
+
messageListeners: [],
|
|
317
|
+
closeListeners: []
|
|
318
|
+
};
|
|
319
|
+
const state = { closed: false };
|
|
320
|
+
const closePair = (code) => {
|
|
321
|
+
if (state.closed) return;
|
|
322
|
+
state.closed = true;
|
|
323
|
+
const s = serverSide.closeListeners.slice();
|
|
324
|
+
const c = clientSide.closeListeners.slice();
|
|
325
|
+
for (const cb of s) try {
|
|
326
|
+
cb(code);
|
|
327
|
+
} catch {}
|
|
328
|
+
for (const cb of c) try {
|
|
329
|
+
cb(code);
|
|
330
|
+
} catch {}
|
|
331
|
+
};
|
|
332
|
+
return {
|
|
333
|
+
server: {
|
|
334
|
+
id: randomUUID(),
|
|
335
|
+
send(frame) {
|
|
336
|
+
if (state.closed) throw new WsClosedError();
|
|
337
|
+
const buf = toBuffer(frame);
|
|
338
|
+
for (const cb of clientSide.messageListeners.slice()) cb(buf);
|
|
339
|
+
},
|
|
340
|
+
onMessage(cb) {
|
|
341
|
+
serverSide.messageListeners.push(cb);
|
|
342
|
+
},
|
|
343
|
+
onClose(cb) {
|
|
344
|
+
serverSide.closeListeners.push(cb);
|
|
345
|
+
},
|
|
346
|
+
close(code, _reason) {
|
|
347
|
+
closePair(code);
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
client: {
|
|
351
|
+
send(frame) {
|
|
352
|
+
if (state.closed) throw new WsClosedError();
|
|
353
|
+
const buf = toBuffer(frame);
|
|
354
|
+
for (const cb of serverSide.messageListeners.slice()) cb(buf);
|
|
355
|
+
},
|
|
356
|
+
onMessage(cb) {
|
|
357
|
+
clientSide.messageListeners.push(cb);
|
|
358
|
+
},
|
|
359
|
+
onClose(cb) {
|
|
360
|
+
clientSide.closeListeners.push(cb);
|
|
361
|
+
},
|
|
362
|
+
close(code, _reason) {
|
|
363
|
+
closePair(code);
|
|
364
|
+
},
|
|
365
|
+
get closed() {
|
|
366
|
+
return state.closed;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
//#endregion
|
|
373
|
+
//#region ../../packages/substrate-server-memory/src/route-matcher.ts
|
|
374
|
+
/**
|
|
375
|
+
* Pure path-pattern matcher used by `MemorySubstrateServer`.
|
|
376
|
+
*
|
|
377
|
+
* Supported syntax:
|
|
378
|
+
* - Literal segments: `/foo/bar`
|
|
379
|
+
* - Named captures: `/users/:id` → params.id
|
|
380
|
+
* - Trailing wildcard: `/files/*` → params.rest (captures joined tail, may be empty)
|
|
381
|
+
*
|
|
382
|
+
* Design calls:
|
|
383
|
+
* - Matching is exact on segment count unless the last registered segment is `*`.
|
|
384
|
+
* - Trailing slash is significant. `/foo` does not match `/foo/`, and vice-versa.
|
|
385
|
+
* Networked servers normalise this in their own idiomatic way; keeping the
|
|
386
|
+
* matcher strict makes test assertions unambiguous.
|
|
387
|
+
* - The matcher does NOT strip or parse querystrings. Callers pre-strip.
|
|
388
|
+
* - The matcher does NOT decode URI components. Tests that need encoded values
|
|
389
|
+
* can assert against the raw bytes; decoding is a per-caller policy.
|
|
390
|
+
*/
|
|
391
|
+
/**
|
|
392
|
+
* @internal — used by `MemorySubstrateServer.inject` for path-pattern matching.
|
|
393
|
+
* Treat as package-private.
|
|
394
|
+
*/
|
|
395
|
+
function matchRoute(registered, actual) {
|
|
396
|
+
const regSegs = splitPath(registered);
|
|
397
|
+
const actSegs = splitPath(actual);
|
|
398
|
+
if ((regSegs.length > 0 ? regSegs[regSegs.length - 1] : void 0) === "*") {
|
|
399
|
+
const prefixLen = regSegs.length - 1;
|
|
400
|
+
if (actSegs.length < prefixLen) return null;
|
|
401
|
+
const params = {};
|
|
402
|
+
for (let i = 0; i < prefixLen; i++) {
|
|
403
|
+
const r = regSegs[i];
|
|
404
|
+
const a = actSegs[i];
|
|
405
|
+
if (r === void 0 || a === void 0) return null;
|
|
406
|
+
const paramMatch = paramName(r);
|
|
407
|
+
if (paramMatch !== null) params[paramMatch] = a;
|
|
408
|
+
else if (r !== a) return null;
|
|
409
|
+
}
|
|
410
|
+
params.rest = actSegs.slice(prefixLen).join("/");
|
|
411
|
+
return params;
|
|
412
|
+
}
|
|
413
|
+
if (regSegs.length !== actSegs.length) return null;
|
|
414
|
+
const params = {};
|
|
415
|
+
for (let i = 0; i < regSegs.length; i++) {
|
|
416
|
+
const r = regSegs[i];
|
|
417
|
+
const a = actSegs[i];
|
|
418
|
+
if (r === void 0 || a === void 0) return null;
|
|
419
|
+
const paramMatch = paramName(r);
|
|
420
|
+
if (paramMatch !== null) params[paramMatch] = a;
|
|
421
|
+
else if (r !== a) return null;
|
|
422
|
+
}
|
|
423
|
+
return params;
|
|
424
|
+
}
|
|
425
|
+
function splitPath(p) {
|
|
426
|
+
if (p === "" || p === "/") return [];
|
|
427
|
+
return (p.startsWith("/") ? p.slice(1) : p).split("/");
|
|
428
|
+
}
|
|
429
|
+
function paramName(seg) {
|
|
430
|
+
if (seg.length >= 2 && seg.startsWith(":")) return seg.slice(1);
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
//#endregion
|
|
435
|
+
//#region ../../packages/substrate-server-memory/src/memory-server.ts
|
|
436
|
+
var UnknownWsPathError = class extends Error {
|
|
437
|
+
constructor(path) {
|
|
438
|
+
super(`No websocket route registered for path: ${path}`);
|
|
439
|
+
this.name = "UnknownWsPathError";
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
/**
|
|
443
|
+
* @internal — thrown when `MemorySubstrateServer.start()` is called twice.
|
|
444
|
+
* Re-exported from the package index; treat as package-private.
|
|
445
|
+
*/
|
|
446
|
+
var AlreadyStartedError = class extends Error {
|
|
447
|
+
constructor() {
|
|
448
|
+
super("MemorySubstrateServer is already started");
|
|
449
|
+
this.name = "AlreadyStartedError";
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
const JSON_HEADERS = Object.freeze({ "content-type": "application/json" });
|
|
453
|
+
function jsonBody(obj) {
|
|
454
|
+
return Buffer.from(JSON.stringify(obj), "utf8");
|
|
455
|
+
}
|
|
456
|
+
function opForRoute(method, pathname) {
|
|
457
|
+
if (method !== "POST") return null;
|
|
458
|
+
if (pathname === "/substrate/v1/query") return "query";
|
|
459
|
+
if (pathname === "/substrate/v1/mutate") return "mutate";
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
function buildPluginPrefix(name) {
|
|
463
|
+
return `/substrate/v1/plugins/${name}`;
|
|
464
|
+
}
|
|
465
|
+
function normaliseSubpath(path) {
|
|
466
|
+
if (path.length === 0) return "/";
|
|
467
|
+
return path.startsWith("/") ? path : `/${path}`;
|
|
468
|
+
}
|
|
469
|
+
function parseQuery(path) {
|
|
470
|
+
const q = path.indexOf("?");
|
|
471
|
+
if (q === -1) return {
|
|
472
|
+
pathname: path,
|
|
473
|
+
query: {}
|
|
474
|
+
};
|
|
475
|
+
const pathname = path.slice(0, q);
|
|
476
|
+
const qs = new URLSearchParams(path.slice(q + 1));
|
|
477
|
+
const query = {};
|
|
478
|
+
for (const [k, v] of qs.entries()) query[k] = v;
|
|
479
|
+
return {
|
|
480
|
+
pathname,
|
|
481
|
+
query
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
var MemorySubstrateServer = class {
|
|
485
|
+
#routes = [];
|
|
486
|
+
#wsRoutes = [];
|
|
487
|
+
#inFlight = /* @__PURE__ */ new Set();
|
|
488
|
+
#shutdownHooks = /* @__PURE__ */ new Set();
|
|
489
|
+
#errorHook;
|
|
490
|
+
#observer;
|
|
491
|
+
#inFlightObserver;
|
|
492
|
+
#started = false;
|
|
493
|
+
#pending = 0;
|
|
494
|
+
route(method, path, handler) {
|
|
495
|
+
this.#routes.push({
|
|
496
|
+
method,
|
|
497
|
+
pattern: path,
|
|
498
|
+
handler
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
websocket(path, handler) {
|
|
502
|
+
this.#wsRoutes.push({
|
|
503
|
+
pattern: path,
|
|
504
|
+
handler
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
onError(hook) {
|
|
508
|
+
this.#errorHook = hook;
|
|
509
|
+
}
|
|
510
|
+
observeRequests(observer) {
|
|
511
|
+
this.#observer = observer;
|
|
512
|
+
}
|
|
513
|
+
observeInFlight(fn) {
|
|
514
|
+
this.#inFlightObserver = fn;
|
|
515
|
+
}
|
|
516
|
+
#notifyInFlight(fn, op, delta) {
|
|
517
|
+
if (fn === void 0) return;
|
|
518
|
+
try {
|
|
519
|
+
fn(op, delta);
|
|
520
|
+
} catch {}
|
|
521
|
+
}
|
|
522
|
+
async start(_opts) {
|
|
523
|
+
if (this.#started) throw new AlreadyStartedError();
|
|
524
|
+
this.#started = true;
|
|
525
|
+
return {
|
|
526
|
+
address: "memory://",
|
|
527
|
+
port: 0
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
async stop(opts) {
|
|
531
|
+
this.#notifyShutdown(WS_REASON_SERVER_RESTART);
|
|
532
|
+
if (opts?.abortInFlight === true) this.abortAllRequests("server_shutdown");
|
|
533
|
+
const grace = opts?.grace;
|
|
534
|
+
if (grace !== void 0 && this.#pending > 0) await new Promise((resolve) => {
|
|
535
|
+
const deadline = setTimeout(resolve, grace);
|
|
536
|
+
deadline.unref?.();
|
|
537
|
+
const tick = () => {
|
|
538
|
+
if (this.#pending === 0) {
|
|
539
|
+
clearTimeout(deadline);
|
|
540
|
+
resolve();
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
setTimeout(tick, 1).unref?.();
|
|
544
|
+
};
|
|
545
|
+
tick();
|
|
546
|
+
});
|
|
547
|
+
this.#started = false;
|
|
548
|
+
}
|
|
549
|
+
abortAllRequests(reason) {
|
|
550
|
+
const controllers = [...this.#inFlight];
|
|
551
|
+
for (const controller of controllers) if (!controller.signal.aborted) controller.abort(reason ?? "aborted");
|
|
552
|
+
}
|
|
553
|
+
onShutdown(cb) {
|
|
554
|
+
this.#shutdownHooks.add(cb);
|
|
555
|
+
}
|
|
556
|
+
#notifyShutdown(reason) {
|
|
557
|
+
for (const hook of [...this.#shutdownHooks]) try {
|
|
558
|
+
hook(reason);
|
|
559
|
+
} catch {}
|
|
560
|
+
}
|
|
561
|
+
createPluginRouter(plugin) {
|
|
562
|
+
const prefix = buildPluginPrefix(plugin.name);
|
|
563
|
+
const prefixed = (path) => `${prefix}${normaliseSubpath(path)}`;
|
|
564
|
+
return {
|
|
565
|
+
get: (path, handler) => this.route("GET", prefixed(path), handler),
|
|
566
|
+
post: (path, handler) => this.route("POST", prefixed(path), handler),
|
|
567
|
+
put: (path, handler) => this.route("PUT", prefixed(path), handler),
|
|
568
|
+
delete: (path, handler) => this.route("DELETE", prefixed(path), handler),
|
|
569
|
+
patch: (path, handler) => this.route("PATCH", prefixed(path), handler),
|
|
570
|
+
websocket: (path, handler) => this.websocket(prefixed(path), handler)
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
async inject(req) {
|
|
574
|
+
this.#pending++;
|
|
575
|
+
const ownedController = req.signal === void 0 ? new AbortController() : null;
|
|
576
|
+
if (ownedController !== null) this.#inFlight.add(ownedController);
|
|
577
|
+
const signal = req.signal ?? ownedController?.signal;
|
|
578
|
+
try {
|
|
579
|
+
const method = req.method ?? "GET";
|
|
580
|
+
const parsed = parseQuery(req.path ?? "/");
|
|
581
|
+
const query = req.query ?? parsed.query;
|
|
582
|
+
const headers = req.headers ?? {};
|
|
583
|
+
const body = req.body ?? null;
|
|
584
|
+
let matched;
|
|
585
|
+
let params = {};
|
|
586
|
+
for (const entry of this.#routes) {
|
|
587
|
+
if (entry.method !== method) continue;
|
|
588
|
+
const m = matchRoute(entry.pattern, parsed.pathname);
|
|
589
|
+
if (m !== null) {
|
|
590
|
+
matched = entry;
|
|
591
|
+
params = {
|
|
592
|
+
...m,
|
|
593
|
+
...req.params ?? {}
|
|
594
|
+
};
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
if (!matched) return {
|
|
599
|
+
status: 404,
|
|
600
|
+
headers: JSON_HEADERS,
|
|
601
|
+
body: jsonBody({ error: "not found" })
|
|
602
|
+
};
|
|
603
|
+
const substrateReq = {
|
|
604
|
+
method,
|
|
605
|
+
path: parsed.pathname,
|
|
606
|
+
headers,
|
|
607
|
+
query,
|
|
608
|
+
params,
|
|
609
|
+
body,
|
|
610
|
+
...req.identity !== void 0 ? { identity: req.identity } : {},
|
|
611
|
+
...req.peerIp !== void 0 ? { peerIp: req.peerIp } : {},
|
|
612
|
+
...signal !== void 0 ? { signal } : {}
|
|
613
|
+
};
|
|
614
|
+
try {
|
|
615
|
+
const op = opForRoute(method, parsed.pathname);
|
|
616
|
+
if (op === null) return await matched.handler(substrateReq);
|
|
617
|
+
const inflight = this.#inFlightObserver;
|
|
618
|
+
this.#notifyInFlight(inflight, op, 1);
|
|
619
|
+
try {
|
|
620
|
+
const startedAt = Date.now();
|
|
621
|
+
const res = await matched.handler(substrateReq);
|
|
622
|
+
const obs = this.#observer;
|
|
623
|
+
if (obs !== void 0) try {
|
|
624
|
+
obs({
|
|
625
|
+
op,
|
|
626
|
+
ok: res.status < 400,
|
|
627
|
+
durationMs: Date.now() - startedAt
|
|
628
|
+
});
|
|
629
|
+
} catch {}
|
|
630
|
+
return res;
|
|
631
|
+
} finally {
|
|
632
|
+
this.#notifyInFlight(inflight, op, -1);
|
|
633
|
+
}
|
|
634
|
+
} catch (err) {
|
|
635
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
636
|
+
if (this.#errorHook) try {
|
|
637
|
+
this.#errorHook(error, substrateReq);
|
|
638
|
+
} catch {}
|
|
639
|
+
return {
|
|
640
|
+
status: 500,
|
|
641
|
+
headers: JSON_HEADERS,
|
|
642
|
+
body: jsonBody({
|
|
643
|
+
schemaVersion: "",
|
|
644
|
+
data: {},
|
|
645
|
+
shapes: {},
|
|
646
|
+
errors: [{
|
|
647
|
+
message: "internal server error",
|
|
648
|
+
code: "internal_error"
|
|
649
|
+
}]
|
|
650
|
+
})
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
} finally {
|
|
654
|
+
this.#pending--;
|
|
655
|
+
if (ownedController !== null) this.#inFlight.delete(ownedController);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
#reportWsError(err) {
|
|
659
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
660
|
+
if (!this.#errorHook) return;
|
|
661
|
+
try {
|
|
662
|
+
this.#errorHook(error);
|
|
663
|
+
} catch {}
|
|
664
|
+
}
|
|
665
|
+
connectWebsocket(path, headers = {}) {
|
|
666
|
+
const parsed = parseQuery(path);
|
|
667
|
+
let matched;
|
|
668
|
+
let params = {};
|
|
669
|
+
for (const entry of this.#wsRoutes) {
|
|
670
|
+
const m = matchRoute(entry.pattern, parsed.pathname);
|
|
671
|
+
if (m !== null) {
|
|
672
|
+
matched = entry;
|
|
673
|
+
params = m;
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (!matched) throw new UnknownWsPathError(parsed.pathname);
|
|
678
|
+
const substrateReq = {
|
|
679
|
+
method: "GET",
|
|
680
|
+
path: parsed.pathname,
|
|
681
|
+
headers,
|
|
682
|
+
query: parsed.query,
|
|
683
|
+
params,
|
|
684
|
+
body: null
|
|
685
|
+
};
|
|
686
|
+
const { server, client } = createMemoryWsPair();
|
|
687
|
+
queueMicrotask(() => {
|
|
688
|
+
try {
|
|
689
|
+
const result = matched.handler(substrateReq, server);
|
|
690
|
+
if (result && typeof result.then === "function") result.catch((err) => {
|
|
691
|
+
this.#reportWsError(err);
|
|
692
|
+
});
|
|
693
|
+
} catch (err) {
|
|
694
|
+
this.#reportWsError(err);
|
|
695
|
+
}
|
|
696
|
+
});
|
|
697
|
+
return client;
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
//#endregion
|
|
702
|
+
export { AlreadyStartedError, FakeEmbedder, MemoryDataset, MemoryEdgesStore, MemorySubstrateServer, UnknownWsPathError, WsClosedError, announcePgGating, assertCannotWriteOutsideTestDatabases, assertNotSuperuser, assertTestDatabase, compose, createMemoryWsPair, interpretSuperuserProbe, loadPlugins, matchRoute, pgTestSchemaName, pgTestSchemaUrl, preparePgTestSchema, publicLeftoverWarning, reachFailureMessage, signHs256 };
|