@jayfong/x-server 1.34.1 → 1.34.3

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.
@@ -13,6 +13,8 @@ var _vtils = require("vtils");
13
13
 
14
14
  var _http_method = require("./http_method");
15
15
 
16
+ var _http_error = require("./http_error");
17
+
16
18
  var _x = require("../x");
17
19
 
18
20
  class Server {
@@ -86,66 +88,89 @@ class Server {
86
88
  }
87
89
 
88
90
  async applyRoutes() {
91
+ const appUrl = _x.x.env.APP_URL.replace(/\/+$/, '');
92
+
93
+ const routeMap = (0, _vtils.keyBy)(this.routes, item => item.path);
94
+
95
+ const handleRoute = async (item, req, res) => {
96
+ const handlerOptions = item.handler.options;
97
+ const handlerMethod = handlerOptions.requestMethod || 'POST';
98
+ const isWS = handlerMethod === 'WS';
99
+ const url = `${appUrl}${// 结构:/test/sss?x=2
100
+ isWS ? res.url : req.url}`;
101
+
102
+ if (isWS) {
103
+ await item.handler.handle(undefined, {
104
+ url: url,
105
+ headers: res.headers,
106
+ setHeader: _vtils.noop,
107
+ redirect: _vtils.noop,
108
+ ws: req,
109
+ req: res,
110
+ res: {}
111
+ });
112
+ return;
113
+ }
114
+
115
+ let files = {};
116
+
117
+ if (handlerMethod === 'FILE') {
118
+ const part = await req.file();
119
+ files = Object.keys(part.fields).reduce((res, name) => {
120
+ ;
121
+ res[name] = (0, _vtils.castArray)(part.fields[name]).map(item => item.file ? item : item.value)[0];
122
+ return res;
123
+ }, {});
124
+ }
125
+
126
+ const data = await item.handler.handle({ // @ts-ignore
127
+ ...req.params,
128
+ // @ts-ignore
129
+ ...req.query,
130
+ // @ts-ignore
131
+ ...req.body,
132
+ ...files
133
+ }, {
134
+ url: url,
135
+ headers: req.headers,
136
+ setHeader: (k, v) => res.header(k, v),
137
+ redirect: url => res.redirect(url),
138
+ ws: undefined,
139
+ req: req,
140
+ res: res
141
+ });
142
+ return data;
143
+ };
144
+
89
145
  for (const item of this.routes) {
90
146
  const handlerOptions = item.handler.options;
91
147
  const handlerMethod = handlerOptions.requestMethod || 'POST';
92
148
  const isWS = handlerMethod === 'WS';
93
149
  const serverMethod = isWS ? 'GET' : _http_method.HandlerMethodToHttpMethod[handlerMethod];
94
-
95
- const appUrl = _x.x.env.APP_URL.replace(/\/+$/, '');
96
-
97
150
  this.fastify.route({
98
151
  method: serverMethod,
99
152
  url: item.path,
153
+ constraints: handlerOptions.requestHost ? {
154
+ host: handlerOptions.requestHost
155
+ } : undefined,
100
156
  websocket: isWS,
101
- handler: async (req, res) => {
102
- const url = `${appUrl}${// 结构:/test/sss?x=2
103
- isWS ? res.url : req.url}`;
104
-
105
- if (isWS) {
106
- await item.handler.handle(undefined, {
107
- url: url,
108
- headers: res.headers,
109
- setHeader: _vtils.noop,
110
- redirect: _vtils.noop,
111
- ws: req,
112
- req: res,
113
- res: {}
114
- });
115
- return;
116
- }
117
-
118
- let files = {};
119
-
120
- if (handlerMethod === 'FILE') {
121
- const part = await req.file();
122
- files = Object.keys(part.fields).reduce((res, name) => {
123
- ;
124
- res[name] = (0, _vtils.castArray)(part.fields[name]).map(item => item.file ? item : item.value)[0];
125
- return res;
126
- }, {});
127
- }
128
-
129
- const data = await item.handler.handle({ // @ts-ignore
130
- ...req.params,
131
- // @ts-ignore
132
- ...req.query,
133
- // @ts-ignore
134
- ...req.body,
135
- ...files
136
- }, {
137
- url: url,
138
- headers: req.headers,
139
- setHeader: (k, v) => res.header(k, v),
140
- redirect: url => res.redirect(url),
141
- ws: undefined,
142
- req: req,
143
- res: res
144
- });
145
- return data;
146
- }
157
+ handler: (req, res) => handleRoute(item, req, res)
147
158
  });
148
159
  }
160
+
161
+ this.fastify.route({
162
+ method: 'POST',
163
+ url: '/@',
164
+ handler: async (req, res) => {
165
+ const requestPath = req.headers['x-path'];
166
+
167
+ if (!requestPath || !routeMap[requestPath]) {
168
+ throw new _http_error.HttpError.NotFound();
169
+ }
170
+
171
+ return handleRoute(routeMap[requestPath], req, res);
172
+ }
173
+ });
149
174
  }
150
175
 
151
176
  async startCrons() {
@@ -1,6 +1,7 @@
1
1
  import Fastify from 'fastify';
2
- import { castArray, noop } from 'vtils';
2
+ import { castArray, keyBy, noop } from 'vtils';
3
3
  import { HandlerMethodToHttpMethod } from "./http_method";
4
+ import { HttpError } from "./http_error";
4
5
  import { x } from "../x";
5
6
  export class Server {
6
7
  constructor(options) {
@@ -73,64 +74,88 @@ export class Server {
73
74
  }
74
75
 
75
76
  async applyRoutes() {
77
+ const appUrl = x.env.APP_URL.replace(/\/+$/, '');
78
+ const routeMap = keyBy(this.routes, item => item.path);
79
+
80
+ const handleRoute = async (item, req, res) => {
81
+ const handlerOptions = item.handler.options;
82
+ const handlerMethod = handlerOptions.requestMethod || 'POST';
83
+ const isWS = handlerMethod === 'WS';
84
+ const url = `${appUrl}${// 结构:/test/sss?x=2
85
+ isWS ? res.url : req.url}`;
86
+
87
+ if (isWS) {
88
+ await item.handler.handle(undefined, {
89
+ url: url,
90
+ headers: res.headers,
91
+ setHeader: noop,
92
+ redirect: noop,
93
+ ws: req,
94
+ req: res,
95
+ res: {}
96
+ });
97
+ return;
98
+ }
99
+
100
+ let files = {};
101
+
102
+ if (handlerMethod === 'FILE') {
103
+ const part = await req.file();
104
+ files = Object.keys(part.fields).reduce((res, name) => {
105
+ ;
106
+ res[name] = castArray(part.fields[name]).map(item => item.file ? item : item.value)[0];
107
+ return res;
108
+ }, {});
109
+ }
110
+
111
+ const data = await item.handler.handle({ // @ts-ignore
112
+ ...req.params,
113
+ // @ts-ignore
114
+ ...req.query,
115
+ // @ts-ignore
116
+ ...req.body,
117
+ ...files
118
+ }, {
119
+ url: url,
120
+ headers: req.headers,
121
+ setHeader: (k, v) => res.header(k, v),
122
+ redirect: url => res.redirect(url),
123
+ ws: undefined,
124
+ req: req,
125
+ res: res
126
+ });
127
+ return data;
128
+ };
129
+
76
130
  for (const item of this.routes) {
77
131
  const handlerOptions = item.handler.options;
78
132
  const handlerMethod = handlerOptions.requestMethod || 'POST';
79
133
  const isWS = handlerMethod === 'WS';
80
134
  const serverMethod = isWS ? 'GET' : HandlerMethodToHttpMethod[handlerMethod];
81
- const appUrl = x.env.APP_URL.replace(/\/+$/, '');
82
135
  this.fastify.route({
83
136
  method: serverMethod,
84
137
  url: item.path,
138
+ constraints: handlerOptions.requestHost ? {
139
+ host: handlerOptions.requestHost
140
+ } : undefined,
85
141
  websocket: isWS,
86
- handler: async (req, res) => {
87
- const url = `${appUrl}${// 结构:/test/sss?x=2
88
- isWS ? res.url : req.url}`;
89
-
90
- if (isWS) {
91
- await item.handler.handle(undefined, {
92
- url: url,
93
- headers: res.headers,
94
- setHeader: noop,
95
- redirect: noop,
96
- ws: req,
97
- req: res,
98
- res: {}
99
- });
100
- return;
101
- }
102
-
103
- let files = {};
104
-
105
- if (handlerMethod === 'FILE') {
106
- const part = await req.file();
107
- files = Object.keys(part.fields).reduce((res, name) => {
108
- ;
109
- res[name] = castArray(part.fields[name]).map(item => item.file ? item : item.value)[0];
110
- return res;
111
- }, {});
112
- }
113
-
114
- const data = await item.handler.handle({ // @ts-ignore
115
- ...req.params,
116
- // @ts-ignore
117
- ...req.query,
118
- // @ts-ignore
119
- ...req.body,
120
- ...files
121
- }, {
122
- url: url,
123
- headers: req.headers,
124
- setHeader: (k, v) => res.header(k, v),
125
- redirect: url => res.redirect(url),
126
- ws: undefined,
127
- req: req,
128
- res: res
129
- });
130
- return data;
131
- }
142
+ handler: (req, res) => handleRoute(item, req, res)
132
143
  });
133
144
  }
145
+
146
+ this.fastify.route({
147
+ method: 'POST',
148
+ url: '/@',
149
+ handler: async (req, res) => {
150
+ const requestPath = req.headers['x-path'];
151
+
152
+ if (!requestPath || !routeMap[requestPath]) {
153
+ throw new HttpError.NotFound();
154
+ }
155
+
156
+ return handleRoute(routeMap[requestPath], req, res);
157
+ }
158
+ });
134
159
  }
135
160
 
136
161
  async startCrons() {
@@ -102,6 +102,10 @@ export declare namespace XHandler {
102
102
  * @default POST
103
103
  */
104
104
  requestMethod?: TReqMethod;
105
+ /**
106
+ * 请求 Host
107
+ */
108
+ requestHost?: string | RegExp;
105
109
  /**
106
110
  * 请求路径
107
111
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.34.1",
3
+ "version": "1.34.3",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",