修复表单读取时出错问题

This commit is contained in:
2022-09-19 16:50:37 +08:00
parent 15dfa5f1dc
commit a6b65e75b8
2 changed files with 18 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{
"name": "yizhi-multipart-reader",
"version": "1.0.5",
"version": "1.0.6",
"description": "",
"main": "dist/index.js",
"types": "typing/index.d.ts",

View File

@ -110,8 +110,13 @@ export class MultipartReader {
//处理请求
#handleRequest(req: http.IncomingMessage) {
const onError = (err: Error) => {
this.#reject?.(err)
}
let old: Buffer | null = null //处理剩下的Buffer
const dataReader = async (_data: Buffer) => {
try {
//暂停读取
req.pause()
//数据内容
@ -121,13 +126,13 @@ export class MultipartReader {
this.#onReadCallback?.(this.#readed += _data.length)
//处理数据
req.resume()
} catch (err) {
onError(err as any)
}
if (this.#state == ReadState.finish) {
this.#resolve?.({ files: this.#files, fields: this.#fields })
}
}
const onError = (err: Error) => {
this.#reject?.(err)
}
//事件处理
req.on('error', onError)
req.on('data', dataReader)