Skip to content

Commit

Permalink
fix: request.headers for http2 cannot be deepmerged (#275)
Browse files Browse the repository at this point in the history
node http2 server initializes headers with Object.create(null) and
they not have the propertyIsEnumerable method. This method is needed by
deepmerge.

Also added unit test for http2 request.
  • Loading branch information
mccare authored Oct 7, 2021
1 parent a6c58ba commit 3716575
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function fastifyMultipart (fastify, options, done) {

const req = this.raw

const busboyOptions = deepmerge.all([{ headers: req.headers }, options || {}, opts || {}])
const busboyOptions = deepmerge.all([{ headers: Object.assign({}, req.headers) }, options || {}, opts || {}])
const stream = busboy(busboyOptions)
let completed = false
let files = 0
Expand Down Expand Up @@ -316,7 +316,7 @@ function fastifyMultipart (fastify, options, done) {
let lastError = null
const request = this.raw
const busboyOptions = deepmerge.all([
{ headers: request.headers },
{ headers: Object.assign({}, request.headers) },
options,
opts
])
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"eslint-plugin-typescript": "^0.14.0",
"fastify": "^3.2.1",
"form-data": "^4.0.0",
"h2url": "^0.2.0",
"pre-commit": "^1.2.2",
"pump": "^3.0.0",
"readable-stream": "^3.6.0",
Expand Down
43 changes: 43 additions & 0 deletions test/multipart-http2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const test = require('tap').test
const FormData = require('form-data')
const Fastify = require('fastify')
const multipart = require('..')
const h2url = require('h2url')
const path = require('path')
const fs = require('fs')
const sendToWormhole = require('stream-wormhole')

const filePath = path.join(__dirname, '../README.md')

test('should respond when all files are processed', function (t) {
const fastify = Fastify({ http2: true })
t.teardown(fastify.close.bind(fastify))

fastify.register(multipart)

fastify.post('/', async function (req, reply) {
const parts = req.files()
for await (const part of parts) {
t.ok(part.file)
await sendToWormhole(part.file)
}
reply.code(200).send()
})

fastify.listen(0, async function () {
const url = `http://localhost:${fastify.server.address().port}`
const form = new FormData()

form.append('upload', fs.createReadStream(filePath))
form.append('upload2', fs.createReadStream(filePath))
form.append('hello', 'world')
form.append('willbe', 'dropped')

const res = await h2url.concat({ url, method: 'POST', headers: form.getHeaders(), body: form })

t.equal(res.headers[':status'], 200)
t.end()
})
})

0 comments on commit 3716575

Please sign in to comment.