@jayfong/x-server 1.15.3 → 1.17.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 +26 -0
- package/lib/_cjs/cli/build_util.js +2 -2
- package/lib/_cjs/core/server.js +8 -0
- 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/core/server.js +6 -0
- package/lib/core/types.d.ts +4 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/services/rate_limit.d.ts +16 -0
- package/lib/services/rate_limit.js +24 -0
- package/package.json +1 -1
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.17.0](https://github.com/jfWorks/x-server/compare/v1.16.1...v1.17.0) (2022-04-27)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* add ctx.url ([1092009](https://github.com/jfWorks/x-server/commit/10920094646e975e74dc0339ecc290e257f3ca52))
|
|
11
|
+
|
|
12
|
+
### [1.16.1](https://github.com/jfWorks/x-server/compare/v1.16.0...v1.16.1) (2022-04-27)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* RateLimitService ([20a489e](https://github.com/jfWorks/x-server/commit/20a489e3cb119ba055d4c77983354f607f61af0b))
|
|
18
|
+
|
|
19
|
+
## [1.16.0](https://github.com/jfWorks/x-server/compare/v1.15.3...v1.16.0) (2022-04-27)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
* add RateLimitService ([49b5edb](https://github.com/jfWorks/x-server/commit/49b5edba451dd355a24a1b5cc4e52ec14a8769e4))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Bug Fixes
|
|
28
|
+
|
|
29
|
+
* X_SERVER_ENVS ([6586cec](https://github.com/jfWorks/x-server/commit/6586cec0eb69336c379a52eb7bb67b29f6694785))
|
|
30
|
+
|
|
5
31
|
### [1.15.3](https://github.com/jfWorks/x-server/compare/v1.15.2...v1.15.3) (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/core/server.js
CHANGED
|
@@ -87,13 +87,20 @@ class Server {
|
|
|
87
87
|
const handlerMethod = handlerOptions.requestMethod || 'POST';
|
|
88
88
|
const isWS = handlerMethod === 'WS';
|
|
89
89
|
const serverMethod = isWS ? 'GET' : _http_method.HandlerMethodToHttpMethod[handlerMethod];
|
|
90
|
+
|
|
91
|
+
const appUrl = _x.x.env.APP_URL.replace(/\/+$/, '');
|
|
92
|
+
|
|
90
93
|
this.fastify.route({
|
|
91
94
|
method: serverMethod,
|
|
92
95
|
url: item.path,
|
|
93
96
|
websocket: isWS,
|
|
94
97
|
handler: async (req, res) => {
|
|
98
|
+
const url = `${appUrl}${// 结构:/test/sss?x=2
|
|
99
|
+
req.url}`;
|
|
100
|
+
|
|
95
101
|
if (isWS) {
|
|
96
102
|
await item.handler.handle(undefined, {
|
|
103
|
+
url: url,
|
|
97
104
|
headers: req.headers,
|
|
98
105
|
redirect: undefined,
|
|
99
106
|
ws: req
|
|
@@ -120,6 +127,7 @@ class Server {
|
|
|
120
127
|
...req.body,
|
|
121
128
|
...files
|
|
122
129
|
}, {
|
|
130
|
+
url: url,
|
|
123
131
|
headers: req.headers,
|
|
124
132
|
redirect: url => res.redirect(url),
|
|
125
133
|
ws: undefined
|
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
|
+
}
|
|
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;
|
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/core/server.js
CHANGED
|
@@ -74,13 +74,18 @@ export class Server {
|
|
|
74
74
|
const handlerMethod = handlerOptions.requestMethod || 'POST';
|
|
75
75
|
const isWS = handlerMethod === 'WS';
|
|
76
76
|
const serverMethod = isWS ? 'GET' : HandlerMethodToHttpMethod[handlerMethod];
|
|
77
|
+
const appUrl = x.env.APP_URL.replace(/\/+$/, '');
|
|
77
78
|
this.fastify.route({
|
|
78
79
|
method: serverMethod,
|
|
79
80
|
url: item.path,
|
|
80
81
|
websocket: isWS,
|
|
81
82
|
handler: async (req, res) => {
|
|
83
|
+
const url = `${appUrl}${// 结构:/test/sss?x=2
|
|
84
|
+
req.url}`;
|
|
85
|
+
|
|
82
86
|
if (isWS) {
|
|
83
87
|
await item.handler.handle(undefined, {
|
|
88
|
+
url: url,
|
|
84
89
|
headers: req.headers,
|
|
85
90
|
redirect: undefined,
|
|
86
91
|
ws: req
|
|
@@ -107,6 +112,7 @@ export class Server {
|
|
|
107
112
|
...req.body,
|
|
108
113
|
...files
|
|
109
114
|
}, {
|
|
115
|
+
url: url,
|
|
110
116
|
headers: req.headers,
|
|
111
117
|
redirect: url => res.redirect(url),
|
|
112
118
|
ws: undefined
|
package/lib/core/types.d.ts
CHANGED
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,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
|
+
}
|