From 534c417ed065fdad7dbfe058b12ef9c743cef164 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Fri, 28 Jan 2022 16:00:09 +0100 Subject: [PATCH] Protomux.from --- README.md | 4 ++++ index.js | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index 5fb5378..2ae1a61 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,10 @@ Options include: } ``` +#### `mux = Protomux.from(stream | muxer, [options])` + +Helper to accept either an existing muxer instance or a stream (which creates a new one). + #### `const p = mux.addProtocol(opts)` Add a new protocol. diff --git a/index.js b/index.js index abc739c..8741416 100644 --- a/index.js +++ b/index.js @@ -107,6 +107,7 @@ module.exports = class Protomux { this.corked = 0 this.backlog = backlog this.onacceptprotocol = onacceptprotocol || (() => this._unopened < this.backlog) + this.isProtomux = true this._unopened = 0 this._batch = null @@ -117,6 +118,10 @@ module.exports = class Protomux { this.stream.on('close', this._shutdown.bind(this)) } + static from (other, opts) { + return other.isProtomux === true ? other : new Protomux(other, opts) + } + sendKeepAlive () { this.stream.write(this._alloc(0)) }