Skip to content

Commit

Permalink
fix: replace busboy in fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Jan 11, 2023
1 parent a65a218 commit dd02fe5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
22 changes: 11 additions & 11 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const Busboy = require('busboy')
const util = require('../core/util')
const {
ReadableStreamFrom,
Expand All @@ -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

Expand Down Expand Up @@ -374,21 +374,21 @@ function bodyMixinMethods (instance) {

const responseFormData = new FormData()

let busboy
let parser

try {
busboy = Busboy({
parser = new FormDataParser({
headers,
defParamCharset: 'utf8'
})
} catch (err) {
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 = []

Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions lib/formdata/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class FormDataParser extends Writable {

#info = {
headerState: headerStates.DEFAULT,
/** @type {FileStream|null} */
stream: null,
body: {
chunks: [],
Expand Down Expand Up @@ -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 &&
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -129,8 +130,5 @@
"testMatch": [
"<rootDir>/test/jest/**"
]
},
"dependencies": {
"busboy": "^1.6.0"
}
}

0 comments on commit dd02fe5

Please sign in to comment.