Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: close muxed streams when underlying socket is closed #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,21 @@ Multiplex.prototype.finalize = function () {

Multiplex.prototype.destroy = function (err) {
if (this.destroyed) return

var list = this._local.concat(this._remote)

this.destroyed = true
this._clear()

if (err) this.emit('error', err)
this.emit('close')

list.forEach(function (stream) {
if (stream) {
stream.emit('error', err || new Error('underlying socket has been closed'))
}
})

this._clear()
}

module.exports = Multiplex
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"devDependencies": {
"chunky": "0.0.0",
"concat-stream": "^1.4.8",
"pump": "^1.0.2",
"tape": "^4.0.0",
"through2": "^0.6.5"
},
Expand Down
59 changes: 57 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var through = require('through2')
var multiplex = require('./')
var net = require('net')
var chunky = require('chunky')
var pump = require('pump')

test('one way piping work with 2 sub-streams', function (t) {
var plex1 = multiplex()
Expand Down Expand Up @@ -141,18 +142,27 @@ test('testing invalid data error', function (t) {
})

test('overflow', function (t) {
t.plan(2)
var plex1 = multiplex()
var plex2 = multiplex({limit: 10})

plex2.on('stream', function (stream) {
stream.on('error', function (err) {
t.equal(err.message, 'Incoming message is too big')
})
})

plex2.on('error', function (err) {
if (err) {
t.equal(err.message, 'Incoming message is too big')
t.end()
}
})

plex1.pipe(plex2).pipe(plex1)
plex1.createStream().write(new Buffer(11))

var stream = plex1.createStream()

stream.write(new Buffer(11))
})

test('2 buffers packed into 1 chunk', function (t) {
Expand Down Expand Up @@ -281,3 +291,48 @@ test('if onstream is not passed, stream is emitted', function (t) {
t.end()
})
})

test('underlying error is propagated to muxed streams', function (t) {
t.plan(4)
var plex1 = multiplex()
var plex2 = multiplex()

var socket

plex2.on('stream', function (stream) {
stream.on('error', function (err) {
t.ok(err)
})

stream.on('close', function () {
t.pass()
})

socket.destroy()
})

var stream1to2 = plex1.createStream(1337)

stream1to2.on('error', function (err) {
t.ok(err)
})

stream1to2.on('close', function () {
t.pass()
})

var server = net.createServer(function (stream) {
pump(plex2, stream)
pump(stream, plex2)
server.close()
})

server.listen(0, function () {
var port = server.address().port
socket = net.connect(port)

pump(plex1, socket)
pump(socket, plex1)
})
})