@jayfong/x-server 1.34.3 → 1.34.5

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.
@@ -92,12 +92,12 @@ class Server {
92
92
 
93
93
  const routeMap = (0, _vtils.keyBy)(this.routes, item => item.path);
94
94
 
95
- const handleRoute = async (item, req, res) => {
95
+ const handleRoute = async (item, req, res, path) => {
96
96
  const handlerOptions = item.handler.options;
97
97
  const handlerMethod = handlerOptions.requestMethod || 'POST';
98
98
  const isWS = handlerMethod === 'WS';
99
99
  const url = `${appUrl}${// 结构:/test/sss?x=2
100
- isWS ? res.url : req.url}`;
100
+ path != null ? path : isWS ? res.url : req.url}`;
101
101
 
102
102
  if (isWS) {
103
103
  await item.handler.handle(undefined, {
@@ -162,13 +162,23 @@ class Server {
162
162
  method: 'POST',
163
163
  url: '/@',
164
164
  handler: async (req, res) => {
165
- const requestPath = req.headers['x-path'];
165
+ let requestPath = req.headers['x-path'] || '';
166
+
167
+ if (!requestPath.startsWith('/')) {
168
+ const [_requestPath, _time] = (0, _vtils.base64UrlDecode)((0, _vtils.rot13)(requestPath)).split('#');
169
+
170
+ if (!_time || Date.now() / 1000 - Number(_time) > 5 * 60) {
171
+ throw new _http_error.HttpError.Forbidden();
172
+ }
173
+
174
+ requestPath = _requestPath;
175
+ }
166
176
 
167
177
  if (!requestPath || !routeMap[requestPath]) {
168
178
  throw new _http_error.HttpError.NotFound();
169
179
  }
170
180
 
171
- return handleRoute(routeMap[requestPath], req, res);
181
+ return handleRoute(routeMap[requestPath], req, res, requestPath);
172
182
  }
173
183
  });
174
184
  }
@@ -1,5 +1,5 @@
1
1
  import Fastify from 'fastify';
2
- import { castArray, keyBy, noop } from 'vtils';
2
+ import { base64UrlDecode, castArray, keyBy, noop, rot13 } from 'vtils';
3
3
  import { HandlerMethodToHttpMethod } from "./http_method";
4
4
  import { HttpError } from "./http_error";
5
5
  import { x } from "../x";
@@ -77,12 +77,12 @@ export class Server {
77
77
  const appUrl = x.env.APP_URL.replace(/\/+$/, '');
78
78
  const routeMap = keyBy(this.routes, item => item.path);
79
79
 
80
- const handleRoute = async (item, req, res) => {
80
+ const handleRoute = async (item, req, res, path) => {
81
81
  const handlerOptions = item.handler.options;
82
82
  const handlerMethod = handlerOptions.requestMethod || 'POST';
83
83
  const isWS = handlerMethod === 'WS';
84
84
  const url = `${appUrl}${// 结构:/test/sss?x=2
85
- isWS ? res.url : req.url}`;
85
+ path != null ? path : isWS ? res.url : req.url}`;
86
86
 
87
87
  if (isWS) {
88
88
  await item.handler.handle(undefined, {
@@ -147,13 +147,23 @@ export class Server {
147
147
  method: 'POST',
148
148
  url: '/@',
149
149
  handler: async (req, res) => {
150
- const requestPath = req.headers['x-path'];
150
+ let requestPath = req.headers['x-path'] || '';
151
+
152
+ if (!requestPath.startsWith('/')) {
153
+ const [_requestPath, _time] = base64UrlDecode(rot13(requestPath)).split('#');
154
+
155
+ if (!_time || Date.now() / 1000 - Number(_time) > 5 * 60) {
156
+ throw new HttpError.Forbidden();
157
+ }
158
+
159
+ requestPath = _requestPath;
160
+ }
151
161
 
152
162
  if (!requestPath || !routeMap[requestPath]) {
153
163
  throw new HttpError.NotFound();
154
164
  }
155
165
 
156
- return handleRoute(routeMap[requestPath], req, res);
166
+ return handleRoute(routeMap[requestPath], req, res, requestPath);
157
167
  }
158
168
  });
159
169
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.34.3",
3
+ "version": "1.34.5",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",