@blamejs/core 0.7.18 → 0.7.19

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 (167) hide show
  1. package/CHANGELOG.md +425 -423
  2. package/README.md +150 -150
  3. package/bin/blamejs.js +0 -0
  4. package/index.js +310 -308
  5. package/lib/api-key.js +660 -660
  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-external.js +365 -0
  16. package/lib/auth/jwt.js +337 -311
  17. package/lib/auth/lockout.js +436 -436
  18. package/lib/auth/oauth.js +721 -721
  19. package/lib/auth/passkey.js +181 -181
  20. package/lib/auth/password.js +628 -594
  21. package/lib/backup/bundle.js +217 -217
  22. package/lib/backup/crypto.js +176 -176
  23. package/lib/backup/index.js +515 -515
  24. package/lib/backup/manifest.js +282 -282
  25. package/lib/break-glass.js +1338 -1338
  26. package/lib/bundler.js +441 -441
  27. package/lib/cache-redis.js +256 -256
  28. package/lib/cache.js +1206 -1206
  29. package/lib/canonical-json.js +115 -115
  30. package/lib/chain-writer.js +234 -234
  31. package/lib/cli-helpers.js +206 -206
  32. package/lib/cli.js +2334 -2334
  33. package/lib/cluster-provider-db.js +317 -317
  34. package/lib/cluster-storage.js +226 -226
  35. package/lib/cluster.js +703 -703
  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 -275
  43. package/lib/db-declare-row-policy.js +267 -267
  44. package/lib/db-declare-view.js +420 -420
  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/forms.js +422 -422
  56. package/lib/framework-error.js +293 -293
  57. package/lib/framework-schema.js +717 -717
  58. package/lib/handlers.js +350 -350
  59. package/lib/http-client-cookie-jar.js +508 -508
  60. package/lib/http-client.js +1195 -1195
  61. package/lib/i18n.js +878 -878
  62. package/lib/jobs.js +185 -185
  63. package/lib/log-stream-cloudwatch.js +369 -369
  64. package/lib/log-stream-local.js +146 -146
  65. package/lib/log-stream-otlp-grpc.js +410 -410
  66. package/lib/log-stream-otlp.js +286 -286
  67. package/lib/log-stream-syslog.js +302 -302
  68. package/lib/log-stream-webhook.js +199 -199
  69. package/lib/log-stream.js +330 -330
  70. package/lib/log.js +500 -500
  71. package/lib/mail-bounce.js +528 -528
  72. package/lib/mail-dkim.js +369 -369
  73. package/lib/mail.js +981 -981
  74. package/lib/metrics.js +683 -683
  75. package/lib/middleware/api-encrypt.js +936 -936
  76. package/lib/middleware/attach-user.js +157 -157
  77. package/lib/middleware/bearer-auth.js +152 -0
  78. package/lib/middleware/body-parser.js +1170 -1170
  79. package/lib/middleware/bot-guard.js +178 -178
  80. package/lib/middleware/compression.js +452 -452
  81. package/lib/middleware/cors.js +314 -314
  82. package/lib/middleware/csp-nonce.js +348 -348
  83. package/lib/middleware/csrf-protect.js +316 -316
  84. package/lib/middleware/db-role-for.js +264 -264
  85. package/lib/middleware/health.js +392 -392
  86. package/lib/middleware/index.js +82 -79
  87. package/lib/middleware/rate-limit.js +358 -358
  88. package/lib/middleware/request-id.js +61 -61
  89. package/lib/middleware/request-log.js +168 -168
  90. package/lib/middleware/require-auth.js +104 -104
  91. package/lib/middleware/security-headers.js +116 -116
  92. package/lib/middleware/sse.js +166 -166
  93. package/lib/migrations.js +383 -383
  94. package/lib/mtls-ca.js +518 -518
  95. package/lib/mtls-engine-default.js +481 -481
  96. package/lib/network-dns.js +632 -632
  97. package/lib/network-heartbeat.js +290 -290
  98. package/lib/network-nts.js +574 -574
  99. package/lib/network-proxy.js +265 -265
  100. package/lib/network-tls.js +328 -328
  101. package/lib/network.js +233 -233
  102. package/lib/notify.js +612 -612
  103. package/lib/ntp-check.js +229 -229
  104. package/lib/numeric-bounds.js +111 -111
  105. package/lib/object-store/azure-blob-bucket-ops.js +349 -349
  106. package/lib/object-store/azure-blob.js +488 -488
  107. package/lib/object-store/gcs-bucket-ops.js +351 -351
  108. package/lib/object-store/gcs.js +519 -519
  109. package/lib/object-store/http-put.js +153 -153
  110. package/lib/object-store/index.js +197 -197
  111. package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
  112. package/lib/object-store/sigv4.js +903 -903
  113. package/lib/observability.js +151 -151
  114. package/lib/otel-export.js +269 -269
  115. package/lib/pagination.js +464 -464
  116. package/lib/parsers/index.js +80 -80
  117. package/lib/parsers/safe-env.js +642 -642
  118. package/lib/parsers/safe-ini.js +292 -292
  119. package/lib/parsers/safe-toml.js +784 -784
  120. package/lib/parsers/safe-xml.js +390 -390
  121. package/lib/parsers/safe-yaml.js +1015 -1015
  122. package/lib/permissions.js +708 -708
  123. package/lib/pqc-agent.js +87 -87
  124. package/lib/pqc-gate.js +279 -279
  125. package/lib/protobuf-encoder.js +190 -190
  126. package/lib/protocol-dispatcher.js +161 -161
  127. package/lib/pubsub-redis.js +167 -167
  128. package/lib/pubsub.js +429 -429
  129. package/lib/queue-local.js +476 -476
  130. package/lib/queue-redis.js +745 -745
  131. package/lib/queue-sqs.js +319 -319
  132. package/lib/queue.js +695 -695
  133. package/lib/redis-client.js +519 -519
  134. package/lib/request-helpers.js +340 -340
  135. package/lib/restore-bundle.js +237 -237
  136. package/lib/restore-rollback.js +259 -259
  137. package/lib/restore.js +409 -409
  138. package/lib/retry.js +376 -376
  139. package/lib/router.js +748 -748
  140. package/lib/safe-async.js +735 -735
  141. package/lib/safe-buffer.js +237 -237
  142. package/lib/safe-json.js +541 -541
  143. package/lib/safe-schema.js +1266 -1266
  144. package/lib/safe-url.js +159 -159
  145. package/lib/scheduler.js +706 -706
  146. package/lib/security-assert.js +373 -373
  147. package/lib/seeders.js +618 -618
  148. package/lib/session.js +535 -478
  149. package/lib/slug.js +269 -269
  150. package/lib/ssrf-guard.js +401 -401
  151. package/lib/storage.js +471 -471
  152. package/lib/subject.js +281 -281
  153. package/lib/template.js +791 -791
  154. package/lib/testing.js +798 -798
  155. package/lib/time.js +310 -310
  156. package/lib/totp.js +302 -302
  157. package/lib/tracing.js +494 -494
  158. package/lib/uuid.js +132 -132
  159. package/lib/validate-opts.js +340 -340
  160. package/lib/vault/index.js +308 -308
  161. package/lib/vault/rotate.js +784 -784
  162. package/lib/vault/wrap.js +296 -296
  163. package/lib/vendor/noble-ciphers.cjs +9 -9
  164. package/lib/webhook.js +595 -595
  165. package/lib/websocket.js +1048 -1048
  166. package/package.json +77 -77
  167. package/sbom.cyclonedx.json +7 -7
@@ -1,544 +1,544 @@
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 { boot } = require("./log");
40
- var safeBuffer = require("./safe-buffer");
41
- var numericBounds = require("./numeric-bounds");
42
- var safeAsync = require("./safe-async");
43
- var retry = require("./retry");
44
- var { FrameworkError } = require("./framework-error");
45
-
46
- var log = boot("atomic-file");
47
-
48
- var DEFAULTS = {
49
- maxBytes: C.BYTES.mib(64), // 64 MiB ceiling on read
50
- retryAttempts: 5,
51
- retryBaseMs: 50,
52
- retryMaxMs: C.TIME.seconds(2),
53
- fileMode: 0o600,
54
- computeHash: false,
55
- lockTimeoutMs: C.TIME.seconds(30),
56
- lockPollMs: 50,
57
- };
58
-
59
- class AtomicFileError extends FrameworkError {
60
- constructor(message, code) {
61
- super(message);
62
- this.name = "AtomicFileError";
63
- this.code = code || "atomic-file/error";
64
- this.isAtomicFileError = true;
65
- }
66
- }
67
-
68
- // ---- Retry helper for transient FS errors ----
69
- //
70
- // Routes through b.retry.withRetry with an FS-specific classifier — the
71
- // errnos that mean "try again" on Linux/macOS/Windows file systems differ
72
- // from the HTTP/network-shaped default classifier. atomic-file's public
73
- // opts (retryAttempts/retryBaseMs/retryMaxMs) are mapped to the retry
74
- // primitive's standard names; jitterFactor 0.5 reproduces the original
75
- // `delay * (0.5 + Math.random()/2)` range of [delay/2, delay].
76
-
77
- var TRANSIENT_FS_ERRNOS = new Set(["EBUSY", "EAGAIN", "ENFILE", "EMFILE", "EPERM"]);
78
-
79
- function _isFsRetryable(e) {
80
- return e != null && TRANSIENT_FS_ERRNOS.has(e.code);
81
- }
82
-
83
- async function _withRetry(fn, opts) {
84
- opts = Object.assign({}, DEFAULTS, opts || {});
85
- return retry.withRetry(function () { return fn(); }, {
86
- maxAttempts: opts.retryAttempts,
87
- baseDelayMs: opts.retryBaseMs,
88
- maxDelayMs: opts.retryMaxMs,
89
- jitterFactor: 0.5,
90
- isRetryable: _isFsRetryable,
91
- signal: opts.signal,
92
- });
93
- }
94
-
95
- // ---- Sync helpers (best effort) ----
96
- //
97
- // fsync / fsyncDir / ensureDir / copyDirRecursive / pathTimestamp are
98
- // public surface — they were previously underscore-prefixed, and other
99
- // modules (vault-passphrase-ops, vault-rotate, backup-bundle, restore-
100
- // bundle, restore-rollback, bundler) duplicated them inline. Hoisted
101
- // here so the framework has one shared implementation per concern.
102
-
103
- function fsync(fd) {
104
- try { fs.fsyncSync(fd); } catch (_e) { /* not all platforms support fsync on every fd type */ }
105
- }
106
-
107
- function fsyncDir(dirPath) {
108
- try {
109
- var fd = fs.openSync(dirPath, "r");
110
- try { fs.fsyncSync(fd); } catch (_e) { /* Windows rejects directory fsync */ }
111
- finally { fs.closeSync(fd); }
112
- } catch (_e) { /* dir fsync is best-effort across filesystems */ }
113
- }
114
-
115
- // Internal aliases so existing code in this file keeps working
116
- function _fsync(fd) { return fsync(fd); }
117
- function _fsyncDir(dirPath) { return fsyncDir(dirPath); }
118
-
119
- // ensureDir — mkdirSync with recursive: true and a default mode of
120
- // 0o700 (owner-only) suitable for framework data directories. Caller
121
- // passes a different mode for less-restricted dirs.
122
- function ensureDir(dirPath, mode) {
123
- if (typeof dirPath !== "string" || dirPath.length === 0) {
124
- throw new AtomicFileError("ensureDir: path must be a non-empty string", "atomic-file/bad-path");
125
- }
126
- fs.mkdirSync(dirPath, { recursive: true, mode: typeof mode === "number" ? mode : 0o700 });
127
- return dirPath;
128
- }
129
-
130
- // copyDirRecursive — synchronous, file-by-file copy that mirrors the
131
- // source's directory structure. Skips symlinks (operator wanting symlink
132
- // preservation should use a real archive tool). Refuses to overwrite
133
- // existing files at dest by default — pass opts.overwrite=true to
134
- // replace. dest is created with mode 0o700 by default.
135
- function copyDirRecursive(src, dest, opts) {
136
- if (typeof src !== "string" || src.length === 0) {
137
- throw new AtomicFileError("copyDirRecursive: src must be a non-empty string", "atomic-file/bad-path");
138
- }
139
- if (typeof dest !== "string" || dest.length === 0) {
140
- throw new AtomicFileError("copyDirRecursive: dest must be a non-empty string", "atomic-file/bad-path");
141
- }
142
- if (!fs.existsSync(src)) {
143
- throw new AtomicFileError("copyDirRecursive: src does not exist: " + src, "atomic-file/missing-src");
144
- }
145
- opts = opts || {};
146
- var dirMode = typeof opts.dirMode === "number" ? opts.dirMode : 0o700;
147
- var overwrite = !!opts.overwrite;
148
- var copyFlags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
149
-
150
- ensureDir(dest, dirMode);
151
- var entries = fs.readdirSync(src, { withFileTypes: true });
152
- var fileCount = 0;
153
- var byteCount = 0;
154
- for (var i = 0; i < entries.length; i++) {
155
- var name = entries[i].name;
156
- var s = path.join(src, name);
157
- var d = path.join(dest, name);
158
- if (entries[i].isDirectory()) {
159
- var sub = copyDirRecursive(s, d, opts);
160
- fileCount += sub.fileCount;
161
- byteCount += sub.byteCount;
162
- } else if (entries[i].isFile()) {
163
- fs.copyFileSync(s, d, copyFlags);
164
- try { byteCount += fs.statSync(d).size; } catch (_e) { /* size best-effort */ }
165
- fileCount++;
166
- }
167
- // Symlinks, sockets, devices: deliberately skipped
168
- }
169
- return { fileCount: fileCount, byteCount: byteCount };
170
- }
171
-
172
- // pathTimestamp — filesystem-safe ISO-8601 timestamp suitable for use as
173
- // a directory or file name on every platform. Standard
174
- // Date.toISOString() embeds ':' and '.' which Windows reserves for
175
- // drive letters and extension separators. This helper substitutes both
176
- // with '-' so the result works as a path segment unmodified. String
177
- // sort still gives chronological order.
178
- //
179
- // atomicFile.pathTimestamp()
180
- // → "2026-04-27T14-00-00-123Z"
181
- // atomicFile.pathTimestamp(new Date(0))
182
- // → "1970-01-01T00-00-00-000Z"
183
- function pathTimestamp(date) {
184
- var d = (date instanceof Date) ? date : new Date();
185
- return d.toISOString().replace(/[:.]/g, "-");
186
- }
187
-
188
- // ---- writeSync ----
189
- // Synchronous atomic write — same temp+fsync+rename+dirfsync flow as
190
- // async write(), but without the retry loop (which requires awaits).
191
- // Use this from sync code paths (process exit handlers, module-load-time
192
- // bootstraps). For everything else, prefer the async write().
193
- //
194
- // Transactional guarantee: either the rename completes (new contents fully
195
- // visible) or the tmp file is removed (no state change). The caller never
196
- // sees a half-written file at `filepath` and never leaves a tmp orphan
197
- // from the current call's failure path.
198
- function writeSync(filepath, data, opts) {
199
- opts = Object.assign({}, DEFAULTS, opts || {});
200
- var buf = safeBuffer.toBuffer(data, {
201
- errorClass: AtomicFileError,
202
- typeCode: "atomic-file/invalid-data",
203
- typeMessage: "data must be Buffer, Uint8Array, or string",
204
- });
205
-
206
- var dir = path.dirname(filepath);
207
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
208
-
209
- var tmpPath = filepath + ".tmp-" + generateToken(C.BYTES.bytes(8));
210
- var renamed = false;
211
- try {
212
- var fd = fs.openSync(tmpPath, "w", opts.fileMode);
213
- try {
214
- var pos = 0;
215
- while (pos < buf.length) {
216
- pos += fs.writeSync(fd, buf, pos, buf.length - pos, null);
217
- }
218
- _fsync(fd);
219
- } finally {
220
- try { fs.closeSync(fd); } catch (_e) { /* already closed? */ }
221
- }
222
- fs.renameSync(tmpPath, filepath);
223
- renamed = true;
224
- _fsyncDir(dir);
225
- } finally {
226
- if (!renamed) {
227
- // Either the write or the rename failed — remove the tmp so the next
228
- // boot doesn't see a leaked partial file.
229
- try { fs.unlinkSync(tmpPath); } catch (_e) { /* may not exist */ }
230
- }
231
- }
232
-
233
- return {
234
- bytesWritten: buf.length,
235
- hash: opts.computeHash ? sha3Hash(buf) : null,
236
- };
237
- }
238
-
239
- // Clean up orphan tmp files left behind by a previously-crashed process.
240
- // Atomic writes use random tmp names (filepath + ".tmp-" + token), so a
241
- // crash leaves a file with a name we can't predict on next boot — only
242
- // glob and prune by age. Default: prune anything older than 5 minutes.
243
- //
244
- // Operators should call this at boot for every "important" filepath
245
- // (vault.key.sealed, audit-sign.key.sealed, db.enc, etc.) BEFORE they
246
- // start their first atomic write to that path.
247
- function cleanOrphans(filepath, opts) {
248
- opts = opts || {};
249
- var olderThanMs = opts.olderThanMs != null ? opts.olderThanMs : C.TIME.minutes(5);
250
- var dir = path.dirname(filepath);
251
- var basename = path.basename(filepath);
252
- var prefix = basename + ".tmp-";
253
- var nowMs = Date.now();
254
- var removed = 0;
255
- var entries = listDir(dir, {
256
- filter: function (name) { return name.startsWith(prefix); },
257
- includeStat: true,
258
- });
259
- for (var i = 0; i < entries.length; i++) {
260
- var entry = entries[i];
261
- try {
262
- if (nowMs - entry.mtimeMs >= olderThanMs) {
263
- fs.unlinkSync(entry.fullPath);
264
- removed += 1;
265
- }
266
- } catch (_e) { /* concurrent cleanup or permission — best effort */ }
267
- }
268
- return removed;
269
- }
270
-
271
- // ---- write ----
272
-
273
- async function write(filepath, data, opts) {
274
- opts = Object.assign({}, DEFAULTS, opts || {});
275
- var buf = safeBuffer.toBuffer(data, {
276
- errorClass: AtomicFileError,
277
- typeCode: "atomic-file/invalid-data",
278
- typeMessage: "data must be Buffer, Uint8Array, or string",
279
- });
280
-
281
- return await _withRetry(function () {
282
- return new Promise(function (resolve, reject) {
283
- var dir = path.dirname(filepath);
284
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
285
- var tmpPath = filepath + ".tmp-" + generateToken(C.BYTES.bytes(8));
286
- var renamed = false;
287
- try {
288
- var fd = fs.openSync(tmpPath, "w", opts.fileMode);
289
- try {
290
- var pos = 0;
291
- while (pos < buf.length) {
292
- pos += fs.writeSync(fd, buf, pos, buf.length - pos, null);
293
- }
294
- _fsync(fd);
295
- } finally {
296
- try { fs.closeSync(fd); } catch (_e) { /* already closed? */ }
297
- }
298
- // Atomic rename — POSIX rename is atomic on the same FS; on Windows,
299
- // fs.renameSync uses MoveFileEx with REPLACE_EXISTING.
300
- fs.renameSync(tmpPath, filepath);
301
- renamed = true;
302
- _fsyncDir(dir);
303
- var hash = opts.computeHash ? sha3Hash(buf) : null;
304
- resolve({ bytesWritten: buf.length, hash: hash });
305
- } catch (e) {
306
- reject(e);
307
- } finally {
308
- if (!renamed) {
309
- try { fs.unlinkSync(tmpPath); } catch (_e) { /* may not exist */ }
310
- }
311
- }
312
- });
313
- }, opts);
314
- }
315
-
316
- // ---- read ----
317
-
318
- async function read(filepath, opts) {
319
- opts = Object.assign({}, DEFAULTS, opts || {});
320
- return await _withRetry(function () {
321
- return new Promise(function (resolve, reject) {
322
- try { resolve(_readSyncCore(filepath, opts)); }
323
- catch (e) { reject(e); }
324
- });
325
- }, opts);
326
- }
327
-
328
- // Sync variant for callers in module-init / boot paths that can't
329
- // `await` (vault.initPlaintext, audit-sign._initPlaintext,
330
- // db._checkRollback, db.loadOrCreateDbKey). Same semantics as
331
- // async read: size cap, optional integrity-hash verification, ENOENT
332
- // translation. No retry loop — sync paths can't usefully back off.
333
- function readSync(filepath, opts) {
334
- opts = Object.assign({}, DEFAULTS, opts || {});
335
- return _readSyncCore(filepath, opts);
336
- }
337
-
338
- // maxBytes via shared lib/numeric-bounds — Infinity / NaN bypass the
339
- // stat.size cap (any-comparison-with-Infinity-or-NaN is false).
340
- function _validateMaxBytes(maxBytes) {
341
- if (!numericBounds.isPositiveFiniteInt(maxBytes)) {
342
- throw new AtomicFileError(
343
- "atomicFile.read: maxBytes must be a positive finite integer; got " +
344
- numericBounds.shape(maxBytes),
345
- "atomic-file/bad-opt");
346
- }
347
- }
348
-
349
- function _readSyncCore(filepath, opts) {
350
- if (!fs.existsSync(filepath)) {
351
- var e = new AtomicFileError("file not found: " + filepath, "atomic-file/not-found");
352
- e.code = "ENOENT";
353
- throw e;
354
- }
355
- _validateMaxBytes(opts.maxBytes);
356
- var stat = fs.statSync(filepath);
357
- if (stat.size > opts.maxBytes) {
358
- throw new AtomicFileError(
359
- "file size " + stat.size + " > maxBytes " + opts.maxBytes,
360
- "atomic-file/too-large"
361
- );
362
- }
363
- var buf = fs.readFileSync(filepath);
364
- if (opts.expectedHash) {
365
- var actual = sha3Hash(buf);
366
- if (actual !== opts.expectedHash) {
367
- throw new AtomicFileError(
368
- "integrity check failed: expected " + opts.expectedHash + " got " + actual,
369
- "atomic-file/integrity"
370
- );
371
- }
372
- }
373
- return opts.encoding ? buf.toString(opts.encoding) : buf;
374
- }
375
-
376
- // ---- writeJson / readJson ----
377
-
378
- async function writeJson(filepath, value, opts) {
379
- opts = opts || {};
380
- var serialized = opts.canonical
381
- ? safeJson.canonical(value)
382
- : safeJson.stringify(value, { indent: opts.indent || 0 });
383
- return await write(filepath, serialized, opts);
384
- }
385
-
386
- async function readJson(filepath, opts) {
387
- opts = opts || {};
388
- var buf = await read(filepath, opts);
389
- var input = Buffer.isBuffer(buf) ? buf : Buffer.from(buf, "utf8");
390
- return safeJson.parse(input, opts);
391
- }
392
-
393
- // ---- copy ----
394
-
395
- async function copy(src, dst, opts) {
396
- opts = Object.assign({}, DEFAULTS, opts || {});
397
- var srcOpts = Object.assign({}, opts);
398
- delete srcOpts.expectedHash; // hash check applies to dst, not src
399
- var buf = await read(src, srcOpts);
400
- if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf, "utf8");
401
- return await write(dst, buf, opts);
402
- }
403
-
404
- // ---- exists ----
405
-
406
- function exists(filepath) {
407
- return fs.existsSync(filepath);
408
- }
409
-
410
- // ---- lock (cross-process file mutex) ----
411
-
412
- async function lock(filepath, fn, opts) {
413
- opts = Object.assign({}, DEFAULTS, opts || {});
414
- var lockPath = filepath + ".lock";
415
- var deadline = Date.now() + opts.lockTimeoutMs;
416
- var fd = null;
417
-
418
- while (Date.now() < deadline) {
419
- try {
420
- // O_CREAT | O_EXCL — fails if file exists
421
- fd = fs.openSync(lockPath, "wx", opts.fileMode);
422
- break;
423
- } catch (e) {
424
- if (e.code !== "EEXIST") throw e;
425
- // Stale lock detection: if the .lock file is older than 5 minutes,
426
- // assume the holding process crashed and remove it.
427
- try {
428
- var stat = fs.statSync(lockPath);
429
- if (Date.now() - stat.mtimeMs > C.TIME.minutes(5)) {
430
- try { fs.unlinkSync(lockPath); }
431
- catch (uerr) { log.debug("stale-lock unlink failed", { path: lockPath, error: uerr.message }); }
432
- continue;
433
- }
434
- } catch (_e) { /* stat raced with another process — keep waiting */ }
435
- await safeAsync.sleep(opts.lockPollMs, { signal: opts.signal });
436
- }
437
- }
438
- if (fd === null) {
439
- throw new AtomicFileError(
440
- "lock timeout after " + opts.lockTimeoutMs + "ms on " + filepath,
441
- "atomic-file/lock-timeout"
442
- );
443
- }
444
- try {
445
- fs.writeSync(fd, Buffer.from(JSON.stringify({
446
- pid: process.pid,
447
- acquiredAt: Date.now(),
448
- }), "utf8"));
449
- _fsync(fd);
450
- } catch (_e) { /* lock content best-effort */ }
451
-
452
- try {
453
- return await fn();
454
- } finally {
455
- try { fs.closeSync(fd); }
456
- catch (cerr) { log.debug("lock fd close failed", { error: cerr.message }); }
457
- try { fs.unlinkSync(lockPath); }
458
- catch (uerr) { log.debug("lock release unlink failed", { path: lockPath, error: uerr.message }); }
459
- }
460
- }
461
-
462
- // Single-directory listing primitive. Wraps fs.readdirSync with the
463
- // optional-stat pattern + missing-dir tolerance + filter that callers
464
- // across the framework were re-implementing.
465
- //
466
- // opts:
467
- // filter: function(name) => boolean — name-only predicate
468
- // includeStat: bool — adds mtimeMs / sizeBytes / isDirectory /
469
- // isFile per entry (one fs.statSync call each).
470
- // Skip when the caller only needs names — saves a
471
- // syscall per entry.
472
- // missingOk: bool — default true. Returns [] when the dir
473
- // doesn't exist (ENOENT). Other errors throw.
474
- //
475
- // Returns: array of { name, fullPath } (plus stat fields when
476
- // includeStat is true). Entries that vanish between readdir and
477
- // stat (concurrent cleanup) are silently dropped.
478
- //
479
- // For recursive directory walks, callers compose listDir per
480
- // subdirectory — the primitive doesn't recurse, so callers can apply
481
- // per-iteration limits / filters / stop conditions cleanly.
482
- function listDir(dir, opts) {
483
- opts = opts || {};
484
- var missingOk = opts.missingOk !== false;
485
- var includeStat = opts.includeStat === true;
486
- var filter = typeof opts.filter === "function" ? opts.filter : null;
487
-
488
- var entries;
489
- try {
490
- entries = fs.readdirSync(dir);
491
- } catch (e) {
492
- if (missingOk && e.code === "ENOENT") return [];
493
- throw new AtomicFileError(
494
- "failed to list directory " + dir + ": " + e.message,
495
- "atomic-file/list-failed"
496
- );
497
- }
498
-
499
- var out = [];
500
- for (var i = 0; i < entries.length; i++) {
501
- var name = entries[i];
502
- if (filter && !filter(name)) continue;
503
- var fullPath = path.join(dir, name);
504
- var entry = { name: name, fullPath: fullPath };
505
- if (includeStat) {
506
- try {
507
- var stat = fs.statSync(fullPath);
508
- entry.mtimeMs = stat.mtimeMs;
509
- entry.sizeBytes = stat.size;
510
- entry.isDirectory = stat.isDirectory();
511
- entry.isFile = stat.isFile();
512
- } catch (_e) {
513
- // Entry vanished between readdir and stat — concurrent cleanup
514
- // by another process. Skip silently; caller asked for stat
515
- // info that no longer exists.
516
- continue;
517
- }
518
- }
519
- out.push(entry);
520
- }
521
- return out;
522
- }
523
-
524
- module.exports = {
525
- write: write,
526
- writeSync: writeSync,
527
- read: read,
528
- readSync: readSync,
529
- writeJson: writeJson,
530
- readJson: readJson,
531
- copy: copy,
532
- exists: exists,
533
- lock: lock,
534
- listDir: listDir,
535
- cleanOrphans: cleanOrphans,
536
- // Filesystem hygiene helpers (lifted from inline duplicates across lib/)
537
- fsync: fsync,
538
- fsyncDir: fsyncDir,
539
- ensureDir: ensureDir,
540
- copyDirRecursive: copyDirRecursive,
541
- pathTimestamp: pathTimestamp,
542
- AtomicFileError: AtomicFileError,
543
- DEFAULTS: DEFAULTS,
544
- };
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 { boot } = require("./log");
40
+ var safeBuffer = require("./safe-buffer");
41
+ var numericBounds = require("./numeric-bounds");
42
+ var safeAsync = require("./safe-async");
43
+ var retry = require("./retry");
44
+ var { FrameworkError } = require("./framework-error");
45
+
46
+ var log = boot("atomic-file");
47
+
48
+ var DEFAULTS = {
49
+ maxBytes: C.BYTES.mib(64), // 64 MiB ceiling on read
50
+ retryAttempts: 5,
51
+ retryBaseMs: 50,
52
+ retryMaxMs: C.TIME.seconds(2),
53
+ fileMode: 0o600,
54
+ computeHash: false,
55
+ lockTimeoutMs: C.TIME.seconds(30),
56
+ lockPollMs: 50,
57
+ };
58
+
59
+ class AtomicFileError extends FrameworkError {
60
+ constructor(message, code) {
61
+ super(message);
62
+ this.name = "AtomicFileError";
63
+ this.code = code || "atomic-file/error";
64
+ this.isAtomicFileError = true;
65
+ }
66
+ }
67
+
68
+ // ---- Retry helper for transient FS errors ----
69
+ //
70
+ // Routes through b.retry.withRetry with an FS-specific classifier — the
71
+ // errnos that mean "try again" on Linux/macOS/Windows file systems differ
72
+ // from the HTTP/network-shaped default classifier. atomic-file's public
73
+ // opts (retryAttempts/retryBaseMs/retryMaxMs) are mapped to the retry
74
+ // primitive's standard names; jitterFactor 0.5 reproduces the original
75
+ // `delay * (0.5 + Math.random()/2)` range of [delay/2, delay].
76
+
77
+ var TRANSIENT_FS_ERRNOS = new Set(["EBUSY", "EAGAIN", "ENFILE", "EMFILE", "EPERM"]);
78
+
79
+ function _isFsRetryable(e) {
80
+ return e != null && TRANSIENT_FS_ERRNOS.has(e.code);
81
+ }
82
+
83
+ async function _withRetry(fn, opts) {
84
+ opts = Object.assign({}, DEFAULTS, opts || {});
85
+ return retry.withRetry(function () { return fn(); }, {
86
+ maxAttempts: opts.retryAttempts,
87
+ baseDelayMs: opts.retryBaseMs,
88
+ maxDelayMs: opts.retryMaxMs,
89
+ jitterFactor: 0.5,
90
+ isRetryable: _isFsRetryable,
91
+ signal: opts.signal,
92
+ });
93
+ }
94
+
95
+ // ---- Sync helpers (best effort) ----
96
+ //
97
+ // fsync / fsyncDir / ensureDir / copyDirRecursive / pathTimestamp are
98
+ // public surface — they were previously underscore-prefixed, and other
99
+ // modules (vault-passphrase-ops, vault-rotate, backup-bundle, restore-
100
+ // bundle, restore-rollback, bundler) duplicated them inline. Hoisted
101
+ // here so the framework has one shared implementation per concern.
102
+
103
+ function fsync(fd) {
104
+ try { fs.fsyncSync(fd); } catch (_e) { /* not all platforms support fsync on every fd type */ }
105
+ }
106
+
107
+ function fsyncDir(dirPath) {
108
+ try {
109
+ var fd = fs.openSync(dirPath, "r");
110
+ try { fs.fsyncSync(fd); } catch (_e) { /* Windows rejects directory fsync */ }
111
+ finally { fs.closeSync(fd); }
112
+ } catch (_e) { /* dir fsync is best-effort across filesystems */ }
113
+ }
114
+
115
+ // Internal aliases so existing code in this file keeps working
116
+ function _fsync(fd) { return fsync(fd); }
117
+ function _fsyncDir(dirPath) { return fsyncDir(dirPath); }
118
+
119
+ // ensureDir — mkdirSync with recursive: true and a default mode of
120
+ // 0o700 (owner-only) suitable for framework data directories. Caller
121
+ // passes a different mode for less-restricted dirs.
122
+ function ensureDir(dirPath, mode) {
123
+ if (typeof dirPath !== "string" || dirPath.length === 0) {
124
+ throw new AtomicFileError("ensureDir: path must be a non-empty string", "atomic-file/bad-path");
125
+ }
126
+ fs.mkdirSync(dirPath, { recursive: true, mode: typeof mode === "number" ? mode : 0o700 });
127
+ return dirPath;
128
+ }
129
+
130
+ // copyDirRecursive — synchronous, file-by-file copy that mirrors the
131
+ // source's directory structure. Skips symlinks (operator wanting symlink
132
+ // preservation should use a real archive tool). Refuses to overwrite
133
+ // existing files at dest by default — pass opts.overwrite=true to
134
+ // replace. dest is created with mode 0o700 by default.
135
+ function copyDirRecursive(src, dest, opts) {
136
+ if (typeof src !== "string" || src.length === 0) {
137
+ throw new AtomicFileError("copyDirRecursive: src must be a non-empty string", "atomic-file/bad-path");
138
+ }
139
+ if (typeof dest !== "string" || dest.length === 0) {
140
+ throw new AtomicFileError("copyDirRecursive: dest must be a non-empty string", "atomic-file/bad-path");
141
+ }
142
+ if (!fs.existsSync(src)) {
143
+ throw new AtomicFileError("copyDirRecursive: src does not exist: " + src, "atomic-file/missing-src");
144
+ }
145
+ opts = opts || {};
146
+ var dirMode = typeof opts.dirMode === "number" ? opts.dirMode : 0o700;
147
+ var overwrite = !!opts.overwrite;
148
+ var copyFlags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
149
+
150
+ ensureDir(dest, dirMode);
151
+ var entries = fs.readdirSync(src, { withFileTypes: true });
152
+ var fileCount = 0;
153
+ var byteCount = 0;
154
+ for (var i = 0; i < entries.length; i++) {
155
+ var name = entries[i].name;
156
+ var s = path.join(src, name);
157
+ var d = path.join(dest, name);
158
+ if (entries[i].isDirectory()) {
159
+ var sub = copyDirRecursive(s, d, opts);
160
+ fileCount += sub.fileCount;
161
+ byteCount += sub.byteCount;
162
+ } else if (entries[i].isFile()) {
163
+ fs.copyFileSync(s, d, copyFlags);
164
+ try { byteCount += fs.statSync(d).size; } catch (_e) { /* size best-effort */ }
165
+ fileCount++;
166
+ }
167
+ // Symlinks, sockets, devices: deliberately skipped
168
+ }
169
+ return { fileCount: fileCount, byteCount: byteCount };
170
+ }
171
+
172
+ // pathTimestamp — filesystem-safe ISO-8601 timestamp suitable for use as
173
+ // a directory or file name on every platform. Standard
174
+ // Date.toISOString() embeds ':' and '.' which Windows reserves for
175
+ // drive letters and extension separators. This helper substitutes both
176
+ // with '-' so the result works as a path segment unmodified. String
177
+ // sort still gives chronological order.
178
+ //
179
+ // atomicFile.pathTimestamp()
180
+ // → "2026-04-27T14-00-00-123Z"
181
+ // atomicFile.pathTimestamp(new Date(0))
182
+ // → "1970-01-01T00-00-00-000Z"
183
+ function pathTimestamp(date) {
184
+ var d = (date instanceof Date) ? date : new Date();
185
+ return d.toISOString().replace(/[:.]/g, "-");
186
+ }
187
+
188
+ // ---- writeSync ----
189
+ // Synchronous atomic write — same temp+fsync+rename+dirfsync flow as
190
+ // async write(), but without the retry loop (which requires awaits).
191
+ // Use this from sync code paths (process exit handlers, module-load-time
192
+ // bootstraps). For everything else, prefer the async write().
193
+ //
194
+ // Transactional guarantee: either the rename completes (new contents fully
195
+ // visible) or the tmp file is removed (no state change). The caller never
196
+ // sees a half-written file at `filepath` and never leaves a tmp orphan
197
+ // from the current call's failure path.
198
+ function writeSync(filepath, data, opts) {
199
+ opts = Object.assign({}, DEFAULTS, opts || {});
200
+ var buf = safeBuffer.toBuffer(data, {
201
+ errorClass: AtomicFileError,
202
+ typeCode: "atomic-file/invalid-data",
203
+ typeMessage: "data must be Buffer, Uint8Array, or string",
204
+ });
205
+
206
+ var dir = path.dirname(filepath);
207
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
208
+
209
+ var tmpPath = filepath + ".tmp-" + generateToken(C.BYTES.bytes(8));
210
+ var renamed = false;
211
+ try {
212
+ var fd = fs.openSync(tmpPath, "w", opts.fileMode);
213
+ try {
214
+ var pos = 0;
215
+ while (pos < buf.length) {
216
+ pos += fs.writeSync(fd, buf, pos, buf.length - pos, null);
217
+ }
218
+ _fsync(fd);
219
+ } finally {
220
+ try { fs.closeSync(fd); } catch (_e) { /* already closed? */ }
221
+ }
222
+ fs.renameSync(tmpPath, filepath);
223
+ renamed = true;
224
+ _fsyncDir(dir);
225
+ } finally {
226
+ if (!renamed) {
227
+ // Either the write or the rename failed — remove the tmp so the next
228
+ // boot doesn't see a leaked partial file.
229
+ try { fs.unlinkSync(tmpPath); } catch (_e) { /* may not exist */ }
230
+ }
231
+ }
232
+
233
+ return {
234
+ bytesWritten: buf.length,
235
+ hash: opts.computeHash ? sha3Hash(buf) : null,
236
+ };
237
+ }
238
+
239
+ // Clean up orphan tmp files left behind by a previously-crashed process.
240
+ // Atomic writes use random tmp names (filepath + ".tmp-" + token), so a
241
+ // crash leaves a file with a name we can't predict on next boot — only
242
+ // glob and prune by age. Default: prune anything older than 5 minutes.
243
+ //
244
+ // Operators should call this at boot for every "important" filepath
245
+ // (vault.key.sealed, audit-sign.key.sealed, db.enc, etc.) BEFORE they
246
+ // start their first atomic write to that path.
247
+ function cleanOrphans(filepath, opts) {
248
+ opts = opts || {};
249
+ var olderThanMs = opts.olderThanMs != null ? opts.olderThanMs : C.TIME.minutes(5);
250
+ var dir = path.dirname(filepath);
251
+ var basename = path.basename(filepath);
252
+ var prefix = basename + ".tmp-";
253
+ var nowMs = Date.now();
254
+ var removed = 0;
255
+ var entries = listDir(dir, {
256
+ filter: function (name) { return name.startsWith(prefix); },
257
+ includeStat: true,
258
+ });
259
+ for (var i = 0; i < entries.length; i++) {
260
+ var entry = entries[i];
261
+ try {
262
+ if (nowMs - entry.mtimeMs >= olderThanMs) {
263
+ fs.unlinkSync(entry.fullPath);
264
+ removed += 1;
265
+ }
266
+ } catch (_e) { /* concurrent cleanup or permission — best effort */ }
267
+ }
268
+ return removed;
269
+ }
270
+
271
+ // ---- write ----
272
+
273
+ async function write(filepath, data, opts) {
274
+ opts = Object.assign({}, DEFAULTS, opts || {});
275
+ var buf = safeBuffer.toBuffer(data, {
276
+ errorClass: AtomicFileError,
277
+ typeCode: "atomic-file/invalid-data",
278
+ typeMessage: "data must be Buffer, Uint8Array, or string",
279
+ });
280
+
281
+ return await _withRetry(function () {
282
+ return new Promise(function (resolve, reject) {
283
+ var dir = path.dirname(filepath);
284
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
285
+ var tmpPath = filepath + ".tmp-" + generateToken(C.BYTES.bytes(8));
286
+ var renamed = false;
287
+ try {
288
+ var fd = fs.openSync(tmpPath, "w", opts.fileMode);
289
+ try {
290
+ var pos = 0;
291
+ while (pos < buf.length) {
292
+ pos += fs.writeSync(fd, buf, pos, buf.length - pos, null);
293
+ }
294
+ _fsync(fd);
295
+ } finally {
296
+ try { fs.closeSync(fd); } catch (_e) { /* already closed? */ }
297
+ }
298
+ // Atomic rename — POSIX rename is atomic on the same FS; on Windows,
299
+ // fs.renameSync uses MoveFileEx with REPLACE_EXISTING.
300
+ fs.renameSync(tmpPath, filepath);
301
+ renamed = true;
302
+ _fsyncDir(dir);
303
+ var hash = opts.computeHash ? sha3Hash(buf) : null;
304
+ resolve({ bytesWritten: buf.length, hash: hash });
305
+ } catch (e) {
306
+ reject(e);
307
+ } finally {
308
+ if (!renamed) {
309
+ try { fs.unlinkSync(tmpPath); } catch (_e) { /* may not exist */ }
310
+ }
311
+ }
312
+ });
313
+ }, opts);
314
+ }
315
+
316
+ // ---- read ----
317
+
318
+ async function read(filepath, opts) {
319
+ opts = Object.assign({}, DEFAULTS, opts || {});
320
+ return await _withRetry(function () {
321
+ return new Promise(function (resolve, reject) {
322
+ try { resolve(_readSyncCore(filepath, opts)); }
323
+ catch (e) { reject(e); }
324
+ });
325
+ }, opts);
326
+ }
327
+
328
+ // Sync variant for callers in module-init / boot paths that can't
329
+ // `await` (vault.initPlaintext, audit-sign._initPlaintext,
330
+ // db._checkRollback, db.loadOrCreateDbKey). Same semantics as
331
+ // async read: size cap, optional integrity-hash verification, ENOENT
332
+ // translation. No retry loop — sync paths can't usefully back off.
333
+ function readSync(filepath, opts) {
334
+ opts = Object.assign({}, DEFAULTS, opts || {});
335
+ return _readSyncCore(filepath, opts);
336
+ }
337
+
338
+ // maxBytes via shared lib/numeric-bounds — Infinity / NaN bypass the
339
+ // stat.size cap (any-comparison-with-Infinity-or-NaN is false).
340
+ function _validateMaxBytes(maxBytes) {
341
+ if (!numericBounds.isPositiveFiniteInt(maxBytes)) {
342
+ throw new AtomicFileError(
343
+ "atomicFile.read: maxBytes must be a positive finite integer; got " +
344
+ numericBounds.shape(maxBytes),
345
+ "atomic-file/bad-opt");
346
+ }
347
+ }
348
+
349
+ function _readSyncCore(filepath, opts) {
350
+ if (!fs.existsSync(filepath)) {
351
+ var e = new AtomicFileError("file not found: " + filepath, "atomic-file/not-found");
352
+ e.code = "ENOENT";
353
+ throw e;
354
+ }
355
+ _validateMaxBytes(opts.maxBytes);
356
+ var stat = fs.statSync(filepath);
357
+ if (stat.size > opts.maxBytes) {
358
+ throw new AtomicFileError(
359
+ "file size " + stat.size + " > maxBytes " + opts.maxBytes,
360
+ "atomic-file/too-large"
361
+ );
362
+ }
363
+ var buf = fs.readFileSync(filepath);
364
+ if (opts.expectedHash) {
365
+ var actual = sha3Hash(buf);
366
+ if (actual !== opts.expectedHash) {
367
+ throw new AtomicFileError(
368
+ "integrity check failed: expected " + opts.expectedHash + " got " + actual,
369
+ "atomic-file/integrity"
370
+ );
371
+ }
372
+ }
373
+ return opts.encoding ? buf.toString(opts.encoding) : buf;
374
+ }
375
+
376
+ // ---- writeJson / readJson ----
377
+
378
+ async function writeJson(filepath, value, opts) {
379
+ opts = opts || {};
380
+ var serialized = opts.canonical
381
+ ? safeJson.canonical(value)
382
+ : safeJson.stringify(value, { indent: opts.indent || 0 });
383
+ return await write(filepath, serialized, opts);
384
+ }
385
+
386
+ async function readJson(filepath, opts) {
387
+ opts = opts || {};
388
+ var buf = await read(filepath, opts);
389
+ var input = Buffer.isBuffer(buf) ? buf : Buffer.from(buf, "utf8");
390
+ return safeJson.parse(input, opts);
391
+ }
392
+
393
+ // ---- copy ----
394
+
395
+ async function copy(src, dst, opts) {
396
+ opts = Object.assign({}, DEFAULTS, opts || {});
397
+ var srcOpts = Object.assign({}, opts);
398
+ delete srcOpts.expectedHash; // hash check applies to dst, not src
399
+ var buf = await read(src, srcOpts);
400
+ if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf, "utf8");
401
+ return await write(dst, buf, opts);
402
+ }
403
+
404
+ // ---- exists ----
405
+
406
+ function exists(filepath) {
407
+ return fs.existsSync(filepath);
408
+ }
409
+
410
+ // ---- lock (cross-process file mutex) ----
411
+
412
+ async function lock(filepath, fn, opts) {
413
+ opts = Object.assign({}, DEFAULTS, opts || {});
414
+ var lockPath = filepath + ".lock";
415
+ var deadline = Date.now() + opts.lockTimeoutMs;
416
+ var fd = null;
417
+
418
+ while (Date.now() < deadline) {
419
+ try {
420
+ // O_CREAT | O_EXCL — fails if file exists
421
+ fd = fs.openSync(lockPath, "wx", opts.fileMode);
422
+ break;
423
+ } catch (e) {
424
+ if (e.code !== "EEXIST") throw e;
425
+ // Stale lock detection: if the .lock file is older than 5 minutes,
426
+ // assume the holding process crashed and remove it.
427
+ try {
428
+ var stat = fs.statSync(lockPath);
429
+ if (Date.now() - stat.mtimeMs > C.TIME.minutes(5)) {
430
+ try { fs.unlinkSync(lockPath); }
431
+ catch (uerr) { log.debug("stale-lock unlink failed", { path: lockPath, error: uerr.message }); }
432
+ continue;
433
+ }
434
+ } catch (_e) { /* stat raced with another process — keep waiting */ }
435
+ await safeAsync.sleep(opts.lockPollMs, { signal: opts.signal });
436
+ }
437
+ }
438
+ if (fd === null) {
439
+ throw new AtomicFileError(
440
+ "lock timeout after " + opts.lockTimeoutMs + "ms on " + filepath,
441
+ "atomic-file/lock-timeout"
442
+ );
443
+ }
444
+ try {
445
+ fs.writeSync(fd, Buffer.from(JSON.stringify({
446
+ pid: process.pid,
447
+ acquiredAt: Date.now(),
448
+ }), "utf8"));
449
+ _fsync(fd);
450
+ } catch (_e) { /* lock content best-effort */ }
451
+
452
+ try {
453
+ return await fn();
454
+ } finally {
455
+ try { fs.closeSync(fd); }
456
+ catch (cerr) { log.debug("lock fd close failed", { error: cerr.message }); }
457
+ try { fs.unlinkSync(lockPath); }
458
+ catch (uerr) { log.debug("lock release unlink failed", { path: lockPath, error: uerr.message }); }
459
+ }
460
+ }
461
+
462
+ // Single-directory listing primitive. Wraps fs.readdirSync with the
463
+ // optional-stat pattern + missing-dir tolerance + filter that callers
464
+ // across the framework were re-implementing.
465
+ //
466
+ // opts:
467
+ // filter: function(name) => boolean — name-only predicate
468
+ // includeStat: bool — adds mtimeMs / sizeBytes / isDirectory /
469
+ // isFile per entry (one fs.statSync call each).
470
+ // Skip when the caller only needs names — saves a
471
+ // syscall per entry.
472
+ // missingOk: bool — default true. Returns [] when the dir
473
+ // doesn't exist (ENOENT). Other errors throw.
474
+ //
475
+ // Returns: array of { name, fullPath } (plus stat fields when
476
+ // includeStat is true). Entries that vanish between readdir and
477
+ // stat (concurrent cleanup) are silently dropped.
478
+ //
479
+ // For recursive directory walks, callers compose listDir per
480
+ // subdirectory — the primitive doesn't recurse, so callers can apply
481
+ // per-iteration limits / filters / stop conditions cleanly.
482
+ function listDir(dir, opts) {
483
+ opts = opts || {};
484
+ var missingOk = opts.missingOk !== false;
485
+ var includeStat = opts.includeStat === true;
486
+ var filter = typeof opts.filter === "function" ? opts.filter : null;
487
+
488
+ var entries;
489
+ try {
490
+ entries = fs.readdirSync(dir);
491
+ } catch (e) {
492
+ if (missingOk && e.code === "ENOENT") return [];
493
+ throw new AtomicFileError(
494
+ "failed to list directory " + dir + ": " + e.message,
495
+ "atomic-file/list-failed"
496
+ );
497
+ }
498
+
499
+ var out = [];
500
+ for (var i = 0; i < entries.length; i++) {
501
+ var name = entries[i];
502
+ if (filter && !filter(name)) continue;
503
+ var fullPath = path.join(dir, name);
504
+ var entry = { name: name, fullPath: fullPath };
505
+ if (includeStat) {
506
+ try {
507
+ var stat = fs.statSync(fullPath);
508
+ entry.mtimeMs = stat.mtimeMs;
509
+ entry.sizeBytes = stat.size;
510
+ entry.isDirectory = stat.isDirectory();
511
+ entry.isFile = stat.isFile();
512
+ } catch (_e) {
513
+ // Entry vanished between readdir and stat — concurrent cleanup
514
+ // by another process. Skip silently; caller asked for stat
515
+ // info that no longer exists.
516
+ continue;
517
+ }
518
+ }
519
+ out.push(entry);
520
+ }
521
+ return out;
522
+ }
523
+
524
+ module.exports = {
525
+ write: write,
526
+ writeSync: writeSync,
527
+ read: read,
528
+ readSync: readSync,
529
+ writeJson: writeJson,
530
+ readJson: readJson,
531
+ copy: copy,
532
+ exists: exists,
533
+ lock: lock,
534
+ listDir: listDir,
535
+ cleanOrphans: cleanOrphans,
536
+ // Filesystem hygiene helpers (lifted from inline duplicates across lib/)
537
+ fsync: fsync,
538
+ fsyncDir: fsyncDir,
539
+ ensureDir: ensureDir,
540
+ copyDirRecursive: copyDirRecursive,
541
+ pathTimestamp: pathTimestamp,
542
+ AtomicFileError: AtomicFileError,
543
+ DEFAULTS: DEFAULTS,
544
+ };