@jayfong/x-server 1.16.0 → 1.18.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,27 @@
|
|
|
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.18.0](https://github.com/jfWorks/x-server/compare/v1.17.0...v1.18.0) (2022-04-27)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* setHeader ([07596ba](https://github.com/jfWorks/x-server/commit/07596ba1c83f6a7583e16b969332936339bd06a8))
|
|
11
|
+
|
|
12
|
+
## [1.17.0](https://github.com/jfWorks/x-server/compare/v1.16.1...v1.17.0) (2022-04-27)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* add ctx.url ([1092009](https://github.com/jfWorks/x-server/commit/10920094646e975e74dc0339ecc290e257f3ca52))
|
|
18
|
+
|
|
19
|
+
### [1.16.1](https://github.com/jfWorks/x-server/compare/v1.16.0...v1.16.1) (2022-04-27)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Bug Fixes
|
|
23
|
+
|
|
24
|
+
* RateLimitService ([20a489e](https://github.com/jfWorks/x-server/commit/20a489e3cb119ba055d4c77983354f607f61af0b))
|
|
25
|
+
|
|
5
26
|
## [1.16.0](https://github.com/jfWorks/x-server/compare/v1.15.3...v1.16.0) (2022-04-27)
|
|
6
27
|
|
|
7
28
|
|
package/lib/_cjs/core/server.js
CHANGED
|
@@ -87,14 +87,22 @@ 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,
|
|
105
|
+
setHeader: res.header,
|
|
98
106
|
redirect: undefined,
|
|
99
107
|
ws: req
|
|
100
108
|
});
|
|
@@ -120,7 +128,9 @@ class Server {
|
|
|
120
128
|
...req.body,
|
|
121
129
|
...files
|
|
122
130
|
}, {
|
|
131
|
+
url: url,
|
|
123
132
|
headers: req.headers,
|
|
133
|
+
setHeader: res.header,
|
|
124
134
|
redirect: url => res.redirect(url),
|
|
125
135
|
ws: undefined
|
|
126
136
|
});
|
|
@@ -8,14 +8,14 @@ var _x = require("../x");
|
|
|
8
8
|
class RateLimitService {
|
|
9
9
|
constructor() {
|
|
10
10
|
this.serviceName = 'rateLimit';
|
|
11
|
-
this.cacheService = _x.x.cache.fork();
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
async limitByCount(options) {
|
|
15
|
-
const
|
|
14
|
+
const cacheKey = `rateLimit_${options.key}`;
|
|
15
|
+
const remainingCount = await _x.x.cache.get(cacheKey);
|
|
16
16
|
|
|
17
17
|
if (remainingCount == null) {
|
|
18
|
-
await
|
|
18
|
+
await _x.x.cache.set(cacheKey, options.count, options.ttl);
|
|
19
19
|
return options.count;
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -23,7 +23,7 @@ class RateLimitService {
|
|
|
23
23
|
return 0;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
await
|
|
26
|
+
await _x.x.cache.decrease(cacheKey);
|
|
27
27
|
return remainingCount - 1;
|
|
28
28
|
}
|
|
29
29
|
|
package/lib/core/server.js
CHANGED
|
@@ -74,14 +74,20 @@ 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,
|
|
90
|
+
setHeader: res.header,
|
|
85
91
|
redirect: undefined,
|
|
86
92
|
ws: req
|
|
87
93
|
});
|
|
@@ -107,7 +113,9 @@ export class Server {
|
|
|
107
113
|
...req.body,
|
|
108
114
|
...files
|
|
109
115
|
}, {
|
|
116
|
+
url: url,
|
|
110
117
|
headers: req.headers,
|
|
118
|
+
setHeader: res.header,
|
|
111
119
|
redirect: url => res.redirect(url),
|
|
112
120
|
ws: undefined
|
|
113
121
|
});
|
package/lib/core/types.d.ts
CHANGED
|
@@ -45,10 +45,18 @@ export declare namespace XHandler {
|
|
|
45
45
|
interface ExtraContext {
|
|
46
46
|
}
|
|
47
47
|
interface Context extends ExtraContext {
|
|
48
|
+
/**
|
|
49
|
+
* 当前请求的地址
|
|
50
|
+
*/
|
|
51
|
+
url: string;
|
|
48
52
|
/**
|
|
49
53
|
* 请求头
|
|
50
54
|
*/
|
|
51
55
|
headers: IncomingHttpHeaders;
|
|
56
|
+
/**
|
|
57
|
+
* 设置响应头
|
|
58
|
+
*/
|
|
59
|
+
setHeader: (key: keyof IncomingHttpHeaders, value: string) => void;
|
|
52
60
|
/**
|
|
53
61
|
* HTTP 跳转
|
|
54
62
|
*/
|
|
@@ -7,7 +7,6 @@ export interface RateLimitLimitByCountOptions {
|
|
|
7
7
|
}
|
|
8
8
|
export declare class RateLimitService implements BaseService {
|
|
9
9
|
serviceName: string;
|
|
10
|
-
private cacheService;
|
|
11
10
|
limitByCount(options: RateLimitLimitByCountOptions): Promise<number>;
|
|
12
11
|
}
|
|
13
12
|
declare module '../x' {
|
|
@@ -2,14 +2,14 @@ import { x } from "../x";
|
|
|
2
2
|
export class RateLimitService {
|
|
3
3
|
constructor() {
|
|
4
4
|
this.serviceName = 'rateLimit';
|
|
5
|
-
this.cacheService = x.cache.fork();
|
|
6
5
|
}
|
|
7
6
|
|
|
8
7
|
async limitByCount(options) {
|
|
9
|
-
const
|
|
8
|
+
const cacheKey = `rateLimit_${options.key}`;
|
|
9
|
+
const remainingCount = await x.cache.get(cacheKey);
|
|
10
10
|
|
|
11
11
|
if (remainingCount == null) {
|
|
12
|
-
await
|
|
12
|
+
await x.cache.set(cacheKey, options.count, options.ttl);
|
|
13
13
|
return options.count;
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -17,7 +17,7 @@ export class RateLimitService {
|
|
|
17
17
|
return 0;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
await
|
|
20
|
+
await x.cache.decrease(cacheKey);
|
|
21
21
|
return remainingCount - 1;
|
|
22
22
|
}
|
|
23
23
|
|