@jayfong/x-server 1.15.3 → 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 +12 -0
- package/lib/_cjs/cli/build_util.js +2 -2
- package/lib/_cjs/index.js +8 -0
- package/lib/_cjs/services/rate_limit.js +32 -0
- package/lib/cli/build_util.js +2 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/services/rate_limit.d.ts +17 -0
- package/lib/services/rate_limit.js +24 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
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
|
+
|
|
5
17
|
### [1.15.3](https://github.com/jfWorks/x-server/compare/v1.15.2...v1.15.3) (2022-04-26)
|
|
6
18
|
|
|
7
19
|
|
|
@@ -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) {
|
|
@@ -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;
|
package/lib/cli/build_util.js
CHANGED
|
@@ -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
|
|
@@ -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
|
+
}
|