Skip to content

Commit dd02fe5

Browse files
committed
fix: replace busboy in fetch
1 parent a65a218 commit dd02fe5

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

lib/fetch/body.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const Busboy = require('busboy')
43
const util = require('../core/util')
54
const {
65
ReadableStreamFrom,
@@ -21,6 +20,7 @@ const { isErrored } = require('../core/util')
2120
const { isUint8Array, isArrayBuffer } = require('util/types')
2221
const { File: UndiciFile } = require('./file')
2322
const { parseMIMEType, serializeAMimeType } = require('./dataURL')
23+
const { FormDataParser } = require('../formdata/parser')
2424

2525
let ReadableStream = globalThis.ReadableStream
2626

@@ -374,21 +374,21 @@ function bodyMixinMethods (instance) {
374374

375375
const responseFormData = new FormData()
376376

377-
let busboy
377+
let parser
378378

379379
try {
380-
busboy = Busboy({
380+
parser = new FormDataParser({
381381
headers,
382382
defParamCharset: 'utf8'
383383
})
384384
} catch (err) {
385385
throw new DOMException(`${err}`, 'AbortError')
386386
}
387387

388-
busboy.on('field', (name, value) => {
388+
parser.on('field', (name, value) => {
389389
responseFormData.append(name, value)
390390
})
391-
busboy.on('file', (name, value, info) => {
391+
parser.on('file', (name, value, info) => {
392392
const { filename, encoding, mimeType } = info
393393
const chunks = []
394394

@@ -417,14 +417,14 @@ function bodyMixinMethods (instance) {
417417
}
418418
})
419419

420-
const busboyResolve = new Promise((resolve, reject) => {
421-
busboy.on('finish', resolve)
422-
busboy.on('error', (err) => reject(new TypeError(err)))
420+
const promise = new Promise((resolve, reject) => {
421+
parser.on('close', () => resolve())
422+
parser.on('error', (err) => reject(new TypeError(err)))
423423
})
424424

425-
if (this.body !== null) for await (const chunk of consumeBody(this[kState].body)) busboy.write(chunk)
426-
busboy.end()
427-
await busboyResolve
425+
if (this.body !== null) for await (const chunk of consumeBody(this[kState].body)) parser.write(chunk)
426+
parser.end()
427+
await promise
428428

429429
return responseFormData
430430
} else if (/application\/x-www-form-urlencoded/.test(contentType)) {

lib/formdata/parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class FormDataParser extends Writable {
2929

3030
#info = {
3131
headerState: headerStates.DEFAULT,
32+
/** @type {FileStream|null} */
3233
stream: null,
3334
body: {
3435
chunks: [],
@@ -383,6 +384,7 @@ class FormDataParser extends Writable {
383384

384385
this.#info.stream.push(limitedChunk)
385386
this.#info.stream.push(null)
387+
this.#info.stream.read() // TODO: why is this needed!?!?!
386388
this.#info.stream.destroy()
387389
} else if (
388390
this.#info.limits.fieldSize < this.#opts.limits.fieldSize &&

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"@types/node": "^18.0.3",
7373
"abort-controller": "^3.0.0",
7474
"atomic-sleep": "^1.0.0",
75+
"busboy": "^1.6.0",
7576
"chai": "^4.3.4",
7677
"chai-as-promised": "^7.1.1",
7778
"chai-iterator": "^3.0.2",
@@ -129,8 +130,5 @@
129130
"testMatch": [
130131
"<rootDir>/test/jest/**"
131132
]
132-
},
133-
"dependencies": {
134-
"busboy": "^1.6.0"
135133
}
136134
}

0 commit comments

Comments
 (0)