node-karin 0.11.13 → 0.11.15

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 (61) hide show
  1. package/config/defSet/config.yaml +3 -3
  2. package/config/view/config.yaml +5 -5
  3. package/lib/adapter/index.js +2 -2
  4. package/lib/cli/index.d.ts +7 -3
  5. package/lib/cli/index.js +258 -233
  6. package/lib/cli/init.js +15 -14
  7. package/lib/cli/karin.js +15 -15
  8. package/lib/cli/pkg.d.ts +4 -0
  9. package/lib/cli/pkg.js +14 -0
  10. package/lib/cli/start.js +8 -0
  11. package/lib/core/index.js +10 -10
  12. package/lib/core/init/config.d.ts +0 -4
  13. package/lib/core/init/config.js +0 -13
  14. package/lib/core/init/init.js +1 -0
  15. package/lib/core/listener/listener.js +1 -1
  16. package/lib/core/process/process.js +2 -6
  17. package/lib/core/server/server.js +13 -1
  18. package/lib/db/index.js +2 -2
  19. package/lib/db/level/level.js +0 -1
  20. package/lib/db/redis/redis_level.d.ts +2 -0
  21. package/lib/db/redis/redis_level.js +12 -11
  22. package/lib/event/index.js +5 -5
  23. package/lib/modules/art-template.js +1 -1
  24. package/lib/modules/axios.js +2 -2
  25. package/lib/modules/chalk.js +2 -2
  26. package/lib/modules/chokidar.js +2 -2
  27. package/lib/modules/commander.js +2 -2
  28. package/lib/modules/express.js +3 -3
  29. package/lib/modules/level.js +2 -2
  30. package/lib/modules/lodash.js +1 -1
  31. package/lib/modules/log4js.js +2 -2
  32. package/lib/modules/moment.js +1 -1
  33. package/lib/modules/node-schedule.js +2 -2
  34. package/lib/modules/redis.js +2 -2
  35. package/lib/modules/ws.js +3 -3
  36. package/lib/modules/yaml.js +2 -2
  37. package/lib/render/app.js +82 -81
  38. package/lib/render/base.js +54 -54
  39. package/lib/render/client.js +144 -142
  40. package/lib/render/client_even.js +140 -137
  41. package/lib/render/http.js +40 -41
  42. package/lib/render/index.js +6 -6
  43. package/lib/render/server.js +93 -88
  44. package/lib/render/wormhole.js +153 -151
  45. package/lib/types/config/config.d.ts +2 -2
  46. package/lib/types/index.js +13 -13
  47. package/lib/types/type/global.d.ts +2 -0
  48. package/lib/utils/index.d.ts +2 -0
  49. package/lib/utils/index.js +13 -11
  50. package/lib/utils/tools/exec.d.ts +5 -17
  51. package/lib/utils/tools/exec.js +28 -26
  52. package/lib/utils/tools/ffmpeg.d.ts +2 -2
  53. package/lib/utils/tools/restart.d.ts +15 -0
  54. package/lib/utils/tools/restart.js +39 -0
  55. package/lib/utils/tools/stop.d.ts +7 -0
  56. package/lib/utils/tools/stop.js +13 -0
  57. package/lib/utils/tools/update.d.ts +26 -84
  58. package/lib/utils/tools/update.js +40 -28
  59. package/package.json +6 -3
  60. package/lib/cli/dev.js +0 -3
  61. /package/lib/cli/{dev.d.ts → start.d.ts} +0 -0
package/lib/cli/init.js CHANGED
@@ -1,21 +1,22 @@
1
1
  #!/usr/bin/env node
2
- import { KarinCfgInit } from '../core/init/config.js'
2
+ import { getRegistry } from './pkg.js';
3
+ import { KarinCfgInit } from '../core/init/config.js';
3
4
  /**
4
5
  * 休眠函数
5
6
  * @param ms 毫秒
6
7
  */
7
- function sleep (ms) {
8
- return new Promise(resolve => setTimeout(resolve, ms))
8
+ function sleep(ms) {
9
+ return new Promise(resolve => setTimeout(resolve, ms));
9
10
  }
10
- async function main () {
11
- /** 捕获错误 打印日志 */
12
- process.on('uncaughtException', err => console.error(err))
13
- process.on('unhandledRejection', err => console.error(err))
14
- const init = new KarinCfgInit()
15
- init.init()
16
- await sleep(1000)
17
- const pkg = init.getRegistry()
18
- /** 结果 */
19
- await init.install(pkg)
11
+ async function main() {
12
+ /** 捕获错误 打印日志 */
13
+ process.on('uncaughtException', err => console.error(err));
14
+ process.on('unhandledRejection', err => console.error(err));
15
+ const init = new KarinCfgInit();
16
+ init.init();
17
+ await sleep(1000);
18
+ const pkg = getRegistry();
19
+ /** 结果 */
20
+ await init.install(pkg);
20
21
  }
21
- main().then(() => process.exit(0)).catch(() => process.exit(0))
22
+ main().then(() => process.exit(0)).catch(() => process.exit(0));
package/lib/cli/karin.js CHANGED
@@ -1,16 +1,16 @@
1
1
  #!/usr/bin/env node
2
- import { program } from 'commander'
3
- import { KarinCli } from './index.js'
4
- const cli = new KarinCli()
5
- program.version(cli.pkg(true).version, '-v, --version', '显示版本号')
6
- program.command('.').description('启动karin').action(() => cli.start('prod' /* Mode.Prod */, 'js' /* Lang.Js */, 'node' /* Runner.Node */))
7
- program.command('start').description('启动karin').action(() => cli.start('prod' /* Mode.Prod */, 'js' /* Lang.Js */, 'node' /* Runner.Node */))
8
- program.command('pm2').description('后台运行karin').action(() => cli.start('prod' /* Mode.Prod */, 'js' /* Lang.Js */, 'pm2' /* Runner.Pm2 */))
9
- program.command('stop').description('停止后台运行').action(() => cli.stop())
10
- program.command('rs').description('重启pm2服务').action(() => cli.restart())
11
- program.command('dev').description('JavaScript开发模式').action(() => cli.start('dev' /* Mode.Dev */, 'js' /* Lang.Js */, 'node' /* Runner.Node */))
12
- program.command('ts').description('TypeScript开发模式').action(() => cli.start('dev' /* Mode.Dev */, 'ts' /* Lang.Ts */, 'tsx' /* Runner.Tsx */))
13
- program.command('log').description('查看日志').action(() => cli.log())
14
- program.command('up').description('更新依赖').action(() => cli.update())
15
- program.command('init').description('初始化karin').action(() => { import('./init.js') })
16
- program.parse(process.argv)
2
+ import { program } from 'commander';
3
+ import { KarinCli } from './index.js';
4
+ const cli = new KarinCli();
5
+ program.version(cli.pkg(true).version, '-v, --version', '显示版本号');
6
+ program.command('.').description('启动karin').action(() => cli.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "node" /* Runner.Node */));
7
+ program.command('start').description('启动karin').action(() => cli.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "node" /* Runner.Node */));
8
+ program.command('pm2').description('后台运行karin').action(() => cli.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "pm2" /* Runner.Pm2 */));
9
+ program.command('stop').description('停止后台运行').action(() => cli.stop());
10
+ program.command('rs').description('重启pm2服务').action(() => cli.restart());
11
+ program.command('dev').description('JavaScript开发模式').action(() => cli.start("dev" /* Mode.Dev */, "js" /* Lang.Js */, "node" /* Runner.Node */));
12
+ program.command('ts').description('TypeScript开发模式').action(() => cli.start("dev" /* Mode.Dev */, "ts" /* Lang.Ts */, "tsx" /* Runner.Tsx */));
13
+ program.command('log').description('查看日志').action(() => cli.log());
14
+ program.command('up').description('更新依赖').action(() => cli.update());
15
+ program.command('init').description('初始化karin').action(() => { import('./init.js'); });
16
+ program.parse(process.argv);
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 获取当前的包管理器 根据锁文件判断 优先级 pnpm > yarn > npm > cnpm
3
+ */
4
+ export declare const getRegistry: () => "pnpm" | "cnpm" | "yarn" | "npm";
package/lib/cli/pkg.js ADDED
@@ -0,0 +1,14 @@
1
+ import fs from 'fs';
2
+ /**
3
+ * 获取当前的包管理器 根据锁文件判断 优先级 pnpm > yarn > npm > cnpm
4
+ */
5
+ export const getRegistry = () => {
6
+ if (fs.existsSync('./pnpm-lock.yaml'))
7
+ return 'pnpm';
8
+ if (fs.existsSync('./yarn.lock'))
9
+ return 'yarn';
10
+ if (fs.existsSync('./package-lock.json'))
11
+ return 'npm';
12
+ // cnpm 没有锁文件
13
+ return 'cnpm';
14
+ };
@@ -0,0 +1,8 @@
1
+ import { program } from 'commander';
2
+ import { KarinCli } from './index.js';
3
+ const karin = new KarinCli();
4
+ program.version(karin.pkg(true).version, '-v, --version', '显示版本号');
5
+ program.command('.').description('启动karin').action(() => karin.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "node" /* Runner.Node */));
6
+ program.command('start').description('启动karin').action(() => karin.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "node" /* Runner.Node */));
7
+ program.command('dev').description('dev开发模式').action(() => karin.start("dev" /* Mode.Dev */, "js" /* Lang.Js */, "node" /* Runner.Node */));
8
+ program.parse(process.argv);
package/lib/core/index.js CHANGED
@@ -1,10 +1,10 @@
1
- export * from './karin/karin.js'
2
- export * from './init/dir.js'
3
- export * from './init/init.js'
4
- export * from './listener/listener.js'
5
- export * from './plugin/base.js'
6
- export * from '../types/plugin/app.js'
7
- export * from './plugin/loader.js'
8
- export * from './process/process.js'
9
- export * from './server/server.js'
10
- import '../adapter/input/index.js'
1
+ export * from './karin/karin.js';
2
+ export * from './init/dir.js';
3
+ export * from './init/init.js';
4
+ export * from './listener/listener.js';
5
+ export * from './plugin/base.js';
6
+ export * from '../types/plugin/app.js';
7
+ export * from './plugin/loader.js';
8
+ export * from './process/process.js';
9
+ export * from './server/server.js';
10
+ import '../adapter/input/index.js';
@@ -40,8 +40,4 @@ export declare class KarinCfgInit {
40
40
  * @param cmd - 命令
41
41
  */
42
42
  shell(cmd: string): Promise<unknown>;
43
- /**
44
- * 获取当前的包管理器 根据锁文件判断
45
- */
46
- getRegistry(): 'pnpm' | 'cnpm' | 'yarn' | 'npm';
47
43
  }
@@ -198,17 +198,4 @@ export class KarinCfgInit {
198
198
  });
199
199
  });
200
200
  }
201
- /**
202
- * 获取当前的包管理器 根据锁文件判断
203
- */
204
- getRegistry() {
205
- if (fs.existsSync('./pnpm-lock.yaml'))
206
- return 'pnpm';
207
- if (fs.existsSync('./yarn.lock'))
208
- return 'yarn';
209
- if (fs.existsSync('./package-lock.json'))
210
- return 'npm';
211
- // cnpm 没有锁文件
212
- return 'cnpm';
213
- }
214
201
  }
@@ -3,6 +3,7 @@ import { logger } from '../../utils/index.js';
3
3
  * 启动日志
4
4
  */
5
5
  logger.mark('Karin 启动中...');
6
+ logger.mark(`当前版本: ${process.env.karin_app_version}`);
6
7
  logger.mark('https://github.com/KarinJS/Karin');
7
8
  /**
8
9
  * 设置标题
@@ -71,7 +71,7 @@ export class Listeners extends EventEmitter {
71
71
  }
72
72
  const element = [
73
73
  segment.reply(message_id),
74
- segment.text(`Karin 重启成功:${restartTime}秒`),
74
+ segment.text(`\nKarin 重启成功:${restartTime}秒`),
75
75
  ];
76
76
  await this.sendMsg(id, contact, element);
77
77
  await level.del(key);
@@ -54,7 +54,7 @@ export default class Process {
54
54
  /**
55
55
  * 使用api来检查后台
56
56
  */
57
- const res = await common.axios(host + '/ping', 'get', { timeout: 100 });
57
+ const res = await common.axios(host + '/ping', 'get', { timeout: 2000 });
58
58
  if (!res)
59
59
  return this;
60
60
  logger.mark(logger.red('检测到后台进程 正在关闭'));
@@ -74,11 +74,7 @@ export default class Process {
74
74
  * 根据配置文件判断是否继续
75
75
  */
76
76
  logger.error(logger.red(`后台进程关闭失败,请检查是否有进程正在占用端口${config.Server.http.port}`));
77
- if (!config.Config.multi_progress) {
78
- logger.error(logger.red('当前配置不允许多进程运行,程序即将退出'));
79
- await this.exit(1);
80
- }
81
- logger.error(logger.red('当前配置允许多进程运行,程序继续运行'));
77
+ await this.exit();
82
78
  return this;
83
79
  }
84
80
  /**
@@ -1,8 +1,9 @@
1
1
  import fs from 'fs';
2
- import Process from '../process/process.js';
2
+ import { level } from '../../db/index.js';
3
3
  import { WebSocketServer } from 'ws';
4
4
  import { createServer } from 'http';
5
5
  import { karin } from '../karin/karin.js';
6
+ import Process from '../process/process.js';
6
7
  import express from 'express';
7
8
  import { exec, config, logger, common } from '../../utils/index.js';
8
9
  import { AdapterOneBot11 } from '../../adapter/onebot/11/index.js';
@@ -29,6 +30,7 @@ export const server = new (class Server {
29
30
  try {
30
31
  /** 防止多进程端口冲突 启动失败 */
31
32
  await Process.check();
33
+ level.open();
32
34
  this.WebSocketServer.on('connection', (socket, request) => {
33
35
  const path = request.url;
34
36
  const headers = request.headers;
@@ -75,6 +77,16 @@ export const server = new (class Server {
75
77
  /** 关闭服务器 */
76
78
  karin.emit('exit.grpc');
77
79
  this.server.close();
80
+ try {
81
+ await level.close();
82
+ const redis = (await import('../../db/index.js')).redis;
83
+ if (redis && redis.id === 'RedisLevel')
84
+ await redis.level.close();
85
+ }
86
+ catch (error) {
87
+ logger.error('[服务器][HTTP] 关闭数据库失败');
88
+ logger.error(error);
89
+ }
78
90
  /** 如果是pm2 获取当前pm2ID 使用 */
79
91
  if (process.env.pm_id)
80
92
  await exec(`pm2 delete ${process.env.pm_id}`);
package/lib/db/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export * from './level/level.js'
2
- export * from './redis/redis.js'
1
+ export * from './level/level.js';
2
+ export * from './redis/redis.js';
@@ -34,4 +34,3 @@ export default class LevelDB extends Level {
34
34
  }
35
35
  }
36
36
  export const level = new LevelDB();
37
- level.open();
@@ -1,5 +1,7 @@
1
+ import { Level } from 'level';
1
2
  export default class RedisLevel {
2
3
  #private;
4
+ level: Level<string, string>;
3
5
  /** 唯一标识符 用于区分不同的数据库 */
4
6
  id: string;
5
7
  constructor();
@@ -1,14 +1,13 @@
1
1
  import { Level } from 'level';
2
2
  export default class RedisLevel {
3
- #level;
3
+ level;
4
4
  /** 过期时间映射表 */
5
5
  #expireMap;
6
6
  /** 唯一标识符 用于区分不同的数据库 */
7
7
  id;
8
8
  constructor() {
9
9
  const path = process.cwd() + '/data/db/RedisLevel';
10
- this.#level = new Level(path, { valueEncoding: 'json' });
11
- this.#level.open();
10
+ this.level = new Level(path, { valueEncoding: 'json' });
12
11
  this.id = 'RedisLevel';
13
12
  this.#expireMap = new Map();
14
13
  this.#expireHandle();
@@ -23,7 +22,7 @@ export default class RedisLevel {
23
22
  const entries = Array.from(this.#expireMap.entries());
24
23
  for (const [key, expire] of entries) {
25
24
  if (expire < now) {
26
- await this.#level.del(key);
25
+ await this.level.del(key);
27
26
  this.#expireMap.delete(key); // 通过代理的方式删除键值对
28
27
  }
29
28
  }
@@ -56,6 +55,8 @@ export default class RedisLevel {
56
55
  },
57
56
  };
58
57
  this.#expireMap = new Proxy(this.#expireMap, handler);
58
+ /** 延迟2秒执行 */
59
+ setTimeout(async () => this.level.open(), 2000);
59
60
  }
60
61
  /**
61
62
  * get 获取数据
@@ -67,11 +68,11 @@ export default class RedisLevel {
67
68
  /** 先查过期时间 */
68
69
  const expire = this.#expireMap.get(key);
69
70
  if (expire && expire < Date.now()) {
70
- await this.#level.del(key);
71
+ await this.level.del(key);
71
72
  this.#expireMap.delete(key);
72
73
  return null;
73
74
  }
74
- return await this.#level.get(key);
75
+ return await this.level.get(key);
75
76
  }
76
77
  catch (error) {
77
78
  if (error.notFound)
@@ -89,7 +90,7 @@ export default class RedisLevel {
89
90
  if (options && options.EX) {
90
91
  this.#expireMap.set(key, Date.now() + options.EX * 1000);
91
92
  }
92
- return await this.#level.put(key, value);
93
+ return await this.level.put(key, value);
93
94
  }
94
95
  /**
95
96
  * del 删除数据
@@ -97,7 +98,7 @@ export default class RedisLevel {
97
98
  */
98
99
  async del(key) {
99
100
  this.#expireMap.delete(key);
100
- return await this.#level.del(key);
101
+ return await this.level.del(key);
101
102
  }
102
103
  /**
103
104
  * keys 获取所有键
@@ -106,7 +107,7 @@ export default class RedisLevel {
106
107
  async keys(prefix = '') {
107
108
  /** 去掉末尾的* */
108
109
  prefix = prefix.replace(/\*$/, '');
109
- const list = await this.#level.keys({ gte: prefix, lt: `${prefix}\xFF` }).all();
110
+ const list = await this.level.keys({ gte: prefix, lt: `${prefix}\xFF` }).all();
110
111
  this.#checkKeys(list);
111
112
  return list;
112
113
  }
@@ -118,7 +119,7 @@ export default class RedisLevel {
118
119
  for (const key of keys) {
119
120
  const expire = this.#expireMap.get(key);
120
121
  if (expire && expire < Date.now()) {
121
- await this.#level.del(key);
122
+ await this.level.del(key);
122
123
  this.#expireMap.delete(key);
123
124
  }
124
125
  }
@@ -192,7 +193,7 @@ export default class RedisLevel {
192
193
  */
193
194
  async setEx(key, seconds, value) {
194
195
  this.#expireMap.set(key, Date.now() + seconds * 1000);
195
- return await this.#level.put(key, value);
196
+ return await this.level.put(key, value);
196
197
  }
197
198
  /**
198
199
  * exists 判断键是否存在
@@ -1,5 +1,5 @@
1
- export * from './handler/base.js'
2
- export * from './handler/message.js'
3
- export * from './handler/notice.js'
4
- export * from './handler/request.js'
5
- export * from './handler/review.js'
1
+ export * from './handler/base.js';
2
+ export * from './handler/message.js';
3
+ export * from './handler/notice.js';
4
+ export * from './handler/request.js';
5
+ export * from './handler/review.js';
@@ -1 +1 @@
1
- export { default } from 'art-template'
1
+ export { default } from 'art-template';
@@ -1,2 +1,2 @@
1
- export * from 'axios'
2
- export { default } from 'axios'
1
+ export * from 'axios';
2
+ export { default } from 'axios';
@@ -1,2 +1,2 @@
1
- export * from 'chalk'
2
- export { default } from 'chalk'
1
+ export * from 'chalk';
2
+ export { default } from 'chalk';
@@ -1,2 +1,2 @@
1
- export * from 'chokidar'
2
- export { default } from 'chokidar'
1
+ export * from 'chokidar';
2
+ export { default } from 'chokidar';
@@ -1,2 +1,2 @@
1
- export * from 'commander'
2
- export { default } from 'commander'
1
+ export * from 'commander';
2
+ export { default } from 'commander';
@@ -1,3 +1,3 @@
1
- import { query, Router } from 'express'
2
- export { default } from 'express'
3
- export { query, Router }
1
+ import { query, Router } from 'express';
2
+ export { default } from 'express';
3
+ export { query, Router };
@@ -1,2 +1,2 @@
1
- export * from 'level'
2
- export { default } from 'level'
1
+ export * from 'level';
2
+ export { default } from 'level';
@@ -1 +1 @@
1
- export { default } from 'lodash'
1
+ export { default } from 'lodash';
@@ -1,2 +1,2 @@
1
- export * from 'log4js'
2
- export { default } from 'log4js'
1
+ export * from 'log4js';
2
+ export { default } from 'log4js';
@@ -1 +1 @@
1
- export { default } from 'moment'
1
+ export { default } from 'moment';
@@ -1,2 +1,2 @@
1
- export * from 'node-schedule'
2
- export { default } from 'node-schedule'
1
+ export * from 'node-schedule';
2
+ export { default } from 'node-schedule';
@@ -1,2 +1,2 @@
1
- export * from 'redis'
2
- export { default } from 'redis'
1
+ export * from 'redis';
2
+ export { default } from 'redis';
package/lib/modules/ws.js CHANGED
@@ -1,3 +1,3 @@
1
- import { WebSocket, WebSocketServer, createWebSocketStream } from 'ws'
2
- export { WebSocket, WebSocketServer, createWebSocketStream }
3
- export { default } from 'ws'
1
+ import { WebSocket, WebSocketServer, createWebSocketStream } from 'ws';
2
+ export { WebSocket, WebSocketServer, createWebSocketStream };
3
+ export { default } from 'ws';
@@ -1,2 +1,2 @@
1
- export * from 'yaml'
2
- export { default } from 'yaml'
1
+ export * from 'yaml';
2
+ export { default } from 'yaml';