@jayfong/x-server 2.16.4 → 2.16.5

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.
@@ -3,36 +3,51 @@
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
  exports.__esModule = true;
5
5
  exports.CaptchaService = void 0;
6
- var _svgCaptcha = _interopRequireDefault(require("svg-captcha"));
7
6
  var _miniSvgDataUri = _interopRequireDefault(require("mini-svg-data-uri"));
7
+ var _svgCaptcha = _interopRequireDefault(require("svg-captcha"));
8
+ var _vtils = require("vtils");
8
9
  var _x = require("../x");
9
10
  class CaptchaService {
10
- constructor(options) {
11
- this.options = options;
11
+ constructor() {
12
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
+ };
13
24
  }
14
- async generate(scene) {
15
- const code = Math.random().toString().slice(0 - this.options.size);
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
+ }
16
31
  const token = await _x.x.cache.save({
17
- code,
18
- scene
19
- }, this.options.ttl);
32
+ code: code,
33
+ scene: payload.scene
34
+ }, payload.ttl);
20
35
  return {
21
36
  token: token,
22
37
  code: code
23
38
  };
24
39
  }
25
- async generateDataUrl(scene) {
40
+ async generateDataUrl(payload) {
26
41
  const {
27
42
  text: code,
28
43
  data: svg
29
44
  } = _svgCaptcha.default.create({
30
- size: this.options.size
45
+ size: payload.size
31
46
  });
32
47
  const token = await _x.x.cache.save({
33
48
  code,
34
- scene
35
- }, this.options.ttl);
49
+ scene: payload.scene
50
+ }, payload.ttl);
36
51
  const dataUrl = (0, _miniSvgDataUri.default)(svg);
37
52
  return {
38
53
  token: token,
@@ -41,9 +56,6 @@ class CaptchaService {
41
56
  };
42
57
  }
43
58
  async verify(options) {
44
- if (options.code.length !== this.options.size) {
45
- return false;
46
- }
47
59
  const expected = await _x.x.cache.get(options.token);
48
60
  if (expected == null) {
49
61
  return false;
@@ -1,6 +1,11 @@
1
+ import { type MsValue } from 'vtils';
1
2
  import { BaseService } from './base';
2
- import type { MsValue } from 'vtils';
3
- export interface CaptchaServiceOptions {
3
+ export type CaptchaServiceGenerateCharset = '09' | '19' | 'az' | 'AZ' | 'aZ' | '0z' | '0Z' | '1z' | '1Z';
4
+ export interface CaptchaServiceGeneratePayload {
5
+ /**
6
+ * 场景值
7
+ */
8
+ scene: string;
4
9
  /**
5
10
  * 验证码长度
6
11
  */
@@ -9,6 +14,10 @@ export interface CaptchaServiceOptions {
9
14
  * 验证码存活时长
10
15
  */
11
16
  ttl: MsValue;
17
+ /**
18
+ * 字符集
19
+ */
20
+ charset: CaptchaServiceGenerateCharset;
12
21
  }
13
22
  export interface CaptchaServiceGenerateResult {
14
23
  token: string;
@@ -30,11 +39,10 @@ export interface CaptchaServiceVerifyOptions {
30
39
  scene: string;
31
40
  }
32
41
  export declare class CaptchaService implements BaseService {
33
- private options;
34
42
  serviceName: string;
35
- constructor(options: CaptchaServiceOptions);
36
- generate(scene: string): Promise<CaptchaServiceGenerateResult>;
37
- generateDataUrl(scene: string): Promise<CaptchaServiceGenerateDataUrlResult>;
43
+ private charsetMap;
44
+ generate(payload: CaptchaServiceGeneratePayload): Promise<CaptchaServiceGenerateResult>;
45
+ generateDataUrl(payload: CaptchaServiceGeneratePayload): Promise<CaptchaServiceGenerateDataUrlResult>;
38
46
  verify(options: CaptchaServiceVerifyOptions): Promise<boolean>;
39
47
  }
40
48
  declare module '../x' {
@@ -1,33 +1,48 @@
1
- import svgCaptcha from 'svg-captcha';
2
1
  import svgToDataUrl from 'mini-svg-data-uri';
2
+ import svgCaptcha from 'svg-captcha';
3
+ import { sample } from 'vtils';
3
4
  import { x } from "../x";
4
5
  export class CaptchaService {
5
- constructor(options) {
6
- this.options = options;
6
+ constructor() {
7
7
  this.serviceName = 'captcha';
8
+ this.charsetMap = {
9
+ '09': '0123456789'.split(''),
10
+ '19': '123456789'.split(''),
11
+ 'az': 'abcdefghijklmnopqrstuvwxyz'.split(''),
12
+ 'AZ': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
13
+ 'aZ': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
14
+ '0z': '0123456789abcdefghijklmnopqrstuvwxyz'.split(''),
15
+ '0Z': '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
16
+ '1z': '123456789abcdefghijklmnopqrstuvwxyz'.split(''),
17
+ '1Z': '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
18
+ };
8
19
  }
9
- async generate(scene) {
10
- const code = Math.random().toString().slice(0 - this.options.size);
20
+ async generate(payload) {
21
+ const chars = this.charsetMap[payload.charset];
22
+ let code = '';
23
+ for (let i = 0; i < payload.size; i++) {
24
+ code += sample(chars);
25
+ }
11
26
  const token = await x.cache.save({
12
- code,
13
- scene
14
- }, this.options.ttl);
27
+ code: code,
28
+ scene: payload.scene
29
+ }, payload.ttl);
15
30
  return {
16
31
  token: token,
17
32
  code: code
18
33
  };
19
34
  }
20
- async generateDataUrl(scene) {
35
+ async generateDataUrl(payload) {
21
36
  const {
22
37
  text: code,
23
38
  data: svg
24
39
  } = svgCaptcha.create({
25
- size: this.options.size
40
+ size: payload.size
26
41
  });
27
42
  const token = await x.cache.save({
28
43
  code,
29
- scene
30
- }, this.options.ttl);
44
+ scene: payload.scene
45
+ }, payload.ttl);
31
46
  const dataUrl = svgToDataUrl(svg);
32
47
  return {
33
48
  token: token,
@@ -36,9 +51,6 @@ export class CaptchaService {
36
51
  };
37
52
  }
38
53
  async verify(options) {
39
- if (options.code.length !== this.options.size) {
40
- return false;
41
- }
42
54
  const expected = await x.cache.get(options.token);
43
55
  if (expected == null) {
44
56
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.16.4",
3
+ "version": "2.16.5",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",