Skip to content

Commit 3716575

Browse files
authored
fix: request.headers for http2 cannot be deepmerged (#275)
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.
1 parent a6c58ba commit 3716575

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function fastifyMultipart (fastify, options, done) {
223223

224224
const req = this.raw
225225

226-
const busboyOptions = deepmerge.all([{ headers: req.headers }, options || {}, opts || {}])
226+
const busboyOptions = deepmerge.all([{ headers: Object.assign({}, req.headers) }, options || {}, opts || {}])
227227
const stream = busboy(busboyOptions)
228228
let completed = false
229229
let files = 0
@@ -316,7 +316,7 @@ function fastifyMultipart (fastify, options, done) {
316316
let lastError = null
317317
const request = this.raw
318318
const busboyOptions = deepmerge.all([
319-
{ headers: request.headers },
319+
{ headers: Object.assign({}, request.headers) },
320320
options,
321321
opts
322322
])

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"eslint-plugin-typescript": "^0.14.0",
2828
"fastify": "^3.2.1",
2929
"form-data": "^4.0.0",
30+
"h2url": "^0.2.0",
3031
"pre-commit": "^1.2.2",
3132
"pump": "^3.0.0",
3233
"readable-stream": "^3.6.0",

test/multipart-http2.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const FormData = require('form-data')
5+
const Fastify = require('fastify')
6+
const multipart = require('..')
7+
const h2url = require('h2url')
8+
const path = require('path')
9+
const fs = require('fs')
10+
const sendToWormhole = require('stream-wormhole')
11+
12+
const filePath = path.join(__dirname, '../README.md')
13+
14+
test('should respond when all files are processed', function (t) {
15+
const fastify = Fastify({ http2: true })
16+
t.teardown(fastify.close.bind(fastify))
17+
18+
fastify.register(multipart)
19+
20+
fastify.post('/', async function (req, reply) {
21+
const parts = req.files()
22+
for await (const part of parts) {
23+
t.ok(part.file)
24+
await sendToWormhole(part.file)
25+
}
26+
reply.code(200).send()
27+
})
28+
29+
fastify.listen(0, async function () {
30+
const url = `http://localhost:${fastify.server.address().port}`
31+
const form = new FormData()
32+
33+
form.append('upload', fs.createReadStream(filePath))
34+
form.append('upload2', fs.createReadStream(filePath))
35+
form.append('hello', 'world')
36+
form.append('willbe', 'dropped')
37+
38+
const res = await h2url.concat({ url, method: 'POST', headers: form.getHeaders(), body: form })
39+
40+
t.equal(res.headers[':status'], 200)
41+
t.end()
42+
})
43+
})

0 commit comments

Comments
 (0)