Skip to content

Commit

Permalink
perf: use @fastify/busboy (#2211)
Browse files Browse the repository at this point in the history
* use @fastify/busboy

* fix file handler

* fix test

* Revert "fix test"

This reverts commit 0eada58.

* v2

* fix parseFormDataString
  • Loading branch information
gurgunday authored Sep 30, 2023
1 parent 8591eb7 commit 5addbb7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
10 changes: 4 additions & 6 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Busboy = require('busboy')
const Busboy = require('@fastify/busboy')
const util = require('../core/util')
const {
ReadableStreamFrom,
Expand Down Expand Up @@ -385,10 +385,9 @@ function bodyMixinMethods (instance) {
let busboy

try {
busboy = Busboy({
busboy = new Busboy({
headers,
preservePath: true,
defParamCharset: 'utf8'
preservePath: true
})
} catch (err) {
throw new DOMException(`${err}`, 'AbortError')
Expand All @@ -397,8 +396,7 @@ function bodyMixinMethods (instance) {
busboy.on('field', (name, value) => {
responseFormData.append(name, value)
})
busboy.on('file', (name, value, info) => {
const { filename, encoding, mimeType } = info
busboy.on('file', (name, value, filename, encoding, mimeType) => {
const chunks = []

if (encoding === 'base64' || encoding.toLowerCase() === 'base64') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,6 @@
]
},
"dependencies": {
"busboy": "^1.6.0"
"@fastify/busboy": "^2.0.0"
}
}
4 changes: 2 additions & 2 deletions test/node-fetch/utils/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const http = require('http')
const zlib = require('zlib')
const { once } = require('events')
const newBusboy = require('busboy')
const Busboy = require('@fastify/busboy')

module.exports = class TestServer {
constructor () {
Expand Down Expand Up @@ -435,7 +435,7 @@ module.exports = class TestServer {
if (p === '/multipart') {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
const busboy = newBusboy({ headers: request.headers })
const busboy = new Busboy({ headers: request.headers })
let body = ''
busboy.on('file', async (fieldName, file, fileName) => {
body += `${fieldName}=${fileName}`
Expand Down
10 changes: 5 additions & 5 deletions test/utils/formdata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const busboy = require('busboy')
const Busboy = require('@fastify/busboy')

function parseFormDataString (
body,
Expand All @@ -9,15 +9,15 @@ function parseFormDataString (
fields: []
}

const bb = busboy({
const bb = new Busboy({
headers: {
'content-type': contentType
}
})

return new Promise((resolve, reject) => {
bb.on('file', (name, file, info) => {
cache.fileMap.set(name, { data: [], info })
bb.on('file', (name, file, filename, encoding, mimeType) => {
cache.fileMap.set(name, { data: [], info: { filename, encoding, mimeType } })

file.on('data', (data) => {
const old = cache.fileMap.get(name)
Expand All @@ -37,7 +37,7 @@ function parseFormDataString (
})

bb.on('field', (key, value) => cache.fields.push({ key, value }))
bb.on('close', () => resolve(cache))
bb.on('finish', () => resolve(cache))
bb.on('error', (e) => reject(e))

bb.end(body)
Expand Down

0 comments on commit 5addbb7

Please sign in to comment.