floating-live 0.13.0 → 0.14.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floating-live",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "type": "commonjs",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -4,13 +4,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  const room_1 = __importDefault(require("./room"));
6
6
  const authConfig = {
7
- warn: "由于b站弹幕接口限制,在未登录账号的情况下,返回的弹幕用户名会变为星号且无法获取到uid。\n详见(github:simon300000/bilibili-live-ws#397)[https://github.com/simon300000/bilibili-live-ws/issues/397]",
7
+ info: "由于b站弹幕接口限制,在未登录账号的情况下,返回的弹幕用户名会变为星号且无法获取到uid。\n详见(github:simon300000/bilibili-live-ws#397)[https://github.com/simon300000/bilibili-live-ws/issues/397]",
8
8
  auth: {
9
9
  placeholder: "此处粘贴cookie",
10
10
  info: "请在登录后的b站页面中获取cookie并粘贴在输入框内,至少包含SESSDATA字段。",
11
11
  },
12
12
  qrcode: {
13
- generate: () => { },
13
+ // generate: () => {},
14
14
  },
15
15
  // verify: { name: "key", type: "string" },
16
16
  };
@@ -21,7 +21,7 @@ module.exports = () => {
21
21
  ctx.rooms.generator.register("bilibili", (id, open, config) => {
22
22
  return new room_1.default(Number(id), open, config);
23
23
  });
24
- ctx.auth;
24
+ ctx.auth.options.register("bilibili", authConfig);
25
25
  },
26
26
  };
27
27
  };
@@ -234,7 +234,7 @@ const parseFunction = {
234
234
  id: msg_id,
235
235
  timestamp: send_time || getDateTimestamp(),
236
236
  info: {
237
- watch: data.num,
237
+ view: data.num,
238
238
  },
239
239
  };
240
240
  stats.id ?? (stats.id = (0, utils_1.generateId)(stats));
@@ -381,7 +381,7 @@ function parseInfo(data) {
381
381
  avatar: anchor_info.base_info.face,
382
382
  },
383
383
  stats: {
384
- watch: watched_show.num,
384
+ view: watched_show.num,
385
385
  like: like_info_v3.total_likes,
386
386
  },
387
387
  liveId: room_info.live_id_str,
@@ -51,7 +51,7 @@ class RoomBilibili extends LiveRoom_1.LiveRoom {
51
51
  };
52
52
  this.stats = {
53
53
  /** 观看数 */
54
- watch: 0,
54
+ view: 0,
55
55
  /** 点赞数 */
56
56
  like: 0,
57
57
  };
@@ -98,7 +98,7 @@ class RoomBilibili extends LiveRoom_1.LiveRoom {
98
98
  this.detail.title = room_info.title;
99
99
  this.detail.area = [room_info.parent_area_name, room_info.area_name];
100
100
  this.detail.cover = room_info.cover;
101
- this.stats.watch = watched_show.num;
101
+ this.stats.view = watched_show.num;
102
102
  this.stats.like = like_info_v3.total_likes;
103
103
  this.anchor.id = room_info.uid;
104
104
  this.anchor.name = anchor_info.base_info.uname;
@@ -32,7 +32,7 @@ export declare abstract class LiveRoom extends EventEmitter implements RoomInfo
32
32
  /** 关闭连接 */
33
33
  abstract close(): void;
34
34
  /** 从服务器获取信息 */
35
- abstract getInfo(): void;
35
+ abstract getInfo(): Promise<void>;
36
36
  /** 销毁事件 */
37
37
  protected onDestory?(): void;
38
38
  /** 销毁 */
@@ -18,6 +18,8 @@ export declare class Reglist<T> {
18
18
  get(id: string): T | undefined;
19
19
  /** 获取注册对象列表 */
20
20
  getList(): T[];
21
+ /** 遍历 */
22
+ forEach(callbackfn: (value: T, key: string) => void): void;
21
23
  /** 获取注册id列表 */
22
24
  getIdList(): string[];
23
25
  /** 获取注册id列表 */
@@ -49,6 +49,10 @@ class Reglist {
49
49
  getList() {
50
50
  return [...this.list.values()];
51
51
  }
52
+ /** 遍历 */
53
+ forEach(callbackfn) {
54
+ this.list.forEach(callbackfn);
55
+ }
52
56
  /** 获取注册id列表 */
53
57
  getIdList() {
54
58
  return [...this.list.keys()];
package/src/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from "events";
3
- import { Reglist } from "./abstract/Reglist";
4
- import { Message } from "./types";
5
3
  import { ModPlugin } from "./module/ModPlugin";
6
4
  import { ModRooms } from "./module/ModRooms";
7
5
  import { ModAuth } from "./module/ModAuth";
8
6
  import { ModMessage } from "./module/ModMessage";
7
+ import { ModState } from "./module/ModState";
8
+ import ModCommand from "./module/ModCommand";
9
9
  export declare class FloatingLive extends EventEmitter {
10
10
  /** 房间控制模块 */
11
11
  rooms: ModRooms;
@@ -15,16 +15,10 @@ export declare class FloatingLive extends EventEmitter {
15
15
  auth: ModAuth;
16
16
  /** 插件处理模块 */
17
17
  plugin: ModPlugin<this>;
18
- helper: {
19
- /** 消息处理 */
20
- messageHandler: Reglist<(msg: Message.All) => void>;
21
- /** 插件销毁 */
22
- pluginDestory: Reglist<() => void>;
23
- /** 添加插件 */
24
- pluginAddHandler: Reglist<(name: string) => void>;
25
- /** 移除插件 */
26
- pluginRemoveHandler: Reglist<(name: string) => void>;
27
- };
18
+ /** 命令模块 */
19
+ command: ModCommand;
20
+ /** 状态模块 */
21
+ state: ModState;
28
22
  /** 插件事件注册表 */
29
23
  private regEventMap;
30
24
  constructor();
@@ -32,4 +26,5 @@ export declare class FloatingLive extends EventEmitter {
32
26
  private init;
33
27
  /** 重写事件监听 */
34
28
  on(eventName: string, listener: (...args: any[]) => void): this;
29
+ emit(eventName: string | symbol, ...args: any[]): boolean;
35
30
  }
package/src/index.js CHANGED
@@ -1,33 +1,33 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.FloatingLive = void 0;
4
7
  const events_1 = require("events");
5
- const Reglist_1 = require("./abstract/Reglist");
6
8
  const ModPlugin_1 = require("./module/ModPlugin");
7
9
  const ModRooms_1 = require("./module/ModRooms");
8
10
  const ModAuth_1 = require("./module/ModAuth");
9
11
  const ModMessage_1 = require("./module/ModMessage");
12
+ const ModState_1 = require("./module/ModState");
13
+ const ModCommand_1 = __importDefault(require("./module/ModCommand"));
10
14
  class FloatingLive extends events_1.EventEmitter {
11
15
  constructor() {
12
16
  super();
13
17
  /** 插件事件注册表 */
14
18
  this.regEventMap = new Map();
15
- this.helper = {
16
- messageHandler: new Reglist_1.Reglist(this, "messageHandler"),
17
- pluginDestory: new Reglist_1.Reglist(this, "pluginDestory"),
18
- pluginAddHandler: new Reglist_1.Reglist(this, "pluginAddHandler"),
19
- pluginRemoveHandler: new Reglist_1.Reglist(this, "pluginRemoveHandler"),
20
- };
19
+ this.command = new ModCommand_1.default(this);
20
+ this.state = new ModState_1.ModState(this);
21
+ this.plugin = new ModPlugin_1.ModPlugin(this);
21
22
  this.rooms = new ModRooms_1.ModRooms(this);
22
23
  this.message = new ModMessage_1.ModMessage(this);
23
24
  this.auth = new ModAuth_1.ModAuth(this);
24
- this.plugin = new ModPlugin_1.ModPlugin(this);
25
25
  this.init();
26
26
  }
27
27
  /** 初始化 */
28
28
  init() {
29
29
  // 设置在插件卸载时移除由插件注册的事件
30
- this.helper.pluginRemoveHandler.register("removePluginEvent", (name) => {
30
+ this.on("plugin:remove", (name) => {
31
31
  this.regEventMap.get(name)?.forEach(([eventName, listener]) => {
32
32
  this.removeListener(eventName, listener);
33
33
  });
@@ -47,5 +47,10 @@ class FloatingLive extends events_1.EventEmitter {
47
47
  }
48
48
  return this;
49
49
  }
50
+ emit(eventName, ...args) {
51
+ const result = super.emit(eventName, ...args);
52
+ super.emit("event", eventName, ...args);
53
+ return result;
54
+ }
50
55
  }
51
56
  exports.FloatingLive = FloatingLive;
@@ -6,9 +6,23 @@ class ModAuth {
6
6
  constructor(main) {
7
7
  this.options = new Reglist_1.Reglist(main, "auth.options");
8
8
  this.main = main;
9
+ this.main.state.register("auth", () => {
10
+ const options = {};
11
+ this.options.forEach((val, key) => {
12
+ options[key] = val;
13
+ });
14
+ return {
15
+ options: options,
16
+ };
17
+ });
18
+ this.main.command.batchRegister({
19
+ auth: (platform, auth) => {
20
+ this.set(platform, auth);
21
+ },
22
+ });
9
23
  }
10
24
  set(name, auth) {
11
- return this.main.rooms.setAuth(name, auth);
25
+ this.main.rooms.setAuth(name, auth);
12
26
  }
13
27
  }
14
28
  exports.ModAuth = ModAuth;
@@ -1,3 +1,9 @@
1
+ import { FloatingLive } from "..";
1
2
  import { Reglist } from "../abstract/Reglist";
2
- export default class ModCommand extends Reglist<(e: any, ...args: any) => void> {
3
+ export default class ModCommand extends Reglist<(e: any, ...args: any[]) => any> {
4
+ constructor(main: FloatingLive);
5
+ /** 批量注册命令 */
6
+ batchRegister(list: Record<string, (...args: any) => any>): void;
7
+ /** 执行命令 */
8
+ execute(name: string, ...args: any[]): any;
3
9
  }
@@ -2,5 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const Reglist_1 = require("../abstract/Reglist");
4
4
  class ModCommand extends Reglist_1.Reglist {
5
+ constructor(main) {
6
+ super(main, "command");
7
+ }
8
+ /** 批量注册命令 */
9
+ batchRegister(list) {
10
+ for (const key in list) {
11
+ this.register(key, list[key]);
12
+ }
13
+ }
14
+ /** 执行命令 */
15
+ execute(name, ...args) {
16
+ return this.get(name)?.(null, ...args);
17
+ }
5
18
  }
6
19
  exports.default = ModCommand;
@@ -7,6 +7,10 @@ class ModMessage {
7
7
  this.main = main;
8
8
  this.handler = new Reglist_1.Reglist(main, "message.handler");
9
9
  }
10
- handle(msg) { }
10
+ handle(msg) {
11
+ this.handler.getList().forEach((handler) => {
12
+ handler(msg);
13
+ });
14
+ }
11
15
  }
12
16
  exports.ModMessage = ModMessage;
@@ -6,17 +6,15 @@ export declare class ModPlugin<T extends FloatingLive> {
6
6
  private list;
7
7
  private pluginEvent;
8
8
  private main;
9
- private helper;
9
+ private exports;
10
10
  constructor(main: T);
11
11
  /** 获取当前安装插件 */
12
12
  get currentPlugin(): string | null;
13
13
  addPluginEvent(eventName: string, listener: (...args: any[]) => void): void;
14
- /** 获取插件操作对象 */
15
- get(name: string): {
16
- [key: string]: any;
17
- } | undefined;
14
+ /** 访问插件操作对象 */
15
+ access(name: string): object | undefined;
18
16
  /** 注册插件 */
19
- register(plugin: FloatingLivePlugin<T>): void | object;
17
+ register(pluginFunc: FloatingLivePlugin<T>): object | undefined;
20
18
  /** 移除插件 */
21
19
  unregister(name: string): void;
22
20
  }
@@ -7,8 +7,8 @@ class ModPlugin {
7
7
  /** 插件列表 */
8
8
  this.list = new Map();
9
9
  this.pluginEvent = new Map();
10
+ this.exports = new Map();
10
11
  this.main = main;
11
- this.helper = main.helper;
12
12
  }
13
13
  /** 获取当前安装插件 */
14
14
  get currentPlugin() {
@@ -22,62 +22,41 @@ class ModPlugin {
22
22
  this.pluginEvent.get(this.currentPlugin)?.push([eventName, listener]);
23
23
  }
24
24
  }
25
- /** 获取插件操作对象 */
26
- get(name) {
27
- return this.list.get(name);
25
+ /** 访问插件操作对象 */
26
+ access(name) {
27
+ return this.exports.get(name);
28
28
  }
29
29
  /** 注册插件 */
30
- register(plugin) {
30
+ register(pluginFunc) {
31
31
  // 执行插件函数
32
- let pluginObject = plugin();
33
- const name = pluginObject.name;
32
+ const plugin = pluginFunc();
33
+ const name = plugin.name;
34
34
  if (this.list.has(name)) {
35
35
  this.main.emit("plugin:duplicate", name);
36
36
  return;
37
37
  }
38
+ this.list.set(name, plugin);
38
39
  this.current = name;
39
- // 如果插件register函数无法调用,则无法注册插件
40
- if (typeof pluginObject.register != "function") {
41
- this.main.emit("plugin:invalid", name);
42
- this.current = null;
43
- return;
44
- }
45
- let pluginExpose = pluginObject.register(this.main);
46
- this.list.set(name, pluginExpose || {});
47
- // 执行钩子函数
48
- this.helper.pluginAddHandler.getList().forEach((handler) => {
49
- handler(name);
50
- });
40
+ // 调用register函数,并保存获取值
41
+ let pluginExpose = plugin.register?.(this.main) || {};
42
+ this.exports.set(name, pluginExpose);
51
43
  this.main.emit("plugin:add", name);
52
44
  this.current = null;
53
45
  return pluginExpose;
54
46
  }
55
47
  /** 移除插件 */
56
48
  unregister(name) {
49
+ const plugin = this.list.get(name);
57
50
  // 检测插件是否存在
58
- if (!this.list.has(name)) {
51
+ if (!plugin) {
59
52
  this.main.emit("plugin:unexist", name);
60
53
  return;
61
54
  }
62
55
  // 获取该插件的销毁函数
63
- let destroyFunc = this.helper.pluginDestory
64
- .getIdByPlugin(name)
65
- .map((id) => this.helper.pluginDestory.get(id));
56
+ plugin.destroy?.();
66
57
  // 从列表中移除插件
67
58
  this.list.delete(name);
68
- // 移除helper注册
69
- this.helper.messageHandler.unregister(name);
70
- this.helper.pluginAddHandler.unregister(name);
71
- this.helper.pluginRemoveHandler.unregister(name);
72
- this.helper.pluginDestory.unregister(name);
73
- // 执行钩子函数
74
- this.helper.pluginRemoveHandler.getList().forEach((handler) => {
75
- handler(name);
76
- });
77
- // 执行销毁函数
78
- destroyFunc.forEach((handler) => {
79
- handler?.();
80
- });
59
+ // 移除插件注册
81
60
  this.main.emit("plugin:remove", name);
82
61
  }
83
62
  }
@@ -1,6 +1,7 @@
1
1
  import { Reglist } from "../abstract/Reglist";
2
2
  import { LiveRoom } from "../abstract/LiveRoom";
3
3
  import { FloatingLive } from "..";
4
+ import { RoomInfo } from "../types";
4
5
  /** 直播间监听实例控制器 */
5
6
  export declare class ModRooms {
6
7
  readonly main: FloatingLive;
@@ -24,7 +25,7 @@ export declare class ModRooms {
24
25
  /** 获取房间 */
25
26
  get(roomKey: string): LiveRoom | undefined;
26
27
  /** 获取房间信息 */
27
- info(roomKey: string): import("../types").RoomInfo | undefined;
28
+ info(roomKey: string): RoomInfo | undefined;
28
29
  /** 更新房间信息 */
29
30
  update(roomKey: string): void;
30
31
  /** 获取roomKey列表 */
@@ -10,6 +10,30 @@ class ModRooms {
10
10
  this.configs = new Map();
11
11
  this.main = main;
12
12
  this.generator = new Reglist_1.Reglist(this.main, "rooms.generator");
13
+ this.main.state.register("rooms", () => {
14
+ const list = [];
15
+ this.roomMap.forEach((room) => {
16
+ list.push(room.info);
17
+ });
18
+ return list;
19
+ });
20
+ this.main.command.batchRegister({
21
+ add: (platform, id, open) => {
22
+ this.add(platform, id, open);
23
+ },
24
+ remove: (key) => {
25
+ this.remove(key);
26
+ },
27
+ open: (key) => {
28
+ this.open(key);
29
+ },
30
+ close: (key) => {
31
+ this.close(key);
32
+ },
33
+ update: (key) => {
34
+ this.update(key);
35
+ },
36
+ });
13
37
  }
14
38
  /** 设置登录凭据 */
15
39
  setAuth(platform, auth) {
@@ -43,9 +67,7 @@ class ModRooms {
43
67
  // 添加监听事件
44
68
  // 直播消息
45
69
  room.on("msg", (data) => {
46
- this.main.helper.messageHandler.getList().forEach((handler) => {
47
- handler(data);
48
- });
70
+ this.main.message.handle(data);
49
71
  this.main.emit("live:message", data);
50
72
  });
51
73
  // 直播消息源数据
@@ -0,0 +1,6 @@
1
+ import { FloatingLive } from "..";
2
+ import { Reglist } from "../abstract/Reglist";
3
+ export declare class ModState extends Reglist<() => object> {
4
+ constructor(main: FloatingLive);
5
+ generate(): Record<string, any>;
6
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModState = void 0;
4
+ const Reglist_1 = require("../abstract/Reglist");
5
+ class ModState extends Reglist_1.Reglist {
6
+ constructor(main) {
7
+ super(main, "state");
8
+ }
9
+ generate() {
10
+ const state = {};
11
+ this.forEach((gen, key) => {
12
+ state[key] = gen();
13
+ });
14
+ return state;
15
+ }
16
+ }
17
+ exports.ModState = ModState;
@@ -40,7 +40,7 @@ export declare type RoomStatsInfo = {
40
40
  /** 点赞数 */
41
41
  like?: number;
42
42
  /** 观看数 */
43
- watch?: number;
43
+ view?: number;
44
44
  /** 在线人数 */
45
45
  online?: number;
46
46
  /** 人气值 */