upstream-patch.json
{"files":[{"filename":"docs/Reference/Reply.md","patch":"@@ -277,8 +277,9 @@ requires heavy resources to be sent after the `data`, for example,\n as soon as possible.\n \n \u003e ℹ️ Note:\n-\u003e The header `Transfer-Encoding: chunked` will be added once you use\n-\u003e the trailer. It is a hard requirement for using trailer in Node.js.\n+\u003e For HTTP/1 responses, the header `Transfer-Encoding: chunked` will be added\n+\u003e once you use a trailer. HTTP/2 does not use chunked transfer encoding and\n+\u003e sends trailers using its native trailing headers support.\n \n \u003e ℹ️ Note:\n \u003e Any error passed to `done` callback will be ignored. If you are interested"},{"filename":"lib/reply.js","patch":"@@ -606,8 +606,11 @@ function onSendEnd (reply, payload) {\n header += trailerName\n }\n if (header !== '') {\n- // it must be chunked for trailer to work\n- reply.header('Transfer-Encoding', 'chunked')\n+ // HTTP/1 must use chunked encoding for trailers. HTTP/2 forbids\n+ // the transfer-encoding header and handles trailers natively.\n+ if (!isHttp2Reply(reply)) {\n+ reply.header('Transfer-Encoding', 'chunked')\n+ }\n reply.header('Trailer', header.trim())\n } else {\n reply[kReplyTrailers] = null"},{"filename":"test/http2/plain.test.js","patch":"@@ -68,6 +68,51 @@ test('http2 plain test', async t =\u003e {\n })\n })\n \n+test('http2 response trailers do not use transfer-encoding', async t =\u003e {\n+ const fastify = Fastify({ http2: true })\n+\n+ fastify.get('/', async (request, reply) =\u003e {\n+ reply.trailer('x-checksum', async () =\u003e 'abc')\n+ return 'hello'\n+ })\n+\n+ await fastify.listen({ port: 0, host: '127.0.0.1' })\n+\n+ const client = http2.connect(`http://127.0.0.1:${fastify.server.address().port}`)\n+ t.after(() =\u003e {\n+ client.close()\n+ return fastify.close()\n+ })\n+\n+ const response = await new Promise((resolve, reject) =\u003e {\n+ const request = client.request({\n+ [http2.constants.HTTP2_HEADER_METHOD]: http2.constants.HTTP2_METHOD_GET,\n+ [http2.constants.HTTP2_HEADER_PATH]: '/'\n+ })\n+ const result = { body: '' }\n+\n+ request.setEncoding('utf8')\n+ request.on('response', headers =\u003e {\n+ result.headers = headers\n+ })\n+ request.on('trailers', trailers =\u003e {\n+ result.trailers = trailers\n+ })\n+ request.on('data', chunk =\u003e {\n+ result.body += chunk\n+ })\n+ request.on('error', reject)\n+ request.on('end', () =\u003e resolve(result))\n+ request.end()\n+ })\n+\n+ t.assert.strictEqual(response.headers[':status'], 200)\n+ t.assert.strictEqual(response.headers['transfer-encoding'], undefined)\n+ t.assert.strictEqual(response.headers.trailer, 'x-checksum')\n+ t.assert.strictEqual(response.body, 'hello')\n+ t.assert.strictEqual(response.trailers['x-checksum'], 'abc')\n+})\n+\n test('http2 large non-stream replies are sent completely', async t =\u003e {\n const modes = ['buffer', 'string']\n "}],"message":"Merge commit from fork","sha":"ad06a4c3fe8a944a904f38068249b18b8f552e90"}