dimsum-chat 0.4.6 → 0.5.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.
@@ -1,2 +1,9 @@
1
1
  import { Message } from './types';
2
- export declare function handleMessage(message: Message): Message | undefined;
2
+ /**
3
+ * Optional context passed to handleMessage for enriching response messages.
4
+ * Extend this interface when more contextual fields are needed in the future.
5
+ */
6
+ export interface HandleMessageContext {
7
+ widgetNickName?: string;
8
+ }
9
+ export declare function handleMessage(message: Message, context?: HandleMessageContext): Message | undefined;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,13 @@
1
+ /**
2
+ * dimsum-chat — 多平台直播消息解析与 WebSocket 通信工具包。
3
+ *
4
+ * 核心能力:
5
+ * - onMessage:一行代码接入直播间消息
6
+ * - Parser:跨平台统一消息解析(B站/抖音/快手/AcFun/CHZZK)
7
+ * - WebSocketManager:WebSocket 连接管理与自动重连
8
+ *
9
+ * @module dimsum-chat
10
+ */
1
11
  export { WebSocketManager, getBfaceURL, getWebSocketURL, onMessage } from './websocket';
2
12
  export { Parser } from './parser';
3
13
  export type { commentParseOptions, abstractLevelOptions } from './parser';
package/dist/parser.d.ts CHANGED
@@ -1,8 +1,45 @@
1
1
  type MessageType = "comment" | "gift" | "follow" | "joinclub" | "like" | "guard" | "superchat" | "enter" | "share" | undefined;
2
+ /**
3
+ * 多平台直播消息解析器。
4
+ *
5
+ * 将各平台(B站/OpenBLive/抖音/快手/AcFun/CHZZK)的原始消息
6
+ * 统一解析为便于使用的属性访问接口,内置属性值缓存机制。
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const parser = new Parser({ type: 'DANMU_MSG', content: '...' })
11
+ * console.log(parser.platform, parser.userName, parser.comment)
12
+ * ```
13
+ *
14
+ * @see {@link https://dimsum.chat/zh/api/parser.html}
15
+ */
2
16
  declare class Parser {
17
+ /**
18
+ * Douyin emoji mappings. Accessible directly without going through CommentBuilder.
19
+ *
20
+ * @example
21
+ * Parser.DouyinEmots.forEach(([emot, url]) => console.log(emot, url));
22
+ */
23
+ static readonly DouyinEmots: readonly [string, string][];
24
+ /**
25
+ * Bilibili emoji mappings (also used by OpenBLive).
26
+ *
27
+ * @example
28
+ * Parser.BilibiliEmots.forEach(([emot, url]) => console.log(emot, url));
29
+ */
30
+ static readonly BilibiliEmots: readonly [string, string][];
31
+ /**
32
+ * Kuaishou emoji mappings.
33
+ *
34
+ * @example
35
+ * Parser.KuaishouEmots.forEach(([emot, url]) => console.log(emot, url));
36
+ */
37
+ static readonly KuaishouEmots: readonly [string, string][];
3
38
  private static douyinGiftGroup;
4
39
  private static kuaishouGiftGroup;
40
+ /** 原始消息类型。 */
5
41
  readonly rawType: string;
42
+ /** 原始消息内容(字符串已自动反序列化为对象)。 */
6
43
  readonly rawContent: any;
7
44
  private _cachedValues;
8
45
  constructor(rawMsg: {
@@ -35,8 +72,11 @@ declare class Parser {
35
72
  * @see {@link https://dimsum.chat/zh/api/parser.html#parser-uid}
36
73
  */
37
74
  get uid(): number | string | undefined;
75
+ /** 粉丝团/守护团等级。 */
38
76
  get clubLevel(): number | undefined;
77
+ /** 粉丝团/守护团名称。 */
39
78
  get clubName(): string | undefined;
79
+ /** AcFun 守护团所属主播的用户 ID。 */
40
80
  get acfunClubUid(): number | undefined;
41
81
  /**
42
82
  * User subscription status in the current Douyin live room.
@@ -45,7 +85,21 @@ declare class Parser {
45
85
  * @see {@link https://dimsum.chat/zh/api/parser.html#parser-douyinsubscribe}
46
86
  */
47
87
  get douyinSubscribe(): 0 | 1 | 2 | undefined;
88
+ /**
89
+ * 用户头像 URL。
90
+ *
91
+ * B 站头像存在跨域限制,建议配合 getBfaceURL() 使用。
92
+ *
93
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-avatar}
94
+ */
48
95
  get avatar(): string | undefined;
96
+ /**
97
+ * 弹幕聊天内容。
98
+ *
99
+ * 当 type 为 comment 或 superchat 时均有值。
100
+ *
101
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-comment}
102
+ */
49
103
  get comment(): string | undefined;
50
104
  /**
51
105
  * Simple method to construct comments as HTML strings. Useful for rendering comment messages in general.
@@ -64,39 +118,132 @@ declare class Parser {
64
118
  */
65
119
  CommentBuilder(builder: (comment: string, stickerUrl?: string, emots?: [string, string][]) => string): string | undefined;
66
120
  /**
67
- * 大航海等级:0-无 3-舰长 2-提督 1-总督
68
- * 若type为guard,则为购买的大航海等级
121
+ * 大航海等级。
122
+ *
123
+ * 0=无,3=舰长,2=提督,1=总督。
124
+ * 当 type 为 guard 时,表示本次购买的大航海等级;
125
+ * 其他消息类型中表示该用户当前的大航海等级。
126
+ * Chzzk 平台的 Subscription 事件也支持此属性。
127
+ *
128
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-guardlevel}
69
129
  */
70
130
  get guardLevel(): 0 | 3 | 2 | 1 | undefined;
131
+ /**
132
+ * 大航海购买数量。
133
+ *
134
+ * B 站表示购买月数,Chzzk 表示订阅月数。
135
+ *
136
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-guardnum}
137
+ */
71
138
  get guardNum(): number | undefined;
139
+ /**
140
+ * 大航海购买价格(CNY)。
141
+ *
142
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-guardprice}
143
+ */
72
144
  get guardPrice(): number | undefined;
145
+ /** Chzzk 订阅等级(1-3)。 */
73
146
  get chzzkTier(): number | undefined;
147
+ /** Chzzk 订阅月数。 */
74
148
  get chzzkTierMonth(): number | undefined;
149
+ /** 礼物名称。 */
75
150
  get giftName(): string | undefined;
151
+ /**
152
+ * 礼物数量。
153
+ *
154
+ * 抖音和快手已由 SDK 内部处理连击去重,每次返回增量值。
155
+ *
156
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-giftnum}
157
+ */
76
158
  get giftNum(): number | undefined;
159
+ /**
160
+ * 单个礼物价格(CNY)。
161
+ *
162
+ * 免费礼物(如 B 站银瓜子)返回 0。
163
+ *
164
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-giftunitprice}
165
+ */
77
166
  get giftUnitPrice(): number | undefined;
167
+ /**
168
+ * 礼物总价值(CNY)。
169
+ *
170
+ * 等价于 giftNum × giftUnitPrice。
171
+ *
172
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-gifttotalprice}
173
+ */
78
174
  get giftTotalPrice(): number | undefined;
175
+ /** 礼物图片 URL。 */
79
176
  get giftImage(): string | undefined;
177
+ /** 超级聊天评论内容。 */
80
178
  get superChatComment(): string | undefined;
179
+ /**
180
+ * 超级聊天价格。
181
+ *
182
+ * B 站与 OpenBLive 为人民币(元),Chzzk 为韩元(KRW)。
183
+ *
184
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-superchatprice}
185
+ */
81
186
  get superChatPrice(): number | undefined;
187
+ /**
188
+ * 广义价格,根据消息类型自动选择对应价格属性:
189
+ *
190
+ * | 消息类型 | 对应属性 |
191
+ * |-----------|-----------------|
192
+ * | gift | giftTotalPrice |
193
+ * | superchat | superChatPrice |
194
+ * | guard | guardPrice |
195
+ * | 其它 | undefined |
196
+ *
197
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-price}
198
+ */
82
199
  get price(): number | undefined;
200
+ /**
201
+ * 获取用户的抽象化分级,用于渲染用户等级标识。
202
+ *
203
+ * 不同平台的映射规则见官网文档。
204
+ *
205
+ * @param options - 各平台分段阈值配置,见 {@link abstractLevelOptions}
206
+ * @returns 0-3 等级,undefined 表示不支持
207
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-getabstractlevel}
208
+ */
83
209
  getAbstractLevel(options?: abstractLevelOptions): number | undefined;
84
210
  }
211
+ /**
212
+ * getCommentHTML 渲染选项。
213
+ *
214
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-getcommenthtml}
215
+ */
85
216
  interface commentParseOptions {
217
+ /** 贴纸表情的 CSS 样式。 */
86
218
  stickerStyle?: string;
219
+ /** 贴纸表情的 CSS 类名。 */
87
220
  stickerClass?: string;
221
+ /** 小表情的 CSS 样式。 */
88
222
  emotStyle?: string;
223
+ /** 小表情的 CSS 类名。 */
89
224
  emotClass?: string;
225
+ /** 自定义 AcFun 贴纸表情。仅 platform='acfun' 时生效。 */
90
226
  acfunCustomStickers?: {
91
227
  keyWord: string;
92
228
  path: string;
93
229
  }[];
94
230
  acfunCustomHtmlBuilder?: (stickerPath: string, content: string) => string;
95
231
  }
232
+ /**
233
+ * getAbstractLevel 配置选项。
234
+ *
235
+ * 各平台的默认分段均为 [7, 11, 15],即 clubLevel ≤ 7 → 0, ≤ 11 → 1, ≤ 15 → 2, > 15 → 3。
236
+ *
237
+ * @see {@link https://dimsum.chat/zh/api/parser.html#parser-getabstractlevel}
238
+ */
96
239
  interface abstractLevelOptions {
240
+ /** 抖音 clubLevel 分段阈值,默认 [7, 11, 15]。 */
97
241
  douyinSteps?: number[];
242
+ /** 快手 clubLevel 分段阈值,默认 [7, 11, 15]。 */
98
243
  kuaishouSteps?: number[];
244
+ /** AcFun clubLevel 分段阈值,默认 [7, 11, 15]。 */
99
245
  acfunSteps?: number[];
246
+ /** AcFun 目标主播 UID。设置后仅该主播的守护团成员返回 >0 的值。 */
100
247
  acfunClubUid?: number;
101
248
  }
102
249
  export { Parser };
@@ -1,20 +1,116 @@
1
1
  import { Parser } from ".";
2
2
  import { Message } from "./types";
3
+ /**
4
+ * WebSocket 连接管理器(单例模式)。
5
+ *
6
+ * 管理 WebSocket 的连接生命周期:
7
+ * - 建立连接(仅调用一次)
8
+ * - 自动重连(断开后每 3 秒重试)
9
+ * - 消息分发到多个监听器
10
+ * - 内部自动处理 DimSumChatWidgetInfoRequest 等握手消息
11
+ *
12
+ * @see {@link https://dimsum.chat/zh/api/websocket-manager.html}
13
+ */
3
14
  declare class WebSocketManager {
4
15
  private static instance;
5
16
  private webSocket;
6
17
  private messageListeners;
18
+ /**
19
+ * Nickname for this widget instance.
20
+ * Set before connection (via onMessageOptions) or dynamically at runtime.
21
+ * When non-empty, it will be included in DimSumChatWidgetInfoResponse
22
+ * so other components can identify this widget via DimSumChatCallMessageRequest.
23
+ */
24
+ widgetNickName: string;
7
25
  private constructor();
26
+ /**
27
+ * 获取唯一的 WebSocket 管理器实例。
28
+ *
29
+ * @returns WebSocketManager 单例
30
+ */
8
31
  static getInstance(): WebSocketManager;
32
+ /**
33
+ * 连接到指定 WebSocket 服务器。
34
+ *
35
+ * 仅可调用一次,重复调用将被忽略。
36
+ * 连接断开后自动每 3 秒尝试重连。
37
+ *
38
+ * @param url - WebSocket 服务器 URL
39
+ * @see {@link https://dimsum.chat/zh/api/websocket-manager.html#websocketmanager-connect}
40
+ */
9
41
  connect(url: string | URL): void;
10
42
  private handleMessage;
43
+ /**
44
+ * 注册消息监听器。
45
+ *
46
+ * 收到 WebSocket 消息后以字符串形式传递给监听器。
47
+ * 添加监听器时若连接已建立,会自动推送一条欢迎消息。
48
+ *
49
+ * @param listener - 消息回调,接收原始 JSON 字符串
50
+ * @see {@link https://dimsum.chat/zh/api/websocket-manager.html#websocketmanager-addmessagelistener}
51
+ */
11
52
  addMessageListener(listener: (message: string) => void): void;
53
+ /**
54
+ * 移除已注册的消息监听器。
55
+ *
56
+ * @param listener - 之前通过 addMessageListener 注册的回调
57
+ */
12
58
  removeMessageListener(listener: (message: string) => void): void;
13
59
  }
60
+ /**
61
+ * 根据当前页面 URL 自动生成 WebSocket 服务器地址。
62
+ *
63
+ * http 页面 → ws://,https 页面 → wss://
64
+ * 路径固定为 /websocket。
65
+ *
66
+ * @returns WebSocket URL,例如 "ws://localhost:13500/websocket"
67
+ */
14
68
  declare function getWebSocketURL(): string;
69
+ /**
70
+ * 生成 B 站用户头像的代理 URL。
71
+ *
72
+ * 走主程序 /bface/ 接口,避免跨域问题。
73
+ *
74
+ * @param uid - B 站用户 ID
75
+ * @returns 头像代理 URL,例如 "http://localhost:13500/bface/123456"
76
+ */
15
77
  declare function getBfaceURL(uid: string | number): string;
78
+ /**
79
+ * onMessage 配置选项。
80
+ *
81
+ * @see {@link https://dimsum.chat/zh/api/websocket-manager.html#onmessage}
82
+ */
16
83
  interface onMessageOptions {
17
84
  customWsServer?: string | URL;
85
+ /**
86
+ * Nickname for this widget instance.
87
+ * Set this to identify your widget when communicating between components
88
+ * (e.g., via DimSumChatCallMessageRequest).
89
+ *
90
+ * Can also be set directly on WebSocketManager.getInstance().widgetNickName
91
+ * at any point before the DimSumChatWidgetInfoRequest is received.
92
+ */
93
+ widgetNickName?: string;
18
94
  }
95
+ /**
96
+ * 注册消息回调,一行代码接入直播间消息。
97
+ *
98
+ * 内部组合了 WebSocketManager、getWebSocketURL、Parser 和 DimSumAuth,
99
+ * 自动处理连接、认证和消息解析,开箱即用。
100
+ *
101
+ * @param callback - 消息回调,接收原始 Message 和已创建的 Parser 实例
102
+ * @param options - 配置选项(可选),支持自定义 WebSocket 服务器
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * import { onMessage } from 'dimsum-chat'
107
+ *
108
+ * onMessage((msg, parser) => {
109
+ * console.log(parser.userName + ': ' + parser.comment)
110
+ * })
111
+ * ```
112
+ *
113
+ * @see {@link https://dimsum.chat/zh/api/websocket-manager.html#onmessage}
114
+ */
19
115
  declare function onMessage(callback: (message: Message, parser: Parser) => void, options?: onMessageOptions): void;
20
116
  export { WebSocketManager, getBfaceURL, getWebSocketURL, onMessage };
package/package.json CHANGED
@@ -1,49 +1,49 @@
1
- {
2
- "name": "dimsum-chat",
3
- "version": "0.4.6",
4
- "description": "This is a simple utility package for DimSum Chat developer.",
5
- "author": "Miego Live",
6
- "license": "MIT",
7
- "homepage": "https://github.com/MiegoLive/dimsum-chat",
8
- "repository": {
9
- "type": "git",
10
- "url": "https://github.com/MiegoLive/dimsum-chat.git"
11
- },
12
- "bugs": {
13
- "url": "https://github.com/MiegoLive/dimsum-chat/issues"
14
- },
15
- "keywords": [
16
- "dimsumchat",
17
- "utils"
18
- ],
19
- "type": "module",
20
- "main": "./dist/dimsum-chat.umd.cjs",
21
- "module": "./dist/dimsum-chat.js",
22
- "types": "./dist/index.d.ts",
23
- "unpkg": "./dist/dimsum-chat.umd.cjs",
24
- "jsdelivr": "./dist/dimsum-chat.umd.cjs",
25
- "exports": {
26
- ".": {
27
- "import": "./dist/dimsum-chat.js",
28
- "require": "./dist/dimsum-chat.umd.cjs"
29
- }
30
- },
31
- "files": [
32
- "dist"
33
- ],
34
- "scripts": {
35
- "dev": "vite",
36
- "build": "tsc && vite build",
37
- "preview": "vite preview",
38
- "pack:dry-run": "npm pack --dry-run",
39
- "test": "vitest",
40
- "coverage": "vitest run --coverage"
41
- },
42
- "devDependencies": {
43
- "@types/node": "^20.11.25",
44
- "typescript": "^5.2.2",
45
- "vite": "^5.1.4",
46
- "vite-plugin-dts": "^3.7.3",
47
- "vitest": "^2.1.1"
48
- }
49
- }
1
+ {
2
+ "name": "dimsum-chat",
3
+ "version": "0.5.0",
4
+ "description": "This is a simple utility package for DimSum Chat developer.",
5
+ "author": "Miego Live",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/MiegoLive/dimsum-chat",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/MiegoLive/dimsum-chat.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/MiegoLive/dimsum-chat/issues"
14
+ },
15
+ "keywords": [
16
+ "dimsumchat",
17
+ "utils"
18
+ ],
19
+ "type": "module",
20
+ "main": "./dist/dimsum-chat.umd.cjs",
21
+ "module": "./dist/dimsum-chat.js",
22
+ "types": "./dist/index.d.ts",
23
+ "unpkg": "./dist/dimsum-chat.umd.cjs",
24
+ "jsdelivr": "./dist/dimsum-chat.umd.cjs",
25
+ "exports": {
26
+ ".": {
27
+ "import": "./dist/dimsum-chat.js",
28
+ "require": "./dist/dimsum-chat.umd.cjs"
29
+ }
30
+ },
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "scripts": {
35
+ "dev": "vite",
36
+ "build": "tsc && vite build",
37
+ "preview": "vite preview",
38
+ "pack:dry-run": "npm pack --dry-run",
39
+ "test": "vitest",
40
+ "coverage": "vitest run --coverage"
41
+ },
42
+ "devDependencies": {
43
+ "@types/node": "^20.11.25",
44
+ "typescript": "^5.2.2",
45
+ "vite": "^5.1.4",
46
+ "vite-plugin-dts": "^3.7.3",
47
+ "vitest": "^2.1.1"
48
+ }
49
+ }