@jayfong/x-server 2.119.0 → 3.1.1

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.
Files changed (288) hide show
  1. package/lib/_virtual/_rolldown/runtime.mjs +4 -0
  2. package/lib/cli/api_generator.d.mts +75 -0
  3. package/lib/cli/api_generator.mjs +334 -0
  4. package/lib/cli/build_util.d.mts +15 -0
  5. package/lib/cli/build_util.mjs +101 -0
  6. package/lib/cli/cli.d.mts +1 -0
  7. package/lib/cli/cli.mjs +345 -0
  8. package/lib/cli/css_register.d.mts +1 -0
  9. package/lib/cli/css_register.mjs +7 -0
  10. package/lib/cli/deploy_util.d.mts +23 -0
  11. package/lib/cli/deploy_util.mjs +70 -0
  12. package/lib/cli/dev_util.d.mts +9 -0
  13. package/lib/cli/dev_util.mjs +25 -0
  14. package/lib/cli/env_util.d.mts +45 -0
  15. package/lib/cli/env_util.mjs +207 -0
  16. package/lib/cli/template_util.d.mts +18 -0
  17. package/lib/cli/template_util.mjs +85 -0
  18. package/lib/cli/templates/crons.ts +1 -1
  19. package/lib/cli/templates/handlers.ts +1 -1
  20. package/lib/cli/templates/hooks.ts +1 -1
  21. package/lib/cli/templates/models.ts +7 -7
  22. package/lib/cli/templates/package.json +11 -1
  23. package/lib/cli/templates/routes.ts +2 -2
  24. package/lib/core/ctx_storage.d.mts +3 -0
  25. package/lib/core/ctx_storage.mjs +4 -0
  26. package/lib/core/define_bus.d.mts +37 -0
  27. package/lib/core/define_bus.mjs +12 -0
  28. package/lib/core/{define_cron.d.ts → define_cron.d.mts} +2 -2
  29. package/lib/core/define_cron.mjs +11 -0
  30. package/lib/core/{define_handler.d.ts → define_handler.d.mts} +19 -19
  31. package/lib/core/define_handler.mjs +44 -0
  32. package/lib/core/define_hook.d.mts +2 -0
  33. package/lib/core/define_hook.mjs +6 -0
  34. package/lib/core/define_server.d.mts +3 -0
  35. package/lib/core/define_server.mjs +8 -0
  36. package/lib/core/{define_task.d.ts → define_task.d.mts} +2 -2
  37. package/lib/core/define_task.mjs +106 -0
  38. package/lib/core/get_handler_url.d.mts +2 -0
  39. package/lib/core/get_handler_url.mjs +7 -0
  40. package/lib/core/handler.d.mts +16 -0
  41. package/lib/core/handler.mjs +132 -0
  42. package/lib/core/http_error.d.mts +2 -0
  43. package/lib/core/http_error.mjs +1 -0
  44. package/lib/core/http_header.d.mts +856 -0
  45. package/lib/core/http_header.mjs +860 -0
  46. package/lib/core/http_method.d.mts +3 -0
  47. package/lib/core/http_method.mjs +10 -0
  48. package/lib/core/http_redirect.d.mts +5 -0
  49. package/lib/core/http_redirect.mjs +10 -0
  50. package/lib/core/server.d.mts +20 -0
  51. package/lib/core/server.mjs +186 -0
  52. package/lib/core/types.d.mts +256 -0
  53. package/lib/exports.d.mts +46 -0
  54. package/lib/exports.mjs +46 -0
  55. package/lib/index.d.mts +48 -0
  56. package/lib/index.mjs +45 -0
  57. package/lib/plugins/base.d.mts +4 -0
  58. package/lib/plugins/compress.d.mts +12 -0
  59. package/lib/plugins/compress.mjs +15 -0
  60. package/lib/plugins/cookie.d.mts +12 -0
  61. package/lib/plugins/cookie.mjs +15 -0
  62. package/lib/plugins/cors.d.mts +22 -0
  63. package/lib/plugins/cors.mjs +34 -0
  64. package/lib/plugins/file_parser.d.mts +15 -0
  65. package/lib/plugins/file_parser.mjs +24 -0
  66. package/lib/plugins/form_body_parser.d.mts +12 -0
  67. package/lib/plugins/form_body_parser.mjs +15 -0
  68. package/lib/plugins/ws_parser.d.mts +12 -0
  69. package/lib/plugins/ws_parser.mjs +15 -0
  70. package/lib/plugins/xml_parser.d.mts +19 -0
  71. package/lib/plugins/xml_parser.mjs +40 -0
  72. package/lib/services/ai.d.mts +31 -0
  73. package/lib/services/ai.mjs +44 -0
  74. package/lib/services/base.d.mts +4 -0
  75. package/lib/services/base.mjs +1 -0
  76. package/lib/services/cache.d.mts +165 -0
  77. package/lib/services/cache.mjs +323 -0
  78. package/lib/services/captcha.d.mts +65 -0
  79. package/lib/services/captcha.mjs +61 -0
  80. package/lib/services/{crypto.js → crypto.d.mts} +19 -78
  81. package/lib/services/crypto.mjs +141 -0
  82. package/lib/services/db_value.d.mts +60 -0
  83. package/lib/services/db_value.mjs +64 -0
  84. package/lib/services/dingtalk.d.mts +39 -0
  85. package/lib/services/dingtalk.mjs +44 -0
  86. package/lib/services/dispose.d.mts +17 -0
  87. package/lib/services/dispose.mjs +18 -0
  88. package/lib/services/emoji.d.mts +10 -0
  89. package/lib/services/emoji.mjs +2025 -0
  90. package/lib/services/file_store.d.mts +49 -0
  91. package/lib/services/file_store.mjs +58 -0
  92. package/lib/services/html.d.mts +11 -0
  93. package/lib/services/html.mjs +12 -0
  94. package/lib/services/jwt.d.mts +33 -0
  95. package/lib/services/jwt.mjs +42 -0
  96. package/lib/services/log.d.mts +37 -0
  97. package/lib/services/log.mjs +99 -0
  98. package/lib/services/mail.d.mts +22 -0
  99. package/lib/services/mail.mjs +28 -0
  100. package/lib/services/pay.d.mts +123 -0
  101. package/lib/services/pay.mjs +157 -0
  102. package/lib/services/random.d.mts +27 -0
  103. package/lib/services/random.mjs +42 -0
  104. package/lib/services/rate_limit.d.mts +37 -0
  105. package/lib/services/rate_limit.mjs +40 -0
  106. package/lib/services/redis.d.mts +11 -0
  107. package/lib/services/redis.mjs +6 -0
  108. package/lib/services/sensitive_words.d.mts +29 -0
  109. package/lib/services/sensitive_words.mjs +37 -0
  110. package/lib/services/sms.d.mts +40 -0
  111. package/lib/services/sms.mjs +30238 -0
  112. package/lib/types/dto.d.mts +30 -0
  113. package/lib/types/dto.mjs +1 -0
  114. package/lib/x.d.mts +11 -0
  115. package/lib/x.mjs +21 -0
  116. package/lib/x_init.d.mts +1 -0
  117. package/lib/x_init.mjs +10 -0
  118. package/package.json +82 -32
  119. package/lib/_cjs/cli/api_generator.js +0 -458
  120. package/lib/_cjs/cli/build_util.js +0 -243
  121. package/lib/_cjs/cli/cli.js +0 -378
  122. package/lib/_cjs/cli/css_register.js +0 -7
  123. package/lib/_cjs/cli/deploy_util.js +0 -76
  124. package/lib/_cjs/cli/dev_util.js +0 -26
  125. package/lib/_cjs/cli/env_util.js +0 -252
  126. package/lib/_cjs/cli/esbuild_register.js +0 -7
  127. package/lib/_cjs/cli/template_util.js +0 -101
  128. package/lib/_cjs/cli/templates/crons.ts +0 -2
  129. package/lib/_cjs/cli/templates/handlers.ts +0 -3
  130. package/lib/_cjs/cli/templates/hooks.ts +0 -2
  131. package/lib/_cjs/cli/templates/models.ts +0 -123
  132. package/lib/_cjs/cli/templates/package.json +0 -3
  133. package/lib/_cjs/cli/templates/routes.ts +0 -68
  134. package/lib/_cjs/core/ctx_storage.js +0 -6
  135. package/lib/_cjs/core/define_bus.js +0 -31
  136. package/lib/_cjs/core/define_cron.js +0 -13
  137. package/lib/_cjs/core/define_handler.js +0 -56
  138. package/lib/_cjs/core/define_hook.js +0 -8
  139. package/lib/_cjs/core/define_server.js +0 -10
  140. package/lib/_cjs/core/define_task.js +0 -113
  141. package/lib/_cjs/core/get_handler_url.js +0 -11
  142. package/lib/_cjs/core/handler.js +0 -216
  143. package/lib/_cjs/core/http_error.js +0 -6
  144. package/lib/_cjs/core/http_header.js +0 -142
  145. package/lib/_cjs/core/http_method.js +0 -12
  146. package/lib/_cjs/core/http_redirect.js +0 -11
  147. package/lib/_cjs/core/server.js +0 -219
  148. package/lib/_cjs/core/types.js +0 -8
  149. package/lib/_cjs/exports.js +0 -273
  150. package/lib/_cjs/index.js +0 -15
  151. package/lib/_cjs/plugins/base.js +0 -3
  152. package/lib/_cjs/plugins/compress.js +0 -18
  153. package/lib/_cjs/plugins/cookie.js +0 -18
  154. package/lib/_cjs/plugins/cors.js +0 -38
  155. package/lib/_cjs/plugins/file_parser.js +0 -28
  156. package/lib/_cjs/plugins/form_body_parser.js +0 -18
  157. package/lib/_cjs/plugins/ws_parser.js +0 -18
  158. package/lib/_cjs/plugins/xml_parser.js +0 -48
  159. package/lib/_cjs/services/ai.js +0 -48
  160. package/lib/_cjs/services/base.js +0 -3
  161. package/lib/_cjs/services/cache.js +0 -400
  162. package/lib/_cjs/services/captcha.js +0 -78
  163. package/lib/_cjs/services/crypto.js +0 -175
  164. package/lib/_cjs/services/db_value.js +0 -77
  165. package/lib/_cjs/services/dingtalk.js +0 -51
  166. package/lib/_cjs/services/dispose.js +0 -23
  167. package/lib/_cjs/services/emoji.js +0 -2034
  168. package/lib/_cjs/services/file_store.js +0 -67
  169. package/lib/_cjs/services/html.js +0 -17
  170. package/lib/_cjs/services/jwt.js +0 -65
  171. package/lib/_cjs/services/log.js +0 -108
  172. package/lib/_cjs/services/mail.js +0 -31
  173. package/lib/_cjs/services/pay.js +0 -191
  174. package/lib/_cjs/services/random.js +0 -51
  175. package/lib/_cjs/services/rate_limit.js +0 -54
  176. package/lib/_cjs/services/redis.js +0 -12
  177. package/lib/_cjs/services/sensitive_words.js +0 -61
  178. package/lib/_cjs/services/sms.js +0 -84
  179. package/lib/_cjs/types/dto.js +0 -3
  180. package/lib/_cjs/x.js +0 -35
  181. package/lib/cli/api_generator.d.ts +0 -74
  182. package/lib/cli/api_generator.js +0 -452
  183. package/lib/cli/build_util.d.ts +0 -17
  184. package/lib/cli/build_util.js +0 -236
  185. package/lib/cli/cli.d.ts +0 -2
  186. package/lib/cli/cli.js +0 -374
  187. package/lib/cli/css_register.js +0 -5
  188. package/lib/cli/deploy_util.d.ts +0 -23
  189. package/lib/cli/deploy_util.js +0 -70
  190. package/lib/cli/dev_util.d.ts +0 -8
  191. package/lib/cli/dev_util.js +0 -20
  192. package/lib/cli/env_util.d.ts +0 -45
  193. package/lib/cli/env_util.js +0 -246
  194. package/lib/cli/esbuild_register.js +0 -5
  195. package/lib/cli/template_util.d.ts +0 -18
  196. package/lib/cli/template_util.js +0 -95
  197. package/lib/core/ctx_storage.d.ts +0 -3
  198. package/lib/core/ctx_storage.js +0 -2
  199. package/lib/core/define_bus.d.ts +0 -37
  200. package/lib/core/define_bus.js +0 -27
  201. package/lib/core/define_cron.js +0 -9
  202. package/lib/core/define_handler.js +0 -48
  203. package/lib/core/define_hook.d.ts +0 -2
  204. package/lib/core/define_hook.js +0 -4
  205. package/lib/core/define_server.d.ts +0 -3
  206. package/lib/core/define_server.js +0 -6
  207. package/lib/core/define_task.js +0 -107
  208. package/lib/core/get_handler_url.d.ts +0 -2
  209. package/lib/core/get_handler_url.js +0 -7
  210. package/lib/core/handler.d.ts +0 -16
  211. package/lib/core/handler.js +0 -209
  212. package/lib/core/http_error.d.ts +0 -2
  213. package/lib/core/http_error.js +0 -2
  214. package/lib/core/http_header.d.ts +0 -856
  215. package/lib/core/http_header.js +0 -139
  216. package/lib/core/http_method.d.ts +0 -3
  217. package/lib/core/http_method.js +0 -8
  218. package/lib/core/http_redirect.d.ts +0 -5
  219. package/lib/core/http_redirect.js +0 -6
  220. package/lib/core/server.d.ts +0 -20
  221. package/lib/core/server.js +0 -213
  222. package/lib/core/types.d.ts +0 -265
  223. package/lib/core/types.js +0 -4
  224. package/lib/exports.d.ts +0 -45
  225. package/lib/exports.js +0 -47
  226. package/lib/index.d.ts +0 -2
  227. package/lib/index.js +0 -3
  228. package/lib/plugins/base.d.ts +0 -4
  229. package/lib/plugins/base.js +0 -1
  230. package/lib/plugins/compress.d.ts +0 -13
  231. package/lib/plugins/compress.js +0 -12
  232. package/lib/plugins/cookie.d.ts +0 -13
  233. package/lib/plugins/cookie.js +0 -12
  234. package/lib/plugins/cors.d.ts +0 -22
  235. package/lib/plugins/cors.js +0 -32
  236. package/lib/plugins/file_parser.d.ts +0 -16
  237. package/lib/plugins/file_parser.js +0 -22
  238. package/lib/plugins/form_body_parser.d.ts +0 -13
  239. package/lib/plugins/form_body_parser.js +0 -12
  240. package/lib/plugins/ws_parser.d.ts +0 -13
  241. package/lib/plugins/ws_parser.js +0 -12
  242. package/lib/plugins/xml_parser.d.ts +0 -19
  243. package/lib/plugins/xml_parser.js +0 -43
  244. package/lib/services/ai.d.ts +0 -31
  245. package/lib/services/ai.js +0 -43
  246. package/lib/services/base.d.ts +0 -4
  247. package/lib/services/base.js +0 -1
  248. package/lib/services/cache.d.ts +0 -173
  249. package/lib/services/cache.js +0 -394
  250. package/lib/services/captcha.d.ts +0 -65
  251. package/lib/services/captcha.js +0 -72
  252. package/lib/services/crypto.d.ts +0 -110
  253. package/lib/services/db_value.d.ts +0 -60
  254. package/lib/services/db_value.js +0 -72
  255. package/lib/services/dingtalk.d.ts +0 -39
  256. package/lib/services/dingtalk.js +0 -45
  257. package/lib/services/dispose.d.ts +0 -17
  258. package/lib/services/dispose.js +0 -17
  259. package/lib/services/emoji.d.ts +0 -10
  260. package/lib/services/emoji.js +0 -2029
  261. package/lib/services/file_store.d.ts +0 -49
  262. package/lib/services/file_store.js +0 -61
  263. package/lib/services/html.d.ts +0 -11
  264. package/lib/services/html.js +0 -12
  265. package/lib/services/jwt.d.ts +0 -34
  266. package/lib/services/jwt.js +0 -59
  267. package/lib/services/log.d.ts +0 -37
  268. package/lib/services/log.js +0 -102
  269. package/lib/services/mail.d.ts +0 -23
  270. package/lib/services/mail.js +0 -25
  271. package/lib/services/pay.d.ts +0 -123
  272. package/lib/services/pay.js +0 -185
  273. package/lib/services/random.d.ts +0 -27
  274. package/lib/services/random.js +0 -45
  275. package/lib/services/rate_limit.d.ts +0 -37
  276. package/lib/services/rate_limit.js +0 -49
  277. package/lib/services/redis.d.ts +0 -12
  278. package/lib/services/redis.js +0 -7
  279. package/lib/services/sensitive_words.d.ts +0 -29
  280. package/lib/services/sensitive_words.js +0 -56
  281. package/lib/services/sms.d.ts +0 -40
  282. package/lib/services/sms.js +0 -78
  283. package/lib/types/dto.d.ts +0 -86
  284. package/lib/types/dto.js +0 -1
  285. package/lib/x.d.ts +0 -17
  286. package/lib/x.js +0 -30
  287. /package/lib/{cli/css_register.d.ts → core/types.mjs} +0 -0
  288. /package/lib/{cli/esbuild_register.d.ts → plugins/base.mjs} +0 -0
@@ -0,0 +1,106 @@
1
+ import { x } from "../x.mjs";
2
+ import { ms, omitStrict } from "vtils";
3
+ import assert from "node:assert";
4
+ import Queue$1 from "bull";
5
+ // src/core/define_task.ts
6
+ function defineTask(options) {
7
+ const queue = new Queue$1(options.name, {
8
+ redis: {
9
+ ...omitStrict(x.redis.options, [
10
+ "Connector",
11
+ "retryStrategy",
12
+ "natMap"
13
+ ]),
14
+ maxRetriesPerRequest: null,
15
+ enableReadyCheck: false
16
+ },
17
+ prefix: `${x.appId}_task`,
18
+ defaultJobOptions: {
19
+ removeOnComplete: true,
20
+ removeOnFail: true
21
+ }
22
+ });
23
+ queue.process(options.concurrency || 1, async (job) => {
24
+ return options.handle(job.data);
25
+ });
26
+ return queue;
27
+ }
28
+ function defineSliceTask(options) {
29
+ const task = defineTask({
30
+ name: options.name,
31
+ concurrency: 1,
32
+ handle: async (data) => {
33
+ const list = (data.count ? await x.redis.multi([[
34
+ "lrange",
35
+ data.redisKey,
36
+ `-${data.count}`,
37
+ "-1"
38
+ ], [
39
+ "ltrim",
40
+ data.redisKey,
41
+ "0",
42
+ `-${data.count + 1}`
43
+ ]]).exec() : await x.redis.multi([[
44
+ "lrange",
45
+ data.redisKey,
46
+ "0",
47
+ "-1"
48
+ ], ["del", data.redisKey]]).exec())[0][1].reverse().map((item) => JSON.parse(item));
49
+ if (list.length) return options.handle(list, data.key);
50
+ }
51
+ });
52
+ const addAction = async (data, addOptions) => {
53
+ const redisKeyPrefix = `${x.appId}_batch_task_${options.name}`;
54
+ const key = addOptions?.key || "";
55
+ const duration = addOptions?.duration != null ? ms(addOptions.duration) : typeof options.duration === "function" ? ms(options.duration(key)) : options.duration && ms(options.duration);
56
+ const threshold = addOptions?.threshold || typeof options.threshold && (typeof options.threshold === "function" ? options.threshold(key) : options.threshold);
57
+ const thresholdTimeout = addOptions?.thresholdTimeout != null ? ms(addOptions.thresholdTimeout) : typeof options.thresholdTimeout === "function" ? ms(options.thresholdTimeout(key)) : options.thresholdTimeout && ms(options.thresholdTimeout);
58
+ assert(duration != null || threshold != null, "参数 threshold 和 duration 必须至少设置 1 个");
59
+ const redisKey = !key ? redisKeyPrefix : `${redisKeyPrefix}_${key}`;
60
+ const res = await x.redis.multi([["llen", redisKey], [
61
+ "lpush",
62
+ redisKey,
63
+ JSON.stringify(data)
64
+ ]]).exec();
65
+ const count = parseInt(res[0][1], 10) + 1;
66
+ if (duration != null) {
67
+ if (count === 1) await task.add({
68
+ key,
69
+ redisKey
70
+ }, { delay: duration });
71
+ } else if (threshold != null) {
72
+ const delayTaskId = redisKey;
73
+ const prevJob = await task.getJob(delayTaskId);
74
+ if (count === threshold) {
75
+ await prevJob?.remove();
76
+ await task.add({
77
+ key,
78
+ redisKey
79
+ });
80
+ } else if (thresholdTimeout) {
81
+ if (!prevJob) await task.add({
82
+ key,
83
+ redisKey
84
+ }, {
85
+ jobId: delayTaskId,
86
+ delay: thresholdTimeout
87
+ });
88
+ }
89
+ }
90
+ };
91
+ const addTask = defineTask({
92
+ name: `${options.name}_add`,
93
+ concurrency: 1,
94
+ handle: async (payload) => {
95
+ await addAction(payload.data, payload.options);
96
+ }
97
+ });
98
+ return { add: async (data, addOptions) => {
99
+ const delay = addOptions.delay && ms(addOptions.delay) || 0;
100
+ await addTask.add({
101
+ data,
102
+ options: addOptions
103
+ }, { delay });
104
+ } };
105
+ }
106
+ export { defineSliceTask, defineTask };
@@ -0,0 +1,2 @@
1
+ import { HandlerPath, HandlerPayloadMap } from "@a/x/routes";
2
+ export declare function getHandlerUrl<T extends HandlerPath>(path: T, query?: Partial<HandlerPayloadMap[T]>): string;
@@ -0,0 +1,7 @@
1
+ import { x } from "../x.mjs";
2
+ import { createUrlQueryString } from "vtils";
3
+ // src/core/get_handler_url.ts
4
+ function getHandlerUrl(path, query) {
5
+ return `${x.env.APP_URL.replace(/\/+$/, "")}${path}${query ? `?${createUrlQueryString(query)}` : ""}`;
6
+ }
7
+ export { getHandlerUrl };
@@ -0,0 +1,16 @@
1
+ import { XHandler } from "./types.mjs";
2
+ import * as vae from "vtils/vae";
3
+ import { RequiredDeep } from "vtils/types";
4
+ import { HandlerMethodMap, HandlerPath, HandlerPayloadMap, HandlerResultMap } from "@a/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
+ readonly requestDataSchemaVae: vae.VaeSchemaOf<TReqDataRequired>;
8
+ private requestHandle;
9
+ constructor(options: XHandler.Options<TReqData, TResData, TReqMethod>);
10
+ handle: XHandler.Handle<TReqData, TResData, TReqMethod>;
11
+ private handleHttp;
12
+ private handleWs;
13
+ private static hooks;
14
+ static addHook(hook: XHandler.Hook): void;
15
+ }
16
+ export type { HandlerMethodMap, HandlerPath, HandlerPayloadMap, HandlerResultMap };
@@ -0,0 +1,132 @@
1
+ import { HttpError } from "./http_error.mjs";
2
+ import { DisposeService } from "../services/dispose.mjs";
3
+ import { ctxStorage } from "./ctx_storage.mjs";
4
+ import { HttpRedirect } from "./http_redirect.mjs";
5
+ import { Server } from "./server.mjs";
6
+ import { xr } from "@jayfong/x-request";
7
+ import LZString from "lz-string";
8
+ import { DataPacker, asyncLimit, base64UrlEncode, rot13, toSingleLineString } from "vtils";
9
+ import * as vae from "vtils/vae";
10
+ // src/core/handler.ts
11
+ var Handler = class Handler {
12
+ options;
13
+ requestDataSchemaVae;
14
+ requestHandle;
15
+ constructor(options) {
16
+ this.options = options;
17
+ if (options.requestDataSchemaVae) {
18
+ const res = typeof options.requestDataSchemaVae === "function" ? options.requestDataSchemaVae(new vae.VaeSchemaBuilder()) : options.requestDataSchemaVae;
19
+ this.requestDataSchemaVae = res instanceof vae.VaeSchema ? res : new vae.VaeObjectSchema(res);
20
+ }
21
+ if (options.concurrency != null) {
22
+ if (typeof options.concurrency === "number") this.requestHandle = asyncLimit(async (data, ctx) => options.handle(data, ctx), { concurrency: options.concurrency });
23
+ else {
24
+ const { count, groupBy } = options.concurrency;
25
+ this.requestHandle = asyncLimit(async (data, ctx) => options.handle(data, ctx), {
26
+ concurrency: count,
27
+ groupBy
28
+ });
29
+ }
30
+ } else this.requestHandle = options.handle;
31
+ }
32
+ handle = async (data, ctx) => {
33
+ if (Handler.hooks.length) await Promise.all(Handler.hooks.map((hook) => hook(this.options, data, ctx)));
34
+ let useAsyncContext = !!Server.options.useAsyncContext;
35
+ if (this.options.useAsyncContext != null) useAsyncContext = this.options.useAsyncContext;
36
+ ctx.setHeader("x-trace-id", ctx.traceId);
37
+ if (this.options.requestMethod === "WS") return useAsyncContext ? ctxStorage.run(ctx, () => this.handleWs(data, ctx)) : this.handleWs(data, ctx);
38
+ return useAsyncContext ? ctxStorage.run(ctx, () => this.handleHttp(data, ctx)) : this.handleHttp(data, ctx);
39
+ };
40
+ handleHttp = async (data, ctx) => {
41
+ if (this.options.parseRequestData) data = await this.options.parseRequestData(data, ctx);
42
+ if (this.requestDataSchemaVae) {
43
+ const res = this.requestDataSchemaVae.parse(data, {
44
+ abortEarly: true,
45
+ preserveUnknownKeys: false
46
+ });
47
+ if (res.success) data = res.data;
48
+ else throw new HttpError.BadRequest(res.issues[0].message);
49
+ }
50
+ try {
51
+ let res = await this.requestHandle(data, ctx);
52
+ if (res instanceof HttpRedirect) {
53
+ ctx.redirect(res.url, res.permanently);
54
+ return {};
55
+ }
56
+ if (this.options.responseContentType) ctx.setHeader("Content-Type", this.options.responseContentType);
57
+ if (this.options.responseDataPack) res = DataPacker.packAsRawType(res);
58
+ if (this.options.responseDataObfuscate) res = { "_#": LZString.compress(JSON.stringify(res)) };
59
+ if (this.options.responseDataEncrypt && Server.options.responseEncryptAlgorithm) {
60
+ if (Server.options.responseEncryptAlgorithm === "simple") res = { _$: rot13(base64UrlEncode(`${Date.now()}${JSON.stringify(res)}`)) };
61
+ }
62
+ return res == null ? {} : res;
63
+ } catch (err) {
64
+ if (HttpError.isHttpError(err)) throw err;
65
+ if (xr.isRequestError(err)) console.error("网络请求错误", {
66
+ error: `${err.code || ""}__${err.name || ""}__${err.message || ""}`,
67
+ response: err.response?.body && err.options.responseType !== "buffer" ? toSingleLineString(String(err.response.body), 100) : "",
68
+ method: err.options.method,
69
+ url: err.options.url?.toString() || "",
70
+ headers: JSON.stringify(err.options.headers || {}),
71
+ query: err.options.searchParams?.toString() || "",
72
+ body: (err.options.json ? JSON.stringify(err.options.json) : err.options.form ? new URLSearchParams(err.options.form).toString() : err.options.body?.toString()) || ""
73
+ });
74
+ else console.error("服务器错误", err);
75
+ throw new HttpError.InternalServerError("服务异常,请稍后再试");
76
+ }
77
+ };
78
+ handleWs = async (_data, ctx) => {
79
+ const dispose = new DisposeService();
80
+ ctx.ws.on("message", async (payload) => {
81
+ try {
82
+ const _ = JSON.parse(payload.toString("utf8"));
83
+ const send = (data) => {
84
+ ctx.ws.send(JSON.stringify({
85
+ id: _.id,
86
+ data
87
+ }));
88
+ };
89
+ const process = async (type, fn) => {
90
+ if (_.type === type) {
91
+ let sent = false;
92
+ try {
93
+ await fn({
94
+ data: _.data,
95
+ send: (data) => {
96
+ sent = true;
97
+ return send(data);
98
+ },
99
+ dispose
100
+ });
101
+ if (!sent) send();
102
+ } catch (err) {
103
+ const _err = HttpError.isHttpError(err) ? err : new HttpError.InternalServerError();
104
+ send({
105
+ statusCode: _err.statusCode,
106
+ error: _err.name,
107
+ message: _err.message
108
+ });
109
+ }
110
+ }
111
+ };
112
+ if (_.type === "ping") send();
113
+ else await this.options.handle(process, ctx);
114
+ } catch (err) {
115
+ const _err = HttpError.isHttpError(err) ? err : new HttpError.InternalServerError();
116
+ ctx.ws.send(JSON.stringify({
117
+ statusCode: _err.statusCode,
118
+ error: _err.name,
119
+ message: _err.message
120
+ }));
121
+ }
122
+ });
123
+ ctx.ws.on("close", () => {
124
+ dispose.dispose();
125
+ });
126
+ };
127
+ static hooks = [];
128
+ static addHook(hook) {
129
+ this.hooks.push(hook);
130
+ }
131
+ };
132
+ export { Handler };
@@ -0,0 +1,2 @@
1
+ import HttpError from "http-errors";
2
+ export { HttpError };
@@ -0,0 +1 @@
1
+ export { default as HttpError } from "http-errors";