@jayfong/x-server 1.15.1 → 1.16.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.16.0](https://github.com/jfWorks/x-server/compare/v1.15.3...v1.16.0) (2022-04-27)
6
+
7
+
8
+ ### Features
9
+
10
+ * add RateLimitService ([49b5edb](https://github.com/jfWorks/x-server/commit/49b5edba451dd355a24a1b5cc4e52ec14a8769e4))
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * X_SERVER_ENVS ([6586cec](https://github.com/jfWorks/x-server/commit/6586cec0eb69336c379a52eb7bb67b29f6694785))
16
+
17
+ ### [1.15.3](https://github.com/jfWorks/x-server/compare/v1.15.2...v1.15.3) (2022-04-26)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * verify ([aeaddd0](https://github.com/jfWorks/x-server/commit/aeaddd0eb307e815ee3d8f92e48dd7755cabce89))
23
+
24
+ ### [1.15.2](https://github.com/jfWorks/x-server/compare/v1.15.1...v1.15.2) (2022-04-26)
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * 验证码应取小写判断 ([36fa3f8](https://github.com/jfWorks/x-server/commit/36fa3f83374777740c28b1775efc4d1f5694a918))
30
+
5
31
  ### [1.15.1](https://github.com/jfWorks/x-server/compare/v1.15.0...v1.15.1) (2022-04-26)
6
32
 
7
33
 
@@ -75,10 +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(';')}` || ''};process.env.X_SERVER_ENVS=${JSON.stringify((options.inlineEnvs || []).reduce((res, item) => {
78
+ js: `${`;${(_options$inlineEnvs = options.inlineEnvs) == null ? void 0 : _options$inlineEnvs.map(env => `process.env[${JSON.stringify(env.key)}]=${JSON.stringify(env.value)}`).join(';')}` || ''};process.env.X_SERVER_ENVS=${JSON.stringify(JSON.stringify((options.inlineEnvs || []).reduce((res, item) => {
79
79
  res[item.key] = item.value;
80
80
  return res;
81
- }, {}))};`
81
+ }, {})))};`
82
82
  },
83
83
  plugins: [{
84
84
  name: 'extract-assets',
package/lib/_cjs/index.js CHANGED
@@ -170,6 +170,14 @@ Object.keys(_mail).forEach(function (key) {
170
170
  exports[key] = _mail[key];
171
171
  });
172
172
 
173
+ var _rate_limit = require("./services/rate_limit");
174
+
175
+ Object.keys(_rate_limit).forEach(function (key) {
176
+ if (key === "default" || key === "__esModule") return;
177
+ if (key in exports && exports[key] === _rate_limit[key]) return;
178
+ exports[key] = _rate_limit[key];
179
+ });
180
+
173
181
  var _redis = require("./services/redis");
174
182
 
175
183
  Object.keys(_redis).forEach(function (key) {
@@ -46,8 +46,13 @@ class CaptchaService {
46
46
  }
47
47
 
48
48
  const expectedCode = await _x.x.cache.get(options.token);
49
+
50
+ if (expectedCode == null) {
51
+ return false;
52
+ }
53
+
49
54
  await _x.x.cache.remove(options.token);
50
- return options.code === expectedCode;
55
+ return options.code.toLowerCase() === expectedCode.toLowerCase();
51
56
  }
52
57
 
53
58
  }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.RateLimitService = void 0;
5
+
6
+ var _x = require("../x");
7
+
8
+ class RateLimitService {
9
+ constructor() {
10
+ this.serviceName = 'rateLimit';
11
+ this.cacheService = _x.x.cache.fork();
12
+ }
13
+
14
+ async limitByCount(options) {
15
+ const remainingCount = await this.cacheService.get(['rateLimit', options.key]);
16
+
17
+ if (remainingCount == null) {
18
+ await this.cacheService.set(['rateLimit', options.key], options.count, options.ttl);
19
+ return options.count;
20
+ }
21
+
22
+ if (remainingCount === 0) {
23
+ return 0;
24
+ }
25
+
26
+ await this.cacheService.decrease(['rateLimit', options.key]);
27
+ return remainingCount - 1;
28
+ }
29
+
30
+ }
31
+
32
+ exports.RateLimitService = RateLimitService;
@@ -51,10 +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(';')}` || ''};process.env.X_SERVER_ENVS=${JSON.stringify((options.inlineEnvs || []).reduce((res, item) => {
54
+ js: `${`;${(_options$inlineEnvs = options.inlineEnvs) == null ? void 0 : _options$inlineEnvs.map(env => `process.env[${JSON.stringify(env.key)}]=${JSON.stringify(env.value)}`).join(';')}` || ''};process.env.X_SERVER_ENVS=${JSON.stringify(JSON.stringify((options.inlineEnvs || []).reduce((res, item) => {
55
55
  res[item.key] = item.value;
56
56
  return res;
57
- }, {}))};`
57
+ }, {})))};`
58
58
  },
59
59
  plugins: [{
60
60
  name: 'extract-assets',
package/lib/index.d.ts CHANGED
@@ -19,6 +19,7 @@ export * from './services/captcha';
19
19
  export * from './services/dispose';
20
20
  export * from './services/jwt';
21
21
  export * from './services/mail';
22
+ export * from './services/rate_limit';
22
23
  export * from './services/redis';
23
24
  export * from './x';
24
25
  export * from '.x/models';
package/lib/index.js CHANGED
@@ -20,6 +20,7 @@ export * from "./services/captcha";
20
20
  export * from "./services/dispose";
21
21
  export * from "./services/jwt";
22
22
  export * from "./services/mail";
23
+ export * from "./services/rate_limit";
23
24
  export * from "./services/redis";
24
25
  export * from "./x"; // @endindex
25
26
  // @ts-ignore
@@ -36,8 +36,13 @@ export class CaptchaService {
36
36
  }
37
37
 
38
38
  const expectedCode = await x.cache.get(options.token);
39
+
40
+ if (expectedCode == null) {
41
+ return false;
42
+ }
43
+
39
44
  await x.cache.remove(options.token);
40
- return options.code === expectedCode;
45
+ return options.code.toLowerCase() === expectedCode.toLowerCase();
41
46
  }
42
47
 
43
48
  }
@@ -0,0 +1,17 @@
1
+ import { BaseService } from './base';
2
+ import { MsValue } from 'vtils/date';
3
+ export interface RateLimitLimitByCountOptions {
4
+ key: string;
5
+ count: number;
6
+ ttl: MsValue;
7
+ }
8
+ export declare class RateLimitService implements BaseService {
9
+ serviceName: string;
10
+ private cacheService;
11
+ limitByCount(options: RateLimitLimitByCountOptions): Promise<number>;
12
+ }
13
+ declare module '../x' {
14
+ interface X {
15
+ rateLimit: RateLimitService;
16
+ }
17
+ }
@@ -0,0 +1,24 @@
1
+ import { x } from "../x";
2
+ export class RateLimitService {
3
+ constructor() {
4
+ this.serviceName = 'rateLimit';
5
+ this.cacheService = x.cache.fork();
6
+ }
7
+
8
+ async limitByCount(options) {
9
+ const remainingCount = await this.cacheService.get(['rateLimit', options.key]);
10
+
11
+ if (remainingCount == null) {
12
+ await this.cacheService.set(['rateLimit', options.key], options.count, options.ttl);
13
+ return options.count;
14
+ }
15
+
16
+ if (remainingCount === 0) {
17
+ return 0;
18
+ }
19
+
20
+ await this.cacheService.decrease(['rateLimit', options.key]);
21
+ return remainingCount - 1;
22
+ }
23
+
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.15.1",
3
+ "version": "1.16.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",