@blamejs/core 0.6.33 → 0.6.35

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.
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ /**
3
+ * pubsub-cluster — table-polling backend for `lib/pubsub.js`.
4
+ *
5
+ * Generalizes the polling pattern previously inlined in
6
+ * `lib/websocket-channels.js`. One shared table
7
+ * `_blamejs_pubsub_messages` carries every cross-node fan-out event;
8
+ * subscribers on each node poll the table at `pollIntervalMs` and
9
+ * dispatch new rows past their last-seen id. Independent pubsub
10
+ * instances on the same database isolate via `topicPrefix` (see
11
+ * lib/pubsub.js).
12
+ *
13
+ * Trade-offs vs. the redis backend:
14
+ * - No external dependency — re-uses the cluster DB the framework
15
+ * already requires for leader election, queues, sessions, etc.
16
+ * - Latency floor is the polling interval (default 100ms). Operators
17
+ * wanting <10ms cross-node latency switch to the redis backend.
18
+ * - Survives transient network blips between app nodes; missed rows
19
+ * are picked up on the next poll until retentionMs elapses.
20
+ *
21
+ * Schema: `_blamejs_pubsub_messages (id, topic, payload, publishedAt,
22
+ * publishedBy)`. Created by `lib/cluster-storage.js` migrations.
23
+ */
24
+ var clusterStorage = require("./cluster-storage");
25
+ var C = require("./constants");
26
+ var lazyRequire = require("./lazy-require");
27
+
28
+ var logger = lazyRequire(function () { return require("./log").boot("pubsub-cluster"); });
29
+
30
+ var DEFAULT_POLL_INTERVAL_MS = 100;
31
+ var DEFAULT_RETENTION_MS = C.TIME.minutes(1);
32
+ var DEFAULT_PRUNE_EVERY_MS = C.TIME.minutes(5);
33
+
34
+ function create(opts) {
35
+ var clusterInstance = opts.cluster;
36
+ var pollIntervalMs = Number(opts.pollIntervalMs) || DEFAULT_POLL_INTERVAL_MS;
37
+ var retentionMs = Number(opts.retentionMs) || DEFAULT_RETENTION_MS;
38
+ var pruneEveryMs = Number(opts.pruneEveryMs) || DEFAULT_PRUNE_EVERY_MS;
39
+
40
+ var lastSeenId = 0;
41
+ var primed = false;
42
+ var lastPruneAt = 0;
43
+ var pollTimer = null;
44
+ var stopped = false;
45
+
46
+ function _nodeId() {
47
+ if (clusterInstance && typeof clusterInstance.currentNodeId === "function") {
48
+ return clusterInstance.currentNodeId();
49
+ }
50
+ return "single-node-local";
51
+ }
52
+
53
+ async function publishRemote(scopedChannel, payload) {
54
+ var serialized = JSON.stringify(payload);
55
+ await clusterStorage.execute(
56
+ "INSERT INTO _blamejs_pubsub_messages " +
57
+ "(topic, payload, publishedAt, publishedBy) VALUES (?, ?, ?, ?)",
58
+ [scopedChannel, serialized, Date.now(), _nodeId()]
59
+ );
60
+ return { remote: 1 };
61
+ }
62
+
63
+ async function _poll(onRemoteMessage) {
64
+ if (stopped) return;
65
+ var nodeId = _nodeId();
66
+ try {
67
+ // First poll: prime lastSeenId to the current MAX so we don't
68
+ // re-dispatch every historical row on startup.
69
+ if (!primed) {
70
+ var primer = await clusterStorage.execute(
71
+ "SELECT COALESCE(MAX(id), 0) AS maxId FROM _blamejs_pubsub_messages",
72
+ []
73
+ );
74
+ if (primer.rows && primer.rows[0]) {
75
+ lastSeenId = Number(primer.rows[0].maxId) || 0;
76
+ }
77
+ primed = true;
78
+ return;
79
+ }
80
+ var result = await clusterStorage.execute(
81
+ "SELECT id, topic, payload, publishedAt, publishedBy " +
82
+ "FROM _blamejs_pubsub_messages " +
83
+ "WHERE id > ? AND publishedBy <> ? ORDER BY id ASC",
84
+ [lastSeenId, nodeId]
85
+ );
86
+ var rows = result.rows || [];
87
+ for (var i = 0; i < rows.length; i++) {
88
+ var row = rows[i];
89
+ try {
90
+ onRemoteMessage(row.topic, row.payload, {
91
+ publishedBy: row.publishedBy,
92
+ publishedAt: Number(row.publishedAt) || null,
93
+ });
94
+ } catch (e) {
95
+ try { logger().warn("malformed pubsub fan-out row id=" + row.id +
96
+ ": " + ((e && e.message) || String(e))); }
97
+ catch (_e) { /* logger best-effort */ }
98
+ }
99
+ if (Number(row.id) > lastSeenId) lastSeenId = Number(row.id);
100
+ }
101
+
102
+ // Rate-limited prune of expired rows.
103
+ var now = Date.now();
104
+ if (now - lastPruneAt >= pruneEveryMs) {
105
+ lastPruneAt = now;
106
+ await clusterStorage.execute(
107
+ "DELETE FROM _blamejs_pubsub_messages WHERE publishedAt < ?",
108
+ [now - retentionMs]
109
+ );
110
+ }
111
+ } catch (e) {
112
+ try { logger().warn("pubsub-cluster poll failed: " +
113
+ ((e && e.message) || String(e))); }
114
+ catch (_e) { /* */ }
115
+ }
116
+ }
117
+
118
+ function start(onRemoteMessage) {
119
+ if (pollTimer) return;
120
+ stopped = false;
121
+ var tick = function () {
122
+ _poll(onRemoteMessage).then(function () {
123
+ if (stopped) return;
124
+ pollTimer = setTimeout(tick, pollIntervalMs);
125
+ if (typeof pollTimer.unref === "function") pollTimer.unref();
126
+ }, function () {
127
+ if (stopped) return;
128
+ pollTimer = setTimeout(tick, pollIntervalMs);
129
+ if (typeof pollTimer.unref === "function") pollTimer.unref();
130
+ });
131
+ };
132
+ pollTimer = setTimeout(tick, 0);
133
+ if (typeof pollTimer.unref === "function") pollTimer.unref();
134
+ }
135
+
136
+ function stop() {
137
+ stopped = true;
138
+ if (pollTimer) { clearTimeout(pollTimer); pollTimer = null; }
139
+ }
140
+
141
+ return {
142
+ name: "cluster",
143
+ publishRemote: publishRemote,
144
+ start: start,
145
+ stop: stop,
146
+ // Cluster backend doesn't need explicit subscribeRemote — every
147
+ // node sees every row. The pubsub.js wrapper still tracks the
148
+ // remoteSubCount for parity with backends that DO need it.
149
+ subscribeRemote: null,
150
+ unsubscribeRemote: null,
151
+ };
152
+ }
153
+
154
+ module.exports = { create: create };
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ /**
3
+ * pubsub-redis — Redis PUB/SUB backend for `lib/pubsub.js`.
4
+ *
5
+ * Two connections per pubsub instance:
6
+ *
7
+ * subscriberConn — placed in subscribe mode via SUBSCRIBE /
8
+ * PSUBSCRIBE. The `lib/redis-client.js` push hook
9
+ * (`setOnPushMessage`) demultiplexes server-pushed
10
+ * "message" / "pmessage" frames from
11
+ * SUBSCRIBE/UNSUBSCRIBE acks. Subscribe-mode
12
+ * connections can't issue arbitrary commands;
13
+ * splitting publisher off is mandatory.
14
+ * publisherConn — issues PUBLISH commands against the same Redis
15
+ * instance. Uses normal command pipelining.
16
+ *
17
+ * The framework's `lib/redis-client.js` is single-connection-per-create
18
+ * — both connections use the same options (URL / password / TLS / CA).
19
+ *
20
+ * Channels are passed through to Redis with the topicPrefix already
21
+ * applied by `lib/pubsub.js`; this backend doesn't add any naming
22
+ * conventions of its own.
23
+ */
24
+ var crypto = require("node:crypto");
25
+ var redisClient = require("./redis-client");
26
+ var lazyRequire = require("./lazy-require");
27
+
28
+ var logger = lazyRequire(function () { return require("./log").boot("pubsub-redis"); });
29
+
30
+ function create(opts) {
31
+ if (typeof opts.redisUrl !== "string" || opts.redisUrl.length === 0) {
32
+ throw new Error("pubsub-redis: redisUrl is required");
33
+ }
34
+ // Per-instance nonce stamped on every outgoing payload so the
35
+ // SUBSCRIBE socket can recognize its own publishes and skip
36
+ // dispatching them (the framework's pubsub.publish() already did
37
+ // the local dispatch synchronously before awaiting the remote
38
+ // write — without this filter every same-instance publish would
39
+ // double-fire local handlers).
40
+ var instanceNonce = crypto.randomBytes(8).toString("hex");
41
+
42
+ var clientOpts = {
43
+ url: opts.redisUrl,
44
+ password: opts.redisPassword,
45
+ username: opts.redisUsername,
46
+ tls: opts.redisTls,
47
+ ca: opts.redisCa,
48
+ servername: opts.redisServername,
49
+ connectTimeoutMs: opts.redisConnectTimeoutMs,
50
+ commandTimeoutMs: opts.redisCommandTimeoutMs,
51
+ maxReconnectAttempts: opts.redisMaxReconnectAttempts,
52
+ };
53
+
54
+ var subscriberConn = null;
55
+ var publisherConn = null;
56
+ var connectPromise = null;
57
+ var stopped = false;
58
+ var savedOnRemoteMessage = null;
59
+
60
+ // Inbound demultiplex — the redis-client routes "message" /
61
+ // "pmessage" frames here. Strip the {_psnode, p} envelope; if the
62
+ // nonce matches this instance, the message is our own publish
63
+ // looping back through Redis — skip dispatch (pubsub.js already
64
+ // dispatched locally in publish()). Otherwise forward to the
65
+ // dispatcher with the unwrapped payload string.
66
+ function _onPush(ev) {
67
+ if (!savedOnRemoteMessage) return;
68
+ var rawPayload = ev.payload;
69
+ var payloadStr = Buffer.isBuffer(rawPayload)
70
+ ? rawPayload.toString("utf8") : String(rawPayload);
71
+ var inner = payloadStr;
72
+ try {
73
+ var envelope = JSON.parse(payloadStr);
74
+ if (envelope && typeof envelope === "object" &&
75
+ typeof envelope._psnode === "string") {
76
+ if (envelope._psnode === instanceNonce) return; // own publish
77
+ inner = JSON.stringify(envelope.p);
78
+ }
79
+ } catch (_e) {
80
+ // Not an envelope — forward as-is for operators publishing raw
81
+ // strings via redis CLI etc.
82
+ }
83
+ try {
84
+ savedOnRemoteMessage(ev.channel, inner, {
85
+ pattern: ev.pattern || null,
86
+ });
87
+ } catch (e) {
88
+ try { logger().warn("pubsub-redis push dispatch failed: " +
89
+ ((e && e.message) || String(e))); }
90
+ catch (_e) { /* */ }
91
+ }
92
+ }
93
+
94
+ async function _ensureConnected() {
95
+ if (stopped) throw new Error("pubsub-redis: backend stopped");
96
+ if (subscriberConn && publisherConn) return;
97
+ if (connectPromise) return connectPromise;
98
+ connectPromise = (async function () {
99
+ subscriberConn = redisClient.create(Object.assign({}, clientOpts, {
100
+ onPushMessage: _onPush,
101
+ }));
102
+ publisherConn = redisClient.create(clientOpts);
103
+ await Promise.all([subscriberConn.connect(), publisherConn.connect()]);
104
+ })();
105
+ try { await connectPromise; }
106
+ finally { connectPromise = null; }
107
+ }
108
+
109
+ async function publishRemote(scopedChannel, payload) {
110
+ await _ensureConnected();
111
+ var serialized = JSON.stringify({ _psnode: instanceNonce, p: payload });
112
+ var n = await publisherConn.command("PUBLISH", scopedChannel, serialized);
113
+ return { remote: Number(n) || 0 };
114
+ }
115
+
116
+ async function subscribeRemote(scopedChannel, isPattern) {
117
+ await _ensureConnected();
118
+ var cmd = isPattern ? "PSUBSCRIBE" : "SUBSCRIBE";
119
+ await subscriberConn.command(cmd, scopedChannel);
120
+ }
121
+
122
+ async function unsubscribeRemote(scopedChannel, isPattern) {
123
+ if (!subscriberConn || !subscriberConn.isOpen()) return;
124
+ var cmd = isPattern ? "PUNSUBSCRIBE" : "UNSUBSCRIBE";
125
+ try { await subscriberConn.command(cmd, scopedChannel); }
126
+ catch (_e) { /* unsubscribe failure is informational */ }
127
+ }
128
+
129
+ function start(onRemoteMessage) {
130
+ savedOnRemoteMessage = onRemoteMessage;
131
+ // Lazy connect — first subscribe / publish opens the sockets. This
132
+ // keeps `b.pubsub.create()` synchronous-safe on a misconfigured
133
+ // redis URL: the validation throw lands on the first publish/subscribe
134
+ // call, where the operator can catch it.
135
+ }
136
+
137
+ async function stop() {
138
+ stopped = true;
139
+ savedOnRemoteMessage = null;
140
+ if (subscriberConn) {
141
+ try { await subscriberConn.close(); } catch (_e) {}
142
+ subscriberConn = null;
143
+ }
144
+ if (publisherConn) {
145
+ try { await publisherConn.close(); } catch (_e) {}
146
+ publisherConn = null;
147
+ }
148
+ }
149
+
150
+ return {
151
+ name: "redis",
152
+ publishRemote: publishRemote,
153
+ subscribeRemote: subscribeRemote,
154
+ unsubscribeRemote: unsubscribeRemote,
155
+ start: start,
156
+ stop: stop,
157
+ };
158
+ }
159
+
160
+ module.exports = { create: create };
package/lib/pubsub.js ADDED
@@ -0,0 +1,357 @@
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 lazyRequire = require("./lazy-require");
76
+ var safeJson = require("./safe-json");
77
+ var validateOpts = require("./validate-opts");
78
+ var { defineClass } = require("./framework-error");
79
+
80
+ var audit = lazyRequire(function () { return require("./audit"); });
81
+ var logger = lazyRequire(function () { return require("./log").boot("pubsub"); });
82
+
83
+ var PubsubError = defineClass("PubsubError");
84
+
85
+ function _err(code, message) { return new PubsubError(code, message, true); }
86
+
87
+ function _resolveBackend(opts) {
88
+ var requested = opts.backend;
89
+ if (requested && typeof requested === "object") {
90
+ if (typeof requested.publishRemote !== "function" ||
91
+ typeof requested.start !== "function" ||
92
+ typeof requested.stop !== "function") {
93
+ throw _err("BAD_BACKEND",
94
+ "pubsub: custom backend must implement { publishRemote, start, stop }");
95
+ }
96
+ return Object.assign({ name: "custom" }, requested);
97
+ }
98
+ if (!requested || requested === "local") {
99
+ return { name: "local", publishRemote: null, start: null, stop: null };
100
+ }
101
+ if (requested === "cluster") {
102
+ return require("./pubsub-cluster").create(opts);
103
+ }
104
+ if (requested === "redis") {
105
+ return require("./pubsub-redis").create(opts);
106
+ }
107
+ throw _err("UNKNOWN_BACKEND",
108
+ "pubsub: unknown backend '" + requested +
109
+ "' (must be 'local', 'cluster', 'redis', or { publishRemote, start, stop })");
110
+ }
111
+
112
+ function _matchPattern(pattern, channel) {
113
+ // Glob-style: '*' matches any single segment; '**' matches any
114
+ // suffix. Operators wanting full Redis-pattern semantics use the
115
+ // redis backend's PSUBSCRIBE which the redis client routes natively.
116
+ if (pattern === channel) return true;
117
+ if (pattern.indexOf("*") === -1) return false;
118
+ var rx = "^" + pattern
119
+ .split("**").map(function (p) {
120
+ return p.split("*").map(function (s) {
121
+ return s.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
122
+ }).join("[^.]*");
123
+ }).join(".*") + "$";
124
+ return new RegExp(rx).test(channel);
125
+ }
126
+
127
+ function create(opts) {
128
+ opts = opts || {};
129
+ validateOpts(opts, [
130
+ "backend", "cluster", "audit", "topicPrefix",
131
+ "pollIntervalMs", "retentionMs", "pruneEveryMs",
132
+ "redisUrl", "redisPassword", "redisUsername", "redisTls",
133
+ "redisCa", "redisServername",
134
+ ], "b.pubsub");
135
+
136
+ var topicPrefix = opts.topicPrefix || "";
137
+ var auditOn = !!opts.audit;
138
+ var backend = _resolveBackend(opts);
139
+
140
+ // channel -> Set<token-record>; token-record is { channel, handler, isPattern }.
141
+ // Tokens are objects so unsubscribe is O(1) without hashing.
142
+ var exactSubs = new Map();
143
+ var patternSubs = new Set();
144
+ var closed = false;
145
+
146
+ function _scoped(channel) {
147
+ return topicPrefix ? topicPrefix + ":" + channel : channel;
148
+ }
149
+ function _unscope(scopedChannel) {
150
+ if (!topicPrefix) return scopedChannel;
151
+ if (scopedChannel.indexOf(topicPrefix + ":") === 0) {
152
+ return scopedChannel.slice(topicPrefix.length + 1);
153
+ }
154
+ return scopedChannel;
155
+ }
156
+
157
+ function _localDispatch(channel, payload, source, meta) {
158
+ var ev = Object.assign({ channel: channel, source: source || "local" },
159
+ meta || {});
160
+ var dispatched = 0;
161
+ var exact = exactSubs.get(channel);
162
+ if (exact) {
163
+ for (var rec of exact) {
164
+ try { rec.handler(payload, ev); dispatched++; }
165
+ catch (e) {
166
+ try { logger().warn("pubsub handler threw on '" + channel +
167
+ "': " + ((e && e.message) || String(e))); }
168
+ catch (_e) { /* logger best-effort */ }
169
+ }
170
+ }
171
+ }
172
+ for (var prec of patternSubs) {
173
+ if (_matchPattern(prec.channel, channel)) {
174
+ try { prec.handler(payload, ev); dispatched++; }
175
+ catch (e) {
176
+ try { logger().warn("pubsub pattern handler threw on '" +
177
+ channel + "' (pattern '" + prec.channel + "'): " +
178
+ ((e && e.message) || String(e))); }
179
+ catch (_e) { /* logger best-effort */ }
180
+ }
181
+ }
182
+ }
183
+ return dispatched;
184
+ }
185
+
186
+ // Backend invokes this when a remote node's record arrives.
187
+ function _onRemoteMessage(scopedChannel, rawPayload, meta) {
188
+ var channel = _unscope(scopedChannel);
189
+ var payload;
190
+ try {
191
+ payload = (typeof rawPayload === "string" ||
192
+ (rawPayload && Buffer.isBuffer(rawPayload)))
193
+ ? safeJson.parse(Buffer.isBuffer(rawPayload)
194
+ ? rawPayload.toString("utf8")
195
+ : rawPayload)
196
+ : rawPayload;
197
+ } catch (e) {
198
+ try { logger().warn("pubsub remote payload parse failed on '" +
199
+ channel + "': " + ((e && e.message) || String(e))); }
200
+ catch (_e) { /* */ }
201
+ return;
202
+ }
203
+ _localDispatch(channel, payload, "remote", meta || {});
204
+ }
205
+
206
+ if (typeof backend.start === "function") {
207
+ backend.start(_onRemoteMessage);
208
+ }
209
+
210
+ // Track which scoped channels we've subscribed remotely so we can
211
+ // ref-count and unsubscribe when the last local handler goes away.
212
+ var remoteSubCount = new Map();
213
+
214
+ async function _maybeRemoteSubscribe(scopedChannel, isPattern) {
215
+ var n = (remoteSubCount.get(scopedChannel) || 0) + 1;
216
+ remoteSubCount.set(scopedChannel, n);
217
+ if (n === 1 && typeof backend.subscribeRemote === "function") {
218
+ try { await backend.subscribeRemote(scopedChannel, isPattern); }
219
+ catch (e) {
220
+ // Roll back the count so a retry isn't blocked by stale bookkeeping.
221
+ remoteSubCount.set(scopedChannel, n - 1);
222
+ throw e;
223
+ }
224
+ }
225
+ }
226
+ async function _maybeRemoteUnsubscribe(scopedChannel, isPattern) {
227
+ var n = (remoteSubCount.get(scopedChannel) || 1) - 1;
228
+ if (n <= 0) {
229
+ remoteSubCount.delete(scopedChannel);
230
+ if (typeof backend.unsubscribeRemote === "function") {
231
+ try { await backend.unsubscribeRemote(scopedChannel, isPattern); }
232
+ catch (_e) { /* unsubscribe failure is informational */ }
233
+ }
234
+ } else {
235
+ remoteSubCount.set(scopedChannel, n);
236
+ }
237
+ }
238
+
239
+ function subscribe(channel, handler) {
240
+ if (closed) throw _err("CLOSED", "pubsub.subscribe: instance closed");
241
+ if (typeof channel !== "string" || channel.length === 0) {
242
+ throw _err("BAD_OPT", "pubsub.subscribe: channel must be a non-empty string");
243
+ }
244
+ if (typeof handler !== "function") {
245
+ throw _err("BAD_OPT", "pubsub.subscribe: handler must be a function");
246
+ }
247
+ var rec = { channel: channel, handler: handler, isPattern: false };
248
+ var set = exactSubs.get(channel);
249
+ if (!set) { set = new Set(); exactSubs.set(channel, set); }
250
+ set.add(rec);
251
+ // Remote subscribe is fire-and-forget from the caller's perspective;
252
+ // the redis backend awaits the SUBSCRIBE ack internally before its
253
+ // promise resolves, so a sync subscribe() that returns the token
254
+ // doesn't strand the operator if they immediately publish().
255
+ _maybeRemoteSubscribe(_scoped(channel), false).catch(function (e) {
256
+ try { logger().warn("pubsub subscribeRemote('" + channel + "') failed: " +
257
+ ((e && e.message) || String(e))); }
258
+ catch (_e) { /* */ }
259
+ });
260
+ return rec;
261
+ }
262
+
263
+ function subscribePattern(pattern, handler) {
264
+ if (closed) throw _err("CLOSED", "pubsub.subscribePattern: instance closed");
265
+ if (typeof pattern !== "string" || pattern.length === 0) {
266
+ throw _err("BAD_OPT", "pubsub.subscribePattern: pattern must be a non-empty string");
267
+ }
268
+ if (typeof handler !== "function") {
269
+ throw _err("BAD_OPT", "pubsub.subscribePattern: handler must be a function");
270
+ }
271
+ var rec = { channel: pattern, handler: handler, isPattern: true };
272
+ patternSubs.add(rec);
273
+ _maybeRemoteSubscribe(_scoped(pattern), true).catch(function (e) {
274
+ try { logger().warn("pubsub subscribePatternRemote('" + pattern + "') failed: " +
275
+ ((e && e.message) || String(e))); }
276
+ catch (_e) { /* */ }
277
+ });
278
+ return rec;
279
+ }
280
+
281
+ function unsubscribe(token) {
282
+ if (!token || typeof token !== "object") return;
283
+ if (token.isPattern) {
284
+ if (patternSubs.delete(token)) {
285
+ _maybeRemoteUnsubscribe(_scoped(token.channel), true);
286
+ }
287
+ return;
288
+ }
289
+ var set = exactSubs.get(token.channel);
290
+ if (set && set.delete(token)) {
291
+ if (set.size === 0) exactSubs.delete(token.channel);
292
+ _maybeRemoteUnsubscribe(_scoped(token.channel), false);
293
+ }
294
+ }
295
+
296
+ async function publish(channel, payload) {
297
+ if (closed) throw _err("CLOSED", "pubsub.publish: instance closed");
298
+ if (typeof channel !== "string" || channel.length === 0) {
299
+ throw _err("BAD_OPT", "pubsub.publish: channel must be a non-empty string");
300
+ }
301
+ var local = _localDispatch(channel, payload, "local");
302
+ var remote = 0;
303
+ if (typeof backend.publishRemote === "function") {
304
+ try {
305
+ var rv = await backend.publishRemote(_scoped(channel), payload);
306
+ remote = (rv && typeof rv === "object" && Number.isFinite(rv.remote))
307
+ ? rv.remote : 1;
308
+ } catch (e) {
309
+ if (auditOn) {
310
+ try { audit().safeEmit("system.pubsub.publish-failed", {
311
+ channel: channel, error: (e && e.message) || String(e),
312
+ }); } catch (_e) { /* */ }
313
+ }
314
+ throw e;
315
+ }
316
+ }
317
+ if (auditOn) {
318
+ try { audit().safeEmit("system.pubsub.publish", {
319
+ channel: channel, localDispatched: local, remoteWritten: remote,
320
+ }); } catch (_e) { /* */ }
321
+ }
322
+ return { local: local, remote: remote };
323
+ }
324
+
325
+ async function close() {
326
+ if (closed) return;
327
+ closed = true;
328
+ if (typeof backend.stop === "function") {
329
+ try { await backend.stop(); } catch (_e) { /* close failure is informational */ }
330
+ }
331
+ exactSubs.clear();
332
+ patternSubs.clear();
333
+ remoteSubCount.clear();
334
+ }
335
+
336
+ return {
337
+ backend: function () { return backend.name; },
338
+ subscribe: subscribe,
339
+ subscribePattern: subscribePattern,
340
+ unsubscribe: unsubscribe,
341
+ publish: publish,
342
+ close: close,
343
+ // Diagnostic — exposed for tests + observability.
344
+ _state: function () {
345
+ return {
346
+ backend: backend.name,
347
+ exactChannels: exactSubs.size,
348
+ patternCount: patternSubs.size,
349
+ remoteSubCount: remoteSubCount.size,
350
+ closed: closed,
351
+ };
352
+ },
353
+ _matchPatternForTest: _matchPattern,
354
+ };
355
+ }
356
+
357
+ module.exports = { create: create, PubsubError: PubsubError };