Protomux.from

This commit is contained in:
Mathias Buus 2022-01-28 16:00:09 +01:00
parent 1c3c559d99
commit 534c417ed0
2 changed files with 9 additions and 0 deletions

View File

@ -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)` #### `const p = mux.addProtocol(opts)`
Add a new protocol. Add a new protocol.

View File

@ -107,6 +107,7 @@ module.exports = class Protomux {
this.corked = 0 this.corked = 0
this.backlog = backlog this.backlog = backlog
this.onacceptprotocol = onacceptprotocol || (() => this._unopened < this.backlog) this.onacceptprotocol = onacceptprotocol || (() => this._unopened < this.backlog)
this.isProtomux = true
this._unopened = 0 this._unopened = 0
this._batch = null this._batch = null
@ -117,6 +118,10 @@ module.exports = class Protomux {
this.stream.on('close', this._shutdown.bind(this)) this.stream.on('close', this._shutdown.bind(this))
} }
static from (other, opts) {
return other.isProtomux === true ? other : new Protomux(other, opts)
}
sendKeepAlive () { sendKeepAlive () {
this.stream.write(this._alloc(0)) this.stream.write(this._alloc(0))
} }