tina4-nodejs 3.13.164 → 3.13.166
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/CLAUDE.md +12 -5
- package/package.json +1 -1
- package/packages/cli/dist/bin.js +1312 -979
- package/packages/cli/src/bin.ts +5 -2
- package/packages/cli/src/commands/lint.ts +3 -1
- package/packages/cli/src/commands/seed.ts +10 -11
- package/packages/core/dist/index.js +1232 -897
- package/packages/core/src/batchSpawn.ts +129 -0
- package/packages/core/src/connectionUrl.ts +242 -0
- package/packages/core/src/devAdmin.ts +25 -11
- package/packages/core/src/fakeData.ts +51 -13
- package/packages/core/src/index.ts +1 -1
- package/packages/core/src/mcp.ts +4 -1
- package/packages/core/src/mqtt.ts +24 -48
- package/packages/core/src/portTakeover.ts +31 -13
- package/packages/core/src/projectIndex.ts +3 -4
- package/packages/core/src/projectIndexStorage.ts +9 -1
- package/packages/core/src/queueBackends/rabbitmqBackend.ts +32 -20
- package/packages/core/src/response.ts +4 -1
- package/packages/core/src/routeDiscovery.ts +8 -2
- package/packages/core/src/router.ts +44 -30
- package/packages/core/src/server.ts +53 -33
- package/packages/core/src/session.ts +5 -0
- package/packages/core/src/sessionHandlers/databaseHandler.ts +16 -0
- package/packages/core/src/urlRedaction.ts +252 -0
- package/packages/core/src/websocketBackplane.ts +15 -3
- package/packages/orm/dist/index.js +1259 -925
- package/packages/orm/src/database.ts +7 -1
- package/packages/orm/src/databaseUrl.ts +73 -190
- package/packages/orm/src/docstore.ts +8 -1
- package/packages/orm/src/graph/graphUrl.ts +43 -7
- package/packages/orm/src/model.ts +2 -1
- package/types/core/src/batchSpawn.d.ts +28 -0
- package/types/core/src/connectionUrl.d.ts +61 -0
- package/types/core/src/fakeData.d.ts +11 -2
- package/types/core/src/index.d.ts +1 -1
- package/types/core/src/portTakeover.d.ts +15 -0
- package/types/core/src/projectIndexStorage.d.ts +5 -0
- package/types/core/src/queueBackends/rabbitmqBackend.d.ts +0 -7
- package/types/core/src/router.d.ts +17 -2
- package/types/core/src/server.d.ts +13 -0
- package/types/core/src/session.d.ts +5 -0
- package/types/core/src/sessionHandlers/databaseHandler.d.ts +8 -0
- package/types/core/src/urlRedaction.d.ts +61 -0
- package/types/orm/src/databaseUrl.d.ts +18 -19
- package/types/orm/src/graph/graphUrl.d.ts +18 -0
|
@@ -10,6 +10,7 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
10
10
|
import type { DatabaseAdapter, DatabaseResult as DatabaseWriteResult, ColumnInfo, FieldDefinition } from "./types.js";
|
|
11
11
|
import { DatabaseResult } from "./databaseResult.js";
|
|
12
12
|
import { DatabaseUrl } from "./databaseUrl.js";
|
|
13
|
+
import { canonicalConnectionUrl, splitConnectionUrl } from "../../core/src/connectionUrl.js";
|
|
13
14
|
import { CachedDatabaseAdapter, type CachedAdapterOptions } from "./cachedDatabase.js";
|
|
14
15
|
import { QueryCache, SQLTranslator } from "./sqlTranslator.js";
|
|
15
16
|
import { quoteIdentifierAnsi, assertColumnNames } from "./adapters/sqlDialect.js";
|
|
@@ -1841,7 +1842,12 @@ async function buildAdapterFromUrl(url: string, username?: string, password?: st
|
|
|
1841
1842
|
}
|
|
1842
1843
|
case "mongodb": {
|
|
1843
1844
|
const { MongodbAdapter } = await import("./adapters/mongodb.js");
|
|
1844
|
-
|
|
1845
|
+
// The driver runs its OWN URL parser and quotes what it could not parse
|
|
1846
|
+
// ("Unable to parse u:Pq1 with URL"), so it gets the credentials
|
|
1847
|
+
// percent-encoded - never a raw password holding / ? # @ (GHSA-h2mr-m3v3-cq8c).
|
|
1848
|
+
const adapter = new MongodbAdapter(
|
|
1849
|
+
canonicalConnectionUrl(splitConnectionUrl(url, { label: "DatabaseUrl", allowHostList: true })),
|
|
1850
|
+
);
|
|
1845
1851
|
await adapter.connect();
|
|
1846
1852
|
return adapter;
|
|
1847
1853
|
}
|
|
@@ -20,6 +20,8 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
20
20
|
* key, byte-identical in all four.
|
|
21
21
|
*/
|
|
22
22
|
import { inspect } from "node:util";
|
|
23
|
+
import { REDACTED, redactCredentials } from "../../core/src/urlRedaction.js";
|
|
24
|
+
import { ConnectionUrlError, splitConnectionUrl, type ConnectionUrlParts } from "../../core/src/connectionUrl.js";
|
|
23
25
|
|
|
24
26
|
/** The canonical engine names. Aliases resolve to these ONCE, at parse. */
|
|
25
27
|
export type DatabaseEngine =
|
|
@@ -76,171 +78,42 @@ function stripOneSlash(path: string): string {
|
|
|
76
78
|
return path.startsWith("/") ? path.slice(1) : path;
|
|
77
79
|
}
|
|
78
80
|
|
|
79
|
-
function decode(value: string | undefined): string | null {
|
|
80
|
-
if (value === undefined || value === "") return null;
|
|
81
|
-
return decodeURIComponent(value);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
81
|
// ── redaction ──────────────────────────────────────────────────────────────
|
|
85
82
|
// ONE primitive, used by every path that can put a connection string in front
|
|
86
|
-
// of a human.
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
/** The single mask. One spelling, so grepping for it finds every redaction. */
|
|
92
|
-
const REDACTED = "***";
|
|
83
|
+
// of a human. It lives in core's dependency-free urlRedaction.ts so that core
|
|
84
|
+
// (mqtt, the AMQP queue, the backplane, the boot log) and this package share
|
|
85
|
+
// the SAME rule rather than two drifting copies. Re-exported here because it
|
|
86
|
+
// has always been part of @tina4/orm's public surface.
|
|
87
|
+
export { redactCredentials };
|
|
93
88
|
|
|
94
89
|
/**
|
|
95
|
-
*
|
|
90
|
+
* The failure a malformed `TINA4_DATABASE_URL` raises.
|
|
96
91
|
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
* `;` for ODBC/libpq, `&` for a query string - plus the closing brace/quote of
|
|
101
|
-
* a quoted value, since `PWD={p;w}` and the libpq quoted form both legally
|
|
102
|
-
* contain a separator.
|
|
92
|
+
* The message NEVER carries the URL, and nothing it chains does either. It
|
|
93
|
+
* stays diagnosable: the scheme or engine is named, plus the host:port when it
|
|
94
|
+
* sits after the last `@` (the only place it cannot be part of a password).
|
|
103
95
|
*/
|
|
104
|
-
function
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
let i = start + 1;
|
|
112
|
-
while (i < text.length) {
|
|
113
|
-
if (text[i] === "\\") { i += 2; continue; }
|
|
114
|
-
if (text[i] === open) return i + 1;
|
|
115
|
-
i++;
|
|
116
|
-
}
|
|
117
|
-
return text.length;
|
|
118
|
-
}
|
|
119
|
-
let i = start;
|
|
120
|
-
while (i < text.length && text[i] !== ";" && text[i] !== "&") i++;
|
|
121
|
-
return i;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** `PWD=`/`password=`/`passwd=` in an ODBC DSN, a libpq DSN or a query string. */
|
|
125
|
-
const SECRET_KEYWORD_PATTERN = /(^|[;&?\s])(pwd|password|passwd)(\s*=\s*)/gi;
|
|
126
|
-
|
|
127
|
-
function redactKeywordValues(text: string): string {
|
|
128
|
-
const pattern = new RegExp(SECRET_KEYWORD_PATTERN.source, SECRET_KEYWORD_PATTERN.flags);
|
|
129
|
-
let out = "";
|
|
130
|
-
let cursor = 0;
|
|
131
|
-
let match: RegExpExecArray | null;
|
|
132
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
133
|
-
const valueStart = match.index + match[0].length;
|
|
134
|
-
out += text.slice(cursor, valueStart) + REDACTED;
|
|
135
|
-
cursor = endOfKeywordValue(text, valueStart);
|
|
136
|
-
pattern.lastIndex = cursor;
|
|
96
|
+
function invalidUrlError(raw: string, engine: DatabaseEngine, why: string): Error {
|
|
97
|
+
let hostPort: string | undefined;
|
|
98
|
+
try {
|
|
99
|
+
const parts = splitConnectionUrl(raw, { label: "DatabaseUrl" });
|
|
100
|
+
if (parts.userinfo !== null && parts.hostPort !== "") hostPort = parts.hostPort;
|
|
101
|
+
} catch {
|
|
102
|
+
// already refused: name nothing beyond the engine
|
|
137
103
|
}
|
|
138
|
-
return
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
* The authority of `scheme://user:pass@host:port/path`, or null when the string
|
|
143
|
-
* has no `://` at all. Everything the other helpers need is derived from here,
|
|
144
|
-
* so "where does userinfo end" is decided in exactly one place.
|
|
145
|
-
*/
|
|
146
|
-
function authorityOf(raw: string): string | null {
|
|
147
|
-
const separator = raw.indexOf("://");
|
|
148
|
-
if (separator === -1) return null;
|
|
149
|
-
const rest = raw.slice(separator + 3);
|
|
150
|
-
const end = rest.search(/[/?#]/);
|
|
151
|
-
return end === -1 ? rest : rest.slice(0, end);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* The raw, still-encoded userinfo, or null when the URL carries none.
|
|
156
|
-
*
|
|
157
|
-
* Read off the RAW string on purpose: `new URL()` normalises
|
|
158
|
-
* `postgres://user:@host/db` and `postgres://user@host/db` to the identical
|
|
159
|
-
* href, so the URL object cannot tell an explicitly-blank password from an
|
|
160
|
-
* absent one (measured on Node 24.9.0 - both report `.password === ""`).
|
|
161
|
-
*/
|
|
162
|
-
function rawUserinfo(raw: string): string | null {
|
|
163
|
-
const authority = authorityOf(raw);
|
|
164
|
-
if (authority === null) return null;
|
|
165
|
-
const at = authority.lastIndexOf("@");
|
|
166
|
-
return at === -1 ? null : authority.slice(0, at);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function redactUserinfoPassword(raw: string): string {
|
|
170
|
-
const authority = authorityOf(raw);
|
|
171
|
-
if (authority === null) return raw;
|
|
172
|
-
const at = authority.lastIndexOf("@");
|
|
173
|
-
if (at === -1) return raw;
|
|
174
|
-
const colon = authority.slice(0, at).indexOf(":");
|
|
175
|
-
if (colon === -1) return raw; // a username with no password
|
|
176
|
-
const authorityStart = raw.indexOf("://") + 3;
|
|
177
|
-
return (
|
|
178
|
-
raw.slice(0, authorityStart + colon + 1) + REDACTED + raw.slice(authorityStart + at)
|
|
104
|
+
return new ConnectionUrlError(
|
|
105
|
+
`DatabaseUrl: invalid ${engine} URL${hostPort ? ` at '${hostPort}'` : ""} (${why}) - expected ` +
|
|
106
|
+
"scheme://[user[:password]@]host[:port]/database. " +
|
|
107
|
+
"The URL itself is not shown because it may contain a password.",
|
|
179
108
|
);
|
|
180
109
|
}
|
|
181
110
|
|
|
182
|
-
/**
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
* second, weaker rule.
|
|
189
|
-
*
|
|
190
|
-
* It cannot be complete on a string with no recognisable credential structure
|
|
191
|
-
* (`notaurl-with-hunter2` has nothing to key off), which is exactly why the
|
|
192
|
-
* invalid-URL error reports the scheme and host instead of any form of the
|
|
193
|
-
* input. Redaction is for strings we can parse enough to redact.
|
|
194
|
-
*/
|
|
195
|
-
export function redactCredentials(raw: string): string {
|
|
196
|
-
if (typeof raw !== "string" || raw === "") return raw;
|
|
197
|
-
return redactKeywordValues(redactUserinfoPassword(raw));
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/** `postgres` from `postgres://…`, or null when the string names no scheme. */
|
|
201
|
-
function schemeOf(raw: string): string | null {
|
|
202
|
-
const match = raw.match(/^([a-zA-Z][a-zA-Z0-9+.-]*):/);
|
|
203
|
-
return match ? match[1].toLowerCase() : null;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* The `host:port` slice of the authority, or null.
|
|
208
|
-
*
|
|
209
|
-
* Credential-free BY CONSTRUCTION: it is the part AFTER the last `@`, and
|
|
210
|
-
* userinfo - the only place a password may appear in a URL - is entirely
|
|
211
|
-
* before it. That is what makes it safe to name in an error message.
|
|
212
|
-
*/
|
|
213
|
-
function hostPortOf(raw: string): string | null {
|
|
214
|
-
const authority = authorityOf(raw);
|
|
215
|
-
if (authority === null) return null;
|
|
216
|
-
const at = authority.lastIndexOf("@");
|
|
217
|
-
const hostPort = at === -1 ? authority : authority.slice(at + 1);
|
|
218
|
-
return hostPort === "" ? null : hostPort;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* The failure a malformed `TINA4_DATABASE_URL` raises.
|
|
223
|
-
*
|
|
224
|
-
* The message NEVER carries the URL. The old one interpolated it, so a typo in
|
|
225
|
-
* the port wrote the password into the boot log, the crash report, the error
|
|
226
|
-
* overlay and the CI log - measured: `TINA4_DATABASE_URL` of
|
|
227
|
-
* `postgres://user:SuperSecret123@host:notaport/db` produced
|
|
228
|
-
* `DatabaseUrl: invalid URL format 'postgres://user:SuperSecret123@host:notaport/db'`.
|
|
229
|
-
*
|
|
230
|
-
* It stays diagnosable: the scheme (or engine) and the host:port are both in
|
|
231
|
-
* the message, along with the shape that was expected. A redaction that leaves
|
|
232
|
-
* nothing to debug with is its own kind of bug.
|
|
233
|
-
*/
|
|
234
|
-
function invalidUrlError(raw: string, engine?: DatabaseEngine): Error {
|
|
235
|
-
const named = engine ?? schemeOf(raw);
|
|
236
|
-
const subject = named ? `invalid ${named} URL` : "invalid URL (no scheme found)";
|
|
237
|
-
const hostPort = hostPortOf(raw);
|
|
238
|
-
const at = hostPort === null ? "" : ` at '${hostPort}'`;
|
|
239
|
-
return new Error(
|
|
240
|
-
`DatabaseUrl: ${subject}${at} - expected ` +
|
|
241
|
-
"scheme://[user[:password]@]host[:port]/database. " +
|
|
242
|
-
"The URL itself is not shown because it may contain a password."
|
|
243
|
-
);
|
|
111
|
+
/** Split under the shared rule; the refusal names the scheme, never the value. */
|
|
112
|
+
function split(url: string): ConnectionUrlParts {
|
|
113
|
+
return splitConnectionUrl(url, {
|
|
114
|
+
label: "DatabaseUrl",
|
|
115
|
+
expected: "scheme://[user[:password]@]host[:port]/database",
|
|
116
|
+
});
|
|
244
117
|
}
|
|
245
118
|
|
|
246
119
|
/**
|
|
@@ -388,13 +261,14 @@ export class DatabaseUrl {
|
|
|
388
261
|
if (url.startsWith("odbc:///")) {
|
|
389
262
|
return { engine: "odbc", connectionString: url.slice("odbc:///".length) };
|
|
390
263
|
}
|
|
391
|
-
|
|
392
|
-
|
|
264
|
+
const parts = split(url);
|
|
265
|
+
if (parts.scheme === "mssql" || parts.scheme === "sqlserver") {
|
|
266
|
+
return DatabaseUrl.parsePathForm(url, parts, "mssql");
|
|
393
267
|
}
|
|
394
|
-
if (
|
|
395
|
-
return DatabaseUrl.
|
|
268
|
+
if (parts.scheme === "firebird") {
|
|
269
|
+
return DatabaseUrl.parsePathForm(url, parts, "firebird");
|
|
396
270
|
}
|
|
397
|
-
return DatabaseUrl.parseStandard(url);
|
|
271
|
+
return DatabaseUrl.parseStandard(url, parts);
|
|
398
272
|
}
|
|
399
273
|
|
|
400
274
|
/**
|
|
@@ -422,60 +296,69 @@ export class DatabaseUrl {
|
|
|
422
296
|
}
|
|
423
297
|
|
|
424
298
|
/**
|
|
425
|
-
* mssql and firebird:
|
|
426
|
-
* matched directly.
|
|
299
|
+
* mssql and firebird: `host[:port]/database`, split by the shared splitter.
|
|
427
300
|
*
|
|
428
|
-
*
|
|
301
|
+
* These used to be one regex each that split userinfo at the FIRST `@` and
|
|
302
|
+
* the password at the first `/`, so `mssql://u:p@ss@h/db` connected to a host
|
|
303
|
+
* named `ss@h` and printed it in the driver's error.
|
|
304
|
+
*
|
|
305
|
+
* The database keeps its own leading slash when the URL had two, which is
|
|
429
306
|
* how the documented absolute Firebird form survives. The old code did
|
|
430
307
|
* `"/" + match[5]`, ADDING a slash - so an absolute path came back with two
|
|
431
308
|
* and a relative path was silently made absolute. Verified against live
|
|
432
309
|
* Firebird 5.0.4: the driver takes one or two leading slashes and rejects a
|
|
433
310
|
* relative path outright.
|
|
434
311
|
*/
|
|
435
|
-
private static
|
|
436
|
-
|
|
437
|
-
|
|
312
|
+
private static parsePathForm(url: string, parts: ConnectionUrlParts, engine: DatabaseEngine): ParsedParts {
|
|
313
|
+
if (parts.host === "" || /[:/]/.test(parts.host) || parts.hostPort.startsWith("[")) {
|
|
314
|
+
throw invalidUrlError(url, engine, "a host is required");
|
|
315
|
+
}
|
|
316
|
+
if (!parts.tail.startsWith("/")) throw invalidUrlError(url, engine, "a /database is required");
|
|
438
317
|
return {
|
|
439
318
|
engine,
|
|
440
|
-
username:
|
|
441
|
-
password:
|
|
442
|
-
host:
|
|
443
|
-
port:
|
|
444
|
-
database:
|
|
319
|
+
username: parts.username === "" ? null : parts.username,
|
|
320
|
+
password: parts.password,
|
|
321
|
+
host: parts.host,
|
|
322
|
+
port: parts.port ?? undefined,
|
|
323
|
+
database: parts.tail.slice(1),
|
|
445
324
|
};
|
|
446
325
|
}
|
|
447
326
|
|
|
448
|
-
/**
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
327
|
+
/**
|
|
328
|
+
* postgres / mysql / mongodb.
|
|
329
|
+
*
|
|
330
|
+
* Userinfo comes from the shared splitter (the last-`@` rule), never from the
|
|
331
|
+
* URL class: `new URL()` cuts a password at its first `/`, `?` or `#`, reads
|
|
332
|
+
* `postgres:/u:pw@h` as a path, and throws a TypeError whose `.input` is the
|
|
333
|
+
* whole URL. The class only sees a credential-free rebuild, for the host,
|
|
334
|
+
* port and path it already normalises the way every corpus row expects.
|
|
335
|
+
*/
|
|
336
|
+
private static parseStandard(url: string, parts: ConnectionUrlParts): ParsedParts {
|
|
337
|
+
const engine = ENGINE_ALIASES[parts.scheme];
|
|
458
338
|
if (engine === undefined) {
|
|
459
339
|
throw new Error(
|
|
460
|
-
`DatabaseUrl: Unsupported database scheme '${scheme}'. Supported: ${Object.keys(ENGINE_ALIASES).join(", ")}`
|
|
340
|
+
`DatabaseUrl: Unsupported database scheme '${parts.scheme}'. Supported: ${Object.keys(ENGINE_ALIASES).join(", ")}`
|
|
461
341
|
);
|
|
462
342
|
}
|
|
343
|
+
let parsed: URL;
|
|
344
|
+
try {
|
|
345
|
+
parsed = new URL(`${parts.scheme}://${parts.hostPort}${parts.tail}`);
|
|
346
|
+
} catch {
|
|
347
|
+
// No `cause`: the TypeError carries its input.
|
|
348
|
+
throw invalidUrlError(url, engine, "the host, port or path is malformed");
|
|
349
|
+
}
|
|
463
350
|
const database = stripOneSlash(parsed.pathname);
|
|
464
351
|
// A password that was WRITTEN but left blank (`postgres://user:@host/db`)
|
|
465
352
|
// is an explicitly-empty password, NOT an absent one, so the
|
|
466
353
|
// TINA4_DATABASE_PASSWORD fallback in the constructor must not fire for it.
|
|
467
|
-
//
|
|
468
|
-
//
|
|
469
|
-
// framework read it. The URL object cannot tell the two apart (it
|
|
470
|
-
// normalises both to `.password === ""`), so the RAW userinfo decides.
|
|
471
|
-
const userinfo = rawUserinfo(url);
|
|
472
|
-
const passwordWasWritten = userinfo !== null && userinfo.includes(":");
|
|
354
|
+
// The splitter reads the RAW userinfo, so it tells the two apart (`""`
|
|
355
|
+
// versus null) where the URL class normalised both to `.password === ""`.
|
|
473
356
|
return {
|
|
474
357
|
engine,
|
|
475
358
|
host: parsed.hostname || undefined,
|
|
476
359
|
port: parsed.port ? parseInt(parsed.port, 10) : undefined,
|
|
477
|
-
username:
|
|
478
|
-
password:
|
|
360
|
+
username: parts.username === "" ? null : parts.username,
|
|
361
|
+
password: parts.password,
|
|
479
362
|
database: engine === "mongodb" ? database || "tina4" : database,
|
|
480
363
|
};
|
|
481
364
|
}
|
|
@@ -58,6 +58,7 @@ import { DatabaseSync } from "node:sqlite";
|
|
|
58
58
|
import { randomBytes } from "node:crypto";
|
|
59
59
|
import { mkdirSync } from "node:fs";
|
|
60
60
|
import { dirname, isAbsolute, join } from "node:path";
|
|
61
|
+
import { canonicalConnectionUrl, splitConnectionUrl } from "../../core/src/connectionUrl.js";
|
|
61
62
|
|
|
62
63
|
// ── ObjectId: zero-dependency 12-byte / 24-hex id ──────────────────────────
|
|
63
64
|
|
|
@@ -1061,7 +1062,13 @@ function mongoConnection(uri: string, dbName: string): Promise<{ client: any; db
|
|
|
1061
1062
|
if (!pending) {
|
|
1062
1063
|
pending = (async () => {
|
|
1063
1064
|
const { MongoClient } = await importMongoDriver();
|
|
1064
|
-
|
|
1065
|
+
// The driver's own parser quotes what it cannot parse ("Protocol and host
|
|
1066
|
+
// list are required in \"mongodb://u:pw@/db\""), so it only ever sees a
|
|
1067
|
+
// URI the shared rule split and re-encoded; anything else is refused
|
|
1068
|
+
// without echoing it (GHSA-h2mr-m3v3-cq8c).
|
|
1069
|
+
const client = new MongoClient(
|
|
1070
|
+
canonicalConnectionUrl(splitConnectionUrl(uri, { label: "DocStore", allowHostList: true })),
|
|
1071
|
+
);
|
|
1065
1072
|
await client.connect();
|
|
1066
1073
|
return { client, db: client.db(dbName) };
|
|
1067
1074
|
})();
|
|
@@ -14,7 +14,10 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
14
14
|
* alias (bolt/neo4j/memgraph all speak Bolt/Cypher and share ONE adapter) resolves
|
|
15
15
|
* to its engine here. See tina4-documentation/plan/v3/features/139-graph-databases.md.
|
|
16
16
|
*/
|
|
17
|
-
import {
|
|
17
|
+
import { inspect } from "node:util";
|
|
18
|
+
|
|
19
|
+
import { redactCredentials } from "../../../core/src/urlRedaction.js";
|
|
20
|
+
import { ConnectionUrlError, splitConnectionUrl } from "../../../core/src/connectionUrl.js";
|
|
18
21
|
|
|
19
22
|
/** The canonical graph engine names. */
|
|
20
23
|
export type GraphEngine = "ultipa" | "bolt" | "arango";
|
|
@@ -59,13 +62,20 @@ export class GraphUrl {
|
|
|
59
62
|
|
|
60
63
|
constructor(url: string) {
|
|
61
64
|
this.raw = url;
|
|
65
|
+
// Userinfo by the shared last-`@` rule; the URL class only ever sees the
|
|
66
|
+
// credential-free rest. It used to parse the whole value, so a password
|
|
67
|
+
// holding `/`, `?` or `#` became the host/port/graph (and then the
|
|
68
|
+
// "connect to <host>" error), and the refusal quoted the URL back.
|
|
69
|
+
const expected = "scheme://[user[:password]@]host[:port]/graph (e.g. ultipa://host:60061/mygraph)";
|
|
70
|
+
const parts = splitConnectionUrl(url, { label: "GraphUrl", expected });
|
|
62
71
|
let parsed: URL;
|
|
63
72
|
try {
|
|
64
|
-
parsed = new URL(
|
|
73
|
+
parsed = new URL(`${parts.scheme}://${parts.hostPort}${parts.tail}`);
|
|
65
74
|
} catch {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
75
|
+
// No `cause`: the TypeError carries its input.
|
|
76
|
+
throw new ConnectionUrlError(
|
|
77
|
+
`GraphUrl: invalid ${parts.scheme} URL - expected ${expected}. `
|
|
78
|
+
+ "The URL itself is not shown because it may contain a password.",
|
|
69
79
|
);
|
|
70
80
|
}
|
|
71
81
|
const scheme = parsed.protocol.replace(/:$/, "").toLowerCase();
|
|
@@ -83,8 +93,8 @@ export class GraphUrl {
|
|
|
83
93
|
this.port = parsed.port ? parseInt(parsed.port, 10) : ENGINE_DEFAULT_PORT[engine];
|
|
84
94
|
const path = (parsed.pathname || "").replace(/^\//, "");
|
|
85
95
|
this.graph = path === "" ? null : path;
|
|
86
|
-
this.username =
|
|
87
|
-
this.password =
|
|
96
|
+
this.username = parts.username ? parts.username : null;
|
|
97
|
+
this.password = parts.password ? parts.password : null;
|
|
88
98
|
this.params = {};
|
|
89
99
|
for (const [key, value] of parsed.searchParams) {
|
|
90
100
|
// First value wins, matching Python's parse_qs[0].
|
|
@@ -94,6 +104,32 @@ export class GraphUrl {
|
|
|
94
104
|
this.useTls = scheme.endsWith("s") || this.params.tls === "1" || this.params.tls === "true";
|
|
95
105
|
}
|
|
96
106
|
|
|
107
|
+
/**
|
|
108
|
+
* What `JSON.stringify` emits, directly or as one field of a logged object.
|
|
109
|
+
* `password` and `raw` both hold the secret, so the default dump printed it
|
|
110
|
+
* twice (GHSA-h2mr-m3v3-cq8c). DatabaseUrl guards the same way; PHP has
|
|
111
|
+
* __debugInfo/jsonSerialize and Ruby a redacted #inspect.
|
|
112
|
+
*/
|
|
113
|
+
toJSON(): Record<string, unknown> {
|
|
114
|
+
return {
|
|
115
|
+
raw: redactCredentials(this.raw),
|
|
116
|
+
scheme: this.scheme,
|
|
117
|
+
engine: this.engine,
|
|
118
|
+
host: this.host,
|
|
119
|
+
port: this.port,
|
|
120
|
+
graph: this.graph,
|
|
121
|
+
username: this.username,
|
|
122
|
+
password: this.password === null ? null : "***",
|
|
123
|
+
params: this.params,
|
|
124
|
+
useTls: this.useTls,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** What `console.log(url)` / `util.inspect(url)` print. */
|
|
129
|
+
[inspect.custom](): string {
|
|
130
|
+
return `GraphUrl('${redactCredentials(this.raw)}')`;
|
|
131
|
+
}
|
|
132
|
+
|
|
97
133
|
/** host:port/graph — for messages, never carrying credentials. */
|
|
98
134
|
getDsn(): string {
|
|
99
135
|
const target = this.port ? `${this.host}:${this.port}` : this.host;
|
|
@@ -8,6 +8,7 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
8
8
|
|
|
9
9
|
import { readdirSync, statSync } from "node:fs";
|
|
10
10
|
import { join, extname } from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
11
12
|
import type { ModelDefinition, FieldDefinition, RelationshipDefinition } from "./types.js";
|
|
12
13
|
|
|
13
14
|
export interface DiscoveredModel {
|
|
@@ -35,7 +36,7 @@ export async function discoverModels(modelsDir: string): Promise<DiscoveredModel
|
|
|
35
36
|
if (ext !== ".ts" && ext !== ".js") continue;
|
|
36
37
|
|
|
37
38
|
try {
|
|
38
|
-
const moduleUrl =
|
|
39
|
+
const moduleUrl = `${pathToFileURL(filePath).href}?t=${Date.now()}`;
|
|
39
40
|
const mod = await import(moduleUrl);
|
|
40
41
|
const ModelClass = mod.default ?? mod;
|
|
41
42
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SpawnSyncOptions, SpawnSyncReturns } from "node:child_process";
|
|
2
|
+
/** True when `file` is a batch file that Node will not spawn without a shell. */
|
|
3
|
+
export declare function isBatchFile(file: string, platform?: NodeJS.Platform): boolean;
|
|
4
|
+
/** One argument, quoted and escaped for a batch file run through `cmd /d /s /c`. */
|
|
5
|
+
export declare function escapeBatchArgument(arg: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Resolve a bare program name such as `npm` to the file Windows would run.
|
|
8
|
+
*
|
|
9
|
+
* Without a shell, Windows' CreateProcess only adds `.exe`, so `npm` (which is
|
|
10
|
+
* `npm.cmd`) is never found and the spawn fails with ENOENT. This walks PATH
|
|
11
|
+
* with PATHEXT the way cmd.exe does. A name that already has a directory or an
|
|
12
|
+
* extension, every other platform, and a name that is not found are returned
|
|
13
|
+
* unchanged, so the spawn reports its own error exactly as before.
|
|
14
|
+
*/
|
|
15
|
+
export declare function findExecutable(name: string, platform?: NodeJS.Platform): string;
|
|
16
|
+
/**
|
|
17
|
+
* `spawnSync`, except that a batch file on Windows runs through cmd.exe with
|
|
18
|
+
* every argument escaped. Returns what spawnSync returns either way.
|
|
19
|
+
*/
|
|
20
|
+
export declare function spawnSyncAllowingBatch(file: string, args: readonly string[], options?: SpawnSyncOptions): SpawnSyncReturns<Buffer | string>;
|
|
21
|
+
/**
|
|
22
|
+
* `execFileSync` for a bare program name (`npm`, `npx`) that may be a batch
|
|
23
|
+
* file: resolves it with {@link findExecutable}, runs it with
|
|
24
|
+
* {@link spawnSyncAllowingBatch}, and like execFileSync returns stdout and
|
|
25
|
+
* throws on a spawn error or a non-zero exit (the error carries `status`,
|
|
26
|
+
* `stdout` and `stderr`).
|
|
27
|
+
*/
|
|
28
|
+
export declare function execFileSyncAllowingBatch(name: string, args: readonly string[], options?: SpawnSyncOptions): string;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error every parser throws for a connection URL it will not accept.
|
|
3
|
+
*
|
|
4
|
+
* The message NEVER carries the value and the error NEVER has a `cause`: a
|
|
5
|
+
* TypeError from `new URL()` keeps the input on `.input`, and a driver's parse
|
|
6
|
+
* error quotes it, so chaining either would put the password straight back.
|
|
7
|
+
*/
|
|
8
|
+
export declare class ConnectionUrlError extends Error {
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
11
|
+
export interface ConnectionUrlParts {
|
|
12
|
+
/** Lower-cased scheme, e.g. `postgres`, `mongodb+srv`. */
|
|
13
|
+
scheme: string;
|
|
14
|
+
/** Raw (still-encoded) userinfo, or null when the URL has no `@`. */
|
|
15
|
+
userinfo: string | null;
|
|
16
|
+
/** Raw (still-encoded) user, or null. */
|
|
17
|
+
rawUsername: string | null;
|
|
18
|
+
/** Raw (still-encoded) password, or null when no `:` was written. */
|
|
19
|
+
rawPassword: string | null;
|
|
20
|
+
/** Decoded user, or null. */
|
|
21
|
+
username: string | null;
|
|
22
|
+
/** Decoded password, or null when no `:` was written ("" when written blank). */
|
|
23
|
+
password: string | null;
|
|
24
|
+
/** The raw `host[:port]` (or host list) after userinfo - credential-free. */
|
|
25
|
+
hostPort: string;
|
|
26
|
+
/** First host, brackets removed for IPv6, or "" when there is none. */
|
|
27
|
+
host: string;
|
|
28
|
+
/** First host's port, or null when none was written. */
|
|
29
|
+
port: number | null;
|
|
30
|
+
/** Path, query and fragment as written: "" or starting with `/`, `?` or `#`. */
|
|
31
|
+
tail: string;
|
|
32
|
+
}
|
|
33
|
+
export interface SplitOptions {
|
|
34
|
+
/** Prefix for the error message, e.g. "DatabaseUrl". */
|
|
35
|
+
label: string;
|
|
36
|
+
/** What the message says was expected, e.g. "scheme://[user[:password]@]host[:port]/database". */
|
|
37
|
+
expected?: string;
|
|
38
|
+
/** Accept `h1:1,h2:2` (MongoDB). */
|
|
39
|
+
allowHostList?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** Percent-decode, leaving an invalid escape as written instead of throwing. */
|
|
42
|
+
export declare function decodeComponent(value: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Split `scheme://[user[:password]@]host[:port][/path][?query][#fragment]`
|
|
45
|
+
* under THE RULE (see the header), refusing what it cannot split safely.
|
|
46
|
+
*
|
|
47
|
+
* Accepts an unencoded `/`, `?`, `#`, `@`, `:` or `%` in a password, which is
|
|
48
|
+
* what people actually paste into a .env file. Refuses, with a message that
|
|
49
|
+
* never echoes the value:
|
|
50
|
+
* - no `scheme://` (including `scheme:/` and `scheme//`)
|
|
51
|
+
* - an `@` outside userinfo (`?email=a@b` - encode it as `%40`)
|
|
52
|
+
* - userinfo with no host after it, a non-numeric port, a bad IPv6 bracket,
|
|
53
|
+
* or whitespace in the host
|
|
54
|
+
*/
|
|
55
|
+
export declare function splitConnectionUrl(raw: string, options: SplitOptions): ConnectionUrlParts;
|
|
56
|
+
/**
|
|
57
|
+
* The same URL with its credentials percent-encoded - what a driver that runs
|
|
58
|
+
* its own URL parser (mongodb, redis) is handed, so it can never re-split a
|
|
59
|
+
* password holding `/`, `?`, `#` or `@` and quote the pieces in its errors.
|
|
60
|
+
*/
|
|
61
|
+
export declare function canonicalConnectionUrl(parts: ConnectionUrlParts): string;
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
export declare class SeedError extends Error {
|
|
2
|
+
readonly seedFile: string;
|
|
3
|
+
readonly completed: string[];
|
|
4
|
+
constructor(seedFile: string, completed: string[], cause: unknown);
|
|
5
|
+
}
|
|
1
6
|
export declare const FIRST_NAMES: string[];
|
|
2
7
|
export declare const LAST_NAMES: string[];
|
|
3
8
|
export declare const PRODUCT_ADJECTIVES: string[];
|
|
@@ -56,8 +61,12 @@ export declare class FakeData {
|
|
|
56
61
|
*/
|
|
57
62
|
choice<T>(items: T[]): T;
|
|
58
63
|
/**
|
|
59
|
-
* Run seed files
|
|
60
|
-
*
|
|
64
|
+
* Run the seed files in a directory in name order, skipping files whose name
|
|
65
|
+
* starts with "_". A file's default export, or else the named run() that
|
|
66
|
+
* `generate seeder` writes, is awaited when it is a function.
|
|
67
|
+
* Returns the executed file paths in order ([] when the directory is missing).
|
|
68
|
+
* A seed file that throws stops the run: it is logged and a SeedError naming
|
|
69
|
+
* it is thrown, and no later file runs (ADR-0108).
|
|
61
70
|
*/
|
|
62
71
|
seedDir(seedDir?: string): Promise<string[]>;
|
|
63
72
|
/**
|
|
@@ -25,7 +25,7 @@ export { getToken, validToken, getPayload, hashPassword, checkPassword, authMidd
|
|
|
25
25
|
export { Session, FileSessionHandler, RedisSessionHandler, buildSessionCookie, isSecureScheme, isValidSessionId, sessionCookieName, VALID_SESSION_BACKENDS, CANONICAL_SESSION_BACKENDS } from "./session.js";
|
|
26
26
|
export type { SessionConfig, SessionHandler } from "./session.js";
|
|
27
27
|
export { I18n } from "./i18n.js";
|
|
28
|
-
export { FakeData } from "./fakeData.js";
|
|
28
|
+
export { FakeData, SeedError } from "./fakeData.js";
|
|
29
29
|
export { Queue } from "./queue.js";
|
|
30
30
|
export type { QueueConfig, QueueJob, ProcessOptions } from "./queue.js";
|
|
31
31
|
export { createJob } from "./job.js";
|
|
@@ -41,6 +41,21 @@ export declare function writePidfile(port: number, baseDir?: string, pid?: numbe
|
|
|
41
41
|
export declare function readPidfile(port: number, baseDir?: string): number | null;
|
|
42
42
|
/** Drop the PID file for `port` (clean shutdown, or after reclaiming it). */
|
|
43
43
|
export declare function removePidfile(port: number, baseDir?: string): void;
|
|
44
|
+
/** What a bind probe learned about a port. */
|
|
45
|
+
export type BindProbe = "free" | "held" | "unknown";
|
|
46
|
+
/**
|
|
47
|
+
* Try a plain TCP listener on `host`:`port` (the same net.listen the server
|
|
48
|
+
* uses) and report what happened.
|
|
49
|
+
*
|
|
50
|
+
* Only a bind that fails with EADDRINUSE is "held". A probe that times out,
|
|
51
|
+
* cannot start, or hits any other bind error (EACCES, EADDRNOTAVAIL) is
|
|
52
|
+
* "unknown": it says nothing about whether something holds the port, so the
|
|
53
|
+
* caller must not report the port as in use. The real bind then decides, and
|
|
54
|
+
* it fails loudly with the true error. A probe is a child `node`, so on a slow
|
|
55
|
+
* host (arm64 under emulation, a small board, a CPU-throttled container) the
|
|
56
|
+
* child alone can take longer than the timeout to start.
|
|
57
|
+
*/
|
|
58
|
+
export declare function probeBind(host: string, port: number, timeout?: number): BindProbe;
|
|
44
59
|
/**
|
|
45
60
|
* Reclaim `port` ONLY from an identity-confirmed Tina4 dev server. The single
|
|
46
61
|
* guarded path for both the CLI (`tina4 serve`) and the runtime bind-failure
|
|
@@ -4,6 +4,11 @@ export interface IndexData {
|
|
|
4
4
|
files: Record<string, FileEntry>;
|
|
5
5
|
generated_at: number;
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* The index key for a file: its path relative to `root`, always `/`-separated
|
|
9
|
+
* so the same file has the same key on every OS (PHP's rule).
|
|
10
|
+
*/
|
|
11
|
+
export declare function indexKey(root: string, fullPath: string): string;
|
|
7
12
|
export declare function projectRoot(): string;
|
|
8
13
|
export declare function indexPath(): string;
|
|
9
14
|
export declare function summarise(entry: FileEntry): string;
|