@aomex/web
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.5.0](https://github.com/aomex/aomex/compare/v1.4.0...v1.5.0) (2024-07-04) | ||
### Features | ||
* **web:** request.body改为同步获取 ([bbc2075](https://github.com/aomex/aomex/commit/bbc207535cb97ebb8e928ca07f8d51056259cfd2)) | ||
# [1.4.0](https://github.com/aomex/aomex/compare/v1.3.0...v1.4.0) (2024-06-29) | ||
@@ -8,0 +19,0 @@ |
@@ -60,2 +60,3 @@ import { I18nMessage, Next, Middleware, MixinMiddleware, OpenAPI, Validator, TransformedValidator, magistrate, I18n, ValidatorToken } from '@aomex/core'; | ||
private _body?; | ||
private _rawBody?; | ||
private _parsedUrl; | ||
@@ -71,3 +72,3 @@ private _query?; | ||
}; | ||
get body(): Promise<unknown>; | ||
get body(): Record<string, unknown>; | ||
get fresh(): boolean; | ||
@@ -91,2 +92,3 @@ get host(): string; | ||
get querystring(): string; | ||
get rawBody(): string; | ||
/** | ||
@@ -93,0 +95,0 @@ * 搜索字符串,比查询字符串多了一个开头问号(?) |
@@ -39,4 +39,2 @@ // src/i18n/locales/zh-cn.ts | ||
import qs from "qs"; | ||
import formidable from "formidable"; | ||
import coBody from "co-body"; | ||
import typeIs from "type-is"; | ||
@@ -57,2 +55,3 @@ import requestIP from "request-ip"; | ||
_body; | ||
_rawBody; | ||
_parsedUrl = null; | ||
@@ -80,26 +79,3 @@ _query; | ||
get body() { | ||
if (this._body) return Promise.resolve(this._body); | ||
if (this.matchContentType("multipart/*") !== null) { | ||
const form = formidable({ | ||
hashAlgorithm: "md5", | ||
keepExtensions: true, | ||
maxFileSize: 1e3 * 1024 * 1024, | ||
allowEmptyFiles: true | ||
}); | ||
return this._body = form.parse(this).then(([fields, files]) => { | ||
const fixedFields = Object.fromEntries( | ||
Object.entries(fields).map(([key, values]) => { | ||
return [key, values == void 0 || values.length > 1 ? values : values[0]]; | ||
}) | ||
); | ||
return this._body = { ...fixedFields, ...files }; | ||
}); | ||
} | ||
return this._body = coBody(this, { | ||
returnRawBody: false, | ||
// @ts-expect-error | ||
onProtoPoisoning: "remove" | ||
}).then((fields) => { | ||
return this._body = fields; | ||
}); | ||
return this._body || {}; | ||
} | ||
@@ -155,2 +131,5 @@ get fresh() { | ||
} | ||
get rawBody() { | ||
return this._rawBody || ""; | ||
} | ||
/** | ||
@@ -473,2 +452,43 @@ * 搜索字符串,比查询字符串多了一个开头问号(?) | ||
import { styleText } from "node:util"; | ||
// src/middleware/parse-body.ts | ||
import { middleware } from "@aomex/core"; | ||
import formidable from "formidable"; | ||
import coBody from "co-body"; | ||
var jsonTypes = ["json", "application/*+json", "application/csp-report"]; | ||
var formTypes = ["urlencoded"]; | ||
var textTypes = ["text"]; | ||
var parseBody = () => { | ||
return middleware.web(async (ctx, next) => { | ||
const { request } = ctx; | ||
if (!request.contentType) return next(); | ||
const methodName = request.matchContentType(...jsonTypes) ? "json" : request.matchContentType(...formTypes) ? "form" : request.matchContentType(...textTypes) ? "text" : false; | ||
if (methodName) { | ||
const { parsed, raw } = await coBody[methodName](request, { | ||
returnRawBody: true, | ||
// @ts-expect-error | ||
onProtoPoisoning: "remove" | ||
}); | ||
request["_body"] = parsed; | ||
request["_rawBody"] = raw; | ||
} else if (request.matchContentType("multipart/*")) { | ||
const form = formidable({ | ||
hashAlgorithm: "md5", | ||
keepExtensions: true, | ||
maxFileSize: 1e3 * 1024 * 1024, | ||
allowEmptyFiles: true | ||
}); | ||
const [fields, files] = await form.parse(request); | ||
const fixedFields = Object.fromEntries( | ||
Object.entries(fields).map(([key, values]) => { | ||
return [key, values == void 0 || values.length > 1 ? values : values[0]]; | ||
}) | ||
); | ||
request["_body"] = { ...fixedFields, ...files }; | ||
} | ||
return next(); | ||
}); | ||
}; | ||
// src/http/app.ts | ||
var WebApp = class extends stream2.EventEmitter { | ||
@@ -488,3 +508,3 @@ constructor(options = {}) { | ||
callback() { | ||
const fn = compose(this.middlewareList); | ||
const fn = compose([parseBody(), ...this.middlewareList]); | ||
if (!this.listenerCount("error")) { | ||
@@ -629,5 +649,5 @@ this.on("error", this.log.bind(this)); | ||
// src/middleware/body.ts | ||
import { validate, Validator as Validator2, rule, ValidatorError, middleware } from "@aomex/core"; | ||
import { validate, Validator as Validator2, rule, ValidatorError, middleware as middleware2 } from "@aomex/core"; | ||
var body = (fields) => { | ||
return middleware.web({ | ||
return middleware2.web({ | ||
fn: async (ctx, next) => { | ||
@@ -659,5 +679,5 @@ try { | ||
// src/middleware/query.ts | ||
import { middleware as middleware2, validate as validate2, Validator as Validator3, ValidatorError as ValidatorError2 } from "@aomex/core"; | ||
import { middleware as middleware3, validate as validate2, Validator as Validator3, ValidatorError as ValidatorError2 } from "@aomex/core"; | ||
var query = (fields) => { | ||
return middleware2.web({ | ||
return middleware3.web({ | ||
fn: async (ctx, next) => { | ||
@@ -687,5 +707,5 @@ try { | ||
// src/middleware/params.ts | ||
import { middleware as middleware3, validate as validate3, Validator as Validator4, ValidatorError as ValidatorError3 } from "@aomex/core"; | ||
import { middleware as middleware4, validate as validate3, Validator as Validator4, ValidatorError as ValidatorError3 } from "@aomex/core"; | ||
var params = (fields) => { | ||
return middleware3.web({ | ||
return middleware4.web({ | ||
fn: async (ctx, next) => { | ||
@@ -692,0 +712,0 @@ try { |
{ | ||
"name": "@aomex/web", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "aomex web层应用", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102222
1271