@jayfong/x-server 2.9.6 → 2.9.7

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.
@@ -44,7 +44,7 @@ class Server {
44
44
  }
45
45
 
46
46
  async prepareFastify() {
47
- this.fastify = (0, _fastify.default)({
47
+ this.fastify = await (0, _fastify.default)({
48
48
  logger: process.env.NODE_ENV === 'development' ? {
49
49
  transport: {
50
50
  target: 'pino-pretty'
@@ -90,98 +90,100 @@ class Server {
90
90
  }
91
91
 
92
92
  async applyRoutes() {
93
- const appUrl = _x.x.env.APP_URL.replace(/\/+$/, '');
93
+ await this.fastify.register(async fastify => {
94
+ const appUrl = _x.x.env.APP_URL.replace(/\/+$/, '');
95
+
96
+ const routeMap = (0, _vtils.keyBy)(this.routes, item => item.path);
97
+
98
+ const handleRoute = async (item, req, res, path) => {
99
+ const handlerOptions = item.handler.options;
100
+ const handlerMethod = handlerOptions.requestMethod || 'POST';
101
+ const isWS = handlerMethod === 'WS';
102
+ const url = `${appUrl}${// 结构:/test/sss?x=2
103
+ path != null ? path : 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
+ }
94
117
 
95
- const routeMap = (0, _vtils.keyBy)(this.routes, item => item.path);
118
+ let files = {};
96
119
 
97
- const handleRoute = async (item, req, res, path) => {
98
- const handlerOptions = item.handler.options;
99
- const handlerMethod = handlerOptions.requestMethod || 'POST';
100
- const isWS = handlerMethod === 'WS';
101
- const url = `${appUrl}${// 结构:/test/sss?x=2
102
- path != null ? path : isWS ? res.url : req.url}`;
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
+ }
103
128
 
104
- if (isWS) {
105
- await item.handler.handle(undefined, {
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
+ }, {
106
137
  url: url,
107
- headers: res.headers,
108
- setHeader: _vtils.noop,
109
- redirect: _vtils.noop,
110
- ws: req,
111
- req: res,
112
- res: {}
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
+ };
147
+
148
+ for (const item of this.routes) {
149
+ const handlerOptions = item.handler.options;
150
+ const handlerMethod = handlerOptions.requestMethod || 'POST';
151
+ const isWS = handlerMethod === 'WS';
152
+ const serverMethod = isWS ? 'GET' : _http_method.HandlerMethodToHttpMethod[handlerMethod];
153
+ fastify.route({
154
+ method: serverMethod,
155
+ url: item.path,
156
+ constraints: handlerOptions.requestHost ? {
157
+ host: handlerOptions.requestHost
158
+ } : undefined,
159
+ websocket: isWS,
160
+ handler: (req, res) => handleRoute(item, req, res)
113
161
  });
114
- return;
115
- }
116
-
117
- let files = {};
118
-
119
- if (handlerMethod === 'FILE') {
120
- const part = await req.file();
121
- files = Object.keys(part.fields).reduce((res, name) => {
122
- ;
123
- res[name] = (0, _vtils.castArray)(part.fields[name]).map(item => item.file ? item : item.value)[0];
124
- return res;
125
- }, {});
126
162
  }
127
163
 
128
- const data = await item.handler.handle({ // @ts-ignore
129
- ...req.params,
130
- // @ts-ignore
131
- ...req.query,
132
- // @ts-ignore
133
- ...req.body,
134
- ...files
135
- }, {
136
- url: url,
137
- headers: req.headers,
138
- setHeader: (k, v) => res.header(k, v),
139
- redirect: url => res.redirect(url),
140
- ws: undefined,
141
- req: req,
142
- res: res
143
- });
144
- return data;
145
- };
146
-
147
- for (const item of this.routes) {
148
- const handlerOptions = item.handler.options;
149
- const handlerMethod = handlerOptions.requestMethod || 'POST';
150
- const isWS = handlerMethod === 'WS';
151
- const serverMethod = isWS ? 'GET' : _http_method.HandlerMethodToHttpMethod[handlerMethod];
152
- this.fastify.route({
153
- method: serverMethod,
154
- url: item.path,
155
- constraints: handlerOptions.requestHost ? {
156
- host: handlerOptions.requestHost
157
- } : undefined,
158
- websocket: isWS,
159
- handler: (req, res) => handleRoute(item, req, res)
160
- });
161
- }
164
+ fastify.route({
165
+ method: 'POST',
166
+ url: '/@',
167
+ handler: async (req, res) => {
168
+ let requestPath = req.headers['x-path'] || '';
162
169
 
163
- this.fastify.route({
164
- method: 'POST',
165
- url: '/@',
166
- handler: async (req, res) => {
167
- let requestPath = req.headers['x-path'] || '';
170
+ if (!requestPath.startsWith('/')) {
171
+ const [_requestPath, _time] = (0, _vtils.base64UrlDecode)((0, _vtils.rot13)(requestPath)).split('#');
168
172
 
169
- if (!requestPath.startsWith('/')) {
170
- const [_requestPath, _time] = (0, _vtils.base64UrlDecode)((0, _vtils.rot13)(requestPath)).split('#');
173
+ if (!_time || Date.now() / 1000 - Number(_time) > 5 * 60) {
174
+ throw new _http_error.HttpError.Forbidden();
175
+ }
171
176
 
172
- if (!_time || Date.now() / 1000 - Number(_time) > 5 * 60) {
173
- throw new _http_error.HttpError.Forbidden();
177
+ requestPath = _requestPath;
174
178
  }
175
179
 
176
- requestPath = _requestPath;
177
- }
180
+ if (!requestPath || !routeMap[requestPath]) {
181
+ throw new _http_error.HttpError.NotFound();
182
+ }
178
183
 
179
- if (!requestPath || !routeMap[requestPath]) {
180
- throw new _http_error.HttpError.NotFound();
184
+ return handleRoute(routeMap[requestPath], req, res, requestPath);
181
185
  }
182
-
183
- return handleRoute(routeMap[requestPath], req, res, requestPath);
184
- }
186
+ });
185
187
  });
186
188
  }
187
189
 
@@ -30,7 +30,7 @@ export class Server {
30
30
  }
31
31
 
32
32
  async prepareFastify() {
33
- this.fastify = Fastify({
33
+ this.fastify = await Fastify({
34
34
  logger: process.env.NODE_ENV === 'development' ? {
35
35
  transport: {
36
36
  target: 'pino-pretty'
@@ -76,97 +76,99 @@ export class Server {
76
76
  }
77
77
 
78
78
  async applyRoutes() {
79
- const appUrl = x.env.APP_URL.replace(/\/+$/, '');
80
- const routeMap = keyBy(this.routes, item => item.path);
81
-
82
- const handleRoute = async (item, req, res, path) => {
83
- const handlerOptions = item.handler.options;
84
- const handlerMethod = handlerOptions.requestMethod || 'POST';
85
- const isWS = handlerMethod === 'WS';
86
- const url = `${appUrl}${// 结构:/test/sss?x=2
87
- path != null ? path : isWS ? res.url : req.url}`;
88
-
89
- if (isWS) {
90
- await item.handler.handle(undefined, {
79
+ await this.fastify.register(async fastify => {
80
+ const appUrl = x.env.APP_URL.replace(/\/+$/, '');
81
+ const routeMap = keyBy(this.routes, item => item.path);
82
+
83
+ const handleRoute = async (item, req, res, path) => {
84
+ const handlerOptions = item.handler.options;
85
+ const handlerMethod = handlerOptions.requestMethod || 'POST';
86
+ const isWS = handlerMethod === 'WS';
87
+ const url = `${appUrl}${// 结构:/test/sss?x=2
88
+ path != null ? path : 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
+ }, {
91
122
  url: url,
92
- headers: res.headers,
93
- setHeader: noop,
94
- redirect: noop,
95
- ws: req,
96
- req: res,
97
- res: {}
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
+ };
132
+
133
+ for (const item of this.routes) {
134
+ const handlerOptions = item.handler.options;
135
+ const handlerMethod = handlerOptions.requestMethod || 'POST';
136
+ const isWS = handlerMethod === 'WS';
137
+ const serverMethod = isWS ? 'GET' : HandlerMethodToHttpMethod[handlerMethod];
138
+ fastify.route({
139
+ method: serverMethod,
140
+ url: item.path,
141
+ constraints: handlerOptions.requestHost ? {
142
+ host: handlerOptions.requestHost
143
+ } : undefined,
144
+ websocket: isWS,
145
+ handler: (req, res) => handleRoute(item, req, res)
98
146
  });
99
- return;
100
147
  }
101
148
 
102
- let files = {};
149
+ fastify.route({
150
+ method: 'POST',
151
+ url: '/@',
152
+ handler: async (req, res) => {
153
+ let requestPath = req.headers['x-path'] || '';
103
154
 
104
- if (handlerMethod === 'FILE') {
105
- const part = await req.file();
106
- files = Object.keys(part.fields).reduce((res, name) => {
107
- ;
108
- res[name] = castArray(part.fields[name]).map(item => item.file ? item : item.value)[0];
109
- return res;
110
- }, {});
111
- }
155
+ if (!requestPath.startsWith('/')) {
156
+ const [_requestPath, _time] = base64UrlDecode(rot13(requestPath)).split('#');
112
157
 
113
- const data = await item.handler.handle({ // @ts-ignore
114
- ...req.params,
115
- // @ts-ignore
116
- ...req.query,
117
- // @ts-ignore
118
- ...req.body,
119
- ...files
120
- }, {
121
- url: url,
122
- headers: req.headers,
123
- setHeader: (k, v) => res.header(k, v),
124
- redirect: url => res.redirect(url),
125
- ws: undefined,
126
- req: req,
127
- res: res
128
- });
129
- return data;
130
- };
131
-
132
- for (const item of this.routes) {
133
- const handlerOptions = item.handler.options;
134
- const handlerMethod = handlerOptions.requestMethod || 'POST';
135
- const isWS = handlerMethod === 'WS';
136
- const serverMethod = isWS ? 'GET' : HandlerMethodToHttpMethod[handlerMethod];
137
- this.fastify.route({
138
- method: serverMethod,
139
- url: item.path,
140
- constraints: handlerOptions.requestHost ? {
141
- host: handlerOptions.requestHost
142
- } : undefined,
143
- websocket: isWS,
144
- handler: (req, res) => handleRoute(item, req, res)
145
- });
146
- }
147
-
148
- this.fastify.route({
149
- method: 'POST',
150
- url: '/@',
151
- handler: async (req, res) => {
152
- let requestPath = req.headers['x-path'] || '';
158
+ if (!_time || Date.now() / 1000 - Number(_time) > 5 * 60) {
159
+ throw new HttpError.Forbidden();
160
+ }
153
161
 
154
- if (!requestPath.startsWith('/')) {
155
- const [_requestPath, _time] = base64UrlDecode(rot13(requestPath)).split('#');
156
-
157
- if (!_time || Date.now() / 1000 - Number(_time) > 5 * 60) {
158
- throw new HttpError.Forbidden();
162
+ requestPath = _requestPath;
159
163
  }
160
164
 
161
- requestPath = _requestPath;
162
- }
165
+ if (!requestPath || !routeMap[requestPath]) {
166
+ throw new HttpError.NotFound();
167
+ }
163
168
 
164
- if (!requestPath || !routeMap[requestPath]) {
165
- throw new HttpError.NotFound();
169
+ return handleRoute(routeMap[requestPath], req, res, requestPath);
166
170
  }
167
-
168
- return handleRoute(routeMap[requestPath], req, res, requestPath);
169
- }
171
+ });
170
172
  });
171
173
  }
172
174
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.9.6",
3
+ "version": "2.9.7",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",
@@ -36,7 +36,7 @@
36
36
  "@types/http-errors": "^1.8.2",
37
37
  "@types/jsonwebtoken": "^8.5.8",
38
38
  "@types/nodemailer": "^6.4.4",
39
- "@types/ws": "^8.5.3",
39
+ "@types/ws": "^8.5.5",
40
40
  "alipay-sdk": "^3.2.0",
41
41
  "bufferutil": "^4.0.6",
42
42
  "bull": "^4.10.4",