@jayfong/x-server 1.20.1 → 1.23.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,27 @@
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.23.0](https://github.com/jfWorks/x-server/compare/v1.22.0...v1.23.0) (2022-05-01)
6
+
7
+
8
+ ### Features
9
+
10
+ * **captcha:** add generateDataUrl ([54205df](https://github.com/jfWorks/x-server/commit/54205df24d0cc397029d8c66ea17020d7cb5c5fd))
11
+
12
+ ## [1.22.0](https://github.com/jfWorks/x-server/compare/v1.21.0...v1.22.0) (2022-05-01)
13
+
14
+
15
+ ### Features
16
+
17
+ * **cors:** allowAll ([5ce4b5d](https://github.com/jfWorks/x-server/commit/5ce4b5d9183f68a32173d77de569f00c5409ccdc))
18
+
19
+ ## [1.21.0](https://github.com/jfWorks/x-server/compare/v1.20.1...v1.21.0) (2022-04-29)
20
+
21
+
22
+ ### Features
23
+
24
+ * export * from '@prisma/client' ([3e6c707](https://github.com/jfWorks/x-server/commit/3e6c707d55bad0cbb719f1b76bdd306ef0527ba1))
25
+
5
26
  ### [1.20.1](https://github.com/jfWorks/x-server/compare/v1.20.0...v1.20.1) (2022-04-27)
6
27
 
7
28
 
@@ -20,3 +20,5 @@ function makeBaseModel<TModelName extends ModelName>(name: TModelName) {
20
20
 
21
21
  // @index('../.prisma/client/index.d.ts', /(?<=const ModelName:).+?(?=\})/s, /(\S+?):/g, (m, _) => `export const ${_.pascal(m[1])}BaseModel = makeBaseModel('${_.camel(m[1])}')`)
22
22
  // @endindex
23
+
24
+ export * from '@prisma/client'
@@ -21,9 +21,10 @@ class CorsPlugin {
21
21
 
22
22
  register(fastify) {
23
23
  const allows = this.options.allow.map(item => ({
24
- type: item.startsWith('*.') ? 'endsWith' : 'equal',
24
+ type: item === '*' ? 'all' : item.startsWith('*.') ? 'endsWith' : 'equal',
25
25
  domain: item.startsWith('*.') ? item.replace('*.', '.') : item
26
26
  }));
27
+ const allowAll = allows.length === 1 && allows[0].type === 'all';
27
28
  const check = (0, _vtils.memoize)(origin => {
28
29
  let i = origin.indexOf(':');
29
30
  let j = origin.indexOf(':', i + 1);
@@ -34,7 +35,7 @@ class CorsPlugin {
34
35
  return pass;
35
36
  });
36
37
  fastify.register(_fastifyCors.default, {
37
- origin: (origin, cb) => cb(null, origin ? check(origin) : true),
38
+ origin: (origin, cb) => allowAll ? cb(null, true) : cb(null, origin ? check(origin) : true),
38
39
  maxAge: (0, _date.ms)(this.options.ttl, true)
39
40
  });
40
41
  }
@@ -7,6 +7,8 @@ exports.CaptchaService = void 0;
7
7
 
8
8
  var _svgCaptcha = _interopRequireDefault(require("svg-captcha"));
9
9
 
10
+ var _miniSvgDataUri = _interopRequireDefault(require("mini-svg-data-uri"));
11
+
10
12
  var _x = require("../x");
11
13
 
12
14
  class CaptchaService {
@@ -40,6 +42,20 @@ class CaptchaService {
40
42
  };
41
43
  }
42
44
 
45
+ async generateDataUrl() {
46
+ const {
47
+ token,
48
+ code,
49
+ svg
50
+ } = await this.generateImage();
51
+ const dataUrl = (0, _miniSvgDataUri.default)(svg);
52
+ return {
53
+ token: token,
54
+ code: code,
55
+ dataUrl: dataUrl
56
+ };
57
+ }
58
+
43
59
  async verify(options) {
44
60
  if (options.code.length !== this.options.size) {
45
61
  return false;
@@ -20,3 +20,5 @@ function makeBaseModel<TModelName extends ModelName>(name: TModelName) {
20
20
 
21
21
  // @index('../.prisma/client/index.d.ts', /(?<=const ModelName:).+?(?=\})/s, /(\S+?):/g, (m, _) => `export const ${_.pascal(m[1])}BaseModel = makeBaseModel('${_.camel(m[1])}')`)
22
22
  // @endindex
23
+
24
+ export * from '@prisma/client'
@@ -12,9 +12,10 @@ export class CorsPlugin {
12
12
 
13
13
  register(fastify) {
14
14
  const allows = this.options.allow.map(item => ({
15
- type: item.startsWith('*.') ? 'endsWith' : 'equal',
15
+ type: item === '*' ? 'all' : item.startsWith('*.') ? 'endsWith' : 'equal',
16
16
  domain: item.startsWith('*.') ? item.replace('*.', '.') : item
17
17
  }));
18
+ const allowAll = allows.length === 1 && allows[0].type === 'all';
18
19
  const check = memoize(origin => {
19
20
  let i = origin.indexOf(':');
20
21
  let j = origin.indexOf(':', i + 1);
@@ -25,7 +26,7 @@ export class CorsPlugin {
25
26
  return pass;
26
27
  });
27
28
  fastify.register(FastifyCors, {
28
- origin: (origin, cb) => cb(null, origin ? check(origin) : true),
29
+ origin: (origin, cb) => allowAll ? cb(null, true) : cb(null, origin ? check(origin) : true),
29
30
  maxAge: ms(this.options.ttl, true)
30
31
  });
31
32
  }
@@ -19,6 +19,11 @@ export interface CaptchaGenerateImageResult {
19
19
  code: string;
20
20
  svg: string;
21
21
  }
22
+ export interface CaptchaGenerateDataUrlResult {
23
+ token: string;
24
+ code: string;
25
+ dataUrl: string;
26
+ }
22
27
  export interface CaptchaVerifyOptions {
23
28
  token: string;
24
29
  code: string;
@@ -29,6 +34,7 @@ export declare class CaptchaService implements BaseService {
29
34
  constructor(options: CaptchaOptions);
30
35
  generate(): Promise<CaptchaGenerateResult>;
31
36
  generateImage(): Promise<CaptchaGenerateImageResult>;
37
+ generateDataUrl(): Promise<CaptchaGenerateDataUrlResult>;
32
38
  verify(options: CaptchaVerifyOptions): Promise<boolean>;
33
39
  }
34
40
  declare module '../x' {
@@ -1,4 +1,5 @@
1
1
  import svgCaptcha from 'svg-captcha';
2
+ import svgToDataUrl from 'mini-svg-data-uri';
2
3
  import { x } from "../x";
3
4
  export class CaptchaService {
4
5
  constructor(options) {
@@ -30,6 +31,20 @@ export class CaptchaService {
30
31
  };
31
32
  }
32
33
 
34
+ async generateDataUrl() {
35
+ const {
36
+ token,
37
+ code,
38
+ svg
39
+ } = await this.generateImage();
40
+ const dataUrl = svgToDataUrl(svg);
41
+ return {
42
+ token: token,
43
+ code: code,
44
+ dataUrl: dataUrl
45
+ };
46
+ }
47
+
33
48
  async verify(options) {
34
49
  if (options.code.length !== this.options.size) {
35
50
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.20.1",
3
+ "version": "1.23.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",
@@ -53,6 +53,7 @@
53
53
  "http-errors": "^1.8.1",
54
54
  "ioredis": "^4.28.5",
55
55
  "jsonwebtoken": "^8.5.1",
56
+ "mini-svg-data-uri": "^1.4.4",
56
57
  "node-ssh": "^12.0.4",
57
58
  "nodemailer": "^6.7.3",
58
59
  "pino-pretty": "^7.6.1",