修复表单读取时出错问题

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", "name": "yizhi-multipart-reader",
"version": "1.0.5", "version": "1.0.6",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"types": "typing/index.d.ts", "types": "typing/index.d.ts",

View File

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