From dbffccffade914642c2ad2195251984a42346c5f Mon Sep 17 00:00:00 2001 From: shanejonas Date: Fri, 28 May 2021 15:44:26 -0700 Subject: [PATCH 1/3] throw error if parent is destroyed already when creating sub streams --- .nvmrc | 2 +- src/ObjectMultiplex.ts | 5 +++++ test/index.js | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/.nvmrc b/.nvmrc index f64b6e4..dae199a 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -v12 \ No newline at end of file +v12 diff --git a/src/ObjectMultiplex.ts b/src/ObjectMultiplex.ts index 07e0f04..ae50629 100644 --- a/src/ObjectMultiplex.ts +++ b/src/ObjectMultiplex.ts @@ -23,6 +23,11 @@ export class ObjectMultiplex extends Duplex { } createStream(name: string): Substream { + // validate stream destroyed already + if (this.destroyed) { + throw new Error('ObjectMultiplex - parent stream already destroyed'); + } + // validate name if (!name) { throw new Error('ObjectMultiplex - name must not be empty'); diff --git a/test/index.js b/test/index.js index df85139..aa42522 100644 --- a/test/index.js +++ b/test/index.js @@ -79,6 +79,17 @@ test('roundtrip', (t) => { setTimeout(() => outTransport.destroy(), 100); }); +test('error on createStream if destroyed', (t) => { + const stream = new ObjMultiplex(); + stream.destroy(); + try { + stream.createStream('controller'); + } catch (e) { + t.assert(e.message.includes('already destroyed'), true); + t.end(); + } +}); + // util function basicTestSetup() { From b90e77594f779a376330d9146b0a002d8d42e35b Mon Sep 17 00:00:00 2001 From: shanejonas Date: Mon, 31 May 2021 11:28:03 -0700 Subject: [PATCH 2/3] add guards for streams ended already --- src/ObjectMultiplex.ts | 7 ++++++- test/index.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ObjectMultiplex.ts b/src/ObjectMultiplex.ts index ae50629..b4f9a6a 100644 --- a/src/ObjectMultiplex.ts +++ b/src/ObjectMultiplex.ts @@ -23,11 +23,16 @@ export class ObjectMultiplex extends Duplex { } createStream(name: string): Substream { - // validate stream destroyed already + // guard stream against destroyed already if (this.destroyed) { throw new Error('ObjectMultiplex - parent stream already destroyed'); } + // guard stream against ended already + if (this._readableState.ended || this._writableState.ended) { + throw new Error('ObjectMultiplex - parent stream already ended'); + } + // validate name if (!name) { throw new Error('ObjectMultiplex - name must not be empty'); diff --git a/test/index.js b/test/index.js index aa42522..3a05b3f 100644 --- a/test/index.js +++ b/test/index.js @@ -90,6 +90,18 @@ test('error on createStream if destroyed', (t) => { } }); +test('error on createStream if ended', (t) => { + const stream = new ObjMultiplex(); + stream.end(); + try { + stream.createStream('controller'); + stream.write({ foo: 'bar' }); + } catch (e) { + t.assert(e.message.includes('already ended'), true); + t.end(); + } +}); + // util function basicTestSetup() { From 93deea16d414043c7658b05e5c68db601923c2ef Mon Sep 17 00:00:00 2001 From: Shane Date: Mon, 31 May 2021 19:36:15 -0700 Subject: [PATCH 3/3] Update test/index.js Co-authored-by: Mark Stacey --- test/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/index.js b/test/index.js index 3a05b3f..5f45690 100644 --- a/test/index.js +++ b/test/index.js @@ -95,7 +95,6 @@ test('error on createStream if ended', (t) => { stream.end(); try { stream.createStream('controller'); - stream.write({ foo: 'bar' }); } catch (e) { t.assert(e.message.includes('already ended'), true); t.end();