cross-tab-worker-databus 0.2.1 → 0.4.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 +31 -0
- package/README.md +3 -0
- package/README.zh.md +3 -0
- package/dist/centrifuge.js +20 -3
- package/dist/centrifuge.js.map +2 -2
- package/dist/chunk-5WRI5ZAA.js +31 -0
- package/dist/chunk-5WRI5ZAA.js.map +7 -0
- package/dist/{chunk-LBXREMZA.js → chunk-ZGQRELIV.js} +91 -5
- package/dist/chunk-ZGQRELIV.js.map +7 -0
- package/dist/cjs/centrifuge.cjs +2205 -0
- package/dist/cjs/centrifuge.cjs.map +7 -0
- package/dist/cjs/hooks.cjs +1975 -0
- package/dist/cjs/hooks.cjs.map +7 -0
- package/dist/cjs/index.cjs +1865 -0
- package/dist/cjs/index.cjs.map +7 -0
- package/dist/core/cluster.d.ts +2 -1
- package/dist/core/cluster.d.ts.map +1 -1
- package/dist/core/data-bus.d.ts +28 -2
- package/dist/core/data-bus.d.ts.map +1 -1
- package/dist/core/routing.d.ts +9 -0
- package/dist/core/routing.d.ts.map +1 -1
- package/dist/core/types.d.ts +3 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/hooks.d.ts +31 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +1946 -0
- package/dist/hooks.js.map +7 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +113 -3
- package/dist/index.js.map +3 -3
- package/dist/websocket.d.ts +86 -0
- package/dist/websocket.d.ts.map +1 -0
- package/docs/api.md +75 -0
- package/docs/getting-started.md +6 -0
- package/docs/transports.md +27 -0
- package/docs/zh/api.md +75 -0
- package/docs/zh/getting-started.md +6 -0
- package/docs/zh/transports.md +24 -0
- package/package.json +24 -7
- package/dist/chunk-LBXREMZA.js.map +0 -7
package/docs/api.md
CHANGED
|
@@ -19,6 +19,17 @@ import {
|
|
|
19
19
|
CentrifugeWorkerTransport,
|
|
20
20
|
createCentrifugeDataBus
|
|
21
21
|
} from 'cross-tab-worker-databus/centrifuge';
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
WebSocketTransport,
|
|
25
|
+
createWebSocketDataBus
|
|
26
|
+
} from 'cross-tab-worker-databus';
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
useCrossTabDataBus,
|
|
30
|
+
useCrossTabStatus,
|
|
31
|
+
useCrossTabSubscription
|
|
32
|
+
} from 'cross-tab-worker-databus/hooks';
|
|
22
33
|
```
|
|
23
34
|
|
|
24
35
|
Business integration should prefer `CrossTabDataBus` or `createCentrifugeDataBus`. `WorkerClusterRuntime` is an advanced coordination API.
|
|
@@ -68,6 +79,8 @@ Registers a local subscription and returns a cleanup function.
|
|
|
68
79
|
- The first handler in the current tab registers a cluster subscription.
|
|
69
80
|
- The current tab only leaves the topic after the last handler is released.
|
|
70
81
|
- Subscriptions are automatically queued when the transport is not yet ready.
|
|
82
|
+
- 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.
|
|
83
|
+
- 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.
|
|
71
84
|
|
|
72
85
|
### `unsubscribe(topic, handler?)`
|
|
73
86
|
|
|
@@ -216,6 +229,66 @@ Available options:
|
|
|
216
229
|
- `workerFactory`: custom Dedicated Worker loading method
|
|
217
230
|
- `sharedWorkerFactory`: custom SharedWorker loading method
|
|
218
231
|
|
|
232
|
+
## WebSocket Transport Backend
|
|
233
|
+
|
|
234
|
+
A dependency-free transport over a plain WebSocket. Any server speaking the JSON frame protocol below can back the same cross-tab clustering stack (owner dedup, sticky routes, failover) as the Centrifuge backend.
|
|
235
|
+
|
|
236
|
+
### `createWebSocketDataBus<TData>(options)`
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
createWebSocketDataBus<TData>(options): CrossTabDataBus<WebSocketDataBusConfig, TData>
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Creates an auto-starting WebSocket DataBus. Defaults: `clusterKey = connection.url`.
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
const bus = createWebSocketDataBus({
|
|
246
|
+
connection: { url: 'wss://example.test/ws' },
|
|
247
|
+
trace: { enabled: true, sink: event => console.log(event) }
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### `WebSocketTransport<TData>`
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
new WebSocketTransport<TData>(connection: WebSocketDataBusConfig)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Implements `DataBusTransport`. Connection lifecycle maps to the DataBus status vocabulary: socket `open` → `connected`, `close` → `disconnected`, `error` → `error` (which triggers DataBus auto-recovery). Subscriptions are re-asserted when a socket reopens in place. Frames dropped while the socket is not open are reported via `handlers.onError`; reopening re-sends subscribe frames.
|
|
258
|
+
|
|
259
|
+
`WebSocketDataBusConfig` fields:
|
|
260
|
+
|
|
261
|
+
- `url` — WebSocket endpoint.
|
|
262
|
+
- `protocols` — optional subprotocol(s) for the handshake.
|
|
263
|
+
- `webSocketFactory` — optional factory `(url, protocols) => WebSocketLike` for tests and non-browser runtimes (defaults to the global `WebSocket`).
|
|
264
|
+
|
|
265
|
+
### Wire protocol
|
|
266
|
+
|
|
267
|
+
JSON text frames:
|
|
268
|
+
|
|
269
|
+
- client → server: `{"op":"subscribe"|"unsubscribe"|"publish","topic":"...","data":...}`
|
|
270
|
+
- server → client: `{"topic":"...","data":...}` for publications. Frames without a string `topic` field are ignored; malformed JSON surfaces via `handlers.onError` without throwing.
|
|
271
|
+
|
|
272
|
+
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.
|
|
273
|
+
|
|
274
|
+
## React Hooks (`cross-tab-worker-databus/hooks`)
|
|
275
|
+
|
|
276
|
+
React (>= 18) is an optional peer dependency; this entry is separate so non-React consumers never load it.
|
|
277
|
+
|
|
278
|
+
### `useCrossTabDataBus(create, deps?)`
|
|
279
|
+
|
|
280
|
+
Creates a bus for the component's lifetime: created on mount, stopped on unmount. StrictMode-safe — the double-invoked effect exercises the same stop/recreate path as BFCache suspend/resume. Returns the active bus or `null` before the first effect (SSR / initial render).
|
|
281
|
+
|
|
282
|
+
Pass a fresh bus per effect run (an inline factory); key recreation through `deps`.
|
|
283
|
+
|
|
284
|
+
### `useCrossTabSubscription(bus, topic, handler)`
|
|
285
|
+
|
|
286
|
+
Attaches a message handler with automatic cleanup. The handler is read through a ref on each delivery, so inline closures do not cause resubscription across re-renders. Queues while `bus` is `null` or the transport is not ready.
|
|
287
|
+
|
|
288
|
+
### `useCrossTabStatus(bus)`
|
|
289
|
+
|
|
290
|
+
Mirrors `bus.onStatus()` into React state and reads the current value synchronously whenever the bus identity changes. Returns `'connecting' | 'connected' | 'disconnected' | 'error'`.
|
|
291
|
+
|
|
219
292
|
## `WorkerClusterRuntime`
|
|
220
293
|
|
|
221
294
|
Advanced API responsible for Worker registration, heartbeat, visibility, routing, BroadcastChannel protocol, and migration. Business modules should not operate on it directly.
|
|
@@ -257,5 +330,7 @@ Selects the actual backend based on `WorkerMode` and capability detection, retur
|
|
|
257
330
|
- `selectLeastLoadedWorker`
|
|
258
331
|
- `selectRebalanceTarget`
|
|
259
332
|
- `hasActiveOwner`
|
|
333
|
+
- `isWildcardTopic(pattern)`
|
|
334
|
+
- `topicMatchesPattern(pattern, topic)` — wildcard matching used by subscriptions: `chat.*` matches `chat.room.1` (segment-boundary prefix), `*` matches everything
|
|
260
335
|
|
|
261
336
|
These pure functions are primarily used for testing, diagnostics, and custom coordination strategies.
|
package/docs/getting-started.md
CHANGED
|
@@ -14,6 +14,8 @@ The package provides the following entry points:
|
|
|
14
14
|
- `cross-tab-worker-databus/centrifuge`: the built-in Centrifuge Worker transport
|
|
15
15
|
- `cross-tab-worker-databus/centrifuge.worker`: the Dedicated Worker build artifact, loaded by default by the built-in factory; typically no need to reference it directly
|
|
16
16
|
- `cross-tab-worker-databus/centrifuge.shared.worker`: the SharedWorker build artifact, loaded by default by the built-in factory; typically no need to reference it directly
|
|
17
|
+
- `cross-tab-worker-databus/hooks`: optional React hooks adapter (`useCrossTabDataBus`, `useCrossTabSubscription`, `useCrossTabStatus`); React (>= 18) is an optional peer dependency
|
|
18
|
+
- `cross-tab-worker-databus` also exports a zero-dependency native `WebSocketTransport` / `createWebSocketDataBus` for servers that speak plain WebSockets
|
|
17
19
|
|
|
18
20
|
The `cross-tab-worker-databus/centrifuge` entry point relies on the optional peer dependency `centrifuge` (^5.5.3). Install it alongside this package when using the built-in Centrifuge transport: `pnpm add centrifuge`.
|
|
19
21
|
|
|
@@ -21,6 +23,10 @@ The `cross-tab-worker-databus/centrifuge` entry point relies on the optional pee
|
|
|
21
23
|
|
|
22
24
|
It is recommended to create an instance in the application's infrastructure layer and have other modules import it directly. This way, business modules within the same Tab share the Worker, connection, and Topic references.
|
|
23
25
|
|
|
26
|
+
Both module formats are published: ESM (`import`) and CommonJS (`require` / `dist/cjs`), so CJS bundler configurations and `require()` callers work out of the box.
|
|
27
|
+
|
|
28
|
+
When consuming the Centrifuge entry as CommonJS in a browser, provide an explicit `workerFactory` (or `sharedWorkerFactory`) if your bundler does not preserve `import.meta.url`; the default module-relative Worker URL is only available in ESM output.
|
|
29
|
+
|
|
24
30
|
```ts
|
|
25
31
|
import { createCentrifugeDataBus } from 'cross-tab-worker-databus/centrifuge';
|
|
26
32
|
|
package/docs/transports.md
CHANGED
|
@@ -125,8 +125,35 @@ opt in:
|
|
|
125
125
|
}
|
|
126
126
|
```
|
|
127
127
|
|
|
128
|
+
## Built-in: native WebSocket backend
|
|
129
|
+
|
|
130
|
+
The package ships a second real backend, `WebSocketTransport`, proving the
|
|
131
|
+
contract above with zero dependencies. Use it when your server already speaks
|
|
132
|
+
WebSockets and you do not need Centrifugo features.
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import { createWebSocketDataBus } from 'cross-tab-worker-databus';
|
|
136
|
+
|
|
137
|
+
const bus = createWebSocketDataBus({
|
|
138
|
+
connection: { url: 'wss://example.test/ws' }
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Wire protocol (JSON text frames):
|
|
143
|
+
|
|
144
|
+
- client → server: `{"op":"subscribe"|"unsubscribe"|"publish","topic":"...","data":...}`
|
|
145
|
+
- server → client: publications are `{"topic":"...","data":...}`. Frames
|
|
146
|
+
without a string `topic` are ignored; malformed JSON is reported through
|
|
147
|
+
`handlers.onError` without throwing.
|
|
148
|
+
|
|
149
|
+
Lifecycle mapping: `open` → `connected`, `close` → `disconnected`,
|
|
150
|
+
`error` → `error` (DataBus auto-recovery). Subscribe frames are re-sent when
|
|
151
|
+
the socket reopens in place. A pattern-aware server may tag publications with
|
|
152
|
+
the concrete topic — see wildcard subscriptions in [api.md](./api.md).
|
|
153
|
+
|
|
128
154
|
## Factory entry point
|
|
129
155
|
|
|
156
|
+
|
|
130
157
|
Provide a `create<Backend>DataBus(options)` factory that wires the transport
|
|
131
158
|
into a `CrossTabDataBus`, mirroring `createCentrifugeDataBus`. This is the
|
|
132
159
|
surface most consumers use; it should accept the connection config, cluster
|
package/docs/zh/api.md
CHANGED
|
@@ -19,6 +19,17 @@ import {
|
|
|
19
19
|
CentrifugeWorkerTransport,
|
|
20
20
|
createCentrifugeDataBus
|
|
21
21
|
} from 'cross-tab-worker-databus/centrifuge';
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
WebSocketTransport,
|
|
25
|
+
createWebSocketDataBus
|
|
26
|
+
} from 'cross-tab-worker-databus';
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
useCrossTabDataBus,
|
|
30
|
+
useCrossTabStatus,
|
|
31
|
+
useCrossTabSubscription
|
|
32
|
+
} from 'cross-tab-worker-databus/hooks';
|
|
22
33
|
```
|
|
23
34
|
|
|
24
35
|
业务接入优先使用 `CrossTabDataBus` 或 `createCentrifugeDataBus`。`WorkerClusterRuntime` 属于高级协调 API。
|
|
@@ -68,6 +79,8 @@ subscribe(
|
|
|
68
79
|
- 当前 Tab 第一个 handler 会登记集群订阅。
|
|
69
80
|
- 最后一个 handler 释放后,当前 Tab 才退出该 Topic。
|
|
70
81
|
- transport 尚未 ready 时订阅自动排队。
|
|
82
|
+
- 通配符订阅:以 `.*` 结尾的 Topic(如 `chat.*`)匹配任意后缀,`*` 匹配全部。pattern 以字面量参与路由、归属与传输订阅;携带匹配的具体 topic(或 pattern 本身)的发布都会投递给通配 handler。匹配规则见下方 `topicMatchesPattern`。
|
|
83
|
+
- 重放(可选):构造 bus 时传 `replay: { maxPerTopic }` 开启缓冲,`maxPerTopic` 必须是正安全整数;`subscribe()` 第三个参数传 `{ replay: true | n }` 后,新 handler 会立即收到缓冲历史(最多 `n` 条,受 `maxPerTopic` 上限约束,默认 100),消息带 `message.replayed: true` 标记——晚加入的 handler 不会错过更早的发布。只有被分发过的消息才入缓冲(无本地订阅者的 topic 会被 owner 丢弃);缓冲仅存内存,该 topic 最后一个 handler 退订时清空。通配订阅会对所有匹配 pattern 的已缓冲 topic 做回放。
|
|
71
84
|
|
|
72
85
|
### `unsubscribe(topic, handler?)`
|
|
73
86
|
|
|
@@ -216,6 +229,66 @@ const transport = new CentrifugeWorkerTransport({
|
|
|
216
229
|
- `workerFactory`:自定义 Dedicated Worker 加载方式
|
|
217
230
|
- `sharedWorkerFactory`:自定义 SharedWorker 加载方式
|
|
218
231
|
|
|
232
|
+
## WebSocket 传输后端
|
|
233
|
+
|
|
234
|
+
基于原生 WebSocket 的零依赖传输。任何实现下列 JSON 帧协议的服务器都能驱动与 Centrifuge 后端相同的跨 Tab 集群栈(owner 去重、粘性路由、故障转移)。
|
|
235
|
+
|
|
236
|
+
### `createWebSocketDataBus<TData>(options)`
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
createWebSocketDataBus<TData>(options): CrossTabDataBus<WebSocketDataBusConfig, TData>
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
创建自动启动的 WebSocket DataBus。默认值:`clusterKey = connection.url`。
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
const bus = createWebSocketDataBus({
|
|
246
|
+
connection: { url: 'wss://example.test/ws' },
|
|
247
|
+
trace: { enabled: true, sink: event => console.log(event) }
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### `WebSocketTransport<TData>`
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
new WebSocketTransport<TData>(connection: WebSocketDataBusConfig)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
实现 `DataBusTransport`。连接生命周期直接映射 DataBus 状态:socket `open` → `connected`,`close` → `disconnected`,`error` → `error`(触发 DataBus 自动恢复)。socket 原地重连时会自动重发订阅;socket 未打开期间被丢弃的帧通过 `handlers.onError` 上报,重开后自动补发订阅帧。
|
|
258
|
+
|
|
259
|
+
`WebSocketDataBusConfig` 字段:
|
|
260
|
+
|
|
261
|
+
- `url` — WebSocket 端点。
|
|
262
|
+
- `protocols` — 可选的握手子协议。
|
|
263
|
+
- `webSocketFactory` — 可选工厂 `(url, protocols) => WebSocketLike`,用于测试与非浏览器运行时(默认使用全局 `WebSocket`)。
|
|
264
|
+
|
|
265
|
+
### 线协议
|
|
266
|
+
|
|
267
|
+
JSON 文本帧:
|
|
268
|
+
|
|
269
|
+
- client → server:`{"op":"subscribe"|"unsubscribe"|"publish","topic":"...","data":...}`
|
|
270
|
+
- server → client:发布为 `{"topic":"...","data":...}`。没有字符串 `topic` 字段的帧被忽略;非法 JSON 通过 `handlers.onError` 上报而不会抛出。
|
|
271
|
+
|
|
272
|
+
支持 pattern 的服务器建议以具体 topic 标注发布;以 pattern 本身标注的发布走精确匹配路径投递。
|
|
273
|
+
|
|
274
|
+
## React Hooks(`cross-tab-worker-databus/hooks`)
|
|
275
|
+
|
|
276
|
+
React(>= 18)是可选 peer 依赖;独立入口保证非 React 消费者不会加载它。
|
|
277
|
+
|
|
278
|
+
### `useCrossTabDataBus(create, deps?)`
|
|
279
|
+
|
|
280
|
+
创建随组件生命周期存活的 bus:挂载时创建,卸载时停止。StrictMode 安全——effect 双调用走的是与 BFCache 挂起/恢复相同的停止/重建路径。返回当前 bus;首次 effect 之前(SSR / 初始渲染)为 `null`。
|
|
281
|
+
|
|
282
|
+
每次 effect 返回一个全新 bus(内联工厂即可);需要重建时通过 `deps` 控制。
|
|
283
|
+
|
|
284
|
+
### `useCrossTabSubscription(bus, topic, handler)`
|
|
285
|
+
|
|
286
|
+
登记消息 handler 并自动清理。handler 经由 ref 在每次投递时读取,因此内联闭包不会导致重渲染时的重订阅。`bus` 为 `null` 或 transport 未 ready 时自动排队。
|
|
287
|
+
|
|
288
|
+
### `useCrossTabStatus(bus)`
|
|
289
|
+
|
|
290
|
+
把 `bus.onStatus()` 镜像为 React 状态,bus 身份变化时同步读取当前值。返回 `'connecting' | 'connected' | 'disconnected' | 'error'`。
|
|
291
|
+
|
|
219
292
|
## `WorkerClusterRuntime`
|
|
220
293
|
|
|
221
294
|
高级 API,负责 Worker 注册、心跳、可见性、路由、BroadcastChannel 协议和迁移。业务模块不应直接操作它。
|
|
@@ -257,5 +330,7 @@ const transport = new CentrifugeWorkerTransport({
|
|
|
257
330
|
- `selectLeastLoadedWorker`
|
|
258
331
|
- `selectRebalanceTarget`
|
|
259
332
|
- `hasActiveOwner`
|
|
333
|
+
- `isWildcardTopic(pattern)`
|
|
334
|
+
- `topicMatchesPattern(pattern, topic)` — 订阅使用的通配匹配:`chat.*` 匹配 `chat.room.1`(按段前缀),`*` 匹配全部
|
|
260
335
|
|
|
261
336
|
这些纯函数主要用于测试、诊断和自定义协调策略。
|
|
@@ -14,6 +14,8 @@ pnpm add cross-tab-worker-databus
|
|
|
14
14
|
- `cross-tab-worker-databus/centrifuge`:内置 Centrifuge Worker transport
|
|
15
15
|
- `cross-tab-worker-databus/centrifuge.worker`:Dedicated Worker 构建产物,默认由内置 factory 加载,通常无需直接引用
|
|
16
16
|
- `cross-tab-worker-databus/centrifuge.shared.worker`:SharedWorker 构建产物,默认由内置 factory 加载,通常无需直接引用
|
|
17
|
+
- `cross-tab-worker-databus/hooks`:可选的 React hooks 适配层(`useCrossTabDataBus`、`useCrossTabSubscription`、`useCrossTabStatus`);React(>= 18)为可选 peer 依赖
|
|
18
|
+
- `cross-tab-worker-databus` 同时导出零依赖的原生 `WebSocketTransport` / `createWebSocketDataBus`,适用于本身使用 WebSocket 的服务器
|
|
17
19
|
|
|
18
20
|
`cross-tab-worker-databus/centrifuge` 入口依赖可选 peer dependency `centrifuge`(^5.5.3)。使用内置 Centrifuge transport 时请一并安装:`pnpm add centrifuge`。
|
|
19
21
|
|
|
@@ -21,6 +23,10 @@ pnpm add cross-tab-worker-databus
|
|
|
21
23
|
|
|
22
24
|
建议在应用基础设施层创建一个实例,其他模块直接导入。这样同一 Tab 内的业务模块会共享 Worker、连接和 Topic 引用。
|
|
23
25
|
|
|
26
|
+
包同时发布 ESM 与 CommonJS 双格式:`import` 与 `require()`(`dist/cjs`)均可直接使用,CJS bundler 配置无需额外处理。
|
|
27
|
+
|
|
28
|
+
在浏览器中以 CommonJS 使用 Centrifuge 入口时,如果 bundler 不保留 `import.meta.url`,请显式提供 `workerFactory`(或 `sharedWorkerFactory`);默认的模块相对 Worker URL 仅在 ESM 产物中可用。
|
|
29
|
+
|
|
24
30
|
```ts
|
|
25
31
|
import { createCentrifugeDataBus } from 'cross-tab-worker-databus/centrifuge';
|
|
26
32
|
|
package/docs/zh/transports.md
CHANGED
|
@@ -114,8 +114,32 @@ shared worker):
|
|
|
114
114
|
}
|
|
115
115
|
```
|
|
116
116
|
|
|
117
|
+
## 内置:原生 WebSocket 后端
|
|
118
|
+
|
|
119
|
+
包内自带第二个真实后端 `WebSocketTransport`,以零依赖验证了上述契约。当你的
|
|
120
|
+
服务器本身使用 WebSocket、且不需要 Centrifugo 特性时可以直接使用。
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { createWebSocketDataBus } from 'cross-tab-worker-databus';
|
|
124
|
+
|
|
125
|
+
const bus = createWebSocketDataBus({
|
|
126
|
+
connection: { url: 'wss://example.test/ws' }
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
线协议(JSON 文本帧):
|
|
131
|
+
|
|
132
|
+
- client → server:`{"op":"subscribe"|"unsubscribe"|"publish","topic":"...","data":...}`
|
|
133
|
+
- server → client:发布为 `{"topic":"...","data":...}`。没有字符串 `topic` 的帧
|
|
134
|
+
会被忽略;非法 JSON 通过 `handlers.onError` 上报而不会抛出。
|
|
135
|
+
|
|
136
|
+
生命周期映射:`open` → `connected`,`close` → `disconnected`,`error` → `error`
|
|
137
|
+
(触发 DataBus 自动恢复)。socket 原地重连时自动重发订阅帧。支持 pattern 的
|
|
138
|
+
服务器可以以具体 topic 标注发布——见 [api.md](../api.md) 中的通配符订阅。
|
|
139
|
+
|
|
117
140
|
## 工厂入口
|
|
118
141
|
|
|
142
|
+
|
|
119
143
|
提供一个 `create<Backend>DataBus(options)` 工厂,把 transport 接入
|
|
120
144
|
`CrossTabDataBus`,与 `createCentrifugeDataBus` 对称。这是大多数消费者使用的
|
|
121
145
|
界面;它应接受连接配置、cluster key(默认为连接 URL),并把 trace /
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cross-tab-worker-databus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Framework-agnostic cross-tab data bus with Dedicated/Shared Worker clustering and Centrifuge support.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Sun1090",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/Sun1090/cross-tab-worker-databus.git"
|
|
10
|
+
"url": "git+https://github.com/Sun1090/cross-tab-worker-databus.git"
|
|
11
11
|
},
|
|
12
12
|
"homepage": "https://github.com/Sun1090/cross-tab-worker-databus#readme",
|
|
13
13
|
"bugs": {
|
|
@@ -29,17 +29,19 @@
|
|
|
29
29
|
"README.md",
|
|
30
30
|
"LICENSE"
|
|
31
31
|
],
|
|
32
|
-
"main": "./dist/index.
|
|
32
|
+
"main": "./dist/cjs/index.cjs",
|
|
33
33
|
"module": "./dist/index.js",
|
|
34
34
|
"types": "./dist/index.d.ts",
|
|
35
35
|
"exports": {
|
|
36
36
|
".": {
|
|
37
37
|
"types": "./dist/index.d.ts",
|
|
38
|
-
"import": "./dist/index.js"
|
|
38
|
+
"import": "./dist/index.js",
|
|
39
|
+
"require": "./dist/cjs/index.cjs"
|
|
39
40
|
},
|
|
40
41
|
"./centrifuge": {
|
|
41
42
|
"types": "./dist/centrifuge.d.ts",
|
|
42
|
-
"import": "./dist/centrifuge.js"
|
|
43
|
+
"import": "./dist/centrifuge.js",
|
|
44
|
+
"require": "./dist/cjs/centrifuge.cjs"
|
|
43
45
|
},
|
|
44
46
|
"./centrifuge.worker": {
|
|
45
47
|
"types": "./dist/workers/centrifuge.worker.d.ts",
|
|
@@ -49,6 +51,11 @@
|
|
|
49
51
|
"types": "./dist/workers/centrifuge.shared.worker.d.ts",
|
|
50
52
|
"default": "./dist/centrifuge.shared.worker.js"
|
|
51
53
|
},
|
|
54
|
+
"./hooks": {
|
|
55
|
+
"types": "./dist/hooks.d.ts",
|
|
56
|
+
"import": "./dist/hooks.js",
|
|
57
|
+
"require": "./dist/cjs/hooks.cjs"
|
|
58
|
+
},
|
|
52
59
|
"./package.json": "./package.json"
|
|
53
60
|
},
|
|
54
61
|
"sideEffects": [
|
|
@@ -57,32 +64,42 @@
|
|
|
57
64
|
],
|
|
58
65
|
"scripts": {
|
|
59
66
|
"build": "node scripts/build.mjs",
|
|
60
|
-
"check": "pnpm typecheck && pnpm
|
|
67
|
+
"check": "pnpm typecheck && pnpm build && pnpm test",
|
|
61
68
|
"examples": "node scripts/serve-examples.mjs",
|
|
62
69
|
"lint": "eslint .",
|
|
63
70
|
"test": "vitest run",
|
|
64
71
|
"test:watch": "vitest",
|
|
65
72
|
"test:coverage": "vitest run --coverage",
|
|
73
|
+
"bench": "vitest bench --run",
|
|
66
74
|
"test:e2e": "pnpm build && playwright test",
|
|
67
75
|
"typecheck": "tsc --noEmit",
|
|
68
76
|
"prepublishOnly": "pnpm check"
|
|
69
77
|
},
|
|
70
78
|
"peerDependencies": {
|
|
71
|
-
"centrifuge": "^5.5.3"
|
|
79
|
+
"centrifuge": "^5.5.3",
|
|
80
|
+
"react": ">=18"
|
|
72
81
|
},
|
|
73
82
|
"peerDependenciesMeta": {
|
|
74
83
|
"centrifuge": {
|
|
75
84
|
"optional": true
|
|
85
|
+
},
|
|
86
|
+
"react": {
|
|
87
|
+
"optional": true
|
|
76
88
|
}
|
|
77
89
|
},
|
|
78
90
|
"devDependencies": {
|
|
79
91
|
"@eslint/js": "^9.39.5",
|
|
80
92
|
"@playwright/test": "^1.62.1",
|
|
93
|
+
"@testing-library/react": "^16.3.3",
|
|
81
94
|
"@types/node": "^24.0.0",
|
|
95
|
+
"@types/react": "^18.3.31",
|
|
82
96
|
"@vitest/coverage-v8": "^3.2.7",
|
|
83
97
|
"esbuild": "^0.25.0",
|
|
84
98
|
"eslint": "^9.39.4",
|
|
85
99
|
"globals": "^17.11.0",
|
|
100
|
+
"jsdom": "^25.0.1",
|
|
101
|
+
"react": "^18.3.1",
|
|
102
|
+
"react-dom": "^18.3.1",
|
|
86
103
|
"typescript": "^5.9.0",
|
|
87
104
|
"typescript-eslint": "^8.68.0",
|
|
88
105
|
"vitest": "^3.2.0"
|