@jayfong/x-server 2.119.0 → 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.
Files changed (290) hide show
  1. package/lib/_virtual/_rolldown/runtime.mjs +4 -0
  2. package/lib/cli/api_generator.d.mts +74 -0
  3. package/lib/cli/api_generator.mjs +310 -0
  4. package/lib/cli/build_util.d.mts +17 -0
  5. package/lib/cli/build_util.mjs +156 -0
  6. package/lib/cli/cli.d.mts +1 -0
  7. package/lib/cli/cli.mjs +355 -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 +7 -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/esbuild_register.d.mts +1 -0
  17. package/lib/cli/esbuild_register.mjs +7 -0
  18. package/lib/cli/template_util.d.mts +18 -0
  19. package/lib/cli/template_util.mjs +85 -0
  20. package/lib/cli/templates/crons.ts +1 -1
  21. package/lib/cli/templates/handlers.ts +1 -1
  22. package/lib/cli/templates/hooks.ts +1 -1
  23. package/lib/cli/templates/models.ts +7 -7
  24. package/lib/cli/templates/package.json +11 -1
  25. package/lib/cli/templates/routes.ts +2 -1
  26. package/lib/core/ctx_storage.d.mts +3 -0
  27. package/lib/core/ctx_storage.mjs +4 -0
  28. package/lib/core/define_bus.d.mts +37 -0
  29. package/lib/core/define_bus.mjs +12 -0
  30. package/lib/core/{define_cron.d.ts → define_cron.d.mts} +2 -2
  31. package/lib/core/define_cron.mjs +11 -0
  32. package/lib/core/{define_handler.d.ts → define_handler.d.mts} +19 -19
  33. package/lib/core/define_handler.mjs +44 -0
  34. package/lib/core/define_hook.d.mts +2 -0
  35. package/lib/core/define_hook.mjs +6 -0
  36. package/lib/core/define_server.d.mts +3 -0
  37. package/lib/core/define_server.mjs +8 -0
  38. package/lib/core/{define_task.d.ts → define_task.d.mts} +2 -2
  39. package/lib/core/define_task.mjs +102 -0
  40. package/lib/core/get_handler_url.d.mts +2 -0
  41. package/lib/core/get_handler_url.mjs +7 -0
  42. package/lib/core/handler.d.mts +16 -0
  43. package/lib/core/handler.mjs +132 -0
  44. package/lib/core/http_error.d.mts +2 -0
  45. package/lib/core/http_error.mjs +1 -0
  46. package/lib/core/http_header.d.mts +856 -0
  47. package/lib/core/http_header.mjs +860 -0
  48. package/lib/core/http_method.d.mts +3 -0
  49. package/lib/core/http_method.mjs +10 -0
  50. package/lib/core/http_redirect.d.mts +5 -0
  51. package/lib/core/http_redirect.mjs +10 -0
  52. package/lib/core/server.d.mts +20 -0
  53. package/lib/core/server.mjs +186 -0
  54. package/lib/core/types.d.mts +256 -0
  55. package/lib/exports.d.mts +46 -0
  56. package/lib/exports.mjs +46 -0
  57. package/lib/index.d.mts +48 -0
  58. package/lib/index.mjs +45 -0
  59. package/lib/plugins/base.d.mts +4 -0
  60. package/lib/plugins/compress.d.mts +12 -0
  61. package/lib/plugins/compress.mjs +15 -0
  62. package/lib/plugins/cookie.d.mts +12 -0
  63. package/lib/plugins/cookie.mjs +15 -0
  64. package/lib/plugins/cors.d.mts +22 -0
  65. package/lib/plugins/cors.mjs +34 -0
  66. package/lib/plugins/file_parser.d.mts +15 -0
  67. package/lib/plugins/file_parser.mjs +24 -0
  68. package/lib/plugins/form_body_parser.d.mts +12 -0
  69. package/lib/plugins/form_body_parser.mjs +15 -0
  70. package/lib/plugins/ws_parser.d.mts +12 -0
  71. package/lib/plugins/ws_parser.mjs +15 -0
  72. package/lib/plugins/xml_parser.d.mts +19 -0
  73. package/lib/plugins/xml_parser.mjs +40 -0
  74. package/lib/services/ai.d.mts +31 -0
  75. package/lib/services/ai.mjs +44 -0
  76. package/lib/services/base.d.mts +4 -0
  77. package/lib/services/base.mjs +1 -0
  78. package/lib/services/cache.d.mts +165 -0
  79. package/lib/services/cache.mjs +323 -0
  80. package/lib/services/captcha.d.mts +65 -0
  81. package/lib/services/captcha.mjs +61 -0
  82. package/lib/services/{crypto.js → crypto.d.mts} +19 -78
  83. package/lib/services/crypto.mjs +141 -0
  84. package/lib/services/db_value.d.mts +60 -0
  85. package/lib/services/db_value.mjs +64 -0
  86. package/lib/services/dingtalk.d.mts +39 -0
  87. package/lib/services/dingtalk.mjs +44 -0
  88. package/lib/services/dispose.d.mts +17 -0
  89. package/lib/services/dispose.mjs +18 -0
  90. package/lib/services/emoji.d.mts +10 -0
  91. package/lib/services/emoji.mjs +2025 -0
  92. package/lib/services/file_store.d.mts +49 -0
  93. package/lib/services/file_store.mjs +58 -0
  94. package/lib/services/html.d.mts +11 -0
  95. package/lib/services/html.mjs +12 -0
  96. package/lib/services/jwt.d.mts +33 -0
  97. package/lib/services/jwt.mjs +42 -0
  98. package/lib/services/log.d.mts +37 -0
  99. package/lib/services/log.mjs +99 -0
  100. package/lib/services/mail.d.mts +22 -0
  101. package/lib/services/mail.mjs +28 -0
  102. package/lib/services/pay.d.mts +123 -0
  103. package/lib/services/pay.mjs +157 -0
  104. package/lib/services/random.d.mts +27 -0
  105. package/lib/services/random.mjs +42 -0
  106. package/lib/services/rate_limit.d.mts +37 -0
  107. package/lib/services/rate_limit.mjs +40 -0
  108. package/lib/services/redis.d.mts +11 -0
  109. package/lib/services/redis.mjs +6 -0
  110. package/lib/services/sensitive_words.d.mts +29 -0
  111. package/lib/services/sensitive_words.mjs +37 -0
  112. package/lib/services/sms.d.mts +40 -0
  113. package/lib/services/sms.mjs +30238 -0
  114. package/lib/types/dto.d.mts +30 -0
  115. package/lib/types/dto.mjs +1 -0
  116. package/lib/x.d.mts +11 -0
  117. package/lib/x.mjs +21 -0
  118. package/lib/x_init.d.mts +1 -0
  119. package/lib/x_init.mjs +10 -0
  120. package/package.json +71 -16
  121. package/lib/_cjs/cli/api_generator.js +0 -458
  122. package/lib/_cjs/cli/build_util.js +0 -243
  123. package/lib/_cjs/cli/cli.js +0 -378
  124. package/lib/_cjs/cli/css_register.js +0 -7
  125. package/lib/_cjs/cli/deploy_util.js +0 -76
  126. package/lib/_cjs/cli/dev_util.js +0 -26
  127. package/lib/_cjs/cli/env_util.js +0 -252
  128. package/lib/_cjs/cli/esbuild_register.js +0 -7
  129. package/lib/_cjs/cli/template_util.js +0 -101
  130. package/lib/_cjs/cli/templates/crons.ts +0 -2
  131. package/lib/_cjs/cli/templates/handlers.ts +0 -3
  132. package/lib/_cjs/cli/templates/hooks.ts +0 -2
  133. package/lib/_cjs/cli/templates/models.ts +0 -123
  134. package/lib/_cjs/cli/templates/package.json +0 -3
  135. package/lib/_cjs/cli/templates/routes.ts +0 -68
  136. package/lib/_cjs/core/ctx_storage.js +0 -6
  137. package/lib/_cjs/core/define_bus.js +0 -31
  138. package/lib/_cjs/core/define_cron.js +0 -13
  139. package/lib/_cjs/core/define_handler.js +0 -56
  140. package/lib/_cjs/core/define_hook.js +0 -8
  141. package/lib/_cjs/core/define_server.js +0 -10
  142. package/lib/_cjs/core/define_task.js +0 -113
  143. package/lib/_cjs/core/get_handler_url.js +0 -11
  144. package/lib/_cjs/core/handler.js +0 -216
  145. package/lib/_cjs/core/http_error.js +0 -6
  146. package/lib/_cjs/core/http_header.js +0 -142
  147. package/lib/_cjs/core/http_method.js +0 -12
  148. package/lib/_cjs/core/http_redirect.js +0 -11
  149. package/lib/_cjs/core/server.js +0 -219
  150. package/lib/_cjs/core/types.js +0 -8
  151. package/lib/_cjs/exports.js +0 -273
  152. package/lib/_cjs/index.js +0 -15
  153. package/lib/_cjs/plugins/base.js +0 -3
  154. package/lib/_cjs/plugins/compress.js +0 -18
  155. package/lib/_cjs/plugins/cookie.js +0 -18
  156. package/lib/_cjs/plugins/cors.js +0 -38
  157. package/lib/_cjs/plugins/file_parser.js +0 -28
  158. package/lib/_cjs/plugins/form_body_parser.js +0 -18
  159. package/lib/_cjs/plugins/ws_parser.js +0 -18
  160. package/lib/_cjs/plugins/xml_parser.js +0 -48
  161. package/lib/_cjs/services/ai.js +0 -48
  162. package/lib/_cjs/services/base.js +0 -3
  163. package/lib/_cjs/services/cache.js +0 -400
  164. package/lib/_cjs/services/captcha.js +0 -78
  165. package/lib/_cjs/services/crypto.js +0 -175
  166. package/lib/_cjs/services/db_value.js +0 -77
  167. package/lib/_cjs/services/dingtalk.js +0 -51
  168. package/lib/_cjs/services/dispose.js +0 -23
  169. package/lib/_cjs/services/emoji.js +0 -2034
  170. package/lib/_cjs/services/file_store.js +0 -67
  171. package/lib/_cjs/services/html.js +0 -17
  172. package/lib/_cjs/services/jwt.js +0 -65
  173. package/lib/_cjs/services/log.js +0 -108
  174. package/lib/_cjs/services/mail.js +0 -31
  175. package/lib/_cjs/services/pay.js +0 -191
  176. package/lib/_cjs/services/random.js +0 -51
  177. package/lib/_cjs/services/rate_limit.js +0 -54
  178. package/lib/_cjs/services/redis.js +0 -12
  179. package/lib/_cjs/services/sensitive_words.js +0 -61
  180. package/lib/_cjs/services/sms.js +0 -84
  181. package/lib/_cjs/types/dto.js +0 -3
  182. package/lib/_cjs/x.js +0 -35
  183. package/lib/cli/api_generator.d.ts +0 -74
  184. package/lib/cli/api_generator.js +0 -452
  185. package/lib/cli/build_util.d.ts +0 -17
  186. package/lib/cli/build_util.js +0 -236
  187. package/lib/cli/cli.d.ts +0 -2
  188. package/lib/cli/cli.js +0 -374
  189. package/lib/cli/css_register.js +0 -5
  190. package/lib/cli/deploy_util.d.ts +0 -23
  191. package/lib/cli/deploy_util.js +0 -70
  192. package/lib/cli/dev_util.d.ts +0 -8
  193. package/lib/cli/dev_util.js +0 -20
  194. package/lib/cli/env_util.d.ts +0 -45
  195. package/lib/cli/env_util.js +0 -246
  196. package/lib/cli/esbuild_register.js +0 -5
  197. package/lib/cli/template_util.d.ts +0 -18
  198. package/lib/cli/template_util.js +0 -95
  199. package/lib/core/ctx_storage.d.ts +0 -3
  200. package/lib/core/ctx_storage.js +0 -2
  201. package/lib/core/define_bus.d.ts +0 -37
  202. package/lib/core/define_bus.js +0 -27
  203. package/lib/core/define_cron.js +0 -9
  204. package/lib/core/define_handler.js +0 -48
  205. package/lib/core/define_hook.d.ts +0 -2
  206. package/lib/core/define_hook.js +0 -4
  207. package/lib/core/define_server.d.ts +0 -3
  208. package/lib/core/define_server.js +0 -6
  209. package/lib/core/define_task.js +0 -107
  210. package/lib/core/get_handler_url.d.ts +0 -2
  211. package/lib/core/get_handler_url.js +0 -7
  212. package/lib/core/handler.d.ts +0 -16
  213. package/lib/core/handler.js +0 -209
  214. package/lib/core/http_error.d.ts +0 -2
  215. package/lib/core/http_error.js +0 -2
  216. package/lib/core/http_header.d.ts +0 -856
  217. package/lib/core/http_header.js +0 -139
  218. package/lib/core/http_method.d.ts +0 -3
  219. package/lib/core/http_method.js +0 -8
  220. package/lib/core/http_redirect.d.ts +0 -5
  221. package/lib/core/http_redirect.js +0 -6
  222. package/lib/core/server.d.ts +0 -20
  223. package/lib/core/server.js +0 -213
  224. package/lib/core/types.d.ts +0 -265
  225. package/lib/core/types.js +0 -4
  226. package/lib/exports.d.ts +0 -45
  227. package/lib/exports.js +0 -47
  228. package/lib/index.d.ts +0 -2
  229. package/lib/index.js +0 -3
  230. package/lib/plugins/base.d.ts +0 -4
  231. package/lib/plugins/base.js +0 -1
  232. package/lib/plugins/compress.d.ts +0 -13
  233. package/lib/plugins/compress.js +0 -12
  234. package/lib/plugins/cookie.d.ts +0 -13
  235. package/lib/plugins/cookie.js +0 -12
  236. package/lib/plugins/cors.d.ts +0 -22
  237. package/lib/plugins/cors.js +0 -32
  238. package/lib/plugins/file_parser.d.ts +0 -16
  239. package/lib/plugins/file_parser.js +0 -22
  240. package/lib/plugins/form_body_parser.d.ts +0 -13
  241. package/lib/plugins/form_body_parser.js +0 -12
  242. package/lib/plugins/ws_parser.d.ts +0 -13
  243. package/lib/plugins/ws_parser.js +0 -12
  244. package/lib/plugins/xml_parser.d.ts +0 -19
  245. package/lib/plugins/xml_parser.js +0 -43
  246. package/lib/services/ai.d.ts +0 -31
  247. package/lib/services/ai.js +0 -43
  248. package/lib/services/base.d.ts +0 -4
  249. package/lib/services/base.js +0 -1
  250. package/lib/services/cache.d.ts +0 -173
  251. package/lib/services/cache.js +0 -394
  252. package/lib/services/captcha.d.ts +0 -65
  253. package/lib/services/captcha.js +0 -72
  254. package/lib/services/crypto.d.ts +0 -110
  255. package/lib/services/db_value.d.ts +0 -60
  256. package/lib/services/db_value.js +0 -72
  257. package/lib/services/dingtalk.d.ts +0 -39
  258. package/lib/services/dingtalk.js +0 -45
  259. package/lib/services/dispose.d.ts +0 -17
  260. package/lib/services/dispose.js +0 -17
  261. package/lib/services/emoji.d.ts +0 -10
  262. package/lib/services/emoji.js +0 -2029
  263. package/lib/services/file_store.d.ts +0 -49
  264. package/lib/services/file_store.js +0 -61
  265. package/lib/services/html.d.ts +0 -11
  266. package/lib/services/html.js +0 -12
  267. package/lib/services/jwt.d.ts +0 -34
  268. package/lib/services/jwt.js +0 -59
  269. package/lib/services/log.d.ts +0 -37
  270. package/lib/services/log.js +0 -102
  271. package/lib/services/mail.d.ts +0 -23
  272. package/lib/services/mail.js +0 -25
  273. package/lib/services/pay.d.ts +0 -123
  274. package/lib/services/pay.js +0 -185
  275. package/lib/services/random.d.ts +0 -27
  276. package/lib/services/random.js +0 -45
  277. package/lib/services/rate_limit.d.ts +0 -37
  278. package/lib/services/rate_limit.js +0 -49
  279. package/lib/services/redis.d.ts +0 -12
  280. package/lib/services/redis.js +0 -7
  281. package/lib/services/sensitive_words.d.ts +0 -29
  282. package/lib/services/sensitive_words.js +0 -56
  283. package/lib/services/sms.d.ts +0 -40
  284. package/lib/services/sms.js +0 -78
  285. package/lib/types/dto.d.ts +0 -86
  286. package/lib/types/dto.js +0 -1
  287. package/lib/x.d.ts +0 -17
  288. package/lib/x.js +0 -30
  289. /package/lib/{cli/css_register.d.ts → core/types.mjs} +0 -0
  290. /package/lib/{cli/esbuild_register.d.ts → plugins/base.mjs} +0 -0
package/lib/exports.d.ts DELETED
@@ -1,45 +0,0 @@
1
- export * from './core/ctx_storage';
2
- export * from './core/define_bus';
3
- export * from './core/define_cron';
4
- export * from './core/define_handler';
5
- export * from './core/define_hook';
6
- export * from './core/define_server';
7
- export * from './core/define_task';
8
- export * from './core/get_handler_url';
9
- export * from './core/handler';
10
- export * from './core/http_error';
11
- export * from './core/http_header';
12
- export * from './core/http_method';
13
- export * from './core/http_redirect';
14
- export * from './core/server';
15
- export * from './core/types';
16
- export * from './plugins/base';
17
- export * from './plugins/compress';
18
- export * from './plugins/cookie';
19
- export * from './plugins/cors';
20
- export * from './plugins/file_parser';
21
- export * from './plugins/form_body_parser';
22
- export * from './plugins/ws_parser';
23
- export * from './plugins/xml_parser';
24
- export * from './services/ai';
25
- export * from './services/base';
26
- export * from './services/cache';
27
- export * from './services/captcha';
28
- export * from './services/crypto';
29
- export * from './services/db_value';
30
- export * from './services/dingtalk';
31
- export * from './services/dispose';
32
- export * from './services/emoji';
33
- export * from './services/file_store';
34
- export * from './services/html';
35
- export * from './services/jwt';
36
- export * from './services/log';
37
- export * from './services/mail';
38
- export * from './services/pay';
39
- export * from './services/random';
40
- export * from './services/rate_limit';
41
- export * from './services/redis';
42
- export * from './services/sensitive_words';
43
- export * from './services/sms';
44
- export * from './types/dto';
45
- export * from './x';
package/lib/exports.js DELETED
@@ -1,47 +0,0 @@
1
- // @index(['./**/*.ts', '!**/*.test.ts', '!./cli/**', '!**/_*', '!./index.ts'], f => `export * from '${f.path}';`)
2
- export * from "./core/ctx_storage";
3
- export * from "./core/define_bus";
4
- export * from "./core/define_cron";
5
- export * from "./core/define_handler";
6
- export * from "./core/define_hook";
7
- export * from "./core/define_server";
8
- export * from "./core/define_task";
9
- export * from "./core/get_handler_url";
10
- export * from "./core/handler";
11
- export * from "./core/http_error";
12
- export * from "./core/http_header";
13
- export * from "./core/http_method";
14
- export * from "./core/http_redirect";
15
- export * from "./core/server";
16
- export * from "./core/types";
17
- export * from "./plugins/base";
18
- export * from "./plugins/compress";
19
- export * from "./plugins/cookie";
20
- export * from "./plugins/cors";
21
- export * from "./plugins/file_parser";
22
- export * from "./plugins/form_body_parser";
23
- export * from "./plugins/ws_parser";
24
- export * from "./plugins/xml_parser";
25
- export * from "./services/ai";
26
- export * from "./services/base";
27
- export * from "./services/cache";
28
- export * from "./services/captcha";
29
- export * from "./services/crypto";
30
- export * from "./services/db_value";
31
- export * from "./services/dingtalk";
32
- export * from "./services/dispose";
33
- export * from "./services/emoji";
34
- export * from "./services/file_store";
35
- export * from "./services/html";
36
- export * from "./services/jwt";
37
- export * from "./services/log";
38
- export * from "./services/mail";
39
- export * from "./services/pay";
40
- export * from "./services/random";
41
- export * from "./services/rate_limit";
42
- export * from "./services/redis";
43
- export * from "./services/sensitive_words";
44
- export * from "./services/sms";
45
- export * from "./types/dto";
46
- export * from "./x";
47
- // @endindex
package/lib/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './exports';
2
- export * from '.x/models';
package/lib/index.js DELETED
@@ -1,3 +0,0 @@
1
- export * from "./exports";
2
- // @ts-ignore
3
- export * from '.x/models';
@@ -1,4 +0,0 @@
1
- import type { FastifyInstance } from 'fastify';
2
- export interface BasePlugin {
3
- readonly register: (fastify: FastifyInstance<any, any, any, any, any>) => void;
4
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,13 +0,0 @@
1
- import { type FastifyCompressOptions } from '@fastify/compress';
2
- import type { FastifyInstance } from 'fastify';
3
- import type { BasePlugin } from './base';
4
- export interface CompressPluginOptions extends FastifyCompressOptions {
5
- }
6
- /**
7
- * 响应压缩插件
8
- */
9
- export declare class CompressPlugin implements BasePlugin {
10
- private options?;
11
- constructor(options?: CompressPluginOptions | undefined);
12
- register(fastify: FastifyInstance<any, any, any, any, any>): void;
13
- }
@@ -1,12 +0,0 @@
1
- import FastifyCompress from '@fastify/compress';
2
- /**
3
- * 响应压缩插件
4
- */
5
- export class CompressPlugin {
6
- constructor(options) {
7
- this.options = options;
8
- }
9
- register(fastify) {
10
- fastify.register(FastifyCompress, this.options ?? {});
11
- }
12
- }
@@ -1,13 +0,0 @@
1
- import { type FastifyCookieOptions } from '@fastify/cookie';
2
- import type { FastifyInstance } from 'fastify';
3
- import type { BasePlugin } from './base';
4
- export interface CookiePluginOptions extends FastifyCookieOptions {
5
- }
6
- /**
7
- * Cookie 支持插件
8
- */
9
- export declare class CookiePlugin implements BasePlugin {
10
- private options?;
11
- constructor(options?: CookiePluginOptions | undefined);
12
- register(fastify: FastifyInstance<any, any, any, any, any>): void;
13
- }
@@ -1,12 +0,0 @@
1
- import FastifyCookie from '@fastify/cookie';
2
- /**
3
- * Cookie 支持插件
4
- */
5
- export class CookiePlugin {
6
- constructor(options) {
7
- this.options = options;
8
- }
9
- register(fastify) {
10
- fastify.register(FastifyCookie, this.options ?? {});
11
- }
12
- }
@@ -1,22 +0,0 @@
1
- import { type FastifyCorsOptions } from '@fastify/cors';
2
- import type { FastifyInstance } from 'fastify';
3
- import { type MsValue } from 'vtils';
4
- import type { BasePlugin } from './base';
5
- export interface CorsPluginOptions extends FastifyCorsOptions {
6
- /**
7
- * 允许的来源,支持位于开头的 * 通配符
8
- */
9
- allow: string[];
10
- /**
11
- * 预检请求缓存时长
12
- */
13
- ttl: MsValue;
14
- }
15
- /**
16
- * CORS 支持插件
17
- */
18
- export declare class CorsPlugin implements BasePlugin {
19
- private options;
20
- constructor(options: CorsPluginOptions);
21
- register(fastify: FastifyInstance<any, any, any, any, any>): void;
22
- }
@@ -1,32 +0,0 @@
1
- import FastifyCors from '@fastify/cors';
2
- import { memoize, ms, omitStrict } from 'vtils';
3
- /**
4
- * CORS 支持插件
5
- */
6
- export class CorsPlugin {
7
- constructor(options) {
8
- this.options = options;
9
- }
10
- register(fastify) {
11
- const allows = this.options.allow.map(item => ({
12
- type: item === '*' ? 'all' : item.startsWith('*.') ? 'endsWith' : 'equal',
13
- domain: item.startsWith('*.') ? item.replace('*.', '.') : item
14
- }));
15
- const allowAll = allows.length === 1 && allows[0].type === 'all';
16
- const check = memoize(origin => {
17
- let i = origin.indexOf(':');
18
- let j = origin.indexOf(':', i + 1);
19
- i += 3;
20
- j = j === -1 ? origin.length : j;
21
- const domain = origin.substring(i, j);
22
- const pass = allows.some(item => item.type === 'equal' ? domain === item.domain : domain.endsWith(item.domain));
23
- return pass;
24
- });
25
- fastify.register(FastifyCors, {
26
- origin: (origin, cb) => allowAll ? cb(null, true) : cb(null, origin ? check(origin) : true),
27
- maxAge: ms(this.options.ttl, true),
28
- exposedHeaders: ['x-trace-id'],
29
- ...omitStrict(this.options, ['allow', 'ttl'])
30
- });
31
- }
32
- }
@@ -1,16 +0,0 @@
1
- import { type FastifyMultipartOptions } from '@fastify/multipart';
2
- import type { FastifyInstance } from 'fastify';
3
- import type { BasePlugin } from './base';
4
- export { MultipartFile } from '@fastify/multipart';
5
- export interface FileParserPluginOptions extends FastifyMultipartOptions {
6
- }
7
- /**
8
- * file 解析器插件
9
- *
10
- * 注意:默认文件大小限制 1MB
11
- */
12
- export declare class FileParserPlugin implements BasePlugin {
13
- private options?;
14
- constructor(options?: FileParserPluginOptions | undefined);
15
- register(fastify: FastifyInstance<any, any, any, any, any>): void;
16
- }
@@ -1,22 +0,0 @@
1
- import FastifyMultipart from '@fastify/multipart';
2
- import { bytes } from 'vtils';
3
- export { MultipartFile } from '@fastify/multipart';
4
- /**
5
- * file 解析器插件
6
- *
7
- * 注意:默认文件大小限制 1MB
8
- */
9
- export class FileParserPlugin {
10
- constructor(options) {
11
- this.options = options;
12
- }
13
- register(fastify) {
14
- fastify.register(FastifyMultipart, {
15
- ...this.options,
16
- limits: {
17
- fileSize: bytes(1, 'MB'),
18
- ...this.options?.limits
19
- }
20
- });
21
- }
22
- }
@@ -1,13 +0,0 @@
1
- import { type FastifyFormbodyOptions } from '@fastify/formbody';
2
- import type { FastifyInstance } from 'fastify';
3
- import type { BasePlugin } from './base';
4
- export interface FormBodyParserPluginOptions extends FastifyFormbodyOptions {
5
- }
6
- /**
7
- * POST application/x-www-form-urlencoded 解析器插件
8
- */
9
- export declare class FormBodyParserPlugin implements BasePlugin {
10
- private options?;
11
- constructor(options?: FormBodyParserPluginOptions | undefined);
12
- register(fastify: FastifyInstance<any, any, any, any, any>): void;
13
- }
@@ -1,12 +0,0 @@
1
- import FastifyFormBody from '@fastify/formbody';
2
- /**
3
- * POST application/x-www-form-urlencoded 解析器插件
4
- */
5
- export class FormBodyParserPlugin {
6
- constructor(options) {
7
- this.options = options;
8
- }
9
- register(fastify) {
10
- fastify.register(FastifyFormBody, this.options || {});
11
- }
12
- }
@@ -1,13 +0,0 @@
1
- import { type WebsocketPluginOptions } from '@fastify/websocket';
2
- import type { FastifyInstance } from 'fastify';
3
- import type { BasePlugin } from './base';
4
- export interface WsParserPluginOptions extends WebsocketPluginOptions {
5
- }
6
- /**
7
- * websocket 解析器插件
8
- */
9
- export declare class WsParserPlugin implements BasePlugin {
10
- private options?;
11
- constructor(options?: WsParserPluginOptions | undefined);
12
- register(fastify: FastifyInstance<any, any, any, any, any>): void;
13
- }
@@ -1,12 +0,0 @@
1
- import FastifyWebsocket from '@fastify/websocket';
2
- /**
3
- * websocket 解析器插件
4
- */
5
- export class WsParserPlugin {
6
- constructor(options) {
7
- this.options = options;
8
- }
9
- register(fastify) {
10
- fastify.register(FastifyWebsocket, this.options ?? {});
11
- }
12
- }
@@ -1,19 +0,0 @@
1
- import { type X2jOptions } from 'fast-xml-parser';
2
- import type { FastifyInstance } from 'fastify';
3
- import type { BasePlugin } from './base';
4
- export interface XmlParserPluginOptions extends Partial<X2jOptions> {
5
- /**
6
- * 要处理的 `Content-Type`
7
- */
8
- contentType?: string | RegExp | string[];
9
- }
10
- /**
11
- * XML 解析器插件
12
- *
13
- * 底层解析器是 [fast-xml-parser](https://github.com/NaturalIntelligence/fast-xml-parser)
14
- */
15
- export declare class XmlParserPlugin implements BasePlugin {
16
- private options?;
17
- constructor(options?: XmlParserPluginOptions | undefined);
18
- register(fastify: FastifyInstance<any, any, any, any, any>): void;
19
- }
@@ -1,43 +0,0 @@
1
- import { XMLParser } from 'fast-xml-parser';
2
- /**
3
- * XML 解析器插件
4
- *
5
- * 底层解析器是 [fast-xml-parser](https://github.com/NaturalIntelligence/fast-xml-parser)
6
- */
7
- export class XmlParserPlugin {
8
- constructor(options) {
9
- this.options = options;
10
- }
11
- register(fastify) {
12
- const {
13
- contentType = ['text/xml', 'application/xml'],
14
- ...parseOptions
15
- } = this.options || {};
16
- const xmlParser = new XMLParser(parseOptions);
17
- fastify.addContentTypeParser(contentType, (_req, payload, done) => {
18
- let body = '';
19
- const handleError = err => done(err);
20
- const handleData = data => body += data;
21
- const handleEnd = () => {
22
- let data;
23
- let err;
24
- try {
25
- data = xmlParser.parse(body);
26
- } catch (_err) {
27
- err = _err;
28
- }
29
- payload.removeListener('error', handleError);
30
- payload.removeListener('data', handleData);
31
- payload.removeListener('end', handleEnd);
32
- if (err) {
33
- done(err);
34
- } else {
35
- done(null, data);
36
- }
37
- };
38
- payload.on('error', handleError);
39
- payload.on('data', handleData);
40
- payload.on('end', handleEnd);
41
- });
42
- }
43
- }
@@ -1,31 +0,0 @@
1
- import type { PartialBy } from 'vtils/types';
2
- import type { BaseService } from './base';
3
- export type AiServiceOptions = {
4
- /** 基础路径,如: https://dashscope.aliyuncs.com/compatible-mode/v1 */
5
- baseUrl: string;
6
- /** API密钥,如: sk-xxx */
7
- apiKey: string;
8
- /** 模型名称,如: deepseek-v3.1 */
9
- model: string;
10
- /** 额外请求头 */
11
- headers?: Record<string, string>;
12
- };
13
- export type AiServiceAskPayload = AiServiceOptions & {
14
- prompt?: string;
15
- question: string;
16
- };
17
- export type AiServiceAskResult = {
18
- answer: string;
19
- };
20
- export declare class AiService implements BaseService {
21
- private options;
22
- serviceName: string;
23
- static ask(payload: AiServiceAskPayload): Promise<AiServiceAskResult>;
24
- constructor(options: AiServiceOptions);
25
- ask(payload: PartialBy<AiServiceAskPayload, keyof AiServiceOptions>): Promise<AiServiceAskResult>;
26
- }
27
- declare module '../x' {
28
- interface X {
29
- ai: AiService;
30
- }
31
- }
@@ -1,43 +0,0 @@
1
- import { xr } from '@jayfong/x-request';
2
- import { ms } from 'vtils';
3
- export class AiService {
4
- static async ask(payload) {
5
- const res = await xr.postJson({
6
- url: `${payload.baseUrl}/chat/completions`,
7
- headers: {
8
- Authorization: `Bearer ${payload.apiKey}`,
9
- ...payload.headers
10
- },
11
- data: {
12
- model: payload.model,
13
- messages: [...(payload.prompt ? [{
14
- role: 'system',
15
- content: payload.prompt
16
- }] : []), {
17
- role: 'user',
18
- content: payload.question
19
- }],
20
- stream: false
21
- },
22
- timeoutMs: ms('1m'),
23
- noThrowHttpErrors: true
24
- });
25
- if (res.status !== 200) {
26
- console.error(res.data);
27
- throw new Error(JSON.stringify(res.data));
28
- }
29
- return {
30
- answer: res.data.choices[0].message.content
31
- };
32
- }
33
- constructor(options) {
34
- this.options = options;
35
- this.serviceName = 'ai';
36
- }
37
- ask(payload) {
38
- return AiService.ask({
39
- ...this.options,
40
- ...payload
41
- });
42
- }
43
- }
@@ -1,4 +0,0 @@
1
- export interface BaseService {
2
- /** 服务名称 */
3
- readonly serviceName: string;
4
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,173 +0,0 @@
1
- import { type MsValue } from 'vtils';
2
- import type { AsyncOrSync, IsEmptyArray } from 'vtils/types';
3
- import type { BaseService } from './base';
4
- export interface CacheSerializer {
5
- stringify(value: unknown): string;
6
- parse<T>(value: string): T;
7
- }
8
- export interface CacheServiceOptions {
9
- /** 默认过期时间 */
10
- ttl: MsValue;
11
- /**
12
- * 启用内存缓存
13
- *
14
- * - `true` 表示启用内存缓存
15
- * - `'only'` 表示仅使用内存缓存,不使用 Redis
16
- */
17
- memory?: boolean | 'only';
18
- /**
19
- * 序列化工具
20
- *
21
- * - `'nativeJson'` 使用原生 JSON 进行序列化和反序列化(默认)
22
- * - `'superJson'` 使用 SuperJson 进行序列化和反序列化,支持更多数据类型(比如Date、RegExp等)但性能较原生 JSON 略差
23
- * - 也可直接传入自定义 `{ stringify, parse }`
24
- */
25
- serializer?: 'nativeJson' | 'superJson' | CacheSerializer;
26
- }
27
- type Data = {
28
- [K in string]: (...args: any[]) => any;
29
- };
30
- type Key<T extends Data> = keyof T;
31
- type KeyPath<T extends Data> = {
32
- [K in keyof T]: T[K] extends (...args: infer X) => any ? IsEmptyArray<X> extends true ? K : [K, ...X] : never;
33
- }[keyof T];
34
- type KeyPathForNumberValue<T extends Data> = {
35
- [K in keyof T]: T[K] extends (...args: infer X) => number ? IsEmptyArray<X> extends true ? K : [K, ...X] : never;
36
- }[keyof T];
37
- type Value<T extends Data, P extends KeyPath<T>> = ReturnType<T[P extends any[] ? P[0] : P]>;
38
- type KeyPartialPath<T extends Data> = {
39
- [K in keyof T]: T[K] extends (...args: infer X) => any ? IsEmptyArray<X> extends true ? K : [K, ...Partial<X>] : never;
40
- }[keyof T];
41
- type CacheSetTTL<V> = MsValue | ((data: V, defaultTTL: MsValue) => MsValue);
42
- type CacheSetOptions<V> = CacheSetTTL<V> | {
43
- ttl?: CacheSetTTL<V>;
44
- };
45
- export declare class CacheService<TData extends Data = Data, TKey extends Key<TData> = any, TKeyPath extends KeyPath<TData> = any, TKeyPathForNumberValue extends KeyPathForNumberValue<TData> = any, TKeyPartialPath extends KeyPartialPath<TData> = any> implements BaseService {
46
- options: CacheServiceOptions;
47
- serviceName: string;
48
- private prefix;
49
- private hasMemoryCache;
50
- private hasMemoryCacheOnly;
51
- private memoryCache;
52
- private serializer;
53
- constructor(options: CacheServiceOptions);
54
- private toRedisKey;
55
- private serialize;
56
- private deserialize;
57
- /**
58
- * 设置缓存内容。对象类内容尽量避免使用,以免造成问题。
59
- *
60
- * @memory 支持
61
- * @param key 键
62
- * @param value 值
63
- * @param ttlOrOptions 缓存时间或选项
64
- * @returns 返回设置的缓存内容
65
- */
66
- set<K extends TKeyPath, V extends Value<TData, K>>(key: K, value: V, ttlOrOptions?: CacheSetOptions<V>): Promise<V>;
67
- /**
68
- * 存储值然后返回其对应的键,该键 URL 友好。
69
- *
70
- * @memory 支持
71
- * @param value 值
72
- * @param ttl 存活时间
73
- */
74
- save<T>(value: T | ((key: string) => AsyncOrSync<T>), ttl?: MsValue): Promise<string>;
75
- /**
76
- * 获取缓存内容。
77
- *
78
- * @memory 支持
79
- * @param key 键
80
- * @param options 选项
81
- * @returns 返回获取到的缓存内容
82
- */
83
- get<K extends TKeyPath, V extends Value<TData, K>>(key: K): Promise<V | null>;
84
- /**
85
- * 获取多个缓存内容。
86
- *
87
- * @memory 支持
88
- * @param keys 键
89
- * @returns 返回获取到的缓存内容
90
- */
91
- getMany<K extends TKeyPath, V extends Value<TData, K>>(...keys: K[]): Promise<Array<V | null>>;
92
- /**
93
- * 获取距离过期时间的毫秒数(未设置过期时间返回-1,键不存在返回-2)。
94
- * memory=only 模式下内存缓存无 TTL 概念,始终返回-1。
95
- *
96
- * @memory 支持
97
- * @param key 键
98
- * @returns 返回距离过期时间的毫秒数
99
- */
100
- ttl<K extends TKeyPath>(key: K): Promise<number>;
101
- /**
102
- * 缓存内容。
103
- *
104
- * @memory 支持
105
- * @param key 键
106
- * @param action 获取缓存内容的操作
107
- * @param ttlOrOptions 缓存时间或选项
108
- * @returns 返回获取到的内容
109
- */
110
- remember<K extends TKeyPath, V extends Value<TData, K>>(key: K, action: () => V | Promise<V>, ttlOrOptions?: CacheSetOptions<V>): Promise<V>;
111
- rememberMany<T, K extends TKeyPath, V extends Value<TData, K>>(raws: T[], key: (raw: T) => K, action: (raws: T[]) => V[] | Promise<V[]>, ttlOrOptions?: CacheSetOptions<V>): Promise<V[]>;
112
- /**
113
- * 自增。
114
- *
115
- * @memory 支持
116
- * @param key 键
117
- * @param increment 增量
118
- */
119
- increase<K extends TKeyPathForNumberValue>(key: K, increment?: number): Promise<number>;
120
- /**
121
- * 自减。
122
- *
123
- * @memory 支持
124
- * @param key 键
125
- * @param decrement 减量
126
- */
127
- decrease<K extends TKeyPathForNumberValue>(key: K, decrement?: number): Promise<number>;
128
- /**
129
- * 新增元素。
130
- *
131
- * @memory 支持
132
- * @param key 键
133
- * @param values 元素列表
134
- */
135
- push<K extends TKeyPath, V extends Value<TData, K>>(key: K, ...values: V[]): Promise<any>;
136
- /**
137
- * 从头部弹出元素并返回。
138
- *
139
- * @memory 支持
140
- * @param key 键
141
- * @param count 弹出元素个数
142
- */
143
- shift<K extends TKeyPath, V extends Value<TData, K>>(key: K, count?: number): Promise<V[]>;
144
- /**
145
- * 删除缓存。
146
- *
147
- * @memory 支持
148
- * @param keys 键列表
149
- */
150
- remove<K extends TKeyPath>(...keys: K[]): Promise<number>;
151
- /**
152
- * 清空全部缓存。
153
- *
154
- * @memory 支持
155
- */
156
- clearAll(): Promise<number>;
157
- /**
158
- * 清空指定前缀的缓存。
159
- *
160
- * @memory 支持
161
- */
162
- clear(...prefixes: Array<TKey | TKeyPartialPath>): Promise<number>;
163
- /**
164
- * 派生出一个指定类型的缓存服务。
165
- */
166
- fork<TNextData extends Data>(options?: Partial<CacheServiceOptions>): CacheService<TNextData, Key<TNextData>, KeyPath<TNextData>, KeyPathForNumberValue<TNextData>, KeyPartialPath<TNextData>>;
167
- }
168
- declare module '../x' {
169
- interface X {
170
- cache: CacheService;
171
- }
172
- }
173
- export {};