diff --git a/lib/util.js b/lib/util.js index 0cc841bc26a..cb1d2a4cf31 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,6 +2,7 @@ const assert = require('assert') const { kDestroyed } = require('./symbols') +const { IncomingMessage } = require('http') function nop () {} @@ -35,7 +36,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) + }) + }) + }) +})