node-karin 0.11.13 → 0.11.14
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/config/view/config.yaml +5 -5
- package/lib/adapter/index.js +2 -2
- package/lib/cli/index.js +229 -232
- package/lib/cli/init.js +14 -14
- package/lib/cli/karin.js +15 -15
- package/lib/cli/start.js +8 -0
- package/lib/core/index.js +10 -10
- package/lib/core/init/init.js +1 -0
- package/lib/core/process/process.js +1 -1
- package/lib/core/server/server.js +13 -1
- package/lib/db/index.js +2 -2
- package/lib/db/level/level.js +0 -1
- package/lib/db/redis/redis_level.d.ts +2 -0
- package/lib/db/redis/redis_level.js +12 -11
- package/lib/event/index.js +5 -5
- package/lib/modules/art-template.js +1 -1
- package/lib/modules/axios.js +2 -2
- package/lib/modules/chalk.js +2 -2
- package/lib/modules/chokidar.js +2 -2
- package/lib/modules/commander.js +2 -2
- package/lib/modules/express.js +3 -3
- package/lib/modules/level.js +2 -2
- package/lib/modules/lodash.js +1 -1
- package/lib/modules/log4js.js +2 -2
- package/lib/modules/moment.js +1 -1
- package/lib/modules/node-schedule.js +2 -2
- package/lib/modules/redis.js +2 -2
- package/lib/modules/ws.js +3 -3
- package/lib/modules/yaml.js +2 -2
- package/lib/render/app.js +82 -81
- package/lib/render/base.js +54 -54
- package/lib/render/client.js +144 -142
- package/lib/render/client_even.js +140 -137
- package/lib/render/http.js +40 -41
- package/lib/render/index.js +6 -6
- package/lib/render/server.js +93 -88
- package/lib/render/wormhole.js +153 -151
- package/lib/types/index.js +13 -13
- package/lib/types/type/global.d.ts +2 -0
- package/lib/utils/index.js +11 -11
- package/package.json +6 -3
- package/lib/cli/dev.js +0 -3
- /package/lib/cli/{dev.d.ts → start.d.ts} +0 -0
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';
|
package/lib/core/init/init.js
CHANGED
|
@@ -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:
|
|
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('检测到后台进程 正在关闭'));
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
-
import
|
|
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';
|
package/lib/db/level/level.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Level } from 'level';
|
|
2
2
|
export default class RedisLevel {
|
|
3
|
-
|
|
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
|
|
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
|
|
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
|
|
71
|
+
await this.level.del(key);
|
|
71
72
|
this.#expireMap.delete(key);
|
|
72
73
|
return null;
|
|
73
74
|
}
|
|
74
|
-
return await this
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
196
|
+
return await this.level.put(key, value);
|
|
196
197
|
}
|
|
197
198
|
/**
|
|
198
199
|
* exists 判断键是否存在
|
package/lib/event/index.js
CHANGED
|
@@ -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';
|
package/lib/modules/axios.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from 'axios'
|
|
2
|
-
export { default } from 'axios'
|
|
1
|
+
export * from 'axios';
|
|
2
|
+
export { default } from 'axios';
|
package/lib/modules/chalk.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from 'chalk'
|
|
2
|
-
export { default } from 'chalk'
|
|
1
|
+
export * from 'chalk';
|
|
2
|
+
export { default } from 'chalk';
|
package/lib/modules/chokidar.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from 'chokidar'
|
|
2
|
-
export { default } from 'chokidar'
|
|
1
|
+
export * from 'chokidar';
|
|
2
|
+
export { default } from 'chokidar';
|
package/lib/modules/commander.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from 'commander'
|
|
2
|
-
export { default } from 'commander'
|
|
1
|
+
export * from 'commander';
|
|
2
|
+
export { default } from 'commander';
|
package/lib/modules/express.js
CHANGED
|
@@ -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 };
|
package/lib/modules/level.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from 'level'
|
|
2
|
-
export { default } from 'level'
|
|
1
|
+
export * from 'level';
|
|
2
|
+
export { default } from 'level';
|
package/lib/modules/lodash.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default } from 'lodash'
|
|
1
|
+
export { default } from 'lodash';
|
package/lib/modules/log4js.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from 'log4js'
|
|
2
|
-
export { default } from 'log4js'
|
|
1
|
+
export * from 'log4js';
|
|
2
|
+
export { default } from 'log4js';
|
package/lib/modules/moment.js
CHANGED
|
@@ -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';
|
package/lib/modules/redis.js
CHANGED
|
@@ -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';
|
package/lib/modules/yaml.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from 'yaml'
|
|
2
|
-
export { default } from 'yaml'
|
|
1
|
+
export * from 'yaml';
|
|
2
|
+
export { default } from 'yaml';
|
package/lib/render/app.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { logger } from '../utils/index.js'
|
|
1
|
+
import { logger } from '../utils/index.js';
|
|
2
2
|
class Renderers {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
index;
|
|
4
|
+
Apps;
|
|
5
|
+
constructor() {
|
|
6
|
+
/** 索引 */
|
|
7
|
+
this.index = 0;
|
|
8
|
+
/**
|
|
9
9
|
* 渲染器列表
|
|
10
10
|
* @type {APP[]}
|
|
11
11
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/**
|
|
12
|
+
this.Apps = [];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
16
15
|
* 注册渲染器
|
|
17
16
|
* @param data 渲染器数据
|
|
18
17
|
* @param data.id 渲染器ID
|
|
@@ -20,100 +19,102 @@ class Renderers {
|
|
|
20
19
|
* @param ata.render 渲染器标准方法
|
|
21
20
|
* @returns 渲染器索引
|
|
22
21
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
+
/**
|
|
38
39
|
* 卸载渲染器
|
|
39
40
|
* @param index 渲染器索引
|
|
40
41
|
* @returns 是否卸载成功
|
|
41
42
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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;
|
|
47
52
|
}
|
|
48
|
-
|
|
49
|
-
logger.mark(`[卸载渲染器] ${app.id}`)
|
|
50
|
-
return true
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
53
|
+
/**
|
|
54
54
|
* 返回渲染器实例 未键入id返回第一个
|
|
55
55
|
* @param id 渲染器ID
|
|
56
56
|
* @returns 渲染器实例
|
|
57
57
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
+
/**
|
|
68
70
|
* 调用标准渲染器
|
|
69
71
|
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
+
}
|
|
78
82
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
/**
|
|
83
|
+
/**
|
|
82
84
|
* 快速渲染
|
|
83
85
|
* @param data html路径、http地址
|
|
84
86
|
* @returns 返回图片base64或数组
|
|
85
87
|
*/
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
/**
|
|
88
|
+
async renderHtml(data) {
|
|
89
|
+
return this.render({
|
|
90
|
+
file: data,
|
|
91
|
+
name: 'render',
|
|
92
|
+
pageGotoParams: {
|
|
93
|
+
waitUntil: 'networkidle2',
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
97
98
|
* 快速分片渲染
|
|
98
99
|
* @param data html路径、http地址
|
|
99
100
|
* @param multiPage 分片高度 自动计算传true
|
|
100
101
|
*/
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
+
}
|
|
111
112
|
}
|
|
112
113
|
/**
|
|
113
114
|
* 渲染器管理器
|
|
114
115
|
*/
|
|
115
|
-
export const render = new Renderers()
|
|
116
|
+
export const render = new Renderers();
|
|
116
117
|
/**
|
|
117
118
|
* @description 即将废弃,请使用 `render`
|
|
118
119
|
*/
|
|
119
|
-
export const Renderer = render
|
|
120
|
+
export const Renderer = render;
|
package/lib/render/base.js
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import chokidar from 'chokidar'
|
|
3
|
-
import template from 'art-template'
|
|
4
|
-
import { common, logger } from '../utils/index.js'
|
|
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
5
|
/**
|
|
6
6
|
* 渲染器基类 所有渲染器都应该继承这个类
|
|
7
7
|
*/
|
|
8
8
|
export class RenderBase {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
/**
|
|
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
|
+
/**
|
|
20
19
|
* 模板渲染
|
|
21
20
|
* @param options 模板名称
|
|
22
21
|
* @param isAbs 是否返回绝对路径
|
|
23
22
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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;
|
|
38
48
|
}
|
|
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) { return `${process.cwd()}/temp/html/${name}/${fileID}.html` }
|
|
46
|
-
return filePath
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
49
|
+
/**
|
|
50
50
|
* 监听模板文件
|
|
51
51
|
* @param tplFile 模板文件路径
|
|
52
52
|
*/
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
64
|
* 渲染标准方法
|
|
65
65
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
async render(options) {
|
|
67
|
+
throw new Error('未实现渲染方法');
|
|
68
|
+
}
|
|
69
69
|
}
|