@blamejs/core 0.7.4 → 0.7.18

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 (180) hide show
  1. package/CHANGELOG.md +423 -395
  2. package/README.md +150 -149
  3. package/bin/blamejs.js +0 -0
  4. package/index.js +308 -284
  5. package/lib/api-key.js +660 -663
  6. package/lib/api-snapshot.js +338 -338
  7. package/lib/app-shutdown.js +385 -385
  8. package/lib/app.js +365 -365
  9. package/lib/archive.js +250 -250
  10. package/lib/atomic-file.js +544 -544
  11. package/lib/audit-chain.js +177 -177
  12. package/lib/audit-sign.js +344 -344
  13. package/lib/audit-tools.js +677 -677
  14. package/lib/audit.js +766 -766
  15. package/lib/auth/jwt.js +311 -311
  16. package/lib/auth/lockout.js +436 -436
  17. package/lib/auth/oauth.js +721 -721
  18. package/lib/auth/passkey.js +181 -181
  19. package/lib/auth/password.js +594 -594
  20. package/lib/backup/bundle.js +217 -217
  21. package/lib/backup/crypto.js +176 -176
  22. package/lib/backup/index.js +515 -515
  23. package/lib/backup/manifest.js +282 -282
  24. package/lib/break-glass.js +1338 -1338
  25. package/lib/bundler.js +441 -441
  26. package/lib/cache-redis.js +256 -256
  27. package/lib/cache.js +1206 -1206
  28. package/lib/canonical-json.js +115 -115
  29. package/lib/chain-writer.js +234 -234
  30. package/lib/cli-helpers.js +206 -206
  31. package/lib/cli.js +2334 -2334
  32. package/lib/cluster-provider-db.js +317 -317
  33. package/lib/cluster-storage.js +226 -226
  34. package/lib/cluster.js +703 -703
  35. package/lib/codepoint-class.js +196 -0
  36. package/lib/config-drift.js +301 -301
  37. package/lib/consent.js +222 -222
  38. package/lib/constants.js +191 -191
  39. package/lib/cookies.js +315 -315
  40. package/lib/credential-hash.js +322 -322
  41. package/lib/crypto.js +266 -266
  42. package/lib/csv.js +275 -286
  43. package/lib/db-declare-row-policy.js +267 -267
  44. package/lib/db-declare-view.js +420 -421
  45. package/lib/db-query.js +406 -406
  46. package/lib/db-schema.js +319 -319
  47. package/lib/db.js +1288 -1288
  48. package/lib/deprecate.js +222 -222
  49. package/lib/dev.js +335 -335
  50. package/lib/dual-control.js +473 -473
  51. package/lib/error-page.js +420 -420
  52. package/lib/external-db-migrate.js +441 -441
  53. package/lib/external-db.js +1061 -1061
  54. package/lib/file-type.js +273 -273
  55. package/lib/file-upload.js +213 -10
  56. package/lib/forms.js +422 -422
  57. package/lib/framework-error.js +293 -215
  58. package/lib/framework-schema.js +717 -717
  59. package/lib/gate-contract.js +971 -0
  60. package/lib/guard-all.js +405 -0
  61. package/lib/guard-archive.js +739 -0
  62. package/lib/guard-csv.js +816 -0
  63. package/lib/guard-email.js +744 -0
  64. package/lib/guard-filename.js +724 -0
  65. package/lib/guard-html.js +976 -0
  66. package/lib/guard-json.js +729 -0
  67. package/lib/guard-markdown.js +586 -0
  68. package/lib/guard-svg.js +976 -0
  69. package/lib/guard-xml.js +405 -0
  70. package/lib/guard-yaml.js +529 -0
  71. package/lib/handlers.js +350 -350
  72. package/lib/http-client-cookie-jar.js +508 -508
  73. package/lib/http-client.js +1195 -1195
  74. package/lib/i18n.js +878 -878
  75. package/lib/jobs.js +185 -185
  76. package/lib/log-stream-cloudwatch.js +369 -369
  77. package/lib/log-stream-local.js +146 -146
  78. package/lib/log-stream-otlp-grpc.js +410 -410
  79. package/lib/log-stream-otlp.js +286 -286
  80. package/lib/log-stream-syslog.js +302 -302
  81. package/lib/log-stream-webhook.js +199 -199
  82. package/lib/log-stream.js +330 -330
  83. package/lib/log.js +500 -500
  84. package/lib/mail-bounce.js +528 -528
  85. package/lib/mail-dkim.js +369 -362
  86. package/lib/mail.js +981 -962
  87. package/lib/metrics.js +683 -683
  88. package/lib/middleware/api-encrypt.js +936 -936
  89. package/lib/middleware/attach-user.js +157 -157
  90. package/lib/middleware/body-parser.js +1170 -1091
  91. package/lib/middleware/bot-guard.js +178 -178
  92. package/lib/middleware/compression.js +452 -452
  93. package/lib/middleware/cors.js +314 -314
  94. package/lib/middleware/csp-nonce.js +348 -348
  95. package/lib/middleware/csrf-protect.js +316 -316
  96. package/lib/middleware/db-role-for.js +264 -264
  97. package/lib/middleware/health.js +392 -392
  98. package/lib/middleware/index.js +79 -79
  99. package/lib/middleware/rate-limit.js +358 -358
  100. package/lib/middleware/request-id.js +61 -61
  101. package/lib/middleware/request-log.js +168 -168
  102. package/lib/middleware/require-auth.js +104 -104
  103. package/lib/middleware/security-headers.js +116 -116
  104. package/lib/middleware/sse.js +166 -166
  105. package/lib/migrations.js +383 -383
  106. package/lib/mtls-ca.js +518 -518
  107. package/lib/mtls-engine-default.js +481 -481
  108. package/lib/network-dns.js +632 -632
  109. package/lib/network-heartbeat.js +290 -290
  110. package/lib/network-nts.js +574 -574
  111. package/lib/network-proxy.js +265 -265
  112. package/lib/network-tls.js +328 -328
  113. package/lib/network.js +233 -233
  114. package/lib/notify.js +612 -612
  115. package/lib/ntp-check.js +229 -229
  116. package/lib/numeric-bounds.js +111 -91
  117. package/lib/object-store/azure-blob-bucket-ops.js +349 -349
  118. package/lib/object-store/azure-blob.js +488 -488
  119. package/lib/object-store/gcs-bucket-ops.js +351 -351
  120. package/lib/object-store/gcs.js +519 -519
  121. package/lib/object-store/http-put.js +153 -153
  122. package/lib/object-store/index.js +197 -197
  123. package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
  124. package/lib/object-store/sigv4.js +903 -903
  125. package/lib/observability.js +151 -151
  126. package/lib/otel-export.js +269 -269
  127. package/lib/pagination.js +464 -464
  128. package/lib/parsers/index.js +80 -80
  129. package/lib/parsers/safe-env.js +642 -642
  130. package/lib/parsers/safe-ini.js +292 -292
  131. package/lib/parsers/safe-toml.js +784 -784
  132. package/lib/parsers/safe-xml.js +390 -390
  133. package/lib/parsers/safe-yaml.js +1015 -1015
  134. package/lib/permissions.js +708 -708
  135. package/lib/pqc-agent.js +87 -87
  136. package/lib/pqc-gate.js +279 -279
  137. package/lib/protobuf-encoder.js +190 -190
  138. package/lib/protocol-dispatcher.js +161 -161
  139. package/lib/pubsub-redis.js +167 -167
  140. package/lib/pubsub.js +429 -429
  141. package/lib/queue-local.js +476 -476
  142. package/lib/queue-redis.js +745 -745
  143. package/lib/queue-sqs.js +319 -319
  144. package/lib/queue.js +695 -695
  145. package/lib/redis-client.js +519 -519
  146. package/lib/request-helpers.js +340 -340
  147. package/lib/restore-bundle.js +237 -237
  148. package/lib/restore-rollback.js +259 -259
  149. package/lib/restore.js +409 -409
  150. package/lib/retry.js +376 -376
  151. package/lib/router.js +748 -748
  152. package/lib/safe-async.js +735 -735
  153. package/lib/safe-buffer.js +237 -237
  154. package/lib/safe-json.js +541 -541
  155. package/lib/safe-schema.js +1266 -1266
  156. package/lib/safe-url.js +159 -159
  157. package/lib/scheduler.js +706 -706
  158. package/lib/security-assert.js +373 -373
  159. package/lib/seeders.js +618 -618
  160. package/lib/session.js +478 -478
  161. package/lib/slug.js +269 -269
  162. package/lib/ssrf-guard.js +401 -401
  163. package/lib/static.js +184 -4
  164. package/lib/storage.js +471 -471
  165. package/lib/subject.js +281 -281
  166. package/lib/template.js +791 -791
  167. package/lib/testing.js +798 -798
  168. package/lib/time.js +310 -310
  169. package/lib/totp.js +302 -302
  170. package/lib/tracing.js +494 -494
  171. package/lib/uuid.js +132 -132
  172. package/lib/validate-opts.js +340 -319
  173. package/lib/vault/index.js +308 -308
  174. package/lib/vault/rotate.js +784 -784
  175. package/lib/vault/wrap.js +296 -296
  176. package/lib/vendor/noble-ciphers.cjs +9 -9
  177. package/lib/webhook.js +595 -595
  178. package/lib/websocket.js +1048 -1048
  179. package/package.json +77 -77
  180. package/sbom.cyclonedx.json +7 -7
@@ -1,441 +1,441 @@
1
- "use strict";
2
- /**
3
- * b.externalDb.migrate — versioned migrations for an externalDb backend.
4
- *
5
- * Mirrors b.migrations (which targets the framework's local SQLite) but
6
- * runs against an externalDb backend. Tracking + lock tables live on
7
- * the externalDb side under `_blamejs_externaldb_migrations` and
8
- * `_blamejs_externaldb_migrations_lock`. Each migration runs inside
9
- * `externalDb.transaction(fn)` so a failing migration rolls back
10
- * cleanly and stops the wave.
11
- *
12
- * Migration file format (filename pattern: NNNN-<slug>.js):
13
- *
14
- * module.exports = {
15
- * description: "Create users table",
16
- * up: async function (xdb, ctx) { await xdb.query("CREATE TABLE ..."); },
17
- * down: async function (xdb, ctx) { await xdb.query("DROP TABLE ..."); },
18
- * };
19
- *
20
- * `xdb` exposes `.query(sql, params) → { rows, rowCount }`. `ctx` carries
21
- * `{ externalDb, backendName }` so migrations that need backend-introspection
22
- * (e.g. `b.db.declareView()`-shaped specs) can call back into the framework.
23
- *
24
- * `up` is required; `down` is optional. Calling `down()` on a migration
25
- * that didn't export `down()` surfaces a clear error.
26
- *
27
- * var migrate = b.externalDb.migrate.create({
28
- * dir: "./migrations-pg",
29
- * backend: "main", // optional; defaults to default backend
30
- * audit: b.audit, // optional
31
- * });
32
- *
33
- * await migrate.up(); // → { applied: [name], skipped: [name] }
34
- * await migrate.down({ steps: 1 });
35
- * migrate.status(); // → { applied: [{name, description, appliedAt}], pending: [name], total }
36
- *
37
- * Concurrent-apply protection: a single-row advisory lock in
38
- * `_blamejs_externaldb_migrations_lock` ensures two processes can't apply
39
- * the same wave concurrently. The losing process gets a clear "lock held
40
- * by other process" error. Stale locks can be force-replaced via
41
- * `staleAfterMs`.
42
- *
43
- * Audit emissions when wired with `audit: b.audit`:
44
- * - externaldb.migrate.up.success { migration, durationMs }
45
- * - externaldb.migrate.up.failure { migration, durationMs, reason }
46
- * - externaldb.migrate.down.success { migration, durationMs }
47
- * - externaldb.migrate.down.failure { migration, durationMs, reason }
48
- * - externaldb.migrate.lock.acquired { holder }
49
- * - externaldb.migrate.lock.released { holder }
50
- */
51
- var path = require("path");
52
- var atomicFile = require("./atomic-file");
53
- var lazyRequire = require("./lazy-require");
54
- var migrationFiles = require("./migration-files");
55
- var numericBounds = require("./numeric-bounds");
56
- var validateOpts = require("./validate-opts");
57
- var { defineClass } = require("./framework-error");
58
-
59
- var ExternalDbMigrateError = defineClass("ExternalDbMigrateError", { alwaysPermanent: true });
60
-
61
- // Lazy require — external-db imports back into this module via its
62
- // public `migrate` namespace; load-order would cycle without lazy.
63
- var externalDbModule = lazyRequire(function () { return require("./external-db"); });
64
-
65
- var TRACKING_TABLE = "_blamejs_externaldb_migrations";
66
- var LOCK_TABLE = "_blamejs_externaldb_migrations_lock";
67
- // Identifiers wrapped in `"..."` per project convention so a reserved-word
68
- // or whitespace-bearing name resolves correctly.
69
- var Q_TRACKING = '"' + TRACKING_TABLE + '"';
70
- var Q_LOCK = '"' + LOCK_TABLE + '"';
71
-
72
- // Filename grammar lives in lib/migration-files (shared with the local
73
- // migrations.js + seeders.js runners). Length capped before the regex
74
- // test so a hostile directory listing can't drive the engine against
75
- // an unbounded filename string.
76
- var FILE_NAME_MAX = 255;
77
-
78
- function _isMigrationFile(name) {
79
- return typeof name === "string" &&
80
- name.length > 0 &&
81
- name.length <= FILE_NAME_MAX &&
82
- migrationFiles.MIGRATION_FILE_RE.test(name);
83
- }
84
-
85
- function _err(code, message) {
86
- return new ExternalDbMigrateError(code, message);
87
- }
88
-
89
- function _lockHolderId() {
90
- return String(process.pid) + "@" + (require("node:os").hostname() || "unknown");
91
- }
92
-
93
- async function _ensureTrackingTable(xdb) {
94
- // Tracking table holds the migration history. ISO-8601 timestamp
95
- // strings (TEXT) keep the framework's tracking table portable across
96
- // Postgres/SQLite without dialect-specific type juggling — operators
97
- // who want strict TIMESTAMPTZ for their own ad-hoc queries against
98
- // the table ALTER it post-creation.
99
- await xdb.query(
100
- "CREATE TABLE IF NOT EXISTS " + Q_TRACKING + " (" +
101
- " name TEXT PRIMARY KEY," +
102
- " description TEXT," +
103
- " appliedAt TEXT NOT NULL" +
104
- ")",
105
- []
106
- );
107
- }
108
-
109
- async function _ensureLockTable(xdb) {
110
- await xdb.query(
111
- "CREATE TABLE IF NOT EXISTS " + Q_LOCK + " (" +
112
- " scope TEXT PRIMARY KEY," +
113
- " lockedAt INTEGER NOT NULL," +
114
- " lockedBy TEXT NOT NULL," +
115
- " CHECK (scope = 'lock')" +
116
- ")",
117
- []
118
- );
119
- }
120
-
121
- // ---- Lock acquire / release ----
122
-
123
- async function _acquireLock(xdb, opts) {
124
- await _ensureLockTable(xdb);
125
- var holder = _lockHolderId();
126
- var nowMs = Date.now();
127
- // See migrations.acquireLock for the same fix — Infinity was
128
- // silently identical to 0 (no staleness check) but obscured the typo.
129
- var staleAfterMs = 0;
130
- if (opts) {
131
- numericBounds.requireNonNegativeFiniteIntIfPresent(opts.staleAfterMs,
132
- "externalDb.migrate.acquireLock: staleAfterMs",
133
- ExternalDbMigrateError, "externalDb-migrate/bad-opt");
134
- if (opts.staleAfterMs !== undefined) staleAfterMs = opts.staleAfterMs;
135
- }
136
- try {
137
- await xdb.query(
138
- "INSERT INTO " + Q_LOCK + " (scope, lockedAt, lockedBy) VALUES ('lock', $1, $2)",
139
- [nowMs, holder]
140
- );
141
- return holder;
142
- } catch (_e) {
143
- // PRIMARY KEY conflict → existing lock. Inspect it.
144
- var existingRes = await xdb.query(
145
- "SELECT lockedAt, lockedBy FROM " + Q_LOCK + " WHERE scope = 'lock'",
146
- []
147
- );
148
- var existing = existingRes && existingRes.rows && existingRes.rows[0];
149
- if (!existing) {
150
- try {
151
- await xdb.query(
152
- "INSERT INTO " + Q_LOCK + " (scope, lockedAt, lockedBy) VALUES ('lock', $1, $2)",
153
- [nowMs, holder]
154
- );
155
- return holder;
156
- } catch (e2) {
157
- throw _err("externaldb-migrate/lock-busy",
158
- "could not acquire migration lock: " + ((e2 && e2.message) || String(e2)));
159
- }
160
- }
161
- var ageMs = nowMs - Number(existing.lockedat || existing.lockedAt);
162
- if (staleAfterMs > 0 && ageMs > staleAfterMs) {
163
- // Force-replace the stale lock atomically.
164
- await xdb.query(
165
- "DELETE FROM " + Q_LOCK + " WHERE scope = 'lock' AND lockedAt = $1",
166
- [Number(existing.lockedat || existing.lockedAt)]
167
- );
168
- await xdb.query(
169
- "INSERT INTO " + Q_LOCK + " (scope, lockedAt, lockedBy) VALUES ('lock', $1, $2)",
170
- [nowMs, holder]
171
- );
172
- return holder;
173
- }
174
- throw _err("externaldb-migrate/lock-held",
175
- "migration lock is held by " + (existing.lockedby || existing.lockedBy) +
176
- " (acquired " + ageMs + "ms ago). Another process is running migrations" +
177
- " — wait for it to finish, or pass staleAfterMs to force-replace stale locks.");
178
- }
179
- }
180
-
181
- async function _releaseLock(xdb, holder) {
182
- try {
183
- await xdb.query(
184
- "DELETE FROM " + Q_LOCK + " WHERE scope = 'lock' AND lockedBy = $1",
185
- [holder]
186
- );
187
- } catch (_e) {
188
- // best-effort release; operator can DELETE manually.
189
- }
190
- }
191
-
192
- // ---- File loading ----
193
-
194
- function _list(dir) {
195
- return atomicFile.listDir(dir, {
196
- filter: _isMigrationFile,
197
- }).map(function (e) { return e.name; }).sort();
198
- }
199
-
200
- function _loadMigration(file, dir) {
201
- var fullPath = path.join(dir, file);
202
- // Drop the require cache so a test/dev that edits a file picks up the
203
- // new content. Matches lib/migrations.js semantics.
204
- try { delete require.cache[require.resolve(fullPath)]; } catch (_e) { /* not yet cached */ }
205
- var mod;
206
- try { mod = require(fullPath); }
207
- catch (e) {
208
- throw _err("externaldb-migrate/load-failed",
209
- "migration '" + file + "' failed to load: " + ((e && e.message) || String(e)));
210
- }
211
- if (!mod || typeof mod.up !== "function") {
212
- throw _err("externaldb-migrate/missing-up",
213
- "migration '" + file + "' must export an `up(xdb, ctx)` function");
214
- }
215
- return mod;
216
- }
217
-
218
- // ---- Audit emit (drop-silent) ----
219
-
220
- function _emit(audit, action, outcome, info, reason) {
221
- if (!audit) return;
222
- try {
223
- audit.safeEmit({
224
- action: action,
225
- outcome: outcome,
226
- metadata: info || {},
227
- reason: reason || null,
228
- });
229
- } catch (_e) { /* drop-silent — audit emit failure must not crash the migration */ }
230
- }
231
-
232
- // ---- Backend resolution ----
233
-
234
- function _resolveBackendName(opts) {
235
- if (opts && typeof opts.backend === "string" && opts.backend.length > 0) {
236
- return opts.backend;
237
- }
238
- // Default to the externalDb's defaultBackend; throw clear if not initialized.
239
- var listed;
240
- try { listed = externalDbModule().listBackends(); }
241
- catch (_e) {
242
- throw _err("externaldb-migrate/not-initialized",
243
- "externalDb is not initialized — call b.externalDb.init({ backends }) first");
244
- }
245
- if (!listed || listed.length === 0) {
246
- throw _err("externaldb-migrate/no-backends",
247
- "externalDb has no backends configured");
248
- }
249
- return listed[0].name;
250
- }
251
-
252
- // ---- Public factory ----
253
-
254
- function create(opts) {
255
- opts = opts || {};
256
- validateOpts(opts, ["dir", "backend", "audit", "staleAfterMs"], "b.externalDb.migrate");
257
- validateOpts.requireNonEmptyString(opts.dir, "externalDb.migrate.create: opts.dir (path to migrations directory)", ExternalDbMigrateError, "externaldb-migrate/no-dir");
258
- validateOpts.optionalFiniteNonNegative(opts.staleAfterMs, "externalDb.migrate: staleAfterMs", ExternalDbMigrateError, "externaldb-migrate/bad-stale");
259
- validateOpts.auditShape(opts.audit, "externalDb.migrate", ExternalDbMigrateError, "externaldb-migrate/bad-audit");
260
- var dir = opts.dir;
261
- var audit = opts.audit || null;
262
-
263
- function _ctx(backendName) {
264
- return {
265
- externalDb: externalDbModule(),
266
- backendName: backendName,
267
- };
268
- }
269
-
270
- async function status() {
271
- var backendName = _resolveBackendName(opts);
272
- return await externalDbModule().transaction(async function (xdb) {
273
- await _ensureTrackingTable(xdb);
274
- var res = await xdb.query(
275
- "SELECT name, description, appliedAt FROM " + Q_TRACKING +
276
- " ORDER BY appliedAt ASC, name ASC",
277
- []
278
- );
279
- var applied = (res && res.rows) || [];
280
- var appliedNames = new Set(applied.map(function (r) { return r.name; }));
281
- var files = _list(dir);
282
- var pending = files.filter(function (f) { return !appliedNames.has(f); });
283
- return {
284
- applied: applied,
285
- pending: pending,
286
- total: files.length,
287
- backend: backendName,
288
- };
289
- }, { backend: backendName });
290
- }
291
-
292
- async function up() {
293
- var backendName = _resolveBackendName(opts);
294
- var ctx = _ctx(backendName);
295
-
296
- return await externalDbModule().transaction(async function (xdb) {
297
- await _ensureTrackingTable(xdb);
298
- await _ensureLockTable(xdb);
299
- }, { backend: backendName }).then(async function () {
300
- // Acquire the lock OUTSIDE the per-migration transaction so the
301
- // lock survives across migration boundaries. We use a separate
302
- // pool acquisition for the lock connection — the migrate runner
303
- // serializes apply order, so this single-connection lock is
304
- // sufficient.
305
- var lockHolder = await externalDbModule().transaction(async function (xdb) {
306
- return await _acquireLock(xdb, opts);
307
- }, { backend: backendName });
308
-
309
- _emit(audit, "externaldb.migrate.lock.acquired", "success",
310
- { holder: lockHolder, backend: backendName }, null);
311
-
312
- try {
313
- var appliedRes = await externalDbModule().query(
314
- "SELECT name FROM " + Q_TRACKING, [], { backend: backendName }
315
- );
316
- var appliedSet = new Set(((appliedRes && appliedRes.rows) || []).map(function (r) { return r.name; }));
317
- var files = _list(dir);
318
- var applied = [];
319
- var skipped = [];
320
-
321
- for (var i = 0; i < files.length; i++) {
322
- var file = files[i];
323
- if (appliedSet.has(file)) { skipped.push(file); continue; }
324
- var mod = _loadMigration(file, dir);
325
- var t0 = Date.now();
326
- try {
327
- await externalDbModule().transaction(async function (xdb) {
328
- await mod.up(xdb, ctx);
329
- await xdb.query(
330
- "INSERT INTO " + Q_TRACKING +
331
- " (name, description, appliedAt) VALUES ($1, $2, $3)",
332
- [file, mod.description || "", new Date().toISOString()]
333
- );
334
- }, { backend: backendName });
335
- _emit(audit, "externaldb.migrate.up", "success",
336
- { migration: file, durationMs: Date.now() - t0, backend: backendName }, null);
337
- applied.push(file);
338
- } catch (e) {
339
- _emit(audit, "externaldb.migrate.up", "failure",
340
- { migration: file, durationMs: Date.now() - t0, backend: backendName },
341
- (e && e.message) || String(e));
342
- throw _err("externaldb-migrate/up-failed",
343
- "migration '" + file + "' failed to apply: " + ((e && e.message) || String(e)));
344
- }
345
- }
346
- return { applied: applied, skipped: skipped, backend: backendName };
347
- } finally {
348
- try {
349
- await externalDbModule().transaction(async function (xdb) {
350
- await _releaseLock(xdb, lockHolder);
351
- }, { backend: backendName });
352
- _emit(audit, "externaldb.migrate.lock.released", "success",
353
- { holder: lockHolder, backend: backendName }, null);
354
- } catch (_e) { /* best-effort release; emit a failure audit */
355
- _emit(audit, "externaldb.migrate.lock.released", "failure",
356
- { holder: lockHolder, backend: backendName }, "release failed");
357
- }
358
- }
359
- });
360
- }
361
-
362
- async function down(downOpts) {
363
- downOpts = downOpts || {};
364
- var steps = (typeof downOpts.steps === "number" && downOpts.steps > 0)
365
- ? Math.floor(downOpts.steps) : 1;
366
- var backendName = _resolveBackendName(opts);
367
- var ctx = _ctx(backendName);
368
-
369
- await externalDbModule().transaction(async function (xdb) {
370
- await _ensureTrackingTable(xdb);
371
- await _ensureLockTable(xdb);
372
- }, { backend: backendName });
373
-
374
- var lockHolder = await externalDbModule().transaction(async function (xdb) {
375
- return await _acquireLock(xdb, opts);
376
- }, { backend: backendName });
377
-
378
- _emit(audit, "externaldb.migrate.lock.acquired", "success",
379
- { holder: lockHolder, backend: backendName }, null);
380
-
381
- try {
382
- var appliedRes = await externalDbModule().query(
383
- "SELECT name FROM " + Q_TRACKING + " ORDER BY appliedAt DESC, name DESC LIMIT $1",
384
- [steps], { backend: backendName }
385
- );
386
- var rows = (appliedRes && appliedRes.rows) || [];
387
- var reverted = [];
388
- for (var i = 0; i < rows.length; i++) {
389
- var file = rows[i].name;
390
- var mod = _loadMigration(file, dir);
391
- if (typeof mod.down !== "function") {
392
- throw _err("externaldb-migrate/no-down",
393
- "migration '" + file + "' has no down() — write one or restore from backup");
394
- }
395
- var t0 = Date.now();
396
- try {
397
- await externalDbModule().transaction(async function (xdb) {
398
- await mod.down(xdb, ctx);
399
- await xdb.query(
400
- "DELETE FROM " + Q_TRACKING + " WHERE name = $1",
401
- [file]
402
- );
403
- }, { backend: backendName });
404
- _emit(audit, "externaldb.migrate.down", "success",
405
- { migration: file, durationMs: Date.now() - t0, backend: backendName }, null);
406
- reverted.push(file);
407
- } catch (e) {
408
- _emit(audit, "externaldb.migrate.down", "failure",
409
- { migration: file, durationMs: Date.now() - t0, backend: backendName },
410
- (e && e.message) || String(e));
411
- throw _err("externaldb-migrate/down-failed",
412
- "migration '" + file + "' failed to roll back: " + ((e && e.message) || String(e)));
413
- }
414
- }
415
- return { reverted: reverted, backend: backendName };
416
- } finally {
417
- try {
418
- await externalDbModule().transaction(async function (xdb) {
419
- await _releaseLock(xdb, lockHolder);
420
- }, { backend: backendName });
421
- _emit(audit, "externaldb.migrate.lock.released", "success",
422
- { holder: lockHolder, backend: backendName }, null);
423
- } catch (_e) {
424
- _emit(audit, "externaldb.migrate.lock.released", "failure",
425
- { holder: lockHolder, backend: backendName }, "release failed");
426
- }
427
- }
428
- }
429
-
430
- return {
431
- up: up,
432
- down: down,
433
- status: status,
434
- };
435
- }
436
-
437
- module.exports = {
438
- create: create,
439
- ExternalDbMigrateError: ExternalDbMigrateError,
440
- TRACKING_TABLE: TRACKING_TABLE,
441
- };
1
+ "use strict";
2
+ /**
3
+ * b.externalDb.migrate — versioned migrations for an externalDb backend.
4
+ *
5
+ * Mirrors b.migrations (which targets the framework's local SQLite) but
6
+ * runs against an externalDb backend. Tracking + lock tables live on
7
+ * the externalDb side under `_blamejs_externaldb_migrations` and
8
+ * `_blamejs_externaldb_migrations_lock`. Each migration runs inside
9
+ * `externalDb.transaction(fn)` so a failing migration rolls back
10
+ * cleanly and stops the wave.
11
+ *
12
+ * Migration file format (filename pattern: NNNN-<slug>.js):
13
+ *
14
+ * module.exports = {
15
+ * description: "Create users table",
16
+ * up: async function (xdb, ctx) { await xdb.query("CREATE TABLE ..."); },
17
+ * down: async function (xdb, ctx) { await xdb.query("DROP TABLE ..."); },
18
+ * };
19
+ *
20
+ * `xdb` exposes `.query(sql, params) → { rows, rowCount }`. `ctx` carries
21
+ * `{ externalDb, backendName }` so migrations that need backend-introspection
22
+ * (e.g. `b.db.declareView()`-shaped specs) can call back into the framework.
23
+ *
24
+ * `up` is required; `down` is optional. Calling `down()` on a migration
25
+ * that didn't export `down()` surfaces a clear error.
26
+ *
27
+ * var migrate = b.externalDb.migrate.create({
28
+ * dir: "./migrations-pg",
29
+ * backend: "main", // optional; defaults to default backend
30
+ * audit: b.audit, // optional
31
+ * });
32
+ *
33
+ * await migrate.up(); // → { applied: [name], skipped: [name] }
34
+ * await migrate.down({ steps: 1 });
35
+ * migrate.status(); // → { applied: [{name, description, appliedAt}], pending: [name], total }
36
+ *
37
+ * Concurrent-apply protection: a single-row advisory lock in
38
+ * `_blamejs_externaldb_migrations_lock` ensures two processes can't apply
39
+ * the same wave concurrently. The losing process gets a clear "lock held
40
+ * by other process" error. Stale locks can be force-replaced via
41
+ * `staleAfterMs`.
42
+ *
43
+ * Audit emissions when wired with `audit: b.audit`:
44
+ * - externaldb.migrate.up.success { migration, durationMs }
45
+ * - externaldb.migrate.up.failure { migration, durationMs, reason }
46
+ * - externaldb.migrate.down.success { migration, durationMs }
47
+ * - externaldb.migrate.down.failure { migration, durationMs, reason }
48
+ * - externaldb.migrate.lock.acquired { holder }
49
+ * - externaldb.migrate.lock.released { holder }
50
+ */
51
+ var path = require("path");
52
+ var atomicFile = require("./atomic-file");
53
+ var lazyRequire = require("./lazy-require");
54
+ var migrationFiles = require("./migration-files");
55
+ var numericBounds = require("./numeric-bounds");
56
+ var validateOpts = require("./validate-opts");
57
+ var { defineClass } = require("./framework-error");
58
+
59
+ var ExternalDbMigrateError = defineClass("ExternalDbMigrateError", { alwaysPermanent: true });
60
+
61
+ // Lazy require — external-db imports back into this module via its
62
+ // public `migrate` namespace; load-order would cycle without lazy.
63
+ var externalDbModule = lazyRequire(function () { return require("./external-db"); });
64
+
65
+ var TRACKING_TABLE = "_blamejs_externaldb_migrations";
66
+ var LOCK_TABLE = "_blamejs_externaldb_migrations_lock";
67
+ // Identifiers wrapped in `"..."` per project convention so a reserved-word
68
+ // or whitespace-bearing name resolves correctly.
69
+ var Q_TRACKING = '"' + TRACKING_TABLE + '"';
70
+ var Q_LOCK = '"' + LOCK_TABLE + '"';
71
+
72
+ // Filename grammar lives in lib/migration-files (shared with the local
73
+ // migrations.js + seeders.js runners). Length capped before the regex
74
+ // test so a hostile directory listing can't drive the engine against
75
+ // an unbounded filename string.
76
+ var FILE_NAME_MAX = 255;
77
+
78
+ function _isMigrationFile(name) {
79
+ return typeof name === "string" &&
80
+ name.length > 0 &&
81
+ name.length <= FILE_NAME_MAX &&
82
+ migrationFiles.MIGRATION_FILE_RE.test(name);
83
+ }
84
+
85
+ function _err(code, message) {
86
+ return new ExternalDbMigrateError(code, message);
87
+ }
88
+
89
+ function _lockHolderId() {
90
+ return String(process.pid) + "@" + (require("node:os").hostname() || "unknown");
91
+ }
92
+
93
+ async function _ensureTrackingTable(xdb) {
94
+ // Tracking table holds the migration history. ISO-8601 timestamp
95
+ // strings (TEXT) keep the framework's tracking table portable across
96
+ // Postgres/SQLite without dialect-specific type juggling — operators
97
+ // who want strict TIMESTAMPTZ for their own ad-hoc queries against
98
+ // the table ALTER it post-creation.
99
+ await xdb.query(
100
+ "CREATE TABLE IF NOT EXISTS " + Q_TRACKING + " (" +
101
+ " name TEXT PRIMARY KEY," +
102
+ " description TEXT," +
103
+ " appliedAt TEXT NOT NULL" +
104
+ ")",
105
+ []
106
+ );
107
+ }
108
+
109
+ async function _ensureLockTable(xdb) {
110
+ await xdb.query(
111
+ "CREATE TABLE IF NOT EXISTS " + Q_LOCK + " (" +
112
+ " scope TEXT PRIMARY KEY," +
113
+ " lockedAt INTEGER NOT NULL," +
114
+ " lockedBy TEXT NOT NULL," +
115
+ " CHECK (scope = 'lock')" +
116
+ ")",
117
+ []
118
+ );
119
+ }
120
+
121
+ // ---- Lock acquire / release ----
122
+
123
+ async function _acquireLock(xdb, opts) {
124
+ await _ensureLockTable(xdb);
125
+ var holder = _lockHolderId();
126
+ var nowMs = Date.now();
127
+ // See migrations.acquireLock for the same fix — Infinity was
128
+ // silently identical to 0 (no staleness check) but obscured the typo.
129
+ var staleAfterMs = 0;
130
+ if (opts) {
131
+ numericBounds.requireNonNegativeFiniteIntIfPresent(opts.staleAfterMs,
132
+ "externalDb.migrate.acquireLock: staleAfterMs",
133
+ ExternalDbMigrateError, "externalDb-migrate/bad-opt");
134
+ if (opts.staleAfterMs !== undefined) staleAfterMs = opts.staleAfterMs;
135
+ }
136
+ try {
137
+ await xdb.query(
138
+ "INSERT INTO " + Q_LOCK + " (scope, lockedAt, lockedBy) VALUES ('lock', $1, $2)",
139
+ [nowMs, holder]
140
+ );
141
+ return holder;
142
+ } catch (_e) {
143
+ // PRIMARY KEY conflict → existing lock. Inspect it.
144
+ var existingRes = await xdb.query(
145
+ "SELECT lockedAt, lockedBy FROM " + Q_LOCK + " WHERE scope = 'lock'",
146
+ []
147
+ );
148
+ var existing = existingRes && existingRes.rows && existingRes.rows[0];
149
+ if (!existing) {
150
+ try {
151
+ await xdb.query(
152
+ "INSERT INTO " + Q_LOCK + " (scope, lockedAt, lockedBy) VALUES ('lock', $1, $2)",
153
+ [nowMs, holder]
154
+ );
155
+ return holder;
156
+ } catch (e2) {
157
+ throw _err("externaldb-migrate/lock-busy",
158
+ "could not acquire migration lock: " + ((e2 && e2.message) || String(e2)));
159
+ }
160
+ }
161
+ var ageMs = nowMs - Number(existing.lockedat || existing.lockedAt);
162
+ if (staleAfterMs > 0 && ageMs > staleAfterMs) {
163
+ // Force-replace the stale lock atomically.
164
+ await xdb.query(
165
+ "DELETE FROM " + Q_LOCK + " WHERE scope = 'lock' AND lockedAt = $1",
166
+ [Number(existing.lockedat || existing.lockedAt)]
167
+ );
168
+ await xdb.query(
169
+ "INSERT INTO " + Q_LOCK + " (scope, lockedAt, lockedBy) VALUES ('lock', $1, $2)",
170
+ [nowMs, holder]
171
+ );
172
+ return holder;
173
+ }
174
+ throw _err("externaldb-migrate/lock-held",
175
+ "migration lock is held by " + (existing.lockedby || existing.lockedBy) +
176
+ " (acquired " + ageMs + "ms ago). Another process is running migrations" +
177
+ " — wait for it to finish, or pass staleAfterMs to force-replace stale locks.");
178
+ }
179
+ }
180
+
181
+ async function _releaseLock(xdb, holder) {
182
+ try {
183
+ await xdb.query(
184
+ "DELETE FROM " + Q_LOCK + " WHERE scope = 'lock' AND lockedBy = $1",
185
+ [holder]
186
+ );
187
+ } catch (_e) {
188
+ // best-effort release; operator can DELETE manually.
189
+ }
190
+ }
191
+
192
+ // ---- File loading ----
193
+
194
+ function _list(dir) {
195
+ return atomicFile.listDir(dir, {
196
+ filter: _isMigrationFile,
197
+ }).map(function (e) { return e.name; }).sort();
198
+ }
199
+
200
+ function _loadMigration(file, dir) {
201
+ var fullPath = path.join(dir, file);
202
+ // Drop the require cache so a test/dev that edits a file picks up the
203
+ // new content. Matches lib/migrations.js semantics.
204
+ try { delete require.cache[require.resolve(fullPath)]; } catch (_e) { /* not yet cached */ }
205
+ var mod;
206
+ try { mod = require(fullPath); }
207
+ catch (e) {
208
+ throw _err("externaldb-migrate/load-failed",
209
+ "migration '" + file + "' failed to load: " + ((e && e.message) || String(e)));
210
+ }
211
+ if (!mod || typeof mod.up !== "function") {
212
+ throw _err("externaldb-migrate/missing-up",
213
+ "migration '" + file + "' must export an `up(xdb, ctx)` function");
214
+ }
215
+ return mod;
216
+ }
217
+
218
+ // ---- Audit emit (drop-silent) ----
219
+
220
+ function _emit(audit, action, outcome, info, reason) {
221
+ if (!audit) return;
222
+ try {
223
+ audit.safeEmit({
224
+ action: action,
225
+ outcome: outcome,
226
+ metadata: info || {},
227
+ reason: reason || null,
228
+ });
229
+ } catch (_e) { /* drop-silent — audit emit failure must not crash the migration */ }
230
+ }
231
+
232
+ // ---- Backend resolution ----
233
+
234
+ function _resolveBackendName(opts) {
235
+ if (opts && typeof opts.backend === "string" && opts.backend.length > 0) {
236
+ return opts.backend;
237
+ }
238
+ // Default to the externalDb's defaultBackend; throw clear if not initialized.
239
+ var listed;
240
+ try { listed = externalDbModule().listBackends(); }
241
+ catch (_e) {
242
+ throw _err("externaldb-migrate/not-initialized",
243
+ "externalDb is not initialized — call b.externalDb.init({ backends }) first");
244
+ }
245
+ if (!listed || listed.length === 0) {
246
+ throw _err("externaldb-migrate/no-backends",
247
+ "externalDb has no backends configured");
248
+ }
249
+ return listed[0].name;
250
+ }
251
+
252
+ // ---- Public factory ----
253
+
254
+ function create(opts) {
255
+ opts = opts || {};
256
+ validateOpts(opts, ["dir", "backend", "audit", "staleAfterMs"], "b.externalDb.migrate");
257
+ validateOpts.requireNonEmptyString(opts.dir, "externalDb.migrate.create: opts.dir (path to migrations directory)", ExternalDbMigrateError, "externaldb-migrate/no-dir");
258
+ validateOpts.optionalFiniteNonNegative(opts.staleAfterMs, "externalDb.migrate: staleAfterMs", ExternalDbMigrateError, "externaldb-migrate/bad-stale");
259
+ validateOpts.auditShape(opts.audit, "externalDb.migrate", ExternalDbMigrateError, "externaldb-migrate/bad-audit");
260
+ var dir = opts.dir;
261
+ var audit = opts.audit || null;
262
+
263
+ function _ctx(backendName) {
264
+ return {
265
+ externalDb: externalDbModule(),
266
+ backendName: backendName,
267
+ };
268
+ }
269
+
270
+ async function status() {
271
+ var backendName = _resolveBackendName(opts);
272
+ return await externalDbModule().transaction(async function (xdb) {
273
+ await _ensureTrackingTable(xdb);
274
+ var res = await xdb.query(
275
+ "SELECT name, description, appliedAt FROM " + Q_TRACKING +
276
+ " ORDER BY appliedAt ASC, name ASC",
277
+ []
278
+ );
279
+ var applied = (res && res.rows) || [];
280
+ var appliedNames = new Set(applied.map(function (r) { return r.name; }));
281
+ var files = _list(dir);
282
+ var pending = files.filter(function (f) { return !appliedNames.has(f); });
283
+ return {
284
+ applied: applied,
285
+ pending: pending,
286
+ total: files.length,
287
+ backend: backendName,
288
+ };
289
+ }, { backend: backendName });
290
+ }
291
+
292
+ async function up() {
293
+ var backendName = _resolveBackendName(opts);
294
+ var ctx = _ctx(backendName);
295
+
296
+ return await externalDbModule().transaction(async function (xdb) {
297
+ await _ensureTrackingTable(xdb);
298
+ await _ensureLockTable(xdb);
299
+ }, { backend: backendName }).then(async function () {
300
+ // Acquire the lock OUTSIDE the per-migration transaction so the
301
+ // lock survives across migration boundaries. We use a separate
302
+ // pool acquisition for the lock connection — the migrate runner
303
+ // serializes apply order, so this single-connection lock is
304
+ // sufficient.
305
+ var lockHolder = await externalDbModule().transaction(async function (xdb) {
306
+ return await _acquireLock(xdb, opts);
307
+ }, { backend: backendName });
308
+
309
+ _emit(audit, "externaldb.migrate.lock.acquired", "success",
310
+ { holder: lockHolder, backend: backendName }, null);
311
+
312
+ try {
313
+ var appliedRes = await externalDbModule().query(
314
+ "SELECT name FROM " + Q_TRACKING, [], { backend: backendName }
315
+ );
316
+ var appliedSet = new Set(((appliedRes && appliedRes.rows) || []).map(function (r) { return r.name; }));
317
+ var files = _list(dir);
318
+ var applied = [];
319
+ var skipped = [];
320
+
321
+ for (var i = 0; i < files.length; i++) {
322
+ var file = files[i];
323
+ if (appliedSet.has(file)) { skipped.push(file); continue; }
324
+ var mod = _loadMigration(file, dir);
325
+ var t0 = Date.now();
326
+ try {
327
+ await externalDbModule().transaction(async function (xdb) {
328
+ await mod.up(xdb, ctx);
329
+ await xdb.query(
330
+ "INSERT INTO " + Q_TRACKING +
331
+ " (name, description, appliedAt) VALUES ($1, $2, $3)",
332
+ [file, mod.description || "", new Date().toISOString()]
333
+ );
334
+ }, { backend: backendName });
335
+ _emit(audit, "externaldb.migrate.up", "success",
336
+ { migration: file, durationMs: Date.now() - t0, backend: backendName }, null);
337
+ applied.push(file);
338
+ } catch (e) {
339
+ _emit(audit, "externaldb.migrate.up", "failure",
340
+ { migration: file, durationMs: Date.now() - t0, backend: backendName },
341
+ (e && e.message) || String(e));
342
+ throw _err("externaldb-migrate/up-failed",
343
+ "migration '" + file + "' failed to apply: " + ((e && e.message) || String(e)));
344
+ }
345
+ }
346
+ return { applied: applied, skipped: skipped, backend: backendName };
347
+ } finally {
348
+ try {
349
+ await externalDbModule().transaction(async function (xdb) {
350
+ await _releaseLock(xdb, lockHolder);
351
+ }, { backend: backendName });
352
+ _emit(audit, "externaldb.migrate.lock.released", "success",
353
+ { holder: lockHolder, backend: backendName }, null);
354
+ } catch (_e) { /* best-effort release; emit a failure audit */
355
+ _emit(audit, "externaldb.migrate.lock.released", "failure",
356
+ { holder: lockHolder, backend: backendName }, "release failed");
357
+ }
358
+ }
359
+ });
360
+ }
361
+
362
+ async function down(downOpts) {
363
+ downOpts = downOpts || {};
364
+ var steps = (typeof downOpts.steps === "number" && downOpts.steps > 0)
365
+ ? Math.floor(downOpts.steps) : 1;
366
+ var backendName = _resolveBackendName(opts);
367
+ var ctx = _ctx(backendName);
368
+
369
+ await externalDbModule().transaction(async function (xdb) {
370
+ await _ensureTrackingTable(xdb);
371
+ await _ensureLockTable(xdb);
372
+ }, { backend: backendName });
373
+
374
+ var lockHolder = await externalDbModule().transaction(async function (xdb) {
375
+ return await _acquireLock(xdb, opts);
376
+ }, { backend: backendName });
377
+
378
+ _emit(audit, "externaldb.migrate.lock.acquired", "success",
379
+ { holder: lockHolder, backend: backendName }, null);
380
+
381
+ try {
382
+ var appliedRes = await externalDbModule().query(
383
+ "SELECT name FROM " + Q_TRACKING + " ORDER BY appliedAt DESC, name DESC LIMIT $1",
384
+ [steps], { backend: backendName }
385
+ );
386
+ var rows = (appliedRes && appliedRes.rows) || [];
387
+ var reverted = [];
388
+ for (var i = 0; i < rows.length; i++) {
389
+ var file = rows[i].name;
390
+ var mod = _loadMigration(file, dir);
391
+ if (typeof mod.down !== "function") {
392
+ throw _err("externaldb-migrate/no-down",
393
+ "migration '" + file + "' has no down() — write one or restore from backup");
394
+ }
395
+ var t0 = Date.now();
396
+ try {
397
+ await externalDbModule().transaction(async function (xdb) {
398
+ await mod.down(xdb, ctx);
399
+ await xdb.query(
400
+ "DELETE FROM " + Q_TRACKING + " WHERE name = $1",
401
+ [file]
402
+ );
403
+ }, { backend: backendName });
404
+ _emit(audit, "externaldb.migrate.down", "success",
405
+ { migration: file, durationMs: Date.now() - t0, backend: backendName }, null);
406
+ reverted.push(file);
407
+ } catch (e) {
408
+ _emit(audit, "externaldb.migrate.down", "failure",
409
+ { migration: file, durationMs: Date.now() - t0, backend: backendName },
410
+ (e && e.message) || String(e));
411
+ throw _err("externaldb-migrate/down-failed",
412
+ "migration '" + file + "' failed to roll back: " + ((e && e.message) || String(e)));
413
+ }
414
+ }
415
+ return { reverted: reverted, backend: backendName };
416
+ } finally {
417
+ try {
418
+ await externalDbModule().transaction(async function (xdb) {
419
+ await _releaseLock(xdb, lockHolder);
420
+ }, { backend: backendName });
421
+ _emit(audit, "externaldb.migrate.lock.released", "success",
422
+ { holder: lockHolder, backend: backendName }, null);
423
+ } catch (_e) {
424
+ _emit(audit, "externaldb.migrate.lock.released", "failure",
425
+ { holder: lockHolder, backend: backendName }, "release failed");
426
+ }
427
+ }
428
+ }
429
+
430
+ return {
431
+ up: up,
432
+ down: down,
433
+ status: status,
434
+ };
435
+ }
436
+
437
+ module.exports = {
438
+ create: create,
439
+ ExternalDbMigrateError: ExternalDbMigrateError,
440
+ TRACKING_TABLE: TRACKING_TABLE,
441
+ };