@midwayjs/captcha 4.0.0-alpha.1 → 4.0.0-beta.2

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/README.md CHANGED
@@ -6,5 +6,5 @@ Document: [https://midwayjs.org](https://midwayjs.org)
6
6
 
7
7
  ## License
8
8
 
9
- [MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
9
+ [MIT]((https://github.com/midwayjs/midway/blob/master/LICENSE))
10
10
 
@@ -1,25 +1,21 @@
1
- interface BaseCaptchaOptions {
2
- size?: number;
3
- noise?: number;
4
- width?: number;
5
- height?: number;
1
+ import { ConfigObject } from 'svg-captcha-fixed';
2
+ export interface CaptchaCacheOptions {
3
+ expirationTime?: number;
4
+ idPrefix?: string;
6
5
  }
7
- export interface CaptchaOptions extends BaseCaptchaOptions {
8
- default?: BaseCaptchaOptions;
6
+ export interface CaptchaOptions extends CaptchaCacheOptions {
7
+ default?: ConfigObject;
9
8
  image?: ImageCaptchaOptions;
10
9
  formula?: FormulaCaptchaOptions;
11
10
  text?: TextCaptchaOptions;
12
- expirationTime?: number;
13
- idPrefix?: string;
14
11
  }
15
- export interface ImageCaptchaOptions extends BaseCaptchaOptions {
12
+ export interface ImageCaptchaOptions extends ConfigObject {
16
13
  type?: 'number' | 'letter' | 'mixed';
17
14
  }
18
- export interface FormulaCaptchaOptions extends BaseCaptchaOptions {
15
+ export interface FormulaCaptchaOptions extends ConfigObject {
19
16
  }
20
17
  export interface TextCaptchaOptions {
21
18
  size?: number;
22
19
  type?: 'number' | 'letter' | 'mixed';
23
20
  }
24
- export {};
25
21
  //# sourceMappingURL=interface.d.ts.map
package/dist/service.d.ts CHANGED
@@ -1,22 +1,22 @@
1
1
  import { MidwayCache } from '@midwayjs/cache-manager';
2
- import { FormulaCaptchaOptions, ImageCaptchaOptions, TextCaptchaOptions, CaptchaOptions } from './interface';
2
+ import { FormulaCaptchaOptions, ImageCaptchaOptions, TextCaptchaOptions, CaptchaOptions, CaptchaCacheOptions } from './interface';
3
3
  export declare class CaptchaService {
4
4
  protected captchaCaching: MidwayCache;
5
5
  protected captcha: CaptchaOptions;
6
- image(options?: ImageCaptchaOptions): Promise<{
6
+ image(options?: ImageCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{
7
7
  id: string;
8
8
  imageBase64: string;
9
9
  }>;
10
- formula(options?: FormulaCaptchaOptions): Promise<{
10
+ formula(options?: FormulaCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{
11
11
  id: string;
12
12
  imageBase64: string;
13
13
  }>;
14
- text(options?: TextCaptchaOptions): Promise<{
14
+ text(options?: TextCaptchaOptions, cacheOption?: CaptchaCacheOptions): Promise<{
15
15
  id: string;
16
16
  text: string;
17
17
  }>;
18
- set(text: string): Promise<string>;
19
- check(id: string, value: string): Promise<boolean>;
18
+ set(text: string, cacheOptions?: CaptchaCacheOptions): Promise<string>;
19
+ check(id: string, value: string, cacheOptions?: CaptchaCacheOptions): Promise<boolean>;
20
20
  private getStoreId;
21
21
  }
22
22
  //# sourceMappingURL=service.d.ts.map
package/dist/service.js CHANGED
@@ -12,48 +12,49 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CaptchaService = void 0;
13
13
  const core_1 = require("@midwayjs/core");
14
14
  const cache_manager_1 = require("@midwayjs/cache-manager");
15
- const svgCaptcha = require("svg-captcha");
15
+ const svgCaptcha = require("svg-captcha-fixed");
16
16
  const svgBase64 = require("mini-svg-data-uri");
17
17
  const nanoid_1 = require("nanoid");
18
18
  const constants_1 = require("./constants");
19
+ const DEFAULT_IMAGE_IGNORE_CHARS = {
20
+ letter: constants_1.numbers,
21
+ number: constants_1.letters,
22
+ };
19
23
  let CaptchaService = class CaptchaService {
20
- async image(options) {
21
- const { width, height, type, size, noise } = Object.assign({}, this.captcha, this.captcha.default, this.captcha.image, options);
22
- let ignoreChars = '';
23
- switch (type) {
24
- case 'letter':
25
- ignoreChars = constants_1.numbers;
26
- break;
27
- case 'number':
28
- ignoreChars = constants_1.letters;
29
- break;
30
- }
24
+ async image(options, cacheOption) {
25
+ // const { expirationTime, idPrefix } = this.captcha;
26
+ const { type, ...others } = {
27
+ ...this.captcha.default,
28
+ ...this.captcha.image,
29
+ ...options,
30
+ };
31
31
  const { data, text } = svgCaptcha.create({
32
- ignoreChars,
33
- width,
34
- height,
35
- size,
36
- noise,
32
+ ignoreChars: DEFAULT_IMAGE_IGNORE_CHARS[type] ?? '',
33
+ ...others,
37
34
  });
38
- const id = await this.set(text);
35
+ const id = await this.set(text, cacheOption);
39
36
  const imageBase64 = svgBase64(data);
40
37
  return { id, imageBase64 };
41
38
  }
42
- async formula(options) {
43
- const { width, height, noise } = Object.assign({}, this.captcha, this.captcha.default, this.captcha.formula, options);
44
- const { data, text } = svgCaptcha.createMathExpr({
45
- width,
46
- height,
47
- noise,
48
- });
49
- const id = await this.set(text);
39
+ async formula(options, cacheOption) {
40
+ const formulaCaptchaOptions = {
41
+ ...this.captcha.default,
42
+ ...this.captcha.formula,
43
+ ...options,
44
+ };
45
+ const { data, text } = svgCaptcha.createMathExpr(formulaCaptchaOptions);
46
+ const id = await this.set(text, cacheOption);
50
47
  const imageBase64 = svgBase64(data);
51
48
  return { id, imageBase64 };
52
49
  }
53
- async text(options) {
54
- const textOptions = Object.assign({}, this.captcha, this.captcha.default, this.captcha.text, options);
50
+ async text(options, cacheOption) {
51
+ const { type, ...textOptions } = {
52
+ ...this.captcha.default,
53
+ ...this.captcha.text,
54
+ ...options,
55
+ };
55
56
  let chars = '';
56
- switch (textOptions.type) {
57
+ switch (type) {
57
58
  case 'letter':
58
59
  chars = constants_1.letters;
59
60
  break;
@@ -68,19 +69,19 @@ let CaptchaService = class CaptchaService {
68
69
  while (textOptions.size--) {
69
70
  text += chars[Math.floor(Math.random() * chars.length)];
70
71
  }
71
- const id = await this.set(text);
72
+ const id = await this.set(text, cacheOption);
72
73
  return { id, text };
73
74
  }
74
- async set(text) {
75
+ async set(text, cacheOptions) {
75
76
  const id = (0, nanoid_1.nanoid)();
76
- await this.captchaCaching.set(this.getStoreId(id), (text || '').toLowerCase(), this.captcha.expirationTime * 1000);
77
+ await this.captchaCaching.set(this.getStoreId(id, cacheOptions), (text || '').toLowerCase(), (cacheOptions?.expirationTime ?? this.captcha.expirationTime) * 1000);
77
78
  return id;
78
79
  }
79
- async check(id, value) {
80
+ async check(id, value, cacheOptions) {
80
81
  if (!id || !value) {
81
82
  return false;
82
83
  }
83
- const storeId = this.getStoreId(id);
84
+ const storeId = this.getStoreId(id, cacheOptions);
84
85
  const storedValue = await this.captchaCaching.get(storeId);
85
86
  if (value.toLowerCase() !== storedValue) {
86
87
  return false;
@@ -88,11 +89,12 @@ let CaptchaService = class CaptchaService {
88
89
  await this.captchaCaching.del(storeId);
89
90
  return true;
90
91
  }
91
- getStoreId(id) {
92
- if (!this.captcha.idPrefix) {
92
+ getStoreId(id, cacheOptions) {
93
+ const idPrefix = cacheOptions?.idPrefix ?? this.captcha.idPrefix;
94
+ if (!idPrefix) {
93
95
  return id;
94
96
  }
95
- return `${this.captcha.idPrefix}:${id}`;
97
+ return `${idPrefix}:${id}`;
96
98
  }
97
99
  };
98
100
  exports.CaptchaService = CaptchaService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/captcha",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-beta.2",
4
4
  "description": "Midway Component for Captcha(Verification Code)",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -18,19 +18,19 @@
18
18
  "index.d.ts"
19
19
  ],
20
20
  "engines": {
21
- "node": ">=12"
21
+ "node": ">=20"
22
22
  },
23
23
  "license": "MIT",
24
24
  "dependencies": {
25
- "@midwayjs/cache-manager": "^4.0.0-alpha.1",
25
+ "@midwayjs/cache-manager": "^4.0.0-beta.2",
26
26
  "mini-svg-data-uri": "1.4.4",
27
- "nanoid": "3.3.8",
28
- "svg-captcha": "1.4.0"
27
+ "nanoid": "3.3.11",
28
+ "svg-captcha-fixed": "1.5.2"
29
29
  },
30
30
  "devDependencies": {
31
- "@midwayjs/core": "^4.0.0-alpha.1",
32
- "@midwayjs/koa": "^4.0.0-alpha.1",
33
- "@midwayjs/mock": "^4.0.0-alpha.1"
31
+ "@midwayjs/core": "^4.0.0-beta.2",
32
+ "@midwayjs/koa": "^4.0.0-beta.2",
33
+ "@midwayjs/mock": "^4.0.0-beta.2"
34
34
  },
35
- "gitHead": "14bb4da91805a1cf52f190c0d37a74b395dd6372"
35
+ "gitHead": "53bfef4c5279da5f09025e4610bdbf64f94f60bd"
36
36
  }