@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
package/lib/handlers.js CHANGED
@@ -1,350 +1,350 @@
1
- "use strict";
2
- /**
3
- * Async handler primitive — buffered emit + resilient drain.
4
- *
5
- * The framework has many "fire something off, don't block the request"
6
- * surfaces: audit emissions from middleware/log-stream/external-db
7
- * hooks, webhook deliveries, log lines being shipped to an external
8
- * sink. Without a shared primitive, each surface tends toward a
9
- * fire-and-forget Promise pattern, which has well-known failure modes:
10
- *
11
- * - Errors disappear silently
12
- * - Promises leak across test/shutdown boundaries
13
- * - In cyclic dispatchers (storage emits about its own writes), the
14
- * pattern is the only thing preventing infinite recursion, but at
15
- * the cost of introducing a race
16
- * - "Did the work complete?" is unanswerable
17
- *
18
- * AsyncHandler replaces the pattern with a uniform contract that bakes
19
- * resilience in by default — operators don't opt in to robustness; it
20
- * is the framework's posture and surfaces only what the operator
21
- * actively configures (DLQ destination, retry policy adjustments, etc).
22
- *
23
- * Public API:
24
- *
25
- * handler.emit(item) sync; buffers; never throws (errors go
26
- * to onError); never returns a Promise
27
- * handler.drain(opts?) async; flushes buffer to storage with
28
- * retry + circuit breaker + DLQ;
29
- * resolves when current backlog is done
30
- * or breaker is open. opts.signal aborts.
31
- * handler.shutdown(opts?) async; final drain with timeout (default
32
- * 30s); subsequent emit() drops with
33
- * onError notification
34
- * handler.getStats() snapshot of operational metrics
35
- *
36
- * Resilience layers (all on by default):
37
- *
38
- * Retry — batches that fail flush() are retried with exponential
39
- * backoff + jitter (safeAsync.asyncRetry). Default 3 attempts.
40
- * Transient external-db hiccups recover automatically.
41
- *
42
- * Circuit breaker — if `failureThreshold` consecutive batches fail
43
- * after retry exhaustion, the breaker OPENS. Subsequent
44
- * emit() calls fast-fail (item goes to DLQ via onError);
45
- * drain() returns immediately with a CIRCUIT_OPEN reason.
46
- * After `cooldownMs` the breaker enters HALF_OPEN and one
47
- * probe batch is allowed; success closes it, failure
48
- * re-opens with another cooldown. Bounded recovery.
49
- *
50
- * Dead-letter queue — items that fail every retry land in DLQ via
51
- * the operator-supplied `deadLetter(items, error)` callback.
52
- * If no DLQ is configured, items go to onError with a
53
- * handler/dropped error. The DLQ is fire-and-forget from
54
- * the handler's perspective — the operator can write to a
55
- * file, an external queue, an audit row, anywhere durable.
56
- *
57
- * Backpressure — buffer capped at `maxBufferSize` (default 10000);
58
- * emit() over the cap goes to onError + DLQ. Operators
59
- * sizing for high-throughput audit emit larger; default is
60
- * sized for "audit emissions piggyback on request handling."
61
- *
62
- * AbortSignal — drain(opts?) accepts opts.signal; aborting cancels
63
- * the in-flight drain (current batch finishes; remaining
64
- * items stay in buffer for next drain).
65
- *
66
- * Bounded shutdown — shutdown(opts?) drains with a timeout (default
67
- * 30s). Items that don't flush in time go to DLQ or
68
- * onError; the call resolves so process exit isn't blocked.
69
- *
70
- * Recursion safety: when a handler is flushing, downstream code MAY
71
- * emit MORE items into the same handler (e.g. audit.flush writes to
72
- * external-db, which would normally audit-emit). Those additional
73
- * items land in the buffer for the NEXT cycle (NOT the current batch),
74
- * keeping batch sizes bounded. The flush function can mark its own
75
- * writes with a sentinel (e.g. `{ skipAudit: true }`) so downstream
76
- * emitters know to skip — caller-controlled, not handler concern.
77
- *
78
- * Metrics (handler.getStats()):
79
- * bufferSize, totalEmitted, totalFlushed, totalRetried,
80
- * totalDeadLettered, lastFlushDurationMs, breakerState.
81
- * Operators wire these to their monitoring; the handler does not
82
- * ship metrics anywhere on its own.
83
- */
84
-
85
- var safeAsync = require("./safe-async");
86
- var C = require("./constants");
87
- var validateOpts = require("./validate-opts");
88
- var { HandlerError } = require("./framework-error");
89
- var { boot } = require("./log");
90
-
91
- var DEFAULTS = {
92
- maxBatch: 100,
93
- maxAgeMs: C.TIME.seconds(1),
94
- maxBufferSize: C.BYTES.bytes(10000),
95
- shutdownTimeoutMs: C.TIME.seconds(30),
96
- retry: {
97
- maxAttempts: 3,
98
- baseDelayMs: 100,
99
- maxDelayMs: C.TIME.seconds(5),
100
- jitterFactor: 0.5,
101
- },
102
- breaker: {
103
- failureThreshold: 5,
104
- cooldownMs: C.TIME.seconds(30),
105
- successThreshold: 1,
106
- },
107
- };
108
-
109
- var _err = HandlerError.factory;
110
-
111
- function create(opts) {
112
- opts = opts || {};
113
- validateOpts(opts, [
114
- "name", "flush",
115
- "maxBatch", "maxAgeMs", "maxBufferSize", "shutdownTimeoutMs",
116
- "retry", "breaker",
117
- "deadLetter", "onError",
118
- ], "b.handlers");
119
- if (typeof opts.flush !== "function") {
120
- throw _err("handlers/invalid", "create requires { flush } async function");
121
- }
122
-
123
- var name = opts.name || "anonymous";
124
- var flush = opts.flush;
125
- var maxBatch = (opts.maxBatch != null) ? opts.maxBatch : DEFAULTS.maxBatch;
126
- var maxAgeMs = (opts.maxAgeMs != null) ? opts.maxAgeMs : DEFAULTS.maxAgeMs;
127
- var maxBufferSize = (opts.maxBufferSize != null) ? opts.maxBufferSize : DEFAULTS.maxBufferSize;
128
- var shutdownTimeoutMs = (opts.shutdownTimeoutMs != null) ? opts.shutdownTimeoutMs : DEFAULTS.shutdownTimeoutMs;
129
- // Handler retry: any thrown error from operator-supplied flush() is
130
- // retried until exhaustion. The default isRetryable classifier is
131
- // network-shaped and would skip generic Errors; override to retry-
132
- // anything since flush failures are caller-defined, not network errors.
133
- var retryConfig = Object.assign(
134
- { isRetryable: function () { return true; } },
135
- DEFAULTS.retry,
136
- opts.retry || {}
137
- );
138
- var breakerConfig = Object.assign({}, DEFAULTS.breaker, opts.breaker || {});
139
- var deadLetter = (typeof opts.deadLetter === "function") ? opts.deadLetter : null;
140
- var handlerLog = boot("handlers/" + name);
141
- var onError = (typeof opts.onError === "function") ? opts.onError : function (err) {
142
- handlerLog.error(err && err.message ? err.message : String(err));
143
- };
144
-
145
- // Buffer + drain coordination.
146
- var _buffer = [];
147
- var _drainPromise = null;
148
- var _ageTimer = null;
149
- var _oldestEnqueueAt = null;
150
- var _shutdown = false;
151
- var _drainMutex = new safeAsync.Mutex();
152
- var _breaker = new safeAsync.CircuitBreaker("handler:" + name, breakerConfig);
153
-
154
- // Metrics.
155
- var _totalEmitted = 0;
156
- var _totalFlushed = 0;
157
- var _totalRetried = 0;
158
- var _totalDeadLettered = 0;
159
- var _lastFlushDurationMs = 0;
160
-
161
- function _scheduleAgeFlush() {
162
- if (_ageTimer) return;
163
- _ageTimer = setTimeout(function () {
164
- _ageTimer = null;
165
- drain().catch(function (e) { onError(e, []); });
166
- }, maxAgeMs);
167
- if (typeof _ageTimer.unref === "function") _ageTimer.unref();
168
- }
169
-
170
- function _cancelAgeFlush() {
171
- if (_ageTimer) { clearTimeout(_ageTimer); _ageTimer = null; }
172
- }
173
-
174
- function _toDeadLetter(items, err) {
175
- _totalDeadLettered += items.length;
176
- if (deadLetter) {
177
- try { deadLetter(items, err); }
178
- catch (dlqErr) { onError(_err("handlers/dlq-failed",
179
- "DLQ callback for handler '" + name + "' threw", dlqErr), items); }
180
- } else {
181
- onError(_err("handlers/dropped",
182
- items.length + " item(s) dropped from handler '" + name + "' after retry exhaustion: " +
183
- (err && err.message ? err.message : String(err)), err), items);
184
- }
185
- }
186
-
187
- function emit(item) {
188
- if (_shutdown) {
189
- onError(_err("handlers/shutdown",
190
- "emit on shut-down handler '" + name + "' — item dropped"), [item]);
191
- _toDeadLetter([item], _err("handlers/shutdown", "handler is shutting down"));
192
- return;
193
- }
194
- if (_buffer.length >= maxBufferSize) {
195
- var dropErr = _err("handlers/buffer-full",
196
- "buffer for handler '" + name + "' exceeded maxBufferSize=" +
197
- maxBufferSize + " — flush is too slow / failing");
198
- onError(dropErr, [item]);
199
- _toDeadLetter([item], dropErr);
200
- return;
201
- }
202
- _totalEmitted += 1;
203
- _buffer.push(item);
204
- if (_oldestEnqueueAt === null) _oldestEnqueueAt = Date.now();
205
-
206
- if (_buffer.length >= maxBatch) {
207
- _cancelAgeFlush();
208
- drain().catch(function (e) { onError(e, []); });
209
- } else {
210
- _scheduleAgeFlush();
211
- }
212
- }
213
-
214
- // Drain the buffer with retry + breaker + DLQ. Items emitted DURING
215
- // a drain land in the buffer for the NEXT drain cycle (NOT the
216
- // current call) — see the recursion-safety note in this module's
217
- // header. Without this bound, a handler whose flush() emits MORE
218
- // items into the same handler (cluster-mode audit: writing an
219
- // audit row goes through externalDb.query, which emits a
220
- // system.externaldb.query audit event back into the buffer)
221
- // produces an unbounded loop where the buffer refills as fast as
222
- // it drains.
223
- //
224
- // Implementation: snapshot _buffer.length at start; process exactly
225
- // that many items (in batches of maxBatch). Items emitted during
226
- // the drain accrue at the tail of _buffer and are visible to the
227
- // next drain() call. Concurrent drain() calls share the in-flight
228
- // Promise.
229
- function drain(drainOpts) {
230
- if (_drainPromise) return _drainPromise;
231
- drainOpts = drainOpts || {};
232
- var signal = drainOpts.signal || null;
233
-
234
- _drainPromise = _drainMutex.runExclusive(async function () {
235
- _cancelAgeFlush();
236
- var remaining = _buffer.length;
237
- while (remaining > 0) {
238
- if (signal && signal.aborted) break;
239
- var take = Math.min(maxBatch, remaining);
240
- var batch = _buffer.splice(0, take);
241
- remaining -= batch.length;
242
- _oldestEnqueueAt = _buffer.length > 0 ? Date.now() : null;
243
-
244
- var t0 = Date.now();
245
- try {
246
- await _breaker.wrap(async function () {
247
- // Retry the entire batch on transient failures. Each retry
248
- // re-enters the operator's flush() with the same items; the
249
- // operator's flush() should be idempotent within a batch
250
- // (or accept the duplicate-write trade-off).
251
- //
252
- // The operator flush() receives an isShutdown() probe in its
253
- // second arg so it can early-exit between items if the
254
- // handler was shut down mid-batch (e.g. by a test reset).
255
- // Without that check, an in-flight flush keeps processing
256
- // batch[i++] items even after shutdown — and audit's
257
- // flush in particular writes those into whatever database
258
- // is currently bound, leaking rows into the next test's db.
259
- var attempts = 0;
260
- await safeAsync.asyncRetry(async function () {
261
- if (attempts > 0) _totalRetried += 1;
262
- attempts += 1;
263
- await flush(batch, { isShutdown: function () { return _shutdown; } });
264
- }, retryConfig);
265
- });
266
- _totalFlushed += batch.length;
267
- } catch (e) {
268
- // Retry exhausted OR breaker open. Items go to DLQ; loop
269
- // continues so other batches still attempt — unless the
270
- // breaker is open, in which case the next iteration's
271
- // _breaker.wrap will fail-fast immediately.
272
- _toDeadLetter(batch, e);
273
- if (e && e.code === "CIRCUIT_OPEN") {
274
- // Stop the drain; remaining buffered items will be tried
275
- // again on the next emit() / drain() call.
276
- break;
277
- }
278
- }
279
- _lastFlushDurationMs = Date.now() - t0;
280
- }
281
- }).then(function (v) { _drainPromise = null; return v; },
282
- function (e) { _drainPromise = null; throw e; });
283
- return _drainPromise;
284
- }
285
-
286
- async function shutdown(shutdownOpts) {
287
- shutdownOpts = shutdownOpts || {};
288
- var timeoutMs = (shutdownOpts.timeoutMs != null) ? shutdownOpts.timeoutMs : shutdownTimeoutMs;
289
- _shutdown = true;
290
- _cancelAgeFlush();
291
- try {
292
- await safeAsync.withTimeout(drain(), timeoutMs, { name: "handler:" + name + ".shutdown" });
293
- } catch (e) {
294
- // Timeout or breaker fail — DLQ whatever's left, then return.
295
- var leftover = _buffer.splice(0);
296
- if (leftover.length > 0) {
297
- _toDeadLetter(leftover, e);
298
- }
299
- }
300
- }
301
-
302
- // Synchronous "stop and drop" — for test resets and other cases
303
- // where draining buffered items would write to a stale or changing
304
- // backing store. Sets the shutdown flag, cancels the age timer, and
305
- // drops every buffered item to the DLQ. After this returns, no
306
- // setTimeout from this handler can fire and emit() drops every
307
- // future item with handlers/shutdown.
308
- function shutdownSync(reason) {
309
- _shutdown = true;
310
- _cancelAgeFlush();
311
- var dropped = _buffer.splice(0);
312
- if (dropped.length > 0) {
313
- _toDeadLetter(
314
- dropped,
315
- _err("handlers/shutdown-drop",
316
- "handler '" + name + "' shut down with " + dropped.length +
317
- " buffered item(s); items dropped: " + (reason || "no-drain shutdown"))
318
- );
319
- }
320
- }
321
-
322
- function getStats() {
323
- return {
324
- bufferSize: _buffer.length,
325
- totalEmitted: _totalEmitted,
326
- totalFlushed: _totalFlushed,
327
- totalRetried: _totalRetried,
328
- totalDeadLettered: _totalDeadLettered,
329
- lastFlushDurationMs: _lastFlushDurationMs,
330
- breakerState: _breaker.getState ? _breaker.getState() : null,
331
- isShutdown: _shutdown,
332
- };
333
- }
334
-
335
- function size() { return _buffer.length; }
336
-
337
- return {
338
- name: name,
339
- emit: emit,
340
- drain: drain,
341
- shutdown: shutdown,
342
- shutdownSync: shutdownSync,
343
- getStats: getStats,
344
- size: size,
345
- };
346
- }
347
-
348
- module.exports = {
349
- create: create,
350
- };
1
+ "use strict";
2
+ /**
3
+ * Async handler primitive — buffered emit + resilient drain.
4
+ *
5
+ * The framework has many "fire something off, don't block the request"
6
+ * surfaces: audit emissions from middleware/log-stream/external-db
7
+ * hooks, webhook deliveries, log lines being shipped to an external
8
+ * sink. Without a shared primitive, each surface tends toward a
9
+ * fire-and-forget Promise pattern, which has well-known failure modes:
10
+ *
11
+ * - Errors disappear silently
12
+ * - Promises leak across test/shutdown boundaries
13
+ * - In cyclic dispatchers (storage emits about its own writes), the
14
+ * pattern is the only thing preventing infinite recursion, but at
15
+ * the cost of introducing a race
16
+ * - "Did the work complete?" is unanswerable
17
+ *
18
+ * AsyncHandler replaces the pattern with a uniform contract that bakes
19
+ * resilience in by default — operators don't opt in to robustness; it
20
+ * is the framework's posture and surfaces only what the operator
21
+ * actively configures (DLQ destination, retry policy adjustments, etc).
22
+ *
23
+ * Public API:
24
+ *
25
+ * handler.emit(item) sync; buffers; never throws (errors go
26
+ * to onError); never returns a Promise
27
+ * handler.drain(opts?) async; flushes buffer to storage with
28
+ * retry + circuit breaker + DLQ;
29
+ * resolves when current backlog is done
30
+ * or breaker is open. opts.signal aborts.
31
+ * handler.shutdown(opts?) async; final drain with timeout (default
32
+ * 30s); subsequent emit() drops with
33
+ * onError notification
34
+ * handler.getStats() snapshot of operational metrics
35
+ *
36
+ * Resilience layers (all on by default):
37
+ *
38
+ * Retry — batches that fail flush() are retried with exponential
39
+ * backoff + jitter (safeAsync.asyncRetry). Default 3 attempts.
40
+ * Transient external-db hiccups recover automatically.
41
+ *
42
+ * Circuit breaker — if `failureThreshold` consecutive batches fail
43
+ * after retry exhaustion, the breaker OPENS. Subsequent
44
+ * emit() calls fast-fail (item goes to DLQ via onError);
45
+ * drain() returns immediately with a CIRCUIT_OPEN reason.
46
+ * After `cooldownMs` the breaker enters HALF_OPEN and one
47
+ * probe batch is allowed; success closes it, failure
48
+ * re-opens with another cooldown. Bounded recovery.
49
+ *
50
+ * Dead-letter queue — items that fail every retry land in DLQ via
51
+ * the operator-supplied `deadLetter(items, error)` callback.
52
+ * If no DLQ is configured, items go to onError with a
53
+ * handler/dropped error. The DLQ is fire-and-forget from
54
+ * the handler's perspective — the operator can write to a
55
+ * file, an external queue, an audit row, anywhere durable.
56
+ *
57
+ * Backpressure — buffer capped at `maxBufferSize` (default 10000);
58
+ * emit() over the cap goes to onError + DLQ. Operators
59
+ * sizing for high-throughput audit emit larger; default is
60
+ * sized for "audit emissions piggyback on request handling."
61
+ *
62
+ * AbortSignal — drain(opts?) accepts opts.signal; aborting cancels
63
+ * the in-flight drain (current batch finishes; remaining
64
+ * items stay in buffer for next drain).
65
+ *
66
+ * Bounded shutdown — shutdown(opts?) drains with a timeout (default
67
+ * 30s). Items that don't flush in time go to DLQ or
68
+ * onError; the call resolves so process exit isn't blocked.
69
+ *
70
+ * Recursion safety: when a handler is flushing, downstream code MAY
71
+ * emit MORE items into the same handler (e.g. audit.flush writes to
72
+ * external-db, which would normally audit-emit). Those additional
73
+ * items land in the buffer for the NEXT cycle (NOT the current batch),
74
+ * keeping batch sizes bounded. The flush function can mark its own
75
+ * writes with a sentinel (e.g. `{ skipAudit: true }`) so downstream
76
+ * emitters know to skip — caller-controlled, not handler concern.
77
+ *
78
+ * Metrics (handler.getStats()):
79
+ * bufferSize, totalEmitted, totalFlushed, totalRetried,
80
+ * totalDeadLettered, lastFlushDurationMs, breakerState.
81
+ * Operators wire these to their monitoring; the handler does not
82
+ * ship metrics anywhere on its own.
83
+ */
84
+
85
+ var safeAsync = require("./safe-async");
86
+ var C = require("./constants");
87
+ var validateOpts = require("./validate-opts");
88
+ var { HandlerError } = require("./framework-error");
89
+ var { boot } = require("./log");
90
+
91
+ var DEFAULTS = {
92
+ maxBatch: 100,
93
+ maxAgeMs: C.TIME.seconds(1),
94
+ maxBufferSize: C.BYTES.bytes(10000),
95
+ shutdownTimeoutMs: C.TIME.seconds(30),
96
+ retry: {
97
+ maxAttempts: 3,
98
+ baseDelayMs: 100,
99
+ maxDelayMs: C.TIME.seconds(5),
100
+ jitterFactor: 0.5,
101
+ },
102
+ breaker: {
103
+ failureThreshold: 5,
104
+ cooldownMs: C.TIME.seconds(30),
105
+ successThreshold: 1,
106
+ },
107
+ };
108
+
109
+ var _err = HandlerError.factory;
110
+
111
+ function create(opts) {
112
+ opts = opts || {};
113
+ validateOpts(opts, [
114
+ "name", "flush",
115
+ "maxBatch", "maxAgeMs", "maxBufferSize", "shutdownTimeoutMs",
116
+ "retry", "breaker",
117
+ "deadLetter", "onError",
118
+ ], "b.handlers");
119
+ if (typeof opts.flush !== "function") {
120
+ throw _err("handlers/invalid", "create requires { flush } async function");
121
+ }
122
+
123
+ var name = opts.name || "anonymous";
124
+ var flush = opts.flush;
125
+ var maxBatch = (opts.maxBatch != null) ? opts.maxBatch : DEFAULTS.maxBatch;
126
+ var maxAgeMs = (opts.maxAgeMs != null) ? opts.maxAgeMs : DEFAULTS.maxAgeMs;
127
+ var maxBufferSize = (opts.maxBufferSize != null) ? opts.maxBufferSize : DEFAULTS.maxBufferSize;
128
+ var shutdownTimeoutMs = (opts.shutdownTimeoutMs != null) ? opts.shutdownTimeoutMs : DEFAULTS.shutdownTimeoutMs;
129
+ // Handler retry: any thrown error from operator-supplied flush() is
130
+ // retried until exhaustion. The default isRetryable classifier is
131
+ // network-shaped and would skip generic Errors; override to retry-
132
+ // anything since flush failures are caller-defined, not network errors.
133
+ var retryConfig = Object.assign(
134
+ { isRetryable: function () { return true; } },
135
+ DEFAULTS.retry,
136
+ opts.retry || {}
137
+ );
138
+ var breakerConfig = Object.assign({}, DEFAULTS.breaker, opts.breaker || {});
139
+ var deadLetter = (typeof opts.deadLetter === "function") ? opts.deadLetter : null;
140
+ var handlerLog = boot("handlers/" + name);
141
+ var onError = (typeof opts.onError === "function") ? opts.onError : function (err) {
142
+ handlerLog.error(err && err.message ? err.message : String(err));
143
+ };
144
+
145
+ // Buffer + drain coordination.
146
+ var _buffer = [];
147
+ var _drainPromise = null;
148
+ var _ageTimer = null;
149
+ var _oldestEnqueueAt = null;
150
+ var _shutdown = false;
151
+ var _drainMutex = new safeAsync.Mutex();
152
+ var _breaker = new safeAsync.CircuitBreaker("handler:" + name, breakerConfig);
153
+
154
+ // Metrics.
155
+ var _totalEmitted = 0;
156
+ var _totalFlushed = 0;
157
+ var _totalRetried = 0;
158
+ var _totalDeadLettered = 0;
159
+ var _lastFlushDurationMs = 0;
160
+
161
+ function _scheduleAgeFlush() {
162
+ if (_ageTimer) return;
163
+ _ageTimer = setTimeout(function () {
164
+ _ageTimer = null;
165
+ drain().catch(function (e) { onError(e, []); });
166
+ }, maxAgeMs);
167
+ if (typeof _ageTimer.unref === "function") _ageTimer.unref();
168
+ }
169
+
170
+ function _cancelAgeFlush() {
171
+ if (_ageTimer) { clearTimeout(_ageTimer); _ageTimer = null; }
172
+ }
173
+
174
+ function _toDeadLetter(items, err) {
175
+ _totalDeadLettered += items.length;
176
+ if (deadLetter) {
177
+ try { deadLetter(items, err); }
178
+ catch (dlqErr) { onError(_err("handlers/dlq-failed",
179
+ "DLQ callback for handler '" + name + "' threw", dlqErr), items); }
180
+ } else {
181
+ onError(_err("handlers/dropped",
182
+ items.length + " item(s) dropped from handler '" + name + "' after retry exhaustion: " +
183
+ (err && err.message ? err.message : String(err)), err), items);
184
+ }
185
+ }
186
+
187
+ function emit(item) {
188
+ if (_shutdown) {
189
+ onError(_err("handlers/shutdown",
190
+ "emit on shut-down handler '" + name + "' — item dropped"), [item]);
191
+ _toDeadLetter([item], _err("handlers/shutdown", "handler is shutting down"));
192
+ return;
193
+ }
194
+ if (_buffer.length >= maxBufferSize) {
195
+ var dropErr = _err("handlers/buffer-full",
196
+ "buffer for handler '" + name + "' exceeded maxBufferSize=" +
197
+ maxBufferSize + " — flush is too slow / failing");
198
+ onError(dropErr, [item]);
199
+ _toDeadLetter([item], dropErr);
200
+ return;
201
+ }
202
+ _totalEmitted += 1;
203
+ _buffer.push(item);
204
+ if (_oldestEnqueueAt === null) _oldestEnqueueAt = Date.now();
205
+
206
+ if (_buffer.length >= maxBatch) {
207
+ _cancelAgeFlush();
208
+ drain().catch(function (e) { onError(e, []); });
209
+ } else {
210
+ _scheduleAgeFlush();
211
+ }
212
+ }
213
+
214
+ // Drain the buffer with retry + breaker + DLQ. Items emitted DURING
215
+ // a drain land in the buffer for the NEXT drain cycle (NOT the
216
+ // current call) — see the recursion-safety note in this module's
217
+ // header. Without this bound, a handler whose flush() emits MORE
218
+ // items into the same handler (cluster-mode audit: writing an
219
+ // audit row goes through externalDb.query, which emits a
220
+ // system.externaldb.query audit event back into the buffer)
221
+ // produces an unbounded loop where the buffer refills as fast as
222
+ // it drains.
223
+ //
224
+ // Implementation: snapshot _buffer.length at start; process exactly
225
+ // that many items (in batches of maxBatch). Items emitted during
226
+ // the drain accrue at the tail of _buffer and are visible to the
227
+ // next drain() call. Concurrent drain() calls share the in-flight
228
+ // Promise.
229
+ function drain(drainOpts) {
230
+ if (_drainPromise) return _drainPromise;
231
+ drainOpts = drainOpts || {};
232
+ var signal = drainOpts.signal || null;
233
+
234
+ _drainPromise = _drainMutex.runExclusive(async function () {
235
+ _cancelAgeFlush();
236
+ var remaining = _buffer.length;
237
+ while (remaining > 0) {
238
+ if (signal && signal.aborted) break;
239
+ var take = Math.min(maxBatch, remaining);
240
+ var batch = _buffer.splice(0, take);
241
+ remaining -= batch.length;
242
+ _oldestEnqueueAt = _buffer.length > 0 ? Date.now() : null;
243
+
244
+ var t0 = Date.now();
245
+ try {
246
+ await _breaker.wrap(async function () {
247
+ // Retry the entire batch on transient failures. Each retry
248
+ // re-enters the operator's flush() with the same items; the
249
+ // operator's flush() should be idempotent within a batch
250
+ // (or accept the duplicate-write trade-off).
251
+ //
252
+ // The operator flush() receives an isShutdown() probe in its
253
+ // second arg so it can early-exit between items if the
254
+ // handler was shut down mid-batch (e.g. by a test reset).
255
+ // Without that check, an in-flight flush keeps processing
256
+ // batch[i++] items even after shutdown — and audit's
257
+ // flush in particular writes those into whatever database
258
+ // is currently bound, leaking rows into the next test's db.
259
+ var attempts = 0;
260
+ await safeAsync.asyncRetry(async function () {
261
+ if (attempts > 0) _totalRetried += 1;
262
+ attempts += 1;
263
+ await flush(batch, { isShutdown: function () { return _shutdown; } });
264
+ }, retryConfig);
265
+ });
266
+ _totalFlushed += batch.length;
267
+ } catch (e) {
268
+ // Retry exhausted OR breaker open. Items go to DLQ; loop
269
+ // continues so other batches still attempt — unless the
270
+ // breaker is open, in which case the next iteration's
271
+ // _breaker.wrap will fail-fast immediately.
272
+ _toDeadLetter(batch, e);
273
+ if (e && e.code === "CIRCUIT_OPEN") {
274
+ // Stop the drain; remaining buffered items will be tried
275
+ // again on the next emit() / drain() call.
276
+ break;
277
+ }
278
+ }
279
+ _lastFlushDurationMs = Date.now() - t0;
280
+ }
281
+ }).then(function (v) { _drainPromise = null; return v; },
282
+ function (e) { _drainPromise = null; throw e; });
283
+ return _drainPromise;
284
+ }
285
+
286
+ async function shutdown(shutdownOpts) {
287
+ shutdownOpts = shutdownOpts || {};
288
+ var timeoutMs = (shutdownOpts.timeoutMs != null) ? shutdownOpts.timeoutMs : shutdownTimeoutMs;
289
+ _shutdown = true;
290
+ _cancelAgeFlush();
291
+ try {
292
+ await safeAsync.withTimeout(drain(), timeoutMs, { name: "handler:" + name + ".shutdown" });
293
+ } catch (e) {
294
+ // Timeout or breaker fail — DLQ whatever's left, then return.
295
+ var leftover = _buffer.splice(0);
296
+ if (leftover.length > 0) {
297
+ _toDeadLetter(leftover, e);
298
+ }
299
+ }
300
+ }
301
+
302
+ // Synchronous "stop and drop" — for test resets and other cases
303
+ // where draining buffered items would write to a stale or changing
304
+ // backing store. Sets the shutdown flag, cancels the age timer, and
305
+ // drops every buffered item to the DLQ. After this returns, no
306
+ // setTimeout from this handler can fire and emit() drops every
307
+ // future item with handlers/shutdown.
308
+ function shutdownSync(reason) {
309
+ _shutdown = true;
310
+ _cancelAgeFlush();
311
+ var dropped = _buffer.splice(0);
312
+ if (dropped.length > 0) {
313
+ _toDeadLetter(
314
+ dropped,
315
+ _err("handlers/shutdown-drop",
316
+ "handler '" + name + "' shut down with " + dropped.length +
317
+ " buffered item(s); items dropped: " + (reason || "no-drain shutdown"))
318
+ );
319
+ }
320
+ }
321
+
322
+ function getStats() {
323
+ return {
324
+ bufferSize: _buffer.length,
325
+ totalEmitted: _totalEmitted,
326
+ totalFlushed: _totalFlushed,
327
+ totalRetried: _totalRetried,
328
+ totalDeadLettered: _totalDeadLettered,
329
+ lastFlushDurationMs: _lastFlushDurationMs,
330
+ breakerState: _breaker.getState ? _breaker.getState() : null,
331
+ isShutdown: _shutdown,
332
+ };
333
+ }
334
+
335
+ function size() { return _buffer.length; }
336
+
337
+ return {
338
+ name: name,
339
+ emit: emit,
340
+ drain: drain,
341
+ shutdown: shutdown,
342
+ shutdownSync: shutdownSync,
343
+ getStats: getStats,
344
+ size: size,
345
+ };
346
+ }
347
+
348
+ module.exports = {
349
+ create: create,
350
+ };