@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
package/lib/pubsub.js CHANGED
@@ -1,429 +1,429 @@
1
- "use strict";
2
- /**
3
- * pubsub — distributed pub/sub primitive.
4
- *
5
- * Generalizes the cluster-table fan-out pattern that previously lived
6
- * inline in `lib/websocket-channels.js` and the `NOT_SUPPORTED` cache
7
- * cluster `invalidateTag` path. Three backends:
8
- *
9
- * local — in-process Map<channel, Set<handler>>; publish dispatches
10
- * SYNCHRONOUSLY before returning. Single-node deploys pay
11
- * zero coordination overhead.
12
- * cluster — shared `_blamejs_pubsub_messages` table polled at
13
- * pollIntervalMs; publish writes a row + dispatches locally;
14
- * other nodes pick up rows via `id > lastSeenId AND
15
- * publishedBy <> selfNodeId`. Default cluster mode for any
16
- * `b.cluster`-aware deploy.
17
- * redis — Redis PUB/SUB on the bespoke `lib/redis-client.js`. One
18
- * connection per pubsub instance enters subscribe mode
19
- * (demultiplexed via `setOnPushMessage`); publish goes
20
- * through a separate command-mode connection.
21
- *
22
- * Operator API:
23
- *
24
- * var ps = b.pubsub.create({
25
- * backend: 'local' | 'cluster' | 'redis' | { custom },
26
- * // cluster opts
27
- * cluster: clusterInstance,
28
- * pollIntervalMs: C.TIME.ms? — default 100ms
29
- * retentionMs: C.TIME.ms? — default 60s
30
- * pruneEveryMs: C.TIME.ms? — default 5min
31
- * // redis opts
32
- * redisUrl: string — required for redis backend
33
- * redisPassword: string?
34
- * redisUsername: string?
35
- * redisTls: boolean?
36
- * redisCa: string|Buffer?
37
- * redisServername: string?
38
- * // common
39
- * topicPrefix: string? — every publish/subscribe channel
40
- * scoped to `<topicPrefix>:<channel>`
41
- * so independent pubsub instances
42
- * sharing a backend don't collide.
43
- * audit: boolean? — default false. When true emits
44
- * `system.pubsub.publish` per call.
45
- * });
46
- *
47
- * var token = ps.subscribe(channel, function (payload, ev) {
48
- * // payload is whatever publish() received (objects survive JSON
49
- * // round-trip on remote backends; on the local backend the
50
- * // reference is passed through). ev = { channel, source: 'local'
51
- * // | 'remote', publishedBy?, publishedAt? }.
52
- * });
53
- * ps.unsubscribe(token);
54
- *
55
- * await ps.publish(channel, payload); // returns { local, remote? }
56
- *
57
- * await ps.close(); // tears down backend
58
- *
59
- * Local dispatch always happens BEFORE the publish() promise resolves,
60
- * regardless of backend — same-node subscribers see the payload with
61
- * near-zero latency. The remote write is awaited so the caller knows
62
- * the cross-node fan-out completed.
63
- *
64
- * Subscription handler errors are caught and logged via the framework's
65
- * boot logger; they never abort dispatch to other handlers on the same
66
- * channel.
67
- *
68
- * Channel naming is operator-defined — pubsub treats names as opaque
69
- * strings (with the optional topicPrefix prepended). Pattern subscribe
70
- * (Redis-style `news.*`) is exposed via `subscribePattern(pattern,
71
- * handler)`; not every backend supports it (cluster-table backend
72
- * matches client-side, redis backend uses PSUBSCRIBE, local backend
73
- * matches client-side too).
74
- */
75
- var C = require("./constants");
76
- var lazyRequire = require("./lazy-require");
77
- var safeJson = require("./safe-json");
78
- var validateOpts = require("./validate-opts");
79
- var { defineClass } = require("./framework-error");
80
-
81
- var audit = lazyRequire(function () { return require("./audit"); });
82
- var logger = lazyRequire(function () { return require("./log").boot("pubsub"); });
83
- // Backend dispatch — lazy so pubsub-cluster / pubsub-redis (which depend
84
- // on this file's exports for their own framework integration) don't
85
- // load-cycle on first import.
86
- var pubsubCluster = lazyRequire(function () { return require("./pubsub-cluster"); });
87
- var pubsubRedis = lazyRequire(function () { return require("./pubsub-redis"); });
88
-
89
- var PubsubError = defineClass("PubsubError");
90
-
91
- function _err(code, message) { return new PubsubError(code, message, true); }
92
-
93
- function _resolveBackend(opts) {
94
- var requested = opts.backend;
95
- if (requested && typeof requested === "object") {
96
- if (typeof requested.publishRemote !== "function" ||
97
- typeof requested.start !== "function" ||
98
- typeof requested.stop !== "function") {
99
- throw _err("BAD_BACKEND",
100
- "pubsub: custom backend must implement { publishRemote, start, stop }");
101
- }
102
- return Object.assign({ name: "custom" }, requested);
103
- }
104
- if (!requested || requested === "local") {
105
- return { name: "local", publishRemote: null, start: null, stop: null };
106
- }
107
- if (requested === "cluster") {
108
- return pubsubCluster().create(opts);
109
- }
110
- if (requested === "redis") {
111
- return pubsubRedis().create(opts);
112
- }
113
- throw _err("UNKNOWN_BACKEND",
114
- "pubsub: unknown backend '" + requested +
115
- "' (must be 'local', 'cluster', 'redis', or { publishRemote, start, stop })");
116
- }
117
-
118
- // Glob-style: '*' matches any single segment (no '.'); '**' matches any
119
- // suffix. Operators wanting full Redis-pattern semantics use the redis
120
- // backend's PSUBSCRIBE which the redis client routes natively.
121
- //
122
- // Hand-rolled matcher (no `new RegExp` from operator input) so a hostile
123
- // operator-supplied pattern can't compile a backtracking regex (ReDoS).
124
- // Channel length is bounded to defeat pathological inputs even on
125
- // well-behaved patterns; pubsub channel names in practice are well under
126
- // 1 KiB and the framework's own callers never exceed it.
127
- var _MAX_CHANNEL_LEN = C.BYTES.kib(1);
128
-
129
- function _matchGlobPart(part, channel, fromIdx) {
130
- // Within one '**'-bounded part, match a sequence of '*'-separated
131
- // literals against channel[fromIdx..] WITHOUT crossing '.' boundaries.
132
- // Returns the matched length, or -1 on no match.
133
- var segments = part.split("*");
134
- var pos = fromIdx;
135
- for (var i = 0; i < segments.length; i++) {
136
- var lit = segments[i];
137
- if (i === 0) {
138
- if (channel.substr(pos, lit.length) !== lit) return -1;
139
- pos += lit.length;
140
- } else if (i === segments.length - 1) {
141
- // Trailing literal: search forward in this segment (no '.' jump).
142
- var hardStop = channel.indexOf(".", pos);
143
- var searchEnd = hardStop === -1 ? channel.length : hardStop;
144
- if (lit === "") { pos = searchEnd; break; }
145
- var found = channel.lastIndexOf(lit, searchEnd - lit.length);
146
- if (found < pos) return -1;
147
- pos = found + lit.length;
148
- } else {
149
- // Middle literal: find next occurrence within current segment.
150
- var hardStop2 = channel.indexOf(".", pos);
151
- var searchEnd2 = hardStop2 === -1 ? channel.length : hardStop2;
152
- var f2 = channel.indexOf(lit, pos);
153
- if (f2 < 0 || f2 + lit.length > searchEnd2) return -1;
154
- pos = f2 + lit.length;
155
- }
156
- }
157
- return pos - fromIdx;
158
- }
159
-
160
- function _matchPattern(pattern, channel) {
161
- if (pattern === channel) return true;
162
- if (typeof channel !== "string" || channel.length > _MAX_CHANNEL_LEN) return false;
163
- if (typeof pattern !== "string" || pattern.length > _MAX_CHANNEL_LEN) return false;
164
- if (pattern.indexOf("*") === -1) return false;
165
- // Split on '**' — each part is a glob that matches without crossing '.';
166
- // between parts any char (including '.') is consumed greedily.
167
- var parts = pattern.split("**");
168
- if (parts.length === 1) {
169
- // No '**'; pattern must consume the entire channel and only '*'
170
- // (single-segment) wildcards are allowed.
171
- var len = _matchGlobPart(parts[0], channel, 0);
172
- return len === channel.length;
173
- }
174
- // Anchor the head.
175
- var headLen = _matchGlobPart(parts[0], channel, 0);
176
- if (headLen < 0) return false;
177
- var idx = headLen;
178
- // Match middle parts: each must appear somewhere after idx, in order,
179
- // separated by arbitrary content (since '**' consumes anything).
180
- for (var p = 1; p < parts.length - 1; p++) {
181
- var found = -1;
182
- for (var probe = idx; probe <= channel.length; probe++) {
183
- var n = _matchGlobPart(parts[p], channel, probe);
184
- if (n >= 0) { found = probe + n; break; }
185
- }
186
- if (found < 0) return false;
187
- idx = found;
188
- }
189
- // Tail: must reach end of channel.
190
- var tail = parts[parts.length - 1];
191
- if (tail === "") return true;
192
- for (var t = idx; t <= channel.length; t++) {
193
- var tn = _matchGlobPart(tail, channel, t);
194
- if (tn >= 0 && t + tn === channel.length) return true;
195
- }
196
- return false;
197
- }
198
-
199
- function create(opts) {
200
- opts = opts || {};
201
- validateOpts(opts, [
202
- "backend", "cluster", "audit", "topicPrefix",
203
- "pollIntervalMs", "retentionMs", "pruneEveryMs",
204
- "redisUrl", "redisPassword", "redisUsername", "redisTls",
205
- "redisCa", "redisServername",
206
- ], "b.pubsub");
207
-
208
- var topicPrefix = opts.topicPrefix || "";
209
- var auditOn = !!opts.audit;
210
- var backend = _resolveBackend(opts);
211
-
212
- // channel -> Set<token-record>; token-record is { channel, handler, isPattern }.
213
- // Tokens are objects so unsubscribe is O(1) without hashing.
214
- var exactSubs = new Map();
215
- var patternSubs = new Set();
216
- var closed = false;
217
-
218
- function _scoped(channel) {
219
- return topicPrefix ? topicPrefix + ":" + channel : channel;
220
- }
221
- function _unscope(scopedChannel) {
222
- if (!topicPrefix) return scopedChannel;
223
- if (scopedChannel.indexOf(topicPrefix + ":") === 0) {
224
- return scopedChannel.slice(topicPrefix.length + 1);
225
- }
226
- return scopedChannel;
227
- }
228
-
229
- function _localDispatch(channel, payload, source, meta) {
230
- var ev = Object.assign({ channel: channel, source: source || "local" },
231
- meta || {});
232
- var dispatched = 0;
233
- var exact = exactSubs.get(channel);
234
- if (exact) {
235
- for (var rec of exact) {
236
- try { rec.handler(payload, ev); dispatched++; }
237
- catch (e) {
238
- try { logger().warn("pubsub handler threw on '" + channel +
239
- "': " + ((e && e.message) || String(e))); }
240
- catch (_e) { /* logger best-effort */ }
241
- }
242
- }
243
- }
244
- for (var prec of patternSubs) {
245
- if (_matchPattern(prec.channel, channel)) {
246
- try { prec.handler(payload, ev); dispatched++; }
247
- catch (e) {
248
- try { logger().warn("pubsub pattern handler threw on '" +
249
- channel + "' (pattern '" + prec.channel + "'): " +
250
- ((e && e.message) || String(e))); }
251
- catch (_e) { /* logger best-effort */ }
252
- }
253
- }
254
- }
255
- return dispatched;
256
- }
257
-
258
- // Backend invokes this when a remote node's record arrives.
259
- function _onRemoteMessage(scopedChannel, rawPayload, meta) {
260
- var channel = _unscope(scopedChannel);
261
- var payload;
262
- try {
263
- payload = (typeof rawPayload === "string" ||
264
- (rawPayload && Buffer.isBuffer(rawPayload)))
265
- ? safeJson.parse(Buffer.isBuffer(rawPayload)
266
- ? rawPayload.toString("utf8")
267
- : rawPayload)
268
- : rawPayload;
269
- } catch (e) {
270
- try { logger().warn("pubsub remote payload parse failed on '" +
271
- channel + "': " + ((e && e.message) || String(e))); }
272
- catch (_e) { /* */ }
273
- return;
274
- }
275
- _localDispatch(channel, payload, "remote", meta || {});
276
- }
277
-
278
- if (typeof backend.start === "function") {
279
- backend.start(_onRemoteMessage);
280
- }
281
-
282
- // Track which scoped channels we've subscribed remotely so we can
283
- // ref-count and unsubscribe when the last local handler goes away.
284
- var remoteSubCount = new Map();
285
-
286
- async function _maybeRemoteSubscribe(scopedChannel, isPattern) {
287
- var n = (remoteSubCount.get(scopedChannel) || 0) + 1;
288
- remoteSubCount.set(scopedChannel, n);
289
- if (n === 1 && typeof backend.subscribeRemote === "function") {
290
- try { await backend.subscribeRemote(scopedChannel, isPattern); }
291
- catch (e) {
292
- // Roll back the count so a retry isn't blocked by stale bookkeeping.
293
- remoteSubCount.set(scopedChannel, n - 1);
294
- throw e;
295
- }
296
- }
297
- }
298
- async function _maybeRemoteUnsubscribe(scopedChannel, isPattern) {
299
- var n = (remoteSubCount.get(scopedChannel) || 1) - 1;
300
- if (n <= 0) {
301
- remoteSubCount.delete(scopedChannel);
302
- if (typeof backend.unsubscribeRemote === "function") {
303
- try { await backend.unsubscribeRemote(scopedChannel, isPattern); }
304
- catch (_e) { /* unsubscribe failure is informational */ }
305
- }
306
- } else {
307
- remoteSubCount.set(scopedChannel, n);
308
- }
309
- }
310
-
311
- function subscribe(channel, handler) {
312
- if (closed) throw _err("CLOSED", "pubsub.subscribe: instance closed");
313
- if (typeof channel !== "string" || channel.length === 0) {
314
- throw _err("BAD_OPT", "pubsub.subscribe: channel must be a non-empty string");
315
- }
316
- if (typeof handler !== "function") {
317
- throw _err("BAD_OPT", "pubsub.subscribe: handler must be a function");
318
- }
319
- var rec = { channel: channel, handler: handler, isPattern: false };
320
- var set = exactSubs.get(channel);
321
- if (!set) { set = new Set(); exactSubs.set(channel, set); }
322
- set.add(rec);
323
- // Remote subscribe is fire-and-forget from the caller's perspective;
324
- // the redis backend awaits the SUBSCRIBE ack internally before its
325
- // promise resolves, so a sync subscribe() that returns the token
326
- // doesn't strand the operator if they immediately publish().
327
- _maybeRemoteSubscribe(_scoped(channel), false).catch(function (e) {
328
- try { logger().warn("pubsub subscribeRemote('" + channel + "') failed: " +
329
- ((e && e.message) || String(e))); }
330
- catch (_e) { /* */ }
331
- });
332
- return rec;
333
- }
334
-
335
- function subscribePattern(pattern, handler) {
336
- if (closed) throw _err("CLOSED", "pubsub.subscribePattern: instance closed");
337
- if (typeof pattern !== "string" || pattern.length === 0) {
338
- throw _err("BAD_OPT", "pubsub.subscribePattern: pattern must be a non-empty string");
339
- }
340
- if (typeof handler !== "function") {
341
- throw _err("BAD_OPT", "pubsub.subscribePattern: handler must be a function");
342
- }
343
- var rec = { channel: pattern, handler: handler, isPattern: true };
344
- patternSubs.add(rec);
345
- _maybeRemoteSubscribe(_scoped(pattern), true).catch(function (e) {
346
- try { logger().warn("pubsub subscribePatternRemote('" + pattern + "') failed: " +
347
- ((e && e.message) || String(e))); }
348
- catch (_e) { /* */ }
349
- });
350
- return rec;
351
- }
352
-
353
- function unsubscribe(token) {
354
- if (!token || typeof token !== "object") return;
355
- if (token.isPattern) {
356
- if (patternSubs.delete(token)) {
357
- _maybeRemoteUnsubscribe(_scoped(token.channel), true);
358
- }
359
- return;
360
- }
361
- var set = exactSubs.get(token.channel);
362
- if (set && set.delete(token)) {
363
- if (set.size === 0) exactSubs.delete(token.channel);
364
- _maybeRemoteUnsubscribe(_scoped(token.channel), false);
365
- }
366
- }
367
-
368
- async function publish(channel, payload) {
369
- if (closed) throw _err("CLOSED", "pubsub.publish: instance closed");
370
- if (typeof channel !== "string" || channel.length === 0) {
371
- throw _err("BAD_OPT", "pubsub.publish: channel must be a non-empty string");
372
- }
373
- var local = _localDispatch(channel, payload, "local");
374
- var remote = 0;
375
- if (typeof backend.publishRemote === "function") {
376
- try {
377
- var rv = await backend.publishRemote(_scoped(channel), payload);
378
- remote = (rv && typeof rv === "object" && Number.isFinite(rv.remote))
379
- ? rv.remote : 1;
380
- } catch (e) {
381
- if (auditOn) {
382
- try { audit().safeEmit("system.pubsub.publish-failed", {
383
- channel: channel, error: (e && e.message) || String(e),
384
- }); } catch (_e) { /* */ }
385
- }
386
- throw e;
387
- }
388
- }
389
- if (auditOn) {
390
- try { audit().safeEmit("system.pubsub.publish", {
391
- channel: channel, localDispatched: local, remoteWritten: remote,
392
- }); } catch (_e) { /* */ }
393
- }
394
- return { local: local, remote: remote };
395
- }
396
-
397
- async function close() {
398
- if (closed) return;
399
- closed = true;
400
- if (typeof backend.stop === "function") {
401
- try { await backend.stop(); } catch (_e) { /* close failure is informational */ }
402
- }
403
- exactSubs.clear();
404
- patternSubs.clear();
405
- remoteSubCount.clear();
406
- }
407
-
408
- return {
409
- backend: function () { return backend.name; },
410
- subscribe: subscribe,
411
- subscribePattern: subscribePattern,
412
- unsubscribe: unsubscribe,
413
- publish: publish,
414
- close: close,
415
- // Diagnostic — exposed for tests + observability.
416
- _state: function () {
417
- return {
418
- backend: backend.name,
419
- exactChannels: exactSubs.size,
420
- patternCount: patternSubs.size,
421
- remoteSubCount: remoteSubCount.size,
422
- closed: closed,
423
- };
424
- },
425
- _matchPatternForTest: _matchPattern,
426
- };
427
- }
428
-
429
- module.exports = { create: create, PubsubError: PubsubError };
1
+ "use strict";
2
+ /**
3
+ * pubsub — distributed pub/sub primitive.
4
+ *
5
+ * Generalizes the cluster-table fan-out pattern that previously lived
6
+ * inline in `lib/websocket-channels.js` and the `NOT_SUPPORTED` cache
7
+ * cluster `invalidateTag` path. Three backends:
8
+ *
9
+ * local — in-process Map<channel, Set<handler>>; publish dispatches
10
+ * SYNCHRONOUSLY before returning. Single-node deploys pay
11
+ * zero coordination overhead.
12
+ * cluster — shared `_blamejs_pubsub_messages` table polled at
13
+ * pollIntervalMs; publish writes a row + dispatches locally;
14
+ * other nodes pick up rows via `id > lastSeenId AND
15
+ * publishedBy <> selfNodeId`. Default cluster mode for any
16
+ * `b.cluster`-aware deploy.
17
+ * redis — Redis PUB/SUB on the bespoke `lib/redis-client.js`. One
18
+ * connection per pubsub instance enters subscribe mode
19
+ * (demultiplexed via `setOnPushMessage`); publish goes
20
+ * through a separate command-mode connection.
21
+ *
22
+ * Operator API:
23
+ *
24
+ * var ps = b.pubsub.create({
25
+ * backend: 'local' | 'cluster' | 'redis' | { custom },
26
+ * // cluster opts
27
+ * cluster: clusterInstance,
28
+ * pollIntervalMs: C.TIME.ms? — default 100ms
29
+ * retentionMs: C.TIME.ms? — default 60s
30
+ * pruneEveryMs: C.TIME.ms? — default 5min
31
+ * // redis opts
32
+ * redisUrl: string — required for redis backend
33
+ * redisPassword: string?
34
+ * redisUsername: string?
35
+ * redisTls: boolean?
36
+ * redisCa: string|Buffer?
37
+ * redisServername: string?
38
+ * // common
39
+ * topicPrefix: string? — every publish/subscribe channel
40
+ * scoped to `<topicPrefix>:<channel>`
41
+ * so independent pubsub instances
42
+ * sharing a backend don't collide.
43
+ * audit: boolean? — default false. When true emits
44
+ * `system.pubsub.publish` per call.
45
+ * });
46
+ *
47
+ * var token = ps.subscribe(channel, function (payload, ev) {
48
+ * // payload is whatever publish() received (objects survive JSON
49
+ * // round-trip on remote backends; on the local backend the
50
+ * // reference is passed through). ev = { channel, source: 'local'
51
+ * // | 'remote', publishedBy?, publishedAt? }.
52
+ * });
53
+ * ps.unsubscribe(token);
54
+ *
55
+ * await ps.publish(channel, payload); // returns { local, remote? }
56
+ *
57
+ * await ps.close(); // tears down backend
58
+ *
59
+ * Local dispatch always happens BEFORE the publish() promise resolves,
60
+ * regardless of backend — same-node subscribers see the payload with
61
+ * near-zero latency. The remote write is awaited so the caller knows
62
+ * the cross-node fan-out completed.
63
+ *
64
+ * Subscription handler errors are caught and logged via the framework's
65
+ * boot logger; they never abort dispatch to other handlers on the same
66
+ * channel.
67
+ *
68
+ * Channel naming is operator-defined — pubsub treats names as opaque
69
+ * strings (with the optional topicPrefix prepended). Pattern subscribe
70
+ * (Redis-style `news.*`) is exposed via `subscribePattern(pattern,
71
+ * handler)`; not every backend supports it (cluster-table backend
72
+ * matches client-side, redis backend uses PSUBSCRIBE, local backend
73
+ * matches client-side too).
74
+ */
75
+ var C = require("./constants");
76
+ var lazyRequire = require("./lazy-require");
77
+ var safeJson = require("./safe-json");
78
+ var validateOpts = require("./validate-opts");
79
+ var { defineClass } = require("./framework-error");
80
+
81
+ var audit = lazyRequire(function () { return require("./audit"); });
82
+ var logger = lazyRequire(function () { return require("./log").boot("pubsub"); });
83
+ // Backend dispatch — lazy so pubsub-cluster / pubsub-redis (which depend
84
+ // on this file's exports for their own framework integration) don't
85
+ // load-cycle on first import.
86
+ var pubsubCluster = lazyRequire(function () { return require("./pubsub-cluster"); });
87
+ var pubsubRedis = lazyRequire(function () { return require("./pubsub-redis"); });
88
+
89
+ var PubsubError = defineClass("PubsubError");
90
+
91
+ function _err(code, message) { return new PubsubError(code, message, true); }
92
+
93
+ function _resolveBackend(opts) {
94
+ var requested = opts.backend;
95
+ if (requested && typeof requested === "object") {
96
+ if (typeof requested.publishRemote !== "function" ||
97
+ typeof requested.start !== "function" ||
98
+ typeof requested.stop !== "function") {
99
+ throw _err("BAD_BACKEND",
100
+ "pubsub: custom backend must implement { publishRemote, start, stop }");
101
+ }
102
+ return Object.assign({ name: "custom" }, requested);
103
+ }
104
+ if (!requested || requested === "local") {
105
+ return { name: "local", publishRemote: null, start: null, stop: null };
106
+ }
107
+ if (requested === "cluster") {
108
+ return pubsubCluster().create(opts);
109
+ }
110
+ if (requested === "redis") {
111
+ return pubsubRedis().create(opts);
112
+ }
113
+ throw _err("UNKNOWN_BACKEND",
114
+ "pubsub: unknown backend '" + requested +
115
+ "' (must be 'local', 'cluster', 'redis', or { publishRemote, start, stop })");
116
+ }
117
+
118
+ // Glob-style: '*' matches any single segment (no '.'); '**' matches any
119
+ // suffix. Operators wanting full Redis-pattern semantics use the redis
120
+ // backend's PSUBSCRIBE which the redis client routes natively.
121
+ //
122
+ // Hand-rolled matcher (no `new RegExp` from operator input) so a hostile
123
+ // operator-supplied pattern can't compile a backtracking regex (ReDoS).
124
+ // Channel length is bounded to defeat pathological inputs even on
125
+ // well-behaved patterns; pubsub channel names in practice are well under
126
+ // 1 KiB and the framework's own callers never exceed it.
127
+ var _MAX_CHANNEL_LEN = C.BYTES.kib(1);
128
+
129
+ function _matchGlobPart(part, channel, fromIdx) {
130
+ // Within one '**'-bounded part, match a sequence of '*'-separated
131
+ // literals against channel[fromIdx..] WITHOUT crossing '.' boundaries.
132
+ // Returns the matched length, or -1 on no match.
133
+ var segments = part.split("*");
134
+ var pos = fromIdx;
135
+ for (var i = 0; i < segments.length; i++) {
136
+ var lit = segments[i];
137
+ if (i === 0) {
138
+ if (channel.substr(pos, lit.length) !== lit) return -1;
139
+ pos += lit.length;
140
+ } else if (i === segments.length - 1) {
141
+ // Trailing literal: search forward in this segment (no '.' jump).
142
+ var hardStop = channel.indexOf(".", pos);
143
+ var searchEnd = hardStop === -1 ? channel.length : hardStop;
144
+ if (lit === "") { pos = searchEnd; break; }
145
+ var found = channel.lastIndexOf(lit, searchEnd - lit.length);
146
+ if (found < pos) return -1;
147
+ pos = found + lit.length;
148
+ } else {
149
+ // Middle literal: find next occurrence within current segment.
150
+ var hardStop2 = channel.indexOf(".", pos);
151
+ var searchEnd2 = hardStop2 === -1 ? channel.length : hardStop2;
152
+ var f2 = channel.indexOf(lit, pos);
153
+ if (f2 < 0 || f2 + lit.length > searchEnd2) return -1;
154
+ pos = f2 + lit.length;
155
+ }
156
+ }
157
+ return pos - fromIdx;
158
+ }
159
+
160
+ function _matchPattern(pattern, channel) {
161
+ if (pattern === channel) return true;
162
+ if (typeof channel !== "string" || channel.length > _MAX_CHANNEL_LEN) return false;
163
+ if (typeof pattern !== "string" || pattern.length > _MAX_CHANNEL_LEN) return false;
164
+ if (pattern.indexOf("*") === -1) return false;
165
+ // Split on '**' — each part is a glob that matches without crossing '.';
166
+ // between parts any char (including '.') is consumed greedily.
167
+ var parts = pattern.split("**");
168
+ if (parts.length === 1) {
169
+ // No '**'; pattern must consume the entire channel and only '*'
170
+ // (single-segment) wildcards are allowed.
171
+ var len = _matchGlobPart(parts[0], channel, 0);
172
+ return len === channel.length;
173
+ }
174
+ // Anchor the head.
175
+ var headLen = _matchGlobPart(parts[0], channel, 0);
176
+ if (headLen < 0) return false;
177
+ var idx = headLen;
178
+ // Match middle parts: each must appear somewhere after idx, in order,
179
+ // separated by arbitrary content (since '**' consumes anything).
180
+ for (var p = 1; p < parts.length - 1; p++) {
181
+ var found = -1;
182
+ for (var probe = idx; probe <= channel.length; probe++) {
183
+ var n = _matchGlobPart(parts[p], channel, probe);
184
+ if (n >= 0) { found = probe + n; break; }
185
+ }
186
+ if (found < 0) return false;
187
+ idx = found;
188
+ }
189
+ // Tail: must reach end of channel.
190
+ var tail = parts[parts.length - 1];
191
+ if (tail === "") return true;
192
+ for (var t = idx; t <= channel.length; t++) {
193
+ var tn = _matchGlobPart(tail, channel, t);
194
+ if (tn >= 0 && t + tn === channel.length) return true;
195
+ }
196
+ return false;
197
+ }
198
+
199
+ function create(opts) {
200
+ opts = opts || {};
201
+ validateOpts(opts, [
202
+ "backend", "cluster", "audit", "topicPrefix",
203
+ "pollIntervalMs", "retentionMs", "pruneEveryMs",
204
+ "redisUrl", "redisPassword", "redisUsername", "redisTls",
205
+ "redisCa", "redisServername",
206
+ ], "b.pubsub");
207
+
208
+ var topicPrefix = opts.topicPrefix || "";
209
+ var auditOn = !!opts.audit;
210
+ var backend = _resolveBackend(opts);
211
+
212
+ // channel -> Set<token-record>; token-record is { channel, handler, isPattern }.
213
+ // Tokens are objects so unsubscribe is O(1) without hashing.
214
+ var exactSubs = new Map();
215
+ var patternSubs = new Set();
216
+ var closed = false;
217
+
218
+ function _scoped(channel) {
219
+ return topicPrefix ? topicPrefix + ":" + channel : channel;
220
+ }
221
+ function _unscope(scopedChannel) {
222
+ if (!topicPrefix) return scopedChannel;
223
+ if (scopedChannel.indexOf(topicPrefix + ":") === 0) {
224
+ return scopedChannel.slice(topicPrefix.length + 1);
225
+ }
226
+ return scopedChannel;
227
+ }
228
+
229
+ function _localDispatch(channel, payload, source, meta) {
230
+ var ev = Object.assign({ channel: channel, source: source || "local" },
231
+ meta || {});
232
+ var dispatched = 0;
233
+ var exact = exactSubs.get(channel);
234
+ if (exact) {
235
+ for (var rec of exact) {
236
+ try { rec.handler(payload, ev); dispatched++; }
237
+ catch (e) {
238
+ try { logger().warn("pubsub handler threw on '" + channel +
239
+ "': " + ((e && e.message) || String(e))); }
240
+ catch (_e) { /* logger best-effort */ }
241
+ }
242
+ }
243
+ }
244
+ for (var prec of patternSubs) {
245
+ if (_matchPattern(prec.channel, channel)) {
246
+ try { prec.handler(payload, ev); dispatched++; }
247
+ catch (e) {
248
+ try { logger().warn("pubsub pattern handler threw on '" +
249
+ channel + "' (pattern '" + prec.channel + "'): " +
250
+ ((e && e.message) || String(e))); }
251
+ catch (_e) { /* logger best-effort */ }
252
+ }
253
+ }
254
+ }
255
+ return dispatched;
256
+ }
257
+
258
+ // Backend invokes this when a remote node's record arrives.
259
+ function _onRemoteMessage(scopedChannel, rawPayload, meta) {
260
+ var channel = _unscope(scopedChannel);
261
+ var payload;
262
+ try {
263
+ payload = (typeof rawPayload === "string" ||
264
+ (rawPayload && Buffer.isBuffer(rawPayload)))
265
+ ? safeJson.parse(Buffer.isBuffer(rawPayload)
266
+ ? rawPayload.toString("utf8")
267
+ : rawPayload)
268
+ : rawPayload;
269
+ } catch (e) {
270
+ try { logger().warn("pubsub remote payload parse failed on '" +
271
+ channel + "': " + ((e && e.message) || String(e))); }
272
+ catch (_e) { /* */ }
273
+ return;
274
+ }
275
+ _localDispatch(channel, payload, "remote", meta || {});
276
+ }
277
+
278
+ if (typeof backend.start === "function") {
279
+ backend.start(_onRemoteMessage);
280
+ }
281
+
282
+ // Track which scoped channels we've subscribed remotely so we can
283
+ // ref-count and unsubscribe when the last local handler goes away.
284
+ var remoteSubCount = new Map();
285
+
286
+ async function _maybeRemoteSubscribe(scopedChannel, isPattern) {
287
+ var n = (remoteSubCount.get(scopedChannel) || 0) + 1;
288
+ remoteSubCount.set(scopedChannel, n);
289
+ if (n === 1 && typeof backend.subscribeRemote === "function") {
290
+ try { await backend.subscribeRemote(scopedChannel, isPattern); }
291
+ catch (e) {
292
+ // Roll back the count so a retry isn't blocked by stale bookkeeping.
293
+ remoteSubCount.set(scopedChannel, n - 1);
294
+ throw e;
295
+ }
296
+ }
297
+ }
298
+ async function _maybeRemoteUnsubscribe(scopedChannel, isPattern) {
299
+ var n = (remoteSubCount.get(scopedChannel) || 1) - 1;
300
+ if (n <= 0) {
301
+ remoteSubCount.delete(scopedChannel);
302
+ if (typeof backend.unsubscribeRemote === "function") {
303
+ try { await backend.unsubscribeRemote(scopedChannel, isPattern); }
304
+ catch (_e) { /* unsubscribe failure is informational */ }
305
+ }
306
+ } else {
307
+ remoteSubCount.set(scopedChannel, n);
308
+ }
309
+ }
310
+
311
+ function subscribe(channel, handler) {
312
+ if (closed) throw _err("CLOSED", "pubsub.subscribe: instance closed");
313
+ if (typeof channel !== "string" || channel.length === 0) {
314
+ throw _err("BAD_OPT", "pubsub.subscribe: channel must be a non-empty string");
315
+ }
316
+ if (typeof handler !== "function") {
317
+ throw _err("BAD_OPT", "pubsub.subscribe: handler must be a function");
318
+ }
319
+ var rec = { channel: channel, handler: handler, isPattern: false };
320
+ var set = exactSubs.get(channel);
321
+ if (!set) { set = new Set(); exactSubs.set(channel, set); }
322
+ set.add(rec);
323
+ // Remote subscribe is fire-and-forget from the caller's perspective;
324
+ // the redis backend awaits the SUBSCRIBE ack internally before its
325
+ // promise resolves, so a sync subscribe() that returns the token
326
+ // doesn't strand the operator if they immediately publish().
327
+ _maybeRemoteSubscribe(_scoped(channel), false).catch(function (e) {
328
+ try { logger().warn("pubsub subscribeRemote('" + channel + "') failed: " +
329
+ ((e && e.message) || String(e))); }
330
+ catch (_e) { /* */ }
331
+ });
332
+ return rec;
333
+ }
334
+
335
+ function subscribePattern(pattern, handler) {
336
+ if (closed) throw _err("CLOSED", "pubsub.subscribePattern: instance closed");
337
+ if (typeof pattern !== "string" || pattern.length === 0) {
338
+ throw _err("BAD_OPT", "pubsub.subscribePattern: pattern must be a non-empty string");
339
+ }
340
+ if (typeof handler !== "function") {
341
+ throw _err("BAD_OPT", "pubsub.subscribePattern: handler must be a function");
342
+ }
343
+ var rec = { channel: pattern, handler: handler, isPattern: true };
344
+ patternSubs.add(rec);
345
+ _maybeRemoteSubscribe(_scoped(pattern), true).catch(function (e) {
346
+ try { logger().warn("pubsub subscribePatternRemote('" + pattern + "') failed: " +
347
+ ((e && e.message) || String(e))); }
348
+ catch (_e) { /* */ }
349
+ });
350
+ return rec;
351
+ }
352
+
353
+ function unsubscribe(token) {
354
+ if (!token || typeof token !== "object") return;
355
+ if (token.isPattern) {
356
+ if (patternSubs.delete(token)) {
357
+ _maybeRemoteUnsubscribe(_scoped(token.channel), true);
358
+ }
359
+ return;
360
+ }
361
+ var set = exactSubs.get(token.channel);
362
+ if (set && set.delete(token)) {
363
+ if (set.size === 0) exactSubs.delete(token.channel);
364
+ _maybeRemoteUnsubscribe(_scoped(token.channel), false);
365
+ }
366
+ }
367
+
368
+ async function publish(channel, payload) {
369
+ if (closed) throw _err("CLOSED", "pubsub.publish: instance closed");
370
+ if (typeof channel !== "string" || channel.length === 0) {
371
+ throw _err("BAD_OPT", "pubsub.publish: channel must be a non-empty string");
372
+ }
373
+ var local = _localDispatch(channel, payload, "local");
374
+ var remote = 0;
375
+ if (typeof backend.publishRemote === "function") {
376
+ try {
377
+ var rv = await backend.publishRemote(_scoped(channel), payload);
378
+ remote = (rv && typeof rv === "object" && Number.isFinite(rv.remote))
379
+ ? rv.remote : 1;
380
+ } catch (e) {
381
+ if (auditOn) {
382
+ try { audit().safeEmit("system.pubsub.publish-failed", {
383
+ channel: channel, error: (e && e.message) || String(e),
384
+ }); } catch (_e) { /* */ }
385
+ }
386
+ throw e;
387
+ }
388
+ }
389
+ if (auditOn) {
390
+ try { audit().safeEmit("system.pubsub.publish", {
391
+ channel: channel, localDispatched: local, remoteWritten: remote,
392
+ }); } catch (_e) { /* */ }
393
+ }
394
+ return { local: local, remote: remote };
395
+ }
396
+
397
+ async function close() {
398
+ if (closed) return;
399
+ closed = true;
400
+ if (typeof backend.stop === "function") {
401
+ try { await backend.stop(); } catch (_e) { /* close failure is informational */ }
402
+ }
403
+ exactSubs.clear();
404
+ patternSubs.clear();
405
+ remoteSubCount.clear();
406
+ }
407
+
408
+ return {
409
+ backend: function () { return backend.name; },
410
+ subscribe: subscribe,
411
+ subscribePattern: subscribePattern,
412
+ unsubscribe: unsubscribe,
413
+ publish: publish,
414
+ close: close,
415
+ // Diagnostic — exposed for tests + observability.
416
+ _state: function () {
417
+ return {
418
+ backend: backend.name,
419
+ exactChannels: exactSubs.size,
420
+ patternCount: patternSubs.size,
421
+ remoteSubCount: remoteSubCount.size,
422
+ closed: closed,
423
+ };
424
+ },
425
+ _matchPatternForTest: _matchPattern,
426
+ };
427
+ }
428
+
429
+ module.exports = { create: create, PubsubError: PubsubError };