@blamejs/core 0.4.1

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 (160) hide show
  1. package/CHANGELOG.md +230 -0
  2. package/LICENSE +201 -0
  3. package/LTS-CALENDAR.md +29 -0
  4. package/MIGRATING.md +7 -0
  5. package/NOTICE +59 -0
  6. package/README.md +100 -0
  7. package/bin/blamejs.js +13 -0
  8. package/index.js +253 -0
  9. package/lib/api-key.js +705 -0
  10. package/lib/api-snapshot.js +335 -0
  11. package/lib/app-shutdown.js +381 -0
  12. package/lib/app.js +364 -0
  13. package/lib/atomic-file.js +525 -0
  14. package/lib/audit-chain.js +168 -0
  15. package/lib/audit-sign.js +319 -0
  16. package/lib/audit-tools.js +682 -0
  17. package/lib/audit.js +753 -0
  18. package/lib/auth/jwt.js +280 -0
  19. package/lib/auth/oauth.js +691 -0
  20. package/lib/auth/passkey.js +185 -0
  21. package/lib/auth/password.js +139 -0
  22. package/lib/auth/totp.js +17 -0
  23. package/lib/auth-header.js +81 -0
  24. package/lib/backup/bundle.js +219 -0
  25. package/lib/backup/crypto.js +174 -0
  26. package/lib/backup/index.js +490 -0
  27. package/lib/backup/manifest.js +275 -0
  28. package/lib/bundler.js +295 -0
  29. package/lib/cache.js +819 -0
  30. package/lib/chain-writer.js +234 -0
  31. package/lib/cli-helpers.js +201 -0
  32. package/lib/cli.js +1377 -0
  33. package/lib/cluster-provider-db.js +245 -0
  34. package/lib/cluster-storage.js +166 -0
  35. package/lib/cluster.js +691 -0
  36. package/lib/consent.js +222 -0
  37. package/lib/constants.js +186 -0
  38. package/lib/cookies.js +293 -0
  39. package/lib/credential-hash.js +303 -0
  40. package/lib/crypto-field.js +159 -0
  41. package/lib/crypto.js +250 -0
  42. package/lib/db-query.js +297 -0
  43. package/lib/db-schema.js +250 -0
  44. package/lib/db.js +1054 -0
  45. package/lib/deprecate.js +226 -0
  46. package/lib/dev.js +324 -0
  47. package/lib/error-page.js +424 -0
  48. package/lib/events.js +135 -0
  49. package/lib/external-db.js +422 -0
  50. package/lib/forms.js +378 -0
  51. package/lib/framework-error.js +189 -0
  52. package/lib/framework-schema.js +604 -0
  53. package/lib/handlers.js +350 -0
  54. package/lib/html-balance.js +227 -0
  55. package/lib/http-client.js +615 -0
  56. package/lib/i18n.js +780 -0
  57. package/lib/jobs.js +181 -0
  58. package/lib/lazy-require.js +48 -0
  59. package/lib/log-stream-local.js +137 -0
  60. package/lib/log-stream-webhook.js +170 -0
  61. package/lib/log-stream.js +211 -0
  62. package/lib/log.js +355 -0
  63. package/lib/mail-bounce.js +507 -0
  64. package/lib/mail.js +701 -0
  65. package/lib/metrics.js +647 -0
  66. package/lib/middleware/api-encrypt.js +553 -0
  67. package/lib/middleware/attach-user.js +156 -0
  68. package/lib/middleware/body-parser.js +883 -0
  69. package/lib/middleware/bot-guard.js +148 -0
  70. package/lib/middleware/compression.js +436 -0
  71. package/lib/middleware/cors.js +236 -0
  72. package/lib/middleware/csp-nonce.js +332 -0
  73. package/lib/middleware/csrf-protect.js +275 -0
  74. package/lib/middleware/error-handler.js +46 -0
  75. package/lib/middleware/health.js +358 -0
  76. package/lib/middleware/index.js +52 -0
  77. package/lib/middleware/rate-limit.js +319 -0
  78. package/lib/middleware/request-id.js +53 -0
  79. package/lib/middleware/require-auth.js +95 -0
  80. package/lib/middleware/security-headers.js +91 -0
  81. package/lib/migrations.js +353 -0
  82. package/lib/mtls-ca.js +333 -0
  83. package/lib/mtls-engine-default.js +285 -0
  84. package/lib/nonce-store.js +177 -0
  85. package/lib/notify.js +643 -0
  86. package/lib/ntp-check.js +178 -0
  87. package/lib/object-store/azure-blob.js +467 -0
  88. package/lib/object-store/gcs.js +469 -0
  89. package/lib/object-store/http-put.js +153 -0
  90. package/lib/object-store/index.js +140 -0
  91. package/lib/object-store/local.js +163 -0
  92. package/lib/object-store/retry.js +15 -0
  93. package/lib/object-store/sigv4.js +535 -0
  94. package/lib/observability.js +114 -0
  95. package/lib/pagination.js +371 -0
  96. package/lib/parsers/index.js +64 -0
  97. package/lib/parsers/safe-csv.js +224 -0
  98. package/lib/parsers/safe-env.js +614 -0
  99. package/lib/parsers/safe-toml.js +745 -0
  100. package/lib/parsers/safe-xml.js +379 -0
  101. package/lib/parsers/safe-yaml.js +977 -0
  102. package/lib/permissions.js +430 -0
  103. package/lib/pqc-agent.js +85 -0
  104. package/lib/pqc-gate.js +266 -0
  105. package/lib/protocol-dispatcher.js +144 -0
  106. package/lib/queue-local.js +327 -0
  107. package/lib/queue.js +430 -0
  108. package/lib/redact.js +192 -0
  109. package/lib/render.js +193 -0
  110. package/lib/request-helpers.js +178 -0
  111. package/lib/restore-bundle.js +239 -0
  112. package/lib/restore-rollback.js +254 -0
  113. package/lib/restore.js +301 -0
  114. package/lib/retry.js +329 -0
  115. package/lib/router.js +437 -0
  116. package/lib/safe-async.js +520 -0
  117. package/lib/safe-buffer.js +162 -0
  118. package/lib/safe-json.js +532 -0
  119. package/lib/safe-schema.js +1176 -0
  120. package/lib/safe-sql.js +157 -0
  121. package/lib/safe-url.js +109 -0
  122. package/lib/scheduler.js +680 -0
  123. package/lib/seeders.js +622 -0
  124. package/lib/session.js +304 -0
  125. package/lib/slug.js +243 -0
  126. package/lib/static.js +268 -0
  127. package/lib/storage.js +470 -0
  128. package/lib/subject.js +281 -0
  129. package/lib/template.js +781 -0
  130. package/lib/testing.js +621 -0
  131. package/lib/totp.js +285 -0
  132. package/lib/tracing.js +484 -0
  133. package/lib/validate-opts.js +56 -0
  134. package/lib/vault/index.js +299 -0
  135. package/lib/vault/passphrase-ops.js +311 -0
  136. package/lib/vault/passphrase-source.js +198 -0
  137. package/lib/vault/rotate.js +761 -0
  138. package/lib/vault/wrap.js +289 -0
  139. package/lib/vendor/MANIFEST.json +84 -0
  140. package/lib/vendor/argon2/argon2.cjs +466 -0
  141. package/lib/vendor/argon2/argon2.d.cts +62 -0
  142. package/lib/vendor/argon2/package.json +1 -0
  143. package/lib/vendor/argon2/prebuilds/darwin-arm64/argon2.armv8.glibc.node +0 -0
  144. package/lib/vendor/argon2/prebuilds/darwin-x64/argon2.glibc.node +0 -0
  145. package/lib/vendor/argon2/prebuilds/freebsd-arm64/argon2.armv8.glibc.node +0 -0
  146. package/lib/vendor/argon2/prebuilds/freebsd-x64/argon2.glibc.node +0 -0
  147. package/lib/vendor/argon2/prebuilds/linux-arm/argon2.armv7.glibc.node +0 -0
  148. package/lib/vendor/argon2/prebuilds/linux-arm/argon2.armv7.musl.node +0 -0
  149. package/lib/vendor/argon2/prebuilds/linux-arm64/argon2.armv8.glibc.node +0 -0
  150. package/lib/vendor/argon2/prebuilds/linux-arm64/argon2.armv8.musl.node +0 -0
  151. package/lib/vendor/argon2/prebuilds/linux-x64/argon2.glibc.node +0 -0
  152. package/lib/vendor/argon2/prebuilds/linux-x64/argon2.musl.node +0 -0
  153. package/lib/vendor/argon2/prebuilds/win32-x64/argon2.glibc.node +0 -0
  154. package/lib/vendor/noble-ciphers.cjs +9 -0
  155. package/lib/vendor/pki.cjs +181 -0
  156. package/lib/vendor/simplewebauthn-server.cjs +328 -0
  157. package/lib/webhook.js +632 -0
  158. package/lib/websocket-channels.js +413 -0
  159. package/lib/websocket.js +833 -0
  160. package/package.json +39 -0
@@ -0,0 +1,525 @@
1
+ "use strict";
2
+ /**
3
+ * Atomic file I/O with integrity verification, retry on transient errors,
4
+ * and cross-process locking.
5
+ *
6
+ * The framework already does atomic writes for vault.key.sealed (lib/vault.js)
7
+ * and audit.tip (lib/db.js). This module exposes the same primitives for
8
+ * any caller that needs:
9
+ *
10
+ * - Crash-safe writes via temp + fsync + atomic rename + dir fsync
11
+ * - Optional integrity hash (SHA3-512) computed on write, verified on read
12
+ * - Retry on EBUSY / EAGAIN / ENFILE with exponential backoff
13
+ * - Cross-process locking for read-modify-write sequences
14
+ * - JSON convenience wrappers using b.json's security defaults
15
+ *
16
+ * The framework's "fail closed" stance applies: a partially-written file
17
+ * NEVER survives a crash to the caller — either the new contents are
18
+ * fully on disk (atomic rename succeeded) or the original (or absence)
19
+ * remains. fsync calls are best-effort across platforms (Windows rejects
20
+ * directory fsync, etc.); the rename remains atomic at the FS level
21
+ * regardless.
22
+ *
23
+ * Public API:
24
+ * atomicFile.write(filepath, data, opts?) → { bytesWritten, hash? }
25
+ * atomicFile.read(filepath, opts?) → Buffer (or string if encoding)
26
+ * atomicFile.readSync(filepath, opts?) → same, sync (for boot paths)
27
+ * atomicFile.writeJson(filepath, value, opts?) → { bytesWritten, hash? }
28
+ * atomicFile.readJson(filepath, opts?) → parsed value
29
+ * atomicFile.copy(src, dst, opts?) → { bytesWritten, hash? }
30
+ * atomicFile.exists(filepath) → boolean
31
+ * atomicFile.lock(filepath, fn, opts?) → fn's return value
32
+ * atomicFile.AtomicFileError → error class
33
+ */
34
+ var fs = require("fs");
35
+ var path = require("path");
36
+ var { generateToken, sha3Hash } = require("./crypto");
37
+ var safeJson = require("./safe-json");
38
+ var C = require("./constants");
39
+ var safeBuffer = require("./safe-buffer");
40
+ var safeAsync = require("./safe-async");
41
+ var retry = require("./retry");
42
+ var { FrameworkError } = require("./framework-error");
43
+
44
+ var DEFAULTS = {
45
+ maxBytes: C.BYTES.mib(64), // 64 MiB ceiling on read
46
+ retryAttempts: 5,
47
+ retryBaseMs: 50,
48
+ retryMaxMs: C.TIME.seconds(2),
49
+ fileMode: 0o600,
50
+ computeHash: false,
51
+ lockTimeoutMs: C.TIME.seconds(30),
52
+ lockPollMs: 50,
53
+ };
54
+
55
+ class AtomicFileError extends FrameworkError {
56
+ constructor(message, code) {
57
+ super(message);
58
+ this.name = "AtomicFileError";
59
+ this.code = code || "atomic-file/error";
60
+ this.isAtomicFileError = true;
61
+ }
62
+ }
63
+
64
+ // ---- Retry helper for transient FS errors ----
65
+ //
66
+ // Routes through b.retry.withRetry with an FS-specific classifier — the
67
+ // errnos that mean "try again" on Linux/macOS/Windows file systems differ
68
+ // from the HTTP/network-shaped default classifier. atomic-file's public
69
+ // opts (retryAttempts/retryBaseMs/retryMaxMs) are mapped to the retry
70
+ // primitive's standard names; jitterFactor 0.5 reproduces the original
71
+ // `delay * (0.5 + Math.random()/2)` range of [delay/2, delay].
72
+
73
+ var TRANSIENT_FS_ERRNOS = new Set(["EBUSY", "EAGAIN", "ENFILE", "EMFILE", "EPERM"]);
74
+
75
+ function _isFsRetryable(e) {
76
+ return e != null && TRANSIENT_FS_ERRNOS.has(e.code);
77
+ }
78
+
79
+ async function _withRetry(fn, opts) {
80
+ opts = Object.assign({}, DEFAULTS, opts || {});
81
+ return retry.withRetry(function () { return fn(); }, {
82
+ maxAttempts: opts.retryAttempts,
83
+ baseDelayMs: opts.retryBaseMs,
84
+ maxDelayMs: opts.retryMaxMs,
85
+ jitterFactor: 0.5,
86
+ isRetryable: _isFsRetryable,
87
+ signal: opts.signal,
88
+ });
89
+ }
90
+
91
+ // ---- Sync helpers (best effort) ----
92
+ //
93
+ // fsync / fsyncDir / ensureDir / copyDirRecursive / pathTimestamp are
94
+ // public surface — they were previously underscore-prefixed, and other
95
+ // modules (vault-passphrase-ops, vault-rotate, backup-bundle, restore-
96
+ // bundle, restore-rollback, bundler) duplicated them inline. Hoisted
97
+ // here so the framework has one shared implementation per concern.
98
+
99
+ function fsync(fd) {
100
+ try { fs.fsyncSync(fd); } catch (_e) { /* not all platforms support fsync on every fd type */ }
101
+ }
102
+
103
+ function fsyncDir(dirPath) {
104
+ try {
105
+ var fd = fs.openSync(dirPath, "r");
106
+ try { fs.fsyncSync(fd); } catch (_e) { /* Windows rejects directory fsync */ }
107
+ finally { fs.closeSync(fd); }
108
+ } catch (_e) { /* dir fsync is best-effort across filesystems */ }
109
+ }
110
+
111
+ // Internal aliases so existing code in this file keeps working
112
+ function _fsync(fd) { return fsync(fd); }
113
+ function _fsyncDir(dirPath) { return fsyncDir(dirPath); }
114
+
115
+ // ensureDir — mkdirSync with recursive: true and a default mode of
116
+ // 0o700 (owner-only) suitable for framework data directories. Caller
117
+ // passes a different mode for less-restricted dirs.
118
+ function ensureDir(dirPath, mode) {
119
+ if (typeof dirPath !== "string" || dirPath.length === 0) {
120
+ throw new AtomicFileError("ensureDir: path must be a non-empty string", "atomic-file/bad-path");
121
+ }
122
+ fs.mkdirSync(dirPath, { recursive: true, mode: typeof mode === "number" ? mode : 0o700 });
123
+ return dirPath;
124
+ }
125
+
126
+ // copyDirRecursive — synchronous, file-by-file copy that mirrors the
127
+ // source's directory structure. Skips symlinks (operator wanting symlink
128
+ // preservation should use a real archive tool). Refuses to overwrite
129
+ // existing files at dest by default — pass opts.overwrite=true to
130
+ // replace. dest is created with mode 0o700 by default.
131
+ function copyDirRecursive(src, dest, opts) {
132
+ if (typeof src !== "string" || src.length === 0) {
133
+ throw new AtomicFileError("copyDirRecursive: src must be a non-empty string", "atomic-file/bad-path");
134
+ }
135
+ if (typeof dest !== "string" || dest.length === 0) {
136
+ throw new AtomicFileError("copyDirRecursive: dest must be a non-empty string", "atomic-file/bad-path");
137
+ }
138
+ if (!fs.existsSync(src)) {
139
+ throw new AtomicFileError("copyDirRecursive: src does not exist: " + src, "atomic-file/missing-src");
140
+ }
141
+ opts = opts || {};
142
+ var dirMode = typeof opts.dirMode === "number" ? opts.dirMode : 0o700;
143
+ var overwrite = !!opts.overwrite;
144
+ var copyFlags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
145
+
146
+ ensureDir(dest, dirMode);
147
+ var entries = fs.readdirSync(src, { withFileTypes: true });
148
+ var fileCount = 0;
149
+ var byteCount = 0;
150
+ for (var i = 0; i < entries.length; i++) {
151
+ var name = entries[i].name;
152
+ var s = path.join(src, name);
153
+ var d = path.join(dest, name);
154
+ if (entries[i].isDirectory()) {
155
+ var sub = copyDirRecursive(s, d, opts);
156
+ fileCount += sub.fileCount;
157
+ byteCount += sub.byteCount;
158
+ } else if (entries[i].isFile()) {
159
+ fs.copyFileSync(s, d, copyFlags);
160
+ try { byteCount += fs.statSync(d).size; } catch (_e) { /* size best-effort */ }
161
+ fileCount++;
162
+ }
163
+ // Symlinks, sockets, devices: deliberately skipped
164
+ }
165
+ return { fileCount: fileCount, byteCount: byteCount };
166
+ }
167
+
168
+ // pathTimestamp — filesystem-safe ISO-8601 timestamp suitable for use as
169
+ // a directory or file name on every platform. Standard
170
+ // Date.toISOString() embeds ':' and '.' which Windows reserves for
171
+ // drive letters and extension separators. This helper substitutes both
172
+ // with '-' so the result works as a path segment unmodified. String
173
+ // sort still gives chronological order.
174
+ //
175
+ // atomicFile.pathTimestamp()
176
+ // → "2026-04-27T14-00-00-123Z"
177
+ // atomicFile.pathTimestamp(new Date(0))
178
+ // → "1970-01-01T00-00-00-000Z"
179
+ function pathTimestamp(date) {
180
+ var d = (date instanceof Date) ? date : new Date();
181
+ return d.toISOString().replace(/[:.]/g, "-");
182
+ }
183
+
184
+ // ---- writeSync ----
185
+ // Synchronous atomic write — same temp+fsync+rename+dirfsync flow as
186
+ // async write(), but without the retry loop (which requires awaits).
187
+ // Use this from sync code paths (process exit handlers, module-load-time
188
+ // bootstraps). For everything else, prefer the async write().
189
+ //
190
+ // Transactional guarantee: either the rename completes (new contents fully
191
+ // visible) or the tmp file is removed (no state change). The caller never
192
+ // sees a half-written file at `filepath` and never leaves a tmp orphan
193
+ // from the current call's failure path.
194
+ function writeSync(filepath, data, opts) {
195
+ opts = Object.assign({}, DEFAULTS, opts || {});
196
+ var buf = safeBuffer.toBuffer(data, {
197
+ errorClass: AtomicFileError,
198
+ typeCode: "atomic-file/invalid-data",
199
+ typeMessage: "data must be Buffer, Uint8Array, or string",
200
+ });
201
+
202
+ var dir = path.dirname(filepath);
203
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
204
+
205
+ var tmpPath = filepath + ".tmp-" + generateToken(8);
206
+ var renamed = false;
207
+ try {
208
+ var fd = fs.openSync(tmpPath, "w", opts.fileMode);
209
+ try {
210
+ var pos = 0;
211
+ while (pos < buf.length) {
212
+ pos += fs.writeSync(fd, buf, pos, buf.length - pos, null);
213
+ }
214
+ _fsync(fd);
215
+ } finally {
216
+ try { fs.closeSync(fd); } catch (_e) { /* already closed? */ }
217
+ }
218
+ fs.renameSync(tmpPath, filepath);
219
+ renamed = true;
220
+ _fsyncDir(dir);
221
+ } finally {
222
+ if (!renamed) {
223
+ // Either the write or the rename failed — remove the tmp so the next
224
+ // boot doesn't see a leaked partial file.
225
+ try { fs.unlinkSync(tmpPath); } catch (_e) { /* may not exist */ }
226
+ }
227
+ }
228
+
229
+ return {
230
+ bytesWritten: buf.length,
231
+ hash: opts.computeHash ? sha3Hash(buf) : null,
232
+ };
233
+ }
234
+
235
+ // Clean up orphan tmp files left behind by a previously-crashed process.
236
+ // Atomic writes use random tmp names (filepath + ".tmp-" + token), so a
237
+ // crash leaves a file with a name we can't predict on next boot — only
238
+ // glob and prune by age. Default: prune anything older than 5 minutes.
239
+ //
240
+ // Operators should call this at boot for every "important" filepath
241
+ // (vault.key.sealed, audit-sign.key.sealed, db.enc, etc.) BEFORE they
242
+ // start their first atomic write to that path.
243
+ function cleanOrphans(filepath, opts) {
244
+ opts = opts || {};
245
+ var olderThanMs = opts.olderThanMs != null ? opts.olderThanMs : C.TIME.minutes(5);
246
+ var dir = path.dirname(filepath);
247
+ var basename = path.basename(filepath);
248
+ var prefix = basename + ".tmp-";
249
+ var nowMs = Date.now();
250
+ var removed = 0;
251
+ var entries = listDir(dir, {
252
+ filter: function (name) { return name.startsWith(prefix); },
253
+ includeStat: true,
254
+ });
255
+ for (var i = 0; i < entries.length; i++) {
256
+ var entry = entries[i];
257
+ try {
258
+ if (nowMs - entry.mtimeMs >= olderThanMs) {
259
+ fs.unlinkSync(entry.fullPath);
260
+ removed += 1;
261
+ }
262
+ } catch (_e) { /* concurrent cleanup or permission — best effort */ }
263
+ }
264
+ return removed;
265
+ }
266
+
267
+ // ---- write ----
268
+
269
+ async function write(filepath, data, opts) {
270
+ opts = Object.assign({}, DEFAULTS, opts || {});
271
+ var buf = safeBuffer.toBuffer(data, {
272
+ errorClass: AtomicFileError,
273
+ typeCode: "atomic-file/invalid-data",
274
+ typeMessage: "data must be Buffer, Uint8Array, or string",
275
+ });
276
+
277
+ return await _withRetry(function () {
278
+ return new Promise(function (resolve, reject) {
279
+ var dir = path.dirname(filepath);
280
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
281
+ var tmpPath = filepath + ".tmp-" + generateToken(8);
282
+ var renamed = false;
283
+ try {
284
+ var fd = fs.openSync(tmpPath, "w", opts.fileMode);
285
+ try {
286
+ var pos = 0;
287
+ while (pos < buf.length) {
288
+ pos += fs.writeSync(fd, buf, pos, buf.length - pos, null);
289
+ }
290
+ _fsync(fd);
291
+ } finally {
292
+ try { fs.closeSync(fd); } catch (_e) { /* already closed? */ }
293
+ }
294
+ // Atomic rename — POSIX rename is atomic on the same FS; on Windows,
295
+ // fs.renameSync uses MoveFileEx with REPLACE_EXISTING.
296
+ fs.renameSync(tmpPath, filepath);
297
+ renamed = true;
298
+ _fsyncDir(dir);
299
+ var hash = opts.computeHash ? sha3Hash(buf) : null;
300
+ resolve({ bytesWritten: buf.length, hash: hash });
301
+ } catch (e) {
302
+ reject(e);
303
+ } finally {
304
+ if (!renamed) {
305
+ try { fs.unlinkSync(tmpPath); } catch (_e) { /* may not exist */ }
306
+ }
307
+ }
308
+ });
309
+ }, opts);
310
+ }
311
+
312
+ // ---- read ----
313
+
314
+ async function read(filepath, opts) {
315
+ opts = Object.assign({}, DEFAULTS, opts || {});
316
+ return await _withRetry(function () {
317
+ return new Promise(function (resolve, reject) {
318
+ try { resolve(_readSyncCore(filepath, opts)); }
319
+ catch (e) { reject(e); }
320
+ });
321
+ }, opts);
322
+ }
323
+
324
+ // Sync variant for callers in module-init / boot paths that can't
325
+ // `await` (vault.initPlaintext, audit-sign._initPlaintext,
326
+ // db._checkRollback, db.loadOrCreateDbKey). Same semantics as
327
+ // async read: size cap, optional integrity-hash verification, ENOENT
328
+ // translation. No retry loop — sync paths can't usefully back off.
329
+ function readSync(filepath, opts) {
330
+ opts = Object.assign({}, DEFAULTS, opts || {});
331
+ return _readSyncCore(filepath, opts);
332
+ }
333
+
334
+ function _readSyncCore(filepath, opts) {
335
+ if (!fs.existsSync(filepath)) {
336
+ var e = new AtomicFileError("file not found: " + filepath, "atomic-file/not-found");
337
+ e.code = "ENOENT";
338
+ throw e;
339
+ }
340
+ var stat = fs.statSync(filepath);
341
+ if (stat.size > opts.maxBytes) {
342
+ throw new AtomicFileError(
343
+ "file size " + stat.size + " > maxBytes " + opts.maxBytes,
344
+ "atomic-file/too-large"
345
+ );
346
+ }
347
+ var buf = fs.readFileSync(filepath);
348
+ if (opts.expectedHash) {
349
+ var actual = sha3Hash(buf);
350
+ if (actual !== opts.expectedHash) {
351
+ throw new AtomicFileError(
352
+ "integrity check failed: expected " + opts.expectedHash + " got " + actual,
353
+ "atomic-file/integrity"
354
+ );
355
+ }
356
+ }
357
+ return opts.encoding ? buf.toString(opts.encoding) : buf;
358
+ }
359
+
360
+ // ---- writeJson / readJson ----
361
+
362
+ async function writeJson(filepath, value, opts) {
363
+ opts = opts || {};
364
+ var serialized = opts.canonical
365
+ ? safeJson.canonical(value)
366
+ : safeJson.stringify(value, { indent: opts.indent || 0 });
367
+ return await write(filepath, serialized, opts);
368
+ }
369
+
370
+ async function readJson(filepath, opts) {
371
+ opts = opts || {};
372
+ var buf = await read(filepath, opts);
373
+ var input = Buffer.isBuffer(buf) ? buf : Buffer.from(buf, "utf8");
374
+ return safeJson.parse(input, opts);
375
+ }
376
+
377
+ // ---- copy ----
378
+
379
+ async function copy(src, dst, opts) {
380
+ opts = Object.assign({}, DEFAULTS, opts || {});
381
+ var srcOpts = Object.assign({}, opts);
382
+ delete srcOpts.expectedHash; // hash check applies to dst, not src
383
+ var buf = await read(src, srcOpts);
384
+ if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf, "utf8");
385
+ return await write(dst, buf, opts);
386
+ }
387
+
388
+ // ---- exists ----
389
+
390
+ function exists(filepath) {
391
+ return fs.existsSync(filepath);
392
+ }
393
+
394
+ // ---- lock (cross-process file mutex) ----
395
+
396
+ async function lock(filepath, fn, opts) {
397
+ opts = Object.assign({}, DEFAULTS, opts || {});
398
+ var lockPath = filepath + ".lock";
399
+ var deadline = Date.now() + opts.lockTimeoutMs;
400
+ var fd = null;
401
+
402
+ while (Date.now() < deadline) {
403
+ try {
404
+ // O_CREAT | O_EXCL — fails if file exists
405
+ fd = fs.openSync(lockPath, "wx", opts.fileMode);
406
+ break;
407
+ } catch (e) {
408
+ if (e.code !== "EEXIST") throw e;
409
+ // Stale lock detection: if the .lock file is older than 5 minutes,
410
+ // assume the holding process crashed and remove it.
411
+ try {
412
+ var stat = fs.statSync(lockPath);
413
+ if (Date.now() - stat.mtimeMs > C.TIME.minutes(5)) {
414
+ try { fs.unlinkSync(lockPath); } catch (_e) {}
415
+ continue;
416
+ }
417
+ } catch (_e) { /* stat raced with another process — keep waiting */ }
418
+ await safeAsync.sleep(opts.lockPollMs, { signal: opts.signal });
419
+ }
420
+ }
421
+ if (fd === null) {
422
+ throw new AtomicFileError(
423
+ "lock timeout after " + opts.lockTimeoutMs + "ms on " + filepath,
424
+ "atomic-file/lock-timeout"
425
+ );
426
+ }
427
+ try {
428
+ fs.writeSync(fd, Buffer.from(JSON.stringify({
429
+ pid: process.pid,
430
+ acquiredAt: Date.now(),
431
+ }), "utf8"));
432
+ _fsync(fd);
433
+ } catch (_e) { /* lock content best-effort */ }
434
+
435
+ try {
436
+ return await fn();
437
+ } finally {
438
+ try { fs.closeSync(fd); } catch (_e) {}
439
+ try { fs.unlinkSync(lockPath); } catch (_e) {}
440
+ }
441
+ }
442
+
443
+ // Single-directory listing primitive. Wraps fs.readdirSync with the
444
+ // optional-stat pattern + missing-dir tolerance + filter that callers
445
+ // across the framework were re-implementing.
446
+ //
447
+ // opts:
448
+ // filter: function(name) => boolean — name-only predicate
449
+ // includeStat: bool — adds mtimeMs / sizeBytes / isDirectory /
450
+ // isFile per entry (one fs.statSync call each).
451
+ // Skip when the caller only needs names — saves a
452
+ // syscall per entry.
453
+ // missingOk: bool — default true. Returns [] when the dir
454
+ // doesn't exist (ENOENT). Other errors throw.
455
+ //
456
+ // Returns: array of { name, fullPath } (plus stat fields when
457
+ // includeStat is true). Entries that vanish between readdir and
458
+ // stat (concurrent cleanup) are silently dropped.
459
+ //
460
+ // For recursive directory walks, callers compose listDir per
461
+ // subdirectory — the primitive doesn't recurse, so callers can apply
462
+ // per-iteration limits / filters / stop conditions cleanly.
463
+ function listDir(dir, opts) {
464
+ opts = opts || {};
465
+ var missingOk = opts.missingOk !== false;
466
+ var includeStat = opts.includeStat === true;
467
+ var filter = typeof opts.filter === "function" ? opts.filter : null;
468
+
469
+ var entries;
470
+ try {
471
+ entries = fs.readdirSync(dir);
472
+ } catch (e) {
473
+ if (missingOk && e.code === "ENOENT") return [];
474
+ throw new AtomicFileError(
475
+ "failed to list directory " + dir + ": " + e.message,
476
+ "atomic-file/list-failed"
477
+ );
478
+ }
479
+
480
+ var out = [];
481
+ for (var i = 0; i < entries.length; i++) {
482
+ var name = entries[i];
483
+ if (filter && !filter(name)) continue;
484
+ var fullPath = path.join(dir, name);
485
+ var entry = { name: name, fullPath: fullPath };
486
+ if (includeStat) {
487
+ try {
488
+ var stat = fs.statSync(fullPath);
489
+ entry.mtimeMs = stat.mtimeMs;
490
+ entry.sizeBytes = stat.size;
491
+ entry.isDirectory = stat.isDirectory();
492
+ entry.isFile = stat.isFile();
493
+ } catch (_e) {
494
+ // Entry vanished between readdir and stat — concurrent cleanup
495
+ // by another process. Skip silently; caller asked for stat
496
+ // info that no longer exists.
497
+ continue;
498
+ }
499
+ }
500
+ out.push(entry);
501
+ }
502
+ return out;
503
+ }
504
+
505
+ module.exports = {
506
+ write: write,
507
+ writeSync: writeSync,
508
+ read: read,
509
+ readSync: readSync,
510
+ writeJson: writeJson,
511
+ readJson: readJson,
512
+ copy: copy,
513
+ exists: exists,
514
+ lock: lock,
515
+ listDir: listDir,
516
+ cleanOrphans: cleanOrphans,
517
+ // Filesystem hygiene helpers (lifted from inline duplicates across lib/)
518
+ fsync: fsync,
519
+ fsyncDir: fsyncDir,
520
+ ensureDir: ensureDir,
521
+ copyDirRecursive: copyDirRecursive,
522
+ pathTimestamp: pathTimestamp,
523
+ AtomicFileError: AtomicFileError,
524
+ DEFAULTS: DEFAULTS,
525
+ };
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ /**
3
+ * Audit hash chain — tamper-evidence math.
4
+ *
5
+ * Per the compliance spec ("Tamper evidence (the chain + checkpoint signing)"
6
+ * in the roadmap):
7
+ *
8
+ * rowHash = SHA3-512(
9
+ * prevHash || canonicalize(row-fields-except-hash) || nonce
10
+ * )
11
+ *
12
+ * Each row's prevHash equals the previous row's rowHash (in monotonic-counter
13
+ * order). The first row uses ZERO_HASH as prevHash. Verification walks the
14
+ * chain forward; any row whose prevHash doesn't match the running hash, or
15
+ * whose rowHash recomputes differently, breaks the chain.
16
+ *
17
+ * Checkpoint signing (ML-DSA-87 over (atRow || atRowHash)) lives in
18
+ * lib/audit-sign.js. This module owns the chain hash math only;
19
+ * verification is O(n) and walks every row at boot.
20
+ */
21
+ var { sha3Hash } = require("./crypto");
22
+
23
+ // All-zero SHA3-512 (128 hex chars) — sentinel prevHash for the first row.
24
+ var ZERO_HASH = "0".repeat(128);
25
+
26
+ // Canonicalize a row for hashing. Excludes the hash/nonce columns themselves
27
+ // and any caller-specified columns. Sorted keys, JSON-encoded values; Buffer
28
+ // values converted to hex for stable byte serialization.
29
+ function canonicalize(row, excludeKeys) {
30
+ var ex = new Set(excludeKeys || []);
31
+ var keys = Object.keys(row).filter(function (k) { return !ex.has(k); }).sort();
32
+ var pairs = {};
33
+ for (var i = 0; i < keys.length; i++) {
34
+ var v = row[keys[i]];
35
+ if (Buffer.isBuffer(v)) v = v.toString("hex");
36
+ else if (v instanceof Uint8Array) v = Buffer.from(v).toString("hex");
37
+ else if (v === undefined) v = null;
38
+ pairs[keys[i]] = v;
39
+ }
40
+ return JSON.stringify(pairs);
41
+ }
42
+
43
+ // Compute a row's hash given its predecessor's hash, the row's logical fields
44
+ // (already excluding prevHash, rowHash, nonce), and the row's nonce buffer.
45
+ function computeRowHash(prevHash, rowFields, nonce) {
46
+ if (typeof prevHash !== "string" || prevHash.length !== 128) {
47
+ throw new Error("prevHash must be a 128-char hex string (SHA3-512); got length " +
48
+ (prevHash && prevHash.length));
49
+ }
50
+ if (!Buffer.isBuffer(nonce) || nonce.length === 0) {
51
+ throw new Error("nonce must be a non-empty Buffer");
52
+ }
53
+ var canonical = canonicalize(rowFields);
54
+ var input = Buffer.concat([
55
+ Buffer.from(prevHash, "hex"),
56
+ Buffer.from(canonical, "utf8"),
57
+ nonce,
58
+ ]);
59
+ return sha3Hash(input);
60
+ }
61
+
62
+ // Read the current chain tip (last row's rowHash + monotonicCounter) for a
63
+ // given audit table. Async to accommodate operator-supplied external-db
64
+ // drivers; queryOneAsync is `async (sql, params?) → row | null`.
65
+ async function getChainTip(queryOneAsync, tableName) {
66
+ var row = await queryOneAsync(
67
+ 'SELECT rowHash, monotonicCounter FROM "' + tableName + '" ' +
68
+ "ORDER BY monotonicCounter DESC LIMIT 1"
69
+ );
70
+ if (!row) return { prevHash: ZERO_HASH, counter: 0 };
71
+ return { prevHash: row.rowHash, counter: row.monotonicCounter };
72
+ }
73
+
74
+ // Walk the entire chain forward, recomputing each row's hash. Returns an
75
+ // object describing the result; callers decide how to react (refuse-to-boot,
76
+ // log warning, etc.). queryAllAsync is `async (sql, params?) → rows`.
77
+ //
78
+ // audit_log only: if a `_blamejs_audit_purge_anchor` row exists, the walk
79
+ // starts at lastPurgedCounter+1 with prevHash = lastPurgedRowHash. The
80
+ // anchor is written by audit-tools.purge() after a successful archive,
81
+ // and lets the chain math survive deletion of historical rows without
82
+ // the bundle as the source of truth.
83
+ async function verifyChain(queryAllAsync, tableName, opts) {
84
+ opts = opts || {};
85
+
86
+ var prevHash = ZERO_HASH;
87
+ var skipBeforeCounter = 0;
88
+ if (tableName === "audit_log") {
89
+ var anchor;
90
+ try {
91
+ anchor = await queryAllAsync(
92
+ "SELECT lastPurgedCounter, lastPurgedRowHash FROM _blamejs_audit_purge_anchor " +
93
+ "WHERE scope = 'audit'"
94
+ );
95
+ } catch (_e) {
96
+ // Anchor table may not exist on a deployment that has never been
97
+ // through a purge. Treat as no anchor.
98
+ anchor = [];
99
+ }
100
+ if (Array.isArray(anchor) && anchor.length > 0) {
101
+ prevHash = anchor[0].lastPurgedRowHash;
102
+ skipBeforeCounter = Number(anchor[0].lastPurgedCounter);
103
+ }
104
+ }
105
+
106
+ var rows = await queryAllAsync(
107
+ 'SELECT * FROM "' + tableName + '" ORDER BY monotonicCounter ASC'
108
+ );
109
+ if (skipBeforeCounter > 0) {
110
+ rows = rows.filter(function (r) {
111
+ return Number(r.monotonicCounter) > skipBeforeCounter;
112
+ });
113
+ }
114
+
115
+ if (rows.length === 0) {
116
+ return { ok: true, table: tableName, rowsVerified: 0, lastHash: prevHash };
117
+ }
118
+ for (var i = 0; i < rows.length; i++) {
119
+ var row = rows[i];
120
+ if (row.prevHash !== prevHash) {
121
+ return {
122
+ ok: false,
123
+ table: tableName,
124
+ rowsVerified: i,
125
+ breakAt: i,
126
+ breakRowId: row._id,
127
+ reason: "prevHash mismatch",
128
+ expected: prevHash,
129
+ actual: row.prevHash,
130
+ };
131
+ }
132
+ var fields = Object.assign({}, row);
133
+ delete fields.prevHash;
134
+ delete fields.rowHash;
135
+ delete fields.nonce;
136
+ // fencingToken is cluster-coordination bookkeeping — orthogonal to
137
+ // tamper-evidence. Excluded from chain hash inputs so deployments
138
+ // upgrading from a pre-cluster schema (no fencingToken column) verify
139
+ // identically before and after the ALTER TABLE.
140
+ delete fields.fencingToken;
141
+ var nonceBuf = Buffer.isBuffer(row.nonce) ? row.nonce : Buffer.from(row.nonce);
142
+ var computed = computeRowHash(prevHash, fields, nonceBuf);
143
+ if (computed !== row.rowHash) {
144
+ return {
145
+ ok: false,
146
+ table: tableName,
147
+ rowsVerified: i,
148
+ breakAt: i,
149
+ breakRowId: row._id,
150
+ reason: "rowHash mismatch",
151
+ expected: computed,
152
+ actual: row.rowHash,
153
+ };
154
+ }
155
+ prevHash = row.rowHash;
156
+
157
+ if (opts.maxRows && i >= opts.maxRows - 1) break;
158
+ }
159
+ return { ok: true, table: tableName, rowsVerified: rows.length, lastHash: prevHash };
160
+ }
161
+
162
+ module.exports = {
163
+ ZERO_HASH: ZERO_HASH,
164
+ canonicalize: canonicalize,
165
+ computeRowHash: computeRowHash,
166
+ getChainTip: getChainTip,
167
+ verifyChain: verifyChain,
168
+ };