Compare commits
8 Commits
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | e7aa69a59b | |
Derrick Hammer | bbe30a3de5 | |
Derrick Hammer | ce12140ac0 | |
Nina Breznik | 6dfa4f32ca | |
Mathias Buus | 131fce63d7 | |
Mathias Buus | 78e83bbfa9 | |
Lucas | b9ff6b50b7 | |
Lucas | 69ae35ca8b |
19
README.md
19
README.md
|
@ -46,10 +46,9 @@ const two = cool.addMessage({
|
|||
}
|
||||
})
|
||||
|
||||
// open the channels
|
||||
// open the channel
|
||||
|
||||
one.open()
|
||||
two.open()
|
||||
cool.open()
|
||||
|
||||
// And send some data
|
||||
|
||||
|
@ -109,13 +108,25 @@ __NOTE__: `mux.createChannel` returns `null` if the channel should not be opened
|
|||
|
||||
If you want multiple sessions with the same `protocol` and `id`, set `unique: false` as an option.
|
||||
|
||||
#### `const opened = mux.opened({ protocol, id })`
|
||||
|
||||
Boolean that indicates if the channel is opened.
|
||||
|
||||
#### `mux.pair({ protocol, id }, callback)`
|
||||
|
||||
Register a callback to be called everytime a new channel is requested.
|
||||
|
||||
#### `mux.unpair({ protocol, id })`
|
||||
|
||||
Unregisters the pair callback.
|
||||
|
||||
#### `channel.open([handshake])`
|
||||
|
||||
Open the channel.
|
||||
|
||||
#### `const m = channel.addMessage(opts)`
|
||||
|
||||
Add a message. Options include:
|
||||
Add/register a message type for a certain encoding. Options include:
|
||||
|
||||
``` js
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "protomux",
|
||||
"version": "3.4.0",
|
||||
"version": "3.4.1",
|
||||
"description": "Multiplex multiple message oriented protocols over a stream",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
|
@ -12,6 +12,7 @@
|
|||
"devDependencies": {
|
||||
"@hyperswarm/secret-stream": "^6.0.0",
|
||||
"brittle": "^3.0.0",
|
||||
"prettier": "^2.8.7",
|
||||
"standard": "^16.0.4"
|
||||
},
|
||||
"scripts": {
|
||||
|
|
39
test.js
39
test.js
|
@ -391,6 +391,45 @@ test('deduplicate muxers', function (t) {
|
|||
bbar.addMessage({ encoding: c.string }).send('hello bar')
|
||||
})
|
||||
|
||||
test('open + send + close on same tick', async function (t) {
|
||||
t.plan(4)
|
||||
|
||||
const a = new Protomux(new SecretStream(true))
|
||||
const b = new Protomux(new SecretStream(false))
|
||||
|
||||
replicate(a, b)
|
||||
|
||||
const ac = a.createChannel({
|
||||
protocol: 'foo',
|
||||
onopen () {
|
||||
t.pass('a opened')
|
||||
},
|
||||
onclose () {
|
||||
t.pass('a closed')
|
||||
}
|
||||
})
|
||||
|
||||
ac.open()
|
||||
ac.addMessage({
|
||||
encoding: c.string,
|
||||
onmessage (message) { t.is(message, 'hello') }
|
||||
})
|
||||
|
||||
const bc = b.createChannel({
|
||||
protocol: 'foo',
|
||||
onopen () {
|
||||
t.fail('b opened')
|
||||
},
|
||||
onclose () {
|
||||
t.pass('b closed')
|
||||
}
|
||||
})
|
||||
|
||||
bc.open()
|
||||
bc.addMessage({ encoding: c.string }).send('hello')
|
||||
bc.close()
|
||||
})
|
||||
|
||||
function replicate (a, b) {
|
||||
a.stream.rawStream.pipe(b.stream.rawStream).pipe(a.stream.rawStream)
|
||||
}
|
||||
|
|
Reference in New Issue