@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
@@ -1,48 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.XmlParserPlugin = void 0;
5
- var _fastXmlParser = require("fast-xml-parser");
6
- /**
7
- * XML 解析器插件
8
- *
9
- * 底层解析器是 [fast-xml-parser](https://github.com/NaturalIntelligence/fast-xml-parser)
10
- */
11
- class XmlParserPlugin {
12
- constructor(options) {
13
- this.options = options;
14
- }
15
- register(fastify) {
16
- const {
17
- contentType = ['text/xml', 'application/xml'],
18
- ...parseOptions
19
- } = this.options || {};
20
- const xmlParser = new _fastXmlParser.XMLParser(parseOptions);
21
- fastify.addContentTypeParser(contentType, (_req, payload, done) => {
22
- let body = '';
23
- const handleError = err => done(err);
24
- const handleData = data => body += data;
25
- const handleEnd = () => {
26
- let data;
27
- let err;
28
- try {
29
- data = xmlParser.parse(body);
30
- } catch (_err) {
31
- err = _err;
32
- }
33
- payload.removeListener('error', handleError);
34
- payload.removeListener('data', handleData);
35
- payload.removeListener('end', handleEnd);
36
- if (err) {
37
- done(err);
38
- } else {
39
- done(null, data);
40
- }
41
- };
42
- payload.on('error', handleError);
43
- payload.on('data', handleData);
44
- payload.on('end', handleEnd);
45
- });
46
- }
47
- }
48
- exports.XmlParserPlugin = XmlParserPlugin;
@@ -1,48 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.AiService = void 0;
5
- var _xRequest = require("@jayfong/x-request");
6
- var _vtils = require("vtils");
7
- class AiService {
8
- static async ask(payload) {
9
- const res = await _xRequest.xr.postJson({
10
- url: `${payload.baseUrl}/chat/completions`,
11
- headers: {
12
- Authorization: `Bearer ${payload.apiKey}`,
13
- ...payload.headers
14
- },
15
- data: {
16
- model: payload.model,
17
- messages: [...(payload.prompt ? [{
18
- role: 'system',
19
- content: payload.prompt
20
- }] : []), {
21
- role: 'user',
22
- content: payload.question
23
- }],
24
- stream: false
25
- },
26
- timeoutMs: (0, _vtils.ms)('1m'),
27
- noThrowHttpErrors: true
28
- });
29
- if (res.status !== 200) {
30
- console.error(res.data);
31
- throw new Error(JSON.stringify(res.data));
32
- }
33
- return {
34
- answer: res.data.choices[0].message.content
35
- };
36
- }
37
- constructor(options) {
38
- this.options = options;
39
- this.serviceName = 'ai';
40
- }
41
- ask(payload) {
42
- return AiService.ask({
43
- ...this.options,
44
- ...payload
45
- });
46
- }
47
- }
48
- exports.AiService = AiService;
@@ -1,3 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
@@ -1,400 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.CacheService = void 0;
6
- var _cuid = _interopRequireDefault(require("cuid"));
7
- var _superjson = _interopRequireDefault(require("superjson"));
8
- var _vtils = require("vtils");
9
- var _x = require("../x");
10
- const nativeJsonSerializer = {
11
- stringify: JSON.stringify,
12
- parse: JSON.parse
13
- };
14
- const superJsonSerializer = {
15
- stringify: _superjson.default.stringify,
16
- parse: _superjson.default.parse
17
- };
18
- class CacheService {
19
- constructor(options) {
20
- this.options = options;
21
- this.serviceName = 'cache';
22
- this.prefix = void 0;
23
- this.hasMemoryCache = false;
24
- this.hasMemoryCacheOnly = false;
25
- this.memoryCache = void 0;
26
- this.serializer = void 0;
27
- this.prefix = `${_x.x.appId}:`;
28
- this.hasMemoryCache = !!options.memory;
29
- this.hasMemoryCacheOnly = options.memory === 'only';
30
- this.serializer = options.serializer == null || options.serializer === 'nativeJson' ? nativeJsonSerializer : options.serializer === 'superJson' ? superJsonSerializer : options.serializer;
31
- this.memoryCache = !this.hasMemoryCache ? undefined : new Map();
32
- }
33
- toRedisKey(key) {
34
- return `${this.prefix}${Array.isArray(key) ? key.join('_') : String(key)}`;
35
- }
36
- serialize(value) {
37
- return this.serializer.stringify(value);
38
- }
39
- deserialize(value) {
40
- return this.serializer.parse(value);
41
- }
42
-
43
- /**
44
- * 设置缓存内容。对象类内容尽量避免使用,以免造成问题。
45
- *
46
- * @memory 支持
47
- * @param key 键
48
- * @param value 值
49
- * @param ttlOrOptions 缓存时间或选项
50
- * @returns 返回设置的缓存内容
51
- */
52
- async set(key, value, ttlOrOptions) {
53
- const ttl = ttlOrOptions == null ? this.options.ttl : typeof ttlOrOptions === 'object' ? ttlOrOptions.ttl : ttlOrOptions;
54
- const redisTtl = typeof ttl === 'function' ? ttl(value, this.options.ttl) : ttl;
55
- if (redisTtl) {
56
- const redisTtlMs = (0, _vtils.ms)(redisTtl);
57
- // Redis 的 TTL 不能小于等于 0
58
- if (redisTtlMs > 0) {
59
- const redisKey = this.toRedisKey(key);
60
- if (!this.hasMemoryCacheOnly) {
61
- const redisValue = this.serialize(value);
62
- await _x.x.redis.set(redisKey, redisValue,
63
- // 毫秒
64
- 'PX', redisTtlMs);
65
- }
66
- if (this.hasMemoryCache) {
67
- this.memoryCache.set(redisKey, value);
68
- }
69
- }
70
- }
71
- return value;
72
- }
73
-
74
- /**
75
- * 存储值然后返回其对应的键,该键 URL 友好。
76
- *
77
- * @memory 支持
78
- * @param value 值
79
- * @param ttl 存活时间
80
- */
81
- async save(value, ttl = this.options.ttl) {
82
- const key = `X__${(0, _cuid.default)()}`;
83
- await this.set(key, typeof value === 'function' ? await value(key) : value, ttl);
84
- return key;
85
- }
86
-
87
- /**
88
- * 获取缓存内容。
89
- *
90
- * @memory 支持
91
- * @param key 键
92
- * @param options 选项
93
- * @returns 返回获取到的缓存内容
94
- */
95
- async get(key) {
96
- const redisKey = this.toRedisKey(key);
97
- if (this.hasMemoryCache) {
98
- if (this.memoryCache.has(redisKey)) {
99
- return this.memoryCache.get(redisKey);
100
- } else if (this.hasMemoryCacheOnly) {
101
- return null;
102
- }
103
- }
104
- const gotValue = await _x.x.redis.get(redisKey);
105
- if (gotValue) {
106
- try {
107
- const parsedValue = this.deserialize(gotValue);
108
- if (this.hasMemoryCache) {
109
- this.memoryCache.set(redisKey, parsedValue);
110
- }
111
- return parsedValue;
112
- } catch {}
113
- }
114
- return null;
115
- }
116
-
117
- /**
118
- * 获取多个缓存内容。
119
- *
120
- * @memory 支持
121
- * @param keys 键
122
- * @returns 返回获取到的缓存内容
123
- */
124
- async getMany(...keys) {
125
- const redisKeys = keys.map(key => this.toRedisKey(key));
126
- if (!redisKeys.length) return [];
127
- if (this.hasMemoryCacheOnly) {
128
- return redisKeys.map(redisKey => this.memoryCache.has(redisKey) ? this.memoryCache.get(redisKey) : null);
129
- }
130
- if (this.hasMemoryCache) {
131
- const result = new Array(keys.length).fill(null);
132
- const missIndexes = [];
133
- const missRedisKeys = [];
134
- for (let i = 0; i < redisKeys.length; i++) {
135
- if (this.memoryCache.has(redisKeys[i])) {
136
- result[i] = this.memoryCache.get(redisKeys[i]);
137
- } else {
138
- missIndexes.push(i);
139
- missRedisKeys.push(redisKeys[i]);
140
- }
141
- }
142
- if (missRedisKeys.length) {
143
- const gotValues = await _x.x.redis.mget(...missRedisKeys);
144
- gotValues.forEach((value, idx) => {
145
- if (value != null) {
146
- try {
147
- const parsed = this.deserialize(value);
148
- this.memoryCache.set(missRedisKeys[idx], parsed);
149
- result[missIndexes[idx]] = parsed;
150
- } catch {}
151
- }
152
- });
153
- }
154
- return result;
155
- }
156
- const gotValues = await _x.x.redis.mget(...redisKeys);
157
- return gotValues.map(value => {
158
- if (value == null) return null;
159
- try {
160
- return this.deserialize(value);
161
- } catch {
162
- return null;
163
- }
164
- });
165
- }
166
-
167
- /**
168
- * 获取距离过期时间的毫秒数(未设置过期时间返回-1,键不存在返回-2)。
169
- * memory=only 模式下内存缓存无 TTL 概念,始终返回-1。
170
- *
171
- * @memory 支持
172
- * @param key 键
173
- * @returns 返回距离过期时间的毫秒数
174
- */
175
- async ttl(key) {
176
- if (this.hasMemoryCacheOnly) {
177
- return -1;
178
- }
179
- const redisKey = this.toRedisKey(key);
180
- return _x.x.redis.pttl(redisKey);
181
- }
182
-
183
- /**
184
- * 缓存内容。
185
- *
186
- * @memory 支持
187
- * @param key 键
188
- * @param action 获取缓存内容的操作
189
- * @param ttlOrOptions 缓存时间或选项
190
- * @returns 返回获取到的内容
191
- */
192
- async remember(key, action, ttlOrOptions) {
193
- let value = await this.get(key);
194
- if (value === null) {
195
- value = await action();
196
- await this.set(key, value, ttlOrOptions);
197
- }
198
- return value;
199
- }
200
- async rememberMany(raws, key, action, ttlOrOptions) {
201
- const keys = raws.map(key);
202
- const values = await this.getMany(...keys);
203
- const shouldUpdateIndexes = [];
204
- for (let i = 0, len = values.length; i < len; i++) {
205
- if (values[i] === null) {
206
- shouldUpdateIndexes.push(i);
207
- }
208
- }
209
- if (shouldUpdateIndexes.length) {
210
- const nextValues = await action(shouldUpdateIndexes.map(i => raws[i]));
211
- await Promise.all(nextValues.map(async (nextValue, index) => {
212
- values[shouldUpdateIndexes[index]] = nextValue;
213
- await this.set(keys[shouldUpdateIndexes[index]], nextValue, ttlOrOptions);
214
- }));
215
- }
216
- return values;
217
- }
218
-
219
- /**
220
- * 自增。
221
- *
222
- * @memory 支持
223
- * @param key 键
224
- * @param increment 增量
225
- */
226
- async increase(key, increment = 1) {
227
- const redisKey = this.toRedisKey(key);
228
- if (this.hasMemoryCacheOnly) {
229
- const current = this.memoryCache.get(redisKey) ?? 0;
230
- const next = current + increment;
231
- this.memoryCache.set(redisKey, next);
232
- return next;
233
- }
234
- const result = await _x.x.redis.incrby(redisKey, increment);
235
- if (this.hasMemoryCache) {
236
- this.memoryCache.delete(redisKey);
237
- }
238
- return result;
239
- }
240
-
241
- /**
242
- * 自减。
243
- *
244
- * @memory 支持
245
- * @param key 键
246
- * @param decrement 减量
247
- */
248
- async decrease(key, decrement = 1) {
249
- return this.increase(key, -decrement);
250
- }
251
-
252
- /**
253
- * 新增元素。
254
- *
255
- * @memory 支持
256
- * @param key 键
257
- * @param values 元素列表
258
- */
259
- async push(key, ...values) {
260
- const redisKey = this.toRedisKey(key);
261
- if (this.hasMemoryCacheOnly) {
262
- const arr = this.memoryCache.get(redisKey) ?? [];
263
- arr.push(...values);
264
- this.memoryCache.set(redisKey, arr);
265
- return arr.length;
266
- }
267
- const result = await _x.x.redis.rpush(redisKey, ...values.map(value => this.serialize(value)));
268
- if (this.hasMemoryCache) {
269
- this.memoryCache.delete(redisKey);
270
- }
271
- return result;
272
- }
273
-
274
- /**
275
- * 从头部弹出元素并返回。
276
- *
277
- * @memory 支持
278
- * @param key 键
279
- * @param count 弹出元素个数
280
- */
281
- async shift(key, count = 1) {
282
- const redisKey = this.toRedisKey(key);
283
- if (this.hasMemoryCacheOnly) {
284
- const arr = this.memoryCache.get(redisKey) ?? [];
285
- const res = arr.splice(0, count);
286
- if (arr.length) {
287
- this.memoryCache.set(redisKey, arr);
288
- } else {
289
- this.memoryCache.delete(redisKey);
290
- }
291
- return res;
292
- }
293
- const res = [];
294
- for (let i = 0; i < count; i++) {
295
- const item = await _x.x.redis.lpop(redisKey);
296
- if (item) {
297
- res.push(this.deserialize(item));
298
- } else {
299
- break;
300
- }
301
- }
302
- if (this.hasMemoryCache && res.length) {
303
- this.memoryCache.delete(redisKey);
304
- }
305
- return res;
306
- }
307
-
308
- /**
309
- * 删除缓存。
310
- *
311
- * @memory 支持
312
- * @param keys 键列表
313
- */
314
- async remove(...keys) {
315
- if (!keys.length) {
316
- return 0;
317
- }
318
- const redisKeys = keys.map(key => this.toRedisKey(key));
319
- let count = 0;
320
- if (!this.hasMemoryCacheOnly) {
321
- count = await _x.x.redis.del(...redisKeys);
322
- }
323
- if (this.hasMemoryCache) {
324
- let deletedCount = 0;
325
- redisKeys.forEach(redisKey => {
326
- const isDeleted = this.memoryCache.delete(redisKey);
327
- if (isDeleted) {
328
- deletedCount++;
329
- }
330
- });
331
- if (this.hasMemoryCacheOnly) {
332
- count = deletedCount;
333
- }
334
- }
335
- return count;
336
- }
337
-
338
- /**
339
- * 清空全部缓存。
340
- *
341
- * @memory 支持
342
- */
343
- async clearAll() {
344
- let count = 0;
345
- if (!this.hasMemoryCacheOnly) {
346
- const keys = await _x.x.redis.keys(`${this.prefix}*`);
347
- count = keys.length ? await _x.x.redis.del(...keys) : 0;
348
- }
349
- if (this.hasMemoryCache) {
350
- const memoryCacheSize = this.memoryCache.size;
351
- this.memoryCache.clear();
352
- if (this.hasMemoryCacheOnly) {
353
- count = memoryCacheSize;
354
- }
355
- }
356
- return count;
357
- }
358
-
359
- /**
360
- * 清空指定前缀的缓存。
361
- *
362
- * @memory 支持
363
- */
364
- async clear(...prefixes) {
365
- if (!prefixes.length) {
366
- return 0;
367
- }
368
- let count = 0;
369
- if (!this.hasMemoryCacheOnly) {
370
- const keys = (await Promise.all(prefixes.map(prefix => _x.x.redis.keys(`${this.toRedisKey(prefix)}*`)))).flat();
371
- count = keys.length ? await _x.x.redis.del(...keys) : 0;
372
- }
373
- if (this.options.memory) {
374
- let deletedCount = 0;
375
- this.memoryCache.forEach((_v, k) => {
376
- if (prefixes.some(prefix => k.startsWith(this.toRedisKey(prefix)))) {
377
- const isDeleted = this.memoryCache.delete(k);
378
- if (isDeleted) {
379
- deletedCount++;
380
- }
381
- }
382
- });
383
- if (this.hasMemoryCacheOnly) {
384
- count = deletedCount;
385
- }
386
- }
387
- return count;
388
- }
389
-
390
- /**
391
- * 派生出一个指定类型的缓存服务。
392
- */
393
- fork(options) {
394
- return options ? new CacheService({
395
- ...this.options,
396
- ...options
397
- }) : this;
398
- }
399
- }
400
- exports.CacheService = CacheService;
@@ -1,78 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- exports.__esModule = true;
5
- exports.CaptchaService = void 0;
6
- var _miniSvgDataUri = _interopRequireDefault(require("mini-svg-data-uri"));
7
- var _svgCaptcha = _interopRequireDefault(require("svg-captcha"));
8
- var _vtils = require("vtils");
9
- var _x = require("../x");
10
- class CaptchaService {
11
- constructor() {
12
- this.serviceName = 'captcha';
13
- this.charsetMap = {
14
- '09': '0123456789'.split(''),
15
- '19': '123456789'.split(''),
16
- az: 'abcdefghijklmnopqrstuvwxyz'.split(''),
17
- AZ: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
18
- aZ: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
19
- '0z': '0123456789abcdefghijklmnopqrstuvwxyz'.split(''),
20
- '0Z': '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
21
- '1z': '123456789abcdefghijklmnopqrstuvwxyz'.split(''),
22
- '1Z': '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
23
- };
24
- }
25
- async generate(payload) {
26
- const chars = this.charsetMap[payload.charset];
27
- let code = '';
28
- for (let i = 0; i < payload.size; i++) {
29
- code += (0, _vtils.sample)(chars);
30
- }
31
- const token = await _x.x.cache.save({
32
- code: code,
33
- scene: payload.scene,
34
- retryCount: payload.retryCount ?? 0,
35
- extra: payload.extra
36
- }, payload.ttl);
37
- return {
38
- token: token,
39
- code: code
40
- };
41
- }
42
- async generateDataUrl(payload) {
43
- const {
44
- text: code,
45
- data: svg
46
- } = _svgCaptcha.default.create({
47
- size: payload.size,
48
- charPreset: this.charsetMap[payload.charset].join('')
49
- });
50
- const token = await _x.x.cache.save({
51
- code: code,
52
- scene: payload.scene,
53
- retryCount: payload.retryCount || 0,
54
- extra: payload.extra
55
- }, payload.ttl);
56
- const dataUrl = (0, _miniSvgDataUri.default)(svg);
57
- return {
58
- token: token,
59
- code: code,
60
- dataUrl: dataUrl
61
- };
62
- }
63
- async verify(options) {
64
- const expected = await _x.x.cache.get(options.token);
65
- if (expected == null) {
66
- return false;
67
- }
68
- if (expected.retryCount === 0) {
69
- await _x.x.cache.remove(options.token);
70
- } else {
71
- expected.retryCount--;
72
- const ttl = await _x.x.cache.ttl(options.token);
73
- await _x.x.cache.set(options.token, expected, ttl);
74
- }
75
- return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase() && (options.extra ? options.extra(expected.extra) : true);
76
- }
77
- }
78
- exports.CaptchaService = CaptchaService;