cross-tab-worker-databus 0.8.0 → 0.11.0
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 +34 -0
- package/README.md +2 -0
- package/README.zh.md +2 -0
- package/dist/centrifuge-protocol.d.ts +4 -0
- package/dist/centrifuge-protocol.d.ts.map +1 -1
- package/dist/centrifuge-session.d.ts.map +1 -1
- package/dist/centrifuge.js +29 -11
- package/dist/centrifuge.js.map +2 -2
- package/dist/centrifuge.shared.worker.js +37 -6
- package/dist/centrifuge.shared.worker.js.map +3 -3
- package/dist/centrifuge.worker.js +37 -6
- package/dist/centrifuge.worker.js.map +3 -3
- package/dist/{chunk-WWUY2FV2.js → chunk-NX76TAV3.js} +137 -26
- package/dist/chunk-NX76TAV3.js.map +7 -0
- package/dist/cjs/centrifuge.cjs +164 -35
- package/dist/cjs/centrifuge.cjs.map +4 -4
- package/dist/cjs/index.cjs +172 -38
- package/dist/cjs/index.cjs.map +3 -3
- package/dist/core/cluster.d.ts +3 -2
- package/dist/core/cluster.d.ts.map +1 -1
- package/dist/core/data-bus.d.ts +19 -0
- package/dist/core/data-bus.d.ts.map +1 -1
- package/dist/core/publication.d.ts +10 -0
- package/dist/core/publication.d.ts.map +1 -0
- package/dist/core/replay-persistence.d.ts +2 -0
- package/dist/core/replay-persistence.d.ts.map +1 -1
- package/dist/core/trace.d.ts +7 -0
- package/dist/core/trace.d.ts.map +1 -1
- package/dist/core/types.d.ts +19 -7
- package/dist/core/types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -14
- package/dist/index.js.map +2 -2
- package/dist/websocket.d.ts.map +1 -1
- package/docs/api.md +27 -6
- package/docs/architecture.md +7 -1
- package/docs/capabilities.md +2 -2
- package/docs/configuration.md +1 -1
- package/docs/roadmap.md +15 -1
- package/docs/transports.md +9 -5
- package/docs/zh/api.md +27 -6
- package/docs/zh/architecture.md +7 -1
- package/docs/zh/capabilities.md +2 -2
- package/docs/zh/configuration.md +1 -1
- package/docs/zh/transports.md +8 -4
- package/package.json +1 -1
- package/dist/chunk-WWUY2FV2.js.map +0 -7
package/dist/core/cluster.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { ClusterEnvironment } from './environment';
|
|
2
|
-
import type { WorkerControlAction, WorkerRecord, WorkerRoute, WorkerStatus } from './types';
|
|
2
|
+
import type { DataBusPublicationMetadata, WorkerControlAction, WorkerRecord, WorkerRoute, WorkerStatus } from './types';
|
|
3
3
|
/** Callbacks the cluster invokes to drive the transport and lifecycle. */
|
|
4
4
|
export interface WorkerClusterHandlers {
|
|
5
5
|
/** A SUBSCRIBE/UNSUBSCRIBE/PUBLISH control action was received for this worker. */
|
|
6
|
-
onControl: (action: WorkerControlAction, topic: string, data?: unknown, messageId?: string) => void;
|
|
6
|
+
onControl: (action: WorkerControlAction, topic: string, data?: unknown, messageId?: string, timestamp?: number) => void;
|
|
7
7
|
/** A fan-out publication event was received from another Worker. */
|
|
8
8
|
onEvent: (eventType: string, payload: unknown, sourceWorkerId: string) => void;
|
|
9
9
|
/** The cluster suspended (tab hidden / pagehide). */
|
|
@@ -137,6 +137,7 @@ export declare class WorkerClusterRuntime {
|
|
|
137
137
|
* silently dropping the publication.
|
|
138
138
|
*/
|
|
139
139
|
publish(topic: string, data: unknown, messageId?: string): boolean;
|
|
140
|
+
publish(topic: string, data: unknown, metadata?: DataBusPublicationMetadata): boolean;
|
|
140
141
|
/** True when `route` exists and its owner worker is among `workers`.
|
|
141
142
|
* Shared by subscribe (skip re-assignment) and publish (route to owner).
|
|
142
143
|
* Intentionally returns a plain boolean (not a type guard) so the caller
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cluster.d.ts","sourceRoot":"","sources":["../../src/core/cluster.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAkB,kBAAkB,EAAe,MAAM,eAAe,CAAC;AAQrF,O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AAC,0BAA0B;IAQlC,8DAA8D;IAC9D,OAAO,CAAC,eAAe;IAQvB;;;;;0CAKsC;IACtC,OAAO,CAAC,WAAW;IAMnB,4EAA4E;IAC5E,OAAO,CAAC,cAAc;IAItB,8GAA8G;IAC9G,OAAO,CAAC,WAAW;IAOnB,kGAAkG;IAClG,OAAO,CAAC,UAAU;IAOlB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,aAAa;IA6BrB,OAAO,CAAC,gBAAgB;IAIxB,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,oBAAoB;IAI5B,OAAO,CAAC,aAAa;IAQrB,8EAA8E;IAC9E,OAAO,CAAC,YAAY;CAGrB"}
|
package/dist/core/data-bus.d.ts
CHANGED
|
@@ -16,12 +16,20 @@ export interface DataBusReplayOptions<TData = unknown> {
|
|
|
16
16
|
maxPerTopic?: number;
|
|
17
17
|
/** Optional durable history backend. Defaults to in-memory only. */
|
|
18
18
|
persistence?: DataBusReplayPersistence<TData>;
|
|
19
|
+
/** Optional producer-timestamp retention window in milliseconds. */
|
|
20
|
+
retentionMs?: number;
|
|
19
21
|
}
|
|
20
22
|
/** Opt-in bounded duplicate suppression for publications carrying `messageId`. */
|
|
21
23
|
export interface DataBusDedupOptions {
|
|
22
24
|
maxEntries?: number;
|
|
23
25
|
ttlMs?: number;
|
|
24
26
|
}
|
|
27
|
+
export interface DataBusDedupStats {
|
|
28
|
+
enabled: boolean;
|
|
29
|
+
tracked: number;
|
|
30
|
+
suppressed: number;
|
|
31
|
+
accepted: number;
|
|
32
|
+
}
|
|
25
33
|
export interface CrossTabDataBusOptions<TConfig, TData> extends Omit<WorkerClusterOptions, 'handlers'> {
|
|
26
34
|
transport: DataBusTransport<TConfig, TData>;
|
|
27
35
|
initialConfig?: TConfig;
|
|
@@ -50,6 +58,7 @@ export declare class CrossTabDataBus<TConfig = unknown, TData = unknown> {
|
|
|
50
58
|
private readonly replayBuffers;
|
|
51
59
|
private readonly replayMaxPerTopic;
|
|
52
60
|
private readonly replayPersistence;
|
|
61
|
+
private readonly replayRetentionMs;
|
|
53
62
|
private readonly replayHydration;
|
|
54
63
|
private readonly initialConfig;
|
|
55
64
|
private readonly hasInitialConfig;
|
|
@@ -58,6 +67,8 @@ export declare class CrossTabDataBus<TConfig = unknown, TData = unknown> {
|
|
|
58
67
|
private readonly dedupTtlMs;
|
|
59
68
|
private readonly dedupEnabled;
|
|
60
69
|
private readonly seenMessageIds;
|
|
70
|
+
private dedupSuppressed;
|
|
71
|
+
private dedupAccepted;
|
|
61
72
|
private activeConfig;
|
|
62
73
|
private status;
|
|
63
74
|
private started;
|
|
@@ -106,6 +117,14 @@ export declare class CrossTabDataBus<TConfig = unknown, TData = unknown> {
|
|
|
106
117
|
unsubscribe(topic: string, handler?: DataBusMessageHandler<TData>): void;
|
|
107
118
|
/** Clear all in-memory replay buffers and, when supported, durable history. */
|
|
108
119
|
clearReplay(): Promise<void>;
|
|
120
|
+
/** Clear replay history for one exact topic, including durable storage. */
|
|
121
|
+
clearReplayTopic(topic: string): Promise<void>;
|
|
122
|
+
/** Remove replay entries older than an epoch-millisecond cutoff. */
|
|
123
|
+
clearReplayBefore(timestamp: number): Promise<void>;
|
|
124
|
+
/** Return bounded deduplication counters for diagnostics and health checks. */
|
|
125
|
+
getDedupStats(): DataBusDedupStats;
|
|
126
|
+
/** Drop all remembered IDs and reset dedup counters. */
|
|
127
|
+
resetDedup(): void;
|
|
109
128
|
/** Publish a message to `topic`. The owning Worker delivers it to the transport. */
|
|
110
129
|
publish(topic: string, data: unknown, options?: DataBusPublishOptions): void;
|
|
111
130
|
/** Register a handler that fires on every transport status change. Immediately invoked with the current status. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-bus.d.ts","sourceRoot":"","sources":["../../src/core/data-bus.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAEtD,OAAO,KAAK,EACV,mBAAmB,EAEnB,qBAAqB,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OAIG;IACH,OAAO,CAAC,YAAY;IA6BpB;;;OAGG;IACH,OAAO,CAAC,aAAa;CAUtB"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DataBusPublication } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Normalize legacy flat publications, metadata payload envelopes, and the
|
|
4
|
+
* canonical `{ op: 'publication', publication: ... }` shape.
|
|
5
|
+
*
|
|
6
|
+
* `fallbackTopic` is used by transports such as Centrifuge where the channel
|
|
7
|
+
* is supplied out-of-band by the client library rather than inside the data.
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseDataBusPublication<TData = unknown>(value: unknown, fallbackTopic?: string): DataBusPublication<TData> | null;
|
|
10
|
+
//# sourceMappingURL=publication.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"publication.d.ts","sourceRoot":"","sources":["../../src/core/publication.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,GAAG,OAAO,EACrD,KAAK,EAAE,OAAO,EACd,aAAa,CAAC,EAAE,MAAM,GACrB,kBAAkB,CAAC,KAAK,CAAC,GAAG,IAAI,CA0BlC"}
|
|
@@ -7,6 +7,8 @@ export interface DataBusReplayPersistence<TData = unknown> {
|
|
|
7
7
|
clear?(): Promise<void>;
|
|
8
8
|
/** Remove persisted replay history for one exact topic. */
|
|
9
9
|
clearTopic?(topic: string): Promise<void>;
|
|
10
|
+
/** Remove persisted messages older than the given epoch-millisecond cutoff. */
|
|
11
|
+
clearBefore?(timestamp: number): Promise<void>;
|
|
10
12
|
}
|
|
11
13
|
export interface IndexedDbReplayPersistenceOptions {
|
|
12
14
|
dbName?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replay-persistence.d.ts","sourceRoot":"","sources":["../../src/core/replay-persistence.ts"],"names":[],"mappings":"AAAA,OAA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AAC,KAAK,GAAG,OAAO,EAC9D,OAAO,EAAE,iCAAiC,GACzC,wBAAwB,CAAC,KAAK,CAAC,CAiFjC"}
|
package/dist/core/trace.d.ts
CHANGED
|
@@ -73,6 +73,8 @@ export interface DataBusMetricsTraceEvent {
|
|
|
73
73
|
dispatchP50Ms: number;
|
|
74
74
|
dispatchP95Ms: number;
|
|
75
75
|
dispatchMaxMs: number;
|
|
76
|
+
dedupAccepted: number;
|
|
77
|
+
dedupSuppressed: number;
|
|
76
78
|
timestamp: number;
|
|
77
79
|
}
|
|
78
80
|
export type DataBusTraceEvent = DataBusLifecycleTraceEvent | DataBusStatusTraceEvent | DataBusSubscriptionTraceEvent | DataBusCoordinationTraceEvent | DataBusErrorTraceEvent | DataBusReliabilityTraceEvent | DataBusMetricsTraceEvent;
|
|
@@ -112,6 +114,8 @@ export declare class DataBusTraceReporter {
|
|
|
112
114
|
private readonly receivedAt;
|
|
113
115
|
private readonly latencyBuckets;
|
|
114
116
|
private latencySumMs;
|
|
117
|
+
private dedupAccepted;
|
|
118
|
+
private dedupSuppressed;
|
|
115
119
|
constructor(options?: DataBusTraceOptions, now?: () => number);
|
|
116
120
|
/** Start the periodic metrics flush interval. No-op when mode is 'events'
|
|
117
121
|
* (no metrics to emit), when disabled, or when already running. */
|
|
@@ -136,6 +140,9 @@ export declare class DataBusTraceReporter {
|
|
|
136
140
|
* as dispatched but do not produce a latency sample.
|
|
137
141
|
*/
|
|
138
142
|
recordDispatched(topic: string): void;
|
|
143
|
+
/** Record deduplication outcomes for the next metrics window. */
|
|
144
|
+
recordDedupAccepted(): void;
|
|
145
|
+
recordDedupSuppressed(): void;
|
|
139
146
|
/** True when metrics recording is active: enabled and mode includes metrics.
|
|
140
147
|
* Extracted so the four record / flush methods share one guard expression
|
|
141
148
|
* instead of repeating `!this.enabled || this.mode === 'events'` at each. */
|
package/dist/core/trace.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trace.d.ts","sour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AAE,GAAG,GAAE,MAAM,MAAiB;IAQvE;uEACmE;IACnE,KAAK,IAAI,IAAI;IAMb,iEAAiE;IACjE,KAAK,IAAI,IAAI;IAOb,IAAI,IAAI,IAAI;IAIZ,4EAA4E;IAC5E,KAAK,CAAC,KAAK,EAAE,sBAAsB,GAAG,IAAI;IAK1C,gGAAgG;IAChG,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAkBnC;;;;OAIG;IACH,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAQpC;;;;;OAKG;IACH,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAgBrC,iEAAiE;IACjE,mBAAmB,IAAI,IAAI;IAI3B,qBAAqB,IAAI,IAAI;IAI7B;;iFAE6E;IAC7E,OAAO,KAAK,aAAa,GAExB;IAED,uEAAuE;IACvE,KAAK,IAAI,IAAI;IAKb,OAAO,CAAC,QAAQ;IA4BhB,OAAO,CAAC,YAAY;IAYpB,OAAO,CAAC,IAAI;CAWb"}
|
package/dist/core/types.d.ts
CHANGED
|
@@ -19,10 +19,24 @@ export type WorkerStatus = 'connecting' | 'connected' | 'disconnected' | 'error'
|
|
|
19
19
|
/** Control-plane actions routed between Workers via BroadcastChannel.
|
|
20
20
|
* These are the three operations a subscriber asks the owning worker to perform. */
|
|
21
21
|
export type WorkerControlAction = 'SUBSCRIBE' | 'UNSUBSCRIBE' | 'PUBLISH';
|
|
22
|
-
/**
|
|
23
|
-
export interface
|
|
22
|
+
/** Transport-neutral metadata attached to a publication. */
|
|
23
|
+
export interface DataBusPublicationMetadata {
|
|
24
24
|
/** Stable caller/server identifier propagated through the cluster and wire protocol. */
|
|
25
25
|
messageId?: string;
|
|
26
|
+
/** Producer-assigned Unix timestamp in milliseconds, used by retention policies. */
|
|
27
|
+
timestamp?: number;
|
|
28
|
+
}
|
|
29
|
+
/** Optional metadata attached to an outbound publication. */
|
|
30
|
+
export type DataBusPublishOptions = DataBusPublicationMetadata;
|
|
31
|
+
/** Canonical transport-neutral publication shape. */
|
|
32
|
+
export interface DataBusPublication<TData = unknown> extends DataBusPublicationMetadata {
|
|
33
|
+
topic: string;
|
|
34
|
+
data: TData;
|
|
35
|
+
}
|
|
36
|
+
/** Forward-compatible JSON envelope accepted from WebSocket-style servers. */
|
|
37
|
+
export interface DataBusPublicationEnvelope<TData = unknown> {
|
|
38
|
+
op: 'publication';
|
|
39
|
+
publication: DataBusPublication<TData>;
|
|
26
40
|
}
|
|
27
41
|
/** Whether the tab is currently visible to the user. Only influences placement
|
|
28
42
|
* of NEW topic routes; existing routes are sticky and never migrated on hide. */
|
|
@@ -101,6 +115,8 @@ export type WorkerClusterMessage<TEvent = unknown> =
|
|
|
101
115
|
data?: unknown;
|
|
102
116
|
/** Optional stable identifier for the publication. */
|
|
103
117
|
messageId?: string;
|
|
118
|
+
/** Optional producer timestamp propagated with PUBLISH actions. */
|
|
119
|
+
timestamp?: number;
|
|
104
120
|
}
|
|
105
121
|
/** The owning worker fans out a publication to every tab. `eventType`
|
|
106
122
|
* distinguishes databus publications from future event types. */
|
|
@@ -128,11 +144,7 @@ export type WorkerClusterMessage<TEvent = unknown> =
|
|
|
128
144
|
};
|
|
129
145
|
/** A publication delivered to a topic handler: the topic name and the
|
|
130
146
|
* structured-clone-safe payload. */
|
|
131
|
-
export interface DataBusMessage<TData = unknown> {
|
|
132
|
-
topic: string;
|
|
133
|
-
data: TData;
|
|
134
|
-
/** Optional caller/server supplied stable identifier used by opt-in dedup. */
|
|
135
|
-
messageId?: string;
|
|
147
|
+
export interface DataBusMessage<TData = unknown> extends DataBusPublication<TData> {
|
|
136
148
|
/** True when this delivery is a historical replay (see the `replay`
|
|
137
149
|
* subscription option) rather than a live publication. */
|
|
138
150
|
replayed?: boolean;
|
package/dist/core/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA;;;;;;GA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,OAAO;IAClE;8EAC0E;IAC1E,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,wBAAwB,CAAC,KAAK,CAAC,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACtF,mFAAmF;IACnF,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IAC7C,6FAA6F;IAC7F,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IAC/C,gDAAgD;IAChD,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IAC3F,mFAAmF;IACnF,IAAI,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC;CAC5B;AAED,6EAA6E;AAC7E,MAAM,MAAM,qBAAqB,CAAC,KAAK,GAAG,OAAO,IAAI,CAAC,OAAO,EAAE,cAAc,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC;AAC9F,yDAAyD;AACzD,MAAM,MAAM,oBAAoB,GAAG,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;AAClE,4EAA4E;AAC5E,MAAM,MAAM,mBAAmB,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* ```
|
|
10
10
|
*/
|
|
11
11
|
export { CrossTabDataBus } from './core/data-bus';
|
|
12
|
-
export type { CrossTabDataBusOptions, DataBusDedupOptions, DataBusReplayOptions } from './core/data-bus';
|
|
12
|
+
export type { CrossTabDataBusOptions, DataBusDedupOptions, DataBusDedupStats, DataBusReplayOptions } from './core/data-bus';
|
|
13
13
|
export { createIndexedDbReplayPersistence } from './core/replay-persistence';
|
|
14
14
|
export type { DataBusReplayPersistence, IndexedDbReplayPersistenceOptions } from './core/replay-persistence';
|
|
15
15
|
export type { DataBusCoordinationTraceEvent, DataBusErrorTraceEvent, DataBusLifecycleTraceEvent, DataBusMetricsTraceEvent, DataBusReliabilityTraceEvent, DataBusStatusTraceEvent, DataBusSubscriptionTraceEvent, DataBusTraceEvent, DataBusTraceMode, DataBusTraceOptions } from './core/trace';
|
|
@@ -23,5 +23,5 @@ export type { CreateWebSocketDataBusOptions, WebSocketDataBusConfig, WebSocketLi
|
|
|
23
23
|
export { selectWorkerBackend } from './worker-mode';
|
|
24
24
|
export type { WorkerBackend, WorkerMode } from './worker-mode';
|
|
25
25
|
export { DEFAULT_MAX_ACTIVE_WORKERS, hasActiveOwner, isWildcardTopic, selectActiveWorkers, selectLeastLoadedWorker, selectRebalanceTarget, topicMatchesPattern } from './core/routing';
|
|
26
|
-
export type { DataBusErrorHandler, DataBusMessage, DataBusPublishOptions, DataBusMessageHandler, DataBusStatusHandler, DataBusTransport, DataBusTransportHandlers, MaybePromise, TabVisibilityState, TopicSubscriberRecord, WorkerClusterMessage, WorkerControlAction, WorkerRecord, WorkerRole, WorkerRoute, WorkerStatus } from './core/types';
|
|
26
|
+
export type { DataBusErrorHandler, DataBusMessage, DataBusPublication, DataBusPublicationEnvelope, DataBusPublicationMetadata, DataBusPublishOptions, DataBusMessageHandler, DataBusStatusHandler, DataBusTransport, DataBusTransportHandlers, MaybePromise, TabVisibilityState, TopicSubscriberRecord, WorkerClusterMessage, WorkerControlAction, WorkerRecord, WorkerRole, WorkerRoute, WorkerStatus } from './core/types';
|
|
27
27
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,YAAY,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CA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AAmB,EACpB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EACV,mBAAmB,EACnB,cAAc,EACd,kBAAkB,EAClB,0BAA0B,EAC1B,0BAA0B,EAC1B,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,gBAAgB,EAChB,wBAAwB,EACxB,YAAY,EACZ,kBAAkB,EAClB,qBAAqB,EACrB,oBAAoB,EACpB,mBAAmB,EACnB,YAAY,EACZ,UAAU,EACV,WAAW,EACX,YAAY,EACb,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -7,12 +7,13 @@ import {
|
|
|
7
7
|
getOrCreateTabId,
|
|
8
8
|
hasActiveOwner,
|
|
9
9
|
isWildcardTopic,
|
|
10
|
+
parseDataBusPublication,
|
|
10
11
|
selectActiveWorkers,
|
|
11
12
|
selectLeastLoadedWorker,
|
|
12
13
|
selectRebalanceTarget,
|
|
13
14
|
selectWorkerBackend,
|
|
14
15
|
topicMatchesPattern
|
|
15
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-NX76TAV3.js";
|
|
16
17
|
|
|
17
18
|
// src/core/replay-persistence.ts
|
|
18
19
|
function createIndexedDbReplayPersistence(options) {
|
|
@@ -76,6 +77,24 @@ function createIndexedDbReplayPersistence(options) {
|
|
|
76
77
|
transaction.oncomplete = () => resolve();
|
|
77
78
|
transaction.onerror = () => reject(transaction.error ?? new Error("Failed to clear topic replay history."));
|
|
78
79
|
});
|
|
80
|
+
},
|
|
81
|
+
async clearBefore(timestamp) {
|
|
82
|
+
const db = await open();
|
|
83
|
+
await new Promise((resolve, reject) => {
|
|
84
|
+
const transaction = db.transaction(storeName, "readwrite");
|
|
85
|
+
const store = transaction.objectStore(storeName);
|
|
86
|
+
const request = store.getAll();
|
|
87
|
+
request.onsuccess = () => {
|
|
88
|
+
for (const record of request.result) {
|
|
89
|
+
const messages = record.messages.filter((message) => (message.timestamp ?? 0) >= timestamp);
|
|
90
|
+
if (messages.length === 0) store.delete(record.topic);
|
|
91
|
+
else if (messages.length !== record.messages.length) store.put({ topic: record.topic, messages });
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
request.onerror = () => reject(request.error ?? new Error("Failed to read replay history."));
|
|
95
|
+
transaction.oncomplete = () => resolve();
|
|
96
|
+
transaction.onerror = () => reject(transaction.error ?? new Error("Failed to prune replay history."));
|
|
97
|
+
});
|
|
79
98
|
}
|
|
80
99
|
};
|
|
81
100
|
}
|
|
@@ -129,10 +148,16 @@ var WebSocketTransport = class {
|
|
|
129
148
|
/** Publish `data` to `topic` as a JSON frame. Requires an open socket. */
|
|
130
149
|
publish(topic, data, options) {
|
|
131
150
|
if (data instanceof ArrayBuffer) {
|
|
132
|
-
this.sendBinaryFrame(topic, data, options?.messageId);
|
|
151
|
+
this.sendBinaryFrame(topic, data, options?.messageId, options?.timestamp);
|
|
133
152
|
return;
|
|
134
153
|
}
|
|
135
|
-
this.sendFrame({
|
|
154
|
+
this.sendFrame({
|
|
155
|
+
op: "publish",
|
|
156
|
+
topic,
|
|
157
|
+
data,
|
|
158
|
+
...options?.messageId === void 0 ? {} : { messageId: options.messageId },
|
|
159
|
+
...options?.timestamp === void 0 ? {} : { timestamp: options.timestamp }
|
|
160
|
+
});
|
|
136
161
|
}
|
|
137
162
|
/** Close the socket and drop all state. Safe to call multiple times. */
|
|
138
163
|
stop() {
|
|
@@ -152,9 +177,15 @@ var WebSocketTransport = class {
|
|
|
152
177
|
}
|
|
153
178
|
this.socket.send(JSON.stringify(payload));
|
|
154
179
|
}
|
|
155
|
-
sendBinaryFrame(topic, data, messageId) {
|
|
156
|
-
if (messageId) {
|
|
157
|
-
this.sendFrame({
|
|
180
|
+
sendBinaryFrame(topic, data, messageId, timestamp) {
|
|
181
|
+
if (messageId !== void 0 || timestamp !== void 0) {
|
|
182
|
+
this.sendFrame({
|
|
183
|
+
op: "publish",
|
|
184
|
+
topic,
|
|
185
|
+
data: Array.from(new Uint8Array(data)),
|
|
186
|
+
...messageId === void 0 ? {} : { messageId },
|
|
187
|
+
...timestamp === void 0 ? {} : { timestamp }
|
|
188
|
+
});
|
|
158
189
|
return;
|
|
159
190
|
}
|
|
160
191
|
if (this.socket?.readyState !== WS_OPEN) {
|
|
@@ -196,14 +227,8 @@ var WebSocketTransport = class {
|
|
|
196
227
|
return;
|
|
197
228
|
}
|
|
198
229
|
if (!parsed || typeof parsed !== "object") return;
|
|
199
|
-
const
|
|
200
|
-
if (
|
|
201
|
-
const message = {
|
|
202
|
-
topic: frame.topic,
|
|
203
|
-
data: frame.data,
|
|
204
|
-
...typeof frame.messageId === "string" ? { messageId: frame.messageId } : {}
|
|
205
|
-
};
|
|
206
|
-
this.handlers?.onMessage(message);
|
|
230
|
+
const publication = parseDataBusPublication(parsed);
|
|
231
|
+
if (publication) this.handlers?.onMessage(publication);
|
|
207
232
|
}
|
|
208
233
|
};
|
|
209
234
|
function defaultWebSocketFactory(url, protocols) {
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/core/replay-persistence.ts", "../src/websocket.ts"],
|
|
4
|
-
"sourcesContent": ["import type { DataBusMessage } from './types';\n\n/** Optional persistence backend for replay history. */\nexport interface DataBusReplayPersistence<TData = unknown> {\n load(): Promise<ReadonlyArray<DataBusMessage<TData>>>;\n append(message: DataBusMessage<TData>): Promise<void>;\n /** Remove all persisted replay history. */\n clear?(): Promise<void>;\n /** Remove persisted replay history for one exact topic. */\n clearTopic?(topic: string): Promise<void>;\n}\n\nexport interface IndexedDbReplayPersistenceOptions {\n dbName?: string;\n maxPerTopic: number;\n}\n\n/** Create a browser IndexedDB-backed replay store. */\nexport function createIndexedDbReplayPersistence<TData = unknown>(\n options: IndexedDbReplayPersistenceOptions\n): DataBusReplayPersistence<TData> {\n const indexedDb = globalThis.indexedDB;\n if (!indexedDb) throw new Error('IndexedDB is unavailable in this environment.');\n const dbName = options.dbName ?? 'cross-tab-worker-databus';\n const storeName = 'replay';\n const maxPerTopic = options.maxPerTopic;\n if (!Number.isSafeInteger(maxPerTopic) || maxPerTopic <= 0) {\n throw new TypeError(`maxPerTopic must be a positive safe integer, got ${String(maxPerTopic)}.`);\n }\n let dbPromise: Promise<IDBDatabase> | null = null;\n const open = (): Promise<IDBDatabase> => {\n if (dbPromise) return dbPromise;\n dbPromise = new Promise((resolve, reject) => {\n const request = indexedDb.open(dbName, 1);\n request.onupgradeneeded = () => request.result.createObjectStore(storeName, { keyPath: 'topic' });\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error ?? new Error('Failed to open replay database.'));\n });\n return dbPromise;\n };\n return {\n async load() {\n const db = await open();\n return new Promise((resolve, reject) => {\n const request = db.transaction(storeName, 'readonly').objectStore(storeName).getAll();\n request.onsuccess = () => resolve(request.result.flatMap(record => record.messages as DataBusMessage<TData>[]));\n request.onerror = () => reject(request.error ?? new Error('Failed to load replay history.'));\n });\n },\n async append(message) {\n const db = await open();\n await new Promise<void>((resolve, reject) => {\n const transaction = db.transaction(storeName, 'readwrite');\n const store = transaction.objectStore(storeName);\n const request = store.get(message.topic);\n request.onsuccess = () => {\n const messages = ((request.result?.messages ?? []) as DataBusMessage<TData>[]).concat(message).slice(-maxPerTopic);\n store.put({ topic: message.topic, messages });\n };\n request.onerror = () => reject(request.error ?? new Error('Failed to read replay history.'));\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error ?? new Error('Failed to persist replay history.'));\n });\n },\n async clear() {\n const db = await open();\n await new Promise<void>((resolve, reject) => {\n const transaction = db.transaction(storeName, 'readwrite');\n transaction.objectStore(storeName).clear();\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error ?? new Error('Failed to clear replay history.'));\n });\n },\n async clearTopic(topic) {\n const db = await open();\n await new Promise<void>((resolve, reject) => {\n const transaction = db.transaction(storeName, 'readwrite');\n transaction.objectStore(storeName).delete(topic);\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error ?? new Error('Failed to clear topic replay history.'));\n });\n }\n };\n}\n", "/**\n * WebSocketTransport \u2014 a dependency-free transport over a plain WebSocket.\n *\n * Validates the `DataBusTransport` abstraction with a second, minimal backend:\n * any WebSocket server that speaks the tiny JSON protocol below can back the\n * same cross-tab clustering stack (owner dedup, sticky routes, EVENT fan-out)\n * that the Centrifuge backend uses.\n *\n * Wire protocol (JSON text frames):\n * - client \u2192 server: `{\"op\":\"subscribe\"|\"unsubscribe\"|\"publish\",\"topic\":...,\"data\":...}`\n * - server \u2192 client: `{\"topic\":...,\"data\":...}` for publications; anything\n * without a string `topic` field is ignored (forward-compatible).\n */\nimport { CrossTabDataBus } from './core/data-bus';\nimport type { CrossTabDataBusOptions } from './core/data-bus';\nimport type {\n DataBusTransport,\n DataBusTransportHandlers,\n DataBusPublishOptions,\n DataBusMessage,\n MaybePromise,\n WorkerStatus\n} from './core/types';\n\n/** Minimal WebSocket surface used by the transport. Matches the browser\n * `WebSocket` subset the transport touches; injectable for tests and runtimes. */\nexport interface WebSocketLike {\n /** Current connection state; 1 (OPEN) means frames may be sent. */\n readonly readyState?: number;\n send(data: string | ArrayBuffer): void;\n close(code?: number, reason?: string): void;\n onopen: (() => void) | null;\n onclose: (() => void) | null;\n onerror: (() => void) | null;\n onmessage: ((event: { data: unknown }) => void) | null;\n}\n\n/** Connection configuration for {@link WebSocketTransport}. */\nexport interface WebSocketDataBusConfig {\n /** WebSocket endpoint, e.g. `wss://example.test/ws`. */\n url: string;\n /** Subprotocol(s) passed to the WebSocket handshake. */\n protocols?: string | string[];\n /** Custom socket factory. Defaults to the global `WebSocket`; injectable\n * for tests and non-browser runtimes. */\n webSocketFactory?: (url: string, protocols?: string | string[]) => WebSocketLike;\n}\n\n/** Options for creating a fully-configured CrossTabDataBus with a WebSocket transport. */\nexport interface CreateWebSocketDataBusOptions<TData = unknown>\n extends Omit<\n CrossTabDataBusOptions<WebSocketDataBusConfig, TData>,\n 'autoStart' | 'clusterKey' | 'initialConfig' | 'transport'\n > {\n /** WebSocket connection configuration. */\n connection: WebSocketDataBusConfig;\n /** Cluster key for cross-tab coordination. Defaults to the connection URL. */\n clusterKey?: string;\n}\n\nconst WS_OPEN = 1;\n\n/** Transport that talks a minimal JSON protocol over a plain WebSocket.\n * Connection lifecycle maps directly to the DataBus status vocabulary:\n * open \u2192 `connected`, close \u2192 `disconnected`, error \u2192 `error` (which the\n * DataBus treats as its auto-recovery trigger). The transport holds no\n * reconnection logic of its own \u2014 reopening is the DataBus's job. */\nexport class WebSocketTransport<TData = unknown>\n implements DataBusTransport<WebSocketDataBusConfig, TData>\n{\n private socket: WebSocketLike | null = null;\n private handlers: DataBusTransportHandlers<TData> | null = null;\n private readonly subscribedTopics = new Set<string>();\n\n constructor(private readonly connection: WebSocketDataBusConfig) {}\n\n /** Open the WebSocket and wire lifecycle listeners. A factory failure is\n * reported through `onStatus('error')` so the DataBus can recover. */\n start(config: WebSocketDataBusConfig, handlers: DataBusTransportHandlers<TData>): MaybePromise<void> {\n if (this.socket) return;\n this.handlers = handlers;\n // The factory may live on the constructor connection (createWebSocketDataBus\n // path) or on the runtime config (direct transport use) \u2014 accept both.\n const factory = config.webSocketFactory ?? this.connection.webSocketFactory ?? defaultWebSocketFactory;\n const protocols = config.protocols ?? this.connection.protocols;\n let socket: WebSocketLike;\n try {\n socket = factory(config.url, protocols);\n } catch (error) {\n handlers.onStatus('error');\n handlers.onError(error);\n return;\n }\n socket.onopen = () => {\n // Re-assert every topic so a reopened socket (recovery path) restores\n // the server-side subscriptions without DataBus involvement.\n for (const topic of this.subscribedTopics) {\n this.sendFrame({ op: 'subscribe', topic });\n }\n handlers.onStatus('connected');\n };\n socket.onclose = () => handlers.onStatus('disconnected');\n socket.onerror = () => handlers.onStatus('error');\n socket.onmessage = event => this.handleMessage(event.data);\n this.socket = socket;\n }\n\n /** Idempotent: re-subscribing an active topic re-sends the frame but does\n * not duplicate the local tracking entry. */\n subscribe(topic: string): MaybePromise<void> {\n this.subscribedTopics.add(topic);\n this.sendFrame({ op: 'subscribe', topic });\n }\n\n /** Idempotent: unsubscribing an unknown topic is a no-op. */\n unsubscribe(topic: string): MaybePromise<void> {\n this.subscribedTopics.delete(topic);\n this.sendFrame({ op: 'unsubscribe', topic });\n }\n\n /** Publish `data` to `topic` as a JSON frame. Requires an open socket. */\n publish(topic: string, data: unknown, options?: DataBusPublishOptions): MaybePromise<void> {\n if (data instanceof ArrayBuffer) {\n this.sendBinaryFrame(topic, data, options?.messageId);\n return;\n }\n this.sendFrame({ op: 'publish', topic, data, ...(options?.messageId ? { messageId: options.messageId } : {}) });\n }\n\n /** Close the socket and drop all state. Safe to call multiple times. */\n stop(): MaybePromise<void> {\n const socket = this.socket;\n this.socket = null;\n this.handlers = null;\n this.subscribedTopics.clear();\n socket?.close();\n }\n\n /** Send one JSON frame. Frames are dropped with an `onError` report when\n * the socket is not open \u2014 subscribe frames are re-sent on open, so the\n * only real loss is a publish during a disconnect window. */\n private sendFrame(payload: { op: string; topic: string; data?: unknown; messageId?: string }): void {\n if (this.socket?.readyState !== WS_OPEN) {\n this.handlers?.onError(new Error(`WebSocket is not open; dropped \"${payload.op}\" frame.`));\n return;\n }\n this.socket.send(JSON.stringify(payload));\n }\n\n private sendBinaryFrame(topic: string, data: ArrayBuffer, messageId?: string): void {\n if (messageId) {\n // Binary frames retain their compact legacy shape; metadata is sent as a\n // JSON envelope so IDs are never silently lost.\n this.sendFrame({ op: 'publish', topic, data: Array.from(new Uint8Array(data)), messageId });\n return;\n }\n if (this.socket?.readyState !== WS_OPEN) {\n this.handlers?.onError(new Error('WebSocket is not open; dropped \"publish\" frame.'));\n return;\n }\n const topicBytes = new TextEncoder().encode(topic);\n if (topicBytes.length > 0xffff) {\n this.handlers?.onError(new Error('WebSocket topic is too long for a binary frame.'));\n return;\n }\n const frame = new Uint8Array(3 + topicBytes.length + data.byteLength);\n frame[0] = 0xc7;\n new DataView(frame.buffer).setUint16(1, topicBytes.length);\n frame.set(topicBytes, 3);\n frame.set(new Uint8Array(data), 3 + topicBytes.length);\n this.socket.send(frame.buffer);\n }\n\n /** Parse a server frame. Only objects carrying a string `topic` are\n * publications; malformed JSON and unknown shapes are ignored so a chatty\n * server cannot crash the message path. */\n private handleMessage(raw: unknown): void {\n let parsed: unknown;\n if (raw instanceof ArrayBuffer) {\n const bytes = new Uint8Array(raw);\n if (bytes[0] !== 0xc7 || bytes.length < 3) return;\n const topicLength = new DataView(raw).getUint16(1);\n if (bytes.length < 3 + topicLength) return;\n const topic = new TextDecoder().decode(bytes.subarray(3, 3 + topicLength));\n const data = bytes.slice(3 + topicLength).buffer;\n this.handlers?.onMessage({ topic, data: data as TData });\n return;\n }\n if (typeof raw !== 'string') return;\n try {\n parsed = JSON.parse(raw);\n } catch {\n this.handlers?.onError(new Error('WebSocket server sent a non-JSON frame.'));\n return;\n }\n if (!parsed || typeof parsed !== 'object') return;\n const frame = parsed as Record<string, unknown>;\n if (typeof frame.topic !== 'string') return;\n const message: DataBusMessage<TData> = {\n topic: frame.topic,\n data: frame.data as TData,\n ...(typeof frame.messageId === 'string' ? { messageId: frame.messageId } : {})\n };\n this.handlers?.onMessage(message);\n }\n}\n\n/** Resolve the platform WebSocket, or null in runtimes without one (SSR/Node). */\nfunction defaultWebSocketFactory(url: string, protocols?: string | string[]): WebSocketLike {\n if (typeof WebSocket === 'undefined') {\n throw new Error('WebSocketTransport requires a WebSocket implementation.');\n }\n return new WebSocket(url, protocols) as unknown as WebSocketLike;\n}\n\n/** Create a CrossTabDataBus backed by a plain WebSocket transport.\n * Cross-tab clustering (owner dedup, sticky routes, failover) works identically\n * to the Centrifuge backend \u2014 only the transport I/O differs. */\nexport function createWebSocketDataBus<TData = unknown>(\n options: CreateWebSocketDataBusOptions<TData>\n): CrossTabDataBus<WebSocketDataBusConfig, TData> {\n const { clusterKey, connection, ...dataBusOptions } = options;\n return new CrossTabDataBus({\n ...dataBusOptions,\n autoStart: true,\n clusterKey: clusterKey ?? connection.url,\n initialConfig: connection,\n transport: new WebSocketTransport<TData>(connection)\n });\n}\n\n/** Re-export for convenience: the status type used by the transport. */\nexport type { WorkerStatus };\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import type { DataBusMessage } from './types';\n\n/** Optional persistence backend for replay history. */\nexport interface DataBusReplayPersistence<TData = unknown> {\n load(): Promise<ReadonlyArray<DataBusMessage<TData>>>;\n append(message: DataBusMessage<TData>): Promise<void>;\n /** Remove all persisted replay history. */\n clear?(): Promise<void>;\n /** Remove persisted replay history for one exact topic. */\n clearTopic?(topic: string): Promise<void>;\n /** Remove persisted messages older than the given epoch-millisecond cutoff. */\n clearBefore?(timestamp: number): Promise<void>;\n}\n\nexport interface IndexedDbReplayPersistenceOptions {\n dbName?: string;\n maxPerTopic: number;\n}\n\n/** Create a browser IndexedDB-backed replay store. */\nexport function createIndexedDbReplayPersistence<TData = unknown>(\n options: IndexedDbReplayPersistenceOptions\n): DataBusReplayPersistence<TData> {\n const indexedDb = globalThis.indexedDB;\n if (!indexedDb) throw new Error('IndexedDB is unavailable in this environment.');\n const dbName = options.dbName ?? 'cross-tab-worker-databus';\n const storeName = 'replay';\n const maxPerTopic = options.maxPerTopic;\n if (!Number.isSafeInteger(maxPerTopic) || maxPerTopic <= 0) {\n throw new TypeError(`maxPerTopic must be a positive safe integer, got ${String(maxPerTopic)}.`);\n }\n let dbPromise: Promise<IDBDatabase> | null = null;\n const open = (): Promise<IDBDatabase> => {\n if (dbPromise) return dbPromise;\n dbPromise = new Promise((resolve, reject) => {\n const request = indexedDb.open(dbName, 1);\n request.onupgradeneeded = () => request.result.createObjectStore(storeName, { keyPath: 'topic' });\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error ?? new Error('Failed to open replay database.'));\n });\n return dbPromise;\n };\n return {\n async load() {\n const db = await open();\n return new Promise((resolve, reject) => {\n const request = db.transaction(storeName, 'readonly').objectStore(storeName).getAll();\n request.onsuccess = () => resolve(request.result.flatMap(record => record.messages as DataBusMessage<TData>[]));\n request.onerror = () => reject(request.error ?? new Error('Failed to load replay history.'));\n });\n },\n async append(message) {\n const db = await open();\n await new Promise<void>((resolve, reject) => {\n const transaction = db.transaction(storeName, 'readwrite');\n const store = transaction.objectStore(storeName);\n const request = store.get(message.topic);\n request.onsuccess = () => {\n const messages = ((request.result?.messages ?? []) as DataBusMessage<TData>[]).concat(message).slice(-maxPerTopic);\n store.put({ topic: message.topic, messages });\n };\n request.onerror = () => reject(request.error ?? new Error('Failed to read replay history.'));\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error ?? new Error('Failed to persist replay history.'));\n });\n },\n async clear() {\n const db = await open();\n await new Promise<void>((resolve, reject) => {\n const transaction = db.transaction(storeName, 'readwrite');\n transaction.objectStore(storeName).clear();\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error ?? new Error('Failed to clear replay history.'));\n });\n },\n async clearTopic(topic) {\n const db = await open();\n await new Promise<void>((resolve, reject) => {\n const transaction = db.transaction(storeName, 'readwrite');\n transaction.objectStore(storeName).delete(topic);\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error ?? new Error('Failed to clear topic replay history.'));\n });\n },\n async clearBefore(timestamp) {\n const db = await open();\n await new Promise<void>((resolve, reject) => {\n const transaction = db.transaction(storeName, 'readwrite');\n const store = transaction.objectStore(storeName);\n const request = store.getAll();\n request.onsuccess = () => {\n for (const record of request.result as Array<{ topic: string; messages: DataBusMessage<TData>[] }>) {\n const messages = record.messages.filter(message => (message.timestamp ?? 0) >= timestamp);\n if (messages.length === 0) store.delete(record.topic);\n else if (messages.length !== record.messages.length) store.put({ topic: record.topic, messages });\n }\n };\n request.onerror = () => reject(request.error ?? new Error('Failed to read replay history.'));\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error ?? new Error('Failed to prune replay history.'));\n });\n }\n };\n}\n", "/**\n * WebSocketTransport \u2014 a dependency-free transport over a plain WebSocket.\n *\n * Validates the `DataBusTransport` abstraction with a second, minimal backend:\n * any WebSocket server that speaks the tiny JSON protocol below can back the\n * same cross-tab clustering stack (owner dedup, sticky routes, EVENT fan-out)\n * that the Centrifuge backend uses.\n *\n * Wire protocol (JSON text frames):\n * - client \u2192 server: `{\"op\":\"subscribe\"|\"unsubscribe\"|\"publish\",\"topic\":...,\"data\":...}`\n * - server \u2192 client: `{\"topic\":...,\"data\":...}` for publications; anything\n * without a string `topic` field is ignored (forward-compatible).\n */\nimport { CrossTabDataBus } from './core/data-bus';\nimport { parseDataBusPublication } from './core/publication';\nimport type { CrossTabDataBusOptions } from './core/data-bus';\nimport type {\n DataBusTransport,\n DataBusTransportHandlers,\n DataBusPublishOptions,\n DataBusMessage,\n MaybePromise,\n WorkerStatus\n} from './core/types';\n\n/** Minimal WebSocket surface used by the transport. Matches the browser\n * `WebSocket` subset the transport touches; injectable for tests and runtimes. */\nexport interface WebSocketLike {\n /** Current connection state; 1 (OPEN) means frames may be sent. */\n readonly readyState?: number;\n send(data: string | ArrayBuffer): void;\n close(code?: number, reason?: string): void;\n onopen: (() => void) | null;\n onclose: (() => void) | null;\n onerror: (() => void) | null;\n onmessage: ((event: { data: unknown }) => void) | null;\n}\n\n/** Connection configuration for {@link WebSocketTransport}. */\nexport interface WebSocketDataBusConfig {\n /** WebSocket endpoint, e.g. `wss://example.test/ws`. */\n url: string;\n /** Subprotocol(s) passed to the WebSocket handshake. */\n protocols?: string | string[];\n /** Custom socket factory. Defaults to the global `WebSocket`; injectable\n * for tests and non-browser runtimes. */\n webSocketFactory?: (url: string, protocols?: string | string[]) => WebSocketLike;\n}\n\n/** Options for creating a fully-configured CrossTabDataBus with a WebSocket transport. */\nexport interface CreateWebSocketDataBusOptions<TData = unknown>\n extends Omit<\n CrossTabDataBusOptions<WebSocketDataBusConfig, TData>,\n 'autoStart' | 'clusterKey' | 'initialConfig' | 'transport'\n > {\n /** WebSocket connection configuration. */\n connection: WebSocketDataBusConfig;\n /** Cluster key for cross-tab coordination. Defaults to the connection URL. */\n clusterKey?: string;\n}\n\nconst WS_OPEN = 1;\n\n/** Transport that talks a minimal JSON protocol over a plain WebSocket.\n * Connection lifecycle maps directly to the DataBus status vocabulary:\n * open \u2192 `connected`, close \u2192 `disconnected`, error \u2192 `error` (which the\n * DataBus treats as its auto-recovery trigger). The transport holds no\n * reconnection logic of its own \u2014 reopening is the DataBus's job. */\nexport class WebSocketTransport<TData = unknown>\n implements DataBusTransport<WebSocketDataBusConfig, TData>\n{\n private socket: WebSocketLike | null = null;\n private handlers: DataBusTransportHandlers<TData> | null = null;\n private readonly subscribedTopics = new Set<string>();\n\n constructor(private readonly connection: WebSocketDataBusConfig) {}\n\n /** Open the WebSocket and wire lifecycle listeners. A factory failure is\n * reported through `onStatus('error')` so the DataBus can recover. */\n start(config: WebSocketDataBusConfig, handlers: DataBusTransportHandlers<TData>): MaybePromise<void> {\n if (this.socket) return;\n this.handlers = handlers;\n // The factory may live on the constructor connection (createWebSocketDataBus\n // path) or on the runtime config (direct transport use) \u2014 accept both.\n const factory = config.webSocketFactory ?? this.connection.webSocketFactory ?? defaultWebSocketFactory;\n const protocols = config.protocols ?? this.connection.protocols;\n let socket: WebSocketLike;\n try {\n socket = factory(config.url, protocols);\n } catch (error) {\n handlers.onStatus('error');\n handlers.onError(error);\n return;\n }\n socket.onopen = () => {\n // Re-assert every topic so a reopened socket (recovery path) restores\n // the server-side subscriptions without DataBus involvement.\n for (const topic of this.subscribedTopics) {\n this.sendFrame({ op: 'subscribe', topic });\n }\n handlers.onStatus('connected');\n };\n socket.onclose = () => handlers.onStatus('disconnected');\n socket.onerror = () => handlers.onStatus('error');\n socket.onmessage = event => this.handleMessage(event.data);\n this.socket = socket;\n }\n\n /** Idempotent: re-subscribing an active topic re-sends the frame but does\n * not duplicate the local tracking entry. */\n subscribe(topic: string): MaybePromise<void> {\n this.subscribedTopics.add(topic);\n this.sendFrame({ op: 'subscribe', topic });\n }\n\n /** Idempotent: unsubscribing an unknown topic is a no-op. */\n unsubscribe(topic: string): MaybePromise<void> {\n this.subscribedTopics.delete(topic);\n this.sendFrame({ op: 'unsubscribe', topic });\n }\n\n /** Publish `data` to `topic` as a JSON frame. Requires an open socket. */\n publish(topic: string, data: unknown, options?: DataBusPublishOptions): MaybePromise<void> {\n if (data instanceof ArrayBuffer) {\n this.sendBinaryFrame(topic, data, options?.messageId, options?.timestamp);\n return;\n }\n this.sendFrame({\n op: 'publish',\n topic,\n data,\n ...(options?.messageId === undefined ? {} : { messageId: options.messageId }),\n ...(options?.timestamp === undefined ? {} : { timestamp: options.timestamp })\n });\n }\n\n /** Close the socket and drop all state. Safe to call multiple times. */\n stop(): MaybePromise<void> {\n const socket = this.socket;\n this.socket = null;\n this.handlers = null;\n this.subscribedTopics.clear();\n socket?.close();\n }\n\n /** Send one JSON frame. Frames are dropped with an `onError` report when\n * the socket is not open \u2014 subscribe frames are re-sent on open, so the\n * only real loss is a publish during a disconnect window. */\n private sendFrame(payload: { op: string; topic: string; data?: unknown; messageId?: string; timestamp?: number }): void {\n if (this.socket?.readyState !== WS_OPEN) {\n this.handlers?.onError(new Error(`WebSocket is not open; dropped \"${payload.op}\" frame.`));\n return;\n }\n this.socket.send(JSON.stringify(payload));\n }\n\n private sendBinaryFrame(topic: string, data: ArrayBuffer, messageId?: string, timestamp?: number): void {\n if (messageId !== undefined || timestamp !== undefined) {\n // Binary frames retain their compact legacy shape; metadata is sent as a\n // JSON envelope so IDs are never silently lost.\n this.sendFrame({\n op: 'publish',\n topic,\n data: Array.from(new Uint8Array(data)),\n ...(messageId === undefined ? {} : { messageId }),\n ...(timestamp === undefined ? {} : { timestamp })\n });\n return;\n }\n if (this.socket?.readyState !== WS_OPEN) {\n this.handlers?.onError(new Error('WebSocket is not open; dropped \"publish\" frame.'));\n return;\n }\n const topicBytes = new TextEncoder().encode(topic);\n if (topicBytes.length > 0xffff) {\n this.handlers?.onError(new Error('WebSocket topic is too long for a binary frame.'));\n return;\n }\n const frame = new Uint8Array(3 + topicBytes.length + data.byteLength);\n frame[0] = 0xc7;\n new DataView(frame.buffer).setUint16(1, topicBytes.length);\n frame.set(topicBytes, 3);\n frame.set(new Uint8Array(data), 3 + topicBytes.length);\n this.socket.send(frame.buffer);\n }\n\n /** Parse a server frame. Only objects carrying a string `topic` are\n * publications; malformed JSON and unknown shapes are ignored so a chatty\n * server cannot crash the message path. */\n private handleMessage(raw: unknown): void {\n let parsed: unknown;\n if (raw instanceof ArrayBuffer) {\n const bytes = new Uint8Array(raw);\n if (bytes[0] !== 0xc7 || bytes.length < 3) return;\n const topicLength = new DataView(raw).getUint16(1);\n if (bytes.length < 3 + topicLength) return;\n const topic = new TextDecoder().decode(bytes.subarray(3, 3 + topicLength));\n const data = bytes.slice(3 + topicLength).buffer;\n this.handlers?.onMessage({ topic, data: data as TData });\n return;\n }\n if (typeof raw !== 'string') return;\n try {\n parsed = JSON.parse(raw);\n } catch {\n this.handlers?.onError(new Error('WebSocket server sent a non-JSON frame.'));\n return;\n }\n if (!parsed || typeof parsed !== 'object') return;\n const publication = parseDataBusPublication<TData>(parsed);\n if (publication) this.handlers?.onMessage(publication as DataBusMessage<TData>);\n }\n}\n\n/** Resolve the platform WebSocket, or null in runtimes without one (SSR/Node). */\nfunction defaultWebSocketFactory(url: string, protocols?: string | string[]): WebSocketLike {\n if (typeof WebSocket === 'undefined') {\n throw new Error('WebSocketTransport requires a WebSocket implementation.');\n }\n return new WebSocket(url, protocols) as unknown as WebSocketLike;\n}\n\n/** Create a CrossTabDataBus backed by a plain WebSocket transport.\n * Cross-tab clustering (owner dedup, sticky routes, failover) works identically\n * to the Centrifuge backend \u2014 only the transport I/O differs. */\nexport function createWebSocketDataBus<TData = unknown>(\n options: CreateWebSocketDataBusOptions<TData>\n): CrossTabDataBus<WebSocketDataBusConfig, TData> {\n const { clusterKey, connection, ...dataBusOptions } = options;\n return new CrossTabDataBus({\n ...dataBusOptions,\n autoStart: true,\n clusterKey: clusterKey ?? connection.url,\n initialConfig: connection,\n transport: new WebSocketTransport<TData>(conn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,GAAG,IAAI,WAAW,CAAC;AACzE,YAAM,OAAO,MAAM,MAAM,IAAI,WAAW,EAAE;AAC1C,WAAK,UAAU,UAAU,EAAE,OAAO,KAAoB,CAAC;AACvD;AAAA,IACF;AACA,QAAI,OAAO,QAAQ,SAAU;AAC7B,QAAI;AACF,eAAS,KAAK,MAAM,GAAG;AAAA,IACzB,QAAQ;AACN,WAAK,UAAU,QAAQ,IAAI,MAAM,yCAAyC,CAAC;AAC3E;AAAA,IACF;AACA,QAAI,CAAC,UAAU,OAAO,WAAW,SAAU;AAC3C,UAAM,cAAc,wBAA+B,MAAM;AACzD,QAAI,YAAa,MAAK,UAAU,UAAU,WAAoC;AAAA,EAChF;AACF;AAGA,SAAS,wBAAwB,KAAa,WAA8C;AAC1F,MAAI,OAAO,cAAc,aAAa;AACpC,UAAM,IAAI,MAAM,yDAAyD;AAAA,EAC3E;AACA,SAAO,IAAI,UAAU,KAAK,SAAS;AACrC;AAKO,SAAS,uBACd,SACgD;AAChD,QAAM,EAAE,YAAY,YAAY,GAAG,eAAe,IAAI;AACtD,SAAO,IAAI,gBAAgB;AAAA,IACzB,GAAG;AAAA,IACH,WAAW;AAAA,IACX,YAAY,cAAc,WAAW;AAAA,IACrC,eAAe;AAAA,IACf,WAAW,IAAI,mBAA0B,UAAU;AAAA,EACrD,CAAC;AACH;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/websocket.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,KAAK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registers a local subscription and returns a cleanup function.
|
|
|
86
86
|
- The current tab only leaves the topic after the last handler is released.
|
|
87
87
|
- Subscriptions are automatically queued when the transport is not yet ready.
|
|
88
88
|
- Wildcard subscriptions: a topic ending in `.*` (`chat.*`) matches any remainder, and `*` matches everything. The pattern is routed, owned, and transport-subscribed as a literal channel; publications tagged with a matching concrete topic (or with the pattern itself) are delivered to wildcard handlers. See `topicMatchesPattern` below.
|
|
89
|
-
- Replay (opt-in): construct the bus with `replay: { maxPerTopic }` and pass `{ replay: true | n }` as the third `subscribe()` argument. `maxPerTopic` must be a positive safe integer. The new handler immediately receives the buffered history (up to `n`, capped by `maxPerTopic`, default 100) with `message.replayed: true`, so late joiners do not miss earlier publications. Only dispatched publications are buffered (a topic with no local subscriber drops them as unowned); buffers are in-memory and cleared when the last handler for the topic unsubscribes. Wildcard subscriptions replay across every buffered topic matching the pattern. For reload/BFCache persistence, pass an optional `persistence` created by `createIndexedDbReplayPersistence({ maxPerTopic })`; persistence is asynchronous and failures are reported through `onError` without breaking live delivery.
|
|
89
|
+
- Replay (opt-in): construct the bus with `replay: { maxPerTopic }` and pass `{ replay: true | n }` as the third `subscribe()` argument. `maxPerTopic` must be a positive safe integer. The new handler immediately receives the buffered history (up to `n`, capped by `maxPerTopic`, default 100) with `message.replayed: true`, so late joiners do not miss earlier publications. Only dispatched publications are buffered (a topic with no local subscriber drops them as unowned); buffers are in-memory and cleared when the last handler for the topic unsubscribes. Wildcard subscriptions replay across every buffered topic matching the pattern. For reload/BFCache persistence, pass an optional `persistence` created by `createIndexedDbReplayPersistence({ maxPerTopic })`; persistence is asynchronous and failures are reported through `onError` without breaking live delivery. Set `retentionMs` to automatically prune durable history via `clearBefore` during hydration and after appends.
|
|
90
90
|
- Persistent replay stores may also implement `clearTopic(topic)`; the bus calls it on final topic unsubscribe. A store may expose `clear()` for application-controlled retention cleanup; `stop()` deliberately preserves durable history for reload/BFCache recovery.
|
|
91
91
|
|
|
92
92
|
### `unsubscribe(topic, handler?)`
|
|
@@ -102,7 +102,11 @@ Prefer using the cleanup function returned by `subscribe` to avoid accidentally
|
|
|
102
102
|
### `publish(topic, data, options?)`
|
|
103
103
|
|
|
104
104
|
```ts
|
|
105
|
-
publish(
|
|
105
|
+
publish(
|
|
106
|
+
topic: string,
|
|
107
|
+
data: unknown,
|
|
108
|
+
options?: { messageId?: string; timestamp?: number }
|
|
109
|
+
): void
|
|
106
110
|
```
|
|
107
111
|
|
|
108
112
|
Routes the publish operation to the current topic owner; uses the current Worker when no valid route exists.
|
|
@@ -113,7 +117,17 @@ When the owning Worker is a remote Tab and the publish control message cannot be
|
|
|
113
117
|
|
|
114
118
|
Incoming messages may include a caller/server supplied `messageId`. Enable bounded duplicate suppression with `dedup: { maxEntries, ttlMs }`; repeated IDs within the window are ignored. This is disabled by default and does not provide an exactly-once server guarantee.
|
|
115
119
|
|
|
116
|
-
When supplied, `options.messageId`
|
|
120
|
+
When supplied, `options.messageId` and `options.timestamp` are propagated through cross-tab routing, Worker boundaries, and supported transports. The server must echo or otherwise preserve them for inbound deduplication and replay retention.
|
|
121
|
+
|
|
122
|
+
`DataBusMessage` and `DataBusPublication` expose the same optional metadata.
|
|
123
|
+
`DataBusPublicationEnvelope<TData>` is the canonical JSON envelope type:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
{
|
|
127
|
+
op: 'publication',
|
|
128
|
+
publication: { topic, data, messageId?, timestamp? }
|
|
129
|
+
}
|
|
130
|
+
```
|
|
117
131
|
|
|
118
132
|
### `clearReplay()`
|
|
119
133
|
|
|
@@ -123,6 +137,10 @@ clearReplay(): Promise<void>
|
|
|
123
137
|
|
|
124
138
|
Clears in-memory replay buffers and invokes the persistence adapter's optional `clear()` hook. Useful for retention policies, logout, or tenant switching. Durable history is otherwise preserved across `stop()`.
|
|
125
139
|
|
|
140
|
+
`clearReplayTopic(topic)` applies the same cleanup to one exact topic. `getDedupStats()` returns bounded counters (`enabled`, `tracked`, `accepted`, `suppressed`), and `resetDedup()` clears remembered IDs and counters without changing configuration.
|
|
141
|
+
|
|
142
|
+
`clearReplayBefore(timestamp)` removes entries older than an epoch-millisecond cutoff from memory and from adapters that implement optional `clearBefore(timestamp)`. Incoming messages receive a bus timestamp when the transport does not provide one.
|
|
143
|
+
|
|
126
144
|
### `onStatus(handler)`
|
|
127
145
|
|
|
128
146
|
```ts
|
|
@@ -183,7 +201,7 @@ trace: {
|
|
|
183
201
|
}
|
|
184
202
|
```
|
|
185
203
|
|
|
186
|
-
Low-frequency event types include `lifecycle`, `status`, `subscription`, `coordination`, and `error`; high-frequency data is output as `message_metrics` per window, containing receive/dispatch counts, active topic count, and dispatch latency aggregates (`dispatchSamples`, `dispatchAvgMs`, `dispatchP50Ms`, `dispatchP95Ms`, `dispatchMaxMs`). All public events use a fixed structure and do not contain raw topics, message payloads, connection addresses, or error bodies. Errors thrown by the sink are isolated and do not interrupt message dispatch, but are output to `console.warn` to facilitate diagnosing configuration issues. The sink should ideally not throw — capture expected error conditions in the event data rather than raising exceptions.
|
|
204
|
+
Low-frequency event types include `lifecycle`, `status`, `subscription`, `coordination`, and `error`; high-frequency data is output as `message_metrics` per window, containing receive/dispatch counts, active topic count, and dispatch latency aggregates (`dispatchSamples`, `dispatchAvgMs`, `dispatchP50Ms`, `dispatchP95Ms`, `dispatchMaxMs`), and deduplication outcomes (`dedupAccepted`, `dedupSuppressed`). All public events use a fixed structure and do not contain raw topics, message payloads, connection addresses, or error bodies. Errors thrown by the sink are isolated and do not interrupt message dispatch, but are output to `console.warn` to facilitate diagnosing configuration issues. The sink should ideally not throw — capture expected error conditions in the event data rather than raising exceptions.
|
|
187
205
|
|
|
188
206
|
### `stop()`
|
|
189
207
|
|
|
@@ -285,8 +303,11 @@ Implements `DataBusTransport`. Connection lifecycle maps to the DataBus status v
|
|
|
285
303
|
|
|
286
304
|
JSON text frames:
|
|
287
305
|
|
|
288
|
-
- client → server: `{"op":"subscribe"|"unsubscribe"|"publish","topic":"...","data"
|
|
289
|
-
- server → client: `{"topic":"...","data"
|
|
306
|
+
- client → server: `{"op":"subscribe"|"unsubscribe"|"publish","topic":"...","data":...,"messageId"?:...,"timestamp"?:...}`
|
|
307
|
+
- server → client (canonical): `{"op":"publication","publication":{"topic":"...","data":...,"messageId"?:...,"timestamp"?:...}}`
|
|
308
|
+
- server → client (legacy, still accepted): `{"topic":"...","data":...}`
|
|
309
|
+
|
|
310
|
+
Frames without a string publication `topic` are ignored; malformed JSON surfaces via `handlers.onError` without throwing.
|
|
290
311
|
|
|
291
312
|
A pattern-aware server may deliver publications tagged with the concrete topic (recommended); publications tagged with the pattern itself are delivered through the exact-match path.
|
|
292
313
|
|
package/docs/architecture.md
CHANGED
|
@@ -384,6 +384,8 @@ A publication travels publisher → current Topic owner → transport/server →
|
|
|
384
384
|
4. The owner broadcasts `EVENT/DATABUS_PUBLICATION` over the BroadcastChannel and, if its own Tab also has a local subscription, dispatches once directly. BroadcastChannel never echoes to the sender, so there is no duplicate dispatch.
|
|
385
385
|
5. Every other Tab receives the `EVENT` but invokes its local handlers only when it holds a `subscriber:{topicKey}:{tabId}` record for that topic; Tabs without a local subscription drop the message.
|
|
386
386
|
|
|
387
|
+
Optional publication metadata (`messageId` and `timestamp`) follows the same path as the payload: `CONTROL/PUBLISH` → transport/server → `DataBusMessage` → `EVENT` fan-out. It is never written to coordination storage. Transports normalize legacy payloads and the canonical `DataBusPublicationEnvelope` before the three dispatch gates.
|
|
388
|
+
|
|
387
389
|
At the transport layer, a Centrifuge client can emit a publication both on the `client` object and on the matching `Subscription` object. To avoid dispatching the same server publication twice, the Centrifuge session only handles the client-level `publication` for topics that have **no active client-side subscription** (server-side subscriptions); topics with an active subscription are delivered solely through the subscription-level listener.
|
|
388
390
|
|
|
389
391
|
```mermaid
|
|
@@ -407,7 +409,11 @@ sequenceDiagram
|
|
|
407
409
|
|
|
408
410
|
BroadcastChannel does not echo to its sender, so the owner receives no `EVENT` back for its own broadcast — its local dispatch is the only local delivery.
|
|
409
411
|
|
|
410
|
-
Publications are not written to localStorage. Message data only
|
|
412
|
+
Publications are not written to localStorage. Message data and publication metadata only exist in the BroadcastChannel in-memory event and within the transport; batch writes only cover coordination metadata.
|
|
413
|
+
|
|
414
|
+
### Service Worker boundary
|
|
415
|
+
|
|
416
|
+
The SDK intentionally does not host a real-time transport in a Service Worker. Service Workers can be terminated between events, do not provide a durable foreground connection lifetime, and impose browser-specific restrictions around long-lived WebSockets. A future adapter would need an explicit connection owner, client wake-up protocol, reconnection policy, and durable handoff semantics; until those are standardized and covered by browser tests, Dedicated/Shared Worker transports remain the supported runtime models.
|
|
411
417
|
|
|
412
418
|
### Dispatch flow: three gates
|
|
413
419
|
|