node-karin 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -674
- package/README.md +57 -57
- package/config/defSet/App.yaml +37 -37
- package/config/defSet/config.yaml +43 -43
- package/config/defSet/group.yaml +18 -18
- package/config/defSet/pm2.yaml +21 -21
- package/config/defSet/server.yaml +42 -42
- package/config/view/App.yaml +74 -74
- package/config/view/config.yaml +100 -100
- package/config/view/group.yaml +62 -62
- package/config/view/pm2.yaml +41 -41
- package/config/view/redis.yaml +25 -25
- package/config/view/server.yaml +93 -93
- package/lib/adapter/onebot/onebot11.d.ts +430 -430
- package/lib/adapter/onebot/onebot11.js +1265 -1302
- package/lib/core/init.js +4 -4
- package/lib/core/karin.d.ts +72 -72
- package/lib/core/karin.js +51 -51
- package/lib/core/listener.d.ts +121 -121
- package/lib/core/listener.js +178 -178
- package/lib/core/plugin.app.d.ts +15 -15
- package/lib/core/plugin.app.js +18 -18
- package/lib/core/plugin.d.ts +182 -182
- package/lib/core/plugin.js +132 -132
- package/lib/core/plugin.loader.d.ts +149 -149
- package/lib/core/plugin.loader.js +451 -451
- package/lib/core/server.d.ts +26 -26
- package/lib/core/server.js +209 -209
- package/lib/db/level.d.ts +20 -20
- package/lib/db/level.js +36 -36
- package/lib/db/redis.d.ts +41 -41
- package/lib/db/redis.js +131 -131
- package/lib/db/redis_level.d.ts +113 -113
- package/lib/db/redis_level.js +274 -274
- package/lib/event/event.d.ts +138 -138
- package/lib/event/event.handler.d.ts +29 -29
- package/lib/event/event.handler.js +138 -138
- package/lib/event/event.js +120 -120
- package/lib/event/message.d.ts +102 -102
- package/lib/event/message.handler.d.ts +25 -25
- package/lib/event/message.handler.js +237 -237
- package/lib/event/message.js +69 -69
- package/lib/event/notice.d.ts +49 -49
- package/lib/event/notice.js +14 -14
- package/lib/event/request.d.ts +49 -49
- package/lib/event/request.js +14 -14
- package/lib/event/review.handler.d.ts +54 -54
- package/lib/event/review.handler.js +374 -374
- package/lib/index.d.ts +23 -23
- package/lib/index.js +39 -40
- package/lib/renderer/app.d.ts +53 -53
- package/lib/renderer/app.js +88 -88
- package/lib/renderer/base.d.ts +30 -30
- package/lib/renderer/base.js +68 -68
- package/lib/renderer/client.d.ts +30 -30
- package/lib/renderer/client.js +155 -155
- package/lib/renderer/http.d.ts +19 -19
- package/lib/renderer/http.js +50 -50
- package/lib/renderer/server.d.ts +42 -42
- package/lib/renderer/server.js +110 -110
- package/lib/renderer/wormhole.d.ts +1 -1
- package/lib/renderer/wormhole.js +154 -154
- package/lib/types/adapter.d.ts +575 -575
- package/lib/types/adapter.js +1 -1
- package/lib/types/config.d.ts +327 -327
- package/lib/types/config.js +1 -1
- package/lib/types/element.d.ts +576 -576
- package/lib/types/element.js +1 -1
- package/lib/types/index.d.ts +8 -8
- package/lib/types/index.js +8 -8
- package/lib/types/logger.d.ts +109 -109
- package/lib/types/logger.js +1 -1
- package/lib/types/onebots11.d.ts +1371 -1371
- package/lib/types/onebots11.js +1 -1
- package/lib/types/plugin.d.ts +282 -282
- package/lib/types/plugin.js +1 -1
- package/lib/types/render.d.ts +111 -111
- package/lib/types/render.js +1 -1
- package/lib/types/reply.d.ts +40 -40
- package/lib/types/reply.js +1 -1
- package/lib/types/types.d.ts +898 -898
- package/lib/types/types.js +1 -1
- package/lib/utils/YamlEditor.d.ts +62 -62
- package/lib/utils/YamlEditor.js +197 -197
- package/lib/utils/button.d.ts +49 -49
- package/lib/utils/button.js +75 -75
- package/lib/utils/common.d.ts +123 -123
- package/lib/utils/common.js +396 -396
- package/lib/utils/config.d.ts +72 -72
- package/lib/utils/config.js +254 -254
- package/lib/utils/exec.d.ts +22 -22
- package/lib/utils/exec.js +36 -36
- package/lib/utils/ffmpeg.d.ts +12 -12
- package/lib/utils/ffmpeg.js +25 -25
- package/lib/utils/handler.d.ts +76 -76
- package/lib/utils/handler.js +98 -98
- package/lib/utils/logger.d.ts +3 -3
- package/lib/utils/logger.js +104 -104
- package/lib/utils/segment.d.ts +276 -276
- package/lib/utils/segment.js +420 -420
- package/lib/utils/update.d.ts +69 -69
- package/lib/utils/update.js +145 -145
- package/lib/utils/updateVersion.d.ts +33 -33
- package/lib/utils/updateVersion.js +140 -140
- package/package.json +92 -91
package/lib/index.d.ts
CHANGED
|
@@ -1,23 +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, };
|
|
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
CHANGED
|
@@ -1,40 +1,39 @@
|
|
|
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
|
-
|
|
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, };
|
package/lib/renderer/app.d.ts
CHANGED
|
@@ -1,53 +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;
|
|
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;
|
package/lib/renderer/app.js
CHANGED
|
@@ -1,93 +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 = []
|
|
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
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
|
|
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
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
|
|
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
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
|
|
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
68
|
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 调用标准渲染器
|
|
72
|
-
*/
|
|
73
|
-
async render (options, id) {
|
|
74
|
-
const res = this.App(id)
|
|
75
|
-
return res.render(options)
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 调用标准渲染器
|
|
72
|
+
*/
|
|
73
|
+
async render (options, id) {
|
|
74
|
+
const res = this.App(id)
|
|
75
|
+
return res.render(options)
|
|
76
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
|
-
})()
|
|
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
|
+
})()
|
package/lib/renderer/base.d.ts
CHANGED
|
@@ -1,30 +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
|
-
}
|
|
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
|
+
}
|
package/lib/renderer/base.js
CHANGED
|
@@ -1,71 +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)
|
|
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
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
|
|
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
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
|
|
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
62
|
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 渲染标准方法
|
|
66
|
-
*/
|
|
67
|
-
async render (options) {
|
|
68
|
-
logger.error('未实现渲染方法')
|
|
69
|
-
return ''
|
|
70
|
-
}
|
|
71
|
-
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 渲染标准方法
|
|
66
|
+
*/
|
|
67
|
+
async render (options) {
|
|
68
|
+
logger.error('未实现渲染方法')
|
|
69
|
+
return ''
|
|
70
|
+
}
|
|
71
|
+
}
|