Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Compatibility with IncomingMessage #270

Merged
merged 2 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert')
const { kDestroyed } = require('./symbols')
const { IncomingMessage } = require('http')

function nop () {}

Expand Down Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions test/stream-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
})