@jayfong/x-server 1.10.3 → 1.11.2
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/CHANGELOG.md +21 -0
- package/lib/_cjs/cli/api_generator.js +295 -0
- package/lib/_cjs/cli/build_util.js +146 -0
- package/lib/_cjs/cli/cli.js +190 -0
- package/lib/_cjs/cli/deploy_util.js +61 -0
- package/lib/_cjs/cli/env_util.js +145 -0
- package/lib/_cjs/cli/register.js +8 -0
- package/lib/_cjs/cli/template_util.js +77 -0
- package/lib/_cjs/cli/templates/handlers.ts +3 -0
- package/lib/_cjs/cli/templates/hooks.ts +2 -0
- package/lib/_cjs/cli/templates/models.ts +22 -0
- package/lib/_cjs/cli/templates/package.json +3 -0
- package/lib/_cjs/cli/templates/routes.ts +26 -0
- package/lib/_cjs/cli/templates/tasks.ts +2 -0
- package/lib/_cjs/core/define_bus.js +28 -0
- package/lib/_cjs/core/define_handler.js +43 -0
- package/lib/_cjs/core/define_hook.js +10 -0
- package/lib/_cjs/core/define_server.js +12 -0
- package/lib/_cjs/core/define_task.js +30 -0
- package/lib/_cjs/core/handler.js +90 -0
- package/lib/_cjs/core/http_error.js +9 -0
- package/lib/_cjs/core/http_method.js +12 -0
- package/lib/_cjs/core/server.js +145 -0
- package/lib/_cjs/core/types.js +18 -0
- package/lib/_cjs/index.js +179 -0
- package/lib/_cjs/plugins/base.js +3 -0
- package/lib/_cjs/plugins/cors.js +44 -0
- package/lib/_cjs/plugins/file_parser.js +24 -0
- package/lib/_cjs/plugins/ws_parser.js +24 -0
- package/lib/_cjs/plugins/xml_parser.js +61 -0
- package/lib/_cjs/services/base.js +3 -0
- package/lib/_cjs/services/cache.js +231 -0
- package/lib/_cjs/services/captcha.js +45 -0
- package/lib/_cjs/services/dispose.js +33 -0
- package/lib/_cjs/services/jwt.js +59 -0
- package/lib/_cjs/services/redis.js +18 -0
- package/lib/_cjs/x.js +20 -0
- package/lib/cli/api_generator.js +269 -332
- package/lib/cli/build_util.d.ts +1 -0
- package/lib/cli/build_util.js +108 -130
- package/lib/cli/cli.js +161 -181
- package/lib/cli/deploy_util.js +37 -41
- package/lib/cli/env_util.js +112 -120
- package/lib/cli/register.js +5 -7
- package/lib/cli/template_util.js +47 -52
- package/lib/core/define_bus.js +22 -14
- package/lib/core/define_handler.js +31 -36
- package/lib/core/define_hook.js +4 -8
- package/lib/core/define_server.js +6 -10
- package/lib/core/define_task.js +20 -25
- package/lib/core/handler.js +78 -74
- package/lib/core/http_error.js +2 -8
- package/lib/core/http_method.js +7 -10
- package/lib/core/server.js +125 -139
- package/lib/core/types.js +11 -2
- package/lib/index.js +23 -39
- package/lib/plugins/base.js +1 -2
- package/lib/plugins/cors.js +30 -36
- package/lib/plugins/file_parser.d.ts +1 -1
- package/lib/plugins/file_parser.js +12 -16
- package/lib/plugins/ws_parser.d.ts +1 -1
- package/lib/plugins/ws_parser.js +12 -16
- package/lib/plugins/xml_parser.d.ts +1 -1
- package/lib/plugins/xml_parser.js +47 -43
- package/lib/services/base.js +1 -2
- package/lib/services/cache.js +213 -190
- package/lib/services/captcha.js +32 -33
- package/lib/services/dispose.d.ts +1 -1
- package/lib/services/dispose.js +22 -23
- package/lib/services/jwt.js +45 -48
- package/lib/services/redis.js +8 -14
- package/lib/x.js +12 -15
- package/package.json +6 -4
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.FileParserPlugin = void 0;
|
|
7
|
-
const fastify_multipart_1 = __importDefault(require("fastify-multipart"));
|
|
1
|
+
import FastifyMultipart from 'fastify-multipart';
|
|
2
|
+
|
|
8
3
|
/**
|
|
9
4
|
* file 解析器插件
|
|
10
5
|
*/
|
|
11
|
-
class FileParserPlugin {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
6
|
+
export class FileParserPlugin {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
register(fastify) {
|
|
12
|
+
fastify.register(FastifyMultipart, this.options);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
}
|
|
@@ -8,6 +8,6 @@ export interface WsParserPluginOptions extends WebsocketPluginOptions {
|
|
|
8
8
|
*/
|
|
9
9
|
export declare class WsParserPlugin implements BasePlugin {
|
|
10
10
|
private options?;
|
|
11
|
-
constructor(options?: WsParserPluginOptions
|
|
11
|
+
constructor(options?: WsParserPluginOptions);
|
|
12
12
|
register(fastify: FastifyInstance): void;
|
|
13
13
|
}
|
package/lib/plugins/ws_parser.js
CHANGED
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.WsParserPlugin = void 0;
|
|
7
|
-
const fastify_websocket_1 = __importDefault(require("fastify-websocket"));
|
|
1
|
+
import FastifyWebsocket from 'fastify-websocket';
|
|
2
|
+
|
|
8
3
|
/**
|
|
9
4
|
* websocket 解析器插件
|
|
10
5
|
*/
|
|
11
|
-
class WsParserPlugin {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
6
|
+
export class WsParserPlugin {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
register(fastify) {
|
|
12
|
+
fastify.register(FastifyWebsocket, this.options);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
}
|
|
@@ -14,6 +14,6 @@ export interface XmlParserPluginOptions extends Partial<fxp.X2jOptions> {
|
|
|
14
14
|
*/
|
|
15
15
|
export declare class XmlParserPlugin implements BasePlugin {
|
|
16
16
|
private options?;
|
|
17
|
-
constructor(options?: XmlParserPluginOptions
|
|
17
|
+
constructor(options?: XmlParserPluginOptions);
|
|
18
18
|
register(fastify: FastifyInstance): void;
|
|
19
19
|
}
|
|
@@ -1,48 +1,52 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.XmlParserPlugin = void 0;
|
|
7
|
-
const fast_xml_parser_1 = __importDefault(require("fast-xml-parser"));
|
|
1
|
+
import fxp from 'fast-xml-parser';
|
|
2
|
+
|
|
8
3
|
/**
|
|
9
4
|
* XML 解析器插件
|
|
10
5
|
*
|
|
11
6
|
* 底层解析器是 [fast-xml-parser](https://github.com/NaturalIntelligence/fast-xml-parser)
|
|
12
7
|
*/
|
|
13
|
-
class XmlParserPlugin {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
8
|
+
export class XmlParserPlugin {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
register(fastify) {
|
|
14
|
+
const {
|
|
15
|
+
contentType = ['text/xml', 'application/xml'],
|
|
16
|
+
...parseOptions
|
|
17
|
+
} = this.options || {};
|
|
18
|
+
fastify.addContentTypeParser(contentType, (req, payload, done) => {
|
|
19
|
+
let body = '';
|
|
20
|
+
|
|
21
|
+
const handleError = err => done(err);
|
|
22
|
+
|
|
23
|
+
const handleData = data => body += data;
|
|
24
|
+
|
|
25
|
+
const handleEnd = () => {
|
|
26
|
+
let data;
|
|
27
|
+
let err;
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
data = fxp.parse(body, parseOptions);
|
|
31
|
+
} catch (_err) {
|
|
32
|
+
err = _err;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
payload.removeListener('error', handleError);
|
|
36
|
+
payload.removeListener('data', handleData);
|
|
37
|
+
payload.removeListener('end', handleEnd);
|
|
38
|
+
|
|
39
|
+
if (err) {
|
|
40
|
+
done(err);
|
|
41
|
+
} else {
|
|
42
|
+
done(null, data);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
payload.on('error', handleError);
|
|
47
|
+
payload.on('data', handleData);
|
|
48
|
+
payload.on('end', handleEnd);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|
package/lib/services/base.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/lib/services/cache.js
CHANGED
|
@@ -1,196 +1,219 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import cuid from 'cuid';
|
|
2
|
+
import { ms } from 'vtils/date';
|
|
3
|
+
import { x } from "../x";
|
|
4
|
+
export class CacheService {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.options = options;
|
|
7
|
+
this.serviceName = 'cache';
|
|
8
|
+
this.prefix = void 0;
|
|
9
|
+
this.prefix = `${x.appName}:`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
toRedisKey(key) {
|
|
13
|
+
return `${this.prefix}${Array.isArray(key) ? key.join('_') : String(key)}`;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 设置缓存内容。对象类内容尽量避免使用,以免造成问题。
|
|
17
|
+
*
|
|
18
|
+
* @param key 键
|
|
19
|
+
* @param value 值
|
|
20
|
+
* @param ttl 缓存时间(单位:时)
|
|
21
|
+
* @returns 返回设置的缓存内容
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
async set(key, value, ttl = this.options.ttl) {
|
|
26
|
+
const redisKey = this.toRedisKey(key);
|
|
27
|
+
const redisValue = JSON.stringify(value);
|
|
28
|
+
const redisTtl = typeof ttl === 'function' ? ttl(value, this.options.ttl) : ttl;
|
|
29
|
+
await x.redis.set(redisKey, redisValue, // 毫秒
|
|
30
|
+
'PX', ms(redisTtl));
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 存储值然后返回其对应的键,该键 URL 友好。
|
|
35
|
+
*
|
|
36
|
+
* @param value 值
|
|
37
|
+
* @param ttl 存活时间
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async save(value, ttl = this.options.ttl) {
|
|
42
|
+
const key = `X__${cuid()}`;
|
|
43
|
+
await this.set(key, typeof value === 'function' ? await value(key) : value, ttl);
|
|
44
|
+
return key;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 获取缓存内容。
|
|
48
|
+
*
|
|
49
|
+
* @param key 键
|
|
50
|
+
* @returns 返回获取到的缓存内容
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
async get(key) {
|
|
55
|
+
const redisKey = this.toRedisKey(key);
|
|
56
|
+
const gotValue = await x.redis.get(redisKey);
|
|
57
|
+
|
|
58
|
+
if (gotValue) {
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(gotValue);
|
|
61
|
+
} catch {}
|
|
15
62
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return value;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 存储值然后返回其对应的键,该键 URL 友好。
|
|
39
|
-
*
|
|
40
|
-
* @param value 值
|
|
41
|
-
* @param ttl 存活时间
|
|
42
|
-
*/
|
|
43
|
-
async save(value, ttl = this.options.ttl) {
|
|
44
|
-
const key = `X__${(0, cuid_1.default)()}`;
|
|
45
|
-
await this.set(key, typeof value === 'function' ? await value(key) : value, ttl);
|
|
46
|
-
return key;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* 获取缓存内容。
|
|
50
|
-
*
|
|
51
|
-
* @param key 键
|
|
52
|
-
* @returns 返回获取到的缓存内容
|
|
53
|
-
*/
|
|
54
|
-
async get(key) {
|
|
55
|
-
const redisKey = this.toRedisKey(key);
|
|
56
|
-
const gotValue = await x_1.x.redis.get(redisKey);
|
|
57
|
-
if (gotValue) {
|
|
58
|
-
try {
|
|
59
|
-
return JSON.parse(gotValue);
|
|
60
|
-
}
|
|
61
|
-
catch { }
|
|
62
|
-
}
|
|
63
|
+
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 获取多个缓存内容。
|
|
68
|
+
*
|
|
69
|
+
* @param keys 键
|
|
70
|
+
* @returns 返回获取到的缓存内容
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
async getMany(...keys) {
|
|
75
|
+
const redisKeys = keys.map(key => this.toRedisKey(key));
|
|
76
|
+
if (!redisKeys.length) return [];
|
|
77
|
+
const gotValues = await x.redis.mget(...redisKeys);
|
|
78
|
+
return gotValues.map(value => {
|
|
79
|
+
if (value == null) return null;
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
return JSON.parse(value);
|
|
83
|
+
} catch {
|
|
63
84
|
return null;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 缓存内容。
|
|
90
|
+
*
|
|
91
|
+
* @param key 键
|
|
92
|
+
* @param action 获取缓存内容的操作
|
|
93
|
+
* @param ttl 缓存时间(单位:时)
|
|
94
|
+
* @returns 返回获取到的内容
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async remember(key, action, ttl = this.options.ttl) {
|
|
99
|
+
let value = await this.get(key);
|
|
100
|
+
|
|
101
|
+
if (value === null) {
|
|
102
|
+
value = await action();
|
|
103
|
+
await this.set(key, value, ttl);
|
|
64
104
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* 新增元素。
|
|
125
|
-
*
|
|
126
|
-
* @param key 键
|
|
127
|
-
* @param values 元素列表
|
|
128
|
-
*/
|
|
129
|
-
async push(key, ...values) {
|
|
130
|
-
const redisKey = this.toRedisKey(key);
|
|
131
|
-
return x_1.x.redis.rpush(redisKey, ...values.map(value => JSON.stringify(value)));
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* 从头部弹出元素并返回。
|
|
135
|
-
*
|
|
136
|
-
* @param key 键
|
|
137
|
-
* @param count 弹出元素个数
|
|
138
|
-
*/
|
|
139
|
-
async shift(key, count = 1) {
|
|
140
|
-
const redisKey = this.toRedisKey(key);
|
|
141
|
-
const res = [];
|
|
142
|
-
for (let i = 0; i < count; i++) {
|
|
143
|
-
const item = await x_1.x.redis.lpop(redisKey);
|
|
144
|
-
if (item) {
|
|
145
|
-
res.push(JSON.parse(item));
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return res;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* 删除缓存。
|
|
155
|
-
*
|
|
156
|
-
* @param keys 键列表
|
|
157
|
-
*/
|
|
158
|
-
async remove(...keys) {
|
|
159
|
-
return Promise.all(keys.map(async (key) => {
|
|
160
|
-
const redisKey = this.toRedisKey(key);
|
|
161
|
-
return x_1.x.redis.del(redisKey);
|
|
162
|
-
}));
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* 清空全部缓存。
|
|
166
|
-
*/
|
|
167
|
-
async clearAll() {
|
|
168
|
-
const keys = await x_1.x.redis.keys(`${this.prefix}*`);
|
|
169
|
-
if (keys.length) {
|
|
170
|
-
return x_1.x.redis.del(...keys);
|
|
171
|
-
}
|
|
172
|
-
return 0;
|
|
105
|
+
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 自增。
|
|
110
|
+
*
|
|
111
|
+
* @param key 键
|
|
112
|
+
* @param increment 增量
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
async increase(key, increment = 1) {
|
|
117
|
+
const redisKey = this.toRedisKey(key);
|
|
118
|
+
return x.redis.incrby(redisKey, increment);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 自减。
|
|
122
|
+
*
|
|
123
|
+
* @param key 键
|
|
124
|
+
* @param decrement 减量
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
async decrease(key, decrement = 1) {
|
|
129
|
+
return this.increase(key, -decrement);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 新增元素。
|
|
133
|
+
*
|
|
134
|
+
* @param key 键
|
|
135
|
+
* @param values 元素列表
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
async push(key, ...values) {
|
|
140
|
+
const redisKey = this.toRedisKey(key);
|
|
141
|
+
return x.redis.rpush(redisKey, ...values.map(value => JSON.stringify(value)));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 从头部弹出元素并返回。
|
|
145
|
+
*
|
|
146
|
+
* @param key 键
|
|
147
|
+
* @param count 弹出元素个数
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
async shift(key, count = 1) {
|
|
152
|
+
const redisKey = this.toRedisKey(key);
|
|
153
|
+
const res = [];
|
|
154
|
+
|
|
155
|
+
for (let i = 0; i < count; i++) {
|
|
156
|
+
const item = await x.redis.lpop(redisKey);
|
|
157
|
+
|
|
158
|
+
if (item) {
|
|
159
|
+
res.push(JSON.parse(item));
|
|
160
|
+
} else {
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
173
163
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
164
|
+
|
|
165
|
+
return res;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 删除缓存。
|
|
169
|
+
*
|
|
170
|
+
* @param keys 键列表
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
async remove(...keys) {
|
|
175
|
+
return Promise.all(keys.map(async key => {
|
|
176
|
+
const redisKey = this.toRedisKey(key);
|
|
177
|
+
return x.redis.del(redisKey);
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* 清空全部缓存。
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
async clearAll() {
|
|
186
|
+
const keys = await x.redis.keys(`${this.prefix}*`);
|
|
187
|
+
|
|
188
|
+
if (keys.length) {
|
|
189
|
+
return x.redis.del(...keys);
|
|
183
190
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
191
|
+
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 清空指定前缀的缓存。
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
async clear(...prefixes) {
|
|
200
|
+
const keys = (await Promise.all(prefixes.map(prefix => x.redis.keys(`${this.toRedisKey(prefix)}*`)))).flat();
|
|
201
|
+
|
|
202
|
+
if (keys.length) {
|
|
203
|
+
return x.redis.del(...keys);
|
|
194
204
|
}
|
|
195
|
-
|
|
196
|
-
|
|
205
|
+
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* 派生出一个指定类型的缓存服务。
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
fork(options) {
|
|
214
|
+
return options ? new CacheService({ ...this.options,
|
|
215
|
+
...options
|
|
216
|
+
}) : this;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
}
|
package/lib/services/captcha.js
CHANGED
|
@@ -1,34 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import svgCaptcha from 'svg-captcha';
|
|
2
|
+
import { x } from "../x";
|
|
3
|
+
export class CaptchaService {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.options = options;
|
|
6
|
+
this.serviceName = 'captcha';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async generate() {
|
|
10
|
+
const res = svgCaptcha.create({
|
|
11
|
+
size: this.options.size
|
|
12
|
+
});
|
|
13
|
+
const token = await x.cache.save(res.text, this.options.ttl);
|
|
14
|
+
return {
|
|
15
|
+
text: res.text,
|
|
16
|
+
svg: res.data,
|
|
17
|
+
token: token
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async verify(options) {
|
|
22
|
+
var _options$text;
|
|
23
|
+
|
|
24
|
+
if (((_options$text = options.text) == null ? void 0 : _options$text.length) !== this.options.size) {
|
|
25
|
+
return false;
|
|
13
26
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
svg: res.data,
|
|
22
|
-
token: token,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
async verify(options) {
|
|
26
|
-
if (options.text?.length !== this.options.size) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
const expectedText = await x_1.x.cache.get(options.token);
|
|
30
|
-
await x_1.x.cache.remove(options.token);
|
|
31
|
-
return options.text === expectedText;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
exports.CaptchaService = CaptchaService;
|
|
27
|
+
|
|
28
|
+
const expectedText = await x.cache.get(options.token);
|
|
29
|
+
await x.cache.remove(options.token);
|
|
30
|
+
return options.text === expectedText;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
}
|
|
@@ -5,7 +5,7 @@ export interface DisposeOptions {
|
|
|
5
5
|
export declare class DisposeService implements BaseService {
|
|
6
6
|
private options?;
|
|
7
7
|
serviceName: string;
|
|
8
|
-
constructor(options?: DisposeOptions
|
|
8
|
+
constructor(options?: DisposeOptions);
|
|
9
9
|
private disposes;
|
|
10
10
|
add(fn: () => any): void;
|
|
11
11
|
dispose(): Promise<any[]>;
|