@jayfong/x-server 1.32.1 → 1.33.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/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
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.33.2](https://github.com/jfWorks/x-server/compare/v1.33.1...v1.33.2) (2022-06-28)
6
+
7
+ ### [1.33.1](https://github.com/jfWorks/x-server/compare/v1.33.0...v1.33.1) (2022-06-12)
8
+
9
+ ## [1.33.0](https://github.com/jfWorks/x-server/compare/v1.32.1...v1.33.0) (2022-06-11)
10
+
11
+
12
+ ### Features
13
+
14
+ * defineTask concurrency ([d8174aa](https://github.com/jfWorks/x-server/commit/d8174aa302e9013610d41265d937512a02412359))
15
+
5
16
  ### [1.32.1](https://github.com/jfWorks/x-server/compare/v1.32.0...v1.32.1) (2022-06-02)
6
17
 
7
18
 
@@ -1,11 +1,11 @@
1
1
  import { x, HttpError } from '@jayfong/x-server'
2
2
  import { PrismaClient } from '@prisma/client'
3
3
 
4
- const prisma = new PrismaClient({
4
+ export const prismaClient = new PrismaClient({
5
5
  rejectOnNotFound: err => new HttpError.NotFound(err.message),
6
6
  })
7
7
 
8
- x.dispose.add(() => prisma.$disconnect())
8
+ x.dispose.add(() => prismaClient.$disconnect())
9
9
 
10
10
  type ModelName =
11
11
  // @index('../.prisma/client/index.d.ts', /(?<=const ModelName:).+?(?=\})/s, /(\S+?):/g, (m, _) => `| '${_.camel(m[1])}'`)
@@ -14,7 +14,7 @@ type ModelName =
14
14
 
15
15
  function makeBaseModel<TModelName extends ModelName>(name: TModelName) {
16
16
  return class BaseModel {
17
- public query = prisma[name]
17
+ public query = prismaClient[name]
18
18
  }
19
19
  }
20
20
 
@@ -18,7 +18,7 @@ function defineTask(options) {
18
18
  },
19
19
  prefix: `${_x.x.appId}_task`
20
20
  });
21
- queue.process(async job => {
21
+ queue.process(options.concurrency || 1, async job => {
22
22
  return options.handle(job.data);
23
23
  });
24
24
  return queue;
@@ -17,16 +17,19 @@ class CaptchaService {
17
17
  this.serviceName = 'captcha';
18
18
  }
19
19
 
20
- async generate() {
20
+ async generate(scene) {
21
21
  const code = Math.random().toString().slice(0 - this.options.size);
22
- const token = await _x.x.cache.save(code, this.options.ttl);
22
+ const token = await _x.x.cache.save({
23
+ code,
24
+ scene
25
+ }, this.options.ttl);
23
26
  return {
24
27
  token: token,
25
28
  code: code
26
29
  };
27
30
  }
28
31
 
29
- async generateImage() {
32
+ async generateImage(scene) {
30
33
  const {
31
34
  text: code,
32
35
  data: svg
@@ -34,7 +37,10 @@ class CaptchaService {
34
37
  size: this.options.size
35
38
  });
36
39
 
37
- const token = await _x.x.cache.save(code, this.options.ttl);
40
+ const token = await _x.x.cache.save({
41
+ code,
42
+ scene
43
+ }, this.options.ttl);
38
44
  return {
39
45
  token: token,
40
46
  code: code,
@@ -42,12 +48,12 @@ class CaptchaService {
42
48
  };
43
49
  }
44
50
 
45
- async generateDataUrl() {
51
+ async generateDataUrl(scene) {
46
52
  const {
47
53
  token,
48
54
  code,
49
55
  svg
50
- } = await this.generateImage();
56
+ } = await this.generateImage(scene);
51
57
  const dataUrl = (0, _miniSvgDataUri.default)(svg);
52
58
  return {
53
59
  token: token,
@@ -61,14 +67,14 @@ class CaptchaService {
61
67
  return false;
62
68
  }
63
69
 
64
- const expectedCode = await _x.x.cache.get(options.token);
70
+ const expected = await _x.x.cache.get(options.token);
65
71
 
66
- if (expectedCode == null) {
72
+ if (expected == null) {
67
73
  return false;
68
74
  }
69
75
 
70
76
  await _x.x.cache.remove(options.token);
71
- return options.code.toLowerCase() === expectedCode.toLowerCase();
77
+ return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase();
72
78
  }
73
79
 
74
80
  }
@@ -1,11 +1,11 @@
1
1
  import { x, HttpError } from '@jayfong/x-server'
2
2
  import { PrismaClient } from '@prisma/client'
3
3
 
4
- const prisma = new PrismaClient({
4
+ export const prismaClient = new PrismaClient({
5
5
  rejectOnNotFound: err => new HttpError.NotFound(err.message),
6
6
  })
7
7
 
8
- x.dispose.add(() => prisma.$disconnect())
8
+ x.dispose.add(() => prismaClient.$disconnect())
9
9
 
10
10
  type ModelName =
11
11
  // @index('../.prisma/client/index.d.ts', /(?<=const ModelName:).+?(?=\})/s, /(\S+?):/g, (m, _) => `| '${_.camel(m[1])}'`)
@@ -14,7 +14,7 @@ type ModelName =
14
14
 
15
15
  function makeBaseModel<TModelName extends ModelName>(name: TModelName) {
16
16
  return class BaseModel {
17
- public query = prisma[name]
17
+ public query = prismaClient[name]
18
18
  }
19
19
  }
20
20
 
@@ -9,7 +9,7 @@ export function defineTask(options) {
9
9
  },
10
10
  prefix: `${x.appId}_task`
11
11
  });
12
- queue.process(async job => {
12
+ queue.process(options.concurrency || 1, async job => {
13
13
  return options.handle(job.data);
14
14
  });
15
15
  return queue;
@@ -139,6 +139,12 @@ export declare namespace XTask {
139
139
  * 任务名称
140
140
  */
141
141
  name: string;
142
+ /**
143
+ * 并发数量
144
+ *
145
+ * @default 1
146
+ */
147
+ concurrency?: number;
142
148
  /**
143
149
  * 处理器
144
150
  */
@@ -27,14 +27,15 @@ export interface CaptchaServiceGenerateDataUrlResult {
27
27
  export interface CaptchaServiceVerifyOptions {
28
28
  token: string;
29
29
  code: string;
30
+ scene: string;
30
31
  }
31
32
  export declare class CaptchaService implements BaseService {
32
33
  private options;
33
34
  serviceName: string;
34
35
  constructor(options: CaptchaServiceOptions);
35
- generate(): Promise<CaptchaServiceGenerateResult>;
36
- generateImage(): Promise<CaptchaServiceGenerateImageResult>;
37
- generateDataUrl(): Promise<CaptchaServiceGenerateDataUrlResult>;
36
+ generate(scene: string): Promise<CaptchaServiceGenerateResult>;
37
+ generateImage(scene: string): Promise<CaptchaServiceGenerateImageResult>;
38
+ generateDataUrl(scene: string): Promise<CaptchaServiceGenerateDataUrlResult>;
38
39
  verify(options: CaptchaServiceVerifyOptions): Promise<boolean>;
39
40
  }
40
41
  declare module '../x' {
@@ -7,23 +7,29 @@ export class CaptchaService {
7
7
  this.serviceName = 'captcha';
8
8
  }
9
9
 
10
- async generate() {
10
+ async generate(scene) {
11
11
  const code = Math.random().toString().slice(0 - this.options.size);
12
- const token = await x.cache.save(code, this.options.ttl);
12
+ const token = await x.cache.save({
13
+ code,
14
+ scene
15
+ }, this.options.ttl);
13
16
  return {
14
17
  token: token,
15
18
  code: code
16
19
  };
17
20
  }
18
21
 
19
- async generateImage() {
22
+ async generateImage(scene) {
20
23
  const {
21
24
  text: code,
22
25
  data: svg
23
26
  } = svgCaptcha.create({
24
27
  size: this.options.size
25
28
  });
26
- const token = await x.cache.save(code, this.options.ttl);
29
+ const token = await x.cache.save({
30
+ code,
31
+ scene
32
+ }, this.options.ttl);
27
33
  return {
28
34
  token: token,
29
35
  code: code,
@@ -31,12 +37,12 @@ export class CaptchaService {
31
37
  };
32
38
  }
33
39
 
34
- async generateDataUrl() {
40
+ async generateDataUrl(scene) {
35
41
  const {
36
42
  token,
37
43
  code,
38
44
  svg
39
- } = await this.generateImage();
45
+ } = await this.generateImage(scene);
40
46
  const dataUrl = svgToDataUrl(svg);
41
47
  return {
42
48
  token: token,
@@ -50,14 +56,14 @@ export class CaptchaService {
50
56
  return false;
51
57
  }
52
58
 
53
- const expectedCode = await x.cache.get(options.token);
59
+ const expected = await x.cache.get(options.token);
54
60
 
55
- if (expectedCode == null) {
61
+ if (expected == null) {
56
62
  return false;
57
63
  }
58
64
 
59
65
  await x.cache.remove(options.token);
60
- return options.code.toLowerCase() === expectedCode.toLowerCase();
66
+ return expected.scene === options.scene && options.code.toLowerCase() === expected.code.toLowerCase();
61
67
  }
62
68
 
63
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.32.1",
3
+ "version": "1.33.2",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",