@jayfong/x-server 1.15.2 → 1.16.1

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.1](https://github.com/jfWorks/x-server/compare/v1.16.0...v1.16.1) (2022-04-27)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * RateLimitService ([20a489e](https://github.com/jfWorks/x-server/commit/20a489e3cb119ba055d4c77983354f607f61af0b))
11
+
12
+ ## [1.16.0](https://github.com/jfWorks/x-server/compare/v1.15.3...v1.16.0) (2022-04-27)
13
+
14
+
15
+ ### Features
16
+
17
+ * add RateLimitService ([49b5edb](https://github.com/jfWorks/x-server/commit/49b5edba451dd355a24a1b5cc4e52ec14a8769e4))
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * X_SERVER_ENVS ([6586cec](https://github.com/jfWorks/x-server/commit/6586cec0eb69336c379a52eb7bb67b29f6694785))
23
+
24
+ ### [1.15.3](https://github.com/jfWorks/x-server/compare/v1.15.2...v1.15.3) (2022-04-26)
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * verify ([aeaddd0](https://github.com/jfWorks/x-server/commit/aeaddd0eb307e815ee3d8f92e48dd7755cabce89))
30
+
5
31
  ### [1.15.2](https://github.com/jfWorks/x-server/compare/v1.15.1...v1.15.2) (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,6 +46,11 @@ 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
55
  return options.code.toLowerCase() === expectedCode.toLowerCase();
51
56
  }
@@ -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
+ }
12
+
13
+ async limitByCount(options) {
14
+ const cacheKey = `rateLimit_${options.key}`;
15
+ const remainingCount = await _x.x.cache.get(cacheKey);
16
+
17
+ if (remainingCount == null) {
18
+ await _x.x.cache.set(cacheKey, options.count, options.ttl);
19
+ return options.count;
20
+ }
21
+
22
+ if (remainingCount === 0) {
23
+ return 0;
24
+ }
25
+
26
+ await _x.x.cache.decrease(cacheKey);
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,6 +36,11 @@ 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
45
  return options.code.toLowerCase() === expectedCode.toLowerCase();
41
46
  }
@@ -0,0 +1,16 @@
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
+ limitByCount(options: RateLimitLimitByCountOptions): Promise<number>;
11
+ }
12
+ declare module '../x' {
13
+ interface X {
14
+ rateLimit: RateLimitService;
15
+ }
16
+ }
@@ -0,0 +1,24 @@
1
+ import { x } from "../x";
2
+ export class RateLimitService {
3
+ constructor() {
4
+ this.serviceName = 'rateLimit';
5
+ }
6
+
7
+ async limitByCount(options) {
8
+ const cacheKey = `rateLimit_${options.key}`;
9
+ const remainingCount = await x.cache.get(cacheKey);
10
+
11
+ if (remainingCount == null) {
12
+ await x.cache.set(cacheKey, options.count, options.ttl);
13
+ return options.count;
14
+ }
15
+
16
+ if (remainingCount === 0) {
17
+ return 0;
18
+ }
19
+
20
+ await x.cache.decrease(cacheKey);
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.2",
3
+ "version": "1.16.1",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",