From 365c92187af91b138939ebf952efcb813ecb1f56 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 29 Jul 2020 19:28:18 +0200 Subject: [PATCH 1/2] Compatibility with IncomingMessage --- lib/util.js | 7 ++++++- test/stream-compat.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 0cc841bc26a..b2edc2bf44e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -3,6 +3,9 @@ const assert = require('assert') const { kDestroyed } = require('./symbols') +// Use of Node.js internal :/ +const { IncomingMessage } = require('_http_incoming') + function nop () {} function isStream (body) { @@ -35,7 +38,9 @@ function destroy (stream, err) { } if (typeof stream.destroy === 'function') { - stream.destroy(err) + if (err || Object.getPrototypeOf(stream).constructor !== IncomingMessage) { + stream.destroy(err) + } } else if (err) { process.nextTick((stream, err) => { stream.emit('error', err) diff --git a/test/stream-compat.js b/test/stream-compat.js index f55132a6ef8..56c2d128bfc 100644 --- a/test/stream-compat.js +++ b/test/stream-compat.js @@ -32,3 +32,42 @@ test('stream body without destroy', (t) => { signal.emit('abort') }) }) + +test('IncomingMessage', { only: true }, (t) => { + t.plan(2) + + const server = createServer((req, res) => { + res.end() + }) + t.teardown(server.close.bind(server)) + + server.listen(0, () => { + const proxyClient = new Client(`http://localhost:${server.address().port}`) + t.teardown(proxyClient.destroy.bind(proxyClient)) + + const proxy = createServer((req, res) => { + proxyClient.request({ + path: '/', + method: 'PUT', + body: req + }, (err, data) => { + t.error(err) + data.body.pipe(res) + }) + }) + t.teardown(proxy.close.bind(proxy)) + + proxy.listen(0, () => { + const client = new Client(`http://localhost:${proxy.address().port}`) + t.teardown(client.destroy.bind(client)) + + client.request({ + path: '/', + method: 'PUT', + body: 'hello world' + }, (err, data) => { + t.error(err) + }) + }) + }) +}) From bcd2001b769d7169094400112f9ff6cdc25fb199 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 29 Jul 2020 19:31:24 +0200 Subject: [PATCH 2/2] require('http') --- lib/util.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/util.js b/lib/util.js index b2edc2bf44e..cb1d2a4cf31 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,9 +2,7 @@ const assert = require('assert') const { kDestroyed } = require('./symbols') - -// Use of Node.js internal :/ -const { IncomingMessage } = require('_http_incoming') +const { IncomingMessage } = require('http') function nop () {}