cross-tab-worker-databus 0.20.70 → 0.20.85
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.
- package/CHANGELOG.md +209 -2
- package/README.md +8 -0
- package/README.zh.md +4 -0
- package/dist/centrifuge-protocol.d.ts +59 -26
- package/dist/centrifuge-protocol.d.ts.map +1 -1
- package/dist/centrifuge-session.d.ts +9 -0
- package/dist/centrifuge-session.d.ts.map +1 -1
- package/dist/centrifuge.d.ts +12 -2
- package/dist/centrifuge.d.ts.map +1 -1
- package/dist/centrifuge.js +168 -88
- package/dist/centrifuge.js.map +3 -3
- package/dist/centrifuge.shared.worker.js +146 -38
- package/dist/centrifuge.shared.worker.js.map +3 -3
- package/dist/centrifuge.worker.js +140 -33
- package/dist/centrifuge.worker.js.map +3 -3
- package/dist/{chunk-IKBJIREH.js → chunk-PW63EWIK.js} +1090 -476
- package/dist/chunk-PW63EWIK.js.map +7 -0
- package/dist/chunk-TZ7ZP7YD.js +175 -0
- package/dist/chunk-TZ7ZP7YD.js.map +7 -0
- package/dist/cjs/centrifuge.cjs +1405 -621
- package/dist/cjs/centrifuge.cjs.map +4 -4
- package/dist/cjs/hooks.cjs +37 -2
- package/dist/cjs/hooks.cjs.map +3 -3
- package/dist/cjs/index.cjs +1469 -688
- package/dist/cjs/index.cjs.map +4 -4
- package/dist/cjs/vue.cjs +45 -2
- package/dist/cjs/vue.cjs.map +3 -3
- package/dist/core/cluster.d.ts +40 -4
- package/dist/core/cluster.d.ts.map +1 -1
- package/dist/core/data-bus.d.ts +93 -66
- package/dist/core/data-bus.d.ts.map +1 -1
- package/dist/core/dedup-manager.d.ts +98 -0
- package/dist/core/dedup-manager.d.ts.map +1 -0
- package/dist/core/environment.d.ts +43 -3
- package/dist/core/environment.d.ts.map +1 -1
- package/dist/core/replay-manager.d.ts +128 -0
- package/dist/core/replay-manager.d.ts.map +1 -0
- package/dist/core/replay-persistence.d.ts +4 -1
- package/dist/core/replay-persistence.d.ts.map +1 -1
- package/dist/core/routing.d.ts +22 -3
- package/dist/core/routing.d.ts.map +1 -1
- package/dist/core/storage-batch.d.ts +0 -4
- package/dist/core/storage-batch.d.ts.map +1 -1
- package/dist/core/trace.d.ts +55 -15
- package/dist/core/trace.d.ts.map +1 -1
- package/dist/core/types.d.ts +58 -9
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/version.d.ts +2 -0
- package/dist/core/version.d.ts.map +1 -0
- package/dist/hooks.d.ts +12 -1
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +28 -2
- package/dist/hooks.js.map +2 -2
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +231 -125
- package/dist/index.js.map +3 -3
- package/dist/utils/constants.d.ts +170 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/error-utils.d.ts +28 -0
- package/dist/utils/error-utils.d.ts.map +1 -0
- package/dist/utils/metadata.d.ts +16 -0
- package/dist/utils/metadata.d.ts.map +1 -0
- package/dist/utils/storage-utils.d.ts +24 -0
- package/dist/utils/storage-utils.d.ts.map +1 -0
- package/dist/utils/validation.d.ts +76 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/vue.d.ts +13 -1
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js +36 -2
- package/dist/vue.js.map +2 -2
- package/dist/websocket.d.ts +6 -1
- package/dist/websocket.d.ts.map +1 -1
- package/dist/worker-mode.d.ts +6 -2
- package/dist/worker-mode.d.ts.map +1 -1
- package/docs/README.md +1 -0
- package/docs/api.md +122 -2
- package/docs/architecture.md +80 -0
- package/docs/benchmarks.md +24 -0
- package/docs/capabilities.md +24 -3
- package/docs/configuration.md +58 -0
- package/docs/getting-started.md +35 -0
- package/docs/release-checklist.md +28 -5
- package/docs/roadmap.md +78 -5
- package/docs/zh/README.md +2 -1
- package/docs/zh/api.md +121 -2
- package/docs/zh/architecture.md +49 -0
- package/docs/zh/benchmarks.md +24 -0
- package/docs/zh/capabilities.md +21 -3
- package/docs/zh/configuration.md +53 -0
- package/docs/zh/getting-started.md +35 -0
- package/docs/zh/release-checklist.md +29 -6
- package/docs/zh/roadmap.md +90 -5
- package/package.json +39 -16
- package/dist/chunk-IKBJIREH.js.map +0 -7
package/dist/cjs/centrifuge.cjs
CHANGED
|
@@ -25,6 +25,145 @@ __export(centrifuge_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(centrifuge_exports);
|
|
27
27
|
|
|
28
|
+
// src/utils/constants.ts
|
|
29
|
+
var WORKER_STATUS = {
|
|
30
|
+
CONNECTING: "connecting",
|
|
31
|
+
CONNECTED: "connected",
|
|
32
|
+
DISCONNECTED: "disconnected",
|
|
33
|
+
ERROR: "error"
|
|
34
|
+
};
|
|
35
|
+
var WORKER_ROLE = {
|
|
36
|
+
ACTIVE: "active",
|
|
37
|
+
STANDBY: "standby"
|
|
38
|
+
};
|
|
39
|
+
var TAB_VISIBILITY = {
|
|
40
|
+
VISIBLE: "visible",
|
|
41
|
+
HIDDEN: "hidden"
|
|
42
|
+
};
|
|
43
|
+
var WORKER_MODE = {
|
|
44
|
+
DEDICATED: "dedicated",
|
|
45
|
+
SHARED: "shared",
|
|
46
|
+
AUTO: "auto"
|
|
47
|
+
};
|
|
48
|
+
var WORKER_BACKEND = {
|
|
49
|
+
DEDICATED: "dedicated",
|
|
50
|
+
SHARED: "shared",
|
|
51
|
+
LOCAL: "local"
|
|
52
|
+
};
|
|
53
|
+
var CHANNEL_FALLBACK = {
|
|
54
|
+
NONE: "none",
|
|
55
|
+
STORAGE_EVENT: "storage-event"
|
|
56
|
+
};
|
|
57
|
+
var CONTROL_ACTION = {
|
|
58
|
+
SUBSCRIBE: "SUBSCRIBE",
|
|
59
|
+
UNSUBSCRIBE: "UNSUBSCRIBE",
|
|
60
|
+
PUBLISH: "PUBLISH"
|
|
61
|
+
};
|
|
62
|
+
var CLUSTER_MESSAGE_TYPE = {
|
|
63
|
+
CONTROL: "CONTROL",
|
|
64
|
+
EVENT: "EVENT",
|
|
65
|
+
REGISTRY: "REGISTRY",
|
|
66
|
+
ROUTE_RELEASED: "ROUTE_RELEASED"
|
|
67
|
+
};
|
|
68
|
+
var DEFAULT_STORAGE_PREFIX = "cross-tab-worker-databus";
|
|
69
|
+
var STORAGE_CHANNEL_PREFIX = `${DEFAULT_STORAGE_PREFIX}:channel:`;
|
|
70
|
+
var TAB_ID_STORAGE_KEY = `${DEFAULT_STORAGE_PREFIX}:tab-id`;
|
|
71
|
+
var PUBLICATION_EVENT = "DATABUS_PUBLICATION";
|
|
72
|
+
var TRACE_EVENT_TYPE = {
|
|
73
|
+
LIFECYCLE: "lifecycle",
|
|
74
|
+
STATUS: "status",
|
|
75
|
+
SUBSCRIPTION: "subscription",
|
|
76
|
+
COORDINATION: "coordination",
|
|
77
|
+
ERROR: "error",
|
|
78
|
+
RELIABILITY: "reliability",
|
|
79
|
+
MESSAGE_METRICS: "message_metrics"
|
|
80
|
+
};
|
|
81
|
+
var TRACE_LIFECYCLE_ACTION = {
|
|
82
|
+
START: "start",
|
|
83
|
+
STOP: "stop",
|
|
84
|
+
SUSPEND: "suspend",
|
|
85
|
+
RESUME: "resume"
|
|
86
|
+
};
|
|
87
|
+
var INVOKE_LABEL = {
|
|
88
|
+
DISPATCH: "dispatch",
|
|
89
|
+
STATUS: "status",
|
|
90
|
+
ERROR_HANDLER: "error handler"
|
|
91
|
+
};
|
|
92
|
+
var TRACE_MODE = {
|
|
93
|
+
EVENTS: "events",
|
|
94
|
+
METRICS: "metrics",
|
|
95
|
+
ALL: "all"
|
|
96
|
+
};
|
|
97
|
+
var TRACE_ERROR_SOURCE = {
|
|
98
|
+
TRANSPORT: "transport",
|
|
99
|
+
OPERATION: "operation"
|
|
100
|
+
};
|
|
101
|
+
var RELIABILITY_OPERATION = {
|
|
102
|
+
TRANSPORT_RECOVERY: "transport_recovery",
|
|
103
|
+
ROUTE_ACK: "route_ack",
|
|
104
|
+
ROUTE_MIGRATION: "route_migration",
|
|
105
|
+
// A re-election that recovered a stranded unconfirmed handoff (previous
|
|
106
|
+
// owner gone, ACK never arrived). Distinct from ROUTE_MIGRATION so trace
|
|
107
|
+
// consumers can tell recoveries apart from routine graceful handoffs.
|
|
108
|
+
ROUTE_MIGRATION_RECOVERY: "route_migration_recovery",
|
|
109
|
+
PERSISTENCE_CLEANUP: "persistence_cleanup",
|
|
110
|
+
PERSISTENCE_RETRY: "persistence_retry",
|
|
111
|
+
DEDUP_SUPPRESSED: "dedup_suppressed"
|
|
112
|
+
};
|
|
113
|
+
var RECOVERY_OUTCOME = {
|
|
114
|
+
SCHEDULED: "scheduled",
|
|
115
|
+
SUCCEEDED: "succeeded",
|
|
116
|
+
FAILED: "failed",
|
|
117
|
+
EXHAUSTED: "exhausted"
|
|
118
|
+
};
|
|
119
|
+
var SUBSCRIPTION_ACTION = {
|
|
120
|
+
SUBSCRIBE: "subscribe",
|
|
121
|
+
UNSUBSCRIBE: "unsubscribe"
|
|
122
|
+
};
|
|
123
|
+
var PRUNE_STRATEGY = {
|
|
124
|
+
COUNT: "count",
|
|
125
|
+
AGE: "age",
|
|
126
|
+
BOTH: "both"
|
|
127
|
+
};
|
|
128
|
+
var PERSISTENCE_OPERATION = {
|
|
129
|
+
LOAD: "load",
|
|
130
|
+
APPEND: "append",
|
|
131
|
+
CLEAR: "clear",
|
|
132
|
+
CLEAR_TOPIC: "clearTopic",
|
|
133
|
+
CLEAR_BEFORE: "clearBefore"
|
|
134
|
+
};
|
|
135
|
+
var FAILURE_SOURCE = {
|
|
136
|
+
TRANSPORT: "transport",
|
|
137
|
+
PERSISTENCE: "persistence",
|
|
138
|
+
DISPATCH: "dispatch"
|
|
139
|
+
};
|
|
140
|
+
var HEALTH_STATE = {
|
|
141
|
+
STOPPED: "stopped",
|
|
142
|
+
STARTING: "starting",
|
|
143
|
+
HEALTHY: "healthy",
|
|
144
|
+
RECOVERING: "recovering",
|
|
145
|
+
SUSPENDED: "suspended",
|
|
146
|
+
DEGRADED: "degraded"
|
|
147
|
+
};
|
|
148
|
+
var CENTRIFUGE_INPUT_TYPE = {
|
|
149
|
+
INIT: "INIT",
|
|
150
|
+
SUBSCRIBE: "SUBSCRIBE",
|
|
151
|
+
UNSUBSCRIBE: "UNSUBSCRIBE",
|
|
152
|
+
PUBLISH: "PUBLISH",
|
|
153
|
+
PUBLISH_BIN: "PUBLISH_BIN",
|
|
154
|
+
PING: "PING",
|
|
155
|
+
STOP: "STOP",
|
|
156
|
+
TOKEN_RESPONSE: "TOKEN_RESPONSE",
|
|
157
|
+
TOKEN_ERROR: "TOKEN_ERROR"
|
|
158
|
+
};
|
|
159
|
+
var CENTRIFUGE_OUTPUT_TYPE = {
|
|
160
|
+
STATUS: "STATUS",
|
|
161
|
+
MESSAGE: "MESSAGE",
|
|
162
|
+
MESSAGE_BIN: "MESSAGE_BIN",
|
|
163
|
+
ERROR: "ERROR",
|
|
164
|
+
TOKEN_REQUEST: "TOKEN_REQUEST"
|
|
165
|
+
};
|
|
166
|
+
|
|
28
167
|
// src/core/environment.ts
|
|
29
168
|
function getStorage(name) {
|
|
30
169
|
try {
|
|
@@ -40,8 +179,53 @@ function randomId() {
|
|
|
40
179
|
return Math.random().toString(36).slice(2);
|
|
41
180
|
}
|
|
42
181
|
}
|
|
182
|
+
function createStorageEventChannel(options) {
|
|
183
|
+
const { name, storage, win } = options;
|
|
184
|
+
if (!storage || !win) return null;
|
|
185
|
+
const key = `${STORAGE_CHANNEL_PREFIX}${name}`;
|
|
186
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
187
|
+
let sequence = 0;
|
|
188
|
+
const onStorage = (event) => {
|
|
189
|
+
if (event.key !== key || event.newValue === null) return;
|
|
190
|
+
let message;
|
|
191
|
+
try {
|
|
192
|
+
const parsed = JSON.parse(event.newValue);
|
|
193
|
+
if (!parsed || typeof parsed !== "object" || typeof parsed.seq !== "number" || !parsed.message) return;
|
|
194
|
+
message = parsed.message;
|
|
195
|
+
} catch {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const event_ = { data: message };
|
|
199
|
+
for (const listener of [...listeners]) listener(event_);
|
|
200
|
+
};
|
|
201
|
+
win.addEventListener("storage", onStorage);
|
|
202
|
+
let closed = false;
|
|
203
|
+
return {
|
|
204
|
+
addEventListener(_type, listener) {
|
|
205
|
+
listeners.add(listener);
|
|
206
|
+
},
|
|
207
|
+
removeEventListener(_type, listener) {
|
|
208
|
+
listeners.delete(listener);
|
|
209
|
+
},
|
|
210
|
+
postMessage(message) {
|
|
211
|
+
if (closed) return;
|
|
212
|
+
sequence += 1;
|
|
213
|
+
storage.setItem(key, JSON.stringify({ seq: sequence, message }));
|
|
214
|
+
},
|
|
215
|
+
close() {
|
|
216
|
+
closed = true;
|
|
217
|
+
win.removeEventListener("storage", onStorage);
|
|
218
|
+
listeners.clear();
|
|
219
|
+
try {
|
|
220
|
+
storage.removeItem(key);
|
|
221
|
+
} catch {
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
}
|
|
43
226
|
var tabIdentityInitialized = false;
|
|
44
|
-
function createBrowserEnvironment() {
|
|
227
|
+
function createBrowserEnvironment(options) {
|
|
228
|
+
const channelFallback = options?.channelFallback ?? CHANNEL_FALLBACK.NONE;
|
|
45
229
|
return {
|
|
46
230
|
storage: getStorage("localStorage"),
|
|
47
231
|
sessionStorage: getStorage("sessionStorage"),
|
|
@@ -49,14 +233,18 @@ function createBrowserEnvironment() {
|
|
|
49
233
|
randomId,
|
|
50
234
|
createChannel: (name) => {
|
|
51
235
|
try {
|
|
52
|
-
|
|
236
|
+
if (typeof BroadcastChannel !== "undefined") return new BroadcastChannel(name);
|
|
53
237
|
} catch {
|
|
54
|
-
return null;
|
|
55
238
|
}
|
|
239
|
+
return channelFallback === CHANNEL_FALLBACK.STORAGE_EVENT ? createStorageEventChannel({
|
|
240
|
+
name,
|
|
241
|
+
storage: getStorage("localStorage"),
|
|
242
|
+
win: typeof window !== "undefined" && typeof window.addEventListener === "function" ? window : null
|
|
243
|
+
}) : null;
|
|
56
244
|
},
|
|
57
245
|
setInterval: (callback, intervalMs) => globalThis.setInterval(callback, intervalMs),
|
|
58
246
|
clearInterval: (handle) => globalThis.clearInterval(handle),
|
|
59
|
-
getVisibilityState: () => typeof document !== "undefined" && document.visibilityState ===
|
|
247
|
+
getVisibilityState: () => typeof document !== "undefined" && document.visibilityState === TAB_VISIBILITY.HIDDEN ? TAB_VISIBILITY.HIDDEN : TAB_VISIBILITY.VISIBLE,
|
|
60
248
|
addVisibilityChangeListener: (listener) => {
|
|
61
249
|
if (typeof document !== "undefined") document.addEventListener("visibilitychange", listener);
|
|
62
250
|
},
|
|
@@ -87,7 +275,7 @@ function canUseStorage(storage, probeKey) {
|
|
|
87
275
|
return false;
|
|
88
276
|
}
|
|
89
277
|
}
|
|
90
|
-
function getOrCreateTabId(environment, key =
|
|
278
|
+
function getOrCreateTabId(environment, key = TAB_ID_STORAGE_KEY) {
|
|
91
279
|
const storage = environment.sessionStorage;
|
|
92
280
|
try {
|
|
93
281
|
const existing = storage?.getItem(key);
|
|
@@ -140,21 +328,73 @@ function avalancheMix(self, neighbor) {
|
|
|
140
328
|
|
|
141
329
|
// src/core/routing.ts
|
|
142
330
|
var DEFAULT_MAX_ACTIVE_WORKERS = 3;
|
|
143
|
-
function
|
|
331
|
+
function effectiveWorkerLoad(worker, options) {
|
|
332
|
+
const baseLoad = Number.isFinite(worker.load) ? worker.load : 0;
|
|
333
|
+
const sample = worker.throughput;
|
|
334
|
+
const messageRateWeight = options?.messageRateWeight ?? 0;
|
|
335
|
+
const byteRateWeight = options?.byteRateWeight ?? 0;
|
|
336
|
+
const scheduleLagWeight = options?.scheduleLagWeight ?? 0;
|
|
337
|
+
if (!sample || !Number.isFinite(sample.windowMs) || sample.windowMs <= 0 || messageRateWeight === 0 && byteRateWeight === 0 && scheduleLagWeight === 0) {
|
|
338
|
+
return baseLoad;
|
|
339
|
+
}
|
|
340
|
+
const windowSeconds = sample.windowMs / 1e3;
|
|
341
|
+
const messageRate = sample.messageCount / windowSeconds;
|
|
342
|
+
const byteRate = sample.byteCount / windowSeconds;
|
|
343
|
+
const scheduleLagRatio = sample.overrunMs / sample.windowMs;
|
|
344
|
+
const weighted = baseLoad + messageRateWeight * messageRate + byteRateWeight * byteRate + scheduleLagWeight * scheduleLagRatio;
|
|
345
|
+
return Number.isFinite(weighted) ? weighted : baseLoad;
|
|
346
|
+
}
|
|
347
|
+
function approximatePayloadBytes(payload) {
|
|
348
|
+
return estimatePayloadBytes(payload, 0);
|
|
349
|
+
}
|
|
350
|
+
var MAX_PAYLOAD_DEPTH = 6;
|
|
351
|
+
function estimatePayloadBytes(payload, depth) {
|
|
352
|
+
if (payload === null || payload === void 0) return 0;
|
|
353
|
+
switch (typeof payload) {
|
|
354
|
+
case "boolean":
|
|
355
|
+
return 4;
|
|
356
|
+
case "number":
|
|
357
|
+
case "bigint":
|
|
358
|
+
return 8;
|
|
359
|
+
case "string":
|
|
360
|
+
return payload.length;
|
|
361
|
+
case "symbol":
|
|
362
|
+
case "function":
|
|
363
|
+
return 0;
|
|
364
|
+
case "object":
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
if (depth >= MAX_PAYLOAD_DEPTH) return 0;
|
|
368
|
+
if (payload instanceof ArrayBuffer) return payload.byteLength;
|
|
369
|
+
if (ArrayBuffer.isView(payload)) return payload.byteLength;
|
|
370
|
+
if (Array.isArray(payload)) {
|
|
371
|
+
let sum2 = 8;
|
|
372
|
+
for (const item of payload) sum2 += estimatePayloadBytes(item, depth + 1);
|
|
373
|
+
return sum2;
|
|
374
|
+
}
|
|
375
|
+
let sum = 0;
|
|
376
|
+
for (const value of Object.values(payload)) {
|
|
377
|
+
sum += estimatePayloadBytes(value, depth + 1);
|
|
378
|
+
}
|
|
379
|
+
return sum;
|
|
380
|
+
}
|
|
381
|
+
function selectLeastLoadedWorker(workers, preferredWorkerId, options) {
|
|
144
382
|
const preferred = workers.find((worker) => worker.workerId === preferredWorkerId);
|
|
145
383
|
if (preferred) return preferred;
|
|
146
384
|
return workers.reduce((least, worker) => {
|
|
147
385
|
if (!least) return worker;
|
|
148
|
-
const byLoad = worker
|
|
386
|
+
const byLoad = effectiveWorkerLoad(worker, options) - effectiveWorkerLoad(least, options);
|
|
149
387
|
if (byLoad !== 0) return byLoad < 0 ? worker : least;
|
|
150
388
|
if (worker.workerId < least.workerId) return worker;
|
|
151
389
|
return least;
|
|
152
390
|
}, void 0);
|
|
153
391
|
}
|
|
154
392
|
function selectActiveWorkers(workers, maxActiveWorkers = DEFAULT_MAX_ACTIVE_WORKERS) {
|
|
155
|
-
const healthyWorkers = workers.filter(
|
|
393
|
+
const healthyWorkers = workers.filter(
|
|
394
|
+
(worker) => worker.status === WORKER_STATUS.CONNECTING || worker.status === WORKER_STATUS.CONNECTED
|
|
395
|
+
);
|
|
156
396
|
const availableWorkers = healthyWorkers.length > 0 ? healthyWorkers : [...workers];
|
|
157
|
-
const visibleWorkers = availableWorkers.filter((worker) => worker.visibilityState ===
|
|
397
|
+
const visibleWorkers = availableWorkers.filter((worker) => worker.visibilityState === TAB_VISIBILITY.VISIBLE);
|
|
158
398
|
const candidates = visibleWorkers.length > 0 ? visibleWorkers : availableWorkers;
|
|
159
399
|
return candidates.sort(
|
|
160
400
|
(left, right) => left.registeredAt - right.registeredAt || (left.workerId < right.workerId ? -1 : left.workerId > right.workerId ? 1 : 0)
|
|
@@ -179,6 +419,7 @@ var BatchingStorageWriter = class {
|
|
|
179
419
|
constructor(storage) {
|
|
180
420
|
this.storage = storage;
|
|
181
421
|
}
|
|
422
|
+
storage;
|
|
182
423
|
/** Coalesced write set. A `null` value represents a pending delete. */
|
|
183
424
|
pending = /* @__PURE__ */ new Map();
|
|
184
425
|
/** Per-key retry counter, reset on a successful write. */
|
|
@@ -235,7 +476,7 @@ var BatchingStorageWriter = class {
|
|
|
235
476
|
this.pending.delete(key);
|
|
236
477
|
this.retryCount.delete(key);
|
|
237
478
|
if (typeof console !== "undefined" && typeof console.warn === "function") {
|
|
238
|
-
console.warn(
|
|
479
|
+
console.warn(`[${DEFAULT_STORAGE_PREFIX}] storage write gave up after retries, dropping key:`, key);
|
|
239
480
|
}
|
|
240
481
|
continue;
|
|
241
482
|
}
|
|
@@ -295,12 +536,16 @@ var BatchingStorageWriter = class {
|
|
|
295
536
|
}
|
|
296
537
|
};
|
|
297
538
|
|
|
298
|
-
// src/
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
539
|
+
// src/utils/metadata.ts
|
|
540
|
+
function publicationMetadata(messageId, timestamp) {
|
|
541
|
+
if (messageId === void 0 && timestamp === void 0) return void 0;
|
|
542
|
+
return {
|
|
543
|
+
...messageId === void 0 ? {} : { messageId },
|
|
544
|
+
...timestamp === void 0 ? {} : { timestamp }
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// src/utils/storage-utils.ts
|
|
304
549
|
function readJson(storage, key) {
|
|
305
550
|
try {
|
|
306
551
|
const value = storage.getItem(key);
|
|
@@ -315,13 +560,6 @@ function writeJson(storage, key, value) {
|
|
|
315
560
|
} catch {
|
|
316
561
|
}
|
|
317
562
|
}
|
|
318
|
-
function publicationMetadata(messageId, timestamp) {
|
|
319
|
-
if (messageId === void 0 && timestamp === void 0) return void 0;
|
|
320
|
-
return {
|
|
321
|
-
...messageId === void 0 ? {} : { messageId },
|
|
322
|
-
...timestamp === void 0 ? {} : { timestamp }
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
563
|
function listKeys(storage, prefix) {
|
|
326
564
|
try {
|
|
327
565
|
return Array.from({ length: storage.length }, (_, index) => storage.key(index)).filter(
|
|
@@ -334,6 +572,120 @@ function listKeys(storage, prefix) {
|
|
|
334
572
|
function readAllByPrefix(storage, prefix) {
|
|
335
573
|
return listKeys(storage, prefix).map((key) => ({ key, value: readJson(storage, key) })).filter((entry) => entry.value !== null);
|
|
336
574
|
}
|
|
575
|
+
|
|
576
|
+
// src/utils/validation.ts
|
|
577
|
+
function assertPositiveSafeInteger(value, name) {
|
|
578
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) {
|
|
579
|
+
throw new TypeError(`${name} must be a positive safe integer, got ${String(value)}.`);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
function assertPositiveFiniteNumber(value, name) {
|
|
583
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
584
|
+
throw new TypeError(`${name} must be a positive finite number.`);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
function assertNonNegativeFiniteNumber(value, name) {
|
|
588
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
589
|
+
throw new TypeError(`${name} must be a non-negative finite number.`);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
function assertPruneStrategy(value) {
|
|
593
|
+
const allowed = [PRUNE_STRATEGY.COUNT, PRUNE_STRATEGY.AGE, PRUNE_STRATEGY.BOTH];
|
|
594
|
+
if (!allowed.includes(String(value))) {
|
|
595
|
+
throw new TypeError("pruneStrategy must be count, age, or both.");
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function assertReplayOptions(replay) {
|
|
599
|
+
if (!replay) return;
|
|
600
|
+
if (replay.maxPerTopic !== void 0) assertPositiveSafeInteger(replay.maxPerTopic, "replay.maxPerTopic");
|
|
601
|
+
if (replay.pruneStrategy !== void 0) assertPruneStrategy(replay.pruneStrategy);
|
|
602
|
+
if (replay.retentionMs !== void 0) assertPositiveFiniteNumber(replay.retentionMs, "replay.retentionMs");
|
|
603
|
+
if (replay.retentionSweepMs !== void 0) {
|
|
604
|
+
assertPositiveFiniteNumber(replay.retentionSweepMs, "replay.retentionSweepMs");
|
|
605
|
+
}
|
|
606
|
+
if (replay.persistenceRetry) assertPersistenceRetryOptions(replay.persistenceRetry);
|
|
607
|
+
}
|
|
608
|
+
function assertPersistenceRetryOptions(retry) {
|
|
609
|
+
if (retry.maxAttempts !== void 0) {
|
|
610
|
+
assertPositiveSafeInteger(retry.maxAttempts, "replay.persistenceRetry.maxAttempts");
|
|
611
|
+
}
|
|
612
|
+
if (retry.backoffMs !== void 0) {
|
|
613
|
+
assertNonNegativeFiniteNumber(retry.backoffMs, "replay.persistenceRetry.backoffMs");
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
function assertDedupOptions(dedup) {
|
|
617
|
+
if (!dedup) return;
|
|
618
|
+
if (dedup.maxEntries !== void 0) assertPositiveSafeInteger(dedup.maxEntries, "dedup.maxEntries");
|
|
619
|
+
if (dedup.ttlMs !== void 0) assertPositiveFiniteNumber(dedup.ttlMs, "dedup.ttlMs");
|
|
620
|
+
if (dedup.sweepMs !== void 0) assertPositiveFiniteNumber(dedup.sweepMs, "dedup.sweepMs");
|
|
621
|
+
const bounds = dedup.adaptiveTtl;
|
|
622
|
+
if (bounds !== void 0) {
|
|
623
|
+
const finite = (value) => typeof value === "number" && Number.isFinite(value);
|
|
624
|
+
if (!finite(bounds.minMs) || !finite(bounds.maxMs) || bounds.minMs <= 0 || bounds.maxMs < bounds.minMs) {
|
|
625
|
+
throw new TypeError("dedup.adaptiveTtl bounds are invalid.");
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
function assertRecoveryOptions(recovery) {
|
|
630
|
+
if (!recovery) return;
|
|
631
|
+
if (recovery.cooldownMs !== void 0) {
|
|
632
|
+
assertPositiveFiniteNumber(recovery.cooldownMs, "recovery.cooldownMs");
|
|
633
|
+
}
|
|
634
|
+
const maxAttempts = recovery.maxAttempts;
|
|
635
|
+
if (maxAttempts !== void 0 && !(maxAttempts === Number.POSITIVE_INFINITY || typeof maxAttempts === "number" && Number.isSafeInteger(maxAttempts) && maxAttempts > 0)) {
|
|
636
|
+
throw new TypeError("recovery.maxAttempts must be a positive safe integer.");
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
function assertLoadWeightingOptions(loadWeighting) {
|
|
640
|
+
if (!loadWeighting) return;
|
|
641
|
+
const weights = {
|
|
642
|
+
messageRateWeight: loadWeighting.messageRateWeight,
|
|
643
|
+
byteRateWeight: loadWeighting.byteRateWeight,
|
|
644
|
+
scheduleLagWeight: loadWeighting.scheduleLagWeight
|
|
645
|
+
};
|
|
646
|
+
for (const [name, value] of Object.entries(weights)) {
|
|
647
|
+
if (value !== void 0) assertNonNegativeFiniteNumber(value, `loadWeighting.${name}`);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function assertClusterOptions(options) {
|
|
651
|
+
if (options.maxActiveWorkers !== void 0) {
|
|
652
|
+
assertPositiveSafeInteger(options.maxActiveWorkers, "maxActiveWorkers");
|
|
653
|
+
}
|
|
654
|
+
if (options.heartbeatIntervalMs !== void 0) {
|
|
655
|
+
assertPositiveFiniteNumber(options.heartbeatIntervalMs, "heartbeatIntervalMs");
|
|
656
|
+
}
|
|
657
|
+
if (options.workerTtlMs !== void 0) {
|
|
658
|
+
assertPositiveFiniteNumber(options.workerTtlMs, "workerTtlMs");
|
|
659
|
+
}
|
|
660
|
+
if (options.routeOwnerCacheMax !== void 0) {
|
|
661
|
+
assertPositiveSafeInteger(options.routeOwnerCacheMax, "routeOwnerCacheMax");
|
|
662
|
+
}
|
|
663
|
+
assertLoadWeightingOptions(options.loadWeighting);
|
|
664
|
+
}
|
|
665
|
+
function assertHeartbeatInterval(value) {
|
|
666
|
+
if (value === Infinity) return;
|
|
667
|
+
if (typeof value === "number" && Number.isFinite(value) && value > 0) return;
|
|
668
|
+
throw new TypeError(
|
|
669
|
+
`Centrifuge heartbeatIntervalMs must be a positive number or Infinity, got ${String(value)}.`
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
function assertStructuredCloneable(value) {
|
|
673
|
+
if (typeof structuredClone !== "function") return;
|
|
674
|
+
try {
|
|
675
|
+
structuredClone(value);
|
|
676
|
+
} catch (error) {
|
|
677
|
+
throw new TypeError(
|
|
678
|
+
"Centrifuge Worker configuration and published data must be structured-cloneable.",
|
|
679
|
+
{ cause: error }
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// src/core/cluster.ts
|
|
685
|
+
var CLUSTER_PROTOCOL_VERSION = 1;
|
|
686
|
+
var DEFAULT_HEARTBEAT_INTERVAL_MS = 3e3;
|
|
687
|
+
var DEFAULT_WORKER_TTL_MS = 1e4;
|
|
688
|
+
var MAX_KNOWN_TOPICS = 500;
|
|
337
689
|
var WorkerClusterRuntime = class {
|
|
338
690
|
tabId;
|
|
339
691
|
workerId;
|
|
@@ -347,6 +699,14 @@ var WorkerClusterRuntime = class {
|
|
|
347
699
|
routePrefix;
|
|
348
700
|
subscriberPrefix;
|
|
349
701
|
channelName;
|
|
702
|
+
/** Adaptive load weighting options; undefined keeps legacy topic-count routing. */
|
|
703
|
+
loadWeighting;
|
|
704
|
+
/** Rolling traffic accumulator folded into the worker record on writeRecord. */
|
|
705
|
+
throughputWindow = {
|
|
706
|
+
startedAt: 0,
|
|
707
|
+
messageCount: 0,
|
|
708
|
+
byteCount: 0
|
|
709
|
+
};
|
|
350
710
|
// Topics this tab has subscribed to (local interest, plaintext).
|
|
351
711
|
subscribedTopics = /* @__PURE__ */ new Set();
|
|
352
712
|
// Topics assigned to this Worker as owner (topicKey → topic). Authoritative:
|
|
@@ -382,12 +742,14 @@ var WorkerClusterRuntime = class {
|
|
|
382
742
|
lifecycleListening = false;
|
|
383
743
|
currentRecord;
|
|
384
744
|
constructor(options) {
|
|
745
|
+
assertClusterOptions(options);
|
|
385
746
|
this.environment = options.environment ?? createBrowserEnvironment();
|
|
386
747
|
this.handlers = options.handlers;
|
|
387
748
|
this.maxActiveWorkers = options.maxActiveWorkers ?? DEFAULT_MAX_ACTIVE_WORKERS;
|
|
388
749
|
this.heartbeatIntervalMs = options.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
|
|
389
750
|
this.routeOwnerCacheMax = options.routeOwnerCacheMax ?? 256;
|
|
390
751
|
this.workerTtlMs = options.workerTtlMs ?? DEFAULT_WORKER_TTL_MS;
|
|
752
|
+
this.loadWeighting = options.loadWeighting;
|
|
391
753
|
const clusterHash = createOpaqueKey(options.clusterKey || "__default__");
|
|
392
754
|
const prefix = options.storagePrefix ?? DEFAULT_STORAGE_PREFIX;
|
|
393
755
|
const baseKey = `${prefix}:${clusterHash}`;
|
|
@@ -404,8 +766,8 @@ var WorkerClusterRuntime = class {
|
|
|
404
766
|
workerId: this.workerId,
|
|
405
767
|
tabId: this.tabId,
|
|
406
768
|
load: 0,
|
|
407
|
-
role:
|
|
408
|
-
status:
|
|
769
|
+
role: WORKER_ROLE.STANDBY,
|
|
770
|
+
status: WORKER_STATUS.CONNECTING,
|
|
409
771
|
visibilityState: this.environment.getVisibilityState(),
|
|
410
772
|
heartbeatAt: now,
|
|
411
773
|
registeredAt: now
|
|
@@ -458,7 +820,7 @@ var WorkerClusterRuntime = class {
|
|
|
458
820
|
this.writeRecord(true);
|
|
459
821
|
for (const topic of this.subscribedTopics) {
|
|
460
822
|
const topicKey = this.rememberTopic(topic);
|
|
461
|
-
if (!this.storage) this.sendControl(this.workerId,
|
|
823
|
+
if (!this.storage) this.sendControl(this.workerId, CONTROL_ACTION.SUBSCRIBE, topic, topicKey);
|
|
462
824
|
else this.writeSubscriber(topicKey);
|
|
463
825
|
}
|
|
464
826
|
this.reconcile();
|
|
@@ -486,9 +848,14 @@ var WorkerClusterRuntime = class {
|
|
|
486
848
|
this.removeStorage(this.workerStorageKey(this.workerId));
|
|
487
849
|
this.flushStorage();
|
|
488
850
|
this.notifyRegistry();
|
|
489
|
-
this.channel
|
|
851
|
+
const channel = this.channel;
|
|
490
852
|
this.channel = null;
|
|
491
853
|
this.handlers.onSuspend?.();
|
|
854
|
+
if (typeof globalThis.setTimeout === "function") {
|
|
855
|
+
globalThis.setTimeout(() => channel?.close(), 0);
|
|
856
|
+
} else {
|
|
857
|
+
channel?.close();
|
|
858
|
+
}
|
|
492
859
|
}
|
|
493
860
|
/** Update the worker's connection status and persist the change. */
|
|
494
861
|
setStatus(status) {
|
|
@@ -506,7 +873,7 @@ var WorkerClusterRuntime = class {
|
|
|
506
873
|
this.subscribedTopics.add(topic);
|
|
507
874
|
if (!this.started) return false;
|
|
508
875
|
if (!this.storage) {
|
|
509
|
-
this.sendControl(this.workerId,
|
|
876
|
+
this.sendControl(this.workerId, CONTROL_ACTION.SUBSCRIBE, topic, topicKey);
|
|
510
877
|
return true;
|
|
511
878
|
}
|
|
512
879
|
this.writeSubscriber(topicKey);
|
|
@@ -516,9 +883,9 @@ var WorkerClusterRuntime = class {
|
|
|
516
883
|
return existingRoute?.workerId === this.workerId;
|
|
517
884
|
}
|
|
518
885
|
const activeWorkers = selectActiveWorkers(workers, this.maxActiveWorkers);
|
|
519
|
-
const owner = selectLeastLoadedWorker(activeWorkers) ?? this.currentRecord;
|
|
886
|
+
const owner = selectLeastLoadedWorker(activeWorkers, void 0, this.loadWeighting) ?? this.currentRecord;
|
|
520
887
|
this.writeRoute(topicKey, owner, void 0, (existingRoute?.generation ?? 0) + 1);
|
|
521
|
-
this.sendControl(owner.workerId,
|
|
888
|
+
this.sendControl(owner.workerId, CONTROL_ACTION.SUBSCRIBE, topic, topicKey);
|
|
522
889
|
this.notifyRegistry();
|
|
523
890
|
return owner.workerId === this.workerId;
|
|
524
891
|
}
|
|
@@ -542,7 +909,7 @@ var WorkerClusterRuntime = class {
|
|
|
542
909
|
const subscribers = this.readSubscriberTabIds(topicKey, this.readWorkers());
|
|
543
910
|
if (subscribers.length === 0) {
|
|
544
911
|
this.removeStorage(this.routeStorageKey(topicKey));
|
|
545
|
-
if (notifyOwner) this.sendControl(route.workerId,
|
|
912
|
+
if (notifyOwner) this.sendControl(route.workerId, CONTROL_ACTION.UNSUBSCRIBE, topic, topicKey);
|
|
546
913
|
}
|
|
547
914
|
return topicKey;
|
|
548
915
|
}
|
|
@@ -561,15 +928,17 @@ var WorkerClusterRuntime = class {
|
|
|
561
928
|
continue;
|
|
562
929
|
}
|
|
563
930
|
const owner = selectLeastLoadedWorker(
|
|
564
|
-
activeWorkers.map((worker) => ({ ...worker, load: projectedLoads.get(worker.workerId) ?? worker.load }))
|
|
931
|
+
activeWorkers.map((worker) => ({ ...worker, load: projectedLoads.get(worker.workerId) ?? worker.load })),
|
|
932
|
+
void 0,
|
|
933
|
+
this.loadWeighting
|
|
565
934
|
);
|
|
566
935
|
if (!owner) continue;
|
|
567
936
|
projectedLoads.set(owner.workerId, (projectedLoads.get(owner.workerId) ?? owner.load) + 1);
|
|
568
937
|
const generation = (previous?.generation ?? 0) + 1;
|
|
569
938
|
this.writeRoute(topicKey, owner, previous?.workerId, generation);
|
|
570
|
-
this.handlers.onDiagnostic?.({ operation:
|
|
939
|
+
this.handlers.onDiagnostic?.({ operation: RELIABILITY_OPERATION.ROUTE_MIGRATION, topic });
|
|
571
940
|
this.flushStorage();
|
|
572
|
-
this.handlers.onControl(
|
|
941
|
+
this.handlers.onControl(CONTROL_ACTION.UNSUBSCRIBE, topic);
|
|
573
942
|
this.sendRouteReleased(owner.workerId, topic, topicKey, generation);
|
|
574
943
|
}
|
|
575
944
|
}
|
|
@@ -577,22 +946,19 @@ var WorkerClusterRuntime = class {
|
|
|
577
946
|
const metadata = typeof metadataOrMessageId === "string" ? { messageId: metadataOrMessageId } : metadataOrMessageId;
|
|
578
947
|
const topicKey = this.rememberTopic(topic);
|
|
579
948
|
if (this.assignedTopics.has(topicKey)) {
|
|
580
|
-
return this.sendControl(this.workerId,
|
|
949
|
+
return this.sendControl(this.workerId, CONTROL_ACTION.PUBLISH, topic, topicKey, data, metadata);
|
|
581
950
|
}
|
|
582
951
|
const cachedPattern = this.wildcardPublishCache.get(topic);
|
|
583
|
-
if (cachedPattern !== void 0 && cachedPattern !== null && this.assignedTopics.has(cachedPattern)) {
|
|
584
|
-
return this.sendControl(this.workerId, "PUBLISH", topic, topicKey, data, metadata);
|
|
585
|
-
}
|
|
586
952
|
if (cachedPattern === void 0) {
|
|
587
953
|
for (const pattern of this.assignedTopics.values()) {
|
|
588
954
|
if (pattern !== topic && topicMatchesPattern(pattern, topic)) {
|
|
589
955
|
this.wildcardPublishCache.set(topic, pattern);
|
|
590
|
-
return this.sendControl(this.workerId,
|
|
956
|
+
return this.sendControl(this.workerId, CONTROL_ACTION.PUBLISH, topic, topicKey, data, metadata);
|
|
591
957
|
}
|
|
592
958
|
}
|
|
593
959
|
this.wildcardPublishCache.set(topic, null);
|
|
594
960
|
}
|
|
595
|
-
return this.sendControl(this.resolvePublishTarget(topic, topicKey),
|
|
961
|
+
return this.sendControl(this.resolvePublishTarget(topic, topicKey), CONTROL_ACTION.PUBLISH, topic, topicKey, data, metadata);
|
|
596
962
|
}
|
|
597
963
|
/**
|
|
598
964
|
* Burst-friendly variant of `publish()`: packs up to N items into a single
|
|
@@ -613,19 +979,15 @@ var WorkerClusterRuntime = class {
|
|
|
613
979
|
}
|
|
614
980
|
const topicKey = this.rememberTopic(topic);
|
|
615
981
|
if (this.assignedTopics.has(topicKey)) {
|
|
616
|
-
|
|
982
|
+
this.dispatchLocalPublishBatch(topic, topicKey, items);
|
|
617
983
|
return true;
|
|
618
984
|
}
|
|
619
985
|
const cachedPattern = this.wildcardPublishCache.get(topic);
|
|
620
|
-
if (cachedPattern !== void 0 && cachedPattern !== null && this.assignedTopics.has(cachedPattern)) {
|
|
621
|
-
for (const item of items) this.dispatchLocalPublish(topic, topicKey, item.data, item.messageId, item.timestamp);
|
|
622
|
-
return true;
|
|
623
|
-
}
|
|
624
986
|
if (cachedPattern === void 0) {
|
|
625
987
|
for (const pattern of this.assignedTopics.values()) {
|
|
626
988
|
if (pattern !== topic && topicMatchesPattern(pattern, topic)) {
|
|
627
989
|
this.wildcardPublishCache.set(topic, pattern);
|
|
628
|
-
|
|
990
|
+
this.dispatchLocalPublishBatch(topic, topicKey, items);
|
|
629
991
|
return true;
|
|
630
992
|
}
|
|
631
993
|
}
|
|
@@ -633,14 +995,14 @@ var WorkerClusterRuntime = class {
|
|
|
633
995
|
}
|
|
634
996
|
const target = this.resolvePublishTarget(topic, topicKey);
|
|
635
997
|
if (target === this.workerId) {
|
|
636
|
-
|
|
998
|
+
this.dispatchLocalPublishBatch(topic, topicKey, items);
|
|
637
999
|
return true;
|
|
638
1000
|
}
|
|
639
1001
|
return this.send({
|
|
640
|
-
type:
|
|
1002
|
+
type: CLUSTER_MESSAGE_TYPE.CONTROL,
|
|
641
1003
|
sourceWorkerId: this.workerId,
|
|
642
1004
|
targetWorkerId: target,
|
|
643
|
-
action:
|
|
1005
|
+
action: CONTROL_ACTION.PUBLISH,
|
|
644
1006
|
topic,
|
|
645
1007
|
topicKey,
|
|
646
1008
|
items: items.map((item) => ({
|
|
@@ -671,8 +1033,17 @@ var WorkerClusterRuntime = class {
|
|
|
671
1033
|
dispatchLocalPublish(topic, topicKey, data, messageId, timestamp) {
|
|
672
1034
|
void topicKey;
|
|
673
1035
|
const meta = publicationMetadata(messageId, timestamp);
|
|
674
|
-
if (meta) this.handlers.onControl(
|
|
675
|
-
else this.handlers.onControl(
|
|
1036
|
+
if (meta) this.handlers.onControl(CONTROL_ACTION.PUBLISH, topic, data, meta.messageId, meta.timestamp);
|
|
1037
|
+
else this.handlers.onControl(CONTROL_ACTION.PUBLISH, topic, data);
|
|
1038
|
+
}
|
|
1039
|
+
/** Fan out a publication batch to the local onControl path: one
|
|
1040
|
+
* onPublishBatch call when the owner supports it, per-item otherwise. */
|
|
1041
|
+
dispatchLocalPublishBatch(topic, topicKey, items) {
|
|
1042
|
+
if (this.handlers.onPublishBatch) {
|
|
1043
|
+
this.handlers.onPublishBatch(topic, items);
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
for (const item of items) this.dispatchLocalPublish(topic, topicKey, item.data, item.messageId, item.timestamp);
|
|
676
1047
|
}
|
|
677
1048
|
/** True when `route` exists and its owner worker is among `workers`.
|
|
678
1049
|
* Shared by subscribe (skip re-assignment) and publish (route to owner).
|
|
@@ -686,7 +1057,39 @@ var WorkerClusterRuntime = class {
|
|
|
686
1057
|
* listeners can attribute the event to its source tab even after fan-out. */
|
|
687
1058
|
broadcastEvent(eventType, payload, originTabId) {
|
|
688
1059
|
const effectiveOriginTabId = originTabId ?? this.tabId;
|
|
689
|
-
this.
|
|
1060
|
+
this.recordTraffic(payload);
|
|
1061
|
+
this.send({ type: CLUSTER_MESSAGE_TYPE.EVENT, sourceWorkerId: this.workerId, eventType, payload, originTabId: effectiveOriginTabId });
|
|
1062
|
+
}
|
|
1063
|
+
/** Count one fan-out unit toward the adaptive load sample. No-op unless
|
|
1064
|
+
* adaptive weighting is configured. */
|
|
1065
|
+
recordTraffic(payload) {
|
|
1066
|
+
if (this.loadWeighting === void 0) return;
|
|
1067
|
+
this.throughputWindow.messageCount += 1;
|
|
1068
|
+
this.throughputWindow.byteCount += approximatePayloadBytes(payload);
|
|
1069
|
+
}
|
|
1070
|
+
/** Convert the accumulated window into a publishable throughput sample and
|
|
1071
|
+
* reset the accumulator. Returns undefined until a full window has elapsed
|
|
1072
|
+
* so the first write does not emit a zero-width sample. */
|
|
1073
|
+
sampleThroughput(now) {
|
|
1074
|
+
if (this.throughputWindow.startedAt === 0) {
|
|
1075
|
+
this.throughputWindow.startedAt = now;
|
|
1076
|
+
return void 0;
|
|
1077
|
+
}
|
|
1078
|
+
const windowMs = now - this.throughputWindow.startedAt;
|
|
1079
|
+
if (windowMs <= 0) return void 0;
|
|
1080
|
+
const sample = {
|
|
1081
|
+
windowMs,
|
|
1082
|
+
messageCount: this.throughputWindow.messageCount,
|
|
1083
|
+
byteCount: this.throughputWindow.byteCount,
|
|
1084
|
+
// How much later the heartbeat landed than its nominal interval. This
|
|
1085
|
+
// window is anchored at the previous writeRecord (one heartbeat tick),
|
|
1086
|
+
// so a starved event loop stretches windowMs past the interval and the
|
|
1087
|
+
// positive excess is the scheduling-overrun signal.
|
|
1088
|
+
overrunMs: Math.max(0, windowMs - this.heartbeatIntervalMs),
|
|
1089
|
+
sampledAt: now
|
|
1090
|
+
};
|
|
1091
|
+
this.throughputWindow = { startedAt: now, messageCount: 0, byteCount: 0 };
|
|
1092
|
+
return sample;
|
|
690
1093
|
}
|
|
691
1094
|
isAssigned(topic) {
|
|
692
1095
|
const topicKey = createOpaqueKey(topic);
|
|
@@ -776,14 +1179,14 @@ var WorkerClusterRuntime = class {
|
|
|
776
1179
|
const message = event.data;
|
|
777
1180
|
if (!message || message.sourceWorkerId === this.workerId) return;
|
|
778
1181
|
switch (message.type) {
|
|
779
|
-
case
|
|
1182
|
+
case CLUSTER_MESSAGE_TYPE.CONTROL:
|
|
780
1183
|
return this.handleControlMessage(message);
|
|
781
|
-
case
|
|
1184
|
+
case CLUSTER_MESSAGE_TYPE.ROUTE_RELEASED:
|
|
782
1185
|
return this.handleRouteReleasedMessage(message);
|
|
783
|
-
case
|
|
1186
|
+
case CLUSTER_MESSAGE_TYPE.EVENT:
|
|
784
1187
|
this.handlers.onEvent(message.eventType, message.payload, message.sourceWorkerId, message.originTabId);
|
|
785
1188
|
return;
|
|
786
|
-
case
|
|
1189
|
+
case CLUSTER_MESSAGE_TYPE.REGISTRY:
|
|
787
1190
|
this.reconcile();
|
|
788
1191
|
return;
|
|
789
1192
|
default: {
|
|
@@ -800,19 +1203,23 @@ var WorkerClusterRuntime = class {
|
|
|
800
1203
|
if (message.targetWorkerId !== this.workerId) return;
|
|
801
1204
|
this.rememberTopic(message.topic);
|
|
802
1205
|
switch (message.action) {
|
|
803
|
-
case
|
|
1206
|
+
case CONTROL_ACTION.SUBSCRIBE:
|
|
804
1207
|
this.assignedTopics.set(message.topicKey, message.topic);
|
|
805
1208
|
this.confirmRoute(message.topicKey);
|
|
806
1209
|
break;
|
|
807
|
-
case
|
|
1210
|
+
case CONTROL_ACTION.UNSUBSCRIBE:
|
|
808
1211
|
if (this.releaseHandoffOnUnsubscribe(message)) return;
|
|
809
1212
|
break;
|
|
810
|
-
case
|
|
1213
|
+
case CONTROL_ACTION.PUBLISH:
|
|
811
1214
|
if (message.items && message.items.length > 0) {
|
|
1215
|
+
if (this.handlers.onPublishBatch) {
|
|
1216
|
+
this.handlers.onPublishBatch(message.topic, message.items);
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
812
1219
|
for (const item of message.items) {
|
|
813
1220
|
const itemMeta = publicationMetadata(item.messageId, item.timestamp);
|
|
814
|
-
if (itemMeta) this.handlers.onControl(
|
|
815
|
-
else this.handlers.onControl(
|
|
1221
|
+
if (itemMeta) this.handlers.onControl(CONTROL_ACTION.PUBLISH, message.topic, item.data, itemMeta.messageId, itemMeta.timestamp);
|
|
1222
|
+
else this.handlers.onControl(CONTROL_ACTION.PUBLISH, message.topic, item.data);
|
|
816
1223
|
}
|
|
817
1224
|
return;
|
|
818
1225
|
}
|
|
@@ -829,7 +1236,7 @@ var WorkerClusterRuntime = class {
|
|
|
829
1236
|
metadata.timestamp
|
|
830
1237
|
);
|
|
831
1238
|
else this.handlers.onControl(message.action, message.topic, message.data);
|
|
832
|
-
if (message.action !==
|
|
1239
|
+
if (message.action !== CONTROL_ACTION.PUBLISH) this.updateLoad();
|
|
833
1240
|
}
|
|
834
1241
|
/**
|
|
835
1242
|
* When this worker is the previous owner in a graceful handoff and the new
|
|
@@ -841,7 +1248,7 @@ var WorkerClusterRuntime = class {
|
|
|
841
1248
|
this.assignedTopics.delete(message.topicKey);
|
|
842
1249
|
const route = this.readRoute(message.topicKey);
|
|
843
1250
|
if (route?.handoffFromWorkerId !== this.workerId) return false;
|
|
844
|
-
this.handlers.onControl(
|
|
1251
|
+
this.handlers.onControl(CONTROL_ACTION.UNSUBSCRIBE, message.topic, void 0);
|
|
845
1252
|
this.sendRouteReleased(route.workerId, message.topic, message.topicKey, route.generation);
|
|
846
1253
|
this.updateLoad();
|
|
847
1254
|
return true;
|
|
@@ -850,7 +1257,7 @@ var WorkerClusterRuntime = class {
|
|
|
850
1257
|
* generation so only the matching new owner may act on it. */
|
|
851
1258
|
sendRouteReleased(targetWorkerId, topic, topicKey, generation) {
|
|
852
1259
|
this.send({
|
|
853
|
-
type:
|
|
1260
|
+
type: CLUSTER_MESSAGE_TYPE.ROUTE_RELEASED,
|
|
854
1261
|
sourceWorkerId: this.workerId,
|
|
855
1262
|
targetWorkerId,
|
|
856
1263
|
topic,
|
|
@@ -869,14 +1276,24 @@ var WorkerClusterRuntime = class {
|
|
|
869
1276
|
if (!route || this.isStaleRouteRelease(route, message)) return;
|
|
870
1277
|
this.assignedTopics.set(message.topicKey, message.topic);
|
|
871
1278
|
this.confirmRoute(message.topicKey);
|
|
872
|
-
this.handlers.onControl(
|
|
1279
|
+
this.handlers.onControl(CONTROL_ACTION.SUBSCRIBE, message.topic, void 0);
|
|
873
1280
|
this.updateLoad();
|
|
874
1281
|
}
|
|
875
1282
|
/** A ROUTE_RELEASED is stale (and must be dropped) unless the route still
|
|
876
1283
|
* points to us, the release comes from the recorded previous owner, and
|
|
877
|
-
* the release generation is at least as new as ours.
|
|
1284
|
+
* the release generation is at least as new as ours. A replayed ACK from an
|
|
1285
|
+
* earlier handoff round (e.g. an a↔b ping-pong) carries an older generation
|
|
1286
|
+
* and must not confirm the current round. */
|
|
878
1287
|
isStaleRouteRelease(route, message) {
|
|
879
|
-
return route.workerId !== this.workerId || route.handoffFromWorkerId !== message.sourceWorkerId ||
|
|
1288
|
+
return route.workerId !== this.workerId || route.handoffFromWorkerId !== message.sourceWorkerId || message.generation < route.generation;
|
|
1289
|
+
}
|
|
1290
|
+
/** True when an unconfirmed handoff route has been stuck longer than a
|
|
1291
|
+
* worker TTL. The ACK for a live handoff is posted synchronously with the
|
|
1292
|
+
* route write, so anything older than the TTL with a dead previous owner
|
|
1293
|
+
* will never complete — while a fresh unconfirmed route may simply be
|
|
1294
|
+
* waiting out its confirmation flush and must be left alone. */
|
|
1295
|
+
isStaleHandoff(route) {
|
|
1296
|
+
return this.environment.now() - route.updatedAt > this.workerTtlMs;
|
|
880
1297
|
}
|
|
881
1298
|
/** Full reconciliation cycle: workers, subscriptions, and assigned topics. */
|
|
882
1299
|
reconcile() {
|
|
@@ -909,20 +1326,39 @@ var WorkerClusterRuntime = class {
|
|
|
909
1326
|
*/
|
|
910
1327
|
reconcileSubscriptions(workers, activeWorkers) {
|
|
911
1328
|
const liveWorkerIds = new Set(workers.map((worker) => worker.workerId));
|
|
1329
|
+
const recoveryProjectedLoads = /* @__PURE__ */ new Map();
|
|
912
1330
|
for (const topic of this.subscribedTopics) {
|
|
913
1331
|
const topicKey = this.rememberTopic(topic);
|
|
914
1332
|
this.writeSubscriber(topicKey);
|
|
915
1333
|
const route = this.readRoute(topicKey);
|
|
916
1334
|
if (!route || !liveWorkerIds.has(route.workerId)) {
|
|
917
|
-
const owner = selectLeastLoadedWorker(activeWorkers) ?? this.currentRecord;
|
|
1335
|
+
const owner = selectLeastLoadedWorker(activeWorkers, void 0, this.loadWeighting) ?? this.currentRecord;
|
|
918
1336
|
this.writeRoute(topicKey, owner, void 0, (route?.generation ?? 0) + 1);
|
|
919
|
-
this.sendControl(owner.workerId,
|
|
1337
|
+
this.sendControl(owner.workerId, CONTROL_ACTION.SUBSCRIBE, topic, topicKey);
|
|
920
1338
|
this.notifyRegistry();
|
|
921
1339
|
continue;
|
|
922
1340
|
}
|
|
923
1341
|
if (route.confirmedAt === void 0) {
|
|
924
1342
|
if (!route.handoffFromWorkerId) {
|
|
925
|
-
this.sendControl(route.workerId,
|
|
1343
|
+
this.sendControl(route.workerId, CONTROL_ACTION.SUBSCRIBE, topic, topicKey);
|
|
1344
|
+
} else if (!liveWorkerIds.has(route.handoffFromWorkerId) && this.isStaleHandoff(route)) {
|
|
1345
|
+
const owner = selectLeastLoadedWorker(
|
|
1346
|
+
activeWorkers.map((worker) => ({ ...worker, load: recoveryProjectedLoads.get(worker.workerId) ?? worker.load })),
|
|
1347
|
+
void 0,
|
|
1348
|
+
this.loadWeighting
|
|
1349
|
+
) ?? this.currentRecord;
|
|
1350
|
+
recoveryProjectedLoads.set(owner.workerId, (recoveryProjectedLoads.get(owner.workerId) ?? owner.load) + 1);
|
|
1351
|
+
if (owner.workerId !== this.workerId) {
|
|
1352
|
+
const subscriberTabIds = new Set(this.readSubscriberTabIds(topicKey, workers));
|
|
1353
|
+
const ownerSubscribed = workers.some(
|
|
1354
|
+
(worker) => worker.workerId === owner.workerId && subscriberTabIds.has(worker.tabId)
|
|
1355
|
+
);
|
|
1356
|
+
if (ownerSubscribed) continue;
|
|
1357
|
+
}
|
|
1358
|
+
this.writeRoute(topicKey, owner, void 0, route.generation + 1);
|
|
1359
|
+
this.sendControl(owner.workerId, CONTROL_ACTION.SUBSCRIBE, topic, topicKey);
|
|
1360
|
+
this.handlers.onDiagnostic?.({ operation: RELIABILITY_OPERATION.ROUTE_MIGRATION_RECOVERY, topic });
|
|
1361
|
+
this.notifyRegistry();
|
|
926
1362
|
}
|
|
927
1363
|
}
|
|
928
1364
|
}
|
|
@@ -933,7 +1369,7 @@ var WorkerClusterRuntime = class {
|
|
|
933
1369
|
const route = this.readRoute(topicKey);
|
|
934
1370
|
if (route?.workerId === this.workerId) continue;
|
|
935
1371
|
this.assignedTopics.delete(topicKey);
|
|
936
|
-
this.handlers.onControl(
|
|
1372
|
+
this.handlers.onControl(CONTROL_ACTION.UNSUBSCRIBE, topic, void 0);
|
|
937
1373
|
if (route?.handoffFromWorkerId === this.workerId) {
|
|
938
1374
|
this.sendRouteReleased(route.workerId, topic, topicKey, route.generation);
|
|
939
1375
|
}
|
|
@@ -948,14 +1384,14 @@ var WorkerClusterRuntime = class {
|
|
|
948
1384
|
sendControl(targetWorkerId, action, topic, topicKey, data, metadata) {
|
|
949
1385
|
if (targetWorkerId === this.workerId) {
|
|
950
1386
|
switch (action) {
|
|
951
|
-
case
|
|
1387
|
+
case CONTROL_ACTION.SUBSCRIBE:
|
|
952
1388
|
this.assignedTopics.set(topicKey, topic);
|
|
953
1389
|
this.confirmRoute(topicKey);
|
|
954
1390
|
break;
|
|
955
|
-
case
|
|
1391
|
+
case CONTROL_ACTION.UNSUBSCRIBE:
|
|
956
1392
|
this.assignedTopics.delete(topicKey);
|
|
957
1393
|
break;
|
|
958
|
-
case
|
|
1394
|
+
case CONTROL_ACTION.PUBLISH:
|
|
959
1395
|
default:
|
|
960
1396
|
break;
|
|
961
1397
|
}
|
|
@@ -967,11 +1403,11 @@ var WorkerClusterRuntime = class {
|
|
|
967
1403
|
metadata.timestamp
|
|
968
1404
|
);
|
|
969
1405
|
else this.handlers.onControl(action, topic, data);
|
|
970
|
-
if (action !==
|
|
1406
|
+
if (action !== CONTROL_ACTION.PUBLISH) this.updateLoad();
|
|
971
1407
|
return true;
|
|
972
1408
|
}
|
|
973
1409
|
return this.send({
|
|
974
|
-
type:
|
|
1410
|
+
type: CLUSTER_MESSAGE_TYPE.CONTROL,
|
|
975
1411
|
sourceWorkerId: this.workerId,
|
|
976
1412
|
targetWorkerId,
|
|
977
1413
|
action,
|
|
@@ -1076,7 +1512,7 @@ var WorkerClusterRuntime = class {
|
|
|
1076
1512
|
confirmedAt: this.environment.now()
|
|
1077
1513
|
});
|
|
1078
1514
|
const topic = this.knownTopics.get(topicKey);
|
|
1079
|
-
if (topic) this.handlers.onDiagnostic?.({ operation:
|
|
1515
|
+
if (topic) this.handlers.onDiagnostic?.({ operation: RELIABILITY_OPERATION.ROUTE_ACK, topic });
|
|
1080
1516
|
}
|
|
1081
1517
|
/** Remove routes whose topic has no subscribers and whose TTL has expired. */
|
|
1082
1518
|
cleanupOrphanedRoutes(workers) {
|
|
@@ -1111,17 +1547,23 @@ var WorkerClusterRuntime = class {
|
|
|
1111
1547
|
* avoid a REGISTRY storm every 3 s; true on status/role changes that
|
|
1112
1548
|
* peers should observe promptly. */
|
|
1113
1549
|
writeRecord(notify) {
|
|
1114
|
-
|
|
1550
|
+
const now = this.environment.now();
|
|
1551
|
+
const sample = this.loadWeighting !== void 0 ? this.sampleThroughput(now) : void 0;
|
|
1552
|
+
this.currentRecord = {
|
|
1553
|
+
...this.currentRecord,
|
|
1554
|
+
heartbeatAt: now,
|
|
1555
|
+
...sample ? { throughput: sample } : {}
|
|
1556
|
+
};
|
|
1115
1557
|
if (this.storage) writeJson(this.storage, this.workerStorageKey(this.workerId), this.currentRecord);
|
|
1116
1558
|
if (notify) this.notifyRegistry();
|
|
1117
1559
|
}
|
|
1118
1560
|
/** Broadcast a REGISTRY message to trigger reconciliation on other tabs. */
|
|
1119
1561
|
notifyRegistry() {
|
|
1120
|
-
this.send({ type:
|
|
1562
|
+
this.send({ type: CLUSTER_MESSAGE_TYPE.REGISTRY, sourceWorkerId: this.workerId });
|
|
1121
1563
|
}
|
|
1122
1564
|
/** Recompute whether this worker is active (eligible to own topics) or standby. Returns true when changed. */
|
|
1123
1565
|
refreshRole(workers) {
|
|
1124
|
-
const role = this.isActiveAmong(workers) ?
|
|
1566
|
+
const role = this.isActiveAmong(workers) ? WORKER_ROLE.ACTIVE : WORKER_ROLE.STANDBY;
|
|
1125
1567
|
if (role === this.currentRecord.role) return false;
|
|
1126
1568
|
this.currentRecord = { ...this.currentRecord, role };
|
|
1127
1569
|
return true;
|
|
@@ -1219,7 +1661,7 @@ var DataBusTraceReporter = class {
|
|
|
1219
1661
|
/** Start the periodic metrics flush interval. No-op when mode is 'events'
|
|
1220
1662
|
* (no metrics to emit), when disabled, or when already running. */
|
|
1221
1663
|
start() {
|
|
1222
|
-
if (!this.enabled || this.intervalHandle || this.mode ===
|
|
1664
|
+
if (!this.enabled || this.intervalHandle || this.mode === TRACE_MODE.EVENTS) return;
|
|
1223
1665
|
this.stopped = false;
|
|
1224
1666
|
this.intervalStartedAt = this.now();
|
|
1225
1667
|
this.intervalHandle = setInterval(() => this.flush(), this.metricsIntervalMs);
|
|
@@ -1235,11 +1677,40 @@ var DataBusTraceReporter = class {
|
|
|
1235
1677
|
this.stopped = true;
|
|
1236
1678
|
this.pause();
|
|
1237
1679
|
}
|
|
1680
|
+
/** Synchronous sink state for diagnostics: whether delivery is async and how
|
|
1681
|
+
* many events are queued behind the microtask flush. A growing queue under
|
|
1682
|
+
* `asyncSink: true` is the first sign of sink back-pressure. */
|
|
1683
|
+
getSinkState() {
|
|
1684
|
+
return { asyncSink: this.asyncSink, pendingEvents: this.pendingEvents.length };
|
|
1685
|
+
}
|
|
1238
1686
|
/** Record an instantaneous trace event (lifecycle, status, error, etc.). */
|
|
1239
1687
|
event(event) {
|
|
1240
|
-
if (!this.enabled || this.mode ===
|
|
1688
|
+
if (!this.enabled || this.mode === TRACE_MODE.METRICS) return;
|
|
1241
1689
|
this.emit({ ...event, timestamp: this.now() });
|
|
1242
1690
|
}
|
|
1691
|
+
/** Synchronous snapshot of the current metrics window without resetting it.
|
|
1692
|
+
* Returns the same derived counters as a periodic `message_metrics` event,
|
|
1693
|
+
* or null when metrics recording is inactive (disabled or events-only mode).
|
|
1694
|
+
* The window keeps accumulating until the next interval flush. */
|
|
1695
|
+
getMetrics() {
|
|
1696
|
+
if (!this.metricsActive || this.stopped) return null;
|
|
1697
|
+
const timestamp = this.now();
|
|
1698
|
+
const samples = this.latencySamples;
|
|
1699
|
+
return {
|
|
1700
|
+
durationMs: Math.max(0, timestamp - this.intervalStartedAt),
|
|
1701
|
+
received: this.received,
|
|
1702
|
+
dispatched: this.dispatched,
|
|
1703
|
+
topics: this.topics.size,
|
|
1704
|
+
dispatchSamples: samples,
|
|
1705
|
+
dispatchAvgMs: roundMs(samples === 0 ? 0 : this.latencySumMs / samples),
|
|
1706
|
+
dispatchP50Ms: roundMs(percentileMs(this.latencyBuckets, samples, 0.5)),
|
|
1707
|
+
dispatchP95Ms: roundMs(percentileMs(this.latencyBuckets, samples, 0.95)),
|
|
1708
|
+
dispatchMaxMs: roundMs(percentileMs(this.latencyBuckets, samples, 1)),
|
|
1709
|
+
dedupAccepted: this.dedupAccepted,
|
|
1710
|
+
dedupSuppressed: this.dedupSuppressed,
|
|
1711
|
+
timestamp
|
|
1712
|
+
};
|
|
1713
|
+
}
|
|
1243
1714
|
/** Record that a message was received on `topic`; stores its timestamp for latency tracking. */
|
|
1244
1715
|
recordReceived(topic) {
|
|
1245
1716
|
if (!this.metricsActive) return;
|
|
@@ -1297,7 +1768,7 @@ var DataBusTraceReporter = class {
|
|
|
1297
1768
|
* Extracted so the four record / flush methods share one guard expression
|
|
1298
1769
|
* instead of repeating `!this.enabled || this.mode === 'events'` at each. */
|
|
1299
1770
|
get metricsActive() {
|
|
1300
|
-
return this.enabled && this.mode !==
|
|
1771
|
+
return this.enabled && this.mode !== TRACE_MODE.EVENTS;
|
|
1301
1772
|
}
|
|
1302
1773
|
/** Emit the accumulated metrics snapshot if the interval is active. */
|
|
1303
1774
|
flush() {
|
|
@@ -1309,7 +1780,7 @@ var DataBusTraceReporter = class {
|
|
|
1309
1780
|
if (this.received > 0 || this.dispatched > 0 || this.dedupAccepted > 0 || this.dedupSuppressed > 0) {
|
|
1310
1781
|
const samples = this.latencySamples;
|
|
1311
1782
|
this.emit({
|
|
1312
|
-
type:
|
|
1783
|
+
type: TRACE_EVENT_TYPE.MESSAGE_METRICS,
|
|
1313
1784
|
durationMs: Math.max(0, timestamp - this.intervalStartedAt),
|
|
1314
1785
|
received: this.received,
|
|
1315
1786
|
dispatched: this.dispatched,
|
|
@@ -1360,7 +1831,7 @@ var DataBusTraceReporter = class {
|
|
|
1360
1831
|
this.sink(event);
|
|
1361
1832
|
} catch (error) {
|
|
1362
1833
|
if (typeof console !== "undefined" && typeof console.warn === "function") {
|
|
1363
|
-
console.warn(
|
|
1834
|
+
console.warn(`[${DEFAULT_STORAGE_PREFIX}] trace sink threw:`, error);
|
|
1364
1835
|
}
|
|
1365
1836
|
}
|
|
1366
1837
|
}
|
|
@@ -1386,183 +1857,576 @@ function roundMs(value) {
|
|
|
1386
1857
|
return Math.round(value * 10) / 10;
|
|
1387
1858
|
}
|
|
1388
1859
|
|
|
1389
|
-
// src/core/
|
|
1390
|
-
var PUBLICATION_EVENT = "DATABUS_PUBLICATION";
|
|
1391
|
-
var SDK_VERSION = "0.20.69";
|
|
1392
|
-
var DEFAULT_REPLAY_MAX_PER_TOPIC = 100;
|
|
1860
|
+
// src/core/replay-manager.ts
|
|
1393
1861
|
var PersistenceRetryCancelledError = class extends Error {
|
|
1394
1862
|
constructor() {
|
|
1395
1863
|
super("Persistence retry cancelled by lifecycle transition.");
|
|
1396
1864
|
this.name = "PersistenceRetryCancelledError";
|
|
1397
1865
|
}
|
|
1398
1866
|
};
|
|
1399
|
-
var
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1867
|
+
var MAX_RETRY_DELAY_MS2 = 1600;
|
|
1868
|
+
var ReplayManager = class {
|
|
1869
|
+
constructor(deps) {
|
|
1870
|
+
this.deps = deps;
|
|
1871
|
+
this.buffers = deps.enabled ? /* @__PURE__ */ new Map() : null;
|
|
1872
|
+
this.maxPerTopic = deps.maxPerTopic;
|
|
1873
|
+
this.persistence = deps.persistence ?? null;
|
|
1874
|
+
this.retentionMs = deps.retentionMs;
|
|
1875
|
+
this.pruneStrategy = deps.pruneStrategy;
|
|
1876
|
+
this.retentionSweepMs = deps.retentionSweepMs;
|
|
1877
|
+
this.persistenceRetryMaxAttempts = deps.persistenceRetryMaxAttempts;
|
|
1878
|
+
this.persistenceRetryBackoffMs = deps.persistenceRetryBackoffMs;
|
|
1879
|
+
this.now = deps.now;
|
|
1880
|
+
this.trace = deps.trace;
|
|
1881
|
+
this.onPersistenceError = deps.onPersistenceError;
|
|
1882
|
+
this.onDispatchError = deps.onDispatchError;
|
|
1883
|
+
this.hydration = this.hydrate();
|
|
1884
|
+
}
|
|
1885
|
+
deps;
|
|
1886
|
+
buffers;
|
|
1887
|
+
maxPerTopic;
|
|
1888
|
+
persistence;
|
|
1889
|
+
retentionMs;
|
|
1890
|
+
pruneStrategy;
|
|
1891
|
+
retentionSweepMs;
|
|
1417
1892
|
persistenceRetryMaxAttempts;
|
|
1418
1893
|
persistenceRetryBackoffMs;
|
|
1419
|
-
persistenceRetryGeneration = 0;
|
|
1420
|
-
replayHydration;
|
|
1421
|
-
// Retention cleanup is coalesced so a burst of publications does not issue
|
|
1422
|
-
// one IndexedDB read/write transaction per message. The newest cutoff wins.
|
|
1423
|
-
replayRetentionCleanup = null;
|
|
1424
|
-
replayRetentionCutoff = null;
|
|
1425
|
-
replayRetentionTimer = null;
|
|
1426
|
-
initialConfig;
|
|
1427
|
-
hasInitialConfig;
|
|
1428
|
-
trace;
|
|
1429
|
-
dedupMaxEntries;
|
|
1430
|
-
dedupTtlMs;
|
|
1431
|
-
dedupAdaptiveBounds;
|
|
1432
|
-
dedupWindowStartedAt = 0;
|
|
1433
|
-
dedupWindowAccepted = 0;
|
|
1434
|
-
dedupAdaptiveWindowMs = 5e3;
|
|
1435
|
-
dedupSweepMs;
|
|
1436
|
-
dedupSweepTimer = null;
|
|
1437
|
-
dedupEnabled;
|
|
1438
1894
|
now;
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
pendingStop = null;
|
|
1476
|
-
// Minimum interval in ms between automatic recovery attempts.
|
|
1477
|
-
recoveryCooldownMs;
|
|
1478
|
-
recoveryMaxAttempts;
|
|
1479
|
-
constructor(options) {
|
|
1480
|
-
const replay = options.replay;
|
|
1481
|
-
if (replay) {
|
|
1482
|
-
const maxPerTopic = replay.maxPerTopic ?? DEFAULT_REPLAY_MAX_PER_TOPIC;
|
|
1483
|
-
if (!Number.isSafeInteger(maxPerTopic) || maxPerTopic <= 0) {
|
|
1484
|
-
throw new TypeError(
|
|
1485
|
-
`replay.maxPerTopic must be a positive safe integer, got ${String(maxPerTopic)}.`
|
|
1486
|
-
);
|
|
1895
|
+
trace;
|
|
1896
|
+
onPersistenceError;
|
|
1897
|
+
onDispatchError;
|
|
1898
|
+
/** Bumped on suspend/stop so in-flight persistence retries are cancelled. */
|
|
1899
|
+
retryGeneration = 0;
|
|
1900
|
+
pendingReplayPersistence = [];
|
|
1901
|
+
persistenceFlushScheduled = false;
|
|
1902
|
+
hydration;
|
|
1903
|
+
/** Coalesced retention cleanup: the newest cutoff wins while one is running. */
|
|
1904
|
+
retentionCleanup = null;
|
|
1905
|
+
retentionCutoff = null;
|
|
1906
|
+
retentionTimer = null;
|
|
1907
|
+
/** True when replay buffering is enabled. */
|
|
1908
|
+
get enabled() {
|
|
1909
|
+
return this.buffers !== null;
|
|
1910
|
+
}
|
|
1911
|
+
/** Append a dispatched publication to the topic's replay ring buffer.
|
|
1912
|
+
* No-op when replay is disabled. */
|
|
1913
|
+
record(message) {
|
|
1914
|
+
if (!this.buffers) return;
|
|
1915
|
+
let buffer = this.buffers.get(message.topic);
|
|
1916
|
+
if (!buffer) {
|
|
1917
|
+
buffer = [];
|
|
1918
|
+
this.buffers.set(message.topic, buffer);
|
|
1919
|
+
}
|
|
1920
|
+
buffer.push(message);
|
|
1921
|
+
const ageBounded = this.pruneStrategy !== PRUNE_STRATEGY.COUNT && this.retentionMs !== void 0;
|
|
1922
|
+
if (this.pruneStrategy !== PRUNE_STRATEGY.AGE || !ageBounded) {
|
|
1923
|
+
while (buffer.length > this.maxPerTopic) buffer.shift();
|
|
1924
|
+
}
|
|
1925
|
+
if (ageBounded) {
|
|
1926
|
+
const cutoff = this.now() - this.retentionMs;
|
|
1927
|
+
while (buffer.length > 0) {
|
|
1928
|
+
const first = buffer[0];
|
|
1929
|
+
if (!first || first.timestamp === void 0 || first.timestamp >= cutoff) break;
|
|
1930
|
+
buffer.shift();
|
|
1487
1931
|
}
|
|
1488
1932
|
}
|
|
1489
|
-
this.
|
|
1490
|
-
this.
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
if (this.replayRetentionMs !== void 0 && (!Number.isFinite(this.replayRetentionMs) || this.replayRetentionMs <= 0)) {
|
|
1496
|
-
throw new TypeError("replay.retentionMs must be a positive finite number.");
|
|
1933
|
+
if (!this.persistence) return;
|
|
1934
|
+
if (this.persistence.appendBatch) {
|
|
1935
|
+
this.pendingReplayPersistence.push(message);
|
|
1936
|
+
this.schedulePersistenceFlush();
|
|
1937
|
+
} else {
|
|
1938
|
+
void this.withPersistenceRetry(PERSISTENCE_OPERATION.APPEND, () => this.persistence.append(message)).catch((error) => this.onPersistenceError(error));
|
|
1497
1939
|
}
|
|
1498
|
-
this.
|
|
1499
|
-
|
|
1500
|
-
throw new TypeError("replay.retentionSweepMs must be a positive finite number.");
|
|
1940
|
+
if (this.retentionMs !== void 0 && this.persistence.clearBefore) {
|
|
1941
|
+
this.scheduleRetentionCleanup(this.now() - this.retentionMs);
|
|
1501
1942
|
}
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1943
|
+
}
|
|
1944
|
+
/** Deliver buffered history to a newly-registered handler. For an exact
|
|
1945
|
+
* topic this is that topic's ring; for a wildcard subscription every
|
|
1946
|
+
* buffered topic matching the pattern contributes (in buffer insertion
|
|
1947
|
+
* order). Replay deliveries are marked `replayed: true` and are not counted
|
|
1948
|
+
* into trace metrics.
|
|
1949
|
+
*
|
|
1950
|
+
* When a durable persistence backend is present, delivery waits for the
|
|
1951
|
+
* hydration load to settle first; `isHandlerActive` is then consulted so a
|
|
1952
|
+
* handler that unsubscribed during the async load does not receive history.
|
|
1953
|
+
*/
|
|
1954
|
+
deliverReplay(topic, replayOption, handler, isHandlerActive) {
|
|
1955
|
+
if (!this.buffers) return;
|
|
1956
|
+
const limit = typeof replayOption === "number" ? Math.min(Math.floor(replayOption), this.maxPerTopic) : this.maxPerTopic;
|
|
1957
|
+
if (this.persistence) {
|
|
1958
|
+
void this.hydration.then(() => {
|
|
1959
|
+
if (isHandlerActive?.() ?? true) this.deliver(topic, limit, handler);
|
|
1960
|
+
});
|
|
1961
|
+
return;
|
|
1506
1962
|
}
|
|
1507
|
-
|
|
1508
|
-
|
|
1963
|
+
this.deliver(topic, limit, handler);
|
|
1964
|
+
}
|
|
1965
|
+
/** Clean up a topic that lost its last local handler: drop the ring buffer,
|
|
1966
|
+
* filter queued batch flushes (so an in-flight append cannot undo the
|
|
1967
|
+
* clearTopic), and prune durable history. */
|
|
1968
|
+
onTopicUnsubscribed(topic) {
|
|
1969
|
+
if (!this.buffers) return;
|
|
1970
|
+
this.buffers.delete(topic);
|
|
1971
|
+
this.pendingReplayPersistence = this.pendingReplayPersistence.filter((message) => message.topic !== topic);
|
|
1972
|
+
if (this.persistence?.clearTopic) {
|
|
1973
|
+
void this.withPersistenceRetry(PERSISTENCE_OPERATION.CLEAR_TOPIC, () => this.persistence.clearTopic(topic)).catch((error) => this.onPersistenceError(error));
|
|
1974
|
+
}
|
|
1975
|
+
}
|
|
1976
|
+
/** Clear all in-memory replay buffers and, when supported, durable history.
|
|
1977
|
+
* Reports persistence failures and rethrows, mirroring the public API
|
|
1978
|
+
* contract that callers can observe a failed clear. */
|
|
1979
|
+
async clearAll() {
|
|
1980
|
+
if (!this.buffers) return;
|
|
1981
|
+
this.buffers.clear();
|
|
1982
|
+
this.pendingReplayPersistence = [];
|
|
1983
|
+
if (this.persistence?.clear) {
|
|
1984
|
+
try {
|
|
1985
|
+
await this.withPersistenceRetry(PERSISTENCE_OPERATION.CLEAR, () => this.persistence.clear());
|
|
1986
|
+
} catch (error) {
|
|
1987
|
+
this.onPersistenceError(error);
|
|
1988
|
+
throw error;
|
|
1989
|
+
}
|
|
1509
1990
|
}
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
if (!
|
|
1514
|
-
|
|
1991
|
+
}
|
|
1992
|
+
/** Clear replay history for one exact topic, including durable storage. */
|
|
1993
|
+
async clearTopic(topic) {
|
|
1994
|
+
if (!this.buffers) return;
|
|
1995
|
+
this.buffers.delete(topic);
|
|
1996
|
+
this.pendingReplayPersistence = this.pendingReplayPersistence.filter((message) => message.topic !== topic);
|
|
1997
|
+
if (this.persistence?.clearTopic) {
|
|
1998
|
+
try {
|
|
1999
|
+
await this.withPersistenceRetry(PERSISTENCE_OPERATION.CLEAR_TOPIC, () => this.persistence.clearTopic(topic));
|
|
2000
|
+
} catch (error) {
|
|
2001
|
+
this.onPersistenceError(error);
|
|
2002
|
+
throw error;
|
|
2003
|
+
}
|
|
1515
2004
|
}
|
|
1516
|
-
|
|
1517
|
-
|
|
2005
|
+
}
|
|
2006
|
+
/** Remove replay entries older than an epoch-millisecond cutoff. */
|
|
2007
|
+
async clearBefore(timestamp) {
|
|
2008
|
+
if (!Number.isFinite(timestamp)) throw new TypeError("timestamp must be finite.");
|
|
2009
|
+
if (this.buffers) {
|
|
2010
|
+
for (const [topic, messages] of this.buffers) {
|
|
2011
|
+
const kept = messages.filter((message) => message.timestamp === void 0 || message.timestamp >= timestamp);
|
|
2012
|
+
if (kept.length) this.buffers.set(topic, kept);
|
|
2013
|
+
else this.buffers.delete(topic);
|
|
2014
|
+
}
|
|
1518
2015
|
}
|
|
1519
|
-
this.
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
this.
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
this.dedupWindowStartedAt = this.now();
|
|
1530
|
-
this.dedupSweepMs = dedup?.sweepMs;
|
|
1531
|
-
this.dedupEnabled = dedup !== void 0;
|
|
1532
|
-
if (!Number.isSafeInteger(this.dedupMaxEntries) || this.dedupMaxEntries <= 0) {
|
|
1533
|
-
throw new TypeError("dedup.maxEntries must be a positive safe integer.");
|
|
1534
|
-
}
|
|
1535
|
-
if (!Number.isFinite(this.dedupTtlMs) || this.dedupTtlMs <= 0) {
|
|
1536
|
-
throw new TypeError("dedup.ttlMs must be a positive finite number.");
|
|
1537
|
-
}
|
|
1538
|
-
if (this.dedupSweepMs !== void 0 && (!Number.isFinite(this.dedupSweepMs) || this.dedupSweepMs <= 0)) {
|
|
1539
|
-
throw new TypeError("dedup.sweepMs must be a positive finite number.");
|
|
2016
|
+
this.pendingReplayPersistence = this.pendingReplayPersistence.filter(
|
|
2017
|
+
(message) => message.timestamp === void 0 || message.timestamp >= timestamp
|
|
2018
|
+
);
|
|
2019
|
+
if (this.persistence?.clearBefore) {
|
|
2020
|
+
try {
|
|
2021
|
+
await this.withPersistenceRetry(PERSISTENCE_OPERATION.CLEAR_BEFORE, () => this.persistence.clearBefore(timestamp));
|
|
2022
|
+
} catch (error) {
|
|
2023
|
+
this.onPersistenceError(error);
|
|
2024
|
+
throw error;
|
|
2025
|
+
}
|
|
1540
2026
|
}
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
2027
|
+
}
|
|
2028
|
+
/** Start the periodic retention sweep. No-op when no durable retention
|
|
2029
|
+
* config makes it necessary. */
|
|
2030
|
+
start() {
|
|
2031
|
+
if (this.retentionTimer || !this.retentionMs || !this.retentionSweepMs || !this.persistence?.clearBefore) return;
|
|
2032
|
+
this.retentionTimer = setInterval(() => {
|
|
2033
|
+
this.scheduleRetentionCleanup(this.now() - this.retentionMs);
|
|
2034
|
+
}, this.retentionSweepMs);
|
|
2035
|
+
}
|
|
2036
|
+
/** Stop the periodic retention sweep. */
|
|
2037
|
+
stop() {
|
|
2038
|
+
if (this.retentionTimer) clearInterval(this.retentionTimer);
|
|
2039
|
+
this.retentionTimer = null;
|
|
2040
|
+
}
|
|
2041
|
+
/** Suspend the manager: cancel in-flight persistence retries (so a hidden tab
|
|
2042
|
+
* or stopped bus does not keep hammering the store) and stop the sweep. */
|
|
2043
|
+
suspend() {
|
|
2044
|
+
this.retryGeneration += 1;
|
|
2045
|
+
this.stop();
|
|
2046
|
+
}
|
|
2047
|
+
/** Drop all in-memory buffers (used on full teardown). */
|
|
2048
|
+
resetBuffers() {
|
|
2049
|
+
this.buffers?.clear();
|
|
2050
|
+
}
|
|
2051
|
+
/** Buffer occupancy for diagnostics. `bytes` is an approximate in-memory
|
|
2052
|
+
* payload footprint (same heuristic as adaptive load weighting), computed on
|
|
2053
|
+
* demand so the hot append path never pays for it. */
|
|
2054
|
+
getStats() {
|
|
2055
|
+
let messages = 0;
|
|
2056
|
+
let bytes = 0;
|
|
2057
|
+
if (this.buffers) {
|
|
2058
|
+
for (const buffer of this.buffers.values()) {
|
|
2059
|
+
messages += buffer.length;
|
|
2060
|
+
for (const message of buffer) bytes += approximatePayloadBytes(message.data);
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
return { enabled: this.enabled, topics: this.buffers?.size ?? 0, messages, bytes };
|
|
2064
|
+
}
|
|
2065
|
+
/** Deliver history from one topic's ring to a handler, isolating a throwing
|
|
2066
|
+
* handler so the remaining buffers are still delivered. */
|
|
2067
|
+
deliver(topic, limit, handler) {
|
|
2068
|
+
if (!this.buffers || limit <= 0) return;
|
|
2069
|
+
const deliverBuffer = (buffer2) => {
|
|
2070
|
+
for (const message of buffer2.slice(-limit)) {
|
|
2071
|
+
try {
|
|
2072
|
+
handler({ ...message, replayed: true });
|
|
2073
|
+
} catch (error) {
|
|
2074
|
+
this.onDispatchError(error);
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
};
|
|
2078
|
+
if (isWildcardTopic(topic)) {
|
|
2079
|
+
for (const [bufferedTopic, buffer2] of this.buffers) {
|
|
2080
|
+
if (topicMatchesPattern(topic, bufferedTopic)) deliverBuffer(buffer2);
|
|
2081
|
+
}
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
const buffer = this.buffers.get(topic);
|
|
2085
|
+
if (buffer) deliverBuffer(buffer);
|
|
2086
|
+
}
|
|
2087
|
+
/** Coalesce queued persistence appends into a single microtask batch so a
|
|
2088
|
+
* burst of publications does not issue one IndexedDB transaction each.
|
|
2089
|
+
* Only reachable when the backend advertises `appendBatch` (the sole queuer,
|
|
2090
|
+
* `record()`, guards on it), so the batched path is unconditional here. */
|
|
2091
|
+
schedulePersistenceFlush() {
|
|
2092
|
+
if (this.persistenceFlushScheduled) return;
|
|
2093
|
+
this.persistenceFlushScheduled = true;
|
|
2094
|
+
queueMicrotask(() => {
|
|
2095
|
+
this.persistenceFlushScheduled = false;
|
|
2096
|
+
const batch = this.pendingReplayPersistence.splice(0);
|
|
2097
|
+
if (batch.length === 0 || !this.persistence) return;
|
|
2098
|
+
void this.withPersistenceRetry(PERSISTENCE_OPERATION.APPEND, () => this.persistence.appendBatch(batch)).catch((error) => this.onPersistenceError(error));
|
|
2099
|
+
});
|
|
2100
|
+
}
|
|
2101
|
+
/** Load durable history into the in-memory rings once at startup, pruning
|
|
2102
|
+
* entries past the retention window first. Failures are reported but do not
|
|
2103
|
+
* block startup — the bus runs with whatever survived. */
|
|
2104
|
+
async hydrate() {
|
|
2105
|
+
if (!this.buffers || !this.persistence) {
|
|
2106
|
+
return;
|
|
2107
|
+
}
|
|
2108
|
+
try {
|
|
2109
|
+
if (this.retentionMs !== void 0 && this.persistence.clearBefore) {
|
|
2110
|
+
await this.withPersistenceRetry(PERSISTENCE_OPERATION.CLEAR_BEFORE, () => this.persistence.clearBefore(this.now() - this.retentionMs));
|
|
2111
|
+
}
|
|
2112
|
+
for (const message of await this.withPersistenceRetry(PERSISTENCE_OPERATION.LOAD, () => this.persistence.load())) {
|
|
2113
|
+
let buffer = this.buffers.get(message.topic);
|
|
2114
|
+
if (!buffer) {
|
|
2115
|
+
buffer = [];
|
|
2116
|
+
this.buffers.set(message.topic, buffer);
|
|
2117
|
+
}
|
|
2118
|
+
buffer.push(message);
|
|
2119
|
+
if (buffer.length > this.maxPerTopic) buffer.shift();
|
|
2120
|
+
}
|
|
2121
|
+
} catch (error) {
|
|
2122
|
+
this.onPersistenceError(error);
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
/** Coalesce retention cleanup: the newest cutoff wins while one pass runs,
|
|
2126
|
+
* so a burst of publications issues at most one clearBefore transaction. */
|
|
2127
|
+
scheduleRetentionCleanup(cutoff) {
|
|
2128
|
+
if (!this.persistence?.clearBefore) return;
|
|
2129
|
+
if (this.retentionCutoff === null || cutoff > this.retentionCutoff) {
|
|
2130
|
+
this.retentionCutoff = cutoff;
|
|
2131
|
+
}
|
|
2132
|
+
if (this.retentionCleanup) return;
|
|
2133
|
+
this.retentionCleanup = (async () => {
|
|
2134
|
+
while (this.retentionCutoff !== null) {
|
|
2135
|
+
const nextCutoff = this.retentionCutoff;
|
|
2136
|
+
this.retentionCutoff = null;
|
|
2137
|
+
try {
|
|
2138
|
+
await this.persistence.clearBefore(nextCutoff);
|
|
2139
|
+
} catch (error) {
|
|
2140
|
+
this.onPersistenceError(error);
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
})().finally(() => {
|
|
2144
|
+
this.retentionCleanup = null;
|
|
2145
|
+
if (this.retentionCutoff !== null) {
|
|
2146
|
+
this.scheduleRetentionCleanup(this.retentionCutoff);
|
|
2147
|
+
}
|
|
2148
|
+
});
|
|
2149
|
+
}
|
|
2150
|
+
/** Run a persistence operation with exponential backoff on transient failure.
|
|
2151
|
+
* Bumped `retryGeneration` (suspend/stop) cancels the loop early; a
|
|
2152
|
+
* structurally failing operation throws after `persistenceRetryMaxAttempts`,
|
|
2153
|
+
* leaving the ring buffer intact so the bus keeps working. */
|
|
2154
|
+
async withPersistenceRetry(persistenceOperation, operation) {
|
|
2155
|
+
const generation = this.retryGeneration;
|
|
2156
|
+
let attempt = 0;
|
|
2157
|
+
let delay = this.persistenceRetryBackoffMs;
|
|
2158
|
+
while (true) {
|
|
2159
|
+
attempt += 1;
|
|
2160
|
+
try {
|
|
2161
|
+
if (generation !== this.retryGeneration) throw new PersistenceRetryCancelledError();
|
|
2162
|
+
return await operation();
|
|
2163
|
+
} catch (error) {
|
|
2164
|
+
if (error instanceof PersistenceRetryCancelledError || generation !== this.retryGeneration) {
|
|
2165
|
+
throw new PersistenceRetryCancelledError();
|
|
2166
|
+
}
|
|
2167
|
+
if (attempt >= this.persistenceRetryMaxAttempts) throw error;
|
|
2168
|
+
this.trace.event({
|
|
2169
|
+
type: TRACE_EVENT_TYPE.RELIABILITY,
|
|
2170
|
+
operation: RELIABILITY_OPERATION.PERSISTENCE_RETRY,
|
|
2171
|
+
persistenceOperation,
|
|
2172
|
+
attempt
|
|
2173
|
+
});
|
|
2174
|
+
if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay));
|
|
2175
|
+
if (generation !== this.retryGeneration) throw new PersistenceRetryCancelledError();
|
|
2176
|
+
delay = Math.min(delay * 2, MAX_RETRY_DELAY_MS2);
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
};
|
|
2181
|
+
|
|
2182
|
+
// src/core/dedup-manager.ts
|
|
2183
|
+
var ADAPTIVE_WINDOW_MS = 5e3;
|
|
2184
|
+
var QUIET_RATE_PER_MS = 0.01;
|
|
2185
|
+
var DedupManager = class {
|
|
2186
|
+
enabled;
|
|
2187
|
+
maxEntries;
|
|
2188
|
+
ttlMs;
|
|
2189
|
+
adaptiveBounds;
|
|
2190
|
+
sweepMs;
|
|
2191
|
+
now;
|
|
2192
|
+
trace;
|
|
2193
|
+
seenMessageIds = /* @__PURE__ */ new Map();
|
|
2194
|
+
sweepTimer = null;
|
|
2195
|
+
windowStartedAt = 0;
|
|
2196
|
+
windowAccepted = 0;
|
|
2197
|
+
suppressed = 0;
|
|
2198
|
+
accepted = 0;
|
|
2199
|
+
constructor(options) {
|
|
2200
|
+
this.enabled = options.enabled;
|
|
2201
|
+
this.maxEntries = options.maxEntries;
|
|
2202
|
+
this.ttlMs = options.ttlMs;
|
|
2203
|
+
this.adaptiveBounds = options.adaptiveBounds;
|
|
2204
|
+
this.sweepMs = options.sweepMs;
|
|
2205
|
+
this.now = options.now;
|
|
2206
|
+
this.trace = options.trace;
|
|
2207
|
+
this.windowStartedAt = this.now();
|
|
2208
|
+
}
|
|
2209
|
+
/** True when a publication carrying `messageId` was already seen. Records the
|
|
2210
|
+
* ID and updates counters/trace on acceptance. Disabled or ID-less messages
|
|
2211
|
+
* always pass through. */
|
|
2212
|
+
isDuplicate(messageId, topic) {
|
|
2213
|
+
if (!this.enabled || !messageId) return false;
|
|
2214
|
+
const now = this.now();
|
|
2215
|
+
const ttlMs = this.currentTtl();
|
|
2216
|
+
for (const [id, timestamp] of this.seenMessageIds) {
|
|
2217
|
+
if (now - timestamp > ttlMs) this.seenMessageIds.delete(id);
|
|
2218
|
+
}
|
|
2219
|
+
if (this.seenMessageIds.has(messageId)) {
|
|
2220
|
+
this.suppressed += 1;
|
|
2221
|
+
this.trace.event({
|
|
2222
|
+
type: TRACE_EVENT_TYPE.RELIABILITY,
|
|
2223
|
+
operation: RELIABILITY_OPERATION.DEDUP_SUPPRESSED,
|
|
2224
|
+
topic
|
|
2225
|
+
});
|
|
2226
|
+
this.trace.recordDedupSuppressed();
|
|
2227
|
+
return true;
|
|
2228
|
+
}
|
|
2229
|
+
this.seenMessageIds.set(messageId, now);
|
|
2230
|
+
this.accepted += 1;
|
|
2231
|
+
this.windowAccepted += 1;
|
|
2232
|
+
this.trace.recordDedupAccepted();
|
|
2233
|
+
while (this.seenMessageIds.size > this.maxEntries) {
|
|
2234
|
+
const oldest = this.seenMessageIds.keys().next().value;
|
|
2235
|
+
if (oldest === void 0) break;
|
|
2236
|
+
this.seenMessageIds.delete(oldest);
|
|
2237
|
+
}
|
|
2238
|
+
return false;
|
|
2239
|
+
}
|
|
2240
|
+
/** Start the periodic expiry sweep. No-op when disabled or no sweepMs was
|
|
2241
|
+
* configured (the hot-path opportunistic expiry still bounds the map). */
|
|
2242
|
+
start() {
|
|
2243
|
+
if (this.sweepTimer || !this.enabled || !this.sweepMs) return;
|
|
2244
|
+
this.sweepTimer = setInterval(() => this.pruneExpired(), this.sweepMs);
|
|
2245
|
+
}
|
|
2246
|
+
/** Stop the periodic expiry sweep. */
|
|
2247
|
+
stop() {
|
|
2248
|
+
if (this.sweepTimer) clearInterval(this.sweepTimer);
|
|
2249
|
+
this.sweepTimer = null;
|
|
2250
|
+
}
|
|
2251
|
+
/** Return bounded deduplication counters for diagnostics and health checks. */
|
|
2252
|
+
getStats() {
|
|
2253
|
+
return {
|
|
2254
|
+
enabled: this.enabled,
|
|
2255
|
+
tracked: this.seenMessageIds.size,
|
|
2256
|
+
suppressed: this.suppressed,
|
|
2257
|
+
accepted: this.accepted,
|
|
2258
|
+
...this.adaptiveBounds ? { ttlMs: this.currentTtl() } : {}
|
|
2259
|
+
};
|
|
2260
|
+
}
|
|
2261
|
+
/** Drop all remembered IDs and reset dedup counters. */
|
|
2262
|
+
reset() {
|
|
2263
|
+
this.seenMessageIds.clear();
|
|
2264
|
+
this.suppressed = 0;
|
|
2265
|
+
this.accepted = 0;
|
|
2266
|
+
this.windowStartedAt = this.now();
|
|
2267
|
+
this.windowAccepted = 0;
|
|
2268
|
+
}
|
|
2269
|
+
/** Remove IDs whose timestamp predates the effective TTL cutoff. */
|
|
2270
|
+
pruneExpired() {
|
|
2271
|
+
const cutoff = this.now() - this.currentTtl();
|
|
2272
|
+
for (const [id, timestamp] of this.seenMessageIds) {
|
|
2273
|
+
if (timestamp < cutoff) this.seenMessageIds.delete(id);
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
/** Effective TTL: the fixed `ttlMs` unless adaptive bounds are configured, in
|
|
2277
|
+
* which case a higher recent message rate shortens the window (dedup only
|
|
2278
|
+
* needs to live long enough to bridge duplicate bursts). The window resets
|
|
2279
|
+
* every ADAPTIVE_WINDOW_MS. */
|
|
2280
|
+
currentTtl() {
|
|
2281
|
+
if (!this.adaptiveBounds) return this.ttlMs;
|
|
2282
|
+
const now = this.now();
|
|
2283
|
+
const elapsed = now - this.windowStartedAt;
|
|
2284
|
+
if (elapsed >= ADAPTIVE_WINDOW_MS) {
|
|
2285
|
+
this.windowStartedAt = now;
|
|
2286
|
+
this.windowAccepted = 0;
|
|
2287
|
+
return this.adaptiveBounds.maxMs;
|
|
2288
|
+
}
|
|
2289
|
+
const rate = this.windowAccepted / Math.max(1, elapsed);
|
|
2290
|
+
const factor = Math.min(1, rate / QUIET_RATE_PER_MS);
|
|
2291
|
+
return this.adaptiveBounds.maxMs - (this.adaptiveBounds.maxMs - this.adaptiveBounds.minMs) * factor;
|
|
2292
|
+
}
|
|
2293
|
+
};
|
|
2294
|
+
|
|
2295
|
+
// src/core/version.ts
|
|
2296
|
+
var SDK_VERSION = true ? "0.20.85" : "";
|
|
2297
|
+
|
|
2298
|
+
// src/core/data-bus.ts
|
|
2299
|
+
var DEFAULT_REPLAY_MAX_PER_TOPIC = 100;
|
|
2300
|
+
var CrossTabDataBus = class {
|
|
2301
|
+
transport;
|
|
2302
|
+
cluster;
|
|
2303
|
+
// Map of topic → set of local subscribers.
|
|
2304
|
+
topicHandlers = /* @__PURE__ */ new Map();
|
|
2305
|
+
// Topics for which the transport has been asked to subscribe (used to avoid
|
|
2306
|
+
// duplicate subscribe calls during reconnection).
|
|
2307
|
+
transportSubscribedTopics = /* @__PURE__ */ new Set();
|
|
2308
|
+
statusHandlers = /* @__PURE__ */ new Set();
|
|
2309
|
+
errorHandlers = /* @__PURE__ */ new Set();
|
|
2310
|
+
replayManager;
|
|
2311
|
+
initialConfig;
|
|
2312
|
+
hasInitialConfig;
|
|
2313
|
+
trace;
|
|
2314
|
+
dedupManager;
|
|
2315
|
+
now;
|
|
2316
|
+
activeConfig;
|
|
2317
|
+
status = WORKER_STATUS.DISCONNECTED;
|
|
2318
|
+
started = false;
|
|
2319
|
+
stopping = false;
|
|
2320
|
+
transportReady = false;
|
|
2321
|
+
// Last transport failure, retained so ready() can surface it to callers who
|
|
2322
|
+
// never awaited start() directly. Cleared on the next successful start.
|
|
2323
|
+
lastError = null;
|
|
2324
|
+
lastErrorAt = null;
|
|
2325
|
+
// Unified failure ledger for the health summary: the most recent failure of
|
|
2326
|
+
// any source (transport, persistence, dispatch) since the last explicit start.
|
|
2327
|
+
lastFailure = null;
|
|
2328
|
+
persistenceFailureCount = 0;
|
|
2329
|
+
persistenceLastFailureAt = null;
|
|
2330
|
+
persistenceLastErrorMessage = null;
|
|
2331
|
+
// Gate that serialises start/stop/suspend/resume — only one lifecycle
|
|
2332
|
+
// transition at a time. Resets to null once the operation settles.
|
|
2333
|
+
startPromise = null;
|
|
2334
|
+
// Timestamp of the last automatic transport recovery attempt.
|
|
2335
|
+
// Used to avoid a tight retry loop when the transport fails repeatedly.
|
|
2336
|
+
lastRecoveryAt = 0;
|
|
2337
|
+
// Monotonic attempt number within one runtime recovery sequence; reset once
|
|
2338
|
+
// a transport reopen succeeds so traces can correlate repeated failures.
|
|
2339
|
+
recoveryAttempt = 0;
|
|
2340
|
+
recoveryExhausted = false;
|
|
2341
|
+
/** Monotonic generation incremented on every successful transport open.
|
|
2342
|
+
* Stays in lockstep with `lastSuccessAt` so callers can detect that the
|
|
2343
|
+
* transport has been reopened even if the timestamp window is short. */
|
|
2344
|
+
recoveryGeneration = 0;
|
|
2345
|
+
/** Timestamp of the most recent successful transport open. Null until the
|
|
2346
|
+
* transport has reached the `ready` state at least once. */
|
|
2347
|
+
lastSuccessAt = null;
|
|
2348
|
+
// True while the tab is hidden so an in-flight transport start does not mark
|
|
2349
|
+
// the transport ready after suspendTransport() has stopped it.
|
|
2350
|
+
suspended = false;
|
|
2351
|
+
// Single gate for async transport.stop() cleanup, shared by failed opens and
|
|
2352
|
+
// page-hide suspension. Kept separate from startPromise so ready() still
|
|
2353
|
+
// surfaces a failure while later opens and automatic recovery wait for the
|
|
2354
|
+
// stop to settle.
|
|
2355
|
+
pendingStop = null;
|
|
2356
|
+
// Minimum interval in ms between automatic recovery attempts.
|
|
2357
|
+
recoveryCooldownMs;
|
|
2358
|
+
recoveryMaxAttempts;
|
|
2359
|
+
constructor(options) {
|
|
2360
|
+
const replay = options.replay;
|
|
2361
|
+
assertReplayOptions(replay);
|
|
2362
|
+
const { autoStart, initialConfig, trace, transport, dedup, recovery, ...clusterOptions } = options;
|
|
2363
|
+
assertRecoveryOptions(recovery);
|
|
2364
|
+
this.recoveryCooldownMs = recovery?.cooldownMs ?? 1e3;
|
|
2365
|
+
this.recoveryMaxAttempts = recovery?.maxAttempts ?? Number.POSITIVE_INFINITY;
|
|
2366
|
+
this.now = dedup?.now ?? Date.now;
|
|
2367
|
+
this.transport = transport;
|
|
2368
|
+
this.initialConfig = initialConfig;
|
|
2369
|
+
this.hasInitialConfig = "initialConfig" in options;
|
|
2370
|
+
this.trace = new DataBusTraceReporter(trace);
|
|
2371
|
+
this.replayManager = new ReplayManager({
|
|
2372
|
+
enabled: replay !== void 0,
|
|
2373
|
+
maxPerTopic: replay?.maxPerTopic ?? DEFAULT_REPLAY_MAX_PER_TOPIC,
|
|
2374
|
+
persistence: replay?.persistence ?? null,
|
|
2375
|
+
retentionMs: replay?.retentionMs,
|
|
2376
|
+
pruneStrategy: replay?.pruneStrategy ?? PRUNE_STRATEGY.COUNT,
|
|
2377
|
+
retentionSweepMs: replay?.retentionSweepMs,
|
|
2378
|
+
persistenceRetryMaxAttempts: replay?.persistenceRetry?.maxAttempts ?? 1,
|
|
2379
|
+
persistenceRetryBackoffMs: replay?.persistenceRetry?.backoffMs ?? 50,
|
|
2380
|
+
now: this.now,
|
|
2381
|
+
trace: this.trace,
|
|
2382
|
+
onPersistenceError: (error) => this.reportPersistenceError(error),
|
|
2383
|
+
onDispatchError: (error) => this.reportError(error, FAILURE_SOURCE.DISPATCH)
|
|
2384
|
+
});
|
|
2385
|
+
assertDedupOptions(dedup);
|
|
2386
|
+
this.dedupManager = new DedupManager({
|
|
2387
|
+
enabled: dedup !== void 0,
|
|
2388
|
+
maxEntries: dedup?.maxEntries ?? 1e3,
|
|
2389
|
+
ttlMs: dedup?.ttlMs ?? 6e4,
|
|
2390
|
+
adaptiveBounds: dedup?.adaptiveTtl,
|
|
2391
|
+
sweepMs: dedup?.sweepMs,
|
|
2392
|
+
now: this.now,
|
|
2393
|
+
trace: this.trace
|
|
2394
|
+
});
|
|
2395
|
+
this.cluster = new WorkerClusterRuntime({
|
|
2396
|
+
...clusterOptions,
|
|
2397
|
+
handlers: {
|
|
2398
|
+
// The cluster calls `onControl` when it receives a SUBSCRIBE/UNSUBSCRIBE/PUBLISH
|
|
2399
|
+
// control message — meaning the owning Worker has delegated the action to us.
|
|
2400
|
+
onControl: (action, topic, data, messageId, timestamp) => {
|
|
2401
|
+
switch (action) {
|
|
2402
|
+
case CONTROL_ACTION.SUBSCRIBE:
|
|
2403
|
+
if (this.subscribeTransport(topic)) this.traceSubscription(SUBSCRIPTION_ACTION.SUBSCRIBE, topic);
|
|
1565
2404
|
break;
|
|
2405
|
+
case CONTROL_ACTION.UNSUBSCRIBE:
|
|
2406
|
+
if (this.unsubscribeTransport(topic)) this.traceSubscription(SUBSCRIPTION_ACTION.UNSUBSCRIBE, topic);
|
|
2407
|
+
break;
|
|
2408
|
+
case CONTROL_ACTION.PUBLISH:
|
|
2409
|
+
this.runTransport(() => this.transport.publish(topic, data, publicationMetadata(messageId, timestamp)));
|
|
2410
|
+
break;
|
|
2411
|
+
default:
|
|
2412
|
+
break;
|
|
2413
|
+
}
|
|
2414
|
+
},
|
|
2415
|
+
// Batched variant of the PUBLISH action (CONTROL frames carrying
|
|
2416
|
+
// multiple items, and the local publishBatch fast path). Uses the
|
|
2417
|
+
// transport's one-frame publishBatch when available, preserving
|
|
2418
|
+
// per-item metadata; otherwise falls back to per-item publishes.
|
|
2419
|
+
onPublishBatch: (topic, items) => {
|
|
2420
|
+
if (typeof this.transport.publishBatch === "function") {
|
|
2421
|
+
this.runTransport(() => this.transport.publishBatch(topic, items));
|
|
2422
|
+
return;
|
|
2423
|
+
}
|
|
2424
|
+
for (const item of items) {
|
|
2425
|
+
this.runTransport(() => this.transport.publish(
|
|
2426
|
+
topic,
|
|
2427
|
+
item.data,
|
|
2428
|
+
publicationMetadata(item.messageId, item.timestamp)
|
|
2429
|
+
));
|
|
1566
2430
|
}
|
|
1567
2431
|
},
|
|
1568
2432
|
// The cluster calls `onEvent` when a publication broadcast arrives from
|
|
@@ -1577,22 +2441,21 @@ var CrossTabDataBus = class {
|
|
|
1577
2441
|
if (this.cluster.hasLocalSubscriber(message.topic)) this.dispatch(message);
|
|
1578
2442
|
},
|
|
1579
2443
|
onSuspend: () => {
|
|
1580
|
-
if (!this.stopping) this.trace.event({ type:
|
|
2444
|
+
if (!this.stopping) this.trace.event({ type: TRACE_EVENT_TYPE.LIFECYCLE, action: TRACE_LIFECYCLE_ACTION.SUSPEND });
|
|
1581
2445
|
this.trace.pause();
|
|
1582
|
-
this.
|
|
2446
|
+
this.replayManager.suspend();
|
|
1583
2447
|
this.stopDedupSweep();
|
|
1584
|
-
this.stopReplayRetentionSweep();
|
|
1585
2448
|
this.suspendTransport();
|
|
1586
2449
|
},
|
|
1587
2450
|
onResume: () => {
|
|
1588
|
-
this.trace.event({ type:
|
|
2451
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.LIFECYCLE, action: TRACE_LIFECYCLE_ACTION.RESUME });
|
|
1589
2452
|
this.trace.start();
|
|
1590
2453
|
this.startDedupSweep();
|
|
1591
|
-
this.
|
|
2454
|
+
this.replayManager.start();
|
|
1592
2455
|
this.resumeTransport();
|
|
1593
2456
|
},
|
|
1594
2457
|
onDiagnostic: (event) => {
|
|
1595
|
-
this.trace.event({ type:
|
|
2458
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.RELIABILITY, ...event });
|
|
1596
2459
|
}
|
|
1597
2460
|
}
|
|
1598
2461
|
});
|
|
@@ -1614,27 +2477,24 @@ var CrossTabDataBus = class {
|
|
|
1614
2477
|
this.suspended = false;
|
|
1615
2478
|
this.activeConfig = config;
|
|
1616
2479
|
this.lastError = null;
|
|
1617
|
-
this.
|
|
2480
|
+
this.lastFailure = null;
|
|
2481
|
+
this.persistenceFailureCount = 0;
|
|
2482
|
+
this.persistenceLastFailureAt = null;
|
|
2483
|
+
this.persistenceLastErrorMessage = null;
|
|
2484
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.LIFECYCLE, action: TRACE_LIFECYCLE_ACTION.START });
|
|
1618
2485
|
this.trace.start();
|
|
1619
2486
|
this.startDedupSweep();
|
|
1620
|
-
this.
|
|
1621
|
-
this.updateStatus(
|
|
2487
|
+
this.replayManager.start();
|
|
2488
|
+
this.updateStatus(WORKER_STATUS.CONNECTING);
|
|
1622
2489
|
this.cluster.start();
|
|
1623
2490
|
const opening = this.openTransport(config, this.pendingStop ?? Promise.resolve(), true);
|
|
1624
2491
|
this.startPromise = opening;
|
|
1625
2492
|
for (const topic of this.topicHandlers.keys()) {
|
|
1626
2493
|
this.cluster.subscribe(topic);
|
|
1627
2494
|
}
|
|
1628
|
-
const snapshot = this.cluster.getSnapshot();
|
|
1629
|
-
this.trace.event({
|
|
1630
|
-
type: "coordination",
|
|
1631
|
-
coordinated: snapshot.coordinated,
|
|
1632
|
-
activeWorkers: snapshot.workers.filter((worker) => worker.role === "active").length,
|
|
1633
|
-
workers: snapshot.workers.map(formatWorkerTrace),
|
|
1634
|
-
routes: snapshot.routes.map(formatRouteTrace)
|
|
1635
|
-
});
|
|
1636
2495
|
void opening.then(
|
|
1637
2496
|
() => {
|
|
2497
|
+
this.emitCoordinationTrace();
|
|
1638
2498
|
if (this.startPromise === opening) this.startPromise = null;
|
|
1639
2499
|
},
|
|
1640
2500
|
() => {
|
|
@@ -1661,7 +2521,7 @@ var CrossTabDataBus = class {
|
|
|
1661
2521
|
onError: (error) => this.reportError(error)
|
|
1662
2522
|
})
|
|
1663
2523
|
).then(() => {
|
|
1664
|
-
if (this.status ===
|
|
2524
|
+
if (this.status === WORKER_STATUS.ERROR) {
|
|
1665
2525
|
throw new Error("Transport failed during startup.");
|
|
1666
2526
|
}
|
|
1667
2527
|
if (!this.suspended && !this.stopping) {
|
|
@@ -1675,7 +2535,7 @@ var CrossTabDataBus = class {
|
|
|
1675
2535
|
if (!this.pendingStop) {
|
|
1676
2536
|
this.pendingStop = this.createStopPromise();
|
|
1677
2537
|
}
|
|
1678
|
-
this.updateStatus(
|
|
2538
|
+
this.updateStatus(WORKER_STATUS.ERROR);
|
|
1679
2539
|
this.reportError(error);
|
|
1680
2540
|
this.lastError = error;
|
|
1681
2541
|
this.lastErrorAt = this.now();
|
|
@@ -1720,17 +2580,12 @@ var CrossTabDataBus = class {
|
|
|
1720
2580
|
this.topicHandlers.set(topic, handlers);
|
|
1721
2581
|
if (wasUnused) this.cluster.subscribe(topic);
|
|
1722
2582
|
if (options?.replay) {
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
2583
|
+
this.replayManager.deliverReplay(
|
|
2584
|
+
topic,
|
|
2585
|
+
options.replay,
|
|
2586
|
+
handler,
|
|
2587
|
+
() => Boolean(this.topicHandlers.get(topic)?.has(handler))
|
|
1726
2588
|
);
|
|
1727
|
-
if (this.replayPersistence) {
|
|
1728
|
-
void this.replayHydration.then(() => {
|
|
1729
|
-
if (this.topicHandlers.get(topic)?.has(handler)) this.deliverReplay(topic, limit, handler);
|
|
1730
|
-
});
|
|
1731
|
-
} else {
|
|
1732
|
-
this.deliverReplay(topic, limit, handler);
|
|
1733
|
-
}
|
|
1734
2589
|
}
|
|
1735
2590
|
return () => this.unsubscribe(topic, handler);
|
|
1736
2591
|
}
|
|
@@ -1745,70 +2600,28 @@ var CrossTabDataBus = class {
|
|
|
1745
2600
|
else handlers.clear();
|
|
1746
2601
|
if (handlers.size > 0) return;
|
|
1747
2602
|
this.topicHandlers.delete(topic);
|
|
1748
|
-
this.
|
|
1749
|
-
if (this.replayPersistence?.clearTopic) {
|
|
1750
|
-
void this.withPersistenceRetry("clearTopic", () => this.replayPersistence.clearTopic(topic)).catch((error) => this.reportPersistenceError(error));
|
|
1751
|
-
}
|
|
2603
|
+
this.replayManager.onTopicUnsubscribed(topic);
|
|
1752
2604
|
this.cluster.unsubscribe(topic);
|
|
1753
2605
|
}
|
|
1754
2606
|
/** Clear all in-memory replay buffers and, when supported, durable history. */
|
|
1755
2607
|
async clearReplay() {
|
|
1756
|
-
this.
|
|
1757
|
-
if (this.replayPersistence?.clear) {
|
|
1758
|
-
try {
|
|
1759
|
-
await this.withPersistenceRetry("clear", () => this.replayPersistence.clear());
|
|
1760
|
-
} catch (error) {
|
|
1761
|
-
this.reportPersistenceError(error);
|
|
1762
|
-
throw error;
|
|
1763
|
-
}
|
|
1764
|
-
}
|
|
2608
|
+
await this.replayManager.clearAll();
|
|
1765
2609
|
}
|
|
1766
2610
|
/** Clear replay history for one exact topic, including durable storage. */
|
|
1767
2611
|
async clearReplayTopic(topic) {
|
|
1768
|
-
this.
|
|
1769
|
-
if (this.replayPersistence?.clearTopic) {
|
|
1770
|
-
try {
|
|
1771
|
-
await this.withPersistenceRetry("clearTopic", () => this.replayPersistence.clearTopic(topic));
|
|
1772
|
-
} catch (error) {
|
|
1773
|
-
this.reportPersistenceError(error);
|
|
1774
|
-
throw error;
|
|
1775
|
-
}
|
|
1776
|
-
}
|
|
2612
|
+
await this.replayManager.clearTopic(topic);
|
|
1777
2613
|
}
|
|
1778
2614
|
/** Remove replay entries older than an epoch-millisecond cutoff. */
|
|
1779
2615
|
async clearReplayBefore(timestamp) {
|
|
1780
|
-
|
|
1781
|
-
if (this.replayBuffers) {
|
|
1782
|
-
for (const [topic, messages] of this.replayBuffers) {
|
|
1783
|
-
const kept = messages.filter((message) => message.timestamp === void 0 || message.timestamp >= timestamp);
|
|
1784
|
-
if (kept.length) this.replayBuffers.set(topic, kept);
|
|
1785
|
-
else this.replayBuffers.delete(topic);
|
|
1786
|
-
}
|
|
1787
|
-
}
|
|
1788
|
-
if (this.replayPersistence?.clearBefore) {
|
|
1789
|
-
try {
|
|
1790
|
-
await this.withPersistenceRetry("clearBefore", () => this.replayPersistence.clearBefore(timestamp));
|
|
1791
|
-
} catch (error) {
|
|
1792
|
-
this.reportPersistenceError(error);
|
|
1793
|
-
throw error;
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
2616
|
+
await this.replayManager.clearBefore(timestamp);
|
|
1796
2617
|
}
|
|
1797
2618
|
/** Return bounded deduplication counters for diagnostics and health checks. */
|
|
1798
2619
|
getDedupStats() {
|
|
1799
|
-
return
|
|
1800
|
-
enabled: this.dedupEnabled,
|
|
1801
|
-
tracked: this.seenMessageIds.size,
|
|
1802
|
-
suppressed: this.dedupSuppressed,
|
|
1803
|
-
accepted: this.dedupAccepted,
|
|
1804
|
-
...this.dedupAdaptiveBounds ? { ttlMs: this.currentDedupTtl() } : {}
|
|
1805
|
-
};
|
|
2620
|
+
return this.dedupManager.getStats();
|
|
1806
2621
|
}
|
|
1807
2622
|
/** Drop all remembered IDs and reset dedup counters. */
|
|
1808
2623
|
resetDedup() {
|
|
1809
|
-
this.
|
|
1810
|
-
this.dedupSuppressed = 0;
|
|
1811
|
-
this.dedupAccepted = 0;
|
|
2624
|
+
this.dedupManager.reset();
|
|
1812
2625
|
}
|
|
1813
2626
|
/** Publish a message to `topic`. The owning Worker delivers it to the transport. */
|
|
1814
2627
|
publish(topic, data, options) {
|
|
@@ -1836,8 +2649,7 @@ var CrossTabDataBus = class {
|
|
|
1836
2649
|
}
|
|
1837
2650
|
const mapped = items.map((item) => ({
|
|
1838
2651
|
data: item.data,
|
|
1839
|
-
...item.options?.messageId
|
|
1840
|
-
...item.options?.timestamp !== void 0 ? { timestamp: item.options.timestamp } : {}
|
|
2652
|
+
...publicationMetadata(item.options?.messageId, item.options?.timestamp)
|
|
1841
2653
|
}));
|
|
1842
2654
|
if (!this.cluster.publishBatch(topic, mapped)) {
|
|
1843
2655
|
this.reportError(
|
|
@@ -1882,6 +2694,41 @@ var CrossTabDataBus = class {
|
|
|
1882
2694
|
lastSuccessAt: this.lastSuccessAt
|
|
1883
2695
|
};
|
|
1884
2696
|
}
|
|
2697
|
+
/** Bounded failure counters for the replay persistence backend. */
|
|
2698
|
+
getPersistenceStats() {
|
|
2699
|
+
return {
|
|
2700
|
+
failures: this.persistenceFailureCount,
|
|
2701
|
+
lastFailureAt: this.persistenceLastFailureAt,
|
|
2702
|
+
lastErrorMessage: this.persistenceLastErrorMessage
|
|
2703
|
+
};
|
|
2704
|
+
}
|
|
2705
|
+
/** Compact health verdict for dashboards, readiness probes, and support
|
|
2706
|
+
* bundles. Answers "is the bus usable right now" first, then attaches the
|
|
2707
|
+
* unified failure ledger and recovery context that explains the verdict. */
|
|
2708
|
+
getHealthSummary() {
|
|
2709
|
+
const transport = this.transport;
|
|
2710
|
+
const transportDown = !this.transportReady || this.status === WORKER_STATUS.ERROR || this.status === WORKER_STATUS.DISCONNECTED;
|
|
2711
|
+
const state = !this.started ? HEALTH_STATE.STOPPED : this.suspended ? HEALTH_STATE.SUSPENDED : transportDown ? this.recoveryExhausted ? HEALTH_STATE.DEGRADED : this.status === WORKER_STATUS.CONNECTING && this.recoveryAttempt === 0 ? HEALTH_STATE.STARTING : HEALTH_STATE.RECOVERING : HEALTH_STATE.HEALTHY;
|
|
2712
|
+
return {
|
|
2713
|
+
healthy: state === HEALTH_STATE.HEALTHY,
|
|
2714
|
+
state,
|
|
2715
|
+
status: this.status,
|
|
2716
|
+
sdkVersion: SDK_VERSION,
|
|
2717
|
+
started: this.started,
|
|
2718
|
+
suspended: this.suspended,
|
|
2719
|
+
transport: {
|
|
2720
|
+
name: transport.diagnosticsName ?? transport.constructor.name,
|
|
2721
|
+
backend: transport.diagnosticsBackend ?? null,
|
|
2722
|
+
ready: this.transportReady,
|
|
2723
|
+
status: this.status
|
|
2724
|
+
},
|
|
2725
|
+
recovery: this.getRecoveryStats(),
|
|
2726
|
+
lastFailure: this.lastFailure,
|
|
2727
|
+
persistence: this.getPersistenceStats(),
|
|
2728
|
+
metrics: this.trace.getMetrics(),
|
|
2729
|
+
trace: this.trace.getSinkState()
|
|
2730
|
+
};
|
|
2731
|
+
}
|
|
1885
2732
|
/** Snapshot of the cluster state (workers, routes, assignments).
|
|
1886
2733
|
* For diagnostics only — the returned object is a shallow copy but
|
|
1887
2734
|
* nested arrays are snapshots at call time. */
|
|
@@ -1890,8 +2737,7 @@ var CrossTabDataBus = class {
|
|
|
1890
2737
|
}
|
|
1891
2738
|
/** Return a single health snapshot combining lifecycle, recovery, dedup, replay, and cluster state. */
|
|
1892
2739
|
getDiagnostics() {
|
|
1893
|
-
|
|
1894
|
-
if (this.replayBuffers) for (const buffer of this.replayBuffers.values()) messages += buffer.length;
|
|
2740
|
+
const replay = this.replayManager.getStats();
|
|
1895
2741
|
const cluster = this.cluster.getSnapshot();
|
|
1896
2742
|
const unknownMessages = this.cluster.getUnknownMessageStats();
|
|
1897
2743
|
const transport = this.transport;
|
|
@@ -1902,12 +2748,26 @@ var CrossTabDataBus = class {
|
|
|
1902
2748
|
transportReady: this.transportReady,
|
|
1903
2749
|
recovery: this.getRecoveryStats(),
|
|
1904
2750
|
dedup: this.getDedupStats(),
|
|
1905
|
-
replay: { enabled:
|
|
2751
|
+
replay: { enabled: replay.enabled, topics: replay.topics, messages: replay.messages, bytes: replay.bytes },
|
|
2752
|
+
persistence: this.getPersistenceStats(),
|
|
1906
2753
|
protocol: { version: cluster.protocolVersion, unknownMessages: unknownMessages.count, lastUnknownMessageType: unknownMessages.lastType, peers: cluster.peerProtocolVersions },
|
|
1907
|
-
transport: {
|
|
1908
|
-
|
|
2754
|
+
transport: {
|
|
2755
|
+
name: transport.diagnosticsName ?? transport.constructor.name,
|
|
2756
|
+
backend: transport.diagnosticsBackend ?? null,
|
|
2757
|
+
status: this.status,
|
|
2758
|
+
suspended: this.suspended
|
|
2759
|
+
},
|
|
2760
|
+
cluster,
|
|
2761
|
+
metrics: this.trace.getMetrics(),
|
|
2762
|
+
trace: this.trace.getSinkState()
|
|
1909
2763
|
};
|
|
1910
2764
|
}
|
|
2765
|
+
/** Synchronous snapshot of the current trace metrics window (throughput,
|
|
2766
|
+
* dispatch latency, dedup outcomes), without flushing or resetting it.
|
|
2767
|
+
* Returns null when trace metrics are inactive (disabled or events-only). */
|
|
2768
|
+
getMetrics() {
|
|
2769
|
+
return this.trace.getMetrics();
|
|
2770
|
+
}
|
|
1911
2771
|
/**
|
|
1912
2772
|
* Gracefully stop the DataBus: unsubscribe all topics, stop the cluster,
|
|
1913
2773
|
* and close the transport. Idempotent.
|
|
@@ -1915,13 +2775,12 @@ var CrossTabDataBus = class {
|
|
|
1915
2775
|
async stop() {
|
|
1916
2776
|
if (!this.started) return;
|
|
1917
2777
|
this.stopping = true;
|
|
1918
|
-
this.
|
|
1919
|
-
this.trace.event({ type:
|
|
2778
|
+
this.replayManager.suspend();
|
|
2779
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.LIFECYCLE, action: TRACE_LIFECYCLE_ACTION.STOP });
|
|
1920
2780
|
this.trace.stop();
|
|
1921
2781
|
this.stopDedupSweep();
|
|
1922
|
-
this.stopReplayRetentionSweep();
|
|
1923
2782
|
this.topicHandlers.clear();
|
|
1924
|
-
this.
|
|
2783
|
+
this.replayManager.resetBuffers();
|
|
1925
2784
|
this.cluster.stop();
|
|
1926
2785
|
try {
|
|
1927
2786
|
await this.startPromise?.catch(() => void 0);
|
|
@@ -1942,7 +2801,7 @@ var CrossTabDataBus = class {
|
|
|
1942
2801
|
this.activeConfig = void 0;
|
|
1943
2802
|
this.recoveryAttempt = 0;
|
|
1944
2803
|
this.recoveryExhausted = false;
|
|
1945
|
-
this.updateStatus(
|
|
2804
|
+
this.updateStatus(WORKER_STATUS.DISCONNECTED);
|
|
1946
2805
|
}
|
|
1947
2806
|
}
|
|
1948
2807
|
/**
|
|
@@ -1951,7 +2810,7 @@ var CrossTabDataBus = class {
|
|
|
1951
2810
|
* and dispatches locally.
|
|
1952
2811
|
*/
|
|
1953
2812
|
handleTransportMessage(message) {
|
|
1954
|
-
if (this.isDuplicate(message)) return;
|
|
2813
|
+
if (this.dedupManager.isDuplicate(message.messageId ?? "", message.topic)) return;
|
|
1955
2814
|
this.trace.recordReceived(message.topic);
|
|
1956
2815
|
if (!this.cluster.isAssigned(message.topic)) {
|
|
1957
2816
|
this.trace.recordDiscarded(message.topic);
|
|
@@ -1965,55 +2824,13 @@ var CrossTabDataBus = class {
|
|
|
1965
2824
|
}
|
|
1966
2825
|
this.trace.recordDiscarded(message.topic);
|
|
1967
2826
|
}
|
|
1968
|
-
|
|
1969
|
-
if (!this.dedupEnabled || !message.messageId) return false;
|
|
1970
|
-
const now = this.now();
|
|
1971
|
-
for (const [id, timestamp] of this.seenMessageIds) {
|
|
1972
|
-
if (now - timestamp > this.dedupTtlMs) this.seenMessageIds.delete(id);
|
|
1973
|
-
}
|
|
1974
|
-
if (this.seenMessageIds.has(message.messageId)) {
|
|
1975
|
-
this.trace.event({ type: "reliability", operation: "dedup_suppressed", topic: message.topic });
|
|
1976
|
-
this.dedupSuppressed += 1;
|
|
1977
|
-
this.trace.recordDedupSuppressed();
|
|
1978
|
-
return true;
|
|
1979
|
-
}
|
|
1980
|
-
this.seenMessageIds.set(message.messageId, now);
|
|
1981
|
-
this.dedupAccepted += 1;
|
|
1982
|
-
this.dedupWindowAccepted += 1;
|
|
1983
|
-
this.trace.recordDedupAccepted();
|
|
1984
|
-
while (this.seenMessageIds.size > this.dedupMaxEntries) {
|
|
1985
|
-
const oldest = this.seenMessageIds.keys().next().value;
|
|
1986
|
-
if (oldest === void 0) break;
|
|
1987
|
-
this.seenMessageIds.delete(oldest);
|
|
1988
|
-
}
|
|
1989
|
-
return false;
|
|
1990
|
-
}
|
|
2827
|
+
/** Start enqueuing the dedup expiry sweep (delegated to {@link DedupManager}). */
|
|
1991
2828
|
startDedupSweep() {
|
|
1992
|
-
|
|
1993
|
-
this.dedupSweepTimer = setInterval(() => this.pruneExpiredDedup(), this.dedupSweepMs);
|
|
2829
|
+
this.dedupManager.start();
|
|
1994
2830
|
}
|
|
2831
|
+
/** Stop enqueuing the dedup expiry sweep. */
|
|
1995
2832
|
stopDedupSweep() {
|
|
1996
|
-
|
|
1997
|
-
this.dedupSweepTimer = null;
|
|
1998
|
-
}
|
|
1999
|
-
pruneExpiredDedup() {
|
|
2000
|
-
const cutoff = this.now() - this.currentDedupTtl();
|
|
2001
|
-
for (const [id, timestamp] of this.seenMessageIds) {
|
|
2002
|
-
if (timestamp < cutoff) this.seenMessageIds.delete(id);
|
|
2003
|
-
}
|
|
2004
|
-
}
|
|
2005
|
-
currentDedupTtl() {
|
|
2006
|
-
if (!this.dedupAdaptiveBounds) return this.dedupTtlMs;
|
|
2007
|
-
const now = this.now();
|
|
2008
|
-
const elapsed = now - this.dedupWindowStartedAt;
|
|
2009
|
-
if (elapsed >= this.dedupAdaptiveWindowMs) {
|
|
2010
|
-
this.dedupWindowStartedAt = now;
|
|
2011
|
-
this.dedupWindowAccepted = 0;
|
|
2012
|
-
return this.dedupAdaptiveBounds.maxMs;
|
|
2013
|
-
}
|
|
2014
|
-
const rate = this.dedupWindowAccepted / Math.max(1, elapsed);
|
|
2015
|
-
const factor = Math.min(1, rate / 0.01);
|
|
2016
|
-
return this.dedupAdaptiveBounds.maxMs - (this.dedupAdaptiveBounds.maxMs - this.dedupAdaptiveBounds.minMs) * factor;
|
|
2833
|
+
this.dedupManager.stop();
|
|
2017
2834
|
}
|
|
2018
2835
|
/** Deliver a message to every local handler registered for its topic,
|
|
2019
2836
|
* plus every handler registered with a wildcard subscription that matches
|
|
@@ -2026,137 +2843,7 @@ var CrossTabDataBus = class {
|
|
|
2026
2843
|
this.invokeHandlers(handlers, (handler) => handler(message));
|
|
2027
2844
|
}
|
|
2028
2845
|
}
|
|
2029
|
-
this.
|
|
2030
|
-
}
|
|
2031
|
-
/** Append a dispatched publication to the topic's replay ring buffer.
|
|
2032
|
-
* No-op when replay is disabled. */
|
|
2033
|
-
recordReplay(message) {
|
|
2034
|
-
if (!this.replayBuffers) return;
|
|
2035
|
-
let buffer = this.replayBuffers.get(message.topic);
|
|
2036
|
-
if (!buffer) {
|
|
2037
|
-
buffer = [];
|
|
2038
|
-
this.replayBuffers.set(message.topic, buffer);
|
|
2039
|
-
}
|
|
2040
|
-
const storedMessage = message;
|
|
2041
|
-
buffer.push(storedMessage);
|
|
2042
|
-
if (this.replayPruneStrategy !== "age") {
|
|
2043
|
-
while (buffer.length > this.replayMaxPerTopic) buffer.shift();
|
|
2044
|
-
}
|
|
2045
|
-
if (this.replayPruneStrategy !== "count" && this.replayRetentionMs !== void 0) {
|
|
2046
|
-
const cutoff = this.now() - this.replayRetentionMs;
|
|
2047
|
-
while (buffer.length > 0) {
|
|
2048
|
-
const first = buffer[0];
|
|
2049
|
-
if (!first || first.timestamp === void 0 || first.timestamp >= cutoff) break;
|
|
2050
|
-
buffer.shift();
|
|
2051
|
-
}
|
|
2052
|
-
}
|
|
2053
|
-
if (this.replayPersistence) {
|
|
2054
|
-
void this.withPersistenceRetry("append", () => this.replayPersistence.append(storedMessage)).catch((error) => this.reportPersistenceError(error));
|
|
2055
|
-
if (this.replayRetentionMs !== void 0 && this.replayPersistence.clearBefore) {
|
|
2056
|
-
this.scheduleReplayRetentionCleanup(this.now() - this.replayRetentionMs);
|
|
2057
|
-
}
|
|
2058
|
-
}
|
|
2059
|
-
}
|
|
2060
|
-
async hydrateReplay() {
|
|
2061
|
-
if (!this.replayBuffers || !this.replayPersistence) {
|
|
2062
|
-
return;
|
|
2063
|
-
}
|
|
2064
|
-
try {
|
|
2065
|
-
if (this.replayRetentionMs !== void 0 && this.replayPersistence.clearBefore) {
|
|
2066
|
-
await this.withPersistenceRetry("clearBefore", () => this.replayPersistence.clearBefore(this.now() - this.replayRetentionMs));
|
|
2067
|
-
}
|
|
2068
|
-
for (const message of await this.withPersistenceRetry("load", () => this.replayPersistence.load())) {
|
|
2069
|
-
let buffer = this.replayBuffers.get(message.topic);
|
|
2070
|
-
if (!buffer) {
|
|
2071
|
-
buffer = [];
|
|
2072
|
-
this.replayBuffers.set(message.topic, buffer);
|
|
2073
|
-
}
|
|
2074
|
-
buffer.push(message);
|
|
2075
|
-
if (buffer.length > this.replayMaxPerTopic) buffer.shift();
|
|
2076
|
-
}
|
|
2077
|
-
} catch (error) {
|
|
2078
|
-
this.reportPersistenceError(error);
|
|
2079
|
-
}
|
|
2080
|
-
}
|
|
2081
|
-
scheduleReplayRetentionCleanup(cutoff) {
|
|
2082
|
-
if (!this.replayPersistence?.clearBefore) return;
|
|
2083
|
-
if (this.replayRetentionCutoff === null || cutoff > this.replayRetentionCutoff) {
|
|
2084
|
-
this.replayRetentionCutoff = cutoff;
|
|
2085
|
-
}
|
|
2086
|
-
if (this.replayRetentionCleanup) return;
|
|
2087
|
-
this.replayRetentionCleanup = (async () => {
|
|
2088
|
-
while (this.replayRetentionCutoff !== null) {
|
|
2089
|
-
const nextCutoff = this.replayRetentionCutoff;
|
|
2090
|
-
this.replayRetentionCutoff = null;
|
|
2091
|
-
try {
|
|
2092
|
-
await this.replayPersistence.clearBefore(nextCutoff);
|
|
2093
|
-
} catch (error) {
|
|
2094
|
-
this.reportPersistenceError(error);
|
|
2095
|
-
}
|
|
2096
|
-
}
|
|
2097
|
-
})().finally(() => {
|
|
2098
|
-
this.replayRetentionCleanup = null;
|
|
2099
|
-
if (this.replayRetentionCutoff !== null) {
|
|
2100
|
-
this.scheduleReplayRetentionCleanup(this.replayRetentionCutoff);
|
|
2101
|
-
}
|
|
2102
|
-
});
|
|
2103
|
-
}
|
|
2104
|
-
startReplayRetentionSweep() {
|
|
2105
|
-
if (this.replayRetentionTimer || !this.replayRetentionMs || !this.replayRetentionSweepMs || !this.replayPersistence?.clearBefore) return;
|
|
2106
|
-
this.replayRetentionTimer = setInterval(() => {
|
|
2107
|
-
this.scheduleReplayRetentionCleanup(this.now() - this.replayRetentionMs);
|
|
2108
|
-
}, this.replayRetentionSweepMs);
|
|
2109
|
-
}
|
|
2110
|
-
stopReplayRetentionSweep() {
|
|
2111
|
-
if (this.replayRetentionTimer) clearInterval(this.replayRetentionTimer);
|
|
2112
|
-
this.replayRetentionTimer = null;
|
|
2113
|
-
}
|
|
2114
|
-
async withPersistenceRetry(persistenceOperation, operation) {
|
|
2115
|
-
const generation = this.persistenceRetryGeneration;
|
|
2116
|
-
let attempt = 0;
|
|
2117
|
-
let delay = this.persistenceRetryBackoffMs;
|
|
2118
|
-
while (true) {
|
|
2119
|
-
attempt += 1;
|
|
2120
|
-
try {
|
|
2121
|
-
if (generation !== this.persistenceRetryGeneration) throw new PersistenceRetryCancelledError();
|
|
2122
|
-
return await operation();
|
|
2123
|
-
} catch (error) {
|
|
2124
|
-
if (error instanceof PersistenceRetryCancelledError || generation !== this.persistenceRetryGeneration) {
|
|
2125
|
-
throw new PersistenceRetryCancelledError();
|
|
2126
|
-
}
|
|
2127
|
-
if (attempt >= this.persistenceRetryMaxAttempts) throw error;
|
|
2128
|
-
this.trace.event({
|
|
2129
|
-
type: "reliability",
|
|
2130
|
-
operation: "persistence_retry",
|
|
2131
|
-
persistenceOperation,
|
|
2132
|
-
attempt
|
|
2133
|
-
});
|
|
2134
|
-
if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay));
|
|
2135
|
-
if (generation !== this.persistenceRetryGeneration) throw new PersistenceRetryCancelledError();
|
|
2136
|
-
delay = Math.min(delay * 2, 1600);
|
|
2137
|
-
}
|
|
2138
|
-
}
|
|
2139
|
-
}
|
|
2140
|
-
/** Deliver buffered history to a newly-registered handler. For an exact
|
|
2141
|
-
* topic this is that topic's ring; for a wildcard subscription every
|
|
2142
|
-
* buffered topic matching the pattern contributes (in buffer insertion
|
|
2143
|
-
* order). Replay deliveries are marked `replayed: true` and are not
|
|
2144
|
-
* counted into trace metrics. */
|
|
2145
|
-
deliverReplay(topic, limit, handler) {
|
|
2146
|
-
if (!this.replayBuffers || limit <= 0) return;
|
|
2147
|
-
const deliver = (buffer2) => {
|
|
2148
|
-
for (const message of buffer2.slice(-limit)) {
|
|
2149
|
-
this.invokeHandlers([handler], (h) => h({ ...message, replayed: true }));
|
|
2150
|
-
}
|
|
2151
|
-
};
|
|
2152
|
-
if (isWildcardTopic(topic)) {
|
|
2153
|
-
for (const [bufferedTopic, buffer2] of this.replayBuffers) {
|
|
2154
|
-
if (topicMatchesPattern(topic, bufferedTopic)) deliver(buffer2);
|
|
2155
|
-
}
|
|
2156
|
-
return;
|
|
2157
|
-
}
|
|
2158
|
-
const buffer = this.replayBuffers.get(topic);
|
|
2159
|
-
if (buffer) deliver(buffer);
|
|
2846
|
+
this.replayManager.record(message);
|
|
2160
2847
|
}
|
|
2161
2848
|
/**
|
|
2162
2849
|
* Propagate a status change to the cluster, trace, and all registered
|
|
@@ -2165,13 +2852,13 @@ var CrossTabDataBus = class {
|
|
|
2165
2852
|
updateStatus(status) {
|
|
2166
2853
|
const previousStatus = this.status;
|
|
2167
2854
|
this.status = status;
|
|
2168
|
-
if (previousStatus !== status) this.trace.event({ type:
|
|
2855
|
+
if (previousStatus !== status) this.trace.event({ type: TRACE_EVENT_TYPE.STATUS, status });
|
|
2169
2856
|
this.cluster.setStatus(status);
|
|
2170
|
-
if (status ===
|
|
2171
|
-
if (status ===
|
|
2857
|
+
if (status === WORKER_STATUS.DISCONNECTED || status === WORKER_STATUS.ERROR) this.transportSubscribedTopics.clear();
|
|
2858
|
+
if (status === WORKER_STATUS.CONNECTED && previousStatus !== WORKER_STATUS.CONNECTED) {
|
|
2172
2859
|
for (const topic of this.cluster.getSnapshot().assignedTopics) this.subscribeTransport(topic);
|
|
2173
2860
|
}
|
|
2174
|
-
if (status ===
|
|
2861
|
+
if (status === WORKER_STATUS.ERROR && this.started && !this.stopping) {
|
|
2175
2862
|
const now = this.now();
|
|
2176
2863
|
if (now - this.lastRecoveryAt >= this.recoveryCooldownMs) {
|
|
2177
2864
|
this.lastRecoveryAt = now;
|
|
@@ -2179,37 +2866,67 @@ var CrossTabDataBus = class {
|
|
|
2179
2866
|
if (attempt > this.recoveryMaxAttempts) {
|
|
2180
2867
|
if (!this.recoveryExhausted) {
|
|
2181
2868
|
this.recoveryExhausted = true;
|
|
2182
|
-
this.trace.event({ type:
|
|
2869
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.RELIABILITY, operation: RELIABILITY_OPERATION.TRANSPORT_RECOVERY, attempt: this.recoveryMaxAttempts, outcome: RECOVERY_OUTCOME.EXHAUSTED });
|
|
2183
2870
|
}
|
|
2184
2871
|
return;
|
|
2185
2872
|
}
|
|
2186
|
-
this.trace.event({ type:
|
|
2873
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.RELIABILITY, operation: RELIABILITY_OPERATION.TRANSPORT_RECOVERY, attempt, outcome: RECOVERY_OUTCOME.SCHEDULED });
|
|
2187
2874
|
setTimeout(() => {
|
|
2188
2875
|
if (this.stopping || !this.started || this.suspended) return;
|
|
2189
|
-
if (this.status !==
|
|
2876
|
+
if (this.status !== WORKER_STATUS.ERROR) return;
|
|
2190
2877
|
void this.reopenTransport(attempt);
|
|
2191
2878
|
}, this.recoveryCooldownMs);
|
|
2192
2879
|
}
|
|
2193
2880
|
}
|
|
2194
2881
|
this.invokeHandlers(this.statusHandlers, (handler) => handler(status));
|
|
2195
2882
|
}
|
|
2196
|
-
reportError(error) {
|
|
2197
|
-
this.
|
|
2198
|
-
|
|
2883
|
+
reportError(error, source = FAILURE_SOURCE.TRANSPORT) {
|
|
2884
|
+
this.lastFailure = {
|
|
2885
|
+
source,
|
|
2886
|
+
message: error instanceof Error ? error.message : String(error),
|
|
2887
|
+
at: this.now()
|
|
2888
|
+
};
|
|
2889
|
+
if (source === FAILURE_SOURCE.PERSISTENCE) {
|
|
2890
|
+
this.persistenceFailureCount += 1;
|
|
2891
|
+
this.persistenceLastFailureAt = this.lastFailure.at;
|
|
2892
|
+
this.persistenceLastErrorMessage = this.lastFailure.message;
|
|
2893
|
+
}
|
|
2894
|
+
this.trace.event({
|
|
2895
|
+
type: TRACE_EVENT_TYPE.ERROR,
|
|
2896
|
+
source: source === FAILURE_SOURCE.TRANSPORT ? TRACE_ERROR_SOURCE.TRANSPORT : TRACE_ERROR_SOURCE.OPERATION
|
|
2897
|
+
});
|
|
2898
|
+
this.invokeHandlers(this.errorHandlers, (handler) => handler(error), INVOKE_LABEL.ERROR_HANDLER);
|
|
2199
2899
|
}
|
|
2900
|
+
/** Report a persistence failure to the trace and the unified failure ledger,
|
|
2901
|
+
* unless it is a {@link PersistenceRetryCancelledError} cancellation from a
|
|
2902
|
+
* lifecycle transition (teardown should stay quiet). */
|
|
2200
2903
|
reportPersistenceError(error) {
|
|
2201
2904
|
if (error instanceof PersistenceRetryCancelledError) return;
|
|
2202
|
-
this.trace.event({ type:
|
|
2203
|
-
this.reportError(error);
|
|
2905
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.RELIABILITY, operation: RELIABILITY_OPERATION.PERSISTENCE_CLEANUP });
|
|
2906
|
+
this.reportError(error, FAILURE_SOURCE.PERSISTENCE);
|
|
2204
2907
|
}
|
|
2205
2908
|
traceSubscription(action, topic) {
|
|
2206
2909
|
this.trace.event({
|
|
2207
|
-
type:
|
|
2910
|
+
type: TRACE_EVENT_TYPE.SUBSCRIPTION,
|
|
2208
2911
|
action,
|
|
2209
2912
|
topic,
|
|
2210
2913
|
activeTopics: this.transportSubscribedTopics.size
|
|
2211
2914
|
});
|
|
2212
2915
|
}
|
|
2916
|
+
/** Emit the coordination trace snapshot from the current cluster state.
|
|
2917
|
+
* Called after a transport opens (start and recovery), when the role and
|
|
2918
|
+
* route picture has settled — the synchronous pre-open snapshot would see no
|
|
2919
|
+
* routes because their writes are still coalesced in the batch writer. */
|
|
2920
|
+
emitCoordinationTrace() {
|
|
2921
|
+
const snapshot = this.cluster.getSnapshot();
|
|
2922
|
+
this.trace.event({
|
|
2923
|
+
type: TRACE_EVENT_TYPE.COORDINATION,
|
|
2924
|
+
coordinated: snapshot.coordinated,
|
|
2925
|
+
activeWorkers: snapshot.workers.filter((worker) => worker.role === WORKER_ROLE.ACTIVE).length,
|
|
2926
|
+
workers: snapshot.workers.map(formatWorkerTrace),
|
|
2927
|
+
routes: snapshot.routes.map(formatRouteTrace)
|
|
2928
|
+
});
|
|
2929
|
+
}
|
|
2213
2930
|
/** Ask the transport to subscribe to a topic (idempotent). */
|
|
2214
2931
|
subscribeTransport(topic) {
|
|
2215
2932
|
if (this.transportSubscribedTopics.has(topic)) return false;
|
|
@@ -2227,17 +2944,17 @@ var CrossTabDataBus = class {
|
|
|
2227
2944
|
* are routed to `reportError` (which surfaces them to error subscribers);
|
|
2228
2945
|
* error-handler failures are logged to the console to avoid infinite
|
|
2229
2946
|
* recursion through reportError itself. */
|
|
2230
|
-
invokeHandlers(handlers, callback, label =
|
|
2947
|
+
invokeHandlers(handlers, callback, label = INVOKE_LABEL.DISPATCH) {
|
|
2231
2948
|
for (const handler of handlers) {
|
|
2232
2949
|
try {
|
|
2233
2950
|
callback(handler);
|
|
2234
2951
|
} catch (error) {
|
|
2235
|
-
if (label ===
|
|
2952
|
+
if (label === INVOKE_LABEL.ERROR_HANDLER) {
|
|
2236
2953
|
if (typeof console !== "undefined" && typeof console.warn === "function") {
|
|
2237
|
-
console.warn(
|
|
2954
|
+
console.warn(`[${DEFAULT_STORAGE_PREFIX}] error handler threw:`, error);
|
|
2238
2955
|
}
|
|
2239
2956
|
} else {
|
|
2240
|
-
this.reportError(error);
|
|
2957
|
+
this.reportError(error, FAILURE_SOURCE.DISPATCH);
|
|
2241
2958
|
}
|
|
2242
2959
|
}
|
|
2243
2960
|
}
|
|
@@ -2251,7 +2968,7 @@ var CrossTabDataBus = class {
|
|
|
2251
2968
|
this.suspended = true;
|
|
2252
2969
|
this.transportReady = false;
|
|
2253
2970
|
this.transportSubscribedTopics.clear();
|
|
2254
|
-
this.updateStatus(
|
|
2971
|
+
this.updateStatus(WORKER_STATUS.DISCONNECTED);
|
|
2255
2972
|
if (this.pendingStop) return;
|
|
2256
2973
|
const pending = this.startPromise ?? Promise.resolve();
|
|
2257
2974
|
const stopping = pending.catch(() => void 0).then(() => this.transport.stop()).catch((error) => this.reportError(error));
|
|
@@ -2285,14 +3002,14 @@ var CrossTabDataBus = class {
|
|
|
2285
3002
|
const traceAttempt = recoveryAttempt ?? (this.recoveryAttempt > 0 ? this.recoveryAttempt : void 0);
|
|
2286
3003
|
this.started = true;
|
|
2287
3004
|
this.suspended = false;
|
|
2288
|
-
this.updateStatus(
|
|
3005
|
+
this.updateStatus(WORKER_STATUS.CONNECTING);
|
|
2289
3006
|
const pending = this.startPromise ?? this.pendingStop ?? Promise.resolve();
|
|
2290
3007
|
const opening = pending.catch(() => void 0).then(() => this.openTransport(config, Promise.resolve(), false));
|
|
2291
3008
|
this.startPromise = opening;
|
|
2292
3009
|
void opening.then(
|
|
2293
3010
|
() => {
|
|
2294
3011
|
if (traceAttempt !== void 0) {
|
|
2295
|
-
this.trace.event({ type:
|
|
3012
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.RELIABILITY, operation: RELIABILITY_OPERATION.TRANSPORT_RECOVERY, attempt: traceAttempt, outcome: RECOVERY_OUTCOME.SUCCEEDED });
|
|
2296
3013
|
this.recoveryAttempt = 0;
|
|
2297
3014
|
this.recoveryExhausted = false;
|
|
2298
3015
|
}
|
|
@@ -2300,7 +3017,7 @@ var CrossTabDataBus = class {
|
|
|
2300
3017
|
},
|
|
2301
3018
|
() => {
|
|
2302
3019
|
if (traceAttempt !== void 0) {
|
|
2303
|
-
this.trace.event({ type:
|
|
3020
|
+
this.trace.event({ type: TRACE_EVENT_TYPE.RELIABILITY, operation: RELIABILITY_OPERATION.TRANSPORT_RECOVERY, attempt: traceAttempt, outcome: RECOVERY_OUTCOME.FAILED });
|
|
2304
3021
|
}
|
|
2305
3022
|
}
|
|
2306
3023
|
);
|
|
@@ -2379,50 +3096,102 @@ function parseDataBusPublication(value, fallbackTopic) {
|
|
|
2379
3096
|
};
|
|
2380
3097
|
}
|
|
2381
3098
|
|
|
3099
|
+
// src/utils/error-utils.ts
|
|
3100
|
+
function serializeError(error) {
|
|
3101
|
+
if (error instanceof Error) {
|
|
3102
|
+
return {
|
|
3103
|
+
name: error.name,
|
|
3104
|
+
message: error.message,
|
|
3105
|
+
...error.stack ? { stack: error.stack } : {}
|
|
3106
|
+
};
|
|
3107
|
+
}
|
|
3108
|
+
const context = error === void 0 ? void 0 : error;
|
|
3109
|
+
return {
|
|
3110
|
+
name: "CentrifugeError",
|
|
3111
|
+
message: typeof error === "string" ? error : "Centrifuge worker operation failed.",
|
|
3112
|
+
...context === void 0 || !isStructuredCloneable(context) ? {} : { context }
|
|
3113
|
+
};
|
|
3114
|
+
}
|
|
3115
|
+
function isStructuredCloneable(value) {
|
|
3116
|
+
if (typeof structuredClone !== "function") return true;
|
|
3117
|
+
try {
|
|
3118
|
+
structuredClone(value);
|
|
3119
|
+
return true;
|
|
3120
|
+
} catch {
|
|
3121
|
+
return false;
|
|
3122
|
+
}
|
|
3123
|
+
}
|
|
3124
|
+
function deserializeWorkerError(error) {
|
|
3125
|
+
const result = new Error(error.message);
|
|
3126
|
+
result.name = error.name;
|
|
3127
|
+
if (error.stack) result.stack = error.stack;
|
|
3128
|
+
if (error.context !== void 0) Object.assign(result, { context: error.context });
|
|
3129
|
+
return result;
|
|
3130
|
+
}
|
|
3131
|
+
|
|
2382
3132
|
// src/centrifuge-session.ts
|
|
2383
3133
|
var CentrifugeSession = class {
|
|
2384
3134
|
constructor(sink) {
|
|
2385
3135
|
this.sink = sink;
|
|
2386
3136
|
}
|
|
3137
|
+
sink;
|
|
2387
3138
|
client = null;
|
|
2388
3139
|
subscriptions = /* @__PURE__ */ new Map();
|
|
2389
3140
|
transferable = false;
|
|
3141
|
+
tokenBridge = false;
|
|
3142
|
+
nextRequestId = 1;
|
|
3143
|
+
pendingTokenRequests = /* @__PURE__ */ new Map();
|
|
2390
3144
|
/** Dispatch an incoming Worker message to the matching operation.
|
|
2391
3145
|
* Unknown message types are ignored rather than thrown, so a future protocol
|
|
2392
3146
|
* extension adding a new variant cannot crash an older session. */
|
|
2393
3147
|
handle(message) {
|
|
2394
3148
|
switch (message.type) {
|
|
2395
|
-
case
|
|
2396
|
-
this.initialize(message.url, message.config, message.transferable === true);
|
|
3149
|
+
case CENTRIFUGE_INPUT_TYPE.INIT:
|
|
3150
|
+
this.initialize(message.url, message.config, message.transferable === true, message.tokenBridge === true);
|
|
2397
3151
|
return;
|
|
2398
|
-
case
|
|
3152
|
+
case CENTRIFUGE_INPUT_TYPE.SUBSCRIBE:
|
|
2399
3153
|
this.subscribe(message.topic);
|
|
2400
3154
|
return;
|
|
2401
|
-
case
|
|
3155
|
+
case CENTRIFUGE_INPUT_TYPE.UNSUBSCRIBE:
|
|
2402
3156
|
this.unsubscribe(message.topic);
|
|
2403
3157
|
return;
|
|
2404
|
-
case
|
|
2405
|
-
case
|
|
3158
|
+
case CENTRIFUGE_INPUT_TYPE.PUBLISH:
|
|
3159
|
+
case CENTRIFUGE_INPUT_TYPE.PUBLISH_BIN:
|
|
2406
3160
|
this.publish(message.topic, message.data, message.messageId, message.timestamp);
|
|
2407
3161
|
return;
|
|
2408
|
-
case
|
|
3162
|
+
case CENTRIFUGE_INPUT_TYPE.STOP:
|
|
2409
3163
|
this.stop();
|
|
2410
3164
|
return;
|
|
3165
|
+
case CENTRIFUGE_INPUT_TYPE.TOKEN_RESPONSE:
|
|
3166
|
+
this.resolveToken(message.requestId, message.token);
|
|
3167
|
+
return;
|
|
3168
|
+
case CENTRIFUGE_INPUT_TYPE.TOKEN_ERROR:
|
|
3169
|
+
this.rejectToken(message.requestId, deserializeWorkerError(message.error));
|
|
3170
|
+
return;
|
|
2411
3171
|
default:
|
|
2412
3172
|
return;
|
|
2413
3173
|
}
|
|
2414
3174
|
}
|
|
2415
3175
|
/** Create the Centrifuge client, wire up lifecycle listeners, and connect. */
|
|
2416
|
-
initialize(url, config, transferable) {
|
|
3176
|
+
initialize(url, config, transferable, tokenBridge) {
|
|
2417
3177
|
if (this.client) return;
|
|
2418
3178
|
this.transferable = transferable;
|
|
2419
|
-
|
|
3179
|
+
this.tokenBridge = tokenBridge;
|
|
3180
|
+
const clientOptions = tokenBridge ? {
|
|
3181
|
+
...config,
|
|
3182
|
+
// Route credential fetches back to the main thread, where the
|
|
3183
|
+
// application's credentialProvider lives (config must stay
|
|
3184
|
+
// structured-cloneable, so functions cannot cross the boundary).
|
|
3185
|
+
getToken: () => this.requestToken("token"),
|
|
3186
|
+
getChannelToken: (channel) => this.requestToken("channelToken", channel)
|
|
3187
|
+
} : config;
|
|
3188
|
+
const client = new import_centrifuge.Centrifuge(url, clientOptions);
|
|
2420
3189
|
this.client = client;
|
|
2421
3190
|
client.on("state", (context) => {
|
|
2422
|
-
this.post({ type:
|
|
3191
|
+
this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: normalizeStatus(context.newState) });
|
|
2423
3192
|
});
|
|
2424
|
-
client.on("connected", () => this.post({ type:
|
|
2425
|
-
client.on("disconnected", () => this.post({ type:
|
|
3193
|
+
client.on("connected", () => this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: WORKER_STATUS.CONNECTED }));
|
|
3194
|
+
client.on("disconnected", () => this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: WORKER_STATUS.DISCONNECTED }));
|
|
2426
3195
|
client.on("error", (context) => this.postError(context));
|
|
2427
3196
|
client.on("publication", (context) => {
|
|
2428
3197
|
const topic = context.channel || getPayloadTopic(context.data);
|
|
@@ -2485,25 +3254,56 @@ var CentrifugeSession = class {
|
|
|
2485
3254
|
postPublication(topic, data) {
|
|
2486
3255
|
if (!topic) return;
|
|
2487
3256
|
if (this.transferable && data instanceof ArrayBuffer) {
|
|
2488
|
-
this.post({ type:
|
|
3257
|
+
this.post({ type: CENTRIFUGE_OUTPUT_TYPE.MESSAGE_BIN, topic, data }, [data]);
|
|
2489
3258
|
return;
|
|
2490
3259
|
}
|
|
2491
3260
|
const publication = parseDataBusPublication(data, topic);
|
|
2492
3261
|
if (!publication) return;
|
|
2493
3262
|
this.post({
|
|
2494
|
-
type:
|
|
3263
|
+
type: CENTRIFUGE_OUTPUT_TYPE.MESSAGE,
|
|
2495
3264
|
topic: publication.topic,
|
|
2496
3265
|
data: publication.data,
|
|
2497
|
-
...publication.messageId
|
|
2498
|
-
...publication.timestamp === void 0 ? {} : { timestamp: publication.timestamp }
|
|
3266
|
+
...publicationMetadata(publication.messageId, publication.timestamp)
|
|
2499
3267
|
});
|
|
2500
3268
|
}
|
|
2501
3269
|
/** Disconnect the client and clear all subscriptions. */
|
|
2502
3270
|
stop() {
|
|
3271
|
+
for (const [, pending] of this.pendingTokenRequests) {
|
|
3272
|
+
pending.reject(new Error("Centrifuge session stopped before the credential was resolved."));
|
|
3273
|
+
}
|
|
3274
|
+
this.pendingTokenRequests.clear();
|
|
2503
3275
|
this.client?.disconnect();
|
|
2504
3276
|
this.subscriptions.clear();
|
|
2505
3277
|
this.client = null;
|
|
2506
|
-
this.post({ type:
|
|
3278
|
+
this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: WORKER_STATUS.DISCONNECTED });
|
|
3279
|
+
}
|
|
3280
|
+
/** Issue a credential request to the main thread and await the response.
|
|
3281
|
+
* Used as Centrifuge's `getToken` / `getChannelToken` when token bridging is
|
|
3282
|
+
* enabled; resolved or rejected by a matching TOKEN_RESPONSE / TOKEN_ERROR. */
|
|
3283
|
+
requestToken(kind, channel) {
|
|
3284
|
+
const requestId = this.nextRequestId;
|
|
3285
|
+
this.nextRequestId += 1;
|
|
3286
|
+
return new Promise((resolve, reject) => {
|
|
3287
|
+
this.pendingTokenRequests.set(requestId, { resolve, reject });
|
|
3288
|
+
this.post({
|
|
3289
|
+
type: CENTRIFUGE_OUTPUT_TYPE.TOKEN_REQUEST,
|
|
3290
|
+
requestId,
|
|
3291
|
+
kind,
|
|
3292
|
+
...channel === void 0 ? {} : { channel }
|
|
3293
|
+
});
|
|
3294
|
+
});
|
|
3295
|
+
}
|
|
3296
|
+
resolveToken(requestId, token) {
|
|
3297
|
+
const pending = this.pendingTokenRequests.get(requestId);
|
|
3298
|
+
if (!pending) return;
|
|
3299
|
+
this.pendingTokenRequests.delete(requestId);
|
|
3300
|
+
pending.resolve(token);
|
|
3301
|
+
}
|
|
3302
|
+
rejectToken(requestId, error) {
|
|
3303
|
+
const pending = this.pendingTokenRequests.get(requestId);
|
|
3304
|
+
if (!pending) return;
|
|
3305
|
+
this.pendingTokenRequests.delete(requestId);
|
|
3306
|
+
pending.reject(error);
|
|
2507
3307
|
}
|
|
2508
3308
|
/** Forward a message to the sink (the transport layer). */
|
|
2509
3309
|
post(message, transfer) {
|
|
@@ -2515,7 +3315,7 @@ var CentrifugeSession = class {
|
|
|
2515
3315
|
* Fatal errors are distinguished by the client eventually emitting
|
|
2516
3316
|
* `disconnected` without a subsequent `connected`. */
|
|
2517
3317
|
postError(error) {
|
|
2518
|
-
this.post({ type:
|
|
3318
|
+
this.post({ type: CENTRIFUGE_OUTPUT_TYPE.ERROR, error: serializeError(error) });
|
|
2519
3319
|
}
|
|
2520
3320
|
};
|
|
2521
3321
|
function getPayloadTopic(data) {
|
|
@@ -2526,33 +3326,19 @@ function getPayloadTopic(data) {
|
|
|
2526
3326
|
const topic = nested ?? payload.channel;
|
|
2527
3327
|
return typeof topic === "string" ? topic : "";
|
|
2528
3328
|
}
|
|
2529
|
-
var LIVE_STATES = /* @__PURE__ */ new Set([
|
|
3329
|
+
var LIVE_STATES = /* @__PURE__ */ new Set([WORKER_STATUS.CONNECTING, WORKER_STATUS.CONNECTED]);
|
|
2530
3330
|
function normalizeStatus(status) {
|
|
2531
3331
|
return LIVE_STATES.has(status) ? status : "disconnected";
|
|
2532
3332
|
}
|
|
2533
|
-
function serializeError(error) {
|
|
2534
|
-
if (error instanceof Error) {
|
|
2535
|
-
return {
|
|
2536
|
-
name: error.name,
|
|
2537
|
-
message: error.message,
|
|
2538
|
-
...error.stack ? { stack: error.stack } : {}
|
|
2539
|
-
};
|
|
2540
|
-
}
|
|
2541
|
-
return {
|
|
2542
|
-
name: "CentrifugeError",
|
|
2543
|
-
message: typeof error === "string" ? error : "Centrifuge worker operation failed.",
|
|
2544
|
-
...error === void 0 ? {} : { context: error }
|
|
2545
|
-
};
|
|
2546
|
-
}
|
|
2547
3333
|
|
|
2548
3334
|
// src/worker-mode.ts
|
|
2549
3335
|
function selectWorkerBackend(mode, availability = {}) {
|
|
2550
3336
|
const hasDedicated = availability.worker ?? typeof Worker !== "undefined";
|
|
2551
3337
|
const hasShared = availability.sharedWorker ?? typeof SharedWorker !== "undefined";
|
|
2552
|
-
if (mode ===
|
|
2553
|
-
return hasShared ?
|
|
3338
|
+
if (mode === WORKER_MODE.SHARED || mode === WORKER_MODE.AUTO) {
|
|
3339
|
+
return hasShared ? WORKER_BACKEND.SHARED : hasDedicated ? WORKER_BACKEND.DEDICATED : WORKER_BACKEND.LOCAL;
|
|
2554
3340
|
}
|
|
2555
|
-
return hasDedicated ?
|
|
3341
|
+
return hasDedicated ? WORKER_BACKEND.DEDICATED : hasShared ? WORKER_BACKEND.SHARED : WORKER_BACKEND.LOCAL;
|
|
2556
3342
|
}
|
|
2557
3343
|
|
|
2558
3344
|
// src/centrifuge-protocol.ts
|
|
@@ -2569,6 +3355,7 @@ var CentrifugeWorkerTransport = class {
|
|
|
2569
3355
|
heartbeatIntervalMs;
|
|
2570
3356
|
workerFactory;
|
|
2571
3357
|
sharedWorkerFactory;
|
|
3358
|
+
credentialProvider;
|
|
2572
3359
|
backend = null;
|
|
2573
3360
|
worker = null;
|
|
2574
3361
|
sharedWorker = null;
|
|
@@ -2586,12 +3373,13 @@ var CentrifugeWorkerTransport = class {
|
|
|
2586
3373
|
return this.backend ?? "uninitialized";
|
|
2587
3374
|
}
|
|
2588
3375
|
constructor(options = {}) {
|
|
2589
|
-
this.workerMode = options.workerMode ??
|
|
3376
|
+
this.workerMode = options.workerMode ?? WORKER_MODE.DEDICATED;
|
|
2590
3377
|
this.transferable = options.transferable ?? false;
|
|
2591
3378
|
this.heartbeatIntervalMs = options.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS2;
|
|
2592
3379
|
assertHeartbeatInterval(this.heartbeatIntervalMs);
|
|
2593
3380
|
this.workerFactory = options.workerFactory;
|
|
2594
3381
|
this.sharedWorkerFactory = options.sharedWorkerFactory;
|
|
3382
|
+
this.credentialProvider = options.credentialProvider;
|
|
2595
3383
|
}
|
|
2596
3384
|
/**
|
|
2597
3385
|
* Start the transport: select a backend, initialise the Worker (or local
|
|
@@ -2602,29 +3390,29 @@ var CentrifugeWorkerTransport = class {
|
|
|
2602
3390
|
assertStructuredCloneable(config.options ?? {});
|
|
2603
3391
|
this.handlers = handlers;
|
|
2604
3392
|
const backend = selectWorkerBackend(this.workerMode, {
|
|
2605
|
-
worker: this.workerFactory !== void 0,
|
|
2606
|
-
sharedWorker: this.sharedWorkerFactory !== void 0
|
|
3393
|
+
worker: this.workerFactory !== void 0 || typeof Worker !== "undefined",
|
|
3394
|
+
sharedWorker: this.sharedWorkerFactory !== void 0 || typeof SharedWorker !== "undefined"
|
|
2607
3395
|
});
|
|
2608
3396
|
const input = this.buildInitInput(config);
|
|
2609
|
-
if (backend ===
|
|
3397
|
+
if (backend === WORKER_BACKEND.SHARED) {
|
|
2610
3398
|
this.startSharedWorker(input);
|
|
2611
|
-
this.backend =
|
|
3399
|
+
this.backend = WORKER_BACKEND.SHARED;
|
|
2612
3400
|
return;
|
|
2613
3401
|
}
|
|
2614
|
-
if (backend ===
|
|
3402
|
+
if (backend === WORKER_BACKEND.DEDICATED) {
|
|
2615
3403
|
this.startDedicatedWorker(input);
|
|
2616
|
-
this.backend =
|
|
3404
|
+
this.backend = WORKER_BACKEND.DEDICATED;
|
|
2617
3405
|
return;
|
|
2618
3406
|
}
|
|
2619
3407
|
this.localSession = new CentrifugeSession({ post: this.handleSessionOutput });
|
|
2620
3408
|
this.localSession.handle(input);
|
|
2621
|
-
this.backend =
|
|
3409
|
+
this.backend = WORKER_BACKEND.LOCAL;
|
|
2622
3410
|
}
|
|
2623
3411
|
subscribe(topic) {
|
|
2624
|
-
this.post({ type:
|
|
3412
|
+
this.post({ type: CENTRIFUGE_INPUT_TYPE.SUBSCRIBE, topic });
|
|
2625
3413
|
}
|
|
2626
3414
|
unsubscribe(topic) {
|
|
2627
|
-
this.post({ type:
|
|
3415
|
+
this.post({ type: CENTRIFUGE_INPUT_TYPE.UNSUBSCRIBE, topic });
|
|
2628
3416
|
}
|
|
2629
3417
|
/**
|
|
2630
3418
|
* Publish data to `topic`. Binary data (ArrayBuffer) is sent via Transferable
|
|
@@ -2632,10 +3420,10 @@ var CentrifugeWorkerTransport = class {
|
|
|
2632
3420
|
*/
|
|
2633
3421
|
publish(topic, data, options) {
|
|
2634
3422
|
if (this.transferable && data instanceof ArrayBuffer) {
|
|
2635
|
-
this.post({ type:
|
|
3423
|
+
this.post({ type: CENTRIFUGE_INPUT_TYPE.PUBLISH_BIN, topic, data, ...publicationMetadata(options?.messageId, options?.timestamp) }, [data]);
|
|
2636
3424
|
return;
|
|
2637
3425
|
}
|
|
2638
|
-
this.post({ type:
|
|
3426
|
+
this.post({ type: CENTRIFUGE_INPUT_TYPE.PUBLISH, topic, data, ...publicationMetadata(options?.messageId, options?.timestamp) });
|
|
2639
3427
|
}
|
|
2640
3428
|
/**
|
|
2641
3429
|
* Gracefully stop the transport: send STOP, clean up event listeners, and
|
|
@@ -2644,7 +3432,7 @@ var CentrifugeWorkerTransport = class {
|
|
|
2644
3432
|
stop() {
|
|
2645
3433
|
if (!this.backend) return;
|
|
2646
3434
|
this.generation++;
|
|
2647
|
-
this.post({ type:
|
|
3435
|
+
this.post({ type: CENTRIFUGE_INPUT_TYPE.STOP });
|
|
2648
3436
|
this.clearHeartbeat();
|
|
2649
3437
|
if (this.worker) {
|
|
2650
3438
|
this.worker.removeEventListener("message", this.handleMessage);
|
|
@@ -2663,10 +3451,11 @@ var CentrifugeWorkerTransport = class {
|
|
|
2663
3451
|
* default-resolution logic kicks in for the common case. */
|
|
2664
3452
|
buildInitInput(config) {
|
|
2665
3453
|
return {
|
|
2666
|
-
type:
|
|
3454
|
+
type: CENTRIFUGE_INPUT_TYPE.INIT,
|
|
2667
3455
|
url: config.url,
|
|
2668
3456
|
config: config.options ?? {},
|
|
2669
3457
|
...this.transferable ? { transferable: true } : {},
|
|
3458
|
+
...this.credentialProvider ? { tokenBridge: true } : {},
|
|
2670
3459
|
...this.heartbeatIntervalMs !== DEFAULT_HEARTBEAT_INTERVAL_MS2 ? { heartbeatIntervalMs: this.heartbeatIntervalMs } : {}
|
|
2671
3460
|
};
|
|
2672
3461
|
}
|
|
@@ -2705,10 +3494,34 @@ var CentrifugeWorkerTransport = class {
|
|
|
2705
3494
|
* Shared by the Worker message listener, the SharedWorker port listener,
|
|
2706
3495
|
* and the local-session sink — all three feed into this single dispatcher. */
|
|
2707
3496
|
handleOutput(message) {
|
|
2708
|
-
if (message.type ===
|
|
2709
|
-
if (message.type ===
|
|
2710
|
-
if (message.type ===
|
|
2711
|
-
if (message.type ===
|
|
3497
|
+
if (message.type === CENTRIFUGE_OUTPUT_TYPE.STATUS) this.handlers?.onStatus(message.status);
|
|
3498
|
+
if (message.type === CENTRIFUGE_OUTPUT_TYPE.MESSAGE) this.handlers?.onMessage({ topic: message.topic, data: message.data, ...publicationMetadata(message.messageId, message.timestamp) });
|
|
3499
|
+
if (message.type === CENTRIFUGE_OUTPUT_TYPE.MESSAGE_BIN) this.handlers?.onMessage({ topic: message.topic, data: message.data, ...publicationMetadata(message.messageId, message.timestamp) });
|
|
3500
|
+
if (message.type === CENTRIFUGE_OUTPUT_TYPE.ERROR) this.handlers?.onError(deserializeWorkerError(message.error));
|
|
3501
|
+
if (message.type === CENTRIFUGE_OUTPUT_TYPE.TOKEN_REQUEST) this.resolveTokenRequest(message.requestId, message.kind, message.channel);
|
|
3502
|
+
}
|
|
3503
|
+
/** Resolve a Worker credential request from the main thread: call the
|
|
3504
|
+
* credentialProvider, then post the fresh token (or a serialized failure)
|
|
3505
|
+
* back to the session that issued the request. */
|
|
3506
|
+
resolveTokenRequest(requestId, kind, channel) {
|
|
3507
|
+
const provider = this.credentialProvider;
|
|
3508
|
+
const value = kind === "channelToken" && provider?.getChannelToken ? provider.getChannelToken(channel ?? "") : provider?.getToken?.();
|
|
3509
|
+
Promise.resolve(value).then(
|
|
3510
|
+
(token) => {
|
|
3511
|
+
if (typeof token !== "string" || token.length === 0) {
|
|
3512
|
+
this.post({
|
|
3513
|
+
type: CENTRIFUGE_INPUT_TYPE.TOKEN_ERROR,
|
|
3514
|
+
requestId,
|
|
3515
|
+
error: serializeError(new Error(`The credential provider returned no ${kind}.`))
|
|
3516
|
+
});
|
|
3517
|
+
return;
|
|
3518
|
+
}
|
|
3519
|
+
this.post({ type: CENTRIFUGE_INPUT_TYPE.TOKEN_RESPONSE, requestId, token });
|
|
3520
|
+
},
|
|
3521
|
+
(error) => {
|
|
3522
|
+
this.post({ type: CENTRIFUGE_INPUT_TYPE.TOKEN_ERROR, requestId, error: serializeError(error) });
|
|
3523
|
+
}
|
|
3524
|
+
);
|
|
2712
3525
|
}
|
|
2713
3526
|
/** Handle a Worker-level failure (crash, message decode error). Discards the
|
|
2714
3527
|
* dead backend so a later start()/reopen can rebuild from scratch, and
|
|
@@ -2724,7 +3537,7 @@ var CentrifugeWorkerTransport = class {
|
|
|
2724
3537
|
this.clearHeartbeat();
|
|
2725
3538
|
this.resetBackend();
|
|
2726
3539
|
this.handlers?.onError(new Error(message));
|
|
2727
|
-
this.handlers?.onStatus(
|
|
3540
|
+
this.handlers?.onStatus(WORKER_STATUS.ERROR);
|
|
2728
3541
|
}
|
|
2729
3542
|
/** Remove every listener attached to the current SharedWorker and its port. */
|
|
2730
3543
|
detachSharedWorkerListeners() {
|
|
@@ -2737,7 +3550,7 @@ var CentrifugeWorkerTransport = class {
|
|
|
2737
3550
|
if (this.heartbeatHandle !== null) return;
|
|
2738
3551
|
if (this.heartbeatIntervalMs === Infinity) return;
|
|
2739
3552
|
this.heartbeatHandle = setInterval(() => {
|
|
2740
|
-
this.post({ type:
|
|
3553
|
+
this.post({ type: CENTRIFUGE_INPUT_TYPE.PING });
|
|
2741
3554
|
}, this.heartbeatIntervalMs);
|
|
2742
3555
|
}
|
|
2743
3556
|
clearHeartbeat() {
|
|
@@ -2788,6 +3601,7 @@ function createCentrifugeDataBus(options) {
|
|
|
2788
3601
|
const {
|
|
2789
3602
|
clusterKey,
|
|
2790
3603
|
connection,
|
|
3604
|
+
credentialProvider,
|
|
2791
3605
|
heartbeatIntervalMs,
|
|
2792
3606
|
sharedWorkerFactory,
|
|
2793
3607
|
transferable,
|
|
@@ -2805,7 +3619,8 @@ function createCentrifugeDataBus(options) {
|
|
|
2805
3619
|
...sharedWorkerFactory ? { sharedWorkerFactory } : {},
|
|
2806
3620
|
...transferable === void 0 ? {} : { transferable },
|
|
2807
3621
|
...workerMode ? { workerMode } : {},
|
|
2808
|
-
...heartbeatIntervalMs === void 0 ? {} : { heartbeatIntervalMs }
|
|
3622
|
+
...heartbeatIntervalMs === void 0 ? {} : { heartbeatIntervalMs },
|
|
3623
|
+
...credentialProvider ? { credentialProvider } : {}
|
|
2809
3624
|
})
|
|
2810
3625
|
});
|
|
2811
3626
|
}
|
|
@@ -2822,7 +3637,7 @@ function createDefaultWorker() {
|
|
|
2822
3637
|
);
|
|
2823
3638
|
}
|
|
2824
3639
|
return new Worker(workerUrl, {
|
|
2825
|
-
name:
|
|
3640
|
+
name: DEFAULT_STORAGE_PREFIX,
|
|
2826
3641
|
type: "module"
|
|
2827
3642
|
});
|
|
2828
3643
|
}
|
|
@@ -2839,43 +3654,12 @@ function createDefaultSharedWorker() {
|
|
|
2839
3654
|
);
|
|
2840
3655
|
}
|
|
2841
3656
|
return new SharedWorker(workerUrl, {
|
|
2842
|
-
name:
|
|
3657
|
+
name: `${DEFAULT_STORAGE_PREFIX}-shared`,
|
|
2843
3658
|
type: "module"
|
|
2844
3659
|
});
|
|
2845
3660
|
}
|
|
2846
|
-
function assertHeartbeatInterval(value) {
|
|
2847
|
-
if (value === Infinity) return;
|
|
2848
|
-
if (typeof value === "number" && Number.isFinite(value) && value > 0) return;
|
|
2849
|
-
throw new TypeError(
|
|
2850
|
-
`Centrifuge heartbeatIntervalMs must be a positive number or Infinity, got ${String(value)}.`
|
|
2851
|
-
);
|
|
2852
|
-
}
|
|
2853
|
-
function assertStructuredCloneable(value) {
|
|
2854
|
-
if (typeof structuredClone !== "function") return;
|
|
2855
|
-
try {
|
|
2856
|
-
structuredClone(value);
|
|
2857
|
-
} catch (error) {
|
|
2858
|
-
throw new TypeError(
|
|
2859
|
-
"Centrifuge Worker configuration and published data must be structured-cloneable.",
|
|
2860
|
-
{ cause: error }
|
|
2861
|
-
);
|
|
2862
|
-
}
|
|
2863
|
-
}
|
|
2864
3661
|
function postToPortLike(target, message, transfer) {
|
|
2865
3662
|
if (transfer) target.postMessage(message, transfer);
|
|
2866
3663
|
else target.postMessage(message);
|
|
2867
3664
|
}
|
|
2868
|
-
function deserializeWorkerError(error) {
|
|
2869
|
-
const result = new Error(error.message);
|
|
2870
|
-
result.name = error.name;
|
|
2871
|
-
if (error.stack) result.stack = error.stack;
|
|
2872
|
-
if (error.context !== void 0) Object.assign(result, { context: error.context });
|
|
2873
|
-
return result;
|
|
2874
|
-
}
|
|
2875
|
-
function publicationMetadata2(metadata) {
|
|
2876
|
-
return {
|
|
2877
|
-
...metadata?.messageId === void 0 ? {} : { messageId: metadata.messageId },
|
|
2878
|
-
...metadata?.timestamp === void 0 ? {} : { timestamp: metadata.timestamp }
|
|
2879
|
-
};
|
|
2880
|
-
}
|
|
2881
3665
|
//# sourceMappingURL=centrifuge.cjs.map
|