From 6505b316d22476ce0eb260c889924dd4f667f826 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 27 Feb 2018 13:46:35 -0600 Subject: [PATCH 1/5] fix: handle throws --- package.json | 1 + src/muxer.js | 7 ++++++- test/mplex.spec.js | 13 +++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index cf5d2f6..e677aa9 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "libp2p-tcp": "^0.11.5", "libp2p-websockets": "~0.10.4", "pre-commit": "^1.2.2", + "pull-abortable": "^4.1.1", "pull-pair": "^1.1.0" }, "dependencies": { diff --git a/src/muxer.js b/src/muxer.js index 6fe8415..f77e438 100644 --- a/src/muxer.js +++ b/src/muxer.js @@ -51,7 +51,12 @@ class MultiplexMuxer extends EventEmitter { // method added to enable pure stream muxer feeling newStream (callback) { callback = callback || noop - let stream = this.multiplex.createStream() + let stream + try { + stream = this.multiplex.createStream() + } catch(err) { + return callback(err) + } const conn = new Connection( catchError(toPull.duplex(stream)), diff --git a/test/mplex.spec.js b/test/mplex.spec.js index e67a996..0fae923 100644 --- a/test/mplex.spec.js +++ b/test/mplex.spec.js @@ -7,6 +7,7 @@ const expect = chai.expect chai.use(dirtyChai) const pair = require('pull-pair/duplex') const pull = require('pull-stream') +const abortable = require('pull-abortable') const multiplex = require('../src') @@ -67,4 +68,16 @@ describe('multiplex-generic', () => { }) ) }) + + it('handle aborted stream', (done) => { + this.timeout(20 * 1000) + const aborter = abortable() + pull(listenerSocket, aborter) + + aborter.abort() + listener.newStream((err) => { + expect(err).to.exist() + done() + }) + }) }) From a99120a5f1975c13daa997461f26d0d887a1364a Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 27 Feb 2018 13:47:22 -0600 Subject: [PATCH 2/5] lint --- src/muxer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/muxer.js b/src/muxer.js index f77e438..78506a6 100644 --- a/src/muxer.js +++ b/src/muxer.js @@ -54,7 +54,7 @@ class MultiplexMuxer extends EventEmitter { let stream try { stream = this.multiplex.createStream() - } catch(err) { + } catch (err) { return callback(err) } From ee6230adbc7a038441fc0cd0d7837e0bb8cbf3e0 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 27 Feb 2018 13:51:23 -0600 Subject: [PATCH 3/5] remove timeout --- test/mplex.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/mplex.spec.js b/test/mplex.spec.js index 0fae923..bf32f6a 100644 --- a/test/mplex.spec.js +++ b/test/mplex.spec.js @@ -70,7 +70,6 @@ describe('multiplex-generic', () => { }) it('handle aborted stream', (done) => { - this.timeout(20 * 1000) const aborter = abortable() pull(listenerSocket, aborter) From 42dcab60f1fe2aee8dfe06b34fac81b9e076dadc Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 7 Mar 2018 19:14:26 -0600 Subject: [PATCH 4/5] fix: return callback with setImmediate --- src/muxer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/muxer.js b/src/muxer.js index 78506a6..95f679e 100644 --- a/src/muxer.js +++ b/src/muxer.js @@ -55,7 +55,7 @@ class MultiplexMuxer extends EventEmitter { try { stream = this.multiplex.createStream() } catch (err) { - return callback(err) + return setImmediate(callback(err)) } const conn = new Connection( From 139d541eae72ad59265858cb842869cd02216d05 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Thu, 22 Mar 2018 12:43:29 -0600 Subject: [PATCH 5/5] fix: review --- test/mplex.spec.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/mplex.spec.js b/test/mplex.spec.js index bf32f6a..e67a996 100644 --- a/test/mplex.spec.js +++ b/test/mplex.spec.js @@ -7,7 +7,6 @@ const expect = chai.expect chai.use(dirtyChai) const pair = require('pull-pair/duplex') const pull = require('pull-stream') -const abortable = require('pull-abortable') const multiplex = require('../src') @@ -68,15 +67,4 @@ describe('multiplex-generic', () => { }) ) }) - - it('handle aborted stream', (done) => { - const aborter = abortable() - pull(listenerSocket, aborter) - - aborter.abort() - listener.newStream((err) => { - expect(err).to.exist() - done() - }) - }) })