@jayfong/x-server 2.106.5 → 2.107.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.
File without changes
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ exports.__esModule = true;
5
+ exports.RandomService = void 0;
6
+ var _cuid = require("@paralleldrive/cuid2");
7
+ var _randomstring = _interopRequireDefault(require("randomstring"));
8
+ class RandomService {
9
+ constructor() {
10
+ this.serviceName = 'random';
11
+ }
12
+ /**
13
+ * 24位。
14
+ *
15
+ * @see https://github.com/paralleldrive/cuid2
16
+ */
17
+ cuid2() {
18
+ return (0, _cuid.createId)();
19
+ }
20
+
21
+ /**
22
+ * [0-9 a-z A-Z]
23
+ */
24
+ generateAlphanumeric(length) {
25
+ return _randomstring.default.generate({
26
+ charset: 'alphanumeric',
27
+ length: length
28
+ });
29
+ }
30
+
31
+ /**
32
+ * [a-z A-Z]
33
+ */
34
+ generateAlphabetic(length) {
35
+ return _randomstring.default.generate({
36
+ charset: 'alphabetic',
37
+ length: length
38
+ });
39
+ }
40
+
41
+ /**
42
+ * [0-9]
43
+ */
44
+ generateNumeric(length) {
45
+ return _randomstring.default.generate({
46
+ charset: 'numeric',
47
+ length: length
48
+ });
49
+ }
50
+ }
51
+ exports.RandomService = RandomService;
package/lib/_cjs/x.js CHANGED
@@ -3,8 +3,8 @@
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
  exports.__esModule = true;
5
5
  exports.x = void 0;
6
- var _os = _interopRequireDefault(require("os"));
7
- var _path = _interopRequireDefault(require("path"));
6
+ var _nodeOs = _interopRequireDefault(require("node:os"));
7
+ var _nodePath = _interopRequireDefault(require("node:path"));
8
8
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
9
9
  var _ctx_storage = require("./core/ctx_storage");
10
10
  var _crypto = require("./services/crypto");
@@ -13,11 +13,12 @@ var _dispose = require("./services/dispose");
13
13
  var _emoji = require("./services/emoji");
14
14
  var _html = require("./services/html");
15
15
  var _log = require("./services/log");
16
+ var _random = require("./services/random");
16
17
  const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
17
18
  const x = exports.x = {
18
19
  appId: env.APP_ID,
19
20
  env: env,
20
- dataDir: _path.default.join(_os.default.homedir(), `.xs/${env.APP_ID}`),
21
+ dataDir: _nodePath.default.join(_nodeOs.default.homedir(), `.xs/${env.APP_ID}`),
21
22
  get ctx() {
22
23
  return _ctx_storage.ctxStorage.getStore();
23
24
  },
@@ -31,4 +32,4 @@ const x = exports.x = {
31
32
  _fsExtra.default.ensureDirSync(x.dataDir);
32
33
  x.register(new _dispose.DisposeService({
33
34
  disposeOnExit: true
34
- }), new _log.LogService(), new _emoji.EmojiService(), new _db_value.DbValueService(), new _crypto.CryptoService(), new _html.HtmlService());
35
+ }), new _log.LogService(), new _emoji.EmojiService(), new _db_value.DbValueService(), new _crypto.CryptoService(), new _html.HtmlService(), new _random.RandomService());
@@ -1,15 +1,15 @@
1
- import type { IncomingHttpHeaders } from 'http';
1
+ import type { IncomingHttpHeaders } from 'node:http';
2
2
  import type { MultipartFile } from '@fastify/multipart';
3
3
  import type { SocketStream } from '@fastify/websocket';
4
4
  import type { Queue } from 'bull';
5
- import { CronJob } from 'cron';
6
- import { FastifyReply, FastifyRequest, FastifyServerOptions } from 'fastify';
5
+ import type { CronJob } from 'cron';
6
+ import type { FastifyReply, FastifyRequest, FastifyServerOptions } from 'fastify';
7
7
  import type { MsValue } from 'vtils';
8
8
  import type { AsyncOrSync, LiteralUnion, OneOrMore, RequiredDeep } from 'vtils/types';
9
- import * as vae from 'vtils/vae';
10
- import { yup } from 'vtils/validator';
11
- import { BasePlugin } from '../plugins/base';
12
- import { BaseService } from '../services/base';
9
+ import type * as vae from 'vtils/vae';
10
+ import type { yup } from 'vtils/validator';
11
+ import type { BasePlugin } from '../plugins/base';
12
+ import type { BaseService } from '../services/base';
13
13
  import type { DisposeService } from '../services/dispose';
14
14
  import type { Handler } from './handler';
15
15
  export declare namespace XServer {
@@ -117,7 +117,7 @@ export declare namespace XHandler {
117
117
  ctx: Context) => AsyncOrSync<TResData>;
118
118
  interface ExtraOptions {
119
119
  }
120
- interface Options<TReqData extends any = void, TResData extends any = void, TReqMethod extends Method = Method> extends ExtraOptions {
120
+ interface Options<TReqData extends any = void, TResData extends any = void, TReqMethod extends Method = Method, TReqDataRequired = RequiredDeep<TReqData>> extends ExtraOptions {
121
121
  /**
122
122
  * 请求方法
123
123
  *
@@ -156,11 +156,11 @@ export declare namespace XHandler {
156
156
  /**
157
157
  * 请求数据验证结构(yup)
158
158
  */
159
- requestDataSchema?: (_: typeof yup) => yup.GetSchema<RequiredDeep<TReqData>>;
159
+ requestDataSchema?: (_: typeof yup) => yup.GetSchema<TReqDataRequired>;
160
160
  /**
161
161
  * 请求数据验证结构(vae)
162
162
  */
163
- requestDataSchemaVae?: vae.VaeSchemaOf<TReqData> | ((_: vae.VaeSchemaBuilder<TReqData>) => vae.VaeSchemaOf<TReqData> | vae.VaeObjectSchemaShapeOf<TReqData>);
163
+ requestDataSchemaVae?: vae.VaeSchemaOf<TReqDataRequired> | ((_: vae.VaeSchemaBuilder<TReqDataRequired>) => vae.VaeSchemaOf<TReqDataRequired> | vae.VaeObjectSchemaShapeOf<TReqDataRequired>);
164
164
  /**
165
165
  * 是否使用异步上下文
166
166
  *
@@ -0,0 +1,27 @@
1
+ import type { BaseService } from './base';
2
+ export declare class RandomService implements BaseService {
3
+ serviceName: string;
4
+ /**
5
+ * 24位。
6
+ *
7
+ * @see https://github.com/paralleldrive/cuid2
8
+ */
9
+ cuid2(): string;
10
+ /**
11
+ * [0-9 a-z A-Z]
12
+ */
13
+ generateAlphanumeric(length: number): string;
14
+ /**
15
+ * [a-z A-Z]
16
+ */
17
+ generateAlphabetic(length: number): string;
18
+ /**
19
+ * [0-9]
20
+ */
21
+ generateNumeric(length: number): string;
22
+ }
23
+ declare module '../x' {
24
+ interface X {
25
+ random: RandomService;
26
+ }
27
+ }
@@ -0,0 +1,45 @@
1
+ import { createId as cuid2 } from '@paralleldrive/cuid2';
2
+ import randomstring from 'randomstring';
3
+ export class RandomService {
4
+ constructor() {
5
+ this.serviceName = 'random';
6
+ }
7
+ /**
8
+ * 24位。
9
+ *
10
+ * @see https://github.com/paralleldrive/cuid2
11
+ */
12
+ cuid2() {
13
+ return cuid2();
14
+ }
15
+
16
+ /**
17
+ * [0-9 a-z A-Z]
18
+ */
19
+ generateAlphanumeric(length) {
20
+ return randomstring.generate({
21
+ charset: 'alphanumeric',
22
+ length: length
23
+ });
24
+ }
25
+
26
+ /**
27
+ * [a-z A-Z]
28
+ */
29
+ generateAlphabetic(length) {
30
+ return randomstring.generate({
31
+ charset: 'alphabetic',
32
+ length: length
33
+ });
34
+ }
35
+
36
+ /**
37
+ * [0-9]
38
+ */
39
+ generateNumeric(length) {
40
+ return randomstring.generate({
41
+ charset: 'numeric',
42
+ length: length
43
+ });
44
+ }
45
+ }
package/lib/x.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { XHandler } from './core/types';
2
- import { BaseService } from './services/base';
1
+ import type { XHandler } from './core/types';
2
+ import type { BaseService } from './services/base';
3
3
  import './services/crypto';
4
4
  import './services/db_value';
5
5
  import './services/dispose';
6
6
  import './services/emoji';
7
7
  import './services/html';
8
8
  import './services/log';
9
+ import './services/random';
9
10
  export interface X {
10
11
  readonly appId: string;
11
12
  readonly env: NodeJS.ProcessEnv;
package/lib/x.js CHANGED
@@ -1,5 +1,5 @@
1
- import os from 'os';
2
- import path from 'path';
1
+ import os from 'node:os';
2
+ import path from 'node:path';
3
3
  import fs from 'fs-extra';
4
4
  import { ctxStorage } from "./core/ctx_storage";
5
5
  import { CryptoService } from "./services/crypto";
@@ -8,6 +8,7 @@ import { DisposeService } from "./services/dispose";
8
8
  import { EmojiService } from "./services/emoji";
9
9
  import { HtmlService } from "./services/html";
10
10
  import { LogService } from "./services/log";
11
+ import { RandomService } from "./services/random";
11
12
  const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
12
13
  export const x = {
13
14
  appId: env.APP_ID,
@@ -26,4 +27,4 @@ export const x = {
26
27
  fs.ensureDirSync(x.dataDir);
27
28
  x.register(new DisposeService({
28
29
  disposeOnExit: true
29
- }), new LogService(), new EmojiService(), new DbValueService(), new CryptoService(), new HtmlService());
30
+ }), new LogService(), new EmojiService(), new DbValueService(), new CryptoService(), new HtmlService(), new RandomService());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.106.5",
3
+ "version": "2.107.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",
@@ -27,6 +27,7 @@
27
27
  "@fastify/formbody": "^7.4.0",
28
28
  "@fastify/multipart": "^7.7.0",
29
29
  "@fastify/websocket": "^8.1.0",
30
+ "@paralleldrive/cuid2": "^3.0.4",
30
31
  "@prisma/adapter-mariadb": "^7.1.0",
31
32
  "@prisma/client": "^7.1.0",
32
33
  "@types/busboy": "^0.3.2",
@@ -67,6 +68,7 @@
67
68
  "pinyin-pro": "^3.26.0",
68
69
  "pirates": "^4.0.6",
69
70
  "prisma": "^7.1.0",
71
+ "randomstring": "^1.3.1",
70
72
  "select-run": "^1.1.2",
71
73
  "supports-color": "^8",
72
74
  "svg-captcha": "^1.4.0",
@@ -86,6 +88,7 @@
86
88
  "@types/ioredis": "^5.0.0",
87
89
  "@types/json-schema": "^7.0.11",
88
90
  "@types/lz-string": "^1.3.34",
91
+ "@types/randomstring": "^1.3.0",
89
92
  "axios": "^1.4.0",
90
93
  "ioredis-mock": "^8.7.0",
91
94
  "json-xml-parse": "^1.2.4",