@aomex/web 0.0.29 → 1.0.1
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 +10 -63
- package/LICENSE +1 -1
- package/dist/index.d.ts +225 -272
- package/dist/index.js +522 -623
- package/dist/index.js.map +1 -1
- package/package.json +12 -18
- package/dist/bin.d.ts +0 -1
- package/dist/bin.js +0 -10
- package/dist/bin.js.map +0 -1
- package/src/bin.ts +0 -9
package/dist/index.js
CHANGED
|
@@ -1,241 +1,73 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (options === true) {
|
|
10
|
-
return middleware.web((_, next) => next());
|
|
11
|
-
}
|
|
12
|
-
const originFn = token instanceof Middleware ? token.fn : compose(toArray(token));
|
|
13
|
-
if (options === false) {
|
|
14
|
-
return middleware.web(originFn);
|
|
15
|
-
}
|
|
16
|
-
const opts = typeof options === "function" ? { custom: options } : options;
|
|
17
|
-
return middleware.web(async (ctx, next) => {
|
|
18
|
-
const skipped = await shouldSkip(ctx, opts);
|
|
19
|
-
return skipped ? next() : originFn(ctx, next);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
var shouldSkip = async (ctx, options) => {
|
|
23
|
-
if (await options.custom?.(ctx))
|
|
24
|
-
return true;
|
|
25
|
-
if (options.path) {
|
|
26
|
-
const { pathname } = ctx.request;
|
|
27
|
-
return toArray(options.path).some(
|
|
28
|
-
(path) => typeof path === "string" ? path === pathname : path.exec(pathname) !== null
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
if (options.ext) {
|
|
32
|
-
const currentExt = extname(ctx.request.pathname);
|
|
33
|
-
if (currentExt === "")
|
|
34
|
-
return false;
|
|
35
|
-
return toArray(options.ext).some((ext) => {
|
|
36
|
-
return currentExt === (ext.startsWith(".") ? ext : `.${ext}`);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
if (options.method) {
|
|
40
|
-
return toArray(options.method).includes(ctx.request.method);
|
|
41
|
-
}
|
|
42
|
-
return false;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// src/override/middleware.ts
|
|
46
|
-
var WebMiddleware = class extends Middleware2 {
|
|
47
|
-
constructor(fn) {
|
|
48
|
-
super(fn);
|
|
49
|
-
}
|
|
50
|
-
skipIf(options) {
|
|
51
|
-
return skip(this, options);
|
|
52
|
-
}
|
|
53
|
-
toDocument(options) {
|
|
54
|
-
options;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
Middleware2.register("web", WebMiddleware);
|
|
58
|
-
|
|
59
|
-
// src/override/chain.ts
|
|
60
|
-
import { Chain } from "@aomex/core";
|
|
61
|
-
var WebChain = class extends Chain {
|
|
62
|
-
};
|
|
63
|
-
Chain.register("web", WebChain);
|
|
64
|
-
|
|
65
|
-
// src/override/file-validator.ts
|
|
66
|
-
import { PersistentFile } from "formidable";
|
|
67
|
-
import mimeTypes from "mime-types";
|
|
68
|
-
import {
|
|
69
|
-
Validator,
|
|
70
|
-
magistrate,
|
|
71
|
-
Rule
|
|
72
|
-
} from "@aomex/core";
|
|
73
|
-
import { bytes } from "@aomex/internal-tools";
|
|
74
|
-
import typeIs from "type-is";
|
|
75
|
-
var FileValidator = class extends Validator {
|
|
76
|
-
/**
|
|
77
|
-
* 允许的最大体积
|
|
78
|
-
*
|
|
79
|
-
* 可选格式:
|
|
80
|
-
* - 1024
|
|
81
|
-
* - 2048
|
|
82
|
-
* - '15KB'
|
|
83
|
-
* - '20MB'
|
|
84
|
-
*/
|
|
85
|
-
maxSize(byte) {
|
|
86
|
-
const validator = this.copy();
|
|
87
|
-
validator.config.maxSize = typeof byte === "number" ? byte : bytes(byte);
|
|
88
|
-
return validator;
|
|
89
|
-
}
|
|
90
|
-
mimeTypes(mineOrExt, ...others) {
|
|
91
|
-
const validator = this.copy();
|
|
92
|
-
validator.config.mimeTypes = [
|
|
93
|
-
...new Set(
|
|
94
|
-
[].concat(mineOrExt).concat(others).map(mimeTypes.contentType).filter(Boolean)
|
|
95
|
-
)
|
|
96
|
-
];
|
|
97
|
-
return validator;
|
|
98
|
-
}
|
|
99
|
-
isEmpty(value) {
|
|
100
|
-
return super.isEmpty(value) || Array.isArray(value) && !value.length;
|
|
101
|
-
}
|
|
102
|
-
validateValue(file, key, superKeys) {
|
|
103
|
-
const { maxSize, mimeTypes: mimeTypes3 } = this.config;
|
|
104
|
-
if (Array.isArray(file)) {
|
|
105
|
-
if (file.length > 1) {
|
|
106
|
-
return magistrate.fail(
|
|
107
|
-
"use rule.array(rule.file()) for multiple files",
|
|
108
|
-
key,
|
|
109
|
-
superKeys
|
|
110
|
-
);
|
|
111
|
-
} else {
|
|
112
|
-
file = file[0];
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (!(file instanceof PersistentFile)) {
|
|
116
|
-
return magistrate.fail("must be file", key, superKeys);
|
|
117
|
-
}
|
|
118
|
-
if (maxSize !== void 0 && file.size > maxSize) {
|
|
119
|
-
return magistrate.fail("file size is too large", key, superKeys);
|
|
120
|
-
}
|
|
121
|
-
const hasMimeTypeLimitation = mimeTypes3 && mimeTypes3.length;
|
|
122
|
-
if (hasMimeTypeLimitation && (!file.mimetype || !typeIs.is(file.mimetype, ...mimeTypes3))) {
|
|
123
|
-
return magistrate.fail("file not match mime-types", key, superKeys);
|
|
1
|
+
// src/i18n/locales/zh-cn.ts
|
|
2
|
+
import { i18n } from "@aomex/core";
|
|
3
|
+
i18n.register("zh_CN", "web", {
|
|
4
|
+
validator: {
|
|
5
|
+
file: {
|
|
6
|
+
must_be_file: "{{label}}\uFF1A\u5FC5\u987B\u662F\u6587\u4EF6\u7C7B\u578B",
|
|
7
|
+
too_large: "{{label}}\uFF1A\u6587\u4EF6\u4F53\u79EF\u592A\u5927",
|
|
8
|
+
unsupported_mimetype: "{{label}}\uFF1A\u4E0D\u652F\u6301\u7684\u6587\u4EF6\u7C7B\u578B"
|
|
124
9
|
}
|
|
125
|
-
return magistrate.ok(file);
|
|
126
10
|
}
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
type: "string",
|
|
130
|
-
format: "binary"
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
Rule.register("file", FileValidator);
|
|
11
|
+
});
|
|
135
12
|
|
|
136
|
-
// src/
|
|
137
|
-
import
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
request.res = response2;
|
|
145
|
-
request.ctx = response2.ctx = this;
|
|
146
|
-
}
|
|
147
|
-
send(statusOrBody, body2) {
|
|
148
|
-
if (typeof statusOrBody === "number") {
|
|
149
|
-
this.response.statusCode = statusOrBody;
|
|
150
|
-
} else {
|
|
151
|
-
body2 = statusOrBody;
|
|
13
|
+
// src/i18n/locales/en-us.ts
|
|
14
|
+
import { i18n as i18n2 } from "@aomex/core";
|
|
15
|
+
i18n2.register("en_US", "web", {
|
|
16
|
+
validator: {
|
|
17
|
+
file: {
|
|
18
|
+
must_be_file: "{{label}}: must be file",
|
|
19
|
+
too_large: "{{label}}: file size too large",
|
|
20
|
+
unsupported_mimetype: "{{label}}: unsupported file mimetype"
|
|
152
21
|
}
|
|
153
|
-
if (body2 !== void 0) {
|
|
154
|
-
this.response.body = body2;
|
|
155
|
-
}
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
throw(arg, ...args) {
|
|
159
|
-
throw createHttpError(arg, ...args);
|
|
160
22
|
}
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
// src/app/app.ts
|
|
164
|
-
import EventEmitter from "node:events";
|
|
165
|
-
import { EOL } from "node:os";
|
|
166
|
-
import { Chain as Chain2, compose as compose2 } from "@aomex/core";
|
|
167
|
-
import { chalk } from "@aomex/internal-tools";
|
|
168
|
-
|
|
169
|
-
// src/util/get-content-type.ts
|
|
170
|
-
import mimeTypes2 from "mime-types";
|
|
171
|
-
import { LRUCache } from "lru-cache";
|
|
172
|
-
var cache = new LRUCache({
|
|
173
|
-
max: 100
|
|
174
23
|
});
|
|
175
|
-
var getMimeType = (filenameOrExt) => {
|
|
176
|
-
let mimeType = cache.get(filenameOrExt);
|
|
177
|
-
if (!mimeType) {
|
|
178
|
-
mimeType = mimeTypes2.contentType(filenameOrExt) || "";
|
|
179
|
-
cache.set(filenameOrExt, mimeType);
|
|
180
|
-
}
|
|
181
|
-
return mimeType;
|
|
182
|
-
};
|
|
183
24
|
|
|
184
|
-
// src/
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
METHOD2["POST"] = "POST";
|
|
188
|
-
METHOD2["PUT"] = "PUT";
|
|
189
|
-
METHOD2["PATCH"] = "PATCH";
|
|
190
|
-
METHOD2["DELETE"] = "DELETE";
|
|
191
|
-
METHOD2["OPTIONS"] = "OPTIONS";
|
|
192
|
-
METHOD2["HEAD"] = "HEAD";
|
|
193
|
-
return METHOD2;
|
|
194
|
-
})(METHOD || {});
|
|
25
|
+
// src/http/app.ts
|
|
26
|
+
import { createServer } from "node:http";
|
|
27
|
+
import stream2 from "node:stream";
|
|
195
28
|
|
|
196
|
-
// src/
|
|
197
|
-
import { createServer } from "http";
|
|
198
|
-
|
|
199
|
-
// src/app/request.ts
|
|
29
|
+
// src/http/request.ts
|
|
200
30
|
import { IncomingMessage } from "node:http";
|
|
201
31
|
import qs from "qs";
|
|
202
|
-
import cookie from "cookie";
|
|
203
|
-
import coBody from "co-body";
|
|
204
32
|
import formidable from "formidable";
|
|
205
|
-
import
|
|
206
|
-
import
|
|
207
|
-
import contentType from "content-type";
|
|
33
|
+
import coBody from "co-body";
|
|
34
|
+
import typeIs from "type-is";
|
|
208
35
|
import requestIP from "request-ip";
|
|
209
36
|
import fresh from "fresh";
|
|
37
|
+
import contentType from "content-type";
|
|
38
|
+
import accepts from "accepts";
|
|
39
|
+
import cookie from "cookie";
|
|
210
40
|
var WebRequest = class extends IncomingMessage {
|
|
211
41
|
app;
|
|
42
|
+
req;
|
|
212
43
|
res;
|
|
213
44
|
ctx;
|
|
214
45
|
params = {};
|
|
215
|
-
_query;
|
|
216
|
-
_body;
|
|
217
46
|
_accept;
|
|
218
|
-
|
|
47
|
+
_cookies;
|
|
48
|
+
_body;
|
|
219
49
|
_parsedUrl = null;
|
|
220
|
-
|
|
221
|
-
|
|
50
|
+
_query;
|
|
51
|
+
get accept() {
|
|
52
|
+
return this._accept || (this._accept = accepts(this));
|
|
53
|
+
}
|
|
54
|
+
get contentType() {
|
|
55
|
+
try {
|
|
56
|
+
return contentType.parse(this).type;
|
|
57
|
+
} catch {
|
|
58
|
+
return "";
|
|
59
|
+
}
|
|
222
60
|
}
|
|
223
61
|
/**
|
|
224
|
-
*
|
|
62
|
+
* 返回从`headers['cookie']`解析后的cookie列表
|
|
225
63
|
*/
|
|
226
|
-
get
|
|
227
|
-
return this.
|
|
228
|
-
}
|
|
229
|
-
get querystring() {
|
|
230
|
-
return this.URL.search.slice(1);
|
|
231
|
-
}
|
|
232
|
-
get query() {
|
|
233
|
-
return this._query ||= qs.parse(this.querystring, this.app.options.query);
|
|
64
|
+
get cookies() {
|
|
65
|
+
return this._cookies ||= cookie.parse(this.headers["cookie"] || "");
|
|
234
66
|
}
|
|
235
67
|
get body() {
|
|
236
68
|
if (this._body)
|
|
237
69
|
return Promise.resolve(this._body);
|
|
238
|
-
if (this.
|
|
70
|
+
if (this.matchContentType("multipart/*") !== null) {
|
|
239
71
|
const form = formidable({
|
|
240
72
|
hashAlgorithm: "md5",
|
|
241
73
|
keepExtensions: true,
|
|
@@ -245,10 +77,7 @@ var WebRequest = class extends IncomingMessage {
|
|
|
245
77
|
return this._body = form.parse(this).then(([fields, files]) => {
|
|
246
78
|
const fixedFields = Object.fromEntries(
|
|
247
79
|
Object.entries(fields).map(([key, values]) => {
|
|
248
|
-
return [
|
|
249
|
-
key,
|
|
250
|
-
values == void 0 || values.length > 1 ? values : values[0]
|
|
251
|
-
];
|
|
80
|
+
return [key, values == void 0 || values.length > 1 ? values : values[0]];
|
|
252
81
|
})
|
|
253
82
|
);
|
|
254
83
|
return this._body = { ...fixedFields, ...files };
|
|
@@ -260,22 +89,25 @@ var WebRequest = class extends IncomingMessage {
|
|
|
260
89
|
return this._body = fields;
|
|
261
90
|
});
|
|
262
91
|
}
|
|
263
|
-
get
|
|
264
|
-
|
|
265
|
-
return
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return result === false ? null : result;
|
|
273
|
-
}
|
|
274
|
-
get accept() {
|
|
275
|
-
return this._accept || (this._accept = accepts(this));
|
|
92
|
+
get fresh() {
|
|
93
|
+
if (this.method !== "GET" && this.method !== "HEAD")
|
|
94
|
+
return false;
|
|
95
|
+
const status = this.res.statusCode;
|
|
96
|
+
if (status < 200)
|
|
97
|
+
return false;
|
|
98
|
+
if (status >= 300 && status !== 304)
|
|
99
|
+
return false;
|
|
100
|
+
return fresh(this.headers, this.res.getHeaders());
|
|
276
101
|
}
|
|
277
|
-
get
|
|
278
|
-
|
|
102
|
+
get host() {
|
|
103
|
+
let host = this.headers["x-forwarded-host"];
|
|
104
|
+
if (!host) {
|
|
105
|
+
if (this.httpVersionMajor >= 2) {
|
|
106
|
+
host = this.headers[":authority"];
|
|
107
|
+
}
|
|
108
|
+
host ||= this.headers["host"];
|
|
109
|
+
}
|
|
110
|
+
return host && host.split(/\s*,\s*/, 1)[0] || "";
|
|
279
111
|
}
|
|
280
112
|
/**
|
|
281
113
|
* 包括了协议,域名,端口和路径的完整链接
|
|
@@ -283,18 +115,14 @@ var WebRequest = class extends IncomingMessage {
|
|
|
283
115
|
get href() {
|
|
284
116
|
return this.origin + this.url;
|
|
285
117
|
}
|
|
118
|
+
get ip() {
|
|
119
|
+
return requestIP.getClientIp(this) || "";
|
|
120
|
+
}
|
|
286
121
|
get origin() {
|
|
287
122
|
return `${this.protocol}://${this.host}`;
|
|
288
123
|
}
|
|
289
|
-
get
|
|
290
|
-
|
|
291
|
-
if (!host) {
|
|
292
|
-
if (this.httpVersionMajor >= 2) {
|
|
293
|
-
host = this.headers[":authority"];
|
|
294
|
-
}
|
|
295
|
-
host ||= this.headers["host"];
|
|
296
|
-
}
|
|
297
|
-
return host && host.split(/\s*,\s*/, 1)[0] || "";
|
|
124
|
+
get pathname() {
|
|
125
|
+
return this.URL.pathname;
|
|
298
126
|
}
|
|
299
127
|
get protocol() {
|
|
300
128
|
if (this.socket.encrypted)
|
|
@@ -302,160 +130,97 @@ var WebRequest = class extends IncomingMessage {
|
|
|
302
130
|
const proto = this.headers["x-forwarded-proto"];
|
|
303
131
|
return proto && proto.split(/\s*,\s*/, 1)[0] || "http";
|
|
304
132
|
}
|
|
305
|
-
|
|
306
|
-
|
|
133
|
+
/**
|
|
134
|
+
* 查询字符串对象
|
|
135
|
+
*/
|
|
136
|
+
get query() {
|
|
137
|
+
return this._query ||= qs.parse(this.querystring);
|
|
307
138
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
if (status < 200)
|
|
314
|
-
return false;
|
|
315
|
-
if (status > 299 && status !== 304)
|
|
316
|
-
return false;
|
|
317
|
-
return fresh(this.headers, this.res.getHeaders());
|
|
139
|
+
/**
|
|
140
|
+
* 查询字符串
|
|
141
|
+
*/
|
|
142
|
+
get querystring() {
|
|
143
|
+
return this.URL.search.slice(1);
|
|
318
144
|
}
|
|
319
145
|
/**
|
|
320
|
-
*
|
|
146
|
+
* 搜索字符串,比查询字符串多了一个开头问号(?)
|
|
321
147
|
*/
|
|
322
|
-
get
|
|
323
|
-
return this.
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
148
|
+
get search() {
|
|
149
|
+
return this.URL.search;
|
|
150
|
+
}
|
|
151
|
+
get secure() {
|
|
152
|
+
return this.protocol === "https";
|
|
327
153
|
}
|
|
328
154
|
get URL() {
|
|
329
155
|
return this._parsedUrl ||= new URL(this.href);
|
|
330
156
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
var createHttpServer = (listener) => {
|
|
335
|
-
return createServer(
|
|
336
|
-
{
|
|
337
|
-
IncomingMessage: WebRequest,
|
|
338
|
-
// @ts-ignore
|
|
339
|
-
ServerResponse: WebResponse
|
|
340
|
-
},
|
|
341
|
-
listener
|
|
342
|
-
);
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
// src/app/app.ts
|
|
346
|
-
var WebApp = class extends EventEmitter {
|
|
347
|
-
constructor(options = {}) {
|
|
348
|
-
super();
|
|
349
|
-
this.options = options;
|
|
350
|
-
}
|
|
351
|
-
chainPoints = [];
|
|
352
|
-
middlewareList = [];
|
|
353
|
-
get debug() {
|
|
354
|
-
return this.options.debug ?? process.env["NODE_ENV"] !== "production";
|
|
355
|
-
}
|
|
356
|
-
listen = (...args) => {
|
|
357
|
-
const server = createHttpServer(this.callback());
|
|
358
|
-
return server.listen(...args);
|
|
359
|
-
};
|
|
360
|
-
callback() {
|
|
361
|
-
const fn = compose2(this.middlewareList);
|
|
362
|
-
if (!this.listenerCount("error")) {
|
|
363
|
-
this.on("error", this.log.bind(this));
|
|
364
|
-
}
|
|
365
|
-
return (req, res) => {
|
|
366
|
-
return fn(new WebContext(this, req, res)).then(res.flush.bind(res)).catch(res.onError);
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
log(err) {
|
|
370
|
-
if (this.options.silent)
|
|
371
|
-
return;
|
|
372
|
-
if ((err.status || err.statusCode) === 404 || err.expose)
|
|
373
|
-
return;
|
|
374
|
-
const msgs = (err.stack || err.toString()).split(EOL, 2);
|
|
375
|
-
console.error(
|
|
376
|
-
["", chalk.bgRed(msgs.shift()), msgs.join(EOL), ""].join(EOL)
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
on(eventName, listener) {
|
|
380
|
-
return super.on(eventName, listener);
|
|
381
|
-
}
|
|
382
|
-
mount(middleware2) {
|
|
383
|
-
if (middleware2 === null)
|
|
384
|
-
return;
|
|
385
|
-
if (middleware2 instanceof Chain2) {
|
|
386
|
-
this.chainPoints.push(Chain2.createSplitPoint(middleware2));
|
|
387
|
-
}
|
|
388
|
-
this.middlewareList.push(middleware2);
|
|
157
|
+
matchContentType(type, ...types) {
|
|
158
|
+
const result = typeIs(this, type, ...types);
|
|
159
|
+
return result === false ? null : result;
|
|
389
160
|
}
|
|
390
161
|
};
|
|
391
162
|
|
|
392
|
-
// src/
|
|
163
|
+
// src/http/response.ts
|
|
393
164
|
import { ServerResponse } from "node:http";
|
|
394
|
-
import stream, { Stream } from "node:stream";
|
|
395
165
|
import assert from "node:assert";
|
|
396
166
|
import statuses from "statuses";
|
|
397
|
-
import
|
|
398
|
-
|
|
167
|
+
import { Stream } from "node:stream";
|
|
168
|
+
|
|
169
|
+
// src/utils/get-mime-type.ts
|
|
170
|
+
import mimeTypes from "mime-types";
|
|
171
|
+
import { LRUCache } from "lru-cache";
|
|
172
|
+
var cache = new LRUCache({
|
|
173
|
+
max: 100
|
|
174
|
+
});
|
|
175
|
+
var getMimeType = (filenameOrExt) => {
|
|
176
|
+
let mimeType = cache.get(filenameOrExt);
|
|
177
|
+
if (!mimeType) {
|
|
178
|
+
mimeType = mimeTypes.contentType(filenameOrExt) || "";
|
|
179
|
+
cache.set(filenameOrExt, mimeType);
|
|
180
|
+
}
|
|
181
|
+
return mimeType;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// src/http/response.ts
|
|
399
185
|
import contentType2 from "content-type";
|
|
186
|
+
import stream from "stream";
|
|
400
187
|
import destroy from "destroy";
|
|
188
|
+
import createHttpError, { isHttpError } from "http-errors";
|
|
189
|
+
import encodeUrl from "encodeurl";
|
|
190
|
+
import escapeHtml from "escape-html";
|
|
401
191
|
import contentDisposition from "content-disposition";
|
|
402
|
-
import
|
|
403
|
-
import { extname as extname2 } from "node:path";
|
|
192
|
+
import { extname } from "node:path";
|
|
404
193
|
import { createReadStream } from "node:fs";
|
|
405
|
-
import typeIs3 from "type-is";
|
|
406
194
|
import vary from "vary";
|
|
195
|
+
import typeIs2 from "type-is";
|
|
407
196
|
import cookie2 from "cookie";
|
|
408
197
|
var WebResponse = class extends ServerResponse {
|
|
409
198
|
app;
|
|
199
|
+
res;
|
|
410
200
|
ctx;
|
|
411
201
|
_body = null;
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
this.
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
removeHeaders(...headers) {
|
|
437
|
-
headers.forEach(this.removeHeader.bind(this));
|
|
438
|
-
}
|
|
439
|
-
redirect(status, url) {
|
|
440
|
-
url = typeof url === "string" ? url : status.toString();
|
|
441
|
-
this.statusCode = typeof status === "string" ? 302 : status;
|
|
442
|
-
this.setHeader("location", encodeUrl(url));
|
|
443
|
-
if (this.req.accept.types("html")) {
|
|
444
|
-
url = escapeHtml(url);
|
|
445
|
-
this.contentType = "html";
|
|
446
|
-
this.body = `Redirecting to <a href="${url}">${url}</a>.`;
|
|
447
|
-
} else {
|
|
448
|
-
this.contentType = "text";
|
|
449
|
-
this.body = `Redirecting to ${url}.`;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
download(filePath, options = {}) {
|
|
453
|
-
this.contentType = extname2(filePath);
|
|
454
|
-
this.setHeader(
|
|
455
|
-
"content-disposition",
|
|
456
|
-
contentDisposition(filePath, options)
|
|
457
|
-
);
|
|
458
|
-
this.body = createReadStream(filePath);
|
|
202
|
+
/**
|
|
203
|
+
* 是否明确设置过内容
|
|
204
|
+
*/
|
|
205
|
+
_explicitBody = false;
|
|
206
|
+
/**
|
|
207
|
+
* 是否明确设置过状态码
|
|
208
|
+
*/
|
|
209
|
+
_explicitStatus = false;
|
|
210
|
+
_statusCode = 404;
|
|
211
|
+
_updatingBodyType = false;
|
|
212
|
+
constructor(req) {
|
|
213
|
+
super(req);
|
|
214
|
+
Object.defineProperty(this, "statusCode", {
|
|
215
|
+
get: () => {
|
|
216
|
+
return this._statusCode;
|
|
217
|
+
},
|
|
218
|
+
set: (code) => {
|
|
219
|
+
this.setStatus(code);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
this.onError = this.onError.bind(this);
|
|
223
|
+
this.flush = this.flush.bind(this);
|
|
459
224
|
}
|
|
460
225
|
get contentLength() {
|
|
461
226
|
const length = this.getHeader("Content-Length");
|
|
@@ -472,69 +237,84 @@ var WebResponse = class extends ServerResponse {
|
|
|
472
237
|
set contentType(typeOrFilenameOrExt) {
|
|
473
238
|
const mimeType = getMimeType(typeOrFilenameOrExt);
|
|
474
239
|
try {
|
|
475
|
-
if (mimeType
|
|
476
|
-
throw new Error();
|
|
240
|
+
if (!mimeType)
|
|
241
|
+
throw new Error("");
|
|
477
242
|
contentType2.parse(mimeType);
|
|
478
243
|
} catch {
|
|
479
|
-
throw new TypeError(
|
|
244
|
+
throw new TypeError(`\u4E0D\u5408\u6CD5\u7684\u7C7B\u578B\uFF1A'${typeOrFilenameOrExt}'`);
|
|
480
245
|
}
|
|
481
246
|
this.setHeader("Content-Type", mimeType);
|
|
482
|
-
this.
|
|
247
|
+
this.updateBodyType();
|
|
483
248
|
}
|
|
484
249
|
get body() {
|
|
485
250
|
return this._body;
|
|
486
251
|
}
|
|
487
252
|
set body(val) {
|
|
488
|
-
this._body = val;
|
|
253
|
+
this._body = val == null ? null : val;
|
|
489
254
|
this._explicitBody = true;
|
|
490
255
|
if (!this._explicitStatus) {
|
|
491
256
|
this.statusCode = 200;
|
|
492
257
|
}
|
|
493
|
-
this.
|
|
258
|
+
this.updateBodyType();
|
|
494
259
|
}
|
|
495
|
-
|
|
496
|
-
|
|
260
|
+
download(filePath, options = {}) {
|
|
261
|
+
this.contentType = extname(filePath);
|
|
262
|
+
this.setHeader("content-disposition", contentDisposition(filePath, options));
|
|
263
|
+
this.body = createReadStream(filePath);
|
|
497
264
|
}
|
|
498
265
|
flush() {
|
|
499
266
|
if (!this.writable || this.headersSent)
|
|
500
267
|
return;
|
|
501
|
-
this.determineHeaders();
|
|
502
268
|
let output = this.body;
|
|
503
269
|
if (statuses.empty[this.statusCode])
|
|
504
|
-
return this.end();
|
|
270
|
+
return void this.end();
|
|
505
271
|
if (output === null) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
this._body = String(null);
|
|
272
|
+
if (this.contentType === "application/json") {
|
|
273
|
+
this.body = String(null);
|
|
274
|
+
} else if (this._explicitBody) {
|
|
275
|
+
this.body = "";
|
|
511
276
|
} else {
|
|
512
|
-
this.
|
|
277
|
+
this.statusCode = this.statusCode;
|
|
278
|
+
this.body = String(this.statusMessage || statuses.message[this.statusCode]);
|
|
513
279
|
}
|
|
514
|
-
this.determineHeaders();
|
|
515
280
|
output = this.body;
|
|
516
281
|
}
|
|
517
282
|
if (this.req.method === "HEAD")
|
|
518
|
-
return this.end();
|
|
283
|
+
return void this.end();
|
|
519
284
|
if (typeof output === "string")
|
|
520
|
-
return this.end(output);
|
|
285
|
+
return void this.end(output);
|
|
521
286
|
if (Buffer.isBuffer(output))
|
|
522
|
-
return this.end(output);
|
|
287
|
+
return void this.end(output);
|
|
523
288
|
if (output instanceof Stream) {
|
|
524
|
-
stream.finished(this, () => {
|
|
525
|
-
destroy(output);
|
|
526
|
-
});
|
|
527
289
|
if (!output.listenerCount("error")) {
|
|
528
290
|
output.once("error", this.onError);
|
|
529
291
|
}
|
|
530
|
-
|
|
292
|
+
stream.finished(this, () => {
|
|
293
|
+
destroy(output);
|
|
294
|
+
});
|
|
295
|
+
return void output.pipe(this);
|
|
531
296
|
}
|
|
532
|
-
return this.end(JSON.stringify(output));
|
|
297
|
+
return void this.end(JSON.stringify(output));
|
|
298
|
+
}
|
|
299
|
+
isJSON(body2) {
|
|
300
|
+
if (!body2)
|
|
301
|
+
return false;
|
|
302
|
+
if (typeof body2 === "string")
|
|
303
|
+
return false;
|
|
304
|
+
if (body2 instanceof Stream)
|
|
305
|
+
return false;
|
|
306
|
+
if (Buffer.isBuffer(body2))
|
|
307
|
+
return false;
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
matchContentType(type, ...types) {
|
|
311
|
+
const result = typeIs2.is(this.contentType, type, ...types);
|
|
312
|
+
return result === false ? null : result;
|
|
533
313
|
}
|
|
534
314
|
onError(error) {
|
|
535
315
|
if (error == null)
|
|
536
316
|
return;
|
|
537
|
-
const err = isHttpError(error) ? error :
|
|
317
|
+
const err = isHttpError(error) ? error : createHttpError(error);
|
|
538
318
|
this.removeHeaders(...this.getHeaderNames());
|
|
539
319
|
err.headers && this.setHeaders(err.headers);
|
|
540
320
|
const code = err.status || err.statusCode;
|
|
@@ -549,69 +329,75 @@ var WebResponse = class extends ServerResponse {
|
|
|
549
329
|
this.app.emit("error", err, this.ctx);
|
|
550
330
|
this.flush();
|
|
551
331
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
332
|
+
redirect(status, url) {
|
|
333
|
+
url = typeof url === "string" ? url : status.toString();
|
|
334
|
+
this.statusCode = typeof status === "string" ? 302 : status;
|
|
335
|
+
this.setHeader("location", encodeUrl(url));
|
|
336
|
+
if (this.req.accept.types("html")) {
|
|
337
|
+
url = escapeHtml(url);
|
|
338
|
+
this.contentType = "html";
|
|
339
|
+
this.body = `Redirecting to <a href="${url}">${url}</a>.`;
|
|
340
|
+
} else {
|
|
341
|
+
this.contentType = "text";
|
|
342
|
+
this.body = `Redirecting to ${url}.`;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
removeCookie(name, options) {
|
|
346
|
+
return this.setCookie(name, "", {
|
|
347
|
+
...options,
|
|
348
|
+
maxAge: void 0,
|
|
349
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
removeHeaders(...headers) {
|
|
353
|
+
headers.forEach(this.removeHeader.bind(this));
|
|
354
|
+
}
|
|
355
|
+
setCookie(name, value, options) {
|
|
356
|
+
const cookies = this.getHeader("Set-Cookie") || [];
|
|
357
|
+
cookies.push(
|
|
358
|
+
cookie2.serialize(name, value, {
|
|
359
|
+
path: "/",
|
|
360
|
+
sameSite: true,
|
|
361
|
+
httpOnly: true,
|
|
362
|
+
secure: this.req.secure,
|
|
363
|
+
...options
|
|
364
|
+
})
|
|
365
|
+
);
|
|
366
|
+
this.setHeader("Set-Cookie", cookies);
|
|
367
|
+
}
|
|
368
|
+
setHeaders(headers) {
|
|
369
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
370
|
+
value !== void 0 && this.setHeader(key, value);
|
|
371
|
+
}
|
|
555
372
|
}
|
|
556
373
|
vary(field) {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
varyAppend(header, field) {
|
|
560
|
-
return vary.append(header, field);
|
|
561
|
-
}
|
|
562
|
-
get cookie() {
|
|
563
|
-
if (this._cookie)
|
|
564
|
-
return this._cookie;
|
|
565
|
-
const defaultSerializeOptions = {
|
|
566
|
-
path: "/",
|
|
567
|
-
sameSite: false,
|
|
568
|
-
secure: this.req.secure,
|
|
569
|
-
httpOnly: true,
|
|
570
|
-
...this.app.options.cookie?.set
|
|
571
|
-
};
|
|
572
|
-
this._cookie = {
|
|
573
|
-
set: (name, value, options) => {
|
|
574
|
-
const setCookie = this.getHeader("Set-Cookie") || [];
|
|
575
|
-
setCookie.push(
|
|
576
|
-
cookie2.serialize(name, value, {
|
|
577
|
-
...defaultSerializeOptions,
|
|
578
|
-
...options
|
|
579
|
-
})
|
|
580
|
-
);
|
|
581
|
-
this.setHeader("Set-Cookie", setCookie);
|
|
582
|
-
},
|
|
583
|
-
remove: (name, options) => {
|
|
584
|
-
this.cookie.set(name, "", {
|
|
585
|
-
...defaultSerializeOptions,
|
|
586
|
-
...options,
|
|
587
|
-
maxAge: void 0,
|
|
588
|
-
expires: /* @__PURE__ */ new Date(0)
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
};
|
|
592
|
-
return this._cookie;
|
|
374
|
+
vary(this, field);
|
|
375
|
+
return this.getHeader("Vary");
|
|
593
376
|
}
|
|
594
377
|
setStatus(code) {
|
|
595
|
-
assert(code >= 100 && code <= 999
|
|
378
|
+
assert(code >= 100 && code <= 999);
|
|
596
379
|
this._statusCode = code;
|
|
597
380
|
this._explicitStatus = true;
|
|
598
|
-
|
|
599
|
-
this.statusMessage = String(statuses.message[code]);
|
|
600
|
-
}
|
|
381
|
+
this.statusMessage = String(statuses.message[code] || code);
|
|
601
382
|
if (statuses.empty[code]) {
|
|
602
383
|
this.body = null;
|
|
603
384
|
} else {
|
|
604
|
-
this.
|
|
385
|
+
this.updateBodyType();
|
|
605
386
|
}
|
|
606
387
|
}
|
|
607
|
-
|
|
608
|
-
if (this.
|
|
388
|
+
updateBodyType() {
|
|
389
|
+
if (this._updatingBodyType)
|
|
609
390
|
return;
|
|
610
|
-
this.
|
|
391
|
+
this._updatingBodyType = true;
|
|
611
392
|
const { body: body2 } = this;
|
|
612
393
|
const missType = !this.hasHeader("Content-Type");
|
|
613
394
|
if (body2 === null) {
|
|
614
|
-
this.
|
|
395
|
+
if (statuses.empty[this.statusCode]) {
|
|
396
|
+
this.removeHeaders("Content-Type", "Content-Length", "Transfer-Encoding");
|
|
397
|
+
} else if (this.contentType === "application/json") {
|
|
398
|
+
this.contentLength = Buffer.byteLength(String(null));
|
|
399
|
+
} else {
|
|
400
|
+
}
|
|
615
401
|
} else if (typeof body2 === "string") {
|
|
616
402
|
this.contentLength = Buffer.byteLength(body2);
|
|
617
403
|
if (missType) {
|
|
@@ -627,219 +413,339 @@ var WebResponse = class extends ServerResponse {
|
|
|
627
413
|
this.contentType = "bin";
|
|
628
414
|
}
|
|
629
415
|
} else {
|
|
630
|
-
this.contentType = "json";
|
|
631
416
|
this.contentLength = Buffer.byteLength(JSON.stringify(body2));
|
|
417
|
+
if (missType) {
|
|
418
|
+
this.contentType = "json";
|
|
419
|
+
}
|
|
632
420
|
}
|
|
633
|
-
this.
|
|
421
|
+
this._updatingBodyType = false;
|
|
634
422
|
}
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
// src/http/app.ts
|
|
426
|
+
import {
|
|
427
|
+
compose,
|
|
428
|
+
flattenMiddlewareToken,
|
|
429
|
+
i18n as i18n3
|
|
430
|
+
} from "@aomex/core";
|
|
431
|
+
|
|
432
|
+
// src/http/context.ts
|
|
433
|
+
import createHttpError2 from "http-errors";
|
|
434
|
+
var WebContext = class {
|
|
435
|
+
constructor(app, request, response2) {
|
|
436
|
+
this.app = app;
|
|
437
|
+
this.request = request;
|
|
438
|
+
this.response = response2;
|
|
439
|
+
request.app = response2.app = app;
|
|
440
|
+
request.res = response2.res = response2;
|
|
441
|
+
request.req = response2.req = request;
|
|
442
|
+
request.ctx = response2.ctx = this;
|
|
443
|
+
}
|
|
444
|
+
send(statusOrBody, body2) {
|
|
445
|
+
if (typeof statusOrBody === "number") {
|
|
446
|
+
this.response.statusCode = statusOrBody;
|
|
654
447
|
} else {
|
|
448
|
+
body2 = statusOrBody;
|
|
449
|
+
}
|
|
450
|
+
if (body2 !== void 0) {
|
|
451
|
+
this.response.body = body2;
|
|
452
|
+
}
|
|
453
|
+
return this;
|
|
454
|
+
}
|
|
455
|
+
throw(arg, ...args) {
|
|
456
|
+
throw createHttpError2(arg, ...args);
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
// src/http/app.ts
|
|
461
|
+
import { EOL } from "node:os";
|
|
462
|
+
import { chalk } from "@aomex/internal-tools";
|
|
463
|
+
var WebApp = class extends stream2.EventEmitter {
|
|
464
|
+
constructor(options = {}) {
|
|
465
|
+
super();
|
|
466
|
+
this.options = options;
|
|
467
|
+
this.middlewareList = [];
|
|
468
|
+
if (options.locale) {
|
|
469
|
+
i18n3.setLocale(options.locale);
|
|
470
|
+
}
|
|
471
|
+
if (options.mount) {
|
|
472
|
+
this.point = options.mount["createPoint"]();
|
|
473
|
+
this.middlewareList = flattenMiddlewareToken(options.mount);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
point;
|
|
477
|
+
middlewareList;
|
|
478
|
+
get debug() {
|
|
479
|
+
return this.options.debug ?? process.env["NODE_ENV"] !== "production";
|
|
480
|
+
}
|
|
481
|
+
callback() {
|
|
482
|
+
const fn = compose(this.middlewareList);
|
|
483
|
+
if (!this.listenerCount("error")) {
|
|
484
|
+
this.on("error", this.log.bind(this));
|
|
485
|
+
}
|
|
486
|
+
return (req, res) => {
|
|
487
|
+
const ctx = new WebContext(this, req, res);
|
|
488
|
+
return fn(ctx).then(res.flush).catch(res.onError);
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
listen = (...args) => {
|
|
492
|
+
const server = createServer(
|
|
493
|
+
{
|
|
494
|
+
IncomingMessage: WebRequest,
|
|
495
|
+
ServerResponse: WebResponse
|
|
496
|
+
},
|
|
497
|
+
this.callback()
|
|
498
|
+
);
|
|
499
|
+
return server.listen(...args);
|
|
500
|
+
};
|
|
501
|
+
log(err, ctx) {
|
|
502
|
+
if (ctx.response.statusCode === 404 || err.expose)
|
|
503
|
+
return;
|
|
504
|
+
const msgs = (err.stack || err.toString()).split(EOL, 2);
|
|
505
|
+
console.error(["", chalk.bgRed(msgs.shift()), msgs.join(EOL), ""].join(EOL));
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// src/override/web-middleware.ts
|
|
510
|
+
import { Middleware as Middleware2 } from "@aomex/core";
|
|
511
|
+
var WebMiddleware = class extends Middleware2 {
|
|
512
|
+
openapiInjector;
|
|
513
|
+
constructor(args) {
|
|
514
|
+
const { fn, openapi = {} } = typeof args === "function" ? { fn: args } : args;
|
|
515
|
+
super(fn);
|
|
516
|
+
this.openapiInjector = openapi;
|
|
517
|
+
}
|
|
518
|
+
openapi() {
|
|
519
|
+
return this.openapiInjector;
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
Middleware2.register("web", WebMiddleware);
|
|
523
|
+
|
|
524
|
+
// src/override/web-middleware-chain.ts
|
|
525
|
+
import { MiddlewareChain } from "@aomex/core";
|
|
526
|
+
var WebMiddlewareChain = class extends MiddlewareChain {
|
|
527
|
+
};
|
|
528
|
+
MiddlewareChain.register("web", WebMiddlewareChain);
|
|
529
|
+
|
|
530
|
+
// src/override/file.validator.ts
|
|
531
|
+
import { PersistentFile } from "formidable";
|
|
532
|
+
import mimeTypes2 from "mime-types";
|
|
533
|
+
import {
|
|
534
|
+
Validator,
|
|
535
|
+
magistrate,
|
|
536
|
+
Rule,
|
|
537
|
+
i18n as i18n4
|
|
538
|
+
} from "@aomex/core";
|
|
539
|
+
import { bytes } from "@aomex/internal-tools";
|
|
540
|
+
import typeIs3 from "type-is";
|
|
541
|
+
var FileValidator = class extends Validator {
|
|
542
|
+
/**
|
|
543
|
+
* 允许的最大体积
|
|
544
|
+
*
|
|
545
|
+
* 可选格式:
|
|
546
|
+
* - 1024
|
|
547
|
+
* - 2048
|
|
548
|
+
* - '15KB'
|
|
549
|
+
* - '20MB'
|
|
550
|
+
*/
|
|
551
|
+
maxSize(byte) {
|
|
552
|
+
const validator = this.copy();
|
|
553
|
+
validator.config.maxSize = typeof byte === "number" ? byte : bytes(byte);
|
|
554
|
+
return validator;
|
|
555
|
+
}
|
|
556
|
+
mimeTypes(mineOrExt, ...others) {
|
|
557
|
+
const validator = this.copy();
|
|
558
|
+
validator.config.mimeTypes = [
|
|
559
|
+
...new Set(
|
|
560
|
+
[].concat(mineOrExt).concat(others).map(mimeTypes2.contentType).filter(Boolean)
|
|
561
|
+
)
|
|
562
|
+
];
|
|
563
|
+
return validator;
|
|
564
|
+
}
|
|
565
|
+
isEmpty(value) {
|
|
566
|
+
return super.isEmpty(value) || Array.isArray(value) && !value.length;
|
|
567
|
+
}
|
|
568
|
+
validateValue(file, _key, label) {
|
|
569
|
+
const { maxSize, mimeTypes: mimeTypes3 } = this.config;
|
|
570
|
+
if (Array.isArray(file)) {
|
|
571
|
+
file = file[0];
|
|
572
|
+
}
|
|
573
|
+
if (!(file instanceof PersistentFile)) {
|
|
574
|
+
return magistrate.fail(i18n4.t("web.validator.file.must_be_file", { label }));
|
|
655
575
|
}
|
|
656
|
-
|
|
576
|
+
if (maxSize !== void 0 && file.size > maxSize) {
|
|
577
|
+
return magistrate.fail(i18n4.t("web.validator.file.too_large", { label }));
|
|
578
|
+
}
|
|
579
|
+
const hasMimeTypeLimitation = mimeTypes3 && mimeTypes3.length;
|
|
580
|
+
if (hasMimeTypeLimitation && (!file.mimetype || !typeIs3.is(file.mimetype, ...mimeTypes3))) {
|
|
581
|
+
return magistrate.fail(
|
|
582
|
+
i18n4.t("web.validator.file.unsupported_mimetype", { label })
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
return magistrate.ok(file);
|
|
586
|
+
}
|
|
587
|
+
toDocument() {
|
|
588
|
+
return {
|
|
589
|
+
type: "string",
|
|
590
|
+
format: "binary"
|
|
591
|
+
};
|
|
657
592
|
}
|
|
658
593
|
};
|
|
594
|
+
Rule.register("file", FileValidator);
|
|
659
595
|
|
|
660
596
|
// src/middleware/body.ts
|
|
661
|
-
import { validate, Validator as Validator2, rule, ValidatorError } from "@aomex/core";
|
|
662
|
-
var
|
|
663
|
-
|
|
664
|
-
|
|
597
|
+
import { validate, Validator as Validator2, rule, ValidatorError, middleware } from "@aomex/core";
|
|
598
|
+
var body = (fields) => {
|
|
599
|
+
return middleware.web({
|
|
600
|
+
fn: async (ctx, next) => {
|
|
665
601
|
try {
|
|
666
|
-
ctx.body = await validate(ctx.request.body,
|
|
667
|
-
throwIfError: true
|
|
668
|
-
});
|
|
602
|
+
ctx.body = await validate(ctx.request.body, fields);
|
|
669
603
|
return next();
|
|
670
604
|
} catch (e) {
|
|
671
605
|
ctx.throw(e instanceof ValidatorError ? 400 : 500, e);
|
|
672
606
|
}
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
}
|
|
607
|
+
},
|
|
608
|
+
openapi: {
|
|
609
|
+
onMethod(methodItem) {
|
|
610
|
+
methodItem.requestBody = {
|
|
611
|
+
content: {
|
|
612
|
+
"*/*": {
|
|
613
|
+
schema: Validator2.toDocument(rule.object(fields)).schema
|
|
614
|
+
}
|
|
615
|
+
},
|
|
616
|
+
required: Object.values(fields).some(
|
|
617
|
+
(validator) => Validator2.toDocument(validator).required
|
|
618
|
+
)
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
});
|
|
690
623
|
};
|
|
691
|
-
var body = (fields) => new WebBodyMiddleware(fields);
|
|
692
624
|
|
|
693
625
|
// src/middleware/query.ts
|
|
694
|
-
import { validate as validate2, Validator as Validator3, ValidatorError as ValidatorError2 } from "@aomex/core";
|
|
695
|
-
var
|
|
696
|
-
|
|
697
|
-
|
|
626
|
+
import { middleware as middleware2, validate as validate2, Validator as Validator3, ValidatorError as ValidatorError2 } from "@aomex/core";
|
|
627
|
+
var query = (fields) => {
|
|
628
|
+
return middleware2.web({
|
|
629
|
+
fn: async (ctx, next) => {
|
|
698
630
|
try {
|
|
699
|
-
ctx.query = await validate2(ctx.request.query,
|
|
700
|
-
throwIfError: true
|
|
701
|
-
});
|
|
631
|
+
ctx.query = await validate2(ctx.request.query, fields);
|
|
702
632
|
return next();
|
|
703
633
|
} catch (e) {
|
|
704
634
|
ctx.throw(e instanceof ValidatorError2 ? 400 : 500, e);
|
|
705
635
|
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
});
|
|
721
|
-
}
|
|
636
|
+
},
|
|
637
|
+
openapi: {
|
|
638
|
+
onMethod(methodItem) {
|
|
639
|
+
methodItem.parameters ||= [];
|
|
640
|
+
Object.entries(fields).forEach(([name, validator]) => {
|
|
641
|
+
methodItem.parameters.push({
|
|
642
|
+
name,
|
|
643
|
+
in: "query",
|
|
644
|
+
...Validator3.toDocument(validator)
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
});
|
|
722
650
|
};
|
|
723
|
-
var query = (fields) => new WebQueryMiddleware(fields);
|
|
724
651
|
|
|
725
652
|
// src/middleware/params.ts
|
|
726
|
-
import { validate as validate3, Validator as Validator4, ValidatorError as ValidatorError3 } from "@aomex/core";
|
|
727
|
-
var
|
|
728
|
-
|
|
729
|
-
|
|
653
|
+
import { middleware as middleware3, validate as validate3, Validator as Validator4, ValidatorError as ValidatorError3 } from "@aomex/core";
|
|
654
|
+
var params = (fields) => {
|
|
655
|
+
return middleware3.web({
|
|
656
|
+
fn: async (ctx, next) => {
|
|
730
657
|
try {
|
|
731
|
-
ctx.params = await validate3(ctx.request.params,
|
|
732
|
-
throwIfError: true
|
|
733
|
-
});
|
|
658
|
+
ctx.params = await validate3(ctx.request.params, fields);
|
|
734
659
|
return next();
|
|
735
660
|
} catch (e) {
|
|
736
661
|
ctx.throw(e instanceof ValidatorError3 ? 400 : 500, e);
|
|
737
662
|
}
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
});
|
|
756
|
-
}
|
|
663
|
+
},
|
|
664
|
+
openapi: {
|
|
665
|
+
onMethod(methodItem) {
|
|
666
|
+
methodItem.parameters ||= [];
|
|
667
|
+
Object.entries(fields).forEach(([name, validator]) => {
|
|
668
|
+
const validatorDocument = Validator4.toDocument(validator);
|
|
669
|
+
methodItem.parameters.push({
|
|
670
|
+
name,
|
|
671
|
+
in: "path",
|
|
672
|
+
...validatorDocument,
|
|
673
|
+
// path必填参数
|
|
674
|
+
required: validatorDocument.required === true
|
|
675
|
+
});
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
});
|
|
757
680
|
};
|
|
758
|
-
var params = (fields) => new WebParamMiddleware(fields);
|
|
759
681
|
|
|
760
682
|
// src/middleware/response.ts
|
|
761
|
-
import {
|
|
762
|
-
forceToValidator,
|
|
763
|
-
validate as validate4,
|
|
764
|
-
Validator as Validator5
|
|
765
|
-
} from "@aomex/core";
|
|
766
|
-
import { Stream as Stream2 } from "node:stream";
|
|
767
|
-
var num3length = /^\d{3}$/;
|
|
768
|
-
var num2length = /^\d{2}x$/i;
|
|
769
|
-
var num1length = /^\dxx$/i;
|
|
770
|
-
var response = (options) => new WebResponseMiddleware(options);
|
|
683
|
+
import { toValidator, Validator as Validator5 } from "@aomex/core";
|
|
771
684
|
var WebResponseMiddleware = class extends WebMiddleware {
|
|
772
685
|
constructor(options) {
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
if (schema
|
|
785
|
-
|
|
686
|
+
super(async (_, next) => next());
|
|
687
|
+
this.options = options;
|
|
688
|
+
}
|
|
689
|
+
openapi() {
|
|
690
|
+
return {
|
|
691
|
+
onMethod: (methodItem) => {
|
|
692
|
+
methodItem.responses ||= {};
|
|
693
|
+
const { statusCode, schema, headers, example, description = "" } = this.options;
|
|
694
|
+
const resItem = methodItem.responses[statusCode] = {
|
|
695
|
+
description
|
|
696
|
+
};
|
|
697
|
+
if (schema) {
|
|
698
|
+
resItem.content = {
|
|
699
|
+
[this.getContentType()]: {
|
|
700
|
+
schema: Validator5.toDocument(toValidator(schema)).schema,
|
|
701
|
+
example
|
|
702
|
+
}
|
|
703
|
+
};
|
|
786
704
|
}
|
|
787
|
-
if (
|
|
788
|
-
|
|
705
|
+
if (headers) {
|
|
706
|
+
resItem.headers = Object.fromEntries(
|
|
707
|
+
Object.entries(headers).map(([key, header]) => [
|
|
708
|
+
key,
|
|
709
|
+
Validator5.toDocument(header)
|
|
710
|
+
])
|
|
711
|
+
);
|
|
789
712
|
}
|
|
790
|
-
} catch (e) {
|
|
791
|
-
ctx.throw(500, e);
|
|
792
713
|
}
|
|
793
|
-
}
|
|
794
|
-
this.options = options;
|
|
795
|
-
}
|
|
796
|
-
matchStatusCode(expected, statusCode) {
|
|
797
|
-
expected = expected.toString();
|
|
798
|
-
if (num3length.test(expected)) {
|
|
799
|
-
return statusCode === Number(expected);
|
|
800
|
-
}
|
|
801
|
-
if (num1length.test(expected)) {
|
|
802
|
-
return expected[0] === statusCode.toString()[0];
|
|
803
|
-
}
|
|
804
|
-
if (num2length.test(expected)) {
|
|
805
|
-
return expected.slice(0, 2) === statusCode.toString().slice(0, 2);
|
|
806
|
-
}
|
|
807
|
-
return false;
|
|
714
|
+
};
|
|
808
715
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
);
|
|
716
|
+
getContentType() {
|
|
717
|
+
let { contentType: contentType3, schema } = this.options;
|
|
718
|
+
if (!contentType3) {
|
|
719
|
+
const validator = toValidator(schema);
|
|
720
|
+
const docs = validator["toDocument"]();
|
|
721
|
+
switch (docs.type) {
|
|
722
|
+
case "array":
|
|
723
|
+
case "object":
|
|
724
|
+
contentType3 = getMimeType("json");
|
|
725
|
+
break;
|
|
726
|
+
case "boolean":
|
|
727
|
+
case "integer":
|
|
728
|
+
case "number":
|
|
729
|
+
contentType3 = getMimeType("text");
|
|
730
|
+
break;
|
|
731
|
+
case "string":
|
|
732
|
+
if (docs.format === "binary") {
|
|
733
|
+
contentType3 = getMimeType("bin");
|
|
734
|
+
} else {
|
|
735
|
+
contentType3 = getMimeType("text");
|
|
736
|
+
}
|
|
737
|
+
break;
|
|
738
|
+
default:
|
|
739
|
+
contentType3 = "*/*";
|
|
740
|
+
}
|
|
741
|
+
} else if (!contentType3.includes("*")) {
|
|
742
|
+
contentType3 = getMimeType(contentType3);
|
|
837
743
|
}
|
|
744
|
+
return contentType3.split(";")[0];
|
|
838
745
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
}
|
|
746
|
+
};
|
|
747
|
+
var response = (options) => {
|
|
748
|
+
return new WebResponseMiddleware(options);
|
|
843
749
|
};
|
|
844
750
|
|
|
845
751
|
// src/index.ts
|
|
@@ -847,25 +753,18 @@ import { default as default2 } from "http-errors";
|
|
|
847
753
|
import { default as default3 } from "statuses";
|
|
848
754
|
export {
|
|
849
755
|
FileValidator,
|
|
850
|
-
METHOD,
|
|
851
756
|
WebApp,
|
|
852
|
-
WebBodyMiddleware,
|
|
853
|
-
WebChain,
|
|
854
757
|
WebContext,
|
|
855
758
|
WebMiddleware,
|
|
856
|
-
|
|
857
|
-
WebQueryMiddleware,
|
|
759
|
+
WebMiddlewareChain,
|
|
858
760
|
WebRequest,
|
|
859
761
|
WebResponse,
|
|
860
762
|
WebResponseMiddleware,
|
|
861
763
|
body,
|
|
862
764
|
default2 as createHttpError,
|
|
863
|
-
createHttpServer,
|
|
864
|
-
getMimeType,
|
|
865
765
|
params,
|
|
866
766
|
query,
|
|
867
767
|
response,
|
|
868
|
-
skip,
|
|
869
768
|
default3 as statuses
|
|
870
769
|
};
|
|
871
770
|
//# sourceMappingURL=index.js.map
|