@jayfong/x-server 1.12.0 → 1.14.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/CHANGELOG.md +26 -0
- package/lib/_cjs/core/define_task.js +1 -1
- package/lib/_cjs/core/handler.js +14 -0
- package/lib/_cjs/services/cache.js +1 -1
- package/lib/_cjs/services/captcha.js +21 -9
- package/lib/_cjs/x.js +1 -1
- package/lib/core/define_task.js +1 -1
- package/lib/core/handler.js +14 -0
- package/lib/core/types.d.ts +6 -1
- package/lib/services/cache.js +1 -1
- package/lib/services/captcha.d.ts +8 -3
- package/lib/services/captcha.js +21 -9
- package/lib/x.d.ts +1 -1
- package/lib/x.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [1.14.0](https://github.com/jfWorks/x-server/compare/v1.13.1...v1.14.0) (2022-04-26)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* requestDataSchema ([72970dd](https://github.com/jfWorks/x-server/commit/72970dd63810ab9b3fcdc9390fcde3cfdd4a6013))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* text -> code ([a8571e3](https://github.com/jfWorks/x-server/commit/a8571e3c99036467f2110b5ab30798c4e6bc42af))
|
|
16
|
+
|
|
17
|
+
### [1.13.1](https://github.com/jfWorks/x-server/compare/v1.13.0...v1.13.1) (2022-04-26)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* appname -> appid ([88d14d1](https://github.com/jfWorks/x-server/commit/88d14d18078c7bc19ddf2c973e53cc0353a533de))
|
|
23
|
+
|
|
24
|
+
## [1.13.0](https://github.com/jfWorks/x-server/compare/v1.12.0...v1.13.0) (2022-04-26)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Features
|
|
28
|
+
|
|
29
|
+
* **CaptchaService:** generate, generateImage ([22bf340](https://github.com/jfWorks/x-server/commit/22bf3404cfb7d3a530714471fe004fcd9ee60760))
|
|
30
|
+
|
|
5
31
|
## [1.12.0](https://github.com/jfWorks/x-server/compare/v1.11.6...v1.12.0) (2022-04-25)
|
|
6
32
|
|
|
7
33
|
|
|
@@ -12,7 +12,7 @@ var _x = require("../x");
|
|
|
12
12
|
async function defineTask(options) {
|
|
13
13
|
const queue = new _bull.default(options.name, {
|
|
14
14
|
redis: _x.x.redis.options,
|
|
15
|
-
prefix: `${_x.x.
|
|
15
|
+
prefix: `${_x.x.appId}_task`
|
|
16
16
|
});
|
|
17
17
|
queue.process(async job => {
|
|
18
18
|
return options.handle(job.data);
|
package/lib/_cjs/core/handler.js
CHANGED
|
@@ -27,6 +27,20 @@ class Handler {
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
this.handleHttp = async (data, ctx) => {
|
|
30
|
+
// 输入验证
|
|
31
|
+
if (this.options.requestDataSchema) {
|
|
32
|
+
try {
|
|
33
|
+
data = await this.options.requestDataSchema.validate( // @ts-ignore
|
|
34
|
+
data, {
|
|
35
|
+
strict: true,
|
|
36
|
+
abortEarly: true,
|
|
37
|
+
stripUnknown: true
|
|
38
|
+
});
|
|
39
|
+
} catch (err) {
|
|
40
|
+
throw new _http_error.HttpError.BadRequest(err.message);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
30
44
|
let res = await this.options.handle(data, ctx); // 打包返回数据
|
|
31
45
|
|
|
32
46
|
if (this.options.responseDataPack) {
|
|
@@ -16,28 +16,40 @@ class CaptchaService {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
async generate() {
|
|
19
|
-
const
|
|
19
|
+
const code = Math.random().toString().slice(0 - this.options.size);
|
|
20
|
+
const token = await _x.x.cache.save(code, this.options.ttl);
|
|
21
|
+
return {
|
|
22
|
+
token: token,
|
|
23
|
+
code: code
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async generateImage() {
|
|
28
|
+
const {
|
|
29
|
+
text: code,
|
|
30
|
+
data: svg
|
|
31
|
+
} = _svgCaptcha.default.create({
|
|
20
32
|
size: this.options.size
|
|
21
33
|
});
|
|
22
34
|
|
|
23
|
-
const token = await _x.x.cache.save(
|
|
35
|
+
const token = await _x.x.cache.save(code, this.options.ttl);
|
|
24
36
|
return {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
token: token,
|
|
38
|
+
code: code,
|
|
39
|
+
svg: svg
|
|
28
40
|
};
|
|
29
41
|
}
|
|
30
42
|
|
|
31
43
|
async verify(options) {
|
|
32
|
-
var _options$
|
|
44
|
+
var _options$code;
|
|
33
45
|
|
|
34
|
-
if (((_options$
|
|
46
|
+
if (((_options$code = options.code) == null ? void 0 : _options$code.length) !== this.options.size) {
|
|
35
47
|
return false;
|
|
36
48
|
}
|
|
37
49
|
|
|
38
|
-
const
|
|
50
|
+
const expectedCode = await _x.x.cache.get(options.token);
|
|
39
51
|
await _x.x.cache.remove(options.token);
|
|
40
|
-
return options.
|
|
52
|
+
return options.code === expectedCode;
|
|
41
53
|
}
|
|
42
54
|
|
|
43
55
|
}
|
package/lib/_cjs/x.js
CHANGED
package/lib/core/define_task.js
CHANGED
|
@@ -3,7 +3,7 @@ import { x } from "../x";
|
|
|
3
3
|
export async function defineTask(options) {
|
|
4
4
|
const queue = new Queue(options.name, {
|
|
5
5
|
redis: x.redis.options,
|
|
6
|
-
prefix: `${x.
|
|
6
|
+
prefix: `${x.appId}_task`
|
|
7
7
|
});
|
|
8
8
|
queue.process(async job => {
|
|
9
9
|
return options.handle(job.data);
|
package/lib/core/handler.js
CHANGED
|
@@ -19,6 +19,20 @@ export class Handler {
|
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
this.handleHttp = async (data, ctx) => {
|
|
22
|
+
// 输入验证
|
|
23
|
+
if (this.options.requestDataSchema) {
|
|
24
|
+
try {
|
|
25
|
+
data = await this.options.requestDataSchema.validate( // @ts-ignore
|
|
26
|
+
data, {
|
|
27
|
+
strict: true,
|
|
28
|
+
abortEarly: true,
|
|
29
|
+
stripUnknown: true
|
|
30
|
+
});
|
|
31
|
+
} catch (err) {
|
|
32
|
+
throw new HttpError.BadRequest(err.message);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
22
36
|
let res = await this.options.handle(data, ctx); // 打包返回数据
|
|
23
37
|
|
|
24
38
|
if (this.options.responseDataPack) {
|
package/lib/core/types.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { BasePlugin } from '../plugins/base';
|
|
3
3
|
import { BaseService } from '../services/base';
|
|
4
|
-
import
|
|
4
|
+
import { yup } from 'vtils/validator';
|
|
5
|
+
import type { AsyncOrSync, OneOrMore, RequiredDeep } from 'vtils/types';
|
|
5
6
|
import type { DisposeService } from '../services/dispose';
|
|
6
7
|
import type { Handler } from './handler';
|
|
7
8
|
import type { IncomingHttpHeaders } from 'http';
|
|
@@ -89,6 +90,10 @@ export declare namespace XHandler {
|
|
|
89
90
|
* @default false
|
|
90
91
|
*/
|
|
91
92
|
responseDataPack?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* 请求数据验证结构
|
|
95
|
+
*/
|
|
96
|
+
requestDataSchema?: (_: typeof yup) => yup.GetSchema<RequiredDeep<TReqData>>;
|
|
92
97
|
/**
|
|
93
98
|
* 处理器
|
|
94
99
|
*/
|
package/lib/services/cache.js
CHANGED
|
@@ -11,19 +11,24 @@ export interface CaptchaOptions {
|
|
|
11
11
|
ttl: MsValue;
|
|
12
12
|
}
|
|
13
13
|
export interface CaptchaGenerateResult {
|
|
14
|
-
text: string;
|
|
15
|
-
svg: string;
|
|
16
14
|
token: string;
|
|
15
|
+
code: string;
|
|
16
|
+
}
|
|
17
|
+
export interface CaptchaGenerateImageResult {
|
|
18
|
+
token: string;
|
|
19
|
+
code: string;
|
|
20
|
+
svg: string;
|
|
17
21
|
}
|
|
18
22
|
export interface CaptchaVerifyOptions {
|
|
19
|
-
text: string;
|
|
20
23
|
token: string;
|
|
24
|
+
code: string;
|
|
21
25
|
}
|
|
22
26
|
export declare class CaptchaService implements BaseService {
|
|
23
27
|
private options;
|
|
24
28
|
serviceName: string;
|
|
25
29
|
constructor(options: CaptchaOptions);
|
|
26
30
|
generate(): Promise<CaptchaGenerateResult>;
|
|
31
|
+
generateImage(): Promise<CaptchaGenerateImageResult>;
|
|
27
32
|
verify(options: CaptchaVerifyOptions): Promise<boolean>;
|
|
28
33
|
}
|
|
29
34
|
declare module '../x' {
|
package/lib/services/captcha.js
CHANGED
|
@@ -7,27 +7,39 @@ export class CaptchaService {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
async generate() {
|
|
10
|
-
const
|
|
10
|
+
const code = Math.random().toString().slice(0 - this.options.size);
|
|
11
|
+
const token = await x.cache.save(code, this.options.ttl);
|
|
12
|
+
return {
|
|
13
|
+
token: token,
|
|
14
|
+
code: code
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async generateImage() {
|
|
19
|
+
const {
|
|
20
|
+
text: code,
|
|
21
|
+
data: svg
|
|
22
|
+
} = svgCaptcha.create({
|
|
11
23
|
size: this.options.size
|
|
12
24
|
});
|
|
13
|
-
const token = await x.cache.save(
|
|
25
|
+
const token = await x.cache.save(code, this.options.ttl);
|
|
14
26
|
return {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
27
|
+
token: token,
|
|
28
|
+
code: code,
|
|
29
|
+
svg: svg
|
|
18
30
|
};
|
|
19
31
|
}
|
|
20
32
|
|
|
21
33
|
async verify(options) {
|
|
22
|
-
var _options$
|
|
34
|
+
var _options$code;
|
|
23
35
|
|
|
24
|
-
if (((_options$
|
|
36
|
+
if (((_options$code = options.code) == null ? void 0 : _options$code.length) !== this.options.size) {
|
|
25
37
|
return false;
|
|
26
38
|
}
|
|
27
39
|
|
|
28
|
-
const
|
|
40
|
+
const expectedCode = await x.cache.get(options.token);
|
|
29
41
|
await x.cache.remove(options.token);
|
|
30
|
-
return options.
|
|
42
|
+
return options.code === expectedCode;
|
|
31
43
|
}
|
|
32
44
|
|
|
33
45
|
}
|
package/lib/x.d.ts
CHANGED
package/lib/x.js
CHANGED