@jayfong/x-server 2.118.1 → 3.0.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/lib/_virtual/_rolldown/runtime.mjs +4 -0
- package/lib/cli/api_generator.d.mts +74 -0
- package/lib/cli/api_generator.mjs +310 -0
- package/lib/cli/build_util.d.mts +17 -0
- package/lib/cli/build_util.mjs +156 -0
- package/lib/cli/cli.d.mts +1 -0
- package/lib/cli/cli.mjs +355 -0
- package/lib/cli/css_register.d.mts +1 -0
- package/lib/cli/css_register.mjs +7 -0
- package/lib/cli/deploy_util.d.mts +23 -0
- package/lib/cli/deploy_util.mjs +70 -0
- package/lib/cli/dev_util.d.mts +7 -0
- package/lib/cli/dev_util.mjs +25 -0
- package/lib/cli/env_util.d.mts +45 -0
- package/lib/cli/env_util.mjs +207 -0
- package/lib/cli/esbuild_register.d.mts +1 -0
- package/lib/cli/esbuild_register.mjs +7 -0
- package/lib/cli/template_util.d.mts +18 -0
- package/lib/cli/template_util.mjs +85 -0
- package/lib/cli/templates/crons.ts +1 -1
- package/lib/cli/templates/handlers.ts +1 -1
- package/lib/cli/templates/hooks.ts +1 -1
- package/lib/cli/templates/models.ts +7 -7
- package/lib/cli/templates/package.json +11 -1
- package/lib/cli/templates/routes.ts +2 -1
- package/lib/core/ctx_storage.d.mts +3 -0
- package/lib/core/ctx_storage.mjs +4 -0
- package/lib/core/define_bus.d.mts +37 -0
- package/lib/core/define_bus.mjs +12 -0
- package/lib/core/{define_cron.d.ts → define_cron.d.mts} +2 -2
- package/lib/core/define_cron.mjs +11 -0
- package/lib/core/{define_handler.d.ts → define_handler.d.mts} +19 -19
- package/lib/core/define_handler.mjs +44 -0
- package/lib/core/define_hook.d.mts +2 -0
- package/lib/core/define_hook.mjs +6 -0
- package/lib/core/define_server.d.mts +3 -0
- package/lib/core/define_server.mjs +8 -0
- package/lib/core/{define_task.d.ts → define_task.d.mts} +2 -2
- package/lib/core/define_task.mjs +102 -0
- package/lib/core/get_handler_url.d.mts +2 -0
- package/lib/core/get_handler_url.mjs +7 -0
- package/lib/core/handler.d.mts +16 -0
- package/lib/core/handler.mjs +132 -0
- package/lib/core/http_error.d.mts +2 -0
- package/lib/core/http_error.mjs +1 -0
- package/lib/core/http_header.d.mts +856 -0
- package/lib/core/http_header.mjs +860 -0
- package/lib/core/http_method.d.mts +3 -0
- package/lib/core/http_method.mjs +10 -0
- package/lib/core/http_redirect.d.mts +5 -0
- package/lib/core/http_redirect.mjs +10 -0
- package/lib/core/server.d.mts +20 -0
- package/lib/core/server.mjs +186 -0
- package/lib/core/types.d.mts +256 -0
- package/lib/exports.d.mts +46 -0
- package/lib/exports.mjs +46 -0
- package/lib/index.d.mts +48 -0
- package/lib/index.mjs +45 -0
- package/lib/plugins/base.d.mts +4 -0
- package/lib/plugins/compress.d.mts +12 -0
- package/lib/plugins/compress.mjs +15 -0
- package/lib/plugins/cookie.d.mts +12 -0
- package/lib/plugins/cookie.mjs +15 -0
- package/lib/plugins/cors.d.mts +22 -0
- package/lib/plugins/cors.mjs +34 -0
- package/lib/plugins/file_parser.d.mts +15 -0
- package/lib/plugins/file_parser.mjs +24 -0
- package/lib/plugins/form_body_parser.d.mts +12 -0
- package/lib/plugins/form_body_parser.mjs +15 -0
- package/lib/plugins/ws_parser.d.mts +12 -0
- package/lib/plugins/ws_parser.mjs +15 -0
- package/lib/plugins/xml_parser.d.mts +19 -0
- package/lib/plugins/xml_parser.mjs +40 -0
- package/lib/services/ai.d.mts +31 -0
- package/lib/services/ai.mjs +44 -0
- package/lib/services/base.d.mts +4 -0
- package/lib/services/base.mjs +1 -0
- package/lib/services/cache.d.mts +165 -0
- package/lib/services/cache.mjs +323 -0
- package/lib/services/captcha.d.mts +65 -0
- package/lib/services/captcha.mjs +61 -0
- package/lib/services/{crypto.js → crypto.d.mts} +19 -78
- package/lib/services/crypto.mjs +141 -0
- package/lib/services/db_value.d.mts +60 -0
- package/lib/services/db_value.mjs +64 -0
- package/lib/services/dingtalk.d.mts +39 -0
- package/lib/services/dingtalk.mjs +44 -0
- package/lib/services/dispose.d.mts +17 -0
- package/lib/services/dispose.mjs +18 -0
- package/lib/services/emoji.d.mts +10 -0
- package/lib/services/emoji.mjs +2025 -0
- package/lib/services/file_store.d.mts +49 -0
- package/lib/services/file_store.mjs +58 -0
- package/lib/services/html.d.mts +11 -0
- package/lib/services/html.mjs +12 -0
- package/lib/services/jwt.d.mts +33 -0
- package/lib/services/jwt.mjs +42 -0
- package/lib/services/log.d.mts +37 -0
- package/lib/services/log.mjs +99 -0
- package/lib/services/mail.d.mts +22 -0
- package/lib/services/mail.mjs +28 -0
- package/lib/services/pay.d.mts +123 -0
- package/lib/services/pay.mjs +157 -0
- package/lib/services/random.d.mts +27 -0
- package/lib/services/random.mjs +42 -0
- package/lib/services/rate_limit.d.mts +37 -0
- package/lib/services/rate_limit.mjs +40 -0
- package/lib/services/redis.d.mts +11 -0
- package/lib/services/redis.mjs +6 -0
- package/lib/services/sensitive_words.d.mts +29 -0
- package/lib/services/sensitive_words.mjs +37 -0
- package/lib/services/sms.d.mts +40 -0
- package/lib/services/sms.mjs +30238 -0
- package/lib/types/dto.d.mts +30 -0
- package/lib/types/dto.mjs +1 -0
- package/lib/x.d.mts +11 -0
- package/lib/x.mjs +21 -0
- package/lib/x_init.d.mts +1 -0
- package/lib/x_init.mjs +10 -0
- package/package.json +99 -43
- package/lib/_cjs/cli/api_generator.js +0 -438
- package/lib/_cjs/cli/build_util.js +0 -243
- package/lib/_cjs/cli/cli.js +0 -378
- package/lib/_cjs/cli/css_register.js +0 -7
- package/lib/_cjs/cli/deploy_util.js +0 -76
- package/lib/_cjs/cli/dev_util.js +0 -25
- package/lib/_cjs/cli/env_util.js +0 -252
- package/lib/_cjs/cli/esbuild_register.js +0 -7
- package/lib/_cjs/cli/template_util.js +0 -101
- package/lib/_cjs/cli/templates/crons.ts +0 -2
- package/lib/_cjs/cli/templates/handlers.ts +0 -3
- package/lib/_cjs/cli/templates/hooks.ts +0 -2
- package/lib/_cjs/cli/templates/models.ts +0 -123
- package/lib/_cjs/cli/templates/package.json +0 -3
- package/lib/_cjs/cli/templates/routes.ts +0 -68
- package/lib/_cjs/core/ctx_storage.js +0 -6
- package/lib/_cjs/core/define_bus.js +0 -31
- package/lib/_cjs/core/define_cron.js +0 -13
- package/lib/_cjs/core/define_handler.js +0 -56
- package/lib/_cjs/core/define_hook.js +0 -8
- package/lib/_cjs/core/define_server.js +0 -10
- package/lib/_cjs/core/define_task.js +0 -113
- package/lib/_cjs/core/get_handler_url.js +0 -11
- package/lib/_cjs/core/handler.js +0 -216
- package/lib/_cjs/core/http_error.js +0 -6
- package/lib/_cjs/core/http_header.js +0 -142
- package/lib/_cjs/core/http_method.js +0 -12
- package/lib/_cjs/core/http_redirect.js +0 -11
- package/lib/_cjs/core/server.js +0 -221
- package/lib/_cjs/core/types.js +0 -8
- package/lib/_cjs/exports.js +0 -273
- package/lib/_cjs/index.js +0 -15
- package/lib/_cjs/plugins/base.js +0 -3
- package/lib/_cjs/plugins/compress.js +0 -18
- package/lib/_cjs/plugins/cookie.js +0 -18
- package/lib/_cjs/plugins/cors.js +0 -38
- package/lib/_cjs/plugins/file_parser.js +0 -28
- package/lib/_cjs/plugins/form_body_parser.js +0 -18
- package/lib/_cjs/plugins/ws_parser.js +0 -18
- package/lib/_cjs/plugins/xml_parser.js +0 -48
- package/lib/_cjs/services/ai.js +0 -48
- package/lib/_cjs/services/base.js +0 -3
- package/lib/_cjs/services/cache.js +0 -400
- package/lib/_cjs/services/captcha.js +0 -78
- package/lib/_cjs/services/crypto.js +0 -175
- package/lib/_cjs/services/db_value.js +0 -77
- package/lib/_cjs/services/dingtalk.js +0 -51
- package/lib/_cjs/services/dispose.js +0 -23
- package/lib/_cjs/services/emoji.js +0 -2034
- package/lib/_cjs/services/file_store.js +0 -67
- package/lib/_cjs/services/html.js +0 -17
- package/lib/_cjs/services/jwt.js +0 -65
- package/lib/_cjs/services/log.js +0 -108
- package/lib/_cjs/services/mail.js +0 -31
- package/lib/_cjs/services/pay.js +0 -191
- package/lib/_cjs/services/random.js +0 -51
- package/lib/_cjs/services/rate_limit.js +0 -54
- package/lib/_cjs/services/redis.js +0 -12
- package/lib/_cjs/services/sensitive_words.js +0 -62
- package/lib/_cjs/services/sms.js +0 -84
- package/lib/_cjs/types/dto.js +0 -3
- package/lib/_cjs/x.js +0 -35
- package/lib/cli/api_generator.d.ts +0 -74
- package/lib/cli/api_generator.js +0 -432
- package/lib/cli/build_util.d.ts +0 -17
- package/lib/cli/build_util.js +0 -236
- package/lib/cli/cli.d.ts +0 -2
- package/lib/cli/cli.js +0 -374
- package/lib/cli/css_register.js +0 -5
- package/lib/cli/deploy_util.d.ts +0 -23
- package/lib/cli/deploy_util.js +0 -70
- package/lib/cli/dev_util.d.ts +0 -8
- package/lib/cli/dev_util.js +0 -19
- package/lib/cli/env_util.d.ts +0 -45
- package/lib/cli/env_util.js +0 -246
- package/lib/cli/esbuild_register.js +0 -5
- package/lib/cli/template_util.d.ts +0 -18
- package/lib/cli/template_util.js +0 -95
- package/lib/core/ctx_storage.d.ts +0 -3
- package/lib/core/ctx_storage.js +0 -2
- package/lib/core/define_bus.d.ts +0 -37
- package/lib/core/define_bus.js +0 -27
- package/lib/core/define_cron.js +0 -9
- package/lib/core/define_handler.js +0 -48
- package/lib/core/define_hook.d.ts +0 -2
- package/lib/core/define_hook.js +0 -4
- package/lib/core/define_server.d.ts +0 -3
- package/lib/core/define_server.js +0 -6
- package/lib/core/define_task.js +0 -107
- package/lib/core/get_handler_url.d.ts +0 -2
- package/lib/core/get_handler_url.js +0 -7
- package/lib/core/handler.d.ts +0 -16
- package/lib/core/handler.js +0 -209
- package/lib/core/http_error.d.ts +0 -2
- package/lib/core/http_error.js +0 -2
- package/lib/core/http_header.d.ts +0 -856
- package/lib/core/http_header.js +0 -139
- package/lib/core/http_method.d.ts +0 -3
- package/lib/core/http_method.js +0 -8
- package/lib/core/http_redirect.d.ts +0 -5
- package/lib/core/http_redirect.js +0 -6
- package/lib/core/server.d.ts +0 -20
- package/lib/core/server.js +0 -215
- package/lib/core/types.d.ts +0 -265
- package/lib/core/types.js +0 -4
- package/lib/exports.d.ts +0 -45
- package/lib/exports.js +0 -47
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -3
- package/lib/plugins/base.d.ts +0 -4
- package/lib/plugins/base.js +0 -1
- package/lib/plugins/compress.d.ts +0 -13
- package/lib/plugins/compress.js +0 -12
- package/lib/plugins/cookie.d.ts +0 -13
- package/lib/plugins/cookie.js +0 -12
- package/lib/plugins/cors.d.ts +0 -22
- package/lib/plugins/cors.js +0 -33
- package/lib/plugins/file_parser.d.ts +0 -16
- package/lib/plugins/file_parser.js +0 -22
- package/lib/plugins/form_body_parser.d.ts +0 -13
- package/lib/plugins/form_body_parser.js +0 -12
- package/lib/plugins/ws_parser.d.ts +0 -13
- package/lib/plugins/ws_parser.js +0 -12
- package/lib/plugins/xml_parser.d.ts +0 -19
- package/lib/plugins/xml_parser.js +0 -43
- package/lib/services/ai.d.ts +0 -31
- package/lib/services/ai.js +0 -43
- package/lib/services/base.d.ts +0 -4
- package/lib/services/base.js +0 -1
- package/lib/services/cache.d.ts +0 -173
- package/lib/services/cache.js +0 -394
- package/lib/services/captcha.d.ts +0 -65
- package/lib/services/captcha.js +0 -72
- package/lib/services/crypto.d.ts +0 -110
- package/lib/services/db_value.d.ts +0 -60
- package/lib/services/db_value.js +0 -72
- package/lib/services/dingtalk.d.ts +0 -39
- package/lib/services/dingtalk.js +0 -45
- package/lib/services/dispose.d.ts +0 -17
- package/lib/services/dispose.js +0 -17
- package/lib/services/emoji.d.ts +0 -10
- package/lib/services/emoji.js +0 -2029
- package/lib/services/file_store.d.ts +0 -49
- package/lib/services/file_store.js +0 -61
- package/lib/services/html.d.ts +0 -11
- package/lib/services/html.js +0 -12
- package/lib/services/jwt.d.ts +0 -34
- package/lib/services/jwt.js +0 -59
- package/lib/services/log.d.ts +0 -37
- package/lib/services/log.js +0 -102
- package/lib/services/mail.d.ts +0 -23
- package/lib/services/mail.js +0 -25
- package/lib/services/pay.d.ts +0 -123
- package/lib/services/pay.js +0 -185
- package/lib/services/random.d.ts +0 -27
- package/lib/services/random.js +0 -45
- package/lib/services/rate_limit.d.ts +0 -37
- package/lib/services/rate_limit.js +0 -49
- package/lib/services/redis.d.ts +0 -12
- package/lib/services/redis.js +0 -7
- package/lib/services/sensitive_words.d.ts +0 -30
- package/lib/services/sensitive_words.js +0 -57
- package/lib/services/sms.d.ts +0 -40
- package/lib/services/sms.js +0 -78
- package/lib/types/dto.d.ts +0 -86
- package/lib/types/dto.js +0 -1
- package/lib/x.d.ts +0 -17
- package/lib/x.js +0 -30
- /package/lib/{cli/css_register.d.ts → core/types.mjs} +0 -0
- /package/lib/{cli/esbuild_register.d.ts → plugins/base.mjs} +0 -0
package/lib/core/define_task.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import assert from 'assert';
|
|
2
|
-
import Queue from 'bull';
|
|
3
|
-
import { ms } from 'vtils';
|
|
4
|
-
import { x } from "../x";
|
|
5
|
-
export function defineTask(options) {
|
|
6
|
-
const queue = new Queue(options.name, {
|
|
7
|
-
redis: {
|
|
8
|
-
...x.redis.options,
|
|
9
|
-
// https://github.com/OptimalBits/bull/issues/2186
|
|
10
|
-
maxRetriesPerRequest: null,
|
|
11
|
-
enableReadyCheck: false
|
|
12
|
-
},
|
|
13
|
-
prefix: `${x.appId}_task`,
|
|
14
|
-
defaultJobOptions: {
|
|
15
|
-
// 都设为 true 防止占用内存
|
|
16
|
-
removeOnComplete: true,
|
|
17
|
-
removeOnFail: true
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
queue.process(options.concurrency || 1, async job => {
|
|
21
|
-
return options.handle(job.data);
|
|
22
|
-
});
|
|
23
|
-
return queue;
|
|
24
|
-
}
|
|
25
|
-
export function defineSliceTask(options) {
|
|
26
|
-
const task = defineTask({
|
|
27
|
-
name: options.name,
|
|
28
|
-
concurrency: 1,
|
|
29
|
-
handle: async data => {
|
|
30
|
-
const res = data.count ? await x.redis.multi([['lrange', data.redisKey, `-${data.count}`, '-1'], ['ltrim', data.redisKey, '0', `-${data.count + 1}`]]).exec() : await x.redis.multi([['lrange', data.redisKey, '0', '-1'], ['del', data.redisKey]]).exec();
|
|
31
|
-
const list = res[0][1].reverse().map(item => JSON.parse(item));
|
|
32
|
-
if (list.length) {
|
|
33
|
-
return options.handle(list, data.key);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
const addAction = async (data, addOptions) => {
|
|
38
|
-
const redisKeyPrefix = `${x.appId}_batch_task_${options.name}`;
|
|
39
|
-
const key = addOptions?.key || '';
|
|
40
|
-
const duration = addOptions?.duration != null ? ms(addOptions.duration) : typeof options.duration === 'function' ? ms(options.duration(key)) : options.duration && ms(options.duration);
|
|
41
|
-
const threshold = addOptions?.threshold || typeof options.threshold && (typeof options.threshold === 'function' ? options.threshold(key) : options.threshold);
|
|
42
|
-
const thresholdTimeout = addOptions?.thresholdTimeout != null ? ms(addOptions.thresholdTimeout) : typeof options.thresholdTimeout === 'function' ? ms(options.thresholdTimeout(key)) : options.thresholdTimeout && ms(options.thresholdTimeout);
|
|
43
|
-
assert(duration != null || threshold != null, '参数 threshold 和 duration 必须至少设置 1 个');
|
|
44
|
-
const redisKey = !key ? redisKeyPrefix : `${redisKeyPrefix}_${key}`;
|
|
45
|
-
const res = await x.redis.multi([['llen', redisKey], ['lpush', redisKey, JSON.stringify(data)]]).exec();
|
|
46
|
-
const count = parseInt(res[0][1], 10) + 1;
|
|
47
|
-
|
|
48
|
-
// 仅时段
|
|
49
|
-
// 1分钟内的合并推送
|
|
50
|
-
if (duration != null) {
|
|
51
|
-
if (count === 1) {
|
|
52
|
-
await task.add({
|
|
53
|
-
key: key,
|
|
54
|
-
redisKey: redisKey
|
|
55
|
-
}, {
|
|
56
|
-
delay: duration
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
// 仅阈值
|
|
61
|
-
// 满10条推送
|
|
62
|
-
else if (threshold != null) {
|
|
63
|
-
const delayTaskId = redisKey;
|
|
64
|
-
const prevJob = await task.getJob(delayTaskId);
|
|
65
|
-
|
|
66
|
-
// 到达阈值,立即发送
|
|
67
|
-
if (count === threshold) {
|
|
68
|
-
await prevJob?.remove();
|
|
69
|
-
await task.add({
|
|
70
|
-
key: key,
|
|
71
|
-
redisKey: redisKey
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
// 存在超时设置
|
|
75
|
-
else if (thresholdTimeout) {
|
|
76
|
-
if (!prevJob) {
|
|
77
|
-
await task.add({
|
|
78
|
-
key: key,
|
|
79
|
-
redisKey: redisKey
|
|
80
|
-
}, {
|
|
81
|
-
jobId: delayTaskId,
|
|
82
|
-
delay: thresholdTimeout
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
const addTask = defineTask({
|
|
89
|
-
name: `${options.name}_add`,
|
|
90
|
-
concurrency: 1,
|
|
91
|
-
handle: async payload => {
|
|
92
|
-
await addAction(payload.data, payload.options);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
const res = {
|
|
96
|
-
add: async (data, addOptions) => {
|
|
97
|
-
const delay = addOptions.delay && ms(addOptions.delay) || 0;
|
|
98
|
-
await addTask.add({
|
|
99
|
-
data: data,
|
|
100
|
-
options: addOptions
|
|
101
|
-
}, {
|
|
102
|
-
delay: delay
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
return res;
|
|
107
|
-
}
|
package/lib/core/handler.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { RequiredDeep } from 'vtils/types';
|
|
2
|
-
import * as vae from 'vtils/vae';
|
|
3
|
-
import type { XHandler } from './types';
|
|
4
|
-
export type { HandlerMethodMap, HandlerPath, HandlerPayloadMap, HandlerResultMap, } from '.x/routes';
|
|
5
|
-
export declare class Handler<TReqData extends any = void, TResData extends any = void, TReqMethod extends XHandler.Method = XHandler.Method, TReqDataRequired = RequiredDeep<TReqData>> {
|
|
6
|
-
readonly options: XHandler.Options<TReqData, TResData, TReqMethod>;
|
|
7
|
-
private requestDataSchema;
|
|
8
|
-
readonly requestDataSchemaVae: vae.VaeSchemaOf<TReqDataRequired>;
|
|
9
|
-
private requestHandle;
|
|
10
|
-
constructor(options: XHandler.Options<TReqData, TResData, TReqMethod>);
|
|
11
|
-
handle: XHandler.Handle<TReqData, TResData, TReqMethod>;
|
|
12
|
-
private handleHttp;
|
|
13
|
-
private handleWs;
|
|
14
|
-
private static hooks;
|
|
15
|
-
static addHook(hook: XHandler.Hook): void;
|
|
16
|
-
}
|
package/lib/core/handler.js
DELETED
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
import { HTTPError as GotHTTPError, ParseError as GotParseError, RequestError as GotRequestError, TimeoutError as GotTimeoutError } from 'got';
|
|
2
|
-
import LZString from 'lz-string';
|
|
3
|
-
import { asyncLimit, base64UrlEncode, DataPacker, rot13, toSingleLineString } from 'vtils';
|
|
4
|
-
import * as vae from 'vtils/vae';
|
|
5
|
-
import { getZhCN, yup } from 'vtils/validator';
|
|
6
|
-
import { HttpError } from "../core/http_error";
|
|
7
|
-
import { DisposeService } from "../services/dispose";
|
|
8
|
-
import { ctxStorage } from "./ctx_storage";
|
|
9
|
-
import { HttpRedirect } from "./http_redirect";
|
|
10
|
-
import { Server } from "./server";
|
|
11
|
-
yup.setLocale(getZhCN({
|
|
12
|
-
getLabel: params => params.label || params.path
|
|
13
|
-
}));
|
|
14
|
-
export class Handler {
|
|
15
|
-
constructor(options) {
|
|
16
|
-
this.options = options;
|
|
17
|
-
this.requestDataSchema = void 0;
|
|
18
|
-
this.requestDataSchemaVae = void 0;
|
|
19
|
-
this.requestHandle = void 0;
|
|
20
|
-
this.handle = async (data, ctx) => {
|
|
21
|
-
// 前置 hook
|
|
22
|
-
if (Handler.hooks.length) {
|
|
23
|
-
await Promise.all(Handler.hooks.map(hook => hook(this.options, data, ctx)));
|
|
24
|
-
}
|
|
25
|
-
let useAsyncContext = !!Server.options.useAsyncContext;
|
|
26
|
-
if (this.options.useAsyncContext != null) {
|
|
27
|
-
useAsyncContext = this.options.useAsyncContext;
|
|
28
|
-
}
|
|
29
|
-
ctx.setHeader('x-trace-id', ctx.traceId);
|
|
30
|
-
if (this.options.requestMethod === 'WS') {
|
|
31
|
-
return useAsyncContext ? ctxStorage.run(ctx, () => this.handleWs(data, ctx)) : this.handleWs(data, ctx);
|
|
32
|
-
}
|
|
33
|
-
return useAsyncContext ? ctxStorage.run(ctx, () => this.handleHttp(data, ctx)) : this.handleHttp(data, ctx);
|
|
34
|
-
};
|
|
35
|
-
this.handleHttp = async (data, ctx) => {
|
|
36
|
-
// 请求数据解析
|
|
37
|
-
if (this.options.parseRequestData) {
|
|
38
|
-
data = await this.options.parseRequestData(data, ctx);
|
|
39
|
-
}
|
|
40
|
-
// 请求数据验证
|
|
41
|
-
// vae
|
|
42
|
-
if (this.requestDataSchemaVae) {
|
|
43
|
-
const res = this.requestDataSchemaVae.parse(
|
|
44
|
-
// @ts-ignore
|
|
45
|
-
data, {
|
|
46
|
-
abortEarly: true,
|
|
47
|
-
preserveUnknownKeys: false
|
|
48
|
-
});
|
|
49
|
-
if (res.success) {
|
|
50
|
-
// @ts-ignore
|
|
51
|
-
data = res.data;
|
|
52
|
-
} else {
|
|
53
|
-
throw new HttpError.BadRequest(
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
res.issues[0].message);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
// yup
|
|
59
|
-
else if (this.requestDataSchema) {
|
|
60
|
-
try {
|
|
61
|
-
data = await this.requestDataSchema.validate(data, {
|
|
62
|
-
abortEarly: true,
|
|
63
|
-
stripUnknown: true
|
|
64
|
-
});
|
|
65
|
-
} catch (err) {
|
|
66
|
-
throw new HttpError.BadRequest(err.message);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
try {
|
|
70
|
-
let res = await this.requestHandle(data, ctx);
|
|
71
|
-
if (res instanceof HttpRedirect) {
|
|
72
|
-
ctx.redirect(res.url, res.permanently);
|
|
73
|
-
return {};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 设置响应的 Content-Type 头
|
|
77
|
-
if (this.options.responseContentType) {
|
|
78
|
-
ctx.setHeader('Content-Type', this.options.responseContentType);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// 打包返回数据
|
|
82
|
-
if (this.options.responseDataPack) {
|
|
83
|
-
res = DataPacker.packAsRawType(res);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 混淆返回数据
|
|
87
|
-
if (this.options.responseDataObfuscate) {
|
|
88
|
-
res = {
|
|
89
|
-
'_#': LZString.compress(JSON.stringify(res))
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// 加密返回数据
|
|
94
|
-
if (this.options.responseDataEncrypt && Server.options.responseEncryptAlgorithm) {
|
|
95
|
-
if (Server.options.responseEncryptAlgorithm === 'simple') {
|
|
96
|
-
res = {
|
|
97
|
-
_$: rot13(base64UrlEncode(`${Date.now()}${JSON.stringify(res)}`))
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return res == null ? {} : res;
|
|
102
|
-
} catch (err) {
|
|
103
|
-
// 业务错误
|
|
104
|
-
if (HttpError.isHttpError(err)) {
|
|
105
|
-
throw err;
|
|
106
|
-
}
|
|
107
|
-
// 网络错误
|
|
108
|
-
if (err instanceof GotHTTPError || err instanceof GotRequestError || err instanceof GotTimeoutError || err instanceof GotParseError) {
|
|
109
|
-
console.error('网络请求错误', {
|
|
110
|
-
error: `${err.code || ''}__${err.name || ''}__${err.message || ''}`,
|
|
111
|
-
response: err.response?.body && err.options.responseType !== 'buffer' ? toSingleLineString(String(err.response.body), 100) : '',
|
|
112
|
-
method: err.options.method,
|
|
113
|
-
url: err.options.url.toString(),
|
|
114
|
-
headers: JSON.stringify(err.options.headers || {}),
|
|
115
|
-
query: (err.options.search ?? err.options.searchParams?.toString()) || '',
|
|
116
|
-
body: (err.options.json ? JSON.stringify(err.options.json) : err.options.form ? new URLSearchParams(err.options.form).toString() : err.options.body?.toString()) || ''
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
// 服务器错误
|
|
120
|
-
else {
|
|
121
|
-
console.error('服务器错误', err);
|
|
122
|
-
}
|
|
123
|
-
throw new HttpError.InternalServerError('服务异常,请稍后再试');
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
this.handleWs = async (_data, ctx) => {
|
|
127
|
-
const dispose = new DisposeService();
|
|
128
|
-
ctx.ws.socket.on('message', async payload => {
|
|
129
|
-
try {
|
|
130
|
-
const _ = JSON.parse(payload.toString('utf8'));
|
|
131
|
-
const send = data => {
|
|
132
|
-
ctx.ws.socket.send(JSON.stringify({
|
|
133
|
-
id: _.id,
|
|
134
|
-
data: data
|
|
135
|
-
}));
|
|
136
|
-
};
|
|
137
|
-
const process = async (type, fn) => {
|
|
138
|
-
if (_.type === type) {
|
|
139
|
-
let sent = false;
|
|
140
|
-
try {
|
|
141
|
-
await fn({
|
|
142
|
-
data: _.data,
|
|
143
|
-
send: data => {
|
|
144
|
-
sent = true;
|
|
145
|
-
return send(data);
|
|
146
|
-
},
|
|
147
|
-
dispose: dispose
|
|
148
|
-
});
|
|
149
|
-
if (!sent) {
|
|
150
|
-
send();
|
|
151
|
-
}
|
|
152
|
-
} catch (err) {
|
|
153
|
-
const _err = HttpError.isHttpError(err) ? err : new HttpError.InternalServerError();
|
|
154
|
-
send({
|
|
155
|
-
statusCode: _err.statusCode,
|
|
156
|
-
error: _err.name,
|
|
157
|
-
message: _err.message
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
if (_.type === 'ping') {
|
|
163
|
-
send();
|
|
164
|
-
} else {
|
|
165
|
-
await this.options.handle(process, ctx);
|
|
166
|
-
}
|
|
167
|
-
} catch (err) {
|
|
168
|
-
const _err = HttpError.isHttpError(err) ? err : new HttpError.InternalServerError();
|
|
169
|
-
ctx.ws.socket.send(JSON.stringify({
|
|
170
|
-
statusCode: _err.statusCode,
|
|
171
|
-
error: _err.name,
|
|
172
|
-
message: _err.message
|
|
173
|
-
}));
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
ctx.ws.socket.on('close', () => {
|
|
177
|
-
dispose.dispose();
|
|
178
|
-
});
|
|
179
|
-
};
|
|
180
|
-
if (options.requestDataSchemaVae) {
|
|
181
|
-
const res = typeof options.requestDataSchemaVae === 'function' ? options.requestDataSchemaVae(new vae.VaeSchemaBuilder()) : options.requestDataSchemaVae;
|
|
182
|
-
this.requestDataSchemaVae = res instanceof vae.VaeSchema ? res : new vae.VaeObjectSchema(res);
|
|
183
|
-
} else if (options.requestDataSchema) {
|
|
184
|
-
this.requestDataSchema = options.requestDataSchema(yup);
|
|
185
|
-
}
|
|
186
|
-
if (options.concurrency != null) {
|
|
187
|
-
if (typeof options.concurrency === 'number') {
|
|
188
|
-
this.requestHandle = asyncLimit(async (data, ctx) => options.handle(data, ctx), {
|
|
189
|
-
concurrency: options.concurrency
|
|
190
|
-
});
|
|
191
|
-
} else {
|
|
192
|
-
const {
|
|
193
|
-
count,
|
|
194
|
-
groupBy
|
|
195
|
-
} = options.concurrency;
|
|
196
|
-
this.requestHandle = asyncLimit(async (data, ctx) => options.handle(data, ctx), {
|
|
197
|
-
concurrency: count,
|
|
198
|
-
groupBy: groupBy
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
} else {
|
|
202
|
-
this.requestHandle = options.handle;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
static addHook(hook) {
|
|
206
|
-
this.hooks.push(hook);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
Handler.hooks = [];
|
package/lib/core/http_error.d.ts
DELETED
package/lib/core/http_error.js
DELETED