@floating-live/bilibili 0.16.0 → 0.16.2

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/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- export * from "./plugin";
2
- export * from "./room";
3
- export * from "./parser";
4
- export * from "./types";
5
- export * from "./utils";
1
+ export * from "./plugin.js";
2
+ export * from "./room.js";
3
+ export * from "./parser.js";
4
+ export * from "./types.js";
package/index.js CHANGED
@@ -1,5 +1,4 @@
1
- export * from "./plugin";
2
- export * from "./room";
3
- export * from "./parser";
4
- export * from "./types";
5
- export * from "./utils";
1
+ export * from "./plugin.js";
2
+ export * from "./room.js";
3
+ export * from "./parser.js";
4
+ export * from "./types.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@floating-live/bilibili",
3
- "version": "0.16.0",
3
+ "version": "0.16.2",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -8,22 +8,30 @@
8
8
  "license": "MIT",
9
9
  "scripts": {
10
10
  "check": "tsc --noEmit",
11
- "clear": "rimraf *.js *.d.ts --glob",
12
- "build": "npm run clear && tsc --build",
11
+ "clear": "rimraf **/*.js **/*.d.ts --glob",
12
+ "build": "npm run clear && tsc --build && tsc-alias",
13
13
  "prepack": "npm run build",
14
14
  "postpack": "npm run clear"
15
15
  },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
16
19
  "dependencies": {
17
- "bilibili-live-danmaku": "^0.7.0-alpha.6"
20
+ "@floating-live/cookies": "^0.1.0",
21
+ "bilibili-live-danmaku": "^0.7.3",
22
+ "md5": "^2.3.0"
18
23
  },
19
24
  "devDependencies": {
20
- "@floating-live/platform": "^0.16.0",
25
+ "@floating-live/platform": "^0.16.2",
26
+ "@types/md5": "^2.3.0",
21
27
  "@types/websocket": "^1.0.5",
22
28
  "@types/ws": "^8.5.3",
23
29
  "rimraf": "^3.0.2",
30
+ "tsc-alias": "^1.8.16",
24
31
  "typescript": "^5.8.3"
25
32
  },
26
33
  "peerDependencies": {
27
34
  "floating-live": "^0.16.0"
28
- }
35
+ },
36
+ "gitHead": "c5b2735707d4c59aed629e594efc4409abe03f78"
29
37
  }
package/parser.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { LiveMessage, LiveRoomData } from "floating-live";
2
- import { BilibiliRoomData, RoomBaseInfo } from "./types";
3
- import { MessageData, ResponseData } from "bilibili-live-danmaku";
2
+ import { BilibiliRoomData, RoomBaseInfo } from "./types.js";
3
+ import { DataXliveGetInfoByRoom, MessageData } from "bilibili-live-danmaku";
4
4
  /** 转换bilibili直播弹幕为floating通用格式 */
5
5
  export declare function parseMessage(data: MessageData.All, room?: BilibiliRoomData): LiveMessage.All | undefined;
6
6
  /** 转换bilibili直播间信息为floating通用格式 */
7
- export declare function parseGetInfoByRoom(data: ResponseData.GetInfoByRoom): BilibiliRoomData;
7
+ export declare function parseGetInfoByRoom(data: DataXliveGetInfoByRoom): BilibiliRoomData;
8
8
  /** 转换bilibili直播间信息为floating通用格式 */
9
9
  export declare function parseRoomBaseInfo(data: RoomBaseInfo): BilibiliRoomData;
10
10
  export declare function mergeLiveRoomData<T extends LiveRoomData>(...list: Partial<T>[]): T;
package/plugin.d.ts CHANGED
@@ -1,17 +1,27 @@
1
- import { BasePlugin, PluginContext } from "floating-live";
1
+ import { AppPluginExposesMap, BasePlugin, PluginContext, UserInfo } from "floating-live";
2
+ import { BilibiliApiClientEx } from "./utils/request.js";
2
3
  interface BilibiliLoginInfo {
3
4
  credentials: string;
4
- userId: number;
5
+ isLogin: boolean;
6
+ user: UserInfo | null;
5
7
  }
6
8
  declare module "floating-live" {
7
9
  interface AppCommandMap {
10
+ /** 检测并更新登录cookie有效性 */
8
11
  "bilibili.credentials.check": (credentials: string) => Promise<BilibiliLoginInfo>;
12
+ /** 设置UA */
13
+ "bilibili.client.userAgent": (credentials: string) => string;
14
+ /** 使用cookie登录 */
15
+ "bilibili.credentials.set": (credentials: string) => string;
16
+ /** 获取二维码 */
9
17
  "bilibili.login.qrcode.get": () => Promise<{
10
18
  key: string;
11
19
  url: string;
12
20
  }>;
21
+ /** 轮询二维码登录状态 */
13
22
  "bilibili.login.qrcode.poll": (key: string) => Promise<{
14
23
  status: number;
24
+ message?: string;
15
25
  credentials?: string;
16
26
  }>;
17
27
  }
@@ -21,6 +31,12 @@ export interface PluginOptions {
21
31
  }
22
32
  export declare class PluginBilibili extends BasePlugin {
23
33
  static pluginName: string;
34
+ apiClient: BilibiliApiClientEx;
35
+ room: AppPluginExposesMap["room"] | null;
24
36
  init(ctx: PluginContext): void;
37
+ /** 设置登录凭证 */
38
+ setCredentials(cookie: string): string;
39
+ /** 设置UA */
40
+ setUserAgent(userAgent: string): string;
25
41
  }
26
42
  export default PluginBilibili;
package/plugin.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import { BasePlugin, } from "floating-live";
2
- import { RoomBilibili } from "./room";
3
- import { checkLoginQRcode, qrcodeGenerate } from "./utils";
4
- import { requestGetInfoByRoom, getLoginUid, Cookies, } from "bilibili-live-danmaku";
5
- import { parseGetInfoByRoom } from "./parser";
2
+ import { RoomBilibili } from "./room.js";
3
+ import { BilibiliApiClientEx, fetchLiveRoomData } from "./utils/request.js";
4
+ import { RequestError } from "bilibili-live-danmaku";
6
5
  const platformInfo = {
7
6
  name: "bilibili",
8
7
  membership: {
@@ -31,32 +30,133 @@ const platformInfo = {
31
30
  },
32
31
  };
33
32
  export class PluginBilibili extends BasePlugin {
33
+ constructor() {
34
+ super(...arguments);
35
+ this.apiClient = new BilibiliApiClientEx();
36
+ this.room = null;
37
+ }
34
38
  init(ctx) {
35
39
  ctx.whenRegister("platform", (platform) => {
36
40
  platform.register("bilibili", platformInfo, ctx.signal);
37
41
  });
42
+ ctx.whenRegister("room", (room) => {
43
+ this.room = room;
44
+ return () => {
45
+ this.room = null;
46
+ };
47
+ });
38
48
  ctx.registerCommand("bilibili.room.create", (e, id, options) => {
39
- return new RoomBilibili(Number(id), options);
49
+ return new RoomBilibili(Number(id), {
50
+ credentials: this.apiClient.cookie,
51
+ userAgent: this.apiClient.userAgent,
52
+ fetch: this.apiClient.fetch,
53
+ ...options,
54
+ });
40
55
  });
41
56
  ctx.registerCommand("bilibili.room.data", async (e, id) => {
42
- const rawInfo = await requestGetInfoByRoom(parseInt("" + id));
43
- return parseGetInfoByRoom(rawInfo);
57
+ return await fetchLiveRoomData(this.apiClient, parseInt(id.toString()));
44
58
  });
45
59
  ctx.registerCommand("bilibili.credentials.check", async (e, credentials) => {
46
- const buvid = new Cookies(credentials).get("buvid3");
47
- const userId = await getLoginUid({ cookie: credentials });
48
- return {
49
- credentials,
50
- userId,
51
- };
60
+ const apiClient = new BilibiliApiClientEx({
61
+ cookie: credentials,
62
+ userAgent: this.apiClient.userAgent,
63
+ fetch: this.apiClient.fetch,
64
+ });
65
+ try {
66
+ // 检测cookie是否需要更新
67
+ const { refresh, timestamp } = (await apiClient.xpassportCookieInfo())
68
+ .data;
69
+ if (refresh) {
70
+ const refresh_token = apiClient.cookies.get("refresh_token");
71
+ // 更新cookie
72
+ const { refresh_token: new_refresh_token } = await apiClient.refreshCookie({
73
+ timestamp,
74
+ refresh_token,
75
+ });
76
+ apiClient.cookies.set("refresh_token", new_refresh_token);
77
+ }
78
+ // 检测登录状态
79
+ const data = (await apiClient.xapiNav()).data;
80
+ return {
81
+ credentials,
82
+ isLogin: true,
83
+ user: {
84
+ id: data.mid,
85
+ name: data.uname,
86
+ avatar: data.face,
87
+ },
88
+ };
89
+ }
90
+ catch (e) {
91
+ if (e.ok && e.code == -101) {
92
+ return {
93
+ credentials,
94
+ data: {},
95
+ isLogin: false,
96
+ user: null,
97
+ };
98
+ }
99
+ else {
100
+ throw e;
101
+ }
102
+ }
103
+ });
104
+ ctx.registerCommand("bilibili.credentials.set", (e, credentials) => {
105
+ return this.setCredentials(credentials);
52
106
  });
53
- ctx.registerCommand("bilibili.login.qrcode.get", () => {
54
- return qrcodeGenerate();
107
+ ctx.registerCommand("bilibili.client.userAgent", (e, userAgent) => {
108
+ return this.setUserAgent(userAgent);
109
+ });
110
+ ctx.registerCommand("bilibili.login.qrcode.get", async () => {
111
+ const { qrcode_key, url } = (await this.apiClient.xpassportQrcodeGenerate()).data;
112
+ return { key: qrcode_key, url };
55
113
  });
56
114
  ctx.registerCommand("bilibili.login.qrcode.poll", async (e, key) => {
57
- const [status, credentials] = await checkLoginQRcode(key);
58
- return { status, credentials };
115
+ const data = await this.apiClient.xpassportQrcodePoll({
116
+ qrcode_key: key,
117
+ });
118
+ const { code, message, refresh_token } = data.data;
119
+ if (code == 86101) {
120
+ // 未扫码
121
+ return { status: 1, message };
122
+ }
123
+ else if (code == 86090) {
124
+ // 等待确认
125
+ return { status: 2, message };
126
+ }
127
+ else if (code == 0) {
128
+ // 扫码成功
129
+ this.apiClient.cookies.set("refresh_token", refresh_token);
130
+ return { status: 0, message, credentials: this.apiClient.cookie };
131
+ }
132
+ else if (code == 86038) {
133
+ // 二维码失效
134
+ return { status: -1, message };
135
+ }
136
+ else {
137
+ throw new RequestError({ ok: true, code, message });
138
+ }
139
+ });
140
+ }
141
+ /** 设置登录凭证 */
142
+ setCredentials(cookie) {
143
+ this.apiClient.setCookie(cookie);
144
+ this.room?.getList()
145
+ ?.filter((r) => r.platform == "bilibili")
146
+ .forEach((r) => {
147
+ r.setCredentials(cookie);
148
+ });
149
+ return cookie;
150
+ }
151
+ /** 设置UA */
152
+ setUserAgent(userAgent) {
153
+ this.apiClient.userAgent = userAgent;
154
+ this.room?.getList()
155
+ ?.filter((r) => r.platform == "bilibili")
156
+ .forEach((r) => {
157
+ r.userAgent = userAgent;
59
158
  });
159
+ return userAgent;
60
160
  }
61
161
  }
62
162
  PluginBilibili.pluginName = "bilibili";
package/room.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { BilibiliRoomData, ConnectTokens } from "./types";
2
- import { LiveWS, ResponseData } from "bilibili-live-danmaku";
3
- import { FetchOptions } from "./utils";
1
+ import { BilibiliRoomData, ConnectTokens } from "./types.js";
2
+ import { LiveWS } from "bilibili-live-danmaku";
4
3
  import { UserInfo, LiveRoomStatsInfo, LiveRoomDetailInfo, LiveRoom } from "floating-live";
5
4
  /** 配置项 */
6
5
  export interface RoomOptions {
@@ -14,6 +13,8 @@ export interface RoomOptions {
14
13
  credentials?: string;
15
14
  /** 连接凭证,若为空,则初始化时自动生成 */
16
15
  tokens?: ConnectTokens;
16
+ /** 用户代理,若为空,则使用默认值 */
17
+ userAgent?: string;
17
18
  /** 预设置的房间数据,若设置,将不会自动更新直播间数据 */
18
19
  data?: BilibiliRoomData;
19
20
  /** 断线重连间隔(默认值为500ms) */
@@ -58,8 +59,8 @@ export declare class RoomBilibili extends LiveRoom implements BilibiliRoomData {
58
59
  autoReconnect: boolean;
59
60
  /** 是否初始化 */
60
61
  whenInit: Promise<RoomBilibili>;
61
- /** 用户代理 */
62
- userAgent?: string;
62
+ get userAgent(): string;
63
+ set userAgent(ua: string);
63
64
  /** 是否处于重连状态 */
64
65
  private reconnecting;
65
66
  /** 最后一次连接时间 */
@@ -68,8 +69,6 @@ export declare class RoomBilibili extends LiveRoom implements BilibiliRoomData {
68
69
  customFetchData?: (id: number | string) => BilibiliRoomData;
69
70
  /** 自定义fetchTokens */
70
71
  customFetchTokens?: (roomId: number, credentials: string) => Promise<ConnectTokens>;
71
- /** 自定义fetch函数 */
72
- fetch: (input: RequestInfo | URL, init?: RequestInit | undefined) => Promise<Response>;
73
72
  constructor(
74
73
  /** 房间id */
75
74
  id: number, options?: RoomOptions);
@@ -82,7 +81,7 @@ export declare class RoomBilibili extends LiveRoom implements BilibiliRoomData {
82
81
  /** 获取房间信息 */
83
82
  fetchData(): Promise<BilibiliRoomData>;
84
83
  /** 设置直播间数据 */
85
- setData({ roomId, shortId, status, timestamp, detail, stats, anchor, liveId, openStatus, available, }: BilibiliRoomData): import("floating-live").LiveRoomData;
84
+ setData({ roomId, shortId, status, timestamp, detail, stats, anchor, liveId, available, }: BilibiliRoomData): import("floating-live").LiveRoomData;
86
85
  /** 开启直播间监听 */
87
86
  open(): Promise<void>;
88
87
  /** 设置登录凭据 */
@@ -90,7 +89,7 @@ export declare class RoomBilibili extends LiveRoom implements BilibiliRoomData {
90
89
  /** 更新连接tokens */
91
90
  updateTokens(): Promise<void>;
92
91
  /** 请求连接tokens */
93
- fetchTokens(credentials?: string): Promise<ConnectTokens>;
92
+ fetchTokens(): Promise<ConnectTokens>;
94
93
  /** 设置直播连接tokens */
95
94
  setTokens(tokens: ConnectTokens): void;
96
95
  /** 无缝重连 */
@@ -104,5 +103,3 @@ export declare class RoomBilibili extends LiveRoom implements BilibiliRoomData {
104
103
  close(): void;
105
104
  }
106
105
  export default RoomBilibili;
107
- /** 获取房间信息 */
108
- export declare function requestGetInfoByRoom(id: number, options?: FetchOptions): Promise<ResponseData.GetInfoByRoom>;
package/room.js CHANGED
@@ -1,30 +1,31 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
1
6
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
7
  if (kind === "m") throw new TypeError("Private method is not writable");
3
8
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
11
  };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _RoomBilibili_instances, _RoomBilibili_tokens, _RoomBilibili_cookies, _RoomBilibili_getFetchOptions;
13
- import { LiveWS,
14
- // requestGetInfoByRoom,
15
- getLoginUid,
16
- // getLiveConfig,
17
- Cookies, customFetch, requestBuvidCookie, } from "bilibili-live-danmaku";
18
- import { parseGetInfoByRoom, parseMessage, parseRoomBaseInfo } from "./parser";
19
- import { getRoomBaseInfo } from "./utils";
20
- import { LiveConnectionStatus, LiveRoom, } from "floating-live";
12
+ var _RoomBilibili_tokens, _RoomBilibili_apiClient;
13
+ import { LiveWS, parseLiveConfig } from "bilibili-live-danmaku";
14
+ import { parseGetInfoByRoom, parseMessage, parseRoomBaseInfo } from "./parser.js";
15
+ import { LiveConnectionStatus, LiveRoom, LiveRoomOpenStatus, } from "floating-live";
16
+ import { BilibiliApiClientEx } from "./utils/request.js";
21
17
  const symbolAbortController = Symbol("abortController");
22
18
  export class RoomBilibili extends LiveRoom {
19
+ get userAgent() {
20
+ return __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").userAgent;
21
+ }
22
+ set userAgent(ua) {
23
+ __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").userAgent = ua;
24
+ }
23
25
  constructor(
24
26
  /** 房间id */
25
27
  id, options = {}) {
26
28
  super();
27
- _RoomBilibili_instances.add(this);
28
29
  /** 平台id */
29
30
  this.platform = "bilibili";
30
31
  /** 直播间room_id */
@@ -55,19 +56,21 @@ export class RoomBilibili extends LiveRoom {
55
56
  /** 连接凭证 */
56
57
  _RoomBilibili_tokens.set(this, void 0);
57
58
  /** 用户cookie */
58
- _RoomBilibili_cookies.set(this, new Cookies());
59
+ _RoomBilibili_apiClient.set(this, void 0);
59
60
  /** 是否处于重连状态 */
60
61
  this.reconnecting = false;
61
62
  /** 最后一次连接时间 */
62
63
  this.lastConnectionTime = 0;
63
- /** 自定义fetch函数 */
64
- this.fetch = fetch;
65
64
  this.id = id;
66
65
  this.connectInterval = options.connectInterval ?? 500;
67
66
  this.connectTimeout = options.connectTimeout ?? 45000;
68
67
  this.autoReconnect = options.autoReconnect ?? false;
69
68
  __classPrivateFieldSet(this, _RoomBilibili_tokens, options.tokens, "f");
70
- __classPrivateFieldSet(this, _RoomBilibili_cookies, new Cookies(options.credentials), "f");
69
+ __classPrivateFieldSet(this, _RoomBilibili_apiClient, new BilibiliApiClientEx({
70
+ userAgent: options.userAgent,
71
+ cookie: options.credentials,
72
+ fetch: options.fetch,
73
+ }), "f");
71
74
  options.data && this.setData(options.data);
72
75
  const init = options.init ?? true;
73
76
  if (init) {
@@ -86,7 +89,7 @@ export class RoomBilibili extends LiveRoom {
86
89
  this.openStatus = 1;
87
90
  // 设置必要 cookie
88
91
  // 如果cookie中没有buvid3字段,会自动获取一个
89
- await this.setCredentials(__classPrivateFieldGet(this, _RoomBilibili_cookies, "f").toString(), false);
92
+ await this.setCredentials(__classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").cookie, false);
90
93
  // 自动更新房间信息
91
94
  if (!data)
92
95
  await this.update();
@@ -110,20 +113,25 @@ export class RoomBilibili extends LiveRoom {
110
113
  async fetchData() {
111
114
  if (this.customFetchData)
112
115
  return this.customFetchData(this.id);
113
- const rawInfo = await requestGetInfoByRoom(this.id, __classPrivateFieldGet(this, _RoomBilibili_instances, "m", _RoomBilibili_getFetchOptions).call(this));
114
- if (rawInfo) {
115
- return parseGetInfoByRoom(rawInfo);
116
+ try {
117
+ const res = await __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").xliveGetInfoByRoom({
118
+ room_id: this.id,
119
+ });
120
+ return parseGetInfoByRoom(res.data);
116
121
  }
117
- else {
122
+ catch (e) {
118
123
  // fallback
119
- const baseInfo = await getRoomBaseInfo(this.id, __classPrivateFieldGet(this, _RoomBilibili_instances, "m", _RoomBilibili_getFetchOptions).call(this));
120
- if (baseInfo)
121
- return parseRoomBaseInfo(baseInfo);
124
+ const res = await __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").xliveGetRoomBaseInfo({
125
+ room_ids: [this.id],
126
+ });
127
+ for (const r in res.data.by_room_ids) {
128
+ return parseRoomBaseInfo(res.data.by_room_ids[r]);
129
+ }
130
+ throw new Error("房间不存在");
122
131
  }
123
- throw new Error("获取房间信息失败");
124
132
  }
125
133
  /** 设置直播间数据 */
126
- setData({ roomId, shortId, status, timestamp, detail, stats, anchor, liveId, openStatus, available, }) {
134
+ setData({ roomId, shortId, status, timestamp, detail, stats, anchor, liveId, available, }) {
127
135
  this.roomId = roomId;
128
136
  this.shortId = shortId;
129
137
  this.status = status;
@@ -142,39 +150,32 @@ export class RoomBilibili extends LiveRoom {
142
150
  // 如果直播间监听已打开或处于正在打开状态,则返回
143
151
  if (this.openStatus)
144
152
  return;
145
- this.openStatus = 1;
153
+ this.openStatus = LiveRoomOpenStatus.opening;
146
154
  await this.update();
147
155
  this.openWebsocketClient();
148
156
  }
149
157
  /** 设置登录凭据 */
150
158
  async setCredentials(credentials, updateTokens = true) {
151
- const cookies = new Cookies(credentials);
152
- if (!cookies.has("buvid3")) {
153
- cookies.append(await requestBuvidCookie({
154
- ...__classPrivateFieldGet(this, _RoomBilibili_instances, "m", _RoomBilibili_getFetchOptions).call(this),
155
- cookie: cookies.toString(),
156
- }));
159
+ __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").setCookie(credentials);
160
+ // 在没有buvid3的情况下,访问bilibili主页,获得buvid3
161
+ if (!__classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").cookies.has("buvid3")) {
162
+ __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").wwwBilibili();
157
163
  }
158
- __classPrivateFieldSet(this, _RoomBilibili_cookies, cookies, "f");
159
164
  if (updateTokens)
160
165
  this.updateTokens();
161
166
  }
162
167
  /** 更新连接tokens */
163
168
  async updateTokens() {
164
- this.setTokens(await this.fetchTokens(__classPrivateFieldGet(this, _RoomBilibili_cookies, "f").toString()));
169
+ this.setTokens(await this.fetchTokens());
165
170
  }
166
171
  /** 请求连接tokens */
167
- async fetchTokens(credentials = "") {
172
+ async fetchTokens() {
168
173
  if (this.customFetchTokens) {
169
- return this.customFetchTokens(this.roomId, credentials);
174
+ return this.customFetchTokens(this.roomId, __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").cookie);
170
175
  }
171
- const cookies = new Cookies(credentials);
172
- const buvid = cookies.get("buvid3") ||
173
- (await requestBuvidCookie(__classPrivateFieldGet(this, _RoomBilibili_instances, "m", _RoomBilibili_getFetchOptions).call(this))).buvid3 ||
174
- "";
175
- const uid = parseInt(cookies.get("DedeUserID")) ||
176
- (await getLoginUid(__classPrivateFieldGet(this, _RoomBilibili_instances, "m", _RoomBilibili_getFetchOptions).call(this)));
177
- const key = (await getLiveConfig(this.roomId, __classPrivateFieldGet(this, _RoomBilibili_instances, "m", _RoomBilibili_getFetchOptions).call(this))).key;
176
+ const buvid = __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").cookies.get("buvid3");
177
+ const uid = parseInt(__classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").cookies.get("DedeUserID"));
178
+ const { key } = await parseLiveConfig((await __classPrivateFieldGet(this, _RoomBilibili_apiClient, "f").xliveGetDanmuInfo({ id: this.roomId })).data);
178
179
  return {
179
180
  uid,
180
181
  buvid,
@@ -225,11 +226,11 @@ export class RoomBilibili extends LiveRoom {
225
226
  openWebsocketClient() {
226
227
  // 如果直播间不可用,则返回
227
228
  if (!this.available) {
228
- this.openStatus = 0;
229
+ this.openStatus = LiveRoomOpenStatus.closed;
229
230
  return;
230
231
  }
231
232
  this.initClient();
232
- this.openStatus = 2;
233
+ this.openStatus = LiveRoomOpenStatus.opened;
233
234
  this.emit("open");
234
235
  }
235
236
  /** 创建直播弹幕客户端 */
@@ -241,10 +242,12 @@ export class RoomBilibili extends LiveRoom {
241
242
  buvid: __classPrivateFieldGet(this, _RoomBilibili_tokens, "f")?.buvid,
242
243
  key: __classPrivateFieldGet(this, _RoomBilibili_tokens, "f")?.key,
243
244
  });
245
+ console.log(__classPrivateFieldGet(this, _RoomBilibili_tokens, "f"));
244
246
  const controller = new AbortController();
245
247
  const signal = controller.signal;
246
248
  client[symbolAbortController] = controller;
247
249
  this.lastConnectionTime = Date.now();
250
+ console.log(client.ws.url);
248
251
  client.addEventListener("open", () => {
249
252
  // 连接到服务器
250
253
  this.opened && this.emitConnention(LiveConnectionStatus.connected);
@@ -281,35 +284,11 @@ export class RoomBilibili extends LiveRoom {
281
284
  close() {
282
285
  if (!this.opened)
283
286
  return;
284
- this.openStatus = 0;
287
+ this.openStatus = LiveRoomOpenStatus.closed;
285
288
  this.client?.close();
286
289
  this.emitConnention(LiveConnectionStatus.off);
287
290
  this.emit("close");
288
291
  }
289
292
  }
290
- _RoomBilibili_tokens = new WeakMap(), _RoomBilibili_cookies = new WeakMap(), _RoomBilibili_instances = new WeakSet(), _RoomBilibili_getFetchOptions = function _RoomBilibili_getFetchOptions() {
291
- return {
292
- fetch: this.fetch,
293
- userAgent: this.userAgent,
294
- cookie: __classPrivateFieldGet(this, _RoomBilibili_cookies, "f").toString(),
295
- };
296
- };
293
+ _RoomBilibili_tokens = new WeakMap(), _RoomBilibili_apiClient = new WeakMap();
297
294
  export default RoomBilibili;
298
- const DANMAKU_SERVER_CONF_URL = "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo";
299
- const getLiveConfig = async (roomid, options) => {
300
- const result = (await customFetch(options, `${DANMAKU_SERVER_CONF_URL}?id=${roomid}`).then((w) => w.json()));
301
- const { data: { token: key, host_list: [{ host }], }, } = result;
302
- const address = `wss://${host}/sub`;
303
- return { key, host, address, raw: result };
304
- };
305
- const ROOM_INIT_URL = "https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom";
306
- /** 获取房间信息 */
307
- export async function requestGetInfoByRoom(id, options) {
308
- return await customFetch(options, `${ROOM_INIT_URL}?room_id=${id}`, {
309
- method: "GET",
310
- })
311
- .then((response) => response.json())
312
- .then((data) => {
313
- return data.data;
314
- });
315
- }
@@ -0,0 +1 @@
1
+ export declare function getCorrespondPath(timestamp: number): Promise<string>;
@@ -0,0 +1,10 @@
1
+ const publicKey = await crypto.subtle.importKey("jwk", {
2
+ kty: "RSA",
3
+ n: "y4HdjgJHBlbaBN04VERG4qNBIFHP6a3GozCl75AihQloSWCXC5HDNgyinEnhaQ_4-gaMud_GF50elYXLlCToR9se9Z8z433U3KjM-3Yx7ptKkmQNAMggQwAVKgq3zYAoidNEWuxpkY_mAitTSRLnsJW-NCTa0bqBFF6Wm1MxgfE",
4
+ e: "AQAB",
5
+ }, { name: "RSA-OAEP", hash: "SHA-256" }, true, ["encrypt"]);
6
+ export async function getCorrespondPath(timestamp) {
7
+ const data = new TextEncoder().encode(`refresh_${timestamp}`);
8
+ const encrypted = new Uint8Array(await crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, data));
9
+ return encrypted.reduce((str, c) => str + c.toString(16).padStart(2, "0"), "");
10
+ }