@aomex/web 0.0.28 → 1.0.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 +6 -59
- package/LICENSE +1 -1
- package/dist/index.d.ts +225 -272
- package/dist/index.js +524 -623
- package/dist/index.js.map +1 -1
- package/package.json +28 -34
- 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;
|
|
152
|
-
}
|
|
153
|
-
if (body2 !== void 0) {
|
|
154
|
-
this.response.body = body2;
|
|
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"
|
|
155
21
|
}
|
|
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
|
-
|
|
184
|
-
// src/util/method.ts
|
|
185
|
-
var METHOD = /* @__PURE__ */ ((METHOD2) => {
|
|
186
|
-
METHOD2["GET"] = "GET";
|
|
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 || {});
|
|
195
24
|
|
|
196
|
-
// src/
|
|
197
|
-
import { createServer } from "http";
|
|
25
|
+
// src/http/app.ts
|
|
26
|
+
import { createServer } from "node:http";
|
|
27
|
+
import { EventEmitter } from "node:stream";
|
|
198
28
|
|
|
199
|
-
// src/
|
|
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,
|
|
@@ -244,10 +76,9 @@ var WebRequest = class extends IncomingMessage {
|
|
|
244
76
|
});
|
|
245
77
|
return this._body = form.parse(this).then(([fields, files]) => {
|
|
246
78
|
const fixedFields = Object.fromEntries(
|
|
247
|
-
Object.entries(fields).map(([key, values]) =>
|
|
248
|
-
key,
|
|
249
|
-
|
|
250
|
-
])
|
|
79
|
+
Object.entries(fields).map(([key, values]) => {
|
|
80
|
+
return [key, values == void 0 || values.length > 1 ? values : values[0]];
|
|
81
|
+
})
|
|
251
82
|
);
|
|
252
83
|
return this._body = { ...fixedFields, ...files };
|
|
253
84
|
});
|
|
@@ -258,22 +89,25 @@ var WebRequest = class extends IncomingMessage {
|
|
|
258
89
|
return this._body = fields;
|
|
259
90
|
});
|
|
260
91
|
}
|
|
261
|
-
get
|
|
262
|
-
|
|
263
|
-
return
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
return result === false ? null : result;
|
|
271
|
-
}
|
|
272
|
-
get accept() {
|
|
273
|
-
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());
|
|
274
101
|
}
|
|
275
|
-
get
|
|
276
|
-
|
|
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] || "";
|
|
277
111
|
}
|
|
278
112
|
/**
|
|
279
113
|
* 包括了协议,域名,端口和路径的完整链接
|
|
@@ -281,18 +115,14 @@ var WebRequest = class extends IncomingMessage {
|
|
|
281
115
|
get href() {
|
|
282
116
|
return this.origin + this.url;
|
|
283
117
|
}
|
|
118
|
+
get ip() {
|
|
119
|
+
return requestIP.getClientIp(this) || "";
|
|
120
|
+
}
|
|
284
121
|
get origin() {
|
|
285
122
|
return `${this.protocol}://${this.host}`;
|
|
286
123
|
}
|
|
287
|
-
get
|
|
288
|
-
|
|
289
|
-
if (!host) {
|
|
290
|
-
if (this.httpVersionMajor >= 2) {
|
|
291
|
-
host = this.headers[":authority"];
|
|
292
|
-
}
|
|
293
|
-
host ||= this.headers["host"];
|
|
294
|
-
}
|
|
295
|
-
return host && host.split(/\s*,\s*/, 1)[0] || "";
|
|
124
|
+
get pathname() {
|
|
125
|
+
return this.URL.pathname;
|
|
296
126
|
}
|
|
297
127
|
get protocol() {
|
|
298
128
|
if (this.socket.encrypted)
|
|
@@ -300,160 +130,97 @@ var WebRequest = class extends IncomingMessage {
|
|
|
300
130
|
const proto = this.headers["x-forwarded-proto"];
|
|
301
131
|
return proto && proto.split(/\s*,\s*/, 1)[0] || "http";
|
|
302
132
|
}
|
|
303
|
-
|
|
304
|
-
|
|
133
|
+
/**
|
|
134
|
+
* 查询字符串对象
|
|
135
|
+
*/
|
|
136
|
+
get query() {
|
|
137
|
+
return this._query ||= qs.parse(this.querystring);
|
|
305
138
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (status < 200)
|
|
312
|
-
return false;
|
|
313
|
-
if (status > 299 && status !== 304)
|
|
314
|
-
return false;
|
|
315
|
-
return fresh(this.headers, this.res.getHeaders());
|
|
139
|
+
/**
|
|
140
|
+
* 查询字符串
|
|
141
|
+
*/
|
|
142
|
+
get querystring() {
|
|
143
|
+
return this.URL.search.slice(1);
|
|
316
144
|
}
|
|
317
145
|
/**
|
|
318
|
-
*
|
|
146
|
+
* 搜索字符串,比查询字符串多了一个开头问号(?)
|
|
319
147
|
*/
|
|
320
|
-
get
|
|
321
|
-
return this.
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
148
|
+
get search() {
|
|
149
|
+
return this.URL.search;
|
|
150
|
+
}
|
|
151
|
+
get secure() {
|
|
152
|
+
return this.protocol === "https";
|
|
325
153
|
}
|
|
326
154
|
get URL() {
|
|
327
155
|
return this._parsedUrl ||= new URL(this.href);
|
|
328
156
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
var createHttpServer = (listener) => {
|
|
333
|
-
return createServer(
|
|
334
|
-
{
|
|
335
|
-
IncomingMessage: WebRequest,
|
|
336
|
-
// @ts-ignore
|
|
337
|
-
ServerResponse: WebResponse
|
|
338
|
-
},
|
|
339
|
-
listener
|
|
340
|
-
);
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
// src/app/app.ts
|
|
344
|
-
var WebApp = class extends EventEmitter {
|
|
345
|
-
constructor(options = {}) {
|
|
346
|
-
super();
|
|
347
|
-
this.options = options;
|
|
348
|
-
}
|
|
349
|
-
chainPoints = [];
|
|
350
|
-
middlewareList = [];
|
|
351
|
-
get debug() {
|
|
352
|
-
return this.options.debug ?? process.env["NODE_ENV"] !== "production";
|
|
353
|
-
}
|
|
354
|
-
listen = (...args) => {
|
|
355
|
-
const server = createHttpServer(this.callback());
|
|
356
|
-
return server.listen(...args);
|
|
357
|
-
};
|
|
358
|
-
callback() {
|
|
359
|
-
const fn = compose2(this.middlewareList);
|
|
360
|
-
if (!this.listenerCount("error")) {
|
|
361
|
-
this.on("error", this.log.bind(this));
|
|
362
|
-
}
|
|
363
|
-
return (req, res) => {
|
|
364
|
-
return fn(new WebContext(this, req, res)).then(res.flush.bind(res)).catch(res.onError);
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
log(err) {
|
|
368
|
-
if (this.options.silent)
|
|
369
|
-
return;
|
|
370
|
-
if ((err.status || err.statusCode) === 404 || err.expose)
|
|
371
|
-
return;
|
|
372
|
-
const msgs = (err.stack || err.toString()).split(EOL, 2);
|
|
373
|
-
console.error(
|
|
374
|
-
["", chalk.bgRed(msgs.shift()), msgs.join(EOL), ""].join(EOL)
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
on(eventName, listener) {
|
|
378
|
-
return super.on(eventName, listener);
|
|
379
|
-
}
|
|
380
|
-
mount(middleware2) {
|
|
381
|
-
if (middleware2 === null)
|
|
382
|
-
return;
|
|
383
|
-
if (middleware2 instanceof Chain2) {
|
|
384
|
-
this.chainPoints.push(Chain2.createSplitPoint(middleware2));
|
|
385
|
-
}
|
|
386
|
-
this.middlewareList.push(middleware2);
|
|
157
|
+
matchContentType(type, ...types) {
|
|
158
|
+
const result = typeIs(this, type, ...types);
|
|
159
|
+
return result === false ? null : result;
|
|
387
160
|
}
|
|
388
161
|
};
|
|
389
162
|
|
|
390
|
-
// src/
|
|
163
|
+
// src/http/response.ts
|
|
391
164
|
import { ServerResponse } from "node:http";
|
|
392
|
-
import stream, { Stream } from "node:stream";
|
|
393
165
|
import assert from "node:assert";
|
|
394
166
|
import statuses from "statuses";
|
|
395
|
-
import
|
|
396
|
-
|
|
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
|
|
397
185
|
import contentType2 from "content-type";
|
|
186
|
+
import stream from "stream";
|
|
398
187
|
import destroy from "destroy";
|
|
188
|
+
import createHttpError, { isHttpError } from "http-errors";
|
|
189
|
+
import encodeUrl from "encodeurl";
|
|
190
|
+
import escapeHtml from "escape-html";
|
|
399
191
|
import contentDisposition from "content-disposition";
|
|
400
|
-
import
|
|
401
|
-
import { extname as extname2 } from "node:path";
|
|
192
|
+
import { extname } from "node:path";
|
|
402
193
|
import { createReadStream } from "node:fs";
|
|
403
|
-
import typeIs3 from "type-is";
|
|
404
194
|
import vary from "vary";
|
|
195
|
+
import typeIs2 from "type-is";
|
|
405
196
|
import cookie2 from "cookie";
|
|
406
197
|
var WebResponse = class extends ServerResponse {
|
|
407
198
|
app;
|
|
199
|
+
res;
|
|
408
200
|
ctx;
|
|
409
201
|
_body = null;
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
this.
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
removeHeaders(...headers) {
|
|
435
|
-
headers.forEach(this.removeHeader.bind(this));
|
|
436
|
-
}
|
|
437
|
-
redirect(status, url) {
|
|
438
|
-
url = typeof url === "string" ? url : status.toString();
|
|
439
|
-
this.statusCode = typeof status === "string" ? 302 : status;
|
|
440
|
-
this.setHeader("location", encodeUrl(url));
|
|
441
|
-
if (this.req.accept.types("html")) {
|
|
442
|
-
url = escapeHtml(url);
|
|
443
|
-
this.contentType = "html";
|
|
444
|
-
this.body = `Redirecting to <a href="${url}">${url}</a>.`;
|
|
445
|
-
} else {
|
|
446
|
-
this.contentType = "text";
|
|
447
|
-
this.body = `Redirecting to ${url}.`;
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
download(filePath, options = {}) {
|
|
451
|
-
this.contentType = extname2(filePath);
|
|
452
|
-
this.setHeader(
|
|
453
|
-
"content-disposition",
|
|
454
|
-
contentDisposition(filePath, options)
|
|
455
|
-
);
|
|
456
|
-
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);
|
|
457
224
|
}
|
|
458
225
|
get contentLength() {
|
|
459
226
|
const length = this.getHeader("Content-Length");
|
|
@@ -470,69 +237,84 @@ var WebResponse = class extends ServerResponse {
|
|
|
470
237
|
set contentType(typeOrFilenameOrExt) {
|
|
471
238
|
const mimeType = getMimeType(typeOrFilenameOrExt);
|
|
472
239
|
try {
|
|
473
|
-
if (mimeType
|
|
474
|
-
throw new Error();
|
|
240
|
+
if (!mimeType)
|
|
241
|
+
throw new Error("");
|
|
475
242
|
contentType2.parse(mimeType);
|
|
476
243
|
} catch {
|
|
477
|
-
throw new TypeError(
|
|
244
|
+
throw new TypeError(`\u4E0D\u5408\u6CD5\u7684\u7C7B\u578B\uFF1A'${typeOrFilenameOrExt}'`);
|
|
478
245
|
}
|
|
479
246
|
this.setHeader("Content-Type", mimeType);
|
|
480
|
-
this.
|
|
247
|
+
this.updateBodyType();
|
|
481
248
|
}
|
|
482
249
|
get body() {
|
|
483
250
|
return this._body;
|
|
484
251
|
}
|
|
485
252
|
set body(val) {
|
|
486
|
-
this._body = val;
|
|
253
|
+
this._body = val == null ? null : val;
|
|
487
254
|
this._explicitBody = true;
|
|
488
255
|
if (!this._explicitStatus) {
|
|
489
256
|
this.statusCode = 200;
|
|
490
257
|
}
|
|
491
|
-
this.
|
|
258
|
+
this.updateBodyType();
|
|
492
259
|
}
|
|
493
|
-
|
|
494
|
-
|
|
260
|
+
download(filePath, options = {}) {
|
|
261
|
+
this.contentType = extname(filePath);
|
|
262
|
+
this.setHeader("content-disposition", contentDisposition(filePath, options));
|
|
263
|
+
this.body = createReadStream(filePath);
|
|
495
264
|
}
|
|
496
265
|
flush() {
|
|
497
266
|
if (!this.writable || this.headersSent)
|
|
498
267
|
return;
|
|
499
|
-
this.determineHeaders();
|
|
500
268
|
let output = this.body;
|
|
501
269
|
if (statuses.empty[this.statusCode])
|
|
502
|
-
return this.end();
|
|
270
|
+
return void this.end();
|
|
503
271
|
if (output === null) {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
this._body = String(null);
|
|
272
|
+
if (this.contentType === "application/json") {
|
|
273
|
+
this.body = String(null);
|
|
274
|
+
} else if (this._explicitBody) {
|
|
275
|
+
this.body = "";
|
|
509
276
|
} else {
|
|
510
|
-
this.
|
|
277
|
+
this.statusCode = this.statusCode;
|
|
278
|
+
this.body = String(this.statusMessage || statuses.message[this.statusCode]);
|
|
511
279
|
}
|
|
512
|
-
this.determineHeaders();
|
|
513
280
|
output = this.body;
|
|
514
281
|
}
|
|
515
282
|
if (this.req.method === "HEAD")
|
|
516
|
-
return this.end();
|
|
283
|
+
return void this.end();
|
|
517
284
|
if (typeof output === "string")
|
|
518
|
-
return this.end(output);
|
|
285
|
+
return void this.end(output);
|
|
519
286
|
if (Buffer.isBuffer(output))
|
|
520
|
-
return this.end(output);
|
|
287
|
+
return void this.end(output);
|
|
521
288
|
if (output instanceof Stream) {
|
|
522
|
-
stream.finished(this, () => {
|
|
523
|
-
destroy(output);
|
|
524
|
-
});
|
|
525
289
|
if (!output.listenerCount("error")) {
|
|
526
290
|
output.once("error", this.onError);
|
|
527
291
|
}
|
|
528
|
-
|
|
292
|
+
stream.finished(this, () => {
|
|
293
|
+
destroy(output);
|
|
294
|
+
});
|
|
295
|
+
return void output.pipe(this);
|
|
529
296
|
}
|
|
530
|
-
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;
|
|
531
313
|
}
|
|
532
314
|
onError(error) {
|
|
533
315
|
if (error == null)
|
|
534
316
|
return;
|
|
535
|
-
const err = isHttpError(error) ? error :
|
|
317
|
+
const err = isHttpError(error) ? error : createHttpError(error);
|
|
536
318
|
this.removeHeaders(...this.getHeaderNames());
|
|
537
319
|
err.headers && this.setHeaders(err.headers);
|
|
538
320
|
const code = err.status || err.statusCode;
|
|
@@ -547,69 +329,75 @@ var WebResponse = class extends ServerResponse {
|
|
|
547
329
|
this.app.emit("error", err, this.ctx);
|
|
548
330
|
this.flush();
|
|
549
331
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
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
|
+
}
|
|
553
372
|
}
|
|
554
373
|
vary(field) {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
varyAppend(header, field) {
|
|
558
|
-
return vary.append(header, field);
|
|
559
|
-
}
|
|
560
|
-
get cookie() {
|
|
561
|
-
if (this._cookie)
|
|
562
|
-
return this._cookie;
|
|
563
|
-
const defaultSerializeOptions = {
|
|
564
|
-
path: "/",
|
|
565
|
-
sameSite: false,
|
|
566
|
-
secure: this.req.secure,
|
|
567
|
-
httpOnly: true,
|
|
568
|
-
...this.app.options.cookie?.set
|
|
569
|
-
};
|
|
570
|
-
this._cookie = {
|
|
571
|
-
set: (name, value, options) => {
|
|
572
|
-
const setCookie = this.getHeader("Set-Cookie") || [];
|
|
573
|
-
setCookie.push(
|
|
574
|
-
cookie2.serialize(name, value, {
|
|
575
|
-
...defaultSerializeOptions,
|
|
576
|
-
...options
|
|
577
|
-
})
|
|
578
|
-
);
|
|
579
|
-
this.setHeader("Set-Cookie", setCookie);
|
|
580
|
-
},
|
|
581
|
-
remove: (name, options) => {
|
|
582
|
-
this.cookie.set(name, "", {
|
|
583
|
-
...defaultSerializeOptions,
|
|
584
|
-
...options,
|
|
585
|
-
maxAge: void 0,
|
|
586
|
-
expires: /* @__PURE__ */ new Date(0)
|
|
587
|
-
});
|
|
588
|
-
}
|
|
589
|
-
};
|
|
590
|
-
return this._cookie;
|
|
374
|
+
vary(this, field);
|
|
375
|
+
return this.getHeader("Vary");
|
|
591
376
|
}
|
|
592
377
|
setStatus(code) {
|
|
593
|
-
assert(code >= 100 && code <= 999
|
|
378
|
+
assert(code >= 100 && code <= 999);
|
|
594
379
|
this._statusCode = code;
|
|
595
380
|
this._explicitStatus = true;
|
|
596
|
-
|
|
597
|
-
this.statusMessage = String(statuses.message[code]);
|
|
598
|
-
}
|
|
381
|
+
this.statusMessage = String(statuses.message[code] || code);
|
|
599
382
|
if (statuses.empty[code]) {
|
|
600
383
|
this.body = null;
|
|
601
384
|
} else {
|
|
602
|
-
this.
|
|
385
|
+
this.updateBodyType();
|
|
603
386
|
}
|
|
604
387
|
}
|
|
605
|
-
|
|
606
|
-
if (this.
|
|
388
|
+
updateBodyType() {
|
|
389
|
+
if (this._updatingBodyType)
|
|
607
390
|
return;
|
|
608
|
-
this.
|
|
391
|
+
this._updatingBodyType = true;
|
|
609
392
|
const { body: body2 } = this;
|
|
610
393
|
const missType = !this.hasHeader("Content-Type");
|
|
611
394
|
if (body2 === null) {
|
|
612
|
-
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
|
+
}
|
|
613
401
|
} else if (typeof body2 === "string") {
|
|
614
402
|
this.contentLength = Buffer.byteLength(body2);
|
|
615
403
|
if (missType) {
|
|
@@ -625,219 +413,339 @@ var WebResponse = class extends ServerResponse {
|
|
|
625
413
|
this.contentType = "bin";
|
|
626
414
|
}
|
|
627
415
|
} else {
|
|
628
|
-
this.contentType = "json";
|
|
629
416
|
this.contentLength = Buffer.byteLength(JSON.stringify(body2));
|
|
417
|
+
if (missType) {
|
|
418
|
+
this.contentType = "json";
|
|
419
|
+
}
|
|
630
420
|
}
|
|
631
|
-
this.
|
|
421
|
+
this._updatingBodyType = false;
|
|
632
422
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
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;
|
|
652
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 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 }));
|
|
575
|
+
}
|
|
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
|
+
);
|
|
653
584
|
}
|
|
654
|
-
|
|
585
|
+
return magistrate.ok(file);
|
|
586
|
+
}
|
|
587
|
+
toDocument() {
|
|
588
|
+
return {
|
|
589
|
+
type: "string",
|
|
590
|
+
format: "binary"
|
|
591
|
+
};
|
|
655
592
|
}
|
|
656
593
|
};
|
|
594
|
+
Rule.register("file", FileValidator);
|
|
657
595
|
|
|
658
596
|
// src/middleware/body.ts
|
|
659
|
-
import { validate, Validator as Validator2, rule, ValidatorError } from "@aomex/core";
|
|
660
|
-
var
|
|
661
|
-
|
|
662
|
-
|
|
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) => {
|
|
663
601
|
try {
|
|
664
|
-
ctx.body = await validate(ctx.request.body,
|
|
665
|
-
throwIfError: true
|
|
666
|
-
});
|
|
602
|
+
ctx.body = await validate(ctx.request.body, fields);
|
|
667
603
|
return next();
|
|
668
604
|
} catch (e) {
|
|
669
605
|
ctx.throw(e instanceof ValidatorError ? 400 : 500, e);
|
|
670
606
|
}
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
}
|
|
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
|
+
});
|
|
688
623
|
};
|
|
689
|
-
var body = (fields) => new WebBodyMiddleware(fields);
|
|
690
624
|
|
|
691
625
|
// src/middleware/query.ts
|
|
692
|
-
import { validate as validate2, Validator as Validator3, ValidatorError as ValidatorError2 } from "@aomex/core";
|
|
693
|
-
var
|
|
694
|
-
|
|
695
|
-
|
|
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) => {
|
|
696
630
|
try {
|
|
697
|
-
ctx.query = await validate2(ctx.request.query,
|
|
698
|
-
throwIfError: true
|
|
699
|
-
});
|
|
631
|
+
ctx.query = await validate2(ctx.request.query, fields);
|
|
700
632
|
return next();
|
|
701
633
|
} catch (e) {
|
|
702
634
|
ctx.throw(e instanceof ValidatorError2 ? 400 : 500, e);
|
|
703
635
|
}
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
});
|
|
719
|
-
}
|
|
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
|
+
});
|
|
720
650
|
};
|
|
721
|
-
var query = (fields) => new WebQueryMiddleware(fields);
|
|
722
651
|
|
|
723
652
|
// src/middleware/params.ts
|
|
724
|
-
import { validate as validate3, Validator as Validator4, ValidatorError as ValidatorError3 } from "@aomex/core";
|
|
725
|
-
var
|
|
726
|
-
|
|
727
|
-
|
|
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) => {
|
|
728
657
|
try {
|
|
729
|
-
ctx.params = await validate3(ctx.request.params,
|
|
730
|
-
throwIfError: true
|
|
731
|
-
});
|
|
658
|
+
ctx.params = await validate3(ctx.request.params, fields);
|
|
732
659
|
return next();
|
|
733
660
|
} catch (e) {
|
|
734
661
|
ctx.throw(e instanceof ValidatorError3 ? 400 : 500, e);
|
|
735
662
|
}
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
});
|
|
754
|
-
}
|
|
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
|
+
});
|
|
755
680
|
};
|
|
756
|
-
var params = (fields) => new WebParamMiddleware(fields);
|
|
757
681
|
|
|
758
682
|
// src/middleware/response.ts
|
|
759
|
-
import {
|
|
760
|
-
forceToValidator,
|
|
761
|
-
validate as validate4,
|
|
762
|
-
Validator as Validator5
|
|
763
|
-
} from "@aomex/core";
|
|
764
|
-
import { Stream as Stream2 } from "node:stream";
|
|
765
|
-
var num3length = /^\d{3}$/;
|
|
766
|
-
var num2length = /^\d{2}x$/i;
|
|
767
|
-
var num1length = /^\dxx$/i;
|
|
768
|
-
var response = (options) => new WebResponseMiddleware(options);
|
|
683
|
+
import { toValidator, Validator as Validator5 } from "@aomex/core";
|
|
769
684
|
var WebResponseMiddleware = class extends WebMiddleware {
|
|
770
685
|
constructor(options) {
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
if (schema
|
|
783
|
-
|
|
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
|
+
};
|
|
784
704
|
}
|
|
785
|
-
if (
|
|
786
|
-
|
|
705
|
+
if (headers) {
|
|
706
|
+
resItem.headers = Object.fromEntries(
|
|
707
|
+
Object.entries(headers).map(([key, header]) => [
|
|
708
|
+
key,
|
|
709
|
+
Validator5.toDocument(header)
|
|
710
|
+
])
|
|
711
|
+
);
|
|
787
712
|
}
|
|
788
|
-
} catch (e) {
|
|
789
|
-
ctx.throw(500, e);
|
|
790
713
|
}
|
|
791
|
-
}
|
|
792
|
-
this.options = options;
|
|
793
|
-
}
|
|
794
|
-
matchStatusCode(expected, statusCode) {
|
|
795
|
-
expected = expected.toString();
|
|
796
|
-
if (num3length.test(expected)) {
|
|
797
|
-
return statusCode === Number(expected);
|
|
798
|
-
}
|
|
799
|
-
if (num1length.test(expected)) {
|
|
800
|
-
return expected[0] === statusCode.toString()[0];
|
|
801
|
-
}
|
|
802
|
-
if (num2length.test(expected)) {
|
|
803
|
-
return expected.slice(0, 2) === statusCode.toString().slice(0, 2);
|
|
804
|
-
}
|
|
805
|
-
return false;
|
|
714
|
+
};
|
|
806
715
|
}
|
|
807
|
-
|
|
808
|
-
|
|
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
|
-
);
|
|
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);
|
|
835
743
|
}
|
|
744
|
+
return contentType3.split(";")[0];
|
|
836
745
|
}
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
}
|
|
746
|
+
};
|
|
747
|
+
var response = (options) => {
|
|
748
|
+
return new WebResponseMiddleware(options);
|
|
841
749
|
};
|
|
842
750
|
|
|
843
751
|
// src/index.ts
|
|
@@ -845,25 +753,18 @@ import { default as default2 } from "http-errors";
|
|
|
845
753
|
import { default as default3 } from "statuses";
|
|
846
754
|
export {
|
|
847
755
|
FileValidator,
|
|
848
|
-
METHOD,
|
|
849
756
|
WebApp,
|
|
850
|
-
WebBodyMiddleware,
|
|
851
|
-
WebChain,
|
|
852
757
|
WebContext,
|
|
853
758
|
WebMiddleware,
|
|
854
|
-
|
|
855
|
-
WebQueryMiddleware,
|
|
759
|
+
WebMiddlewareChain,
|
|
856
760
|
WebRequest,
|
|
857
761
|
WebResponse,
|
|
858
762
|
WebResponseMiddleware,
|
|
859
763
|
body,
|
|
860
764
|
default2 as createHttpError,
|
|
861
|
-
createHttpServer,
|
|
862
|
-
getMimeType,
|
|
863
765
|
params,
|
|
864
766
|
query,
|
|
865
767
|
response,
|
|
866
|
-
skip,
|
|
867
768
|
default3 as statuses
|
|
868
769
|
};
|
|
869
770
|
//# sourceMappingURL=index.js.map
|