node-karin 0.2.0 → 0.2.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.
Files changed (83) hide show
  1. package/config/defSet/config.yaml +3 -0
  2. package/lib/adapter/onebot/onebot11.d.ts +4 -6
  3. package/lib/adapter/onebot/onebot11.js +3 -8
  4. package/lib/core/index.d.ts +9 -0
  5. package/lib/core/index.js +9 -0
  6. package/lib/core/init.d.ts +1 -0
  7. package/lib/core/init.js +20 -2
  8. package/lib/core/karin.d.ts +2 -4
  9. package/lib/core/karin.js +2 -2
  10. package/lib/core/listener.d.ts +2 -5
  11. package/lib/core/listener.js +9 -11
  12. package/lib/core/plugin.app.d.ts +1 -1
  13. package/lib/core/plugin.d.ts +2 -4
  14. package/lib/core/plugin.js +1 -1
  15. package/lib/core/plugin.loader.d.ts +6 -7
  16. package/lib/core/plugin.loader.js +13 -16
  17. package/lib/core/process.d.ts +18 -0
  18. package/lib/core/process.js +98 -0
  19. package/lib/core/server.d.ts +4 -5
  20. package/lib/core/server.js +11 -13
  21. package/lib/db/index.d.ts +4 -0
  22. package/lib/db/index.js +4 -0
  23. package/lib/db/level.d.ts +1 -3
  24. package/lib/db/level.js +1 -3
  25. package/lib/db/redis.js +1 -2
  26. package/lib/event/event.d.ts +1 -3
  27. package/lib/event/event.handler.d.ts +1 -2
  28. package/lib/event/event.handler.js +13 -16
  29. package/lib/event/index.d.ts +7 -0
  30. package/lib/event/index.js +9 -0
  31. package/lib/event/message.d.ts +1 -2
  32. package/lib/event/message.handler.d.ts +1 -1
  33. package/lib/event/message.handler.js +12 -15
  34. package/lib/event/notice.d.ts +1 -1
  35. package/lib/event/request.d.ts +1 -1
  36. package/lib/event/review.handler.d.ts +12 -15
  37. package/lib/event/review.handler.js +6 -7
  38. package/lib/index.d.ts +152 -26
  39. package/lib/index.js +24 -37
  40. package/lib/modules.d.ts +14 -0
  41. package/lib/modules.js +14 -0
  42. package/lib/renderer/app.d.ts +14 -21
  43. package/lib/renderer/app.js +12 -11
  44. package/lib/renderer/base.d.ts +1 -1
  45. package/lib/renderer/base.js +2 -3
  46. package/lib/renderer/client.d.ts +2 -2
  47. package/lib/renderer/client.js +7 -8
  48. package/lib/renderer/http.d.ts +2 -2
  49. package/lib/renderer/http.js +2 -2
  50. package/lib/renderer/index.d.ts +6 -0
  51. package/lib/renderer/index.js +6 -0
  52. package/lib/renderer/server.d.ts +4 -4
  53. package/lib/renderer/server.js +7 -7
  54. package/lib/renderer/wormhole.d.ts +1 -1
  55. package/lib/renderer/wormhole.js +7 -7
  56. package/lib/tools/init.js +1 -1
  57. package/lib/types/adapter.d.ts +1 -1
  58. package/lib/types/config.d.ts +9 -3
  59. package/lib/types/{types.d.ts → event.d.ts} +1 -3
  60. package/lib/types/index.d.ts +3 -2
  61. package/lib/types/index.js +3 -2
  62. package/lib/types/plugin.d.ts +6 -6
  63. package/lib/utils/button.d.ts +8 -9
  64. package/lib/utils/button.js +1 -1
  65. package/lib/utils/common.d.ts +10 -5
  66. package/lib/utils/common.js +20 -8
  67. package/lib/utils/config.d.ts +6 -5
  68. package/lib/utils/config.js +6 -6
  69. package/lib/utils/ffmpeg.js +2 -2
  70. package/lib/utils/handler.d.ts +3 -4
  71. package/lib/utils/handler.js +1 -1
  72. package/lib/utils/index.d.ts +11 -0
  73. package/lib/utils/index.js +11 -0
  74. package/lib/utils/logger.d.ts +1 -1
  75. package/lib/utils/logger.js +4 -4
  76. package/lib/utils/segment.d.ts +2 -3
  77. package/lib/utils/segment.js +1 -1
  78. package/lib/utils/update.d.ts +1 -2
  79. package/lib/utils/update.js +1 -1
  80. package/package.json +3 -2
  81. /package/lib/types/{types.js → event.js} +0 -0
  82. /package/lib/utils/{YamlEditor.d.ts → yamlEditor.d.ts} +0 -0
  83. /package/lib/utils/{YamlEditor.js → yamlEditor.js} +0 -0
@@ -11,6 +11,9 @@ log4jsCfg:
11
11
  # 日志文件最大大小 MB
12
12
  maxLogSize: 30
13
13
 
14
+ # 关闭后台进程失败后是否继续启动 继续启动会导致多进程
15
+ multi_progress: false
16
+
14
17
  # 控制台触发插件日志颜色 十六进制 默认#FFFF00 不支持热更新
15
18
  log_color: "#E1D919"
16
19
 
@@ -2,9 +2,7 @@
2
2
  import WebSocket from 'ws';
3
3
  import { IncomingMessage } from 'http';
4
4
  import { KarinAdapter } from '../../types/adapter.js';
5
- import { CustomNodeSegment, OneBot11Api, OneBot11ApiParamsType, OneBot11Segment } from '../../types/onebots11.js';
6
- import { Scene, contact } from '../../types/types.js';
7
- import { KarinElement } from '../../types/element.js';
5
+ import { Scene, contact, OneBot11Api, KarinElement, OneBot11Segment, CustomNodeSegment, OneBot11ApiParamsType } from '../../types/index.js';
8
6
  /**
9
7
  * @class OneBot11
10
8
  * @extends KarinAdapter
@@ -44,13 +42,13 @@ export declare class OneBot11 implements KarinAdapter {
44
42
  * @param {Array<{type: string, data: any}>} data onebot11格式消息
45
43
  * @return karin格式消息
46
44
  * */
47
- AdapterConvertKarin(data: Array<OneBot11Segment>): (import("../../types/element.js").TextElement | import("../../types/element.js").AtElement | import("../../types/element.js").FaceElement | import("../../types/element.js").ReplyElement | import("../../types/element.js").ImageElement | import("../../types/element.js").VoiceElement | import("../../types/element.js").VideoElement | import("../../types/element.js").PokeElement | import("../../types/element.js").LocationElement | import("../../types/element.js").ForwardElement | import("../../types/element.js").ContactElement | import("../../types/element.js").JsonElement | import("../../types/element.js").XmlElement)[];
45
+ AdapterConvertKarin(data: Array<OneBot11Segment>): (import("../../types/index.js").TextElement | import("../../types/index.js").AtElement | import("../../types/index.js").FaceElement | import("../../types/index.js").ReplyElement | import("../../types/index.js").ImageElement | import("../../types/index.js").VoiceElement | import("../../types/index.js").VideoElement | import("../../types/index.js").PokeElement | import("../../types/index.js").LocationElement | import("../../types/index.js").ForwardElement | import("../../types/index.js").ContactElement | import("../../types/index.js").JsonElement | import("../../types/index.js").XmlElement)[];
48
46
  /**
49
47
  * karin转onebot11
50
48
  * @param data karin格式消息
51
49
  * @return {Array<{type: string, data: any}>} onebot11格式消息
52
50
  * */
53
- KarinConvertAdapter(data: Array<KarinElement>): (import("../../types/element.js").BubbleFaceElement | import("../../types/element.js").RecordElement | import("../../types/element.js").BasketballElement | import("../../types/element.js").DiceElement | import("../../types/element.js").RpsElement | import("../../types/element.js").WeatherElement | import("../../types/element.js").LocationElement | import("../../types/element.js").ShareElement | import("../../types/element.js").GiftElement | import("../../types/element.js").MarketFaceElement | import("../../types/element.js").ContactElement | import("../../types/element.js").RowElement | import("../../types/element.js").LongMsgElement | {
51
+ KarinConvertAdapter(data: Array<KarinElement>): (import("../../types/index.js").BubbleFaceElement | import("../../types/index.js").RecordElement | import("../../types/index.js").BasketballElement | import("../../types/index.js").DiceElement | import("../../types/index.js").RpsElement | import("../../types/index.js").WeatherElement | import("../../types/index.js").LocationElement | import("../../types/index.js").ShareElement | import("../../types/index.js").GiftElement | import("../../types/index.js").MarketFaceElement | import("../../types/index.js").ContactElement | import("../../types/index.js").RowElement | import("../../types/index.js").LongMsgElement | {
54
52
  type: string;
55
53
  data: {
56
54
  text: string;
@@ -245,7 +243,7 @@ export declare class OneBot11 implements KarinAdapter {
245
243
  message_seq: any;
246
244
  contact: contact;
247
245
  sender: any;
248
- elements: (import("../../types/element.js").TextElement | import("../../types/element.js").AtElement | import("../../types/element.js").FaceElement | import("../../types/element.js").ReplyElement | import("../../types/element.js").ImageElement | import("../../types/element.js").VoiceElement | import("../../types/element.js").VideoElement | import("../../types/element.js").PokeElement | import("../../types/element.js").LocationElement | import("../../types/element.js").ForwardElement | import("../../types/element.js").ContactElement | import("../../types/element.js").JsonElement | import("../../types/element.js").XmlElement)[];
246
+ elements: (import("../../types/index.js").TextElement | import("../../types/index.js").AtElement | import("../../types/index.js").FaceElement | import("../../types/index.js").ReplyElement | import("../../types/index.js").ImageElement | import("../../types/index.js").VoiceElement | import("../../types/index.js").VideoElement | import("../../types/index.js").PokeElement | import("../../types/index.js").LocationElement | import("../../types/index.js").ForwardElement | import("../../types/index.js").ContactElement | import("../../types/index.js").JsonElement | import("../../types/index.js").XmlElement)[];
249
247
  }[]>;
250
248
  /**
251
249
  * 获取合并转发消息
@@ -1,13 +1,8 @@
1
- import { KarinMessage } from '../../event/message.js';
2
- import { KarinNotice } from '../../event/notice.js';
3
- import { KarinRequest } from '../../event/request.js';
4
1
  import WebSocket from 'ws';
5
2
  import { randomUUID } from 'crypto';
6
- import logger from '../../utils/logger.js';
7
- import common from '../../utils/common.js';
8
- import listener from '../../core/listener.js';
9
- import config from '../../utils/config.js';
10
- import segment from '../../utils/segment.js';
3
+ import { listener } from '../../core/listener.js';
4
+ import { common, config, logger, segment } from '../../utils/index.js';
5
+ import { KarinMessage, KarinNotice, KarinRequest } from '../../event/index.js';
11
6
  /**
12
7
  * @class OneBot11
13
8
  * @extends KarinAdapter
@@ -0,0 +1,9 @@
1
+ export * from './dir.js';
2
+ export * from './init.js';
3
+ export * from './karin.js';
4
+ export * from './listener.js';
5
+ export * from './plugin.js';
6
+ export * from './plugin.app.js';
7
+ export * from './plugin.loader.js';
8
+ export * from './process.js';
9
+ export * from './server.js';
@@ -0,0 +1,9 @@
1
+ export * from './dir.js'
2
+ export * from './init.js'
3
+ export * from './karin.js'
4
+ export * from './listener.js'
5
+ export * from './plugin.js'
6
+ export * from './plugin.app.js'
7
+ export * from './plugin.loader.js'
8
+ export * from './process.js'
9
+ export * from './server.js'
@@ -0,0 +1 @@
1
+ export {};
package/lib/core/init.js CHANGED
@@ -1,4 +1,22 @@
1
- 'use strict'
2
- process.title = 'karin'
1
+ import { logger } from '../utils/index.js'
2
+ /**
3
+ * 启动日志
4
+ */
5
+ logger.mark('Karin 启动中...')
6
+ logger.mark('https://github.com/KarinJS/Karin')
7
+ /**
8
+ * 设置标题
9
+ */
10
+ process.title = 'Karin'
11
+ /**
12
+ * 设置时区
13
+ */
14
+ process.env.TZ = 'Asia/Shanghai'
15
+ /**
16
+ * 设置应用模式
17
+ */
3
18
  process.env.karin_app_mode = process.argv[2]?.includes('dev') ? 'dev' : 'prod'
19
+ /**
20
+ * 设置语言环境
21
+ */
4
22
  process.env.karin_app_lang = JSON.stringify(process.execArgv).includes('tsx@') ? 'ts' : 'js'
@@ -1,7 +1,5 @@
1
- import { Permission } from '../types/types.js';
2
- import { PluginApps } from '../types/plugin.js';
3
- import { KarinElement } from '../types/element.js';
4
1
  import { KarinMessage } from '../event/message.js';
2
+ import { Permission, PluginApps, KarinElement } from '../types/index.js';
5
3
  type FncFunction = (e: KarinMessage) => Promise<boolean>;
6
4
  type FncElement = string | KarinElement | Array<KarinElement>;
7
5
  export interface OptionsCommand {
@@ -55,7 +53,7 @@ export interface OptionsElement extends OptionsCommand {
55
53
  */
56
54
  stop?: boolean;
57
55
  }
58
- export default class Karin {
56
+ export declare class Karin {
59
57
  /**
60
58
  * @param reg - 正则表达式
61
59
  * @param fnc - 函数
package/lib/core/karin.js CHANGED
@@ -1,6 +1,6 @@
1
- import common from '../utils/common.js'
2
1
  import PluginApp from './plugin.app.js'
3
- export default class Karin {
2
+ import { common } from '../utils/index.js'
3
+ export class Karin {
4
4
  /**
5
5
  * - 快速构建命令
6
6
  * @param reg - 正则表达式
@@ -1,12 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
- import { KarinAdapter } from '../types/adapter.js';
4
- import { contact } from '../types/types.js';
5
- import { KarinElement } from '../types/element.js';
3
+ import { KarinAdapter, contact, KarinElement } from '../types/index.js';
6
4
  /**
7
5
  * 监听器管理
8
6
  */
9
- declare const _default: {
7
+ export declare const listener: {
10
8
  /**
11
9
  * Bot索引
12
10
  * @type - Bot索引
@@ -118,4 +116,3 @@ declare const _default: {
118
116
  prependOnceListener<K_11>(eventName: string | symbol, listener: (...args: any[]) => void): any;
119
117
  eventNames(): (string | symbol)[];
120
118
  };
121
- export default _default;
@@ -1,13 +1,11 @@
1
1
  import { EventEmitter } from 'events'
2
- import loader from './plugin.loader.js'
3
- import Common from '../utils/common.js'
4
- import logger from '../utils/logger.js'
5
- import Config from '../utils/config.js'
6
- import MessageHandller from '../event/message.handler.js'
2
+ import { PluginLoader } from './plugin.loader.js'
3
+ import { common, logger, config } from '../utils/index.js'
4
+ import { MessageHandler } from '../event/message.handler.js'
7
5
  /**
8
6
  * 监听器管理
9
7
  */
10
- export default new (class Listeners extends EventEmitter {
8
+ export const listener = new (class Listeners extends EventEmitter {
11
9
  /**
12
10
  * Bot索引
13
11
  * @type - Bot索引
@@ -26,10 +24,10 @@ export default new (class Listeners extends EventEmitter {
26
24
  this.list = []
27
25
  this.adapter = []
28
26
  this.on('error', data => logger.error(data))
29
- this.on('plugin', () => loader.load())
27
+ this.on('plugin', () => PluginLoader.load())
30
28
  this.on('adapter', data => {
31
29
  let path = data.path || '无'
32
- if (path && data.type !== 'grpc') { path = `ws://127.0.0.1:/${Config.Server.http.port}${data.path}` }
30
+ if (path && data.type !== 'grpc') { path = `ws://127.0.0.1:/${config.Server.http.port}${data.path}` }
33
31
  path = logger.green(path)
34
32
  logger.info(`[适配器][注册][${data.type}] ` + path)
35
33
  this.addAdapter(data)
@@ -39,7 +37,7 @@ export default new (class Listeners extends EventEmitter {
39
37
  logger.info(`[机器人][注册][${data.type}] ` + logger.green(`[account:${data.bot.account.uid || data.bot.account.uin}(${data.bot.account.name})]`))
40
38
  this.emit('karin:online', data.bot.account.uid || data.bot.account.uin)
41
39
  })
42
- this.on('message', data => new MessageHandller(data))
40
+ this.on('message', data => new MessageHandler(data))
43
41
  }
44
42
 
45
43
  /**
@@ -160,10 +158,10 @@ export default new (class Listeners extends EventEmitter {
160
158
  if (!bot) { throw new Error('发送消息失败: 未找到对应Bot实例') }
161
159
  const { recallMsg, retry_count } = options
162
160
  /** 标准化 */
163
- const NewElements = Common.makeMessage(elements)
161
+ const NewElements = common.makeMessage(elements)
164
162
  /** 结果 */
165
163
  let result = { message_id: '' }
166
- const reply_log = Common.makeMessageLog(NewElements)
164
+ const reply_log = common.makeMessageLog(NewElements)
167
165
  const self_id = bot.account.uid || bot.account.uin
168
166
  if (contact.scene === 'group') {
169
167
  logger.bot('info', self_id, `Send Proactive Group ${contact.peer}: ${reply_log}`)
@@ -1,4 +1,4 @@
1
- import { dirName, fileName, PluginApps } from '../types/plugin.js';
1
+ import { dirName, fileName, PluginApps } from '../types/index.js';
2
2
  export interface PluginAppType {
3
3
  file?: {
4
4
  dir?: dirName;
@@ -1,10 +1,8 @@
1
- import { Plugin as PluginType } from '../types/plugin.js';
2
- import { KarinElement, KarinNodeElement } from '../types/element.js';
3
- import { E } from '../types/types.js';
1
+ import { PluginType as PluginType, KarinElement, KarinNodeElement, E } from '../types/index.js';
4
2
  /**
5
3
  * 插件基类
6
4
  */
7
- export default class Plugin implements PluginType {
5
+ export declare class Plugin implements PluginType {
8
6
  e: PluginType['e'];
9
7
  init?: () => void;
10
8
  accept?: (e: E) => Promise<void>;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 插件基类
3
3
  */
4
- export default class Plugin {
4
+ export class Plugin {
5
5
  e
6
6
  init
7
7
  accept
@@ -1,12 +1,12 @@
1
1
  import path from 'path';
2
2
  import chokidar from 'chokidar';
3
3
  import schedule from 'node-schedule';
4
- import Plugin from './plugin.js';
5
- import { Plugin as PluginType, PluginApps, PluginTask, dirName, fileName, AppInfo } from '../types/plugin.js';
4
+ import { Plugin } from './plugin.js';
5
+ import { PluginType as PluginType, PluginApps, PluginTask, dirName, fileName, AppInfo } from '../types/index.js';
6
6
  /**
7
7
  * 加载插件
8
8
  */
9
- declare const _default: {
9
+ export declare const PluginLoader: {
10
10
  dir: './plugins';
11
11
  dirPath: string;
12
12
  /**
@@ -112,11 +112,11 @@ declare const _default: {
112
112
  event: import("../types/index.js").Event | "message.group_message" | "message.friend_message" | "message.guild_message" | "message.nearby" | "message.stranger" | "message.stranger_from_group" | "message.friend_apply" | "message.group_apply" | "message.invited_group" | "message.friend_poke" | "message.friend_recall" | "message.friend_file_uploaded" | "message.group_poke" | "message.group_card_changed" | "message.group_member_unique_title_changed" | "message.group_essence_changed" | "message.group_recall" | "message.group_member_increase" | "message.group_member_decrease" | "message.group_admin_changed" | "message.group_member_ban" | "message.group_sign" | "message.group_whole_ban" | "message.group_file_uploaded" | "request.group_message" | "request.friend_message" | "request.guild_message" | "request.nearby" | "request.stranger" | "request.stranger_from_group" | "request.friend_apply" | "request.group_apply" | "request.invited_group" | "request.friend_poke" | "request.friend_recall" | "request.friend_file_uploaded" | "request.group_poke" | "request.group_card_changed" | "request.group_member_unique_title_changed" | "request.group_essence_changed" | "request.group_recall" | "request.group_member_increase" | "request.group_member_decrease" | "request.group_admin_changed" | "request.group_member_ban" | "request.group_sign" | "request.group_whole_ban" | "request.group_file_uploaded" | "notice.group_message" | "notice.friend_message" | "notice.guild_message" | "notice.nearby" | "notice.stranger" | "notice.stranger_from_group" | "notice.friend_apply" | "notice.group_apply" | "notice.invited_group" | "notice.friend_poke" | "notice.friend_recall" | "notice.friend_file_uploaded" | "notice.group_poke" | "notice.group_card_changed" | "notice.group_member_unique_title_changed" | "notice.group_essence_changed" | "notice.group_recall" | "notice.group_member_increase" | "notice.group_member_decrease" | "notice.group_admin_changed" | "notice.group_member_ban" | "notice.group_sign" | "notice.group_whole_ban" | "notice.group_file_uploaded" | "meta_event.group_message" | "meta_event.friend_message" | "meta_event.guild_message" | "meta_event.nearby" | "meta_event.stranger" | "meta_event.stranger_from_group" | "meta_event.friend_apply" | "meta_event.group_apply" | "meta_event.invited_group" | "meta_event.friend_poke" | "meta_event.friend_recall" | "meta_event.friend_file_uploaded" | "meta_event.group_poke" | "meta_event.group_card_changed" | "meta_event.group_member_unique_title_changed" | "meta_event.group_essence_changed" | "meta_event.group_recall" | "meta_event.group_member_increase" | "meta_event.group_member_decrease" | "meta_event.group_admin_changed" | "meta_event.group_member_ban" | "meta_event.group_sign" | "meta_event.group_whole_ban" | "meta_event.group_file_uploaded" | "message_sent.group_message" | "message_sent.friend_message" | "message_sent.guild_message" | "message_sent.nearby" | "message_sent.stranger" | "message_sent.stranger_from_group" | "message_sent.friend_apply" | "message_sent.group_apply" | "message_sent.invited_group" | "message_sent.friend_poke" | "message_sent.friend_recall" | "message_sent.friend_file_uploaded" | "message_sent.group_poke" | "message_sent.group_card_changed" | "message_sent.group_member_unique_title_changed" | "message_sent.group_essence_changed" | "message_sent.group_recall" | "message_sent.group_member_increase" | "message_sent.group_member_decrease" | "message_sent.group_admin_changed" | "message_sent.group_member_ban" | "message_sent.group_sign" | "message_sent.group_whole_ban" | "message_sent.group_file_uploaded";
113
113
  priority: number;
114
114
  accept: boolean;
115
- rule: import("../types/plugin.js").PluginRule[];
115
+ rule: import("../types/index.js").PluginRule[];
116
116
  type: "function" | "class";
117
117
  task: PluginTask[];
118
- handler: import("../types/plugin.js").PluginHandler[];
119
- button: import("../types/plugin.js").PluginButton[];
118
+ handler: import("../types/index.js").PluginHandler[];
119
+ button: import("../types/index.js").PluginButton[];
120
120
  };
121
121
  /** 排序 */
122
122
  orderBy(): void;
@@ -146,4 +146,3 @@ declare const _default: {
146
146
  */
147
147
  watchDir(dir: dirName): void;
148
148
  };
149
- export default _default;
@@ -1,18 +1,15 @@
1
1
  import fs from 'fs'
2
- import lodash from 'lodash'
3
2
  import path from 'path'
3
+ import lodash from 'lodash'
4
4
  import schedule from 'node-schedule'
5
- import Renderer from '../renderer/app.js'
6
- import button from '../utils/button.js'
7
- import Common from '../utils/common.js'
8
- import handler from '../utils/handler.js'
9
- import listener from './listener.js'
10
- import logger from '../utils/logger.js'
5
+ import { listener } from './listener.js'
11
6
  import PluginApp from './plugin.app.js'
7
+ import { render } from '../renderer/index.js'
8
+ import { button, common, handler, logger } from '../utils/index.js'
12
9
  /**
13
10
  * 加载插件
14
11
  */
15
- export default new (class PluginLoader {
12
+ export const PluginLoader = new (class PluginLoader {
16
13
  dir
17
14
  dirPath
18
15
  /**
@@ -42,7 +39,7 @@ export default new (class PluginLoader {
42
39
  PluginList
43
40
  constructor () {
44
41
  this.dir = './plugins'
45
- this.dirPath = Common.urlToPath(import.meta.url)
42
+ this.dirPath = common.urlToPath(import.meta.url)
46
43
  this.Apps = []
47
44
  this.task = []
48
45
  this.watcher = new Map()
@@ -76,7 +73,7 @@ export default new (class PluginLoader {
76
73
  })
77
74
  logger.info(`[按钮][${button.Apps.length}个] 加载完成`)
78
75
  logger.info(`[插件][${this.Apps.length}个] 加载完成`)
79
- logger.info(`[渲染器][${Renderer.Apps.length}个] 加载完成`)
76
+ logger.info(`[渲染器][${render.Apps.length}个] 加载完成`)
80
77
  logger.info(`[定时任务][${this.task.length}个] 加载完成`)
81
78
  logger.info(`[Handler][Key:${handlerKeys.length}个][fnc:${handlerCount}个] 加载完成`)
82
79
  logger.info(logger.green('-----------'))
@@ -93,7 +90,7 @@ export default new (class PluginLoader {
93
90
  getPlugins () {
94
91
  const Apps = []
95
92
  /** 获取所有插件包 */
96
- const plugins = Common.getPlugins()
93
+ const plugins = common.getPlugins()
97
94
  const isTs = process.env.karin_app_lang === 'ts'
98
95
  for (const dir of plugins) {
99
96
  /**
@@ -102,7 +99,7 @@ export default new (class PluginLoader {
102
99
  */
103
100
  const PluginPath = `${this.dir}/${dir}`
104
101
  // 非插件包
105
- if (!Common.isPlugin(PluginPath)) {
102
+ if (!common.isPlugin(PluginPath)) {
106
103
  const list = fs.readdirSync(`${this.dir}/${dir}`, {
107
104
  withFileTypes: true,
108
105
  })
@@ -114,22 +111,22 @@ export default new (class PluginLoader {
114
111
  }
115
112
  }
116
113
  /** js环境 */
117
- if (Common.exists(`${PluginPath}/index.js`)) {
114
+ if (common.exists(`${PluginPath}/index.js`)) {
118
115
  Apps.push({ dir, name: 'index.js' })
119
116
  }
120
117
  const appList = ['apps', 'dist/apps']
121
118
  appList.forEach(app => {
122
- if (Common.isDir(`${PluginPath}/${app}`)) {
119
+ if (common.isDir(`${PluginPath}/${app}`)) {
123
120
  const result = this.getApps((`${dir}/${app}`))
124
121
  Apps.push(...result)
125
122
  }
126
123
  })
127
124
  /** ts环境 */
128
125
  if (isTs) {
129
- if (Common.exists(`${PluginPath}/index.ts`)) {
126
+ if (common.exists(`${PluginPath}/index.ts`)) {
130
127
  Apps.push({ dir, name: 'index.ts' })
131
128
  }
132
- if (Common.isDir(`${PluginPath}/src/apps`)) {
129
+ if (common.isDir(`${PluginPath}/src/apps`)) {
133
130
  const result = this.getApps((`${dir}/src/apps`), true)
134
131
  Apps.push(...result)
135
132
  }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * 处理基本事件
3
+ */
4
+ export default class Process {
5
+ /**
6
+ * 进程初始化
7
+ */
8
+ static process(): Promise<Process>;
9
+ /**
10
+ * 检查后台进程
11
+ */
12
+ static check(): Promise<Process>;
13
+ /**
14
+ * 退出Karin
15
+ * @param code 退出码
16
+ */
17
+ static exit(code?: any): Promise<void>;
18
+ }
@@ -0,0 +1,98 @@
1
+ import { listener } from './listener.js'
2
+ import { logger, common, config } from '../utils/index.js'
3
+ /**
4
+ * 处理基本事件
5
+ */
6
+ export default class Process {
7
+ /**
8
+ * 进程初始化
9
+ */
10
+ static async process () {
11
+ /**
12
+ * 监听挂起信号 在终端关闭时触发
13
+ */
14
+ process.once('SIGHUP', async (code) => await this.exit(code))
15
+ /**
16
+ * 监听中断信号 用户按下 Ctrl + C 时触发
17
+ */
18
+ process.once('SIGINT', async (code) => await this.exit(code))
19
+ /**
20
+ * 监听终止信号 程序正常退出时触发
21
+ */
22
+ process.once('SIGTERM', async (code) => await this.exit(code))
23
+ /**
24
+ * 监听退出信号 windows下程序正常退出时触发
25
+ */
26
+ process.once('SIGBREAK', async (code) => await this.exit(code))
27
+ /**
28
+ * 监听退出信号 与 SIGINT 类似,但会生成核心转储
29
+ */
30
+ process.once('SIGQUIT', async (code) => await this.exit(code))
31
+ /**
32
+ * 监听退出信号 Node.js进程退出时触发
33
+ */
34
+ process.once('exit', async (code) => await this.exit(code))
35
+ /**
36
+ * 捕获警告
37
+ */
38
+ process.on('warning', warning => listener.emit('warn', warning))
39
+ /**
40
+ * 捕获错误
41
+ */
42
+ process.on('uncaughtException', error => listener.emit('error', error))
43
+ /**
44
+ * 捕获未处理的Promise错误
45
+ */
46
+ process.on('unhandledRejection', error => listener.emit('error', error))
47
+ return this
48
+ }
49
+
50
+ /**
51
+ * 检查后台进程
52
+ */
53
+ static async check () {
54
+ const host = `http://localhost:${config.Server.http.port}/api`
55
+ /**
56
+ * 使用api来检查后台
57
+ */
58
+ const res = await common.axios(host + '/info', 'get', { timeout: 100 })
59
+ if (!res) { return this }
60
+ logger.mark(logger.red('检测到后台进程 正在关闭'))
61
+ /** 发退出信号 */
62
+ await common.axios(host + '/exit', 'get', { timeout: 10 })
63
+ for (let i = 0; i < 50; i++) {
64
+ const res = await common.axios(host + '/info', 'get', { timeout: 100 })
65
+ /** 请求成功继续循环 */
66
+ if (res) { continue }
67
+ /** 请求异常即代表后台进程已关闭 */
68
+ logger.mark(logger.green('后台进程已关闭'))
69
+ return this
70
+ }
71
+ /**
72
+ * 走到这里说明后台关闭失败
73
+ * 根据配置文件判断是否继续
74
+ */
75
+ logger.error(logger.red('后台进程关闭失败,请手动关闭'))
76
+ if (!config.Config.multi_progress) {
77
+ logger.error(logger.red('当前配置不允许多进程运行,程序即将退出'))
78
+ await this.exit(1)
79
+ }
80
+ logger.error(logger.red('当前配置允许多进程运行,程序继续运行'))
81
+ return this
82
+ }
83
+
84
+ /**
85
+ * 退出Karin
86
+ * @param code 退出码
87
+ */
88
+ static async exit (code = 0) {
89
+ try {
90
+ const { redis } = await import('../db/index.js')
91
+ if (redis && redis.save) { await redis.save() }
92
+ logger.mark(`Karin 已停止运行 运行时间:${common.uptime()} 退出码:${code || '未知'}`)
93
+ } finally {
94
+ process.exit()
95
+ }
96
+ }
97
+ }
98
+ Process.process()
@@ -1,7 +1,7 @@
1
- import { Express } from 'express';
2
1
  import { WebSocketServer } from 'ws';
2
+ import { Express } from 'express';
3
3
  import { Server as ServerType, ServerResponse, IncomingMessage } from 'http';
4
- declare const _default: {
4
+ export declare const server: {
5
5
  reg: RegExp;
6
6
  list: string[];
7
7
  app: Express;
@@ -11,7 +11,7 @@ declare const _default: {
11
11
  /**
12
12
  * 监听WebSocket连接并初始化http服务器
13
13
  */
14
- init(): false | any;
14
+ init(): Promise<false | any>;
15
15
  /**
16
16
  * HTTP渲染器
17
17
  */
@@ -21,6 +21,5 @@ declare const _default: {
21
21
  */
22
22
  staticPath(): void;
23
23
  /** 重启当前HTTP服务器 */
24
- "__#3@#restartServer"(): Promise<void>;
24
+ "__#13@#restartServer"(): Promise<void>;
25
25
  };
26
- export default _default;
@@ -1,16 +1,12 @@
1
1
  import fs from 'fs'
2
- import express from 'express'
3
- import { createServer } from 'http'
2
+ import Process from './process.js'
3
+ import { listener } from './listener.js'
4
4
  import { WebSocketServer } from 'ws'
5
- import { render } from '../index.js'
6
- import connect from '../renderer/wormhole.js'
7
- import HttpRenderer from '../renderer/http.js'
8
- import logger from '../utils/logger.js'
9
- import common from '../utils/common.js'
10
- import config from '../utils/config.js'
11
- import listener from './listener.js'
12
- import exec from '../utils/exec.js'
13
- export default new (class Server {
5
+ import { createServer } from 'http'
6
+ import express from 'express'
7
+ import { exec, config, logger, common } from '../utils/index.js'
8
+ import { render, HttpRenderer, Wormhole } from '../renderer/index.js'
9
+ export const server = new (class Server {
14
10
  reg
15
11
  list
16
12
  app
@@ -29,8 +25,10 @@ export default new (class Server {
29
25
  /**
30
26
  * 监听WebSocket连接并初始化http服务器
31
27
  */
32
- init () {
28
+ async init () {
33
29
  try {
30
+ // 防止多进程端口冲突 启动失败
31
+ await Process.check()
34
32
  this.WebSocketServer.on('connection', (socket, request) => {
35
33
  const path = request.url
36
34
  const headers = request.headers
@@ -90,7 +88,7 @@ export default new (class Server {
90
88
  if (enable) {
91
89
  this.static()
92
90
  if (WormholeClient) {
93
- connect()
91
+ Wormhole()
94
92
  return this
95
93
  }
96
94
  const { host, post, token } = config.Server.HttpRender
@@ -0,0 +1,4 @@
1
+ import LevelDB from './level.js';
2
+ import { RedisClientType } from 'redis';
3
+ export declare const level: LevelDB;
4
+ export declare const redis: RedisClientType;
@@ -0,0 +1,4 @@
1
+ import Redis from './redis.js'
2
+ import LevelDB from './level.js'
3
+ export const level = new LevelDB()
4
+ export const redis = await new Redis().start()
package/lib/db/level.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Level } from 'level';
2
2
  /**
3
3
  * @type {Level}
4
4
  */
5
- declare class LevelDB extends Level {
5
+ export default class LevelDB extends Level {
6
6
  id: string;
7
7
  constructor();
8
8
  /**
@@ -16,5 +16,3 @@ declare class LevelDB extends Level {
16
16
  */
17
17
  set(key: string, value: string): Promise<void>;
18
18
  }
19
- declare const level: LevelDB;
20
- export default level;
package/lib/db/level.js CHANGED
@@ -3,7 +3,7 @@ const path = process.cwd() + '/data/db/Level'
3
3
  /**
4
4
  * @type {Level}
5
5
  */
6
- class LevelDB extends Level {
6
+ export default class LevelDB extends Level {
7
7
  id
8
8
  constructor () {
9
9
  super(path, { valueEncoding: 'json' })
@@ -34,5 +34,3 @@ class LevelDB extends Level {
34
34
  return await super.put(key, value)
35
35
  }
36
36
  }
37
- const level = new LevelDB()
38
- export default level
package/lib/db/redis.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { exec } from 'child_process'
2
2
  import RedisLevel from './redis_level.js'
3
- import logger from '../utils/logger.js'
4
- import config from '../utils/config.js'
3
+ import { logger, config } from '../utils/index.js'
5
4
  import { createClient, createCluster } from 'redis'
6
5
  export default class Redis {
7
6
  id
@@ -1,6 +1,4 @@
1
- import { Reply, replyCallback } from '../types/reply.js';
2
- import { KarinAdapter } from '../types/adapter.js';
3
- import { Event, contact, Sender, SubEventForEvent } from '../types/types.js';
1
+ import { KarinAdapter, Reply, replyCallback, Event, contact, Sender, SubEventForEvent } from '../types/index.js';
4
2
  /**
5
3
  * - 事件基类 所有事件都继承自此类并且需要实现此类的所有属性
6
4
  * @class KarinEvent