@jayfong/x-server 1.13.1 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,32 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [1.15.0](https://github.com/jfWorks/x-server/compare/v1.14.1...v1.15.0) (2022-04-26)
6
+
7
+
8
+ ### Features
9
+
10
+ * x.env ([e39771f](https://github.com/jfWorks/x-server/commit/e39771f998f4b952bda528105fb18b6895e0ce81))
11
+
12
+ ### [1.14.1](https://github.com/jfWorks/x-server/compare/v1.14.0...v1.14.1) (2022-04-26)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * requestDataSchema ([8b4f910](https://github.com/jfWorks/x-server/commit/8b4f910b1e9efbc082571b9ef407d7baf31b6319))
18
+
19
+ ## [1.14.0](https://github.com/jfWorks/x-server/compare/v1.13.1...v1.14.0) (2022-04-26)
20
+
21
+
22
+ ### Features
23
+
24
+ * requestDataSchema ([72970dd](https://github.com/jfWorks/x-server/commit/72970dd63810ab9b3fcdc9390fcde3cfdd4a6013))
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * text -> code ([a8571e3](https://github.com/jfWorks/x-server/commit/a8571e3c99036467f2110b5ab30798c4e6bc42af))
30
+
5
31
  ### [1.13.1](https://github.com/jfWorks/x-server/compare/v1.13.0...v1.13.1) (2022-04-26)
6
32
 
7
33
 
@@ -23,7 +23,7 @@ var _vtils = require("vtils");
23
23
 
24
24
  class BuildUtil {
25
25
  static async build(options) {
26
- var _options$minify, _options$inlineEnvs;
26
+ var _options$minify;
27
27
 
28
28
  const mainFile = _nodePath.default.join(options.cwd, 'src/main.ts');
29
29
 
@@ -75,7 +75,10 @@ class BuildUtil {
75
75
  sourcemap: false,
76
76
  treeShaking: true,
77
77
  banner: {
78
- js: `${`;${(_options$inlineEnvs = options.inlineEnvs) == null ? void 0 : _options$inlineEnvs.map(env => `process.env[${JSON.stringify(env.key)}]=${JSON.stringify(env.value)}`).join(';')}` || ''};`
78
+ js: `;process.env.X_SERVER_ENVS=${JSON.stringify((options.inlineEnvs || []).reduce((res, item) => {
79
+ res[item.key] = item.value;
80
+ return res;
81
+ }, {}))}};`
79
82
  },
80
83
  plugins: [{
81
84
  name: 'extract-assets',
@@ -105,9 +105,18 @@ class EnvUtil {
105
105
  }
106
106
 
107
107
  static async importFile(options) {
108
- for (const env of await EnvUtil.parseFile(options)) {
108
+ const envs = await EnvUtil.parseFile(options);
109
+ const envsObj = {};
110
+
111
+ for (const env of envs) {
112
+ envsObj[env.key] = env.value;
109
113
  process.env[env.key] = env.value;
110
114
  }
115
+
116
+ process.env.X_SERVER_ENVS = JSON.stringify({ // @ts-ignore
117
+ ...JSON.parse(process.env.X_SERVER_ENVS || '{}'),
118
+ ...envsObj
119
+ });
111
120
  }
112
121
 
113
122
  static makeTypes(envs) {
@@ -9,9 +9,12 @@ var _dispose = require("../services/dispose");
9
9
 
10
10
  var _http_error = require("../core/http_error");
11
11
 
12
+ var _validator = require("vtils/validator");
13
+
12
14
  class Handler {
13
15
  constructor(options) {
14
16
  this.options = options;
17
+ this.requestDataSchema = void 0;
15
18
 
16
19
  this.handle = async (data, ctx) => {
17
20
  // 前置 hook
@@ -27,6 +30,19 @@ class Handler {
27
30
  };
28
31
 
29
32
  this.handleHttp = async (data, ctx) => {
33
+ // 请求数据验证
34
+ if (this.requestDataSchema) {
35
+ try {
36
+ data = await this.requestDataSchema.validate(data, {
37
+ strict: true,
38
+ abortEarly: true,
39
+ stripUnknown: true
40
+ });
41
+ } catch (err) {
42
+ throw new _http_error.HttpError.BadRequest(err.message);
43
+ }
44
+ }
45
+
30
46
  let res = await this.options.handle(data, ctx); // 打包返回数据
31
47
 
32
48
  if (this.options.responseDataPack) {
@@ -78,6 +94,10 @@ class Handler {
78
94
  dispose.dispose();
79
95
  });
80
96
  };
97
+
98
+ if (options.requestDataSchema) {
99
+ this.requestDataSchema = options.requestDataSchema(_validator.yup);
100
+ }
81
101
  }
82
102
 
83
103
  static addHook(hook) {
@@ -16,37 +16,38 @@ class CaptchaService {
16
16
  }
17
17
 
18
18
  async generate() {
19
- const text = Math.random().toString().slice(0 - this.options.size);
20
- const token = await _x.x.cache.save(text, this.options.ttl);
19
+ const code = Math.random().toString().slice(0 - this.options.size);
20
+ const token = await _x.x.cache.save(code, this.options.ttl);
21
21
  return {
22
- text: text,
23
- token: token
22
+ token: token,
23
+ code: code
24
24
  };
25
25
  }
26
26
 
27
27
  async generateImage() {
28
- const res = _svgCaptcha.default.create({
28
+ const {
29
+ text: code,
30
+ data: svg
31
+ } = _svgCaptcha.default.create({
29
32
  size: this.options.size
30
33
  });
31
34
 
32
- const token = await _x.x.cache.save(res.text, this.options.ttl);
35
+ const token = await _x.x.cache.save(code, this.options.ttl);
33
36
  return {
34
- text: res.text,
35
- svg: res.data,
36
- token: token
37
+ token: token,
38
+ code: code,
39
+ svg: svg
37
40
  };
38
41
  }
39
42
 
40
43
  async verify(options) {
41
- var _options$text;
42
-
43
- if (((_options$text = options.text) == null ? void 0 : _options$text.length) !== this.options.size) {
44
+ if (options.code.length !== this.options.size) {
44
45
  return false;
45
46
  }
46
47
 
47
- const expectedText = await _x.x.cache.get(options.token);
48
+ const expectedCode = await _x.x.cache.get(options.token);
48
49
  await _x.x.cache.remove(options.token);
49
- return options.text === expectedText;
50
+ return options.code === expectedCode;
50
51
  }
51
52
 
52
53
  }
package/lib/_cjs/x.js CHANGED
@@ -5,8 +5,10 @@ exports.x = void 0;
5
5
 
6
6
  var _dispose = require("./services/dispose");
7
7
 
8
+ const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
8
9
  const x = {
9
- appId: process.env.APP_ID,
10
+ appId: env.APP_ID,
11
+ env: env,
10
12
  register: (...services) => {
11
13
  for (const service of services) {
12
14
  // @ts-ignore
@@ -8,7 +8,7 @@ import path from 'node:path';
8
8
  import { dedent, uniq } from 'vtils';
9
9
  export class BuildUtil {
10
10
  static async build(options) {
11
- var _options$minify, _options$inlineEnvs;
11
+ var _options$minify;
12
12
 
13
13
  const mainFile = path.join(options.cwd, 'src/main.ts');
14
14
  const pkgFile = path.join(options.cwd, 'package.json');
@@ -51,7 +51,10 @@ export class BuildUtil {
51
51
  sourcemap: false,
52
52
  treeShaking: true,
53
53
  banner: {
54
- js: `${`;${(_options$inlineEnvs = options.inlineEnvs) == null ? void 0 : _options$inlineEnvs.map(env => `process.env[${JSON.stringify(env.key)}]=${JSON.stringify(env.value)}`).join(';')}` || ''};`
54
+ js: `;process.env.X_SERVER_ENVS=${JSON.stringify((options.inlineEnvs || []).reduce((res, item) => {
55
+ res[item.key] = item.value;
56
+ return res;
57
+ }, {}))}};`
55
58
  },
56
59
  plugins: [{
57
60
  name: 'extract-assets',
@@ -95,9 +95,18 @@ export class EnvUtil {
95
95
  }
96
96
 
97
97
  static async importFile(options) {
98
- for (const env of await EnvUtil.parseFile(options)) {
98
+ const envs = await EnvUtil.parseFile(options);
99
+ const envsObj = {};
100
+
101
+ for (const env of envs) {
102
+ envsObj[env.key] = env.value;
99
103
  process.env[env.key] = env.value;
100
104
  }
105
+
106
+ process.env.X_SERVER_ENVS = JSON.stringify({ // @ts-ignore
107
+ ...JSON.parse(process.env.X_SERVER_ENVS || '{}'),
108
+ ...envsObj
109
+ });
101
110
  }
102
111
 
103
112
  static makeTypes(envs) {
@@ -1,6 +1,7 @@
1
1
  import type { XHandler } from './types';
2
2
  export declare class Handler<TReqData extends any = void, TResData extends any = void, TReqMethod extends XHandler.Method = XHandler.Method> {
3
3
  readonly options: XHandler.Options<TReqData, TResData, TReqMethod>;
4
+ private requestDataSchema;
4
5
  constructor(options: XHandler.Options<TReqData, TResData, TReqMethod>);
5
6
  handle: XHandler.Handle<TReqData, TResData, TReqMethod>;
6
7
  private handleHttp;
@@ -1,9 +1,11 @@
1
1
  import { DataPacker } from 'vtils';
2
2
  import { DisposeService } from "../services/dispose";
3
3
  import { HttpError } from "../core/http_error";
4
+ import { yup } from 'vtils/validator';
4
5
  export class Handler {
5
6
  constructor(options) {
6
7
  this.options = options;
8
+ this.requestDataSchema = void 0;
7
9
 
8
10
  this.handle = async (data, ctx) => {
9
11
  // 前置 hook
@@ -19,6 +21,19 @@ export class Handler {
19
21
  };
20
22
 
21
23
  this.handleHttp = async (data, ctx) => {
24
+ // 请求数据验证
25
+ if (this.requestDataSchema) {
26
+ try {
27
+ data = await this.requestDataSchema.validate(data, {
28
+ strict: true,
29
+ abortEarly: true,
30
+ stripUnknown: true
31
+ });
32
+ } catch (err) {
33
+ throw new HttpError.BadRequest(err.message);
34
+ }
35
+ }
36
+
22
37
  let res = await this.options.handle(data, ctx); // 打包返回数据
23
38
 
24
39
  if (this.options.responseDataPack) {
@@ -70,6 +85,10 @@ export class Handler {
70
85
  dispose.dispose();
71
86
  });
72
87
  };
88
+
89
+ if (options.requestDataSchema) {
90
+ this.requestDataSchema = options.requestDataSchema(yup);
91
+ }
73
92
  }
74
93
 
75
94
  static addHook(hook) {
@@ -1,7 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  import { BasePlugin } from '../plugins/base';
3
3
  import { BaseService } from '../services/base';
4
- import type { AsyncOrSync, OneOrMore } from 'vtils/types';
4
+ import { yup } from 'vtils/validator';
5
+ import type { AsyncOrSync, OneOrMore, RequiredDeep } from 'vtils/types';
5
6
  import type { DisposeService } from '../services/dispose';
6
7
  import type { Handler } from './handler';
7
8
  import type { IncomingHttpHeaders } from 'http';
@@ -89,6 +90,10 @@ export declare namespace XHandler {
89
90
  * @default false
90
91
  */
91
92
  responseDataPack?: boolean;
93
+ /**
94
+ * 请求数据验证结构
95
+ */
96
+ requestDataSchema?: (_: typeof yup) => yup.GetSchema<RequiredDeep<TReqData>>;
92
97
  /**
93
98
  * 处理器
94
99
  */
@@ -11,17 +11,17 @@ export interface CaptchaOptions {
11
11
  ttl: MsValue;
12
12
  }
13
13
  export interface CaptchaGenerateResult {
14
- text: string;
15
14
  token: string;
15
+ code: string;
16
16
  }
17
17
  export interface CaptchaGenerateImageResult {
18
- text: string;
19
- svg: string;
20
18
  token: string;
19
+ code: string;
20
+ svg: string;
21
21
  }
22
22
  export interface CaptchaVerifyOptions {
23
- text: string;
24
23
  token: string;
24
+ code: string;
25
25
  }
26
26
  export declare class CaptchaService implements BaseService {
27
27
  private options;
@@ -7,36 +7,37 @@ export class CaptchaService {
7
7
  }
8
8
 
9
9
  async generate() {
10
- const text = Math.random().toString().slice(0 - this.options.size);
11
- const token = await x.cache.save(text, this.options.ttl);
10
+ const code = Math.random().toString().slice(0 - this.options.size);
11
+ const token = await x.cache.save(code, this.options.ttl);
12
12
  return {
13
- text: text,
14
- token: token
13
+ token: token,
14
+ code: code
15
15
  };
16
16
  }
17
17
 
18
18
  async generateImage() {
19
- const res = svgCaptcha.create({
19
+ const {
20
+ text: code,
21
+ data: svg
22
+ } = svgCaptcha.create({
20
23
  size: this.options.size
21
24
  });
22
- const token = await x.cache.save(res.text, this.options.ttl);
25
+ const token = await x.cache.save(code, this.options.ttl);
23
26
  return {
24
- text: res.text,
25
- svg: res.data,
26
- token: token
27
+ token: token,
28
+ code: code,
29
+ svg: svg
27
30
  };
28
31
  }
29
32
 
30
33
  async verify(options) {
31
- var _options$text;
32
-
33
- if (((_options$text = options.text) == null ? void 0 : _options$text.length) !== this.options.size) {
34
+ if (options.code.length !== this.options.size) {
34
35
  return false;
35
36
  }
36
37
 
37
- const expectedText = await x.cache.get(options.token);
38
+ const expectedCode = await x.cache.get(options.token);
38
39
  await x.cache.remove(options.token);
39
- return options.text === expectedText;
40
+ return options.code === expectedCode;
40
41
  }
41
42
 
42
43
  }
package/lib/x.d.ts CHANGED
@@ -1,7 +1,9 @@
1
+ /// <reference types="node" />
1
2
  import { BaseService } from './services/base';
2
3
  import './services/dispose';
3
4
  export interface X {
4
5
  readonly appId: string;
6
+ readonly env: NodeJS.ProcessEnv;
5
7
  readonly register: (...services: BaseService[]) => void;
6
8
  }
7
9
  export declare const x: X;
package/lib/x.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { DisposeService } from "./services/dispose";
2
+ const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
2
3
  export const x = {
3
- appId: process.env.APP_ID,
4
+ appId: env.APP_ID,
5
+ env: env,
4
6
  register: (...services) => {
5
7
  for (const service of services) {
6
8
  // @ts-ignore
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.13.1",
3
+ "version": "1.15.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",