tina4-nodejs 3.13.94 → 3.13.96

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/CLAUDE.md +158 -30
  2. package/README.md +1 -1
  3. package/package.json +3 -1
  4. package/packages/cli/dist/bin.js +30911 -28444
  5. package/packages/cli/src/commands/metrics.ts +17 -11
  6. package/packages/cli/src/commands/serve.ts +10 -9
  7. package/packages/core/dist/index.js +30810 -28261
  8. package/packages/core/public/css/tina4.min.css +1 -1
  9. package/packages/core/src/ai.ts +7 -1
  10. package/packages/core/src/auth.ts +191 -39
  11. package/packages/core/src/background.ts +19 -19
  12. package/packages/core/src/cache.ts +492 -49
  13. package/packages/core/src/devAdmin.ts +79 -32
  14. package/packages/core/src/dispatchPipeline.ts +285 -0
  15. package/packages/core/src/dotenv.ts +185 -40
  16. package/packages/core/src/index.ts +6 -7
  17. package/packages/core/src/logger.ts +257 -36
  18. package/packages/core/src/mcp.ts +1 -1
  19. package/packages/core/src/messenger.ts +294 -106
  20. package/packages/core/src/metrics.ts +199 -961
  21. package/packages/core/src/middleware.ts +390 -123
  22. package/packages/core/src/queue.ts +188 -32
  23. package/packages/core/src/queueBackends/kafkaBackend.ts +1 -1
  24. package/packages/core/src/queueBackends/liteBackend.ts +13 -0
  25. package/packages/core/src/queueBackends/mongoBackend.ts +101 -9
  26. package/packages/core/src/queueBackends/rabbitmqBackend.ts +22 -4
  27. package/packages/core/src/rateLimiter.ts +10 -5
  28. package/packages/core/src/request.ts +34 -16
  29. package/packages/core/src/response.ts +46 -1
  30. package/packages/core/src/router.ts +29 -4
  31. package/packages/core/src/server.ts +886 -421
  32. package/packages/core/src/session.ts +244 -27
  33. package/packages/core/src/sessionHandlers/databaseHandler.ts +338 -48
  34. package/packages/core/src/sessionHandlers/memcachedHandler.ts +181 -0
  35. package/packages/core/src/sessionHandlers/mongoClient.ts +293 -208
  36. package/packages/core/src/sessionHandlers/mongoHandler.ts +88 -8
  37. package/packages/core/src/sessionHandlers/respClient.ts +16 -147
  38. package/packages/core/src/sessionHandlers/sqlClient.ts +290 -0
  39. package/packages/core/src/sessionHandlers/syncBridge.ts +190 -0
  40. package/packages/core/src/sessionHandlers/syncSocket.ts +236 -0
  41. package/packages/core/src/testClient.ts +18 -5
  42. package/packages/core/src/trustedProxy.ts +249 -0
  43. package/packages/core/src/types.ts +29 -5
  44. package/packages/core/src/websocket.ts +66 -0
  45. package/packages/orm/dist/index.js +22717 -20168
  46. package/packages/orm/src/adapters/firebird.ts +183 -56
  47. package/packages/orm/src/adapters/mongodb.ts +25 -4
  48. package/packages/orm/src/adapters/mssql.ts +114 -29
  49. package/packages/orm/src/adapters/mysql.ts +103 -40
  50. package/packages/orm/src/adapters/odbc.ts +44 -21
  51. package/packages/orm/src/adapters/postgres.ts +118 -26
  52. package/packages/orm/src/adapters/sqlDialect.ts +120 -0
  53. package/packages/orm/src/adapters/sqlite.ts +60 -24
  54. package/packages/orm/src/autoCrud.ts +12 -10
  55. package/packages/orm/src/baseModel.ts +135 -40
  56. package/packages/orm/src/cachedDatabase.ts +43 -19
  57. package/packages/orm/src/connectTimeout.ts +265 -0
  58. package/packages/orm/src/database.ts +241 -197
  59. package/packages/orm/src/databaseResult.ts +51 -28
  60. package/packages/orm/src/databaseUrl.ts +484 -0
  61. package/packages/orm/src/docstore.ts +386 -145
  62. package/packages/orm/src/index.ts +13 -6
  63. package/packages/orm/src/migration.ts +44 -11
  64. package/packages/orm/src/model.ts +4 -0
  65. package/packages/orm/src/queryBuilder.ts +47 -6
  66. package/packages/orm/src/sqlTranslator.ts +310 -4
  67. package/packages/orm/src/types.ts +21 -77
  68. package/packages/swagger/dist/index.js +78 -20
  69. package/packages/swagger/src/generator.ts +172 -29
  70. package/types/core/src/ai.d.ts +1 -1
  71. package/types/core/src/auth.d.ts +28 -5
  72. package/types/core/src/background.d.ts +3 -3
  73. package/types/core/src/cache.d.ts +15 -12
  74. package/types/core/src/dispatchPipeline.d.ts +117 -0
  75. package/types/core/src/dotenv.d.ts +38 -16
  76. package/types/core/src/index.d.ts +6 -9
  77. package/types/core/src/logger.d.ts +93 -16
  78. package/types/core/src/messenger.d.ts +47 -6
  79. package/types/core/src/metrics.d.ts +25 -61
  80. package/types/core/src/middleware.d.ts +134 -11
  81. package/types/core/src/queue.d.ts +54 -5
  82. package/types/core/src/queueBackends/kafkaBackend.d.ts +1 -1
  83. package/types/core/src/queueBackends/liteBackend.d.ts +9 -0
  84. package/types/core/src/queueBackends/mongoBackend.d.ts +24 -2
  85. package/types/core/src/queueBackends/rabbitmqBackend.d.ts +3 -3
  86. package/types/core/src/router.d.ts +14 -3
  87. package/types/core/src/server.d.ts +15 -4
  88. package/types/core/src/session.d.ts +87 -2
  89. package/types/core/src/sessionHandlers/databaseHandler.d.ts +60 -5
  90. package/types/core/src/sessionHandlers/memcachedHandler.d.ts +60 -0
  91. package/types/core/src/sessionHandlers/mongoClient.d.ts +16 -5
  92. package/types/core/src/sessionHandlers/mongoHandler.d.ts +51 -3
  93. package/types/core/src/sessionHandlers/respClient.d.ts +2 -2
  94. package/types/core/src/sessionHandlers/sqlClient.d.ts +39 -0
  95. package/types/core/src/sessionHandlers/syncBridge.d.ts +91 -0
  96. package/types/core/src/sessionHandlers/syncSocket.d.ts +49 -0
  97. package/types/core/src/trustedProxy.d.ts +44 -0
  98. package/types/core/src/types.d.ts +28 -5
  99. package/types/core/src/websocket.d.ts +26 -0
  100. package/types/orm/src/adapters/firebird.d.ts +55 -10
  101. package/types/orm/src/adapters/mongodb.d.ts +2 -2
  102. package/types/orm/src/adapters/mssql.d.ts +18 -11
  103. package/types/orm/src/adapters/mysql.d.ts +11 -10
  104. package/types/orm/src/adapters/odbc.d.ts +9 -12
  105. package/types/orm/src/adapters/postgres.d.ts +11 -10
  106. package/types/orm/src/adapters/sqlDialect.d.ts +71 -0
  107. package/types/orm/src/adapters/sqlite.d.ts +15 -3
  108. package/types/orm/src/baseModel.d.ts +45 -9
  109. package/types/orm/src/cachedDatabase.d.ts +18 -5
  110. package/types/orm/src/connectTimeout.d.ts +100 -0
  111. package/types/orm/src/database.d.ts +78 -28
  112. package/types/orm/src/databaseResult.d.ts +29 -15
  113. package/types/orm/src/databaseUrl.d.ts +125 -0
  114. package/types/orm/src/docstore.d.ts +102 -43
  115. package/types/orm/src/index.d.ts +6 -4
  116. package/types/orm/src/migration.d.ts +4 -3
  117. package/types/orm/src/queryBuilder.d.ts +23 -3
  118. package/types/orm/src/sqlTranslator.d.ts +126 -2
  119. package/types/orm/src/types.d.ts +21 -38
  120. package/packages/core/src/scss.ts +0 -623
  121. package/packages/core/src/sessionHandlers/redisHandler.ts +0 -219
  122. package/types/core/src/scss.d.ts +0 -19
  123. package/types/core/src/sessionHandlers/redisHandler.d.ts +0 -60
@@ -97,47 +97,70 @@ export class DatabaseResult implements Iterable<Record<string, unknown>> {
97
97
  return this.records;
98
98
  }
99
99
 
100
- /** Pagination envelope — accepts either (page, perPage) or (offset, limit) style.
100
+ /**
101
+ * Describe the page this result IS — the canonical pagination envelope.
102
+ *
103
+ * Takes NO arguments and derives every field from the query that produced this
104
+ * result (ADR-0043). Passing ANY argument RAISES: a DatabaseResult holds no
105
+ * connection, so an argument could only re-slice the rows already in memory and
106
+ * then report total_pages for pages it can never reach. To read page N, FETCH
107
+ * page N (limit + offset) and call this with no arguments.
108
+ *
109
+ * The envelope is EXACTLY seven snake_case keys, identical across all four
110
+ * frameworks: `records, total, page, per_page, total_pages, limit, offset`.
111
+ *
112
+ * per_page = the query's limit
113
+ * page = floor(offset / limit) + 1
114
+ * total = the TRUE total for the filter — Database.fetch (and
115
+ * QueryBuilder.get) run a COUNT probe whenever a limit was
116
+ * applied — NEVER the number of rows returned
117
+ * total_pages = ceil(total / per_page)
118
+ * records = the rows the query returned, VERBATIM (never re-sliced)
119
+ * limit = the SQL limit actually applied
120
+ * offset = the SQL offset actually applied
101
121
  *
102
- * When called with two arguments both >= 0 and the first >= the second
103
- * (i.e. offset-style), pass `{ offset, limit }` as the first argument.
104
- * The simplest way is to always use the default (page, perPage) form and
105
- * let the autoCRUD layer supply offset/limit from the query string.
122
+ * The JSON payload is snake_case even though the method name is camelCase a
123
+ * JSON key is data, not a language surface (ADR-0043). The old duplicate and
124
+ * camelCase keys (`data`, `count`, `perPage`, `totalPages`, `has_next`,
125
+ * `has_prev`) are removed: Node emitted 13 keys, the worst offender of the four.
106
126
  *
107
- * Returns a superset of keys for backwards-compatibility across all clients.
127
+ * @throws {TypeError} if called with any argument.
108
128
  */
109
- toPaginate(page = 1, perPage = 10): {
129
+ toPaginate(): {
110
130
  records: Record<string, unknown>[];
111
- data: Record<string, unknown>[];
112
- count: number;
113
131
  total: number;
114
- limit: number;
115
- offset: number;
116
132
  page: number;
117
133
  per_page: number;
118
- perPage: number;
119
- totalPages: number;
120
134
  total_pages: number;
121
- has_next: boolean;
122
- has_prev: boolean;
135
+ limit: number;
136
+ offset: number;
123
137
  } {
124
- const totalPages = Math.max(1, Math.ceil(this.count / perPage));
125
- const offset = (page - 1) * perPage;
126
- const sliced = this.records.slice(offset, offset + perPage);
138
+ // No parameters (ADR-0043). `arguments` catches an argument passed anyway —
139
+ // including from plain JS, where the 0-arity signature is not enforced — so a
140
+ // caller porting the old two-argument form gets a hard error, never a silent
141
+ // in-memory re-slice that lies about total_pages.
142
+ if (arguments.length > 0) {
143
+ throw new TypeError(
144
+ "toPaginate() takes no arguments and derives the page from the query that " +
145
+ "ran (ADR-0043). A DatabaseResult holds no connection, so an argument could " +
146
+ "only re-slice the rows already in memory and report total_pages for pages " +
147
+ "it can never reach. To read a page, FETCH it: db.fetch(sql, params, perPage, " +
148
+ "(page - 1) * perPage), then call toPaginate() with no arguments.",
149
+ );
150
+ }
151
+
152
+ const perPage = this.limit > 0 ? this.limit : this.records.length;
153
+ const page = perPage > 0 ? Math.floor(this.offset / perPage) + 1 : 1;
154
+ const totalPages = perPage > 0 ? Math.max(1, Math.ceil(this.count / perPage)) : 1;
155
+
127
156
  return {
128
- records: sliced,
129
- data: sliced,
130
- count: this.count,
157
+ records: this.records,
131
158
  total: this.count,
132
- limit: perPage,
133
- offset,
134
159
  page,
135
160
  per_page: perPage,
136
- perPage,
137
- totalPages,
138
161
  total_pages: totalPages,
139
- has_next: page < totalPages,
140
- has_prev: page > 1,
162
+ limit: perPage,
163
+ offset: this.offset,
141
164
  };
142
165
  }
143
166
 
@@ -216,7 +239,7 @@ export class DatabaseResult implements Iterable<Record<string, unknown>> {
216
239
  if (!this._adapter) return this._fallbackColumnInfo();
217
240
 
218
241
  try {
219
- const rawCols: ColumnInfo[] = this._adapter.columns(table);
242
+ const rawCols: ColumnInfo[] = this._adapter.getColumns(table);
220
243
  return this._normalizeColumns(rawCols);
221
244
  } catch {
222
245
  return this._fallbackColumnInfo();
@@ -0,0 +1,484 @@
1
+ /**
2
+ * A parsed database connection URL, as a VALUE.
3
+ *
4
+ * Feature 5 of the feature audit. This used to be `parseDatabaseUrl()`, a single
5
+ * function with a cyclomatic complexity of 43 - the worst function measured
6
+ * anywhere in the audit - whose entire job is string-to-struct. It is now one
7
+ * small parser per engine, each well under the threshold, behind a value type
8
+ * with the same surface as PHP's `DatabaseUrl` (the reference for this row).
9
+ *
10
+ * Core Principle 6 says a connection string must mean literally the same thing
11
+ * in every framework. `test/fixtures/database_url_corpus.json` is the answer
12
+ * key, byte-identical in all four.
13
+ */
14
+ import { inspect } from "node:util";
15
+
16
+ /** The canonical engine names. Aliases resolve to these ONCE, at parse. */
17
+ export type DatabaseEngine =
18
+ | "sqlite"
19
+ | "postgres"
20
+ | "mysql"
21
+ | "mssql"
22
+ | "firebird"
23
+ | "mongodb"
24
+ | "odbc";
25
+
26
+ /**
27
+ * URL scheme to canonical engine.
28
+ *
29
+ * `sqlite3` is accepted because the driver is literally named sqlite3 in every
30
+ * framework (Python's sqlite3 module, Ruby's sqlite3 gem, PHP's ext-sqlite3,
31
+ * Node's node:sqlite), so people type it. The "3" is a file-format version, not
32
+ * a different engine, which is why the canonical name stays `sqlite`.
33
+ */
34
+ const ENGINE_ALIASES: Record<string, DatabaseEngine> = {
35
+ sqlite: "sqlite",
36
+ sqlite3: "sqlite",
37
+ postgres: "postgres",
38
+ postgresql: "postgres",
39
+ pgsql: "postgres",
40
+ mysql: "mysql",
41
+ mssql: "mssql",
42
+ sqlserver: "mssql",
43
+ firebird: "firebird",
44
+ mongodb: "mongodb",
45
+ "mongodb+srv": "mongodb",
46
+ odbc: "odbc",
47
+ };
48
+
49
+ /**
50
+ * Default port per engine, applied AT PARSE.
51
+ *
52
+ * The port is part of our contract, not the driver's business. Node used to
53
+ * leave it unset and let the third-party driver fill in its own default, so the
54
+ * parsed struct for `postgresql://localhost/db` differed from PHP's while the
55
+ * connection still worked - a divergence hidden behind somebody else's
56
+ * assumption.
57
+ */
58
+ const DEFAULT_PORTS: Partial<Record<DatabaseEngine, number>> = {
59
+ postgres: 5432,
60
+ mysql: 3306,
61
+ mssql: 1433,
62
+ firebird: 3050,
63
+ mongodb: 27017,
64
+ };
65
+
66
+ /** Strip EXACTLY ONE leading slash: the URL path separator, never more. */
67
+ function stripOneSlash(path: string): string {
68
+ return path.startsWith("/") ? path.slice(1) : path;
69
+ }
70
+
71
+ function decode(value: string | undefined): string | null {
72
+ if (value === undefined || value === "") return null;
73
+ return decodeURIComponent(value);
74
+ }
75
+
76
+ // ── redaction ──────────────────────────────────────────────────────────────
77
+ // ONE primitive, used by every path that can put a connection string in front
78
+ // of a human. Before this, `toSafeString()` had ZERO call sites outside the
79
+ // corpus test - its own docblock called it "the ONLY form allowed in a log
80
+ // line" while the invalid-URL exception interpolated the RAW url and the odbc
81
+ // branch returned the connection string VERBATIM, `PWD=` and all.
82
+
83
+ /** The single mask. One spelling, so grepping for it finds every redaction. */
84
+ const REDACTED = "***";
85
+
86
+ /**
87
+ * Where a keyword VALUE ends in a keyword/value connection string.
88
+ *
89
+ * NOT at the first whitespace. tina4-php redacted its connect-failure message
90
+ * with a `\bpassword=\S` + star pattern, and a password containing a SPACE kept
91
+ * its TAIL in the logged line. The real terminators are the field separators -
92
+ * `;` for ODBC/libpq, `&` for a query string - plus the closing brace/quote of
93
+ * a quoted value, since `PWD={p;w}` and the libpq quoted form both legally
94
+ * contain a separator.
95
+ */
96
+ function endOfKeywordValue(text: string, start: number): number {
97
+ const open = text[start];
98
+ if (open === "{") {
99
+ const close = text.indexOf("}", start + 1);
100
+ return close === -1 ? text.length : close + 1;
101
+ }
102
+ if (open === "'" || open === '"') {
103
+ let i = start + 1;
104
+ while (i < text.length) {
105
+ if (text[i] === "\\") { i += 2; continue; }
106
+ if (text[i] === open) return i + 1;
107
+ i++;
108
+ }
109
+ return text.length;
110
+ }
111
+ let i = start;
112
+ while (i < text.length && text[i] !== ";" && text[i] !== "&") i++;
113
+ return i;
114
+ }
115
+
116
+ /** `PWD=`/`password=`/`passwd=` in an ODBC DSN, a libpq DSN or a query string. */
117
+ const SECRET_KEYWORD_PATTERN = /(^|[;&?\s])(pwd|password|passwd)(\s*=\s*)/gi;
118
+
119
+ function redactKeywordValues(text: string): string {
120
+ const pattern = new RegExp(SECRET_KEYWORD_PATTERN.source, SECRET_KEYWORD_PATTERN.flags);
121
+ let out = "";
122
+ let cursor = 0;
123
+ let match: RegExpExecArray | null;
124
+ while ((match = pattern.exec(text)) !== null) {
125
+ const valueStart = match.index + match[0].length;
126
+ out += text.slice(cursor, valueStart) + REDACTED;
127
+ cursor = endOfKeywordValue(text, valueStart);
128
+ pattern.lastIndex = cursor;
129
+ }
130
+ return out + text.slice(cursor);
131
+ }
132
+
133
+ /**
134
+ * The authority of `scheme://user:pass@host:port/path`, or null when the string
135
+ * has no `://` at all. Everything the other helpers need is derived from here,
136
+ * so "where does userinfo end" is decided in exactly one place.
137
+ */
138
+ function authorityOf(raw: string): string | null {
139
+ const separator = raw.indexOf("://");
140
+ if (separator === -1) return null;
141
+ const rest = raw.slice(separator + 3);
142
+ const end = rest.search(/[/?#]/);
143
+ return end === -1 ? rest : rest.slice(0, end);
144
+ }
145
+
146
+ /**
147
+ * The raw, still-encoded userinfo, or null when the URL carries none.
148
+ *
149
+ * Read off the RAW string on purpose: `new URL()` normalises
150
+ * `postgres://user:@host/db` and `postgres://user@host/db` to the identical
151
+ * href, so the URL object cannot tell an explicitly-blank password from an
152
+ * absent one (measured on Node 24.9.0 - both report `.password === ""`).
153
+ */
154
+ function rawUserinfo(raw: string): string | null {
155
+ const authority = authorityOf(raw);
156
+ if (authority === null) return null;
157
+ const at = authority.lastIndexOf("@");
158
+ return at === -1 ? null : authority.slice(0, at);
159
+ }
160
+
161
+ function redactUserinfoPassword(raw: string): string {
162
+ const authority = authorityOf(raw);
163
+ if (authority === null) return raw;
164
+ const at = authority.lastIndexOf("@");
165
+ if (at === -1) return raw;
166
+ const colon = authority.slice(0, at).indexOf(":");
167
+ if (colon === -1) return raw; // a username with no password
168
+ const authorityStart = raw.indexOf("://") + 3;
169
+ return (
170
+ raw.slice(0, authorityStart + colon + 1) + REDACTED + raw.slice(authorityStart + at)
171
+ );
172
+ }
173
+
174
+ /**
175
+ * Remove every credential from an arbitrary connection string.
176
+ *
177
+ * THE single redaction primitive. It works on a RAW string - valid or
178
+ * malformed, a URL or an ODBC DSN - so the error paths can use it too, and it
179
+ * is what `toSafeString()` calls for the odbc form rather than hand-rolling a
180
+ * second, weaker rule.
181
+ *
182
+ * It cannot be complete on a string with no recognisable credential structure
183
+ * (`notaurl-with-hunter2` has nothing to key off), which is exactly why the
184
+ * invalid-URL error reports the scheme and host instead of any form of the
185
+ * input. Redaction is for strings we can parse enough to redact.
186
+ */
187
+ export function redactCredentials(raw: string): string {
188
+ if (typeof raw !== "string" || raw === "") return raw;
189
+ return redactKeywordValues(redactUserinfoPassword(raw));
190
+ }
191
+
192
+ /** `postgres` from `postgres://…`, or null when the string names no scheme. */
193
+ function schemeOf(raw: string): string | null {
194
+ const match = raw.match(/^([a-zA-Z][a-zA-Z0-9+.-]*):/);
195
+ return match ? match[1].toLowerCase() : null;
196
+ }
197
+
198
+ /**
199
+ * The `host:port` slice of the authority, or null.
200
+ *
201
+ * Credential-free BY CONSTRUCTION: it is the part AFTER the last `@`, and
202
+ * userinfo - the only place a password may appear in a URL - is entirely
203
+ * before it. That is what makes it safe to name in an error message.
204
+ */
205
+ function hostPortOf(raw: string): string | null {
206
+ const authority = authorityOf(raw);
207
+ if (authority === null) return null;
208
+ const at = authority.lastIndexOf("@");
209
+ const hostPort = at === -1 ? authority : authority.slice(at + 1);
210
+ return hostPort === "" ? null : hostPort;
211
+ }
212
+
213
+ /**
214
+ * The failure a malformed `TINA4_DATABASE_URL` raises.
215
+ *
216
+ * The message NEVER carries the URL. The old one interpolated it, so a typo in
217
+ * the port wrote the password into the boot log, the crash report, the error
218
+ * overlay and the CI log - measured: `TINA4_DATABASE_URL` of
219
+ * `postgres://user:SuperSecret123@host:notaport/db` produced
220
+ * `DatabaseUrl: invalid URL format 'postgres://user:SuperSecret123@host:notaport/db'`.
221
+ *
222
+ * It stays diagnosable: the scheme (or engine) and the host:port are both in
223
+ * the message, along with the shape that was expected. A redaction that leaves
224
+ * nothing to debug with is its own kind of bug.
225
+ */
226
+ function invalidUrlError(raw: string, engine?: DatabaseEngine): Error {
227
+ const named = engine ?? schemeOf(raw);
228
+ const subject = named ? `invalid ${named} URL` : "invalid URL (no scheme found)";
229
+ const hostPort = hostPortOf(raw);
230
+ const at = hostPort === null ? "" : ` at '${hostPort}'`;
231
+ return new Error(
232
+ `DatabaseUrl: ${subject}${at} - expected ` +
233
+ "scheme://[user[:password]@]host[:port]/database. " +
234
+ "The URL itself is not shown because it may contain a password."
235
+ );
236
+ }
237
+
238
+ /**
239
+ * DISPLAY REDACTS, FIDELITY DOES NOT. JSON.stringify, util.inspect, String() and
240
+ * toSafeString() replace the password with the redaction marker, so a log line, a
241
+ * stack or a status payload is safe. structuredClone deliberately does not: its
242
+ * contract is a faithful structural copy, and a masked clone would produce an
243
+ * object whose password is the literal "***".
244
+ *
245
+ * The consequence: DO NOT PERSIST THIS OBJECT. A DatabaseUrl structured-cloned
246
+ * onto a worker thread, into a cache or into a queue payload carries a cleartext
247
+ * credential across that boundary. Use toSafeString() instead.
248
+ * test/databaseUrlRedaction.test.ts fails the build if framework code ever does.
249
+ */
250
+ export class DatabaseUrl {
251
+ readonly engine: DatabaseEngine;
252
+ /** Null for sqlite and odbc - a file or a DSN string has no host. */
253
+ readonly host: string | null;
254
+ /** Null for sqlite and odbc. Otherwise always set: the engine default applies. */
255
+ readonly port: number | null;
256
+ readonly database: string;
257
+ /** Null when absent, never an empty string - absent and blank differ. */
258
+ readonly username: string | null;
259
+ readonly password: string | null;
260
+ /** ODBC only: the raw connection string handed to odbc.connect(). */
261
+ readonly connectionString: string | null;
262
+
263
+ constructor(url: string, username?: string, password?: string) {
264
+ const parsed = DatabaseUrl.parse(url);
265
+ this.engine = parsed.engine;
266
+ this.host = parsed.host ?? null;
267
+ this.port = parsed.port ?? DEFAULT_PORTS[parsed.engine] ?? null;
268
+ this.database = parsed.database ?? "";
269
+ this.connectionString = parsed.connectionString ?? null;
270
+ // Separate credentials fill in only when the URL carried none.
271
+ this.username = parsed.username ?? (username ? username : null);
272
+ this.password = parsed.password ?? (password ? password : null);
273
+ }
274
+
275
+ static fromEnv(key = "TINA4_DATABASE_URL"): DatabaseUrl | null {
276
+ const url = (process.env[key] ?? "").trim();
277
+ if (url === "") return null;
278
+ return new DatabaseUrl(
279
+ url,
280
+ process.env.TINA4_DATABASE_USERNAME,
281
+ process.env.TINA4_DATABASE_PASSWORD
282
+ );
283
+ }
284
+
285
+ /**
286
+ * Connection target for the adapter. sqlite and odbc are the whole value.
287
+ *
288
+ * NOT SAFE TO LOG. For every network engine this is credential-free
289
+ * (host:port/database), which makes it look loggable - but the odbc branch
290
+ * returns the connection string VERBATIM, `PWD=` included, because that is
291
+ * what the driver has to receive. Log `toSafeString()`; never this.
292
+ */
293
+ dsn(): string {
294
+ if (this.engine === "sqlite") return this.database;
295
+ if (this.engine === "odbc") return this.connectionString ?? "";
296
+ let dsn = this.host ?? "";
297
+ if (this.port !== null) dsn += `:${this.port}`;
298
+ if (this.database !== "") dsn += `/${this.database}`;
299
+ return dsn;
300
+ }
301
+
302
+ /**
303
+ * The URL with the password replaced by ***.
304
+ *
305
+ * The ONLY form allowed in a log line or an error message: a connection URL in
306
+ * a log is a credential leak. Node had no such method at all before this,
307
+ * which meant every call site that wanted to log a connection target had to
308
+ * redact it by hand. It round-trips, so it stays readable as well as safe.
309
+ */
310
+ toSafeString(): string {
311
+ if (this.engine === "sqlite") return `sqlite:///${this.database}`;
312
+ // The odbc branch used to return the connection string VERBATIM - `PWD=`
313
+ // and all - so the ONE method whose job is redaction handed back the
314
+ // password in full. The negative test "to_safe_string_never_contains_the_
315
+ // password" passed in all four frameworks the whole time, because the
316
+ // shared corpus had no odbc row: a green guard protecting nothing.
317
+ if (this.engine === "odbc") return `odbc:///${redactCredentials(this.connectionString ?? "")}`;
318
+
319
+ let out = `${this.engine}://`;
320
+ if (this.username !== null) {
321
+ out += this.username;
322
+ if (this.password !== null) out += ":***";
323
+ out += "@";
324
+ }
325
+ out += this.host ?? "";
326
+ if (this.port !== null) out += `:${this.port}`;
327
+ if (this.database !== "") out += `/${this.database}`;
328
+ return out;
329
+ }
330
+
331
+ /**
332
+ * What `JSON.stringify(url)` emits.
333
+ *
334
+ * Without it, stringifying the value - directly, or as one field of a config
335
+ * object being logged - emitted `"password":"<the real password>"`, measured
336
+ * on this class. Python guards the same exposure with `__repr__` and Ruby
337
+ * with `#inspect`; JSON is the shape Node actually serialises into a log
338
+ * line, so it needs the guard too.
339
+ *
340
+ * Structure is preserved so the dump is still worth having: only the secret
341
+ * is masked. `null` stays `null` - an ABSENT password and a masked one are
342
+ * different facts, and flattening them would hide exactly the confusion C7
343
+ * is about.
344
+ */
345
+ toJSON(): Record<string, unknown> {
346
+ return {
347
+ engine: this.engine,
348
+ host: this.host,
349
+ port: this.port,
350
+ database: this.database,
351
+ username: this.username,
352
+ password: this.password === null ? null : REDACTED,
353
+ connectionString:
354
+ this.connectionString === null ? null : redactCredentials(this.connectionString),
355
+ };
356
+ }
357
+
358
+ /**
359
+ * What `console.log(url)` / `util.inspect(url)` print.
360
+ *
361
+ * Node's equivalent of Python's `__repr__` and Ruby's `#inspect`, and the
362
+ * same rendering they produce - `DatabaseUrl('postgres://user:***@h:5432/db')`
363
+ * (tina4-python/tina4_python/database/database_url.py:153). Without it,
364
+ * `console.log(url)` printed the default field dump, password included.
365
+ */
366
+ [inspect.custom](): string {
367
+ return `DatabaseUrl('${this.toSafeString()}')`;
368
+ }
369
+
370
+ // ── parsing ────────────────────────────────────────────────
371
+ // One small parser per engine. The 43-CC original is gone.
372
+
373
+ private static parse(url: string): ParsedParts {
374
+ if (typeof url !== "string" || url.trim() === "") {
375
+ throw new Error("DatabaseUrl: the URL is empty");
376
+ }
377
+ if (url.startsWith("sqlite:") || url.startsWith("sqlite3:")) {
378
+ return DatabaseUrl.parseSqlite(url);
379
+ }
380
+ if (url.startsWith("odbc:///")) {
381
+ return { engine: "odbc", connectionString: url.slice("odbc:///".length) };
382
+ }
383
+ if (url.startsWith("mssql://") || url.startsWith("sqlserver://")) {
384
+ return DatabaseUrl.parseRegexForm(url, "mssql", /(?:mssql|sqlserver):\/\/(?:([^:]+):([^@]+)@)?([^:/]+)(?::(\d+))?\/(.*)/);
385
+ }
386
+ if (url.startsWith("firebird://")) {
387
+ return DatabaseUrl.parseRegexForm(url, "firebird", /firebird:\/\/(?:([^:]+):([^@]+)@)?([^:/]+)(?::(\d+))?\/(.*)/);
388
+ }
389
+ return DatabaseUrl.parseStandard(url);
390
+ }
391
+
392
+ /**
393
+ * sqlite is parsed on the RAW string. The URL class collapses `sqlite:/x` and
394
+ * `sqlite:///x`, losing the difference between a one-slash ABSOLUTE path and
395
+ * the documented three-slash RELATIVE form.
396
+ *
397
+ * sqlite:///app.db -> app.db (three slashes = relative to cwd)
398
+ * sqlite:////abs/app.db -> /abs/app.db (four slashes = absolute)
399
+ * sqlite:/abs/app.db -> /abs/app.db (one slash = a real absolute path)
400
+ * sqlite:app.db -> app.db
401
+ */
402
+ private static parseSqlite(url: string): ParsedParts {
403
+ const normalised = url.startsWith("sqlite3:") ? `sqlite:${url.slice("sqlite3:".length)}` : url;
404
+ if (normalised === "sqlite::memory:" || normalised === "sqlite:///:memory:") {
405
+ return { engine: "sqlite", database: ":memory:" };
406
+ }
407
+ if (normalised.startsWith("sqlite:///")) {
408
+ return { engine: "sqlite", database: stripOneSlash(normalised.slice("sqlite://".length)) };
409
+ }
410
+ if (normalised.startsWith("sqlite://")) {
411
+ return { engine: "sqlite", database: normalised.slice("sqlite://".length) };
412
+ }
413
+ return { engine: "sqlite", database: normalised.slice("sqlite:".length) };
414
+ }
415
+
416
+ /**
417
+ * mssql and firebird: the URL class does not know these schemes, so they are
418
+ * matched directly.
419
+ *
420
+ * The captured path keeps its own leading slash when the URL had two, which is
421
+ * how the documented absolute Firebird form survives. The old code did
422
+ * `"/" + match[5]`, ADDING a slash - so an absolute path came back with two
423
+ * and a relative path was silently made absolute. Verified against live
424
+ * Firebird 5.0.4: the driver takes one or two leading slashes and rejects a
425
+ * relative path outright.
426
+ */
427
+ private static parseRegexForm(url: string, engine: DatabaseEngine, pattern: RegExp): ParsedParts {
428
+ const m = url.match(pattern);
429
+ if (!m) throw invalidUrlError(url, engine);
430
+ return {
431
+ engine,
432
+ username: decode(m[1]),
433
+ password: decode(m[2]),
434
+ host: m[3],
435
+ port: m[4] ? parseInt(m[4], 10) : undefined,
436
+ database: m[5],
437
+ };
438
+ }
439
+
440
+ /** postgres / mysql / mongodb, via the URL class. */
441
+ private static parseStandard(url: string): ParsedParts {
442
+ let parsed: URL;
443
+ try {
444
+ parsed = new URL(url);
445
+ } catch {
446
+ throw invalidUrlError(url);
447
+ }
448
+ const scheme = parsed.protocol.replace(/:$/, "").toLowerCase();
449
+ const engine = ENGINE_ALIASES[scheme];
450
+ if (engine === undefined) {
451
+ throw new Error(
452
+ `DatabaseUrl: Unsupported database scheme '${scheme}'. Supported: ${Object.keys(ENGINE_ALIASES).join(", ")}`
453
+ );
454
+ }
455
+ const database = stripOneSlash(parsed.pathname);
456
+ // A password that was WRITTEN but left blank (`postgres://user:@host/db`)
457
+ // is an explicitly-empty password, NOT an absent one, so the
458
+ // TINA4_DATABASE_PASSWORD fallback in the constructor must not fire for it.
459
+ // `decode()` collapsed both to null and the fallback DID fire, so the same
460
+ // .env authenticated with two different passwords depending on which
461
+ // framework read it. The URL object cannot tell the two apart (it
462
+ // normalises both to `.password === ""`), so the RAW userinfo decides.
463
+ const userinfo = rawUserinfo(url);
464
+ const passwordWasWritten = userinfo !== null && userinfo.includes(":");
465
+ return {
466
+ engine,
467
+ host: parsed.hostname || undefined,
468
+ port: parsed.port ? parseInt(parsed.port, 10) : undefined,
469
+ username: decode(parsed.username),
470
+ password: passwordWasWritten ? decodeURIComponent(parsed.password) : null,
471
+ database: engine === "mongodb" ? database || "tina4" : database,
472
+ };
473
+ }
474
+ }
475
+
476
+ interface ParsedParts {
477
+ engine: DatabaseEngine;
478
+ host?: string;
479
+ port?: number;
480
+ database?: string;
481
+ username?: string | null;
482
+ password?: string | null;
483
+ connectionString?: string;
484
+ }