node-karin 0.0.3

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 (107) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +57 -0
  3. package/config/defSet/App.yaml +37 -0
  4. package/config/defSet/config.yaml +43 -0
  5. package/config/defSet/group.yaml +18 -0
  6. package/config/defSet/pm2.yaml +21 -0
  7. package/config/defSet/redis.yaml +18 -0
  8. package/config/defSet/server.yaml +42 -0
  9. package/config/view/App.yaml +74 -0
  10. package/config/view/config.yaml +100 -0
  11. package/config/view/group.yaml +62 -0
  12. package/config/view/pm2.yaml +41 -0
  13. package/config/view/redis.yaml +25 -0
  14. package/config/view/server.yaml +93 -0
  15. package/lib/adapter/onebot/onebot11.d.ts +430 -0
  16. package/lib/adapter/onebot/onebot11.js +1302 -0
  17. package/lib/core/init.d.ts +0 -0
  18. package/lib/core/init.js +4 -0
  19. package/lib/core/karin.d.ts +72 -0
  20. package/lib/core/karin.js +51 -0
  21. package/lib/core/listener.d.ts +121 -0
  22. package/lib/core/listener.js +188 -0
  23. package/lib/core/plugin.app.d.ts +15 -0
  24. package/lib/core/plugin.app.js +18 -0
  25. package/lib/core/plugin.d.ts +182 -0
  26. package/lib/core/plugin.js +138 -0
  27. package/lib/core/plugin.loader.d.ts +149 -0
  28. package/lib/core/plugin.loader.js +462 -0
  29. package/lib/core/server.d.ts +26 -0
  30. package/lib/core/server.js +213 -0
  31. package/lib/db/level.d.ts +20 -0
  32. package/lib/db/level.js +38 -0
  33. package/lib/db/redis.d.ts +41 -0
  34. package/lib/db/redis.js +137 -0
  35. package/lib/db/redis_level.d.ts +113 -0
  36. package/lib/db/redis_level.js +290 -0
  37. package/lib/event/event.d.ts +138 -0
  38. package/lib/event/event.handler.d.ts +29 -0
  39. package/lib/event/event.handler.js +142 -0
  40. package/lib/event/event.js +120 -0
  41. package/lib/event/message.d.ts +102 -0
  42. package/lib/event/message.handler.d.ts +25 -0
  43. package/lib/event/message.handler.js +240 -0
  44. package/lib/event/message.js +70 -0
  45. package/lib/event/notice.d.ts +49 -0
  46. package/lib/event/notice.js +15 -0
  47. package/lib/event/request.d.ts +49 -0
  48. package/lib/event/request.js +15 -0
  49. package/lib/event/review.handler.d.ts +54 -0
  50. package/lib/event/review.handler.js +382 -0
  51. package/lib/index.d.ts +23 -0
  52. package/lib/index.js +40 -0
  53. package/lib/renderer/app.d.ts +53 -0
  54. package/lib/renderer/app.js +93 -0
  55. package/lib/renderer/base.d.ts +30 -0
  56. package/lib/renderer/base.js +71 -0
  57. package/lib/renderer/client.d.ts +30 -0
  58. package/lib/renderer/client.js +159 -0
  59. package/lib/renderer/http.d.ts +19 -0
  60. package/lib/renderer/http.js +51 -0
  61. package/lib/renderer/server.d.ts +42 -0
  62. package/lib/renderer/server.js +112 -0
  63. package/lib/renderer/wormhole.d.ts +1 -0
  64. package/lib/renderer/wormhole.js +154 -0
  65. package/lib/types/adapter.d.ts +575 -0
  66. package/lib/types/adapter.js +1 -0
  67. package/lib/types/config.d.ts +327 -0
  68. package/lib/types/config.js +1 -0
  69. package/lib/types/element.d.ts +576 -0
  70. package/lib/types/element.js +1 -0
  71. package/lib/types/index.d.ts +8 -0
  72. package/lib/types/index.js +8 -0
  73. package/lib/types/logger.d.ts +109 -0
  74. package/lib/types/logger.js +1 -0
  75. package/lib/types/onebots11.d.ts +1371 -0
  76. package/lib/types/onebots11.js +1 -0
  77. package/lib/types/plugin.d.ts +282 -0
  78. package/lib/types/plugin.js +1 -0
  79. package/lib/types/render.d.ts +111 -0
  80. package/lib/types/render.js +1 -0
  81. package/lib/types/reply.d.ts +40 -0
  82. package/lib/types/reply.js +1 -0
  83. package/lib/types/types.d.ts +898 -0
  84. package/lib/types/types.js +1 -0
  85. package/lib/utils/YamlEditor.d.ts +62 -0
  86. package/lib/utils/YamlEditor.js +208 -0
  87. package/lib/utils/button.d.ts +49 -0
  88. package/lib/utils/button.js +79 -0
  89. package/lib/utils/common.d.ts +123 -0
  90. package/lib/utils/common.js +413 -0
  91. package/lib/utils/config.d.ts +72 -0
  92. package/lib/utils/config.js +254 -0
  93. package/lib/utils/exec.d.ts +22 -0
  94. package/lib/utils/exec.js +36 -0
  95. package/lib/utils/ffmpeg.d.ts +12 -0
  96. package/lib/utils/ffmpeg.js +25 -0
  97. package/lib/utils/handler.d.ts +76 -0
  98. package/lib/utils/handler.js +102 -0
  99. package/lib/utils/logger.d.ts +3 -0
  100. package/lib/utils/logger.js +104 -0
  101. package/lib/utils/segment.d.ts +276 -0
  102. package/lib/utils/segment.js +448 -0
  103. package/lib/utils/update.d.ts +69 -0
  104. package/lib/utils/update.js +151 -0
  105. package/lib/utils/updateVersion.d.ts +33 -0
  106. package/lib/utils/updateVersion.js +145 -0
  107. package/package.json +92 -0
@@ -0,0 +1,382 @@
1
+ import Config from '../utils/config.js'
2
+ import logger from '../utils/logger.js'
3
+ /**
4
+ * 事件拦截器
5
+ * 利用可执行函数的特性,热更新所有拦截器
6
+ * 所有拦截器返回的都是布尔值 为true说明通过 为false则未通过
7
+ */
8
+ export default new (class Review {
9
+ GroupCD
10
+ GroupUserCD
11
+ App = Config['App']
12
+ Config = Config['Config']
13
+ CD
14
+ mode
15
+ alias
16
+ GroupEnable
17
+ UserEnable
18
+ GroupMsgPrint
19
+ PluginEnable
20
+ constructor () {
21
+ /** 群聊所有消息cd */
22
+ this.GroupCD = {}
23
+ /** 群聊个人cd */
24
+ this.GroupUserCD = {}
25
+ /** 事件cd */
26
+ this.CD = (e, config) => true
27
+ /** 响应模式 */
28
+ this.mode = (e, config) => true
29
+ /** 前缀、别名 */
30
+ this.alias = (e, config) => true
31
+ /** 群聊黑白名单 哪个群可以触发事件 */
32
+ this.GroupEnable = e => true
33
+ /** 用户黑白名单 谁可以触发事件 */
34
+ this.UserEnable = e => true
35
+ /** 群聊事件日志 哪个群可以打印日志 */
36
+ this.GroupMsgPrint = e => true
37
+ /** 插件黑白名单 哪个插件可以被触发 */
38
+ this.PluginEnable = (app, config) => true
39
+ // 延迟1秒执行
40
+ setTimeout(() => {
41
+ this.main()
42
+ }, 1000)
43
+ }
44
+
45
+ main () {
46
+ this.App = Config.App
47
+ this.Config = Config.Config
48
+ this.#CD()
49
+ this.#mode()
50
+ this.#alias()
51
+ this.#GroupEnable()
52
+ this.#UserEnable()
53
+ this.#GroupMsgPrint()
54
+ this.#PluginEnable()
55
+ }
56
+
57
+ /**
58
+ * 群聊黑白名单 允许哪个群触发事件
59
+ */
60
+ #GroupEnable () {
61
+ /** 同时启用 */
62
+ if (this.App.WhiteList.groups && this.App.BlackList.groups) {
63
+ this.GroupEnable = e => {
64
+ /** 白名单不为空 */
65
+ if (Array.isArray(this.Config.WhiteList.groups) && this.Config.WhiteList.groups.length) {
66
+ const group_id = (Number(e.group_id) || String(e.group_id))
67
+ return this.Config.WhiteList.groups.includes(group_id)
68
+ }
69
+ /** 白名单为空 检查黑名单是否为空 */
70
+ if (Array.isArray(this.Config.BlackList.groups) && this.Config.BlackList.groups.length) {
71
+ const group_id = (Number(e.group_id) || String(e.group_id))
72
+ return !this.Config.BlackList.groups.includes(group_id)
73
+ }
74
+ /** 黑白名单都为空 */
75
+ return true
76
+ }
77
+ return true
78
+ }
79
+ /** 白名单启用 */
80
+ if (this.App.WhiteList.groups) {
81
+ this.GroupEnable = e => {
82
+ if (Array.isArray(this.Config.WhiteList.groups) && this.Config.WhiteList.groups.length) {
83
+ const group_id = (Number(e.group_id) || String(e.group_id))
84
+ return this.Config.WhiteList.groups.includes(group_id)
85
+ }
86
+ return true
87
+ }
88
+ return true
89
+ }
90
+ /** 黑名单启用 */
91
+ if (this.App.BlackList.groups) {
92
+ this.GroupEnable = e => {
93
+ if (Array.isArray(this.Config.BlackList.groups) && this.Config.BlackList.groups.length) {
94
+ const group_id = (Number(e.group_id) || String(e.group_id))
95
+ return !this.Config.BlackList.groups.includes(group_id)
96
+ }
97
+ return true
98
+ }
99
+ return true
100
+ }
101
+ /** 都没有启用 */
102
+ this.GroupEnable = () => true
103
+ }
104
+
105
+ /**
106
+ * 用户黑白名单 允许那个用户触发事件
107
+ */
108
+ #UserEnable () {
109
+ /** 同时启用 */
110
+ if (this.App.WhiteList.users && this.App.BlackList.users) {
111
+ this.UserEnable = e => {
112
+ /** 白名单不为空 */
113
+ if (Array.isArray(this.Config.WhiteList.users) && this.Config.WhiteList.users.length) {
114
+ const user_id = (Number(e.user_id) || String(e.user_id))
115
+ return this.Config.WhiteList.users.includes(user_id)
116
+ }
117
+ /** 白名单为空 检查黑名单是否为空 */
118
+ if (Array.isArray(this.Config.BlackList.users) && this.Config.BlackList.users.length) {
119
+ const user_id = (Number(e.user_id) || String(e.user_id))
120
+ return !this.Config.BlackList.users.includes(user_id)
121
+ }
122
+ /** 黑白名单都为空 */
123
+ return true
124
+ }
125
+ return true
126
+ }
127
+ /** 白名单启用 */
128
+ if (this.App.WhiteList.users) {
129
+ this.UserEnable = e => {
130
+ if (Array.isArray(this.Config.WhiteList.users) && this.Config.WhiteList.users.length) {
131
+ const user_id = (Number(e.user_id) || String(e.user_id))
132
+ return this.Config.WhiteList.users.includes(user_id)
133
+ }
134
+ return true
135
+ }
136
+ return true
137
+ }
138
+ /** 黑名单启用 */
139
+ if (this.App.BlackList.users) {
140
+ this.UserEnable = e => {
141
+ if (Array.isArray(this.Config.BlackList.users) && this.Config.BlackList.users.length) {
142
+ const user_id = (Number(e.user_id) || String(e.user_id))
143
+ return !this.Config.BlackList.users.includes(user_id)
144
+ }
145
+ return true
146
+ }
147
+ return true
148
+ }
149
+ /** 都没有启用 */
150
+ this.UserEnable = () => true
151
+ }
152
+
153
+ /**
154
+ * 群聊事件日志 是否打印
155
+ */
156
+ #GroupMsgPrint () {
157
+ /** 同时启用 */
158
+ if (this.App.WhiteList.GroupMsgLog && this.App.BlackList.GroupMsgLog) {
159
+ this.GroupMsgPrint = e => {
160
+ /** 白名单不为空 */
161
+ if (Array.isArray(this.Config.WhiteList.GroupMsgLog) && this.Config.WhiteList.GroupMsgLog.length) {
162
+ const group_id = (Number(e.group_id) || String(e.group_id))
163
+ return this.Config.WhiteList.GroupMsgLog.includes(group_id)
164
+ }
165
+ /** 白名单为空 检查黑名单是否为空 */
166
+ if (Array.isArray(this.Config.BlackList.GroupMsgLog) && this.Config.BlackList.GroupMsgLog.length) {
167
+ const group_id = (Number(e.group_id) || String(e.group_id))
168
+ return !this.Config.BlackList.GroupMsgLog.includes(group_id)
169
+ }
170
+ /** 黑白名单都为空 */
171
+ return true
172
+ }
173
+ return true
174
+ }
175
+ /** 白名单启用 */
176
+ if (this.App.WhiteList.GroupMsgLog) {
177
+ this.GroupMsgPrint = e => {
178
+ if (Array.isArray(this.Config.WhiteList.GroupMsgLog) && this.Config.WhiteList.GroupMsgLog.length) {
179
+ const group_id = (Number(e.group_id) || String(e.group_id))
180
+ return this.Config.WhiteList.GroupMsgLog.includes(group_id)
181
+ }
182
+ return true
183
+ }
184
+ return true
185
+ }
186
+ /** 黑名单启用 */
187
+ if (this.App.BlackList.GroupMsgLog) {
188
+ this.GroupMsgPrint = e => {
189
+ if (Array.isArray(this.Config.BlackList.GroupMsgLog) && this.Config.BlackList.GroupMsgLog.length) {
190
+ const group_id = (Number(e.group_id) || String(e.group_id))
191
+ return !this.Config.BlackList.GroupMsgLog.includes(group_id)
192
+ }
193
+ return true
194
+ }
195
+ return true
196
+ }
197
+ /** 都没有启用 */
198
+ this.GroupMsgPrint = () => true
199
+ }
200
+
201
+ /**
202
+ * 黑白名单插件 哪个插件可以被触发
203
+ */
204
+ #PluginEnable () {
205
+ /** 同时启用 */
206
+ if (this.App.GroupConfig.enable && this.App.GroupConfig.disable) {
207
+ this.PluginEnable = (app, config) => {
208
+ /** 白名单不为空 */
209
+ if (Array.isArray(config.enable) && config.enable.length) {
210
+ /** 插件包是否处于功能白名单 */
211
+ if (config.enable.includes(app.file.dir)) { return true }
212
+ /** 插件是否处于功能白名单 */
213
+ if (config.enable.includes(`${app.file.dir}/${app.file.name}`)) { return true }
214
+ /** 插件名称是否处于功能白名单 */
215
+ if (config.enable.includes(app.name)) { return true }
216
+ logger.debug(logger.green(`[功能白名单] 插件名称 [${app.name}] 不存在功能白名单中`))
217
+ return false
218
+ }
219
+ /** 白名单为空 检查黑名单是否为空 */
220
+ if (Array.isArray(config.disable) && config.disable.length) {
221
+ /** 插件包是否处于功能黑名单 */
222
+ if (config.disable.includes(app.file.dir)) {
223
+ logger.debug(logger.red(`[功能黑名单] 插件包 [${app.file.dir}] 处于功能黑名单`))
224
+ return false
225
+ }
226
+ /** 插件是否处于功能黑名单 */
227
+ if (config.disable.includes(`${app.file.dir}/${app.file.name}`)) {
228
+ logger.debug(logger.red(`[功能黑名单] 插件 [${app.file.dir}/${app.file.name}] 处于功能黑名单`))
229
+ return false
230
+ }
231
+ /** 插件名称是否处于功能黑名单 */
232
+ if (config.disable.includes(app.name)) {
233
+ logger.debug(logger.red(`[功能黑名单] 插件名称 [${app.name}] 处于功能黑名单`))
234
+ return false
235
+ }
236
+ return true
237
+ }
238
+ /** 黑白名单都为空 */
239
+ return true
240
+ }
241
+ return true
242
+ }
243
+ /** 白名单启用 */
244
+ if (this.App.GroupConfig.enable) {
245
+ this.PluginEnable = (app, config) => {
246
+ if (Array.isArray(config.enable) && config.enable.length) {
247
+ if (config.enable.includes(app.file.dir)) { return true }
248
+ if (config.enable.includes(`${app.file.dir}/${app.file.name}`)) { return true }
249
+ if (config.enable.includes(app.name)) { return true }
250
+ logger.debug(logger.green(`[功能白名单] 插件名称 [${app.name}] 不存在功能白名单中`))
251
+ return false
252
+ }
253
+ return true
254
+ }
255
+ return true
256
+ }
257
+ /** 黑名单启用 */
258
+ if (this.App.GroupConfig.disable) {
259
+ this.PluginEnable = (app, config) => {
260
+ if (Array.isArray(config.disable) && config.disable.length) {
261
+ if (config.disable.includes(app.file.dir)) {
262
+ logger.debug(logger.red(`[功能黑名单] 插件包 [${app.file.dir}] 处于功能黑名单`))
263
+ return false
264
+ }
265
+ if (config.disable.includes(`${app.file.dir}/${app.file.name}`)) {
266
+ logger.debug(logger.red(`[功能黑名单] 插件 [${app.file.dir}/${app.file.name}] 处于功能黑名单`))
267
+ return false
268
+ }
269
+ if (config.disable.includes(app.name)) {
270
+ logger.debug(logger.red(`[功能黑名单] 插件名称 [${app.name}] 处于功能黑名单`))
271
+ return false
272
+ }
273
+ return true
274
+ }
275
+ return true
276
+ }
277
+ return true
278
+ }
279
+ /** 都没有启用 */
280
+ this.PluginEnable = () => true
281
+ return true
282
+ }
283
+
284
+ /** 群聊cd */
285
+ #CD () {
286
+ /** 同时启用 */
287
+ if (this.App.GroupConfig.GroupCD && this.App.GroupConfig.GroupUserCD) {
288
+ this.CD = (e, config) => {
289
+ const key = `${e.group_id}.${e.user_id}`
290
+ /** cd中... */
291
+ if (this.GroupCD[e.group_id] || this.GroupUserCD[key]) { return false }
292
+ /** 全局 */
293
+ this.GroupCD[e.group_id] = true
294
+ setTimeout(() => delete this.GroupCD[e.group_id], config.GroupCD * 1000)
295
+ /** 个人 */
296
+ this.GroupUserCD[key] = true
297
+ setTimeout(() => delete this.GroupUserCD[key], config.GroupUserCD * 1000)
298
+ return true
299
+ }
300
+ return true
301
+ }
302
+ /** 启用单个群聊所有消息冷却时间 */
303
+ if (this.App.GroupConfig.GroupCD) {
304
+ this.CD = (e, config) => {
305
+ /** cd中... */
306
+ if (this.GroupCD[e.group_id]) { return false }
307
+ /** 全局 */
308
+ this.GroupCD[e.group_id] = true
309
+ setTimeout(() => delete this.GroupCD[e.group_id], config.GroupCD * 1000)
310
+ return true
311
+ }
312
+ return true
313
+ }
314
+ /** 启用单个群聊个人消息冷却时间 */
315
+ if (this.App.GroupConfig.GroupUserCD) {
316
+ this.CD = (e, config) => {
317
+ const key = `${e.group_id}.${e.user_id}`
318
+ /** cd中... */
319
+ if (this.GroupUserCD[key]) { return false }
320
+ /** 个人 */
321
+ this.GroupUserCD[key] = true
322
+ setTimeout(() => delete this.GroupUserCD[key], config.GroupUserCD * 1000)
323
+ return true
324
+ }
325
+ return true
326
+ }
327
+ /** 都没有启用 */
328
+ this.CD = () => true
329
+ }
330
+
331
+ /**
332
+ * 响应模式
333
+ */
334
+ #mode () {
335
+ /** 启用 */
336
+ if (this.App.GroupConfig.mode) {
337
+ this.mode = (e, config) => {
338
+ const modeMap = {
339
+ 0: () => true,
340
+ 1: () => e.atBot,
341
+ 2: () => e.isMaster,
342
+ 3: () => !!e.alias,
343
+ 4: () => {
344
+ if (e.atBot) { return true }
345
+ return !!e.alias
346
+ },
347
+ 5: () => {
348
+ if (e.isMaster) { return true }
349
+ if (e.atBot) { return true }
350
+ return !!e.alias
351
+ },
352
+ }
353
+ return modeMap[Number(config.mode) || 0]()
354
+ }
355
+ return true
356
+ }
357
+ /** 未启用 */
358
+ this.mode = () => true
359
+ }
360
+
361
+ /**
362
+ * 前缀、别名
363
+ */
364
+ #alias () {
365
+ /** 启用 */
366
+ if (this.App.GroupConfig.alias) {
367
+ this.alias = (e, config) => {
368
+ const aliasRegex = new RegExp(`^(${config.alias.join('|')})`)
369
+ const match = e.msg.match(aliasRegex)
370
+ if (match) {
371
+ e.msg = e.msg.replace(aliasRegex, '').trim()
372
+ e.alias = match[1]
373
+ return true
374
+ }
375
+ return false
376
+ }
377
+ return true
378
+ }
379
+ /** 未启用 */
380
+ this.alias = () => true
381
+ }
382
+ })()
package/lib/index.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import './core/init.js';
2
+ import config from './utils/config.js';
3
+ import logger from './utils/logger.js';
4
+ import listener from './core/listener.js';
5
+ import server from './core/server.js';
6
+ import Plugin from './core/plugin.js';
7
+ import PluginLoader from './core/plugin.loader.js';
8
+ import common from './utils/common.js';
9
+ import segment from './utils/segment.js';
10
+ import handler from './utils/handler.js';
11
+ import ffmpeg from './utils/ffmpeg.js';
12
+ import exec from './utils/exec.js';
13
+ import button from './utils/button.js';
14
+ import update from './utils/update.js';
15
+ import YamlEditor from './utils/YamlEditor.js';
16
+ import render from './renderer/app.js';
17
+ import { KarinMessage } from './event/message.js';
18
+ import level from './db/level.js';
19
+ import { RedisClientType } from 'redis';
20
+ import { kritor } from 'kritor-proto';
21
+ export * as types from './types/index.js';
22
+ declare const redis: RedisClientType;
23
+ export { config, config as Cfg, common, listener, logger, Plugin, PluginLoader, server, Plugin as plugin, listener as Bot, segment, handler, ffmpeg, exec, button, level, redis, update, render, kritor, YamlEditor, render as Renderer, update as Update, KarinMessage, };
package/lib/index.js ADDED
@@ -0,0 +1,40 @@
1
+ // 基本模块
2
+ import './core/init.js'
3
+ import config from './utils/config.js'
4
+ import logger from './utils/logger.js'
5
+ // 核心模块
6
+ import listener from './core/listener.js'
7
+ import server from './core/server.js'
8
+ import Plugin from './core/plugin.js'
9
+ import PluginLoader from './core/plugin.loader.js'
10
+ // 工具类
11
+ import common from './utils/common.js'
12
+ import segment from './utils/segment.js'
13
+ import handler from './utils/handler.js'
14
+ import ffmpeg from './utils/ffmpeg.js'
15
+ import exec from './utils/exec.js'
16
+ import button from './utils/button.js'
17
+ import update from './utils/update.js'
18
+ import YamlEditor from './utils/YamlEditor.js'
19
+ import render from './renderer/app.js'
20
+ import { KarinMessage } from './event/message.js'
21
+ // 适配器
22
+ import Puppeteer from './renderer/server.js'
23
+ import OneBot11 from './adapter/onebot/onebot11.js'
24
+ // 数据库
25
+ import level from './db/level.js'
26
+ import Redis from './db/redis.js'
27
+ // proto
28
+ import { kritor } from 'kritor-proto'
29
+ export * as types from './types/index.js'
30
+ // 初始化
31
+ server.init()
32
+ const redis = (await new Redis().start())
33
+ PluginLoader.load()
34
+ listener.emit('adapter', Puppeteer)
35
+ listener.emit('adapter', OneBot11)
36
+ // Exporting Modules
37
+ export {
38
+ // 基本模块
39
+ config, config as Cfg, common, listener, logger, Plugin, PluginLoader, server, Plugin as plugin, listener as Bot, segment, handler, ffmpeg, exec, button, level, redis, update, render, kritor, YamlEditor, render as Renderer, update as Update, KarinMessage,
40
+ }
@@ -0,0 +1,53 @@
1
+ import { KarinRender, KarinRenderApp, KarinRenderType } from '../types/render.js';
2
+ /**
3
+ * 渲染器管理器
4
+ */
5
+ declare const _default: {
6
+ index: number;
7
+ Apps: Array<KarinRenderApp>;
8
+ /**
9
+ * 注册渲染器
10
+ * @param {object} data 渲染器数据
11
+ * @param {string} data.id 渲染器ID
12
+ * @param {'image'|string} data.type 渲染器类型
13
+ * @param {RenderBase.render} data.render 渲染器标准方法
14
+ * @returns {number} 渲染器索引
15
+ */
16
+ app(data: {
17
+ /**
18
+ * 渲染器ID
19
+ */
20
+ id: string;
21
+ /**
22
+ * 渲染器类型
23
+ */
24
+ type?: 'image' | string;
25
+ /**
26
+ * 渲染器标准方法
27
+ */
28
+ render: KarinRender['render'];
29
+ }): number;
30
+ /**
31
+ * 卸载渲染器
32
+ * @param index 渲染器索引
33
+ * @returns 是否卸载成功
34
+ */
35
+ unapp(index: number): boolean;
36
+ /**
37
+ * 返回渲染器实例 未键入id返回第一个
38
+ * @param id 渲染器ID
39
+ * @returns 渲染器实例
40
+ */
41
+ App(id?: string): KarinRenderApp;
42
+ /**
43
+ * 调用标准渲染器
44
+ */
45
+ render(options: KarinRenderType, id: string): Promise<string | string[]>;
46
+ /**
47
+ * 快速渲染
48
+ * @param data html路径、http地址
49
+ * @returns 返回图片base64或数组
50
+ */
51
+ renderHtml(data: string): Promise<string | string[]>;
52
+ };
53
+ export default _default;
@@ -0,0 +1,93 @@
1
+ import logger from '../utils/logger.js'
2
+ /**
3
+ * 渲染器管理器
4
+ */
5
+ export default new (class Renderer {
6
+ index
7
+ Apps
8
+ constructor () {
9
+ /** 索引 */
10
+ this.index = 0
11
+ /**
12
+ * 渲染器列表
13
+ * @type {APP[]}
14
+ */
15
+ this.Apps = []
16
+ }
17
+
18
+ /**
19
+ * 注册渲染器
20
+ * @param {object} data 渲染器数据
21
+ * @param {string} data.id 渲染器ID
22
+ * @param {'image'|string} data.type 渲染器类型
23
+ * @param {RenderBase.render} data.render 渲染器标准方法
24
+ * @returns {number} 渲染器索引
25
+ */
26
+ app (data) {
27
+ this.index++
28
+ const index = this.index
29
+ const { id, type = 'image', render } = data
30
+ if (!id) { throw new Error('[注册渲染器失败] 缺少渲染器ID') }
31
+ if (!type) { throw new Error('[注册渲染器失败] 缺少渲染器类型') }
32
+ if (!render) { throw new Error('[注册渲染器失败] 缺少渲染器标准方法') }
33
+ const time = Date.now()
34
+ const options = { index, id, type, render, time }
35
+ this.Apps.push(options)
36
+ logger.mark(`${logger.violet(`[渲染器:${index}]`)} 注册成功: ` + logger.green(id))
37
+ return index
38
+ }
39
+
40
+ /**
41
+ * 卸载渲染器
42
+ * @param index 渲染器索引
43
+ * @returns 是否卸载成功
44
+ */
45
+ unapp (index) {
46
+ const app = this.Apps.find(app => app.index === index)
47
+ if (!app) {
48
+ logger.error(`[卸载渲染器失败] 未找到渲染器索引:${index}`)
49
+ return false
50
+ }
51
+ this.Apps = this.Apps.filter(app => app.index !== index)
52
+ logger.mark(`[卸载渲染器] ${app.id}`)
53
+ return true
54
+ }
55
+
56
+ /**
57
+ * 返回渲染器实例 未键入id返回第一个
58
+ * @param id 渲染器ID
59
+ * @returns 渲染器实例
60
+ */
61
+ App (id = '') {
62
+ if (this.Apps.length === 0) { throw new Error('[调用渲染器失败] 渲染器列表为空') }
63
+ if (!id) { return this.Apps[0] }
64
+ /** 筛选出id一致的渲染器 */
65
+ const app = this.Apps.find(app => app.id === id)
66
+ if (!app) { throw new Error(`[调用渲染器失败] 未找到渲染器:${id}`) }
67
+ return app
68
+ }
69
+
70
+ /**
71
+ * 调用标准渲染器
72
+ */
73
+ async render (options, id) {
74
+ const res = this.App(id)
75
+ return res.render(options)
76
+ }
77
+
78
+ /**
79
+ * 快速渲染
80
+ * @param data html路径、http地址
81
+ * @returns 返回图片base64或数组
82
+ */
83
+ async renderHtml (data) {
84
+ const app = this.App()
85
+ return app.render({
86
+ file: data,
87
+ name: 'render',
88
+ pageGotoParams: {
89
+ waitUntil: 'networkidle2',
90
+ },
91
+ })
92
+ }
93
+ })()
@@ -0,0 +1,30 @@
1
+ import chokidar from 'chokidar';
2
+ import { KarinRender, KarinRenderType } from '../types/render.js';
3
+ /**
4
+ * 渲染器基类 所有渲染器都应该继承这个类
5
+ */
6
+ export default 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(options: KarinRenderType): Promise<string | Array<string>>;
30
+ }
@@ -0,0 +1,71 @@
1
+ import fs from 'fs'
2
+ import chokidar from 'chokidar'
3
+ import template from 'art-template'
4
+ import common from '../utils/common.js'
5
+ import logger from '../utils/logger.js'
6
+ /**
7
+ * 渲染器基类 所有渲染器都应该继承这个类
8
+ */
9
+ export default class RenderBase {
10
+ dir
11
+ html
12
+ watcher
13
+ constructor () {
14
+ this.dir = './temp/html'
15
+ this.html = {}
16
+ this.watcher = {}
17
+ common.mkdir(this.dir)
18
+ }
19
+
20
+ /**
21
+ * 模板渲染
22
+ * @param options 模板名称
23
+ * @param isAbs 是否返回绝对路径
24
+ */
25
+ dealTpl (options, isAbs = true) {
26
+ let { name, fileID, file: tplFile } = options
27
+ fileID = fileID || name
28
+ const filePath = `./temp/html/${name}/${fileID}.html`
29
+ /** 读取html模板 */
30
+ if (!this.html[tplFile]) {
31
+ common.mkdir(`./temp/html/${name}`)
32
+ try {
33
+ this.html[tplFile] = fs.readFileSync(tplFile, 'utf8')
34
+ } catch (error) {
35
+ logger.error(`加载html错误:${tplFile}`)
36
+ return ''
37
+ }
38
+ this.watch(tplFile)
39
+ }
40
+ /** 替换模板 */
41
+ const tmpHtml = template.render(this.html[tplFile], options.data)
42
+ /** 保存模板 */
43
+ fs.writeFileSync(filePath, tmpHtml)
44
+ logger.debug(`[图片生成][使用模板] ${filePath}`)
45
+ /** 是否返回绝对路径 */
46
+ if (isAbs) { return `${process.cwd()}/temp/html/${name}/${fileID}.html` }
47
+ return filePath
48
+ }
49
+
50
+ /**
51
+ * 监听模板文件
52
+ * @param tplFile 模板文件路径
53
+ */
54
+ watch (tplFile) {
55
+ if (this.watcher[tplFile]) { 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
+ */
67
+ async render (options) {
68
+ logger.error('未实现渲染方法')
69
+ return ''
70
+ }
71
+ }