From dd02fe5930507d67f20acf9044aa7e6036c65df1 Mon Sep 17 00:00:00 2001 From: Khafra <42794878+KhafraDev@users.noreply.github.com> Date: Wed, 11 Jan 2023 15:57:55 -0500 Subject: [PATCH] fix: replace busboy in fetch --- lib/fetch/body.js | 22 +++++++++++----------- lib/formdata/parser.js | 2 ++ package.json | 4 +--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/fetch/body.js b/lib/fetch/body.js index c291afa9368..6ebf9b8723e 100644 --- a/lib/fetch/body.js +++ b/lib/fetch/body.js @@ -1,6 +1,5 @@ 'use strict' -const Busboy = require('busboy') const util = require('../core/util') const { ReadableStreamFrom, @@ -21,6 +20,7 @@ const { isErrored } = require('../core/util') const { isUint8Array, isArrayBuffer } = require('util/types') const { File: UndiciFile } = require('./file') const { parseMIMEType, serializeAMimeType } = require('./dataURL') +const { FormDataParser } = require('../formdata/parser') let ReadableStream = globalThis.ReadableStream @@ -374,10 +374,10 @@ function bodyMixinMethods (instance) { const responseFormData = new FormData() - let busboy + let parser try { - busboy = Busboy({ + parser = new FormDataParser({ headers, defParamCharset: 'utf8' }) @@ -385,10 +385,10 @@ function bodyMixinMethods (instance) { throw new DOMException(`${err}`, 'AbortError') } - busboy.on('field', (name, value) => { + parser.on('field', (name, value) => { responseFormData.append(name, value) }) - busboy.on('file', (name, value, info) => { + parser.on('file', (name, value, info) => { const { filename, encoding, mimeType } = info const chunks = [] @@ -417,14 +417,14 @@ function bodyMixinMethods (instance) { } }) - const busboyResolve = new Promise((resolve, reject) => { - busboy.on('finish', resolve) - busboy.on('error', (err) => reject(new TypeError(err))) + const promise = new Promise((resolve, reject) => { + parser.on('close', () => resolve()) + parser.on('error', (err) => reject(new TypeError(err))) }) - if (this.body !== null) for await (const chunk of consumeBody(this[kState].body)) busboy.write(chunk) - busboy.end() - await busboyResolve + if (this.body !== null) for await (const chunk of consumeBody(this[kState].body)) parser.write(chunk) + parser.end() + await promise return responseFormData } else if (/application\/x-www-form-urlencoded/.test(contentType)) { diff --git a/lib/formdata/parser.js b/lib/formdata/parser.js index b34b5a22a51..910214f3a88 100644 --- a/lib/formdata/parser.js +++ b/lib/formdata/parser.js @@ -29,6 +29,7 @@ class FormDataParser extends Writable { #info = { headerState: headerStates.DEFAULT, + /** @type {FileStream|null} */ stream: null, body: { chunks: [], @@ -383,6 +384,7 @@ class FormDataParser extends Writable { this.#info.stream.push(limitedChunk) this.#info.stream.push(null) + this.#info.stream.read() // TODO: why is this needed!?!?! this.#info.stream.destroy() } else if ( this.#info.limits.fieldSize < this.#opts.limits.fieldSize && diff --git a/package.json b/package.json index ea556017951..84bbe944f40 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "@types/node": "^18.0.3", "abort-controller": "^3.0.0", "atomic-sleep": "^1.0.0", + "busboy": "^1.6.0", "chai": "^4.3.4", "chai-as-promised": "^7.1.1", "chai-iterator": "^3.0.2", @@ -129,8 +130,5 @@ "testMatch": [ "/test/jest/**" ] - }, - "dependencies": { - "busboy": "^1.6.0" } }