@laplace.live/ws 7.1.8 → 7.1.10

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.
@@ -1,301 +0,0 @@
1
- import { BilibiliInternal } from '@laplace.live/internal';
2
-
3
- /**
4
- * Platform-specific inflate/decompress implementations injected into the
5
- * decoder at construction time.
6
- *
7
- * - **Node / Bun**: backed by `node:zlib` (`inflate` + `brotliDecompress`).
8
- * - **Browser**: backed by native `DecompressionStream` (inflate) + a bundled JS Brotli decoder.
9
- */
10
- type Inflates = {
11
- inflateAsync: (b: Uint8Array<ArrayBuffer>) => Uint8Array | Promise<Uint8Array>;
12
- brotliDecompressAsync: (b: Uint8Array<ArrayBuffer>) => Uint8Array | Promise<Uint8Array>;
13
- };
14
-
15
- /**
16
- * A typed {@link Event} that carries an arbitrary data payload.
17
- *
18
- * Used throughout the library to deliver structured messages such as
19
- * heartbeat counts, danmaku payloads, and other server-pushed data.
20
- *
21
- * Also used internally with `LaplaceRawEvent<Event>` as a catch-all meta-event
22
- * (type `"event"`) to forward all events through {@link KeepLive}.
23
- *
24
- * @typeParam T - The type of the data payload attached to this event.
25
- *
26
- * @example
27
- * ```ts
28
- * live.addEventListener('heartbeat', (e) => {
29
- * console.log('online:', e.data)
30
- * })
31
- * ```
32
- */
33
- declare class LaplaceRawEvent<T> extends Event {
34
- data: T;
35
- constructor(type: string, data: T);
36
- }
37
- /**
38
- * Maps known event names to their event types for typed `addEventListener`.
39
- *
40
- * - Built-in lifecycle events (`open`, `live`, `close`, etc.) map to plain `Event`.
41
- * - `heartbeat` maps to `LaplaceRawEvent<number>` (online viewer count).
42
- * - `msg` maps to `LaplaceRawEvent<unknown>` (any server command).
43
- * - Bilibili commands map to `LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.*>`.
44
- * - Unknown/dynamic events fall through to a generic overload on `addEventListener`.
45
- */
46
- interface LiveEventMap {
47
- open: Event;
48
- live: Event;
49
- close: Event;
50
- error: Event;
51
- heartbeat: LaplaceRawEvent<number>;
52
- msg: LaplaceRawEvent<unknown>;
53
- event: LaplaceRawEvent<Event>;
54
- DANMU_MSG: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.DANMU_MSG>;
55
- RECALL_DANMU_MSG: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.RECALL_DANMU_MSG>;
56
- INTERACT_WORD: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.INTERACT_WORD>;
57
- ENTRY_EFFECT: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ENTRY_EFFECT>;
58
- USER_TOAST_MSG: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.USER_TOAST_MSG>;
59
- USER_TOAST_MSG_V2: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.USER_TOAST_MSG_V2>;
60
- SEND_GIFT: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.SEND_GIFT>;
61
- SEND_GIFT_V2: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.SEND_GIFT_V2>;
62
- INTERACT_WORD_V2: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.INTERACT_WORD_V2>;
63
- SUPER_CHAT_MESSAGE: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.SUPER_CHAT_MESSAGE>;
64
- SUPER_CHAT_MESSAGE_DELETE: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.SUPER_CHAT_MESSAGE_DELETE>;
65
- USER_VIRTUAL_MVP: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.USER_VIRTUAL_MVP>;
66
- LIKE_INFO_V3_CLICK: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.LIKE_INFO_V3_CLICK>;
67
- POPULARITY_RED_POCKET_START: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.POPULARITY_RED_POCKET_START>;
68
- POPULARITY_RED_POCKET_V2_START: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.POPULARITY_RED_POCKET_V2_START>;
69
- POPULARITY_RED_POCKET_WINNER_LIST: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.POPULARITY_RED_POCKET_WINNER_LIST>;
70
- POPULARITY_RED_POCKET_V2_WINNER_LIST: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.POPULARITY_RED_POCKET_V2_WINNER_LIST>;
71
- ANCHOR_LOT_START: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ANCHOR_LOT_START>;
72
- ANCHOR_LOT_AWARD: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ANCHOR_LOT_AWARD>;
73
- COMMON_NOTICE_DANMAKU: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.COMMON_NOTICE_DANMAKU>;
74
- EFFECT_DANMAKU_MSG: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.EFFECT_DANMAKU_MSG>;
75
- DANMU_ACTIVITY_CONFIG: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.DANMU_ACTIVITY_CONFIG>;
76
- ROOM_CHANGE: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ROOM_CHANGE>;
77
- ROOM_SILENT_ON: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ROOM_SILENT_ON>;
78
- ROOM_SILENT_OFF: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ROOM_SILENT_OFF>;
79
- WATCHED_CHANGE: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.WATCHED_CHANGE>;
80
- LIKE_INFO_V3_UPDATE: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.LIKE_INFO_V3_UPDATE>;
81
- ONLINE_RANK_COUNT: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ONLINE_RANK_COUNT>;
82
- ONLINE_RANK_V2: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ONLINE_RANK_V2>;
83
- ONLINE_RANK_V3: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ONLINE_RANK_V3>;
84
- ROOM_REAL_TIME_MESSAGE_UPDATE: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ROOM_REAL_TIME_MESSAGE_UPDATE>;
85
- ROOM_REAL_TIME_MESSAGE_UPDATE_V2: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ROOM_REAL_TIME_MESSAGE_UPDATE_V2>;
86
- LIVE: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.LIVE>;
87
- PREPARING: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.PREPARING>;
88
- WARNING: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.WARNING>;
89
- CUT_OFF: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.CUT_OFF>;
90
- ROOM_BLOCK_MSG: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ROOM_BLOCK_MSG>;
91
- ROOM_ADMINS: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ROOM_ADMINS>;
92
- room_admin_entrance: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.room_admin_entrance>;
93
- ROOM_ADMIN_REVOKE: LaplaceRawEvent<BilibiliInternal.WebSocket.Prod.ROOM_ADMIN_REVOKE>;
94
- LIVE_OPEN_PLATFORM_DM: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_DM>;
95
- LIVE_OPEN_PLATFORM_SEND_GIFT: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_SEND_GIFT>;
96
- LIVE_OPEN_PLATFORM_SUPER_CHAT: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_SUPER_CHAT>;
97
- LIVE_OPEN_PLATFORM_GUARD: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_GUARD>;
98
- LIVE_OPEN_PLATFORM_LIKE: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_LIKE>;
99
- LIVE_OPEN_PLATFORM_LIVE_ROOM_ENTER: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_LIVE_ROOM_ENTER>;
100
- LIVE_OPEN_PLATFORM_LIVE_START: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_LIVE_START>;
101
- LIVE_OPEN_PLATFORM_LIVE_END: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_LIVE_END>;
102
- LIVE_OPEN_PLATFORM_INTERACTION_END: LaplaceRawEvent<BilibiliInternal.WebSocket.OpenPlatform.LIVE_OPEN_PLATFORM_INTERACTION_END>;
103
- }
104
- /**
105
- * Typed {@link EventTarget} base class for live connections.
106
- *
107
- * Provides typed `addEventListener` / `removeEventListener` via
108
- * {@link LiveEventMap} and a `dispatchEvent` override that emits a
109
- * catch-all `"event"` meta-event for every dispatched event.
110
- *
111
- * Extend this instead of `EventTarget` when building wrappers around
112
- * `LiveWS` / `KeepLiveWS` to inherit typed event signatures
113
- * automatically — no `declare` duplication required.
114
- *
115
- * @example
116
- * ```ts
117
- * import { LaplaceEventTarget, LaplaceRawEvent } from '@laplace.live/ws'
118
- *
119
- * class MyWrapper extends LaplaceEventTarget {
120
- * // addEventListener is already typed — no extra `declare` needed
121
- * }
122
- * ```
123
- */
124
- declare class LaplaceEventTarget extends EventTarget {
125
- dispatchEvent(event: Event): boolean;
126
- addEventListener: {
127
- <K extends keyof LiveEventMap>(type: K, listener: (ev: LiveEventMap[K]) => void, options?: boolean | AddEventListenerOptions): void;
128
- <T = unknown>(type: string, listener: (ev: LaplaceRawEvent<T>) => void, options?: boolean | AddEventListenerOptions): void;
129
- };
130
- removeEventListener: {
131
- <K extends keyof LiveEventMap>(type: K, listener: (ev: LiveEventMap[K]) => void, options?: boolean | EventListenerOptions): void;
132
- (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
133
- };
134
- }
135
-
136
- /**
137
- * Options for configuring the Bilibili live connection authentication.
138
- *
139
- * When {@link authBody} is provided it takes precedence over all other
140
- * fields and is sent as the join payload verbatim.
141
- */
142
- type LiveOptions = {
143
- /** Protocol version. Defaults to `3` (brotli-compressed). */
144
- protover?: 1 | 2 | 3;
145
- /** Authentication token obtained from the danmaku info API. */
146
- key?: string;
147
- /**
148
- * Pre-built authentication body. When supplied as a `Uint8Array` it is
149
- * sent raw; when supplied as an object it is JSON-encoded before sending.
150
- * Takes precedence over {@link key}, {@link uid}, and {@link buvid}.
151
- */
152
- authBody?: Uint8Array | Record<string, unknown>;
153
- /** User ID. Defaults to `0` (anonymous). */
154
- uid?: number;
155
- /** Browser unique visitor ID used for risk-control tracking. */
156
- buvid?: string;
157
- };
158
- /**
159
- * Base class for a single Bilibili live room connection.
160
- *
161
- * Extends {@link LaplaceEventTarget} and emits the following events:
162
- *
163
- * | Event | Payload | Description |
164
- * |---------------|--------------------|--------------------------------------------------|
165
- * | `open` | — | Underlying transport connected. |
166
- * | `live` | — | Server acknowledged the join (room entered). |
167
- * | `heartbeat` | `LaplaceRawEvent<number>`| Online viewer count received from server. |
168
- * | `msg` | `LaplaceRawEvent<any>` | Any server command message. |
169
- * | `DANMU_MSG` | `LaplaceRawEvent<any>` | Danmaku (chat) message. |
170
- * | `close` | — | Connection closed. |
171
- * | `error` | — | Unrecoverable error (connection is closed). |
172
- * | `event` | `LaplaceRawEvent<Event>` | Meta-event wrapping every dispatched event. |
173
- *
174
- * Subclasses ({@link LiveWSBase}, {@link LiveTCPBase}) provide the concrete
175
- * transport and supply `send` / `close` callbacks to this constructor.
176
- *
177
- * @param inflates - Platform-specific inflate + brotli decompressors.
178
- * @param roomid - Numeric Bilibili live room ID.
179
- * @param options - Transport callbacks and authentication options.
180
- *
181
- * @throws {Error} If `roomid` is not a finite number.
182
- */
183
- declare class Live extends LaplaceEventTarget {
184
- /** The live room ID this instance is connected to. */
185
- roomid: number;
186
- /** Latest known online viewer count, updated on each heartbeat. */
187
- online: number;
188
- /** `true` after the server acknowledges the join (`welcome` packet). */
189
- live: boolean;
190
- /** `true` after {@link close} has been called. */
191
- closed: boolean;
192
- /** @internal Handle for the heartbeat interval timer. */
193
- timeout: ReturnType<typeof setTimeout>;
194
- /** Send raw binary data over the underlying transport. */
195
- send: (data: Uint8Array) => void;
196
- /** Gracefully close the connection and set {@link closed} to `true`. */
197
- close: () => void;
198
- constructor(inflates: Inflates, roomid: number, { send, close, protover, key, authBody, uid, buvid, }: {
199
- send: (data: Uint8Array) => void;
200
- close: () => void;
201
- } & LiveOptions);
202
- /** Send a heartbeat packet to the server. */
203
- heartbeat(): void;
204
- /**
205
- * Send a heartbeat and resolve with the online viewer count from the
206
- * next heartbeat response.
207
- * @returns A promise that resolves with the current online count.
208
- */
209
- getOnline(): Promise<number>;
210
- }
211
-
212
- /**
213
- * Auto-reconnecting wrapper around a {@link Live} subclass.
214
- *
215
- * `KeepLive` maintains a persistent connection to a Bilibili live room by
216
- * automatically re-creating the underlying {@link Live} instance whenever
217
- * the connection drops or a heartbeat timeout is reached. All events from
218
- * the inner connection are forwarded to this instance.
219
- *
220
- * @typeParam T - The concrete {@link Live} instance type being managed.
221
- *
222
- * @example
223
- * ```ts
224
- * const keep = new KeepLiveWS(12345, { key: '...' })
225
- * keep.addEventListener('heartbeat', (e) => console.log('online:', e.data))
226
- * keep.addEventListener('DANMU_MSG', ({ data }) => console.log('danmaku:', data.msg_id))
227
- * ```
228
- */
229
- declare class KeepLive<T extends Live> extends LaplaceEventTarget {
230
- /** @internal Factory that creates a fresh connection with the original arguments. */
231
- private createConnection;
232
- /** `true` after {@link close} has been called; prevents further reconnects. */
233
- closed: boolean;
234
- /** Delay in milliseconds before attempting a reconnect. */
235
- interval: number;
236
- /** Maximum milliseconds to wait for a heartbeat before forcing a reconnect. */
237
- timeout: number;
238
- /** The current underlying connection instance. */
239
- connection: T;
240
- constructor(createConnection: () => T);
241
- /**
242
- * Wire up event forwarding and timeout handling on the current connection.
243
- * When `reconnect` is `true`, the previous connection is closed and a fresh
244
- * one is created via the factory.
245
- *
246
- * @param reconnect - Whether to tear down and recreate the connection first.
247
- */
248
- connect(reconnect?: boolean): void;
249
- /** Latest known online viewer count from the underlying connection. */
250
- get online(): number;
251
- /** The live room ID. */
252
- get roomid(): number;
253
- /** Permanently close the connection and stop reconnecting. */
254
- close(): void;
255
- /** Send a heartbeat packet to the server. */
256
- heartbeat(): void;
257
- /**
258
- * Send a heartbeat and resolve with the online viewer count from the
259
- * next heartbeat response.
260
- * @returns A promise that resolves with the current online count.
261
- */
262
- getOnline(): Promise<number>;
263
- /**
264
- * Send raw binary data through the underlying connection.
265
- * @param data - The binary packet to send.
266
- */
267
- send(data: Uint8Array): void;
268
- }
269
-
270
- /**
271
- * Configuration options for WebSocket-based live connections.
272
- * Extends {@link LiveOptions} with an optional WebSocket server address.
273
- */
274
- type WSOptions = LiveOptions & {
275
- /** WebSocket server URL. Defaults to `wss://broadcastlv.chat.bilibili.com/sub`. */
276
- address?: string;
277
- /** Factory to create the WebSocket (e.g. for runtime-specific options like Bun's proxy). */
278
- createWebSocket?: (address: string) => WebSocket;
279
- };
280
- /**
281
- * WebSocket transport for Bilibili live room connections.
282
- *
283
- * Wraps a native {@link WebSocket}, wiring its lifecycle events (`open`,
284
- * `message`, `close`, `error`) into the {@link Live} event system. Binary
285
- * frames are forwarded as `LaplaceRawEvent<Uint8Array>` for protocol decoding.
286
- *
287
- * Not typically instantiated directly — use {@link LiveWS} (server) or the
288
- * browser-specific `LiveWS` which inject the appropriate inflate
289
- * implementation.
290
- *
291
- * @param inflates - Platform-specific inflate/brotli decompressors.
292
- * @param roomid - Numeric Bilibili live room ID.
293
- * @param options - WebSocket address and authentication options.
294
- */
295
- declare class LiveWSBase extends Live {
296
- /** The underlying native WebSocket instance. */
297
- ws: WebSocket;
298
- constructor(inflates: Inflates, roomid: number, { address, createWebSocket, ...options }?: WSOptions);
299
- }
300
-
301
- export { type Inflates as I, KeepLive as K, Live as L, type WSOptions as W, type LiveOptions as a, LiveWSBase as b, LaplaceEventTarget as c, LaplaceRawEvent as d, type LiveEventMap as e };