node-karin 0.12.0 → 0.12.2-5.pr.206.027f2f2

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 (254) hide show
  1. package/CHANGELOG.md +1029 -0
  2. package/default/comment/config.json +34 -0
  3. package/default/comment/friendDirect.json +10 -0
  4. package/default/comment/groupGuild.json +11 -0
  5. package/default/comment/server.json +17 -0
  6. package/default/config/adapter.json +28 -0
  7. package/default/config/config.json +47 -0
  8. package/default/config/groups.json +47 -0
  9. package/default/config/groups.yaml +70 -0
  10. package/default/config/pm2.json +16 -0
  11. package/default/config/privates.json +23 -0
  12. package/default/config/privates.yaml +31 -0
  13. package/default/config/redis.json +10 -0
  14. package/default/config/render.json +19 -0
  15. package/default/view/group.yaml +73 -0
  16. package/dist/index.d.ts +9869 -0
  17. package/dist/index.js +12643 -0
  18. package/dist/module/art-template.cjs +11 -0
  19. package/dist/module/art-template.d.ts +1 -0
  20. package/dist/module/axios.cjs +22 -0
  21. package/dist/module/axios.d.ts +2 -0
  22. package/dist/module/chalk.cjs +22 -0
  23. package/dist/module/chalk.d.ts +2 -0
  24. package/dist/module/chokidar.cjs +22 -0
  25. package/dist/module/chokidar.d.ts +2 -0
  26. package/dist/module/commander.cjs +35 -0
  27. package/dist/module/commander.d.ts +3 -0
  28. package/dist/module/commander.js +3 -0
  29. package/dist/module/dotenv.cjs +22 -0
  30. package/dist/module/dotenv.d.ts +2 -0
  31. package/dist/module/dotenv.js +2 -0
  32. package/dist/module/express.cjs +112 -0
  33. package/dist/module/express.d.ts +1 -0
  34. package/dist/module/express.js +1 -0
  35. package/dist/module/level.cjs +22 -0
  36. package/dist/module/level.d.ts +2 -0
  37. package/dist/module/lodash.cjs +11 -0
  38. package/dist/module/lodash.d.ts +1 -0
  39. package/dist/module/log4js.cjs +22 -0
  40. package/dist/module/log4js.d.ts +2 -0
  41. package/dist/module/moment.cjs +11 -0
  42. package/dist/module/moment.d.ts +1 -0
  43. package/dist/module/node-schedule.cjs +22 -0
  44. package/dist/module/node-schedule.d.ts +2 -0
  45. package/dist/module/redis.cjs +22 -0
  46. package/dist/module/redis.d.ts +2 -0
  47. package/dist/module/ws.cjs +28 -0
  48. package/dist/module/ws.d.ts +1 -0
  49. package/dist/module/ws.js +1 -0
  50. package/dist/module/yaml.cjs +22 -0
  51. package/dist/module/yaml.d.ts +2 -0
  52. package/dist/root.d.ts +40 -0
  53. package/dist/root.js +44 -0
  54. package/package.json +142 -128
  55. package/LICENSE +0 -674
  56. package/README.md +0 -56
  57. package/config/defSet/App.yaml +0 -42
  58. package/config/defSet/config.yaml +0 -65
  59. package/config/defSet/group.yaml +0 -56
  60. package/config/defSet/pm2.yaml +0 -21
  61. package/config/defSet/redis.yaml +0 -18
  62. package/config/defSet/server.yaml +0 -42
  63. package/config/view/group.yaml +0 -92
  64. package/lib/adapter/index.d.ts +0 -2
  65. package/lib/adapter/index.js +0 -2
  66. package/lib/adapter/input/index.d.ts +0 -78
  67. package/lib/adapter/input/index.js +0 -205
  68. package/lib/adapter/onebot/11/convert.d.ts +0 -12
  69. package/lib/adapter/onebot/11/convert.js +0 -167
  70. package/lib/adapter/onebot/11/event.d.ts +0 -25
  71. package/lib/adapter/onebot/11/event.js +0 -442
  72. package/lib/adapter/onebot/11/index.d.ts +0 -508
  73. package/lib/adapter/onebot/11/index.js +0 -1047
  74. package/lib/cli/index.d.ts +0 -92
  75. package/lib/cli/index.js +0 -307
  76. package/lib/cli/init.d.ts +0 -2
  77. package/lib/cli/init.js +0 -22
  78. package/lib/cli/karin.d.ts +0 -2
  79. package/lib/cli/karin.js +0 -16
  80. package/lib/cli/pkg.d.ts +0 -4
  81. package/lib/cli/pkg.js +0 -14
  82. package/lib/cli/start.d.ts +0 -1
  83. package/lib/cli/start.js +0 -8
  84. package/lib/core/index.d.ts +0 -10
  85. package/lib/core/index.js +0 -10
  86. package/lib/core/init/config.d.ts +0 -43
  87. package/lib/core/init/config.js +0 -201
  88. package/lib/core/init/dir.d.ts +0 -4
  89. package/lib/core/init/dir.js +0 -7
  90. package/lib/core/init/init.d.ts +0 -1
  91. package/lib/core/init/init.js +0 -47
  92. package/lib/core/karin/karin.d.ts +0 -198
  93. package/lib/core/karin/karin.js +0 -226
  94. package/lib/core/listener/listener.d.ts +0 -145
  95. package/lib/core/listener/listener.js +0 -235
  96. package/lib/core/plugin/base.d.ts +0 -156
  97. package/lib/core/plugin/base.js +0 -131
  98. package/lib/core/plugin/loader.d.ts +0 -141
  99. package/lib/core/plugin/loader.js +0 -618
  100. package/lib/core/process/process.d.ts +0 -18
  101. package/lib/core/process/process.js +0 -96
  102. package/lib/core/server/server.d.ts +0 -25
  103. package/lib/core/server/server.js +0 -295
  104. package/lib/db/index.d.ts +0 -2
  105. package/lib/db/index.js +0 -2
  106. package/lib/db/level/level.d.ts +0 -19
  107. package/lib/db/level/level.js +0 -36
  108. package/lib/db/redis/redis.d.ts +0 -2
  109. package/lib/db/redis/redis.js +0 -135
  110. package/lib/db/redis/redis_level.d.ts +0 -112
  111. package/lib/db/redis/redis_level.js +0 -281
  112. package/lib/event/handler/base.d.ts +0 -42
  113. package/lib/event/handler/base.js +0 -200
  114. package/lib/event/handler/message.d.ts +0 -29
  115. package/lib/event/handler/message.js +0 -307
  116. package/lib/event/handler/notice.d.ts +0 -18
  117. package/lib/event/handler/notice.js +0 -214
  118. package/lib/event/handler/request.d.ts +0 -18
  119. package/lib/event/handler/request.js +0 -115
  120. package/lib/event/handler/review.d.ts +0 -55
  121. package/lib/event/handler/review.js +0 -386
  122. package/lib/event/index.d.ts +0 -5
  123. package/lib/event/index.js +0 -5
  124. package/lib/index.d.ts +0 -8
  125. package/lib/index.js +0 -8
  126. package/lib/modules/art-template.d.ts +0 -1
  127. package/lib/modules/axios.d.ts +0 -2
  128. package/lib/modules/chalk.d.ts +0 -2
  129. package/lib/modules/chokidar.d.ts +0 -2
  130. package/lib/modules/commander.d.ts +0 -2
  131. package/lib/modules/commander.js +0 -2
  132. package/lib/modules/express.d.ts +0 -3
  133. package/lib/modules/express.js +0 -3
  134. package/lib/modules/level.d.ts +0 -2
  135. package/lib/modules/lodash.d.ts +0 -1
  136. package/lib/modules/log4js.d.ts +0 -2
  137. package/lib/modules/moment.d.ts +0 -1
  138. package/lib/modules/node-schedule.d.ts +0 -2
  139. package/lib/modules/redis.d.ts +0 -2
  140. package/lib/modules/ws.d.ts +0 -3
  141. package/lib/modules/ws.js +0 -3
  142. package/lib/modules/yaml.d.ts +0 -2
  143. package/lib/modules.d.ts +0 -15
  144. package/lib/modules.js +0 -15
  145. package/lib/render/app.d.ts +0 -56
  146. package/lib/render/app.js +0 -120
  147. package/lib/render/base.d.ts +0 -30
  148. package/lib/render/base.js +0 -69
  149. package/lib/render/client.d.ts +0 -41
  150. package/lib/render/client.js +0 -238
  151. package/lib/render/http.d.ts +0 -19
  152. package/lib/render/http.js +0 -50
  153. package/lib/render/index.d.ts +0 -6
  154. package/lib/render/index.js +0 -6
  155. package/lib/render/server.d.ts +0 -25
  156. package/lib/render/server.js +0 -101
  157. package/lib/render/wormhole.d.ts +0 -1
  158. package/lib/render/wormhole.js +0 -156
  159. package/lib/types/adapter/api.d.ts +0 -399
  160. package/lib/types/adapter/api.js +0 -1
  161. package/lib/types/adapter/base.d.ts +0 -560
  162. package/lib/types/adapter/base.js +0 -1
  163. package/lib/types/config/config.d.ts +0 -379
  164. package/lib/types/config/config.js +0 -1
  165. package/lib/types/element/element.d.ts +0 -565
  166. package/lib/types/element/element.js +0 -1
  167. package/lib/types/element/qqbot.d.ts +0 -123
  168. package/lib/types/element/qqbot.js +0 -1
  169. package/lib/types/event/contact.d.ts +0 -28
  170. package/lib/types/event/contact.js +0 -1
  171. package/lib/types/event/event.d.ts +0 -305
  172. package/lib/types/event/event.js +0 -1
  173. package/lib/types/event/index.d.ts +0 -12
  174. package/lib/types/event/index.js +0 -7
  175. package/lib/types/event/message.d.ts +0 -109
  176. package/lib/types/event/message.js +0 -74
  177. package/lib/types/event/notice.d.ts +0 -513
  178. package/lib/types/event/notice.js +0 -52
  179. package/lib/types/event/reply.d.ts +0 -50
  180. package/lib/types/event/reply.js +0 -1
  181. package/lib/types/event/request.d.ts +0 -126
  182. package/lib/types/event/request.js +0 -52
  183. package/lib/types/event/sender.d.ts +0 -44
  184. package/lib/types/event/sender.js +0 -1
  185. package/lib/types/index.d.ts +0 -13
  186. package/lib/types/index.js +0 -13
  187. package/lib/types/logger/logger.d.ts +0 -110
  188. package/lib/types/logger/logger.js +0 -1
  189. package/lib/types/onebot11/api.d.ts +0 -53
  190. package/lib/types/onebot11/api.js +0 -1
  191. package/lib/types/onebot11/event.d.ts +0 -619
  192. package/lib/types/onebot11/event.js +0 -2
  193. package/lib/types/onebot11/index.d.ts +0 -6
  194. package/lib/types/onebot11/index.js +0 -6
  195. package/lib/types/onebot11/params.d.ts +0 -609
  196. package/lib/types/onebot11/params.js +0 -1
  197. package/lib/types/onebot11/response.d.ts +0 -516
  198. package/lib/types/onebot11/response.js +0 -1
  199. package/lib/types/onebot11/segment.d.ts +0 -269
  200. package/lib/types/onebot11/segment.js +0 -1
  201. package/lib/types/onebot11/sender.d.ts +0 -59
  202. package/lib/types/onebot11/sender.js +0 -1
  203. package/lib/types/plugin/app.d.ts +0 -71
  204. package/lib/types/plugin/app.js +0 -1
  205. package/lib/types/plugin/plugin.d.ts +0 -473
  206. package/lib/types/plugin/plugin.js +0 -1
  207. package/lib/types/render/render.d.ts +0 -118
  208. package/lib/types/render/render.js +0 -1
  209. package/lib/types/type/global.d.ts +0 -25
  210. package/lib/types/type/global.js +0 -1
  211. package/lib/utils/common/common.d.ts +0 -236
  212. package/lib/utils/common/common.js +0 -708
  213. package/lib/utils/config/config.d.ts +0 -193
  214. package/lib/utils/config/config.js +0 -328
  215. package/lib/utils/config/updateVersion.d.ts +0 -33
  216. package/lib/utils/config/updateVersion.js +0 -145
  217. package/lib/utils/config/yamlEditor.d.ts +0 -101
  218. package/lib/utils/config/yamlEditor.js +0 -330
  219. package/lib/utils/core/handler.d.ts +0 -18
  220. package/lib/utils/core/handler.js +0 -46
  221. package/lib/utils/core/logger.d.ts +0 -3
  222. package/lib/utils/core/logger.js +0 -105
  223. package/lib/utils/core/segment.d.ts +0 -279
  224. package/lib/utils/core/segment.js +0 -451
  225. package/lib/utils/index.d.ts +0 -13
  226. package/lib/utils/index.js +0 -13
  227. package/lib/utils/tools/button.d.ts +0 -2
  228. package/lib/utils/tools/button.js +0 -27
  229. package/lib/utils/tools/exec.d.ts +0 -20
  230. package/lib/utils/tools/exec.js +0 -54
  231. package/lib/utils/tools/ffmpeg.d.ts +0 -12
  232. package/lib/utils/tools/ffmpeg.js +0 -25
  233. package/lib/utils/tools/restart.d.ts +0 -15
  234. package/lib/utils/tools/restart.js +0 -39
  235. package/lib/utils/tools/stop.d.ts +0 -7
  236. package/lib/utils/tools/stop.js +0 -13
  237. package/lib/utils/tools/update.d.ts +0 -67
  238. package/lib/utils/tools/update.js +0 -151
  239. /package/{config → default}/view/App.yaml +0 -0
  240. /package/{config → default}/view/config.yaml +0 -0
  241. /package/{config → default}/view/pm2.yaml +0 -0
  242. /package/{config → default}/view/redis.yaml +0 -0
  243. /package/{config → default}/view/server.yaml +0 -0
  244. /package/{lib/modules → dist/module}/art-template.js +0 -0
  245. /package/{lib/modules → dist/module}/axios.js +0 -0
  246. /package/{lib/modules → dist/module}/chalk.js +0 -0
  247. /package/{lib/modules → dist/module}/chokidar.js +0 -0
  248. /package/{lib/modules → dist/module}/level.js +0 -0
  249. /package/{lib/modules → dist/module}/lodash.js +0 -0
  250. /package/{lib/modules → dist/module}/log4js.js +0 -0
  251. /package/{lib/modules → dist/module}/moment.js +0 -0
  252. /package/{lib/modules → dist/module}/node-schedule.js +0 -0
  253. /package/{lib/modules → dist/module}/redis.js +0 -0
  254. /package/{lib/modules → dist/module}/yaml.js +0 -0
package/lib/render/app.js DELETED
@@ -1,120 +0,0 @@
1
- import { logger } from '../utils/index.js';
2
- class Renderers {
3
- index;
4
- Apps;
5
- constructor() {
6
- /** 索引 */
7
- this.index = 0;
8
- /**
9
- * 渲染器列表
10
- * @type {APP[]}
11
- */
12
- this.Apps = [];
13
- }
14
- /**
15
- * 注册渲染器
16
- * @param data 渲染器数据
17
- * @param data.id 渲染器ID
18
- * @param data.type 渲染器类型
19
- * @param ata.render 渲染器标准方法
20
- * @returns 渲染器索引
21
- */
22
- app(data) {
23
- this.index++;
24
- const index = this.index;
25
- const { id, type = 'image', render } = data;
26
- if (!id)
27
- throw new Error('[注册渲染器失败] 缺少渲染器ID');
28
- if (!type)
29
- throw new Error('[注册渲染器失败] 缺少渲染器类型');
30
- if (!render)
31
- throw new Error('[注册渲染器失败] 缺少渲染器标准方法');
32
- const time = Date.now();
33
- const options = { index, id, type, render, time };
34
- this.Apps.push(options);
35
- logger.mark(`${logger.violet(`[渲染器:${index}]`)} 注册成功: ` + logger.green(id));
36
- return index;
37
- }
38
- /**
39
- * 卸载渲染器
40
- * @param index 渲染器索引
41
- * @returns 是否卸载成功
42
- */
43
- unapp(index) {
44
- const app = this.Apps.find(app => app.index === index);
45
- if (!app) {
46
- logger.error(`[卸载渲染器失败] 未找到渲染器索引:${index}`);
47
- return false;
48
- }
49
- this.Apps = this.Apps.filter(app => app.index !== index);
50
- logger.mark(`[卸载渲染器] ${app.id}`);
51
- return true;
52
- }
53
- /**
54
- * 返回渲染器实例 未键入id返回第一个
55
- * @param id 渲染器ID
56
- * @returns 渲染器实例
57
- */
58
- App(id = '') {
59
- if (this.Apps.length === 0)
60
- throw new Error('[调用渲染器失败] 渲染器列表为空');
61
- if (!id)
62
- return this.Apps[0];
63
- /** 筛选出id一致的渲染器 */
64
- const app = this.Apps.find(app => app.id === id);
65
- if (!app)
66
- throw new Error(`[调用渲染器失败] 未找到渲染器:${id}`);
67
- return app;
68
- }
69
- /**
70
- * 调用标准渲染器
71
- */
72
- async render(options, id) {
73
- const res = this.App(id);
74
- if (typeof options.multiPage === 'number' || options.multiPage === true) {
75
- const result = await res.render(options);
76
- return result;
77
- }
78
- else {
79
- const result = await res.render(options);
80
- return result;
81
- }
82
- }
83
- /**
84
- * 快速渲染
85
- * @param data html路径、http地址
86
- * @returns 返回图片base64或数组
87
- */
88
- async renderHtml(data) {
89
- return this.render({
90
- file: data,
91
- name: 'render',
92
- pageGotoParams: {
93
- waitUntil: 'networkidle2',
94
- },
95
- });
96
- }
97
- /**
98
- * 快速分片渲染
99
- * @param data html路径、http地址
100
- * @param multiPage 分片高度 自动计算传true
101
- */
102
- async renderMultiHtml(data, multiPage) {
103
- return await this.render({
104
- file: data,
105
- name: 'render',
106
- multiPage,
107
- pageGotoParams: {
108
- waitUntil: 'networkidle2',
109
- },
110
- });
111
- }
112
- }
113
- /**
114
- * 渲染器管理器
115
- */
116
- export const render = new Renderers();
117
- /**
118
- * @description 即将废弃,请使用 `render`
119
- */
120
- export const Renderer = render;
@@ -1,30 +0,0 @@
1
- import chokidar from 'chokidar';
2
- import { KarinRender, KarinRenderType, RenderResult } from '../types/index.js';
3
- /**
4
- * 渲染器基类 所有渲染器都应该继承这个类
5
- */
6
- export declare class RenderBase implements KarinRender {
7
- dir: './temp/html';
8
- html: {
9
- [key: string]: string;
10
- };
11
- watcher: {
12
- [key: string]: chokidar.FSWatcher;
13
- };
14
- constructor();
15
- /**
16
- * 模板渲染
17
- * @param options 模板名称
18
- * @param isAbs 是否返回绝对路径
19
- */
20
- dealTpl(options: KarinRenderType, isAbs?: boolean): string;
21
- /**
22
- * 监听模板文件
23
- * @param tplFile 模板文件路径
24
- */
25
- watch(tplFile: string): void;
26
- /**
27
- * 渲染标准方法
28
- */
29
- render<T extends KarinRenderType>(options: T): Promise<RenderResult<T>>;
30
- }
@@ -1,69 +0,0 @@
1
- import fs from 'fs';
2
- import chokidar from 'chokidar';
3
- import template from 'art-template';
4
- import { common, logger } from '../utils/index.js';
5
- /**
6
- * 渲染器基类 所有渲染器都应该继承这个类
7
- */
8
- export class RenderBase {
9
- dir;
10
- html;
11
- watcher;
12
- constructor() {
13
- this.dir = './temp/html';
14
- this.html = {};
15
- this.watcher = {};
16
- common.mkdir(this.dir);
17
- }
18
- /**
19
- * 模板渲染
20
- * @param options 模板名称
21
- * @param isAbs 是否返回绝对路径
22
- */
23
- dealTpl(options, isAbs = true) {
24
- let { name, fileID, file: tplFile } = options;
25
- fileID = fileID || name;
26
- const filePath = `./temp/html/${name}/${fileID}.html`;
27
- /** 读取html模板 */
28
- if (!this.html[tplFile]) {
29
- common.mkdir(`./temp/html/${name}`);
30
- try {
31
- this.html[tplFile] = fs.readFileSync(tplFile, 'utf8');
32
- }
33
- catch (error) {
34
- logger.error(`加载html错误:${tplFile}`);
35
- return '';
36
- }
37
- this.watch(tplFile);
38
- }
39
- /** 替换模板 */
40
- const tmpHtml = template.render(this.html[tplFile], options.data);
41
- /** 保存模板 */
42
- fs.writeFileSync(filePath, tmpHtml);
43
- logger.debug(`[图片生成][使用模板] ${filePath}`);
44
- /** 是否返回绝对路径 */
45
- if (isAbs)
46
- return `${process.cwd()}/temp/html/${name}/${fileID}.html`;
47
- return filePath;
48
- }
49
- /**
50
- * 监听模板文件
51
- * @param tplFile 模板文件路径
52
- */
53
- watch(tplFile) {
54
- if (this.watcher[tplFile])
55
- return;
56
- const watcher = chokidar.watch(tplFile);
57
- watcher.on('change', () => {
58
- delete this.html[tplFile];
59
- logger.mark(`[修改html模板] ${tplFile}`);
60
- });
61
- this.watcher[tplFile] = watcher;
62
- }
63
- /**
64
- * 渲染标准方法
65
- */
66
- async render(options) {
67
- throw new Error('未实现渲染方法');
68
- }
69
- }
@@ -1,41 +0,0 @@
1
- import WebSocket from 'ws';
2
- import { RenderBase } from './base.js';
3
- import { KarinRenderType, RenderResult } from '../types/index.js';
4
- export declare class RenderClient extends RenderBase {
5
- url: string;
6
- type: string;
7
- id: string;
8
- index: number;
9
- retry: number;
10
- short: boolean;
11
- reg: RegExp;
12
- ws: WebSocket;
13
- protocol?: {
14
- application: string;
15
- short: boolean;
16
- cache: boolean;
17
- vue: boolean;
18
- };
19
- constructor(url: string);
20
- /**
21
- * 初始化
22
- */
23
- start(): Promise<void>;
24
- /**
25
- * 创建短连接
26
- */
27
- link(): Promise<void>;
28
- /**
29
- * 心跳
30
- */
31
- heartbeat(): Promise<void>;
32
- /**
33
- * 接受消息
34
- */
35
- message(str: string): Promise<void>;
36
- /**
37
- * 渲染标准方法
38
- * @param options 渲染参数
39
- */
40
- render<T extends KarinRenderType>(options: T): Promise<RenderResult<T>>;
41
- }
@@ -1,238 +0,0 @@
1
- import fs from 'fs';
2
- import axios from 'axios';
3
- import WebSocket from 'ws';
4
- import { render } from './app.js';
5
- import { RenderBase } from './base.js';
6
- import { createHash, randomUUID } from 'crypto';
7
- import { karin } from '../core/index.js';
8
- import { common, logger } from '../utils/index.js';
9
- export class RenderClient extends RenderBase {
10
- url;
11
- type;
12
- id;
13
- index;
14
- retry;
15
- short;
16
- reg;
17
- ws;
18
- // NOTE: 渲染器协议暂定方案,目前仅short用于短连接模式确认,其他无用
19
- protocol;
20
- constructor(url) {
21
- super();
22
- this.url = url;
23
- this.type = 'image';
24
- this.id = 'puppeteer';
25
- this.index = 0;
26
- this.retry = 0;
27
- this.short = false;
28
- this.reg = new RegExp(`(${process.cwd().replace(/\\/g, '\\\\')}|${process.cwd().replace(/\\/g, '/')})`, 'g');
29
- }
30
- /**
31
- * 初始化
32
- */
33
- async start() {
34
- /** 连接ws */
35
- this.ws = new WebSocket(this.url);
36
- /** 建立连接 */
37
- this.ws.on('open', () => {
38
- logger.mark(`[渲染器:${this.id}][WebSocket] 建立连接:${logger.green(this.url)}`);
39
- /** 注册渲染器 */
40
- try {
41
- this.index = render.app({ id: this.id, type: this.type, render: this.render.bind(this) });
42
- this.retry = 0;
43
- }
44
- catch (error) {
45
- logger.error(`[渲染器:${this.id}] 注册渲染器失败:`, error);
46
- /** 断开连接 */
47
- this.ws.close();
48
- }
49
- /** 心跳 */
50
- this.heartbeat();
51
- /** 监听消息 */
52
- this.ws.on('message', data => this.message(data.toString()));
53
- });
54
- /** 监听断开 */
55
- this.ws.once('close', async () => {
56
- this.retry++;
57
- /** 停止监听 */
58
- this.ws.removeAllListeners();
59
- /** 卸载渲染器 */
60
- this.index && render.unapp(this.index) && (this.index = 0);
61
- logger.warn(`[渲染器:${this.id}][重连次数:${this.retry}] 连接断开,5秒后将尝试重连:${this.url}`);
62
- await common.sleep(5000);
63
- await this.start();
64
- });
65
- /** 监听错误 */
66
- this.ws.on('error', async (e) => {
67
- logger.debug(e);
68
- await common.sleep(5000);
69
- this.ws.close();
70
- });
71
- }
72
- /**
73
- * 创建短连接
74
- */
75
- async link() {
76
- return new Promise((resolve, reject) => {
77
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
78
- return resolve();
79
- }
80
- logger.debug(`[渲染器:${this.id}][正向WS] 创建短连接`);
81
- /** 连接ws */
82
- this.ws = new WebSocket(this.url);
83
- /** 建立连接 */
84
- this.ws.on('open', () => {
85
- logger.mark(`[渲染器:${this.id}][WebSocket] 建立连接:${logger.green(this.url)}`);
86
- /** 监听消息 */
87
- this.ws.on('message', data => this.message(data.toString()));
88
- resolve();
89
- });
90
- /** 监听断开 */
91
- this.ws.once('close', async () => {
92
- logger.debug(`[渲染器:${this.id}][正向WS] 关闭短连接`);
93
- /** 停止监听 */
94
- this.ws.removeAllListeners();
95
- });
96
- /** 监听错误 */
97
- this.ws.on('error', async (e) => {
98
- logger.debug(e);
99
- this.ws.close();
100
- });
101
- });
102
- }
103
- /**
104
- * 心跳
105
- */
106
- async heartbeat() {
107
- /** 无限循环 错误则停止 */
108
- while (true) {
109
- if (this.short) {
110
- try {
111
- const res = await axios.head(this.url);
112
- if (res.status === 200) {
113
- logger.debug(`[渲染器:${this.id}] 心跳:${this.url}`);
114
- }
115
- else {
116
- logger.debug(`[渲染器:${this.id}] 心跳失败:服务器错误,错误代码 ${res.status}`);
117
- break;
118
- }
119
- }
120
- catch (e) {
121
- logger.debug(`[渲染器:${this.id}] 心跳失败:`, e);
122
- break;
123
- }
124
- // NOTE: 真的需要心跳的这么快吗
125
- await common.sleep(60 * 1000);
126
- }
127
- else {
128
- try {
129
- this.ws.send(JSON.stringify({ action: 'heartbeat' }));
130
- logger.debug(`[渲染器:${this.id}] 心跳:${this.url}`);
131
- }
132
- catch (e) {
133
- logger.debug(`[渲染器:${this.id}] 心跳失败:`, e);
134
- this.ws.close();
135
- break;
136
- }
137
- await common.sleep(5000);
138
- }
139
- }
140
- }
141
- /**
142
- * 接受消息
143
- */
144
- async message(str) {
145
- const data = JSON.parse(str);
146
- switch (data.action) {
147
- /** 静态文件 */
148
- case 'static': {
149
- const filePath = decodeURIComponent(data.params.file);
150
- logger.debug(`[渲染器:${this.id}][正向WS] 访问静态文件:${filePath}`);
151
- const file = fs.readFileSync('.' + filePath);
152
- const md5 = createHash('md5').update(file).digest('hex');
153
- const params = data.params.md5?.includes(md5)
154
- ? { echo: data.echo, action: 'static', status: 'ok', data: { verifiedMd5: md5 } }
155
- : { echo: data.echo, action: 'static', status: 'ok', data: { file } };
156
- return this.ws.send(JSON.stringify(params));
157
- }
158
- /** 渲染结果 */
159
- case 'renderRes': {
160
- karin.emit(data.echo, data);
161
- break;
162
- }
163
- /** 超时 */
164
- case 'timeout': {
165
- logger.debug(`[渲染器:${this.id}][正向WS] 处理超时`);
166
- break;
167
- }
168
- /** 确认协议 */
169
- case 'protocol': {
170
- // 已确认协议,跳过
171
- if (this.protocol)
172
- break;
173
- if (data.data)
174
- this.protocol = data.data;
175
- // 短连接模式
176
- if (data.data?.short) {
177
- logger.debug(`[渲染器:${this.id}][正向WS] 切换到短连接模式`);
178
- this.short = data.data?.short;
179
- this.ws.removeAllListeners();
180
- this.ws.close();
181
- }
182
- break;
183
- }
184
- /** 未知数据 */
185
- default: {
186
- logger.warn(`[渲染器:${this.id}] 收到未知数据:`, data);
187
- }
188
- }
189
- }
190
- /**
191
- * 渲染标准方法
192
- * @param options 渲染参数
193
- */
194
- async render(options) {
195
- /** 渲染模板 */
196
- let file = options.file;
197
- let action = 'renderHtml';
198
- if (options.file.includes('http') || options.vue) {
199
- action = 'render';
200
- }
201
- else {
202
- file = this.dealTpl(options);
203
- /** 判断是本地karin-puppeteer还是远程 */
204
- if (!/127\.0\.0\.1|localhost/.test(this.url)) {
205
- file = fs.readFileSync(file, 'utf-8').replace(this.reg, '');
206
- }
207
- else {
208
- action = 'render';
209
- file = 'file://' + file;
210
- }
211
- }
212
- if (!file) {
213
- logger.error(`[渲染器:${this.id}:${this.index}] 渲染文件不存在:${options.file}`);
214
- return '';
215
- }
216
- /** 编码 */
217
- file = encodeURIComponent(file);
218
- const data = options;
219
- const echo = randomUUID();
220
- /** 移除掉模板参数 */
221
- if (data.data)
222
- delete data.data;
223
- data.file = file;
224
- const req = JSON.stringify({ echo, action, data });
225
- logger.debug(`[渲染器:${this.id}:${this.index}][正向WS] 请求:${this.url} \nhtml: ${options.file} \ndata: ${JSON.stringify(data)}`);
226
- if (this.short) {
227
- await this.link();
228
- }
229
- this.ws.send(req);
230
- return new Promise((resolve, reject) => {
231
- karin.once(echo, (data) => {
232
- if (data.ok)
233
- return resolve(data.data);
234
- reject(new Error(JSON.stringify(data)));
235
- });
236
- });
237
- }
238
- }
@@ -1,19 +0,0 @@
1
- import { RenderBase } from './base.js';
2
- import { KarinRenderType } from '../types/index.js';
3
- export declare class HttpRenderer extends RenderBase {
4
- id: string;
5
- host: string;
6
- url: string;
7
- token: string;
8
- /**
9
- * 构造函数
10
- * @param host - 静态服务器地址
11
- * @param url - 渲染接口
12
- * @param token - token
13
- */
14
- constructor(host: string, url: string, token: string);
15
- /**
16
- * 渲染
17
- */
18
- render<T extends KarinRenderType>(options: T): Promise<any>;
19
- }
@@ -1,50 +0,0 @@
1
- import axios from 'axios';
2
- import { RenderBase } from './base.js';
3
- import logger from '../utils/core/logger.js';
4
- export class HttpRenderer extends RenderBase {
5
- id;
6
- host;
7
- url;
8
- token;
9
- /**
10
- * 构造函数
11
- * @param host - 静态服务器地址
12
- * @param url - 渲染接口
13
- * @param token - token
14
- */
15
- constructor(host, url, token) {
16
- super();
17
- this.id = 'puppeteer';
18
- this.host = host;
19
- this.url = url;
20
- this.token = token;
21
- }
22
- /**
23
- * 渲染
24
- */
25
- async render(options) {
26
- const name = options.name || 'render';
27
- let file = options.file;
28
- /** 非http渲染模板并转为http静态资源 */
29
- if (!options.file.includes('http') && !options.vue) {
30
- const isLocalhost = this.host.includes('127.0.0.1') || this.host.includes('localhost');
31
- file = this.dealTpl(options, isLocalhost);
32
- if (!file) {
33
- logger.error(`[渲染器:${this.id}] 模板文件不存在:${name}`);
34
- return '';
35
- }
36
- options.file = isLocalhost ? 'file://' + file : `${this.host}/api/renderHtml?html=${file}`;
37
- }
38
- delete options.data;
39
- const data = options;
40
- const headers = {
41
- Authorization: this.token,
42
- };
43
- logger.debug(`[渲染器:${this.id}][POST] \n请求:${this.url} \nhtml: ${options.file} \ndata: ${JSON.stringify(data)}`);
44
- const res = await axios({ method: 'post', url: this.url, headers, data });
45
- if (res.status === 200 && res.data.ok) {
46
- return res.data.data;
47
- }
48
- throw new Error(`渲染失败:${JSON.stringify(res.data)}`);
49
- }
50
- }
@@ -1,6 +0,0 @@
1
- export * from './app.js';
2
- export * from './base.js';
3
- export * from './client.js';
4
- export * from './http.js';
5
- export * from './server.js';
6
- export * from './wormhole.js';
@@ -1,6 +0,0 @@
1
- export * from './app.js';
2
- export * from './base.js';
3
- export * from './client.js';
4
- export * from './http.js';
5
- export * from './server.js';
6
- export * from './wormhole.js';
@@ -1,25 +0,0 @@
1
- import WebSocket from 'ws';
2
- import { RenderBase } from './base.js';
3
- import { IncomingMessage } from 'http';
4
- import { KarinRenderType, RenderResult } from '../types/index.js';
5
- declare class Puppeteer extends RenderBase {
6
- socket: WebSocket;
7
- id: any;
8
- type: any;
9
- host: any;
10
- url: string;
11
- index: number;
12
- constructor();
13
- server(socket: WebSocket, request: IncomingMessage): Promise<void>;
14
- /**
15
- * 渲染模板
16
- * @param options 模板参数
17
- */
18
- render<T extends KarinRenderType>(options: T): Promise<RenderResult<T>>;
19
- }
20
- export declare const RenderServer: {
21
- type: string;
22
- path: string;
23
- adapter: typeof Puppeteer;
24
- };
25
- export {};