From 37165757947857cd8b3977ba362c4536fd2d837e Mon Sep 17 00:00:00 2001 From: Christian van der Leeden Date: Thu, 7 Oct 2021 12:24:36 +0200 Subject: [PATCH] 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. --- index.js | 4 ++-- package.json | 1 + test/multipart-http2.test.js | 43 ++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 test/multipart-http2.test.js diff --git a/index.js b/index.js index 8d2b1de0..4e363687 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 ]) diff --git a/package.json b/package.json index 42d52b84..b6b0f873 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/multipart-http2.test.js b/test/multipart-http2.test.js new file mode 100644 index 00000000..5e00ce4a --- /dev/null +++ b/test/multipart-http2.test.js @@ -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() + }) +})