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

throw error if parent is destroyed already when creating sub streams #17

Merged
merged 3 commits into from
Jun 1, 2021
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v12
v12
10 changes: 10 additions & 0 deletions src/ObjectMultiplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ export class ObjectMultiplex extends Duplex {
}

createStream(name: string): Substream {
// guard stream against destroyed already
if (this.destroyed) {
shanejonas marked this conversation as resolved.
Show resolved Hide resolved
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');
Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ 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();
}
});

test('error on createStream if ended', (t) => {
const stream = new ObjMultiplex();
stream.end();
try {
stream.createStream('controller');
} catch (e) {
t.assert(e.message.includes('already ended'), true);
t.end();
}
});

// util

function basicTestSetup() {
Expand Down