@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,392 +1,392 @@
1
- "use strict";
2
- /**
3
- * health — liveness / readiness / startup endpoint primitive.
4
- *
5
- * Without this, every operator wires 5–20 lines of boilerplate that
6
- * gets subtly wrong: /readyz returning 200 before the DB is connected
7
- * (LB routes traffic into a half-booted process), /healthz blocking
8
- * on a slow check (orchestrator kills the pod), no graceful-shutdown
9
- * coordination (LB sends traffic into a draining process), or
10
- * leaking internal state on a public endpoint.
11
- *
12
- * Three probe tiers, each its own URL path, each its own check set:
13
- *
14
- * /healthz liveness "process is alive" — defaults to always-200
15
- * unless the operator registers a liveness
16
- * check (e.g. deadlock detector). Orchestrators
17
- * use this to decide whether to KILL the pod.
18
- * /readyz readiness "process can serve traffic" — every readiness
19
- * check must pass. Load balancers use this to
20
- * decide whether to ROUTE TRAFFIC. Default tier
21
- * for registerCheck.
22
- * /startupz startup "slow init has finished" — separate from
23
- * readiness so a slow-loading model / migration
24
- * doesn't fail the liveness probe before it
25
- * finishes. Operators register here for the
26
- * first-N-seconds-of-process startup work.
27
- *
28
- * Graceful-shutdown integration: markShuttingDown() flips readiness
29
- * (and only readiness) to 503 immediately. /healthz keeps returning
30
- * 200 throughout so the orchestrator doesn't kill the pod mid-drain.
31
- * Standard Kubernetes pattern. `b.app.shutdown()` calls this on the
32
- * health-check instance it manages; standalone callers can invoke
33
- * markShuttingDown() directly.
34
- *
35
- * var hc = b.middleware.health.create({
36
- * livenessPath: "/healthz",
37
- * readinessPath: "/readyz",
38
- * startupPath: "/startupz",
39
- * defaultTimeoutMs: 5000,
40
- * cacheMs: 0,
41
- * detailLevel: "minimal", // "minimal" | "detailed"
42
- * detailPredicate: function (req) { ... }, // operator decides
43
- * });
44
- *
45
- * hc.registerCheck("db", async function () {
46
- * return { ok: b.db.isReady(), latencyMs: ... };
47
- * }, { tier: "readiness", timeoutMs: 1000, critical: true });
48
- *
49
- * hc.registerCheck("queue", async function () {
50
- * return b.queue.isReady();
51
- * }, { tier: "readiness", critical: false });
52
- *
53
- * router.use(hc.middleware());
54
- *
55
- * // On SIGTERM:
56
- * hc.markShuttingDown();
57
- * // → /readyz returns 503 immediately; LB drains the pod;
58
- * // /healthz still 200 so orchestrator waits for clean exit.
59
- *
60
- * Check function shape — anything truthy/falsy works, but two patterns
61
- * are preferred:
62
- *
63
- * () => true | false fast path; ok flag only
64
- * () => ({ ok: bool, latencyMs?, ... }) detail visible in detailed mode
65
- *
66
- * Promise versions of either also work; per-check timeout (default
67
- * `defaultTimeoutMs`) bounds the wait. A timed-out check fails ok=false
68
- * without blocking other checks.
69
- *
70
- * Status codes:
71
- * 200 "ok" every check passed (or no checks registered)
72
- * 200 "degraded" a NON-critical check failed; service still
73
- * serving (informational status; operator can
74
- * alert on the body content)
75
- * 503 "fail" a CRITICAL check failed; LB should drain
76
- * 503 "shutting-down" readiness only; markShuttingDown was called
77
- *
78
- * Security:
79
- * - Default `detailLevel: "minimal"` returns only `{ status }` to the
80
- * public endpoint. Internal state (per-check latency, error text,
81
- * downstream service health) is not leaked.
82
- * - `detailLevel: "detailed"` enables the full check breakdown for
83
- * every probe — only suitable for endpoints behind operator auth.
84
- * - `detailPredicate(req)` lets the operator decide per-request:
85
- * return true for internal IPs / authed requests, false otherwise.
86
- * Detail predicate throwing → minimal response (fail closed).
87
- * - `Cache-Control: no-store` on every response so probes never get
88
- * cached by intermediate proxies.
89
- *
90
- * Caching:
91
- * - cacheMs > 0 caches each tier's result for that duration. With
92
- * k8s-style 1Hz probes against 10 checks, this drops the ambient
93
- * load substantially. Cache is per-tier (liveness cached separately
94
- * from readiness) — markShuttingDown bypasses readiness cache.
95
- *
96
- * Concurrent execution:
97
- * - All checks for a tier run via Promise.all with per-check withTimeout.
98
- * - One stuck check doesn't block the others; the timed-out check
99
- * fails as { ok: false, error: "timeout" } and the others complete.
100
- */
101
-
102
- var C = require("../constants");
103
- var nb = require("../numeric-bounds");
104
- var requestHelpers = require("../request-helpers");
105
- var safeAsync = require("../safe-async");
106
- var validateOpts = require("../validate-opts");
107
- var { defineClass } = require("../framework-error");
108
-
109
- var HTTP_STATUS = requestHelpers.HTTP_STATUS;
110
-
111
- var HealthError = defineClass("HealthError", { alwaysPermanent: true });
112
-
113
- var TIERS = Object.freeze(["liveness", "readiness", "startup"]);
114
- var TIER_SET = new Set(TIERS);
115
-
116
- var DEFAULT_TIMEOUT_MS = C.TIME.seconds(5);
117
-
118
- function create(opts) {
119
- opts = opts || {};
120
- validateOpts(opts, [
121
- "livenessPath", "readinessPath", "startupPath", "detailLevel",
122
- "detailPredicate", "defaultTimeoutMs", "cacheMs", "includeMeta", "version",
123
- ], "middleware.health");
124
- var livenessPath = opts.livenessPath || "/healthz";
125
- var readinessPath = opts.readinessPath || "/readyz";
126
- var startupPath = opts.startupPath || "/startupz";
127
- var detailLevel = opts.detailLevel || "minimal";
128
- if (detailLevel !== "minimal" && detailLevel !== "detailed") {
129
- throw new HealthError("health/bad-detail-level",
130
- "detailLevel must be 'minimal' or 'detailed'");
131
- }
132
- var detailPredicate = typeof opts.detailPredicate === "function" ? opts.detailPredicate : null;
133
- var defaultTimeoutMs;
134
- if (opts.defaultTimeoutMs === undefined) {
135
- defaultTimeoutMs = DEFAULT_TIMEOUT_MS;
136
- } else if (nb.isPositiveFiniteInt(opts.defaultTimeoutMs)) {
137
- defaultTimeoutMs = opts.defaultTimeoutMs;
138
- } else {
139
- throw new HealthError("health/bad-opt",
140
- "defaultTimeoutMs must be a positive finite integer; got " +
141
- nb.shape(opts.defaultTimeoutMs));
142
- }
143
- var cacheMs;
144
- if (opts.cacheMs === undefined) {
145
- cacheMs = 0;
146
- } else if (nb.isNonNegativeFiniteInt(opts.cacheMs)) {
147
- cacheMs = opts.cacheMs;
148
- } else {
149
- throw new HealthError("health/bad-opt",
150
- "cacheMs must be a non-negative finite integer; got " +
151
- nb.shape(opts.cacheMs));
152
- }
153
- var includeMeta = opts.includeMeta !== false;
154
- var version = opts.version || null;
155
-
156
- var checks = []; // { name, fn, tiers: Set, timeoutMs, critical }
157
- var shuttingDown = false;
158
- var startedAt = Date.now();
159
- var cache = {}; // tier → { result, expiresAt }
160
-
161
- function registerCheck(name, fn, copts) {
162
- if (typeof name !== "string" || name.length === 0) {
163
- throw new HealthError("health/bad-name",
164
- "registerCheck: name must be a non-empty string");
165
- }
166
- if (typeof fn !== "function") {
167
- throw new HealthError("health/bad-fn",
168
- "registerCheck: fn must be a function");
169
- }
170
- copts = copts || {};
171
- var tier = copts.tier || "readiness";
172
- var tiers = Array.isArray(tier) ? tier : [tier];
173
- for (var i = 0; i < tiers.length; i++) {
174
- if (!TIER_SET.has(tiers[i])) {
175
- throw new HealthError("health/bad-tier",
176
- "registerCheck: tier '" + tiers[i] + "' must be one of " + TIERS.join(", "));
177
- }
178
- }
179
- // Refuse duplicate names within a tier — silent overwrite makes
180
- // operator config bugs impossible to debug.
181
- for (var j = 0; j < checks.length; j++) {
182
- if (checks[j].name === name) {
183
- for (var k = 0; k < tiers.length; k++) {
184
- if (checks[j].tiers.has(tiers[k])) {
185
- throw new HealthError("health/duplicate-check",
186
- "registerCheck: check '" + name + "' already registered for tier '" + tiers[k] + "'");
187
- }
188
- }
189
- }
190
- }
191
- var timeoutMs;
192
- if (copts.timeoutMs === undefined) {
193
- timeoutMs = defaultTimeoutMs;
194
- } else if (nb.isPositiveFiniteInt(copts.timeoutMs)) {
195
- timeoutMs = copts.timeoutMs;
196
- } else {
197
- throw new HealthError("health/bad-opt",
198
- "registerCheck: timeoutMs must be a positive finite integer; got " +
199
- nb.shape(copts.timeoutMs));
200
- }
201
- checks.push({
202
- name: name,
203
- fn: fn,
204
- tiers: new Set(tiers),
205
- timeoutMs: timeoutMs,
206
- critical: copts.critical !== false,
207
- });
208
- }
209
-
210
- // Bypass cache when shuttingDown was just flipped; readiness must
211
- // surface 503 immediately, not wait for the cache window to expire.
212
- function _bypassCacheFor(tier) {
213
- return tier === "readiness" && shuttingDown;
214
- }
215
-
216
- async function runChecks(tier) {
217
- if (!TIER_SET.has(tier)) {
218
- throw new HealthError("health/bad-tier", "runChecks: tier must be one of " + TIERS.join(", "));
219
- }
220
- if (cacheMs > 0 && !_bypassCacheFor(tier)) {
221
- var cached = cache[tier];
222
- if (cached && cached.expiresAt > Date.now()) return cached.result;
223
- }
224
- var tierChecks = checks.filter(function (c) { return c.tiers.has(tier); });
225
- var promises = tierChecks.map(function (c) {
226
- var start = Date.now();
227
- // Wrap the check in withTimeout. Promise.resolve() coerces sync
228
- // returns into Promises so the timeout can be applied uniformly.
229
- return safeAsync.withTimeout(
230
- Promise.resolve().then(function () { return c.fn(); }),
231
- c.timeoutMs,
232
- { name: "health-check:" + c.name }
233
- )
234
- .then(function (raw) {
235
- var ok, detail;
236
- if (raw === true) { ok = true; detail = null; }
237
- else if (raw === false) { ok = false; detail = null; }
238
- else if (raw && typeof raw === "object") {
239
- ok = !!raw.ok;
240
- detail = raw;
241
- } else {
242
- // Truthy non-boolean / non-object → treat as ok=true. Falsy → ok=false.
243
- ok = !!raw;
244
- detail = null;
245
- }
246
- return {
247
- name: c.name,
248
- ok: ok,
249
- detail: detail,
250
- ms: Date.now() - start,
251
- critical: c.critical,
252
- };
253
- })
254
- .catch(function (err) {
255
- return {
256
- name: c.name,
257
- ok: false,
258
- error: (err && err.message) || String(err),
259
- ms: Date.now() - start,
260
- critical: c.critical,
261
- };
262
- });
263
- });
264
-
265
- var checked = await Promise.all(promises);
266
- var results = {};
267
- var anyFailed = false;
268
- var anyCriticalFailed = false;
269
- for (var i = 0; i < checked.length; i++) {
270
- var r = checked[i];
271
- var entry = { ok: r.ok, ms: r.ms };
272
- if (r.detail) {
273
- // Merge detail keys other than `ok` into the entry.
274
- var keys = Object.keys(r.detail);
275
- for (var n = 0; n < keys.length; n++) {
276
- if (keys[n] !== "ok") entry[keys[n]] = r.detail[keys[n]];
277
- }
278
- }
279
- if (r.error) entry.error = r.error;
280
- if (!r.critical) entry.critical = false;
281
- results[r.name] = entry;
282
- if (!r.ok) {
283
- anyFailed = true;
284
- if (r.critical) anyCriticalFailed = true;
285
- }
286
- }
287
-
288
- var status;
289
- if (tier === "readiness" && shuttingDown) status = "shutting-down";
290
- else if (anyCriticalFailed) status = "fail";
291
- else if (anyFailed) status = "degraded";
292
- else status = "ok";
293
-
294
- var result = { status: status, checks: results, tier: tier, shuttingDown: shuttingDown };
295
- if (cacheMs > 0 && !_bypassCacheFor(tier)) {
296
- cache[tier] = { result: result, expiresAt: Date.now() + cacheMs };
297
- }
298
- return result;
299
- }
300
-
301
- function _writeResponse(res, result, includeDetail) {
302
- var status = (result.status === "ok" || result.status === "degraded")
303
- ? HTTP_STATUS.OK : HTTP_STATUS.SERVICE_UNAVAILABLE;
304
- var payload;
305
- if (includeDetail) {
306
- payload = { status: result.status, checks: result.checks };
307
- if (includeMeta) {
308
- payload.uptime = Date.now() - startedAt;
309
- if (version) payload.version = version;
310
- }
311
- } else {
312
- payload = { status: result.status };
313
- }
314
- var body = JSON.stringify(payload);
315
- res.writeHead(status, {
316
- "Content-Type": "application/json; charset=utf-8",
317
- "Content-Length": Buffer.byteLength(body),
318
- "Cache-Control": "no-store",
319
- });
320
- res.end(body);
321
- }
322
-
323
- function _wantDetail(req) {
324
- if (detailLevel === "detailed") return true;
325
- if (!detailPredicate) return false;
326
- try { return !!detailPredicate(req); }
327
- catch (_e) { return false; /* fail-closed */ }
328
- }
329
-
330
- function middleware() {
331
- return async function health(req, res, next) {
332
- if (req.method !== "GET" && req.method !== "HEAD") return next();
333
- var url = req.url || "";
334
- var path = url.split("?")[0];
335
- var tier;
336
- if (path === livenessPath) tier = "liveness";
337
- else if (path === readinessPath) tier = "readiness";
338
- else if (path === startupPath) tier = "startup";
339
- else return next();
340
-
341
- try {
342
- var result = await runChecks(tier);
343
- if (req.method === "HEAD") {
344
- // HEAD: status code only, no body.
345
- var status = (result.status === "ok" || result.status === "degraded")
346
- ? HTTP_STATUS.OK : HTTP_STATUS.SERVICE_UNAVAILABLE;
347
- res.writeHead(status, { "Cache-Control": "no-store" });
348
- res.end();
349
- return;
350
- }
351
- _writeResponse(res, result, _wantDetail(req));
352
- } catch {
353
- // Catastrophic — write 503 with minimal detail and never leak
354
- // internal error info on a public endpoint.
355
- var body = JSON.stringify({ status: "fail", error: "health-check-internal" });
356
- res.writeHead(HTTP_STATUS.SERVICE_UNAVAILABLE, {
357
- "Content-Type": "application/json; charset=utf-8",
358
- "Content-Length": Buffer.byteLength(body),
359
- "Cache-Control": "no-store",
360
- });
361
- res.end(body);
362
- }
363
- };
364
- }
365
-
366
- function markShuttingDown() { shuttingDown = true; }
367
- function isShuttingDown() { return shuttingDown; }
368
- function uptime() { return Date.now() - startedAt; }
369
-
370
- function _resetForTest() {
371
- checks = [];
372
- shuttingDown = false;
373
- startedAt = Date.now();
374
- cache = {};
375
- }
376
-
377
- return {
378
- registerCheck: registerCheck,
379
- middleware: middleware,
380
- runChecks: runChecks,
381
- markShuttingDown: markShuttingDown,
382
- isShuttingDown: isShuttingDown,
383
- uptime: uptime,
384
- _resetForTest: _resetForTest,
385
- };
386
- }
387
-
388
- module.exports = {
389
- create: create,
390
- HealthError: HealthError,
391
- TIERS: TIERS,
392
- };
1
+ "use strict";
2
+ /**
3
+ * health — liveness / readiness / startup endpoint primitive.
4
+ *
5
+ * Without this, every operator wires 5–20 lines of boilerplate that
6
+ * gets subtly wrong: /readyz returning 200 before the DB is connected
7
+ * (LB routes traffic into a half-booted process), /healthz blocking
8
+ * on a slow check (orchestrator kills the pod), no graceful-shutdown
9
+ * coordination (LB sends traffic into a draining process), or
10
+ * leaking internal state on a public endpoint.
11
+ *
12
+ * Three probe tiers, each its own URL path, each its own check set:
13
+ *
14
+ * /healthz liveness "process is alive" — defaults to always-200
15
+ * unless the operator registers a liveness
16
+ * check (e.g. deadlock detector). Orchestrators
17
+ * use this to decide whether to KILL the pod.
18
+ * /readyz readiness "process can serve traffic" — every readiness
19
+ * check must pass. Load balancers use this to
20
+ * decide whether to ROUTE TRAFFIC. Default tier
21
+ * for registerCheck.
22
+ * /startupz startup "slow init has finished" — separate from
23
+ * readiness so a slow-loading model / migration
24
+ * doesn't fail the liveness probe before it
25
+ * finishes. Operators register here for the
26
+ * first-N-seconds-of-process startup work.
27
+ *
28
+ * Graceful-shutdown integration: markShuttingDown() flips readiness
29
+ * (and only readiness) to 503 immediately. /healthz keeps returning
30
+ * 200 throughout so the orchestrator doesn't kill the pod mid-drain.
31
+ * Standard Kubernetes pattern. `b.app.shutdown()` calls this on the
32
+ * health-check instance it manages; standalone callers can invoke
33
+ * markShuttingDown() directly.
34
+ *
35
+ * var hc = b.middleware.health.create({
36
+ * livenessPath: "/healthz",
37
+ * readinessPath: "/readyz",
38
+ * startupPath: "/startupz",
39
+ * defaultTimeoutMs: 5000,
40
+ * cacheMs: 0,
41
+ * detailLevel: "minimal", // "minimal" | "detailed"
42
+ * detailPredicate: function (req) { ... }, // operator decides
43
+ * });
44
+ *
45
+ * hc.registerCheck("db", async function () {
46
+ * return { ok: b.db.isReady(), latencyMs: ... };
47
+ * }, { tier: "readiness", timeoutMs: 1000, critical: true });
48
+ *
49
+ * hc.registerCheck("queue", async function () {
50
+ * return b.queue.isReady();
51
+ * }, { tier: "readiness", critical: false });
52
+ *
53
+ * router.use(hc.middleware());
54
+ *
55
+ * // On SIGTERM:
56
+ * hc.markShuttingDown();
57
+ * // → /readyz returns 503 immediately; LB drains the pod;
58
+ * // /healthz still 200 so orchestrator waits for clean exit.
59
+ *
60
+ * Check function shape — anything truthy/falsy works, but two patterns
61
+ * are preferred:
62
+ *
63
+ * () => true | false fast path; ok flag only
64
+ * () => ({ ok: bool, latencyMs?, ... }) detail visible in detailed mode
65
+ *
66
+ * Promise versions of either also work; per-check timeout (default
67
+ * `defaultTimeoutMs`) bounds the wait. A timed-out check fails ok=false
68
+ * without blocking other checks.
69
+ *
70
+ * Status codes:
71
+ * 200 "ok" every check passed (or no checks registered)
72
+ * 200 "degraded" a NON-critical check failed; service still
73
+ * serving (informational status; operator can
74
+ * alert on the body content)
75
+ * 503 "fail" a CRITICAL check failed; LB should drain
76
+ * 503 "shutting-down" readiness only; markShuttingDown was called
77
+ *
78
+ * Security:
79
+ * - Default `detailLevel: "minimal"` returns only `{ status }` to the
80
+ * public endpoint. Internal state (per-check latency, error text,
81
+ * downstream service health) is not leaked.
82
+ * - `detailLevel: "detailed"` enables the full check breakdown for
83
+ * every probe — only suitable for endpoints behind operator auth.
84
+ * - `detailPredicate(req)` lets the operator decide per-request:
85
+ * return true for internal IPs / authed requests, false otherwise.
86
+ * Detail predicate throwing → minimal response (fail closed).
87
+ * - `Cache-Control: no-store` on every response so probes never get
88
+ * cached by intermediate proxies.
89
+ *
90
+ * Caching:
91
+ * - cacheMs > 0 caches each tier's result for that duration. With
92
+ * k8s-style 1Hz probes against 10 checks, this drops the ambient
93
+ * load substantially. Cache is per-tier (liveness cached separately
94
+ * from readiness) — markShuttingDown bypasses readiness cache.
95
+ *
96
+ * Concurrent execution:
97
+ * - All checks for a tier run via Promise.all with per-check withTimeout.
98
+ * - One stuck check doesn't block the others; the timed-out check
99
+ * fails as { ok: false, error: "timeout" } and the others complete.
100
+ */
101
+
102
+ var C = require("../constants");
103
+ var nb = require("../numeric-bounds");
104
+ var requestHelpers = require("../request-helpers");
105
+ var safeAsync = require("../safe-async");
106
+ var validateOpts = require("../validate-opts");
107
+ var { defineClass } = require("../framework-error");
108
+
109
+ var HTTP_STATUS = requestHelpers.HTTP_STATUS;
110
+
111
+ var HealthError = defineClass("HealthError", { alwaysPermanent: true });
112
+
113
+ var TIERS = Object.freeze(["liveness", "readiness", "startup"]);
114
+ var TIER_SET = new Set(TIERS);
115
+
116
+ var DEFAULT_TIMEOUT_MS = C.TIME.seconds(5);
117
+
118
+ function create(opts) {
119
+ opts = opts || {};
120
+ validateOpts(opts, [
121
+ "livenessPath", "readinessPath", "startupPath", "detailLevel",
122
+ "detailPredicate", "defaultTimeoutMs", "cacheMs", "includeMeta", "version",
123
+ ], "middleware.health");
124
+ var livenessPath = opts.livenessPath || "/healthz";
125
+ var readinessPath = opts.readinessPath || "/readyz";
126
+ var startupPath = opts.startupPath || "/startupz";
127
+ var detailLevel = opts.detailLevel || "minimal";
128
+ if (detailLevel !== "minimal" && detailLevel !== "detailed") {
129
+ throw new HealthError("health/bad-detail-level",
130
+ "detailLevel must be 'minimal' or 'detailed'");
131
+ }
132
+ var detailPredicate = typeof opts.detailPredicate === "function" ? opts.detailPredicate : null;
133
+ var defaultTimeoutMs;
134
+ if (opts.defaultTimeoutMs === undefined) {
135
+ defaultTimeoutMs = DEFAULT_TIMEOUT_MS;
136
+ } else if (nb.isPositiveFiniteInt(opts.defaultTimeoutMs)) {
137
+ defaultTimeoutMs = opts.defaultTimeoutMs;
138
+ } else {
139
+ throw new HealthError("health/bad-opt",
140
+ "defaultTimeoutMs must be a positive finite integer; got " +
141
+ nb.shape(opts.defaultTimeoutMs));
142
+ }
143
+ var cacheMs;
144
+ if (opts.cacheMs === undefined) {
145
+ cacheMs = 0;
146
+ } else if (nb.isNonNegativeFiniteInt(opts.cacheMs)) {
147
+ cacheMs = opts.cacheMs;
148
+ } else {
149
+ throw new HealthError("health/bad-opt",
150
+ "cacheMs must be a non-negative finite integer; got " +
151
+ nb.shape(opts.cacheMs));
152
+ }
153
+ var includeMeta = opts.includeMeta !== false;
154
+ var version = opts.version || null;
155
+
156
+ var checks = []; // { name, fn, tiers: Set, timeoutMs, critical }
157
+ var shuttingDown = false;
158
+ var startedAt = Date.now();
159
+ var cache = {}; // tier → { result, expiresAt }
160
+
161
+ function registerCheck(name, fn, copts) {
162
+ if (typeof name !== "string" || name.length === 0) {
163
+ throw new HealthError("health/bad-name",
164
+ "registerCheck: name must be a non-empty string");
165
+ }
166
+ if (typeof fn !== "function") {
167
+ throw new HealthError("health/bad-fn",
168
+ "registerCheck: fn must be a function");
169
+ }
170
+ copts = copts || {};
171
+ var tier = copts.tier || "readiness";
172
+ var tiers = Array.isArray(tier) ? tier : [tier];
173
+ for (var i = 0; i < tiers.length; i++) {
174
+ if (!TIER_SET.has(tiers[i])) {
175
+ throw new HealthError("health/bad-tier",
176
+ "registerCheck: tier '" + tiers[i] + "' must be one of " + TIERS.join(", "));
177
+ }
178
+ }
179
+ // Refuse duplicate names within a tier — silent overwrite makes
180
+ // operator config bugs impossible to debug.
181
+ for (var j = 0; j < checks.length; j++) {
182
+ if (checks[j].name === name) {
183
+ for (var k = 0; k < tiers.length; k++) {
184
+ if (checks[j].tiers.has(tiers[k])) {
185
+ throw new HealthError("health/duplicate-check",
186
+ "registerCheck: check '" + name + "' already registered for tier '" + tiers[k] + "'");
187
+ }
188
+ }
189
+ }
190
+ }
191
+ var timeoutMs;
192
+ if (copts.timeoutMs === undefined) {
193
+ timeoutMs = defaultTimeoutMs;
194
+ } else if (nb.isPositiveFiniteInt(copts.timeoutMs)) {
195
+ timeoutMs = copts.timeoutMs;
196
+ } else {
197
+ throw new HealthError("health/bad-opt",
198
+ "registerCheck: timeoutMs must be a positive finite integer; got " +
199
+ nb.shape(copts.timeoutMs));
200
+ }
201
+ checks.push({
202
+ name: name,
203
+ fn: fn,
204
+ tiers: new Set(tiers),
205
+ timeoutMs: timeoutMs,
206
+ critical: copts.critical !== false,
207
+ });
208
+ }
209
+
210
+ // Bypass cache when shuttingDown was just flipped; readiness must
211
+ // surface 503 immediately, not wait for the cache window to expire.
212
+ function _bypassCacheFor(tier) {
213
+ return tier === "readiness" && shuttingDown;
214
+ }
215
+
216
+ async function runChecks(tier) {
217
+ if (!TIER_SET.has(tier)) {
218
+ throw new HealthError("health/bad-tier", "runChecks: tier must be one of " + TIERS.join(", "));
219
+ }
220
+ if (cacheMs > 0 && !_bypassCacheFor(tier)) {
221
+ var cached = cache[tier];
222
+ if (cached && cached.expiresAt > Date.now()) return cached.result;
223
+ }
224
+ var tierChecks = checks.filter(function (c) { return c.tiers.has(tier); });
225
+ var promises = tierChecks.map(function (c) {
226
+ var start = Date.now();
227
+ // Wrap the check in withTimeout. Promise.resolve() coerces sync
228
+ // returns into Promises so the timeout can be applied uniformly.
229
+ return safeAsync.withTimeout(
230
+ Promise.resolve().then(function () { return c.fn(); }),
231
+ c.timeoutMs,
232
+ { name: "health-check:" + c.name }
233
+ )
234
+ .then(function (raw) {
235
+ var ok, detail;
236
+ if (raw === true) { ok = true; detail = null; }
237
+ else if (raw === false) { ok = false; detail = null; }
238
+ else if (raw && typeof raw === "object") {
239
+ ok = !!raw.ok;
240
+ detail = raw;
241
+ } else {
242
+ // Truthy non-boolean / non-object → treat as ok=true. Falsy → ok=false.
243
+ ok = !!raw;
244
+ detail = null;
245
+ }
246
+ return {
247
+ name: c.name,
248
+ ok: ok,
249
+ detail: detail,
250
+ ms: Date.now() - start,
251
+ critical: c.critical,
252
+ };
253
+ })
254
+ .catch(function (err) {
255
+ return {
256
+ name: c.name,
257
+ ok: false,
258
+ error: (err && err.message) || String(err),
259
+ ms: Date.now() - start,
260
+ critical: c.critical,
261
+ };
262
+ });
263
+ });
264
+
265
+ var checked = await Promise.all(promises);
266
+ var results = {};
267
+ var anyFailed = false;
268
+ var anyCriticalFailed = false;
269
+ for (var i = 0; i < checked.length; i++) {
270
+ var r = checked[i];
271
+ var entry = { ok: r.ok, ms: r.ms };
272
+ if (r.detail) {
273
+ // Merge detail keys other than `ok` into the entry.
274
+ var keys = Object.keys(r.detail);
275
+ for (var n = 0; n < keys.length; n++) {
276
+ if (keys[n] !== "ok") entry[keys[n]] = r.detail[keys[n]];
277
+ }
278
+ }
279
+ if (r.error) entry.error = r.error;
280
+ if (!r.critical) entry.critical = false;
281
+ results[r.name] = entry;
282
+ if (!r.ok) {
283
+ anyFailed = true;
284
+ if (r.critical) anyCriticalFailed = true;
285
+ }
286
+ }
287
+
288
+ var status;
289
+ if (tier === "readiness" && shuttingDown) status = "shutting-down";
290
+ else if (anyCriticalFailed) status = "fail";
291
+ else if (anyFailed) status = "degraded";
292
+ else status = "ok";
293
+
294
+ var result = { status: status, checks: results, tier: tier, shuttingDown: shuttingDown };
295
+ if (cacheMs > 0 && !_bypassCacheFor(tier)) {
296
+ cache[tier] = { result: result, expiresAt: Date.now() + cacheMs };
297
+ }
298
+ return result;
299
+ }
300
+
301
+ function _writeResponse(res, result, includeDetail) {
302
+ var status = (result.status === "ok" || result.status === "degraded")
303
+ ? HTTP_STATUS.OK : HTTP_STATUS.SERVICE_UNAVAILABLE;
304
+ var payload;
305
+ if (includeDetail) {
306
+ payload = { status: result.status, checks: result.checks };
307
+ if (includeMeta) {
308
+ payload.uptime = Date.now() - startedAt;
309
+ if (version) payload.version = version;
310
+ }
311
+ } else {
312
+ payload = { status: result.status };
313
+ }
314
+ var body = JSON.stringify(payload);
315
+ res.writeHead(status, {
316
+ "Content-Type": "application/json; charset=utf-8",
317
+ "Content-Length": Buffer.byteLength(body),
318
+ "Cache-Control": "no-store",
319
+ });
320
+ res.end(body);
321
+ }
322
+
323
+ function _wantDetail(req) {
324
+ if (detailLevel === "detailed") return true;
325
+ if (!detailPredicate) return false;
326
+ try { return !!detailPredicate(req); }
327
+ catch (_e) { return false; /* fail-closed */ }
328
+ }
329
+
330
+ function middleware() {
331
+ return async function health(req, res, next) {
332
+ if (req.method !== "GET" && req.method !== "HEAD") return next();
333
+ var url = req.url || "";
334
+ var path = url.split("?")[0];
335
+ var tier;
336
+ if (path === livenessPath) tier = "liveness";
337
+ else if (path === readinessPath) tier = "readiness";
338
+ else if (path === startupPath) tier = "startup";
339
+ else return next();
340
+
341
+ try {
342
+ var result = await runChecks(tier);
343
+ if (req.method === "HEAD") {
344
+ // HEAD: status code only, no body.
345
+ var status = (result.status === "ok" || result.status === "degraded")
346
+ ? HTTP_STATUS.OK : HTTP_STATUS.SERVICE_UNAVAILABLE;
347
+ res.writeHead(status, { "Cache-Control": "no-store" });
348
+ res.end();
349
+ return;
350
+ }
351
+ _writeResponse(res, result, _wantDetail(req));
352
+ } catch {
353
+ // Catastrophic — write 503 with minimal detail and never leak
354
+ // internal error info on a public endpoint.
355
+ var body = JSON.stringify({ status: "fail", error: "health-check-internal" });
356
+ res.writeHead(HTTP_STATUS.SERVICE_UNAVAILABLE, {
357
+ "Content-Type": "application/json; charset=utf-8",
358
+ "Content-Length": Buffer.byteLength(body),
359
+ "Cache-Control": "no-store",
360
+ });
361
+ res.end(body);
362
+ }
363
+ };
364
+ }
365
+
366
+ function markShuttingDown() { shuttingDown = true; }
367
+ function isShuttingDown() { return shuttingDown; }
368
+ function uptime() { return Date.now() - startedAt; }
369
+
370
+ function _resetForTest() {
371
+ checks = [];
372
+ shuttingDown = false;
373
+ startedAt = Date.now();
374
+ cache = {};
375
+ }
376
+
377
+ return {
378
+ registerCheck: registerCheck,
379
+ middleware: middleware,
380
+ runChecks: runChecks,
381
+ markShuttingDown: markShuttingDown,
382
+ isShuttingDown: isShuttingDown,
383
+ uptime: uptime,
384
+ _resetForTest: _resetForTest,
385
+ };
386
+ }
387
+
388
+ module.exports = {
389
+ create: create,
390
+ HealthError: HealthError,
391
+ TIERS: TIERS,
392
+ };