floating-live 0.15.2 → 0.15.4

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/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Floating Live
2
2
  一个用于处理和保存直播弹幕的Node.js小工具,开箱即用。
3
3
 
4
- 本体无弹幕获取功能,需要配合[floating-live-danmaku](https://github.com/Minteea/floating-live-danmaku)中的弹幕获取插件使用。
4
+ 本体无弹幕获取功能,需要配合[floating-live-plugins](https://github.com/Minteea/floating-live-plugins)中的插件使用。
5
5
 
6
- 目前支持bilibili和AcFun的直播弹幕,不同平台的弹幕会转换为同一种格式便于保存。
6
+ 目前插件库支持bilibili和AcFun的直播弹幕,不同平台的弹幕会转换为同一种格式便于保存。
7
7
 
8
8
  项目仍在开发中,功能尚未完善,目前除了记录保存直播弹幕外暂时没有别的用处啦,敬请期待。
9
9
 
package/dist/live.d.ts CHANGED
@@ -7,7 +7,7 @@ import { ModCommand } from "./module/command";
7
7
  import { ModValue } from "./module/value";
8
8
  import { ModHook } from "./module/hook";
9
9
  import { FloatingEventMap } from "./types/events";
10
- import { FloatingCommandMap } from "./types";
10
+ import { ErrorOptions, FloatingCommandMap } from "./types";
11
11
  export declare class FloatingLive extends EventEmitter {
12
12
  /** 房间控制模块 */
13
13
  room: ModRoom;
@@ -25,11 +25,14 @@ export declare class FloatingLive extends EventEmitter {
25
25
  call<T extends keyof FloatingCommandMap>(name: T, ...args: Parameters<FloatingCommandMap[T]>): ReturnType<FloatingCommandMap[T]>;
26
26
  on<T extends keyof FloatingEventMap>(eventName: T, listener: FloatingEventMap[T]): this;
27
27
  emit<T extends keyof FloatingEventMap>(eventName: T, ...args: Parameters<FloatingEventMap[T]>): boolean;
28
- throw(err: Error): void;
28
+ /** 抛出错误 */
29
+ throw(err: Error, options?: ErrorOptions): void;
30
+ throw(options: ErrorOptions): void;
29
31
  getSnapshot(): {
30
32
  room: import("./types").RoomInfo[];
31
33
  value: Record<string, any>;
32
34
  manifest: Record<string, Record<string, any>>;
33
35
  command: string[];
36
+ hook: Record<string, any>;
34
37
  };
35
38
  }
package/dist/live.js CHANGED
@@ -29,8 +29,13 @@ class FloatingLive extends events_1.EventEmitter {
29
29
  super.emit("event", eventName, ...args);
30
30
  return emit;
31
31
  }
32
- throw(err) {
33
- this.emit("error", err);
32
+ throw(err, options) {
33
+ if (err instanceof Error) {
34
+ throw Object.assign(err, options);
35
+ }
36
+ else {
37
+ throw Object.assign(new Error(), err);
38
+ }
34
39
  }
35
40
  getSnapshot() {
36
41
  return {
@@ -38,6 +43,7 @@ class FloatingLive extends events_1.EventEmitter {
38
43
  value: this.value.getSnapshot(),
39
44
  manifest: this.manifest.getSnapshot(),
40
45
  command: this.command.getSnapshot(),
46
+ hook: this.value.getSnapshot(),
41
47
  };
42
48
  }
43
49
  }
@@ -19,7 +19,16 @@ class ModCommand {
19
19
  }
20
20
  /** 调用命令 */
21
21
  call(name, ...args) {
22
- return this.list.get(name)?.(...args);
22
+ const command = this.list.get(name);
23
+ if (!command) {
24
+ this.main.throw({
25
+ message: "命令不存在",
26
+ id: "command:call_unexist",
27
+ });
28
+ }
29
+ else {
30
+ return command(...args);
31
+ }
23
32
  }
24
33
  /** 检测命令是否存在 */
25
34
  has(name) {
@@ -1,16 +1,14 @@
1
1
  import { FloatingLive } from "../live";
2
- import { PluginConstructor } from "../types/plugin";
2
+ import { PluginItem, PluginConstructor } from "../types/plugin";
3
3
  export declare class ModPlugin {
4
4
  /** 插件列表 */
5
5
  private list;
6
6
  protected readonly main: FloatingLive;
7
7
  constructor(main: FloatingLive);
8
8
  /** 注册插件 */
9
- register(pluginFunc: PluginConstructor, options?: any): Promise<void> | undefined;
9
+ register(pluginFunc: PluginConstructor, options?: Record<string, any>): Promise<PluginItem | undefined>;
10
10
  /** 移除插件 */
11
- unregister(name: string): void;
11
+ unregister(name: string): Promise<void>;
12
12
  /** 获取插件实例 */
13
- get(name: string): {
14
- [key: string]: any;
15
- } | undefined;
13
+ get(name: string): PluginItem | undefined;
16
14
  }
@@ -8,52 +8,52 @@ class ModPlugin {
8
8
  this.main = main;
9
9
  }
10
10
  /** 注册插件 */
11
- register(pluginFunc, options) {
11
+ async register(pluginFunc, options = {}) {
12
12
  const name = pluginFunc.pluginName;
13
13
  if (!name) {
14
- this.main.throw(Object.assign(new TypeError("缺少插件id"), {
15
- from: "plugin",
16
- signal: "plugin_id_missing",
17
- }));
18
- return;
14
+ this.main.throw({
15
+ message: "缺少插件id",
16
+ id: "plugin:register_id_missing",
17
+ });
19
18
  }
20
19
  if (this.list.has(name)) {
21
- this.main.throw(Object.assign(new Error("插件id重复"), {
22
- from: "plugin",
23
- signal: "plugin_id_duplicate",
24
- }));
25
- return;
20
+ this.main.throw({
21
+ message: "已存在相同id的插件",
22
+ id: "plugin:register_id_duplicate",
23
+ });
26
24
  }
27
- return this.main.hook
25
+ return await this.main.hook
28
26
  .call("plugin.register", { name, options })
29
- .then((res) => {
27
+ .then(async (res) => {
30
28
  if (!res)
31
29
  return;
32
30
  // 执行插件函数
33
31
  const plugin = new pluginFunc(this.main, options);
34
32
  this.list.set(name, plugin);
35
33
  // 调用插件的register钩子
36
- plugin.register?.(this.main, options);
34
+ await plugin.register?.(this.main, options);
37
35
  this.main.emit("plugin:add", name);
36
+ return plugin;
38
37
  });
39
38
  }
40
39
  /** 移除插件 */
41
- unregister(name) {
40
+ async unregister(name) {
42
41
  const plugin = this.list.get(name);
43
42
  // 检测插件是否存在
44
43
  if (!plugin) {
45
- this.main.throw(Object.assign(new Error("插件不存在"), {
46
- from: "plugin",
47
- signal: "plugin_unexist",
48
- }));
49
- return;
44
+ this.main.throw({
45
+ message: "插件不存在",
46
+ id: "plugin:unregister_unexist",
47
+ });
48
+ }
49
+ else {
50
+ // 调用该插件的destroy钩子
51
+ await plugin.destroy?.();
52
+ // 从列表中移除插件
53
+ this.list.delete(name);
54
+ // 移除插件注册
55
+ this.main.emit("plugin:remove", name);
50
56
  }
51
- // 调用该插件的destroy钩子
52
- plugin.destroy?.();
53
- // 从列表中移除插件
54
- this.list.delete(name);
55
- // 移除插件注册
56
- this.main.emit("plugin:remove", name);
57
57
  }
58
58
  /** 获取插件实例 */
59
59
  get(name) {
@@ -9,7 +9,7 @@ export declare class ModRoom {
9
9
  /** 添加房间监听实例 */
10
10
  addRoom(room: LiveRoom, open?: boolean): void;
11
11
  /** 添加房间 */
12
- add(platform: string, id: number, options?: boolean | Record<string, any>): void;
12
+ add(platform: string, id: number, options?: boolean | Record<string, any>): Promise<void>;
13
13
  /** 移除房间 */
14
14
  remove(roomKey: string): void;
15
15
  /** 获取房间 */
@@ -17,10 +17,10 @@ class ModRoom {
17
17
  addRoom(room, open) {
18
18
  let key = room.key;
19
19
  if (this.list.has(key)) {
20
- this.main.throw(Object.assign(new Error("房间已存在"), {
21
- from: "room",
22
- signal: "room_exist",
23
- }));
20
+ this.main.throw({
21
+ message: "房间已存在",
22
+ id: "room:add_exist",
23
+ });
24
24
  return;
25
25
  }
26
26
  this.list.set(key, room);
@@ -81,24 +81,29 @@ class ModRoom {
81
81
  this.main.emit("room:add", key, room.info);
82
82
  }
83
83
  /** 添加房间 */
84
- add(platform, id, options) {
85
- const opt = typeof options == "boolean" ? { open: options } : options;
86
- const ctx = { platform, id, options: opt || {} };
87
- this.main.hook.call("room.add", ctx).then((res) => {
88
- if (!res)
89
- return;
90
- const room = this.main.call(`${platform}.room.create`, id, opt);
91
- room && this.addRoom(room);
92
- });
84
+ async add(platform, id, options) {
85
+ const opt = typeof options == "boolean" ? { open: options } : options || {};
86
+ const ctx = { platform, id, options: opt };
87
+ const res = await this.main.hook.call("room.add", ctx);
88
+ if (!res) {
89
+ this.main.throw({ message: "无法添加房间", id: "room:add_fail" });
90
+ }
91
+ const room = this.main.call(`${platform}.room.create`, id, opt);
92
+ if (!room) {
93
+ this.main.throw({ message: "无法创建房间", id: "room:create_fail" });
94
+ }
95
+ else {
96
+ this.addRoom(room);
97
+ }
93
98
  }
94
99
  /** 移除房间 */
95
100
  remove(roomKey) {
96
101
  let room = this.list.get(roomKey);
97
102
  if (!room) {
98
- this.main.throw(Object.assign(new Error("房间不存在"), {
99
- from: "room",
100
- signal: "room_unexist",
101
- }));
103
+ this.main.throw({
104
+ message: "房间不存在",
105
+ id: "room:remove_unexist",
106
+ });
102
107
  }
103
108
  this.list.delete(roomKey); // 从表中删除房间
104
109
  room?.removeAllListeners(); // 移除房间监听实例
@@ -1,4 +1,5 @@
1
1
  import { LiveRoom } from "../core";
2
+ import { RoomInfo } from "./room";
2
3
  export interface FloatingCommandMap {
3
4
  add: (platform: string, id: number, options?: boolean | Record<string, any>) => void;
4
5
  remove: (key: string) => void;
@@ -6,4 +7,5 @@ export interface FloatingCommandMap {
6
7
  close: (key: string) => void;
7
8
  update: (key: string) => void;
8
9
  [name: `${string}.room.create`]: (id: string | number, options?: Record<string, any>) => LiveRoom;
10
+ [name: `${string}.room.info`]: (id: string | number) => RoomInfo;
9
11
  }
@@ -0,0 +1,8 @@
1
+ export interface ErrorOptions {
2
+ /** 错误消息 */
3
+ message?: string;
4
+ /** 错误id */
5
+ id?: string;
6
+ /** 错误名称 */
7
+ [key: string]: any;
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -6,3 +6,4 @@ export * from "./events";
6
6
  export * from "./manifest";
7
7
  export * from "./hook";
8
8
  export * from "./value";
9
+ export * from "./error";
@@ -22,3 +22,4 @@ __exportStar(require("./events"), exports);
22
22
  __exportStar(require("./manifest"), exports);
23
23
  __exportStar(require("./hook"), exports);
24
24
  __exportStar(require("./value"), exports);
25
+ __exportStar(require("./error"), exports);
@@ -1,16 +1,13 @@
1
1
  import { FloatingLive } from "../live";
2
- /** 插件模块, 加载时调用 */
3
- export type FloatingLivePlugin<C extends object = object> = () => {
4
- /** 插件名称 */
5
- name: string;
6
- register: (ctx: FloatingLive, options?: C) => void;
7
- destroy?: (ctx: FloatingLive) => void;
8
- };
9
- export interface IPlugin {
10
- register?(ctx: FloatingLive, options: any): void;
11
- destroy?(): void;
2
+ /** 插件对象 */
3
+ export interface PluginItem {
4
+ pluginName?: string;
5
+ register?(ctx: FloatingLive, options: any): void | Promise<void>;
6
+ destroy?(): void | Promise<void>;
7
+ [name: string]: any;
12
8
  }
9
+ /** 插件构造器 */
13
10
  export interface PluginConstructor {
14
11
  pluginName: string;
15
- new (ctx: FloatingLive, options: any): IPlugin;
12
+ new (ctx: FloatingLive, options: any): PluginItem;
16
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floating-live",
3
- "version": "0.15.2",
3
+ "version": "0.15.4",
4
4
  "type": "commonjs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",