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)) }