2021-11-10 14:15:09 +00:00
|
|
|
const Protomux = require('./')
|
|
|
|
const SecretStream = require('@hyperswarm/secret-stream')
|
|
|
|
const test = require('brittle')
|
|
|
|
const c = require('compact-encoding')
|
|
|
|
|
|
|
|
test('basic', function (t) {
|
2021-12-30 20:13:47 +00:00
|
|
|
const a = new Protomux(new SecretStream(true))
|
|
|
|
const b = new Protomux(new SecretStream(false))
|
|
|
|
|
|
|
|
replicate(a, b)
|
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
const p = a.addProtocol({
|
2021-11-10 14:15:09 +00:00
|
|
|
name: 'foo',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 1,
|
2021-12-30 20:13:47 +00:00
|
|
|
onremoteopen () {
|
|
|
|
t.pass('a remote opened')
|
2022-02-01 18:09:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
p.addMessage({
|
|
|
|
encoding: c.string,
|
|
|
|
onmessage (message) {
|
2021-12-30 20:13:47 +00:00
|
|
|
t.is(message, 'hello world')
|
|
|
|
}
|
|
|
|
})
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2021-12-30 20:13:47 +00:00
|
|
|
const bp = b.addProtocol({
|
2021-11-10 14:15:09 +00:00
|
|
|
name: 'foo',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 1
|
2021-12-30 20:13:47 +00:00
|
|
|
})
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
t.plan(2)
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
bp.addMessage({ encoding: c.string }).send('hello world')
|
2021-11-10 14:15:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('echo message', function (t) {
|
2021-12-30 20:13:47 +00:00
|
|
|
const a = new Protomux(new SecretStream(true))
|
2022-02-01 18:09:56 +00:00
|
|
|
const b = new Protomux(new SecretStream(false))
|
2021-11-10 14:15:09 +00:00
|
|
|
|
|
|
|
replicate(a, b)
|
|
|
|
|
2021-12-30 20:13:47 +00:00
|
|
|
const ap = a.addProtocol({
|
|
|
|
name: 'foo',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 1
|
|
|
|
})
|
|
|
|
|
|
|
|
const aEcho = ap.addMessage({
|
|
|
|
encoding: c.string,
|
|
|
|
onmessage (message) {
|
|
|
|
aEcho.send('echo: ' + message)
|
2021-12-30 20:13:47 +00:00
|
|
|
}
|
|
|
|
})
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2021-12-30 20:13:47 +00:00
|
|
|
b.addProtocol({
|
|
|
|
name: 'other',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 2
|
2021-12-30 20:13:47 +00:00
|
|
|
})
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2021-12-30 20:13:47 +00:00
|
|
|
const bp = b.addProtocol({
|
|
|
|
name: 'foo',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 1,
|
2021-12-30 20:13:47 +00:00
|
|
|
onremoteopen () {
|
|
|
|
t.pass('b remote opened')
|
2022-02-01 18:09:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const bEcho = bp.addMessage({
|
|
|
|
encoding: c.string,
|
|
|
|
onmessage (message) {
|
2021-12-30 20:13:47 +00:00
|
|
|
t.is(message, 'echo: hello world')
|
|
|
|
}
|
|
|
|
})
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
t.plan(2)
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
bEcho.send('hello world')
|
2021-11-10 14:15:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('multi message', function (t) {
|
2021-12-30 20:13:47 +00:00
|
|
|
const a = new Protomux(new SecretStream(true))
|
|
|
|
|
|
|
|
a.addProtocol({
|
2021-11-10 14:15:09 +00:00
|
|
|
name: 'other',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 2
|
2021-12-30 20:13:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const ap = a.addProtocol({
|
2021-11-10 14:15:09 +00:00
|
|
|
name: 'multi',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 3
|
2021-12-30 20:13:47 +00:00
|
|
|
})
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
const a1 = ap.addMessage({ encoding: c.int })
|
|
|
|
const a2 = ap.addMessage({ encoding: c.string })
|
|
|
|
const a3 = ap.addMessage({ encoding: c.string })
|
|
|
|
|
2021-12-30 20:13:47 +00:00
|
|
|
const b = new Protomux(new SecretStream(false))
|
|
|
|
|
|
|
|
const bp = b.addProtocol({
|
2021-11-10 14:15:09 +00:00
|
|
|
name: 'multi',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 2
|
2021-12-30 20:13:47 +00:00
|
|
|
})
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
const b1 = bp.addMessage({ encoding: c.int })
|
|
|
|
const b2 = bp.addMessage({ encoding: c.string })
|
|
|
|
|
2021-11-10 14:15:09 +00:00
|
|
|
replicate(a, b)
|
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
t.plan(2)
|
2021-11-10 14:15:09 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
a1.send(42)
|
|
|
|
a2.send('a string with 42')
|
|
|
|
a3.send('should be ignored')
|
2021-11-10 14:15:09 +00:00
|
|
|
|
|
|
|
const expected = [
|
2022-02-01 18:09:56 +00:00
|
|
|
42,
|
|
|
|
'a string with 42'
|
2021-11-10 14:15:09 +00:00
|
|
|
]
|
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
b1.onmessage = function (message) {
|
|
|
|
t.is(message, expected.shift())
|
|
|
|
}
|
|
|
|
|
|
|
|
b2.onmessage = function (message) {
|
|
|
|
t.is(message, expected.shift())
|
2021-11-10 14:15:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-12-27 15:09:34 +00:00
|
|
|
test('corks', function (t) {
|
2021-12-30 20:13:47 +00:00
|
|
|
const a = new Protomux(new SecretStream(true))
|
|
|
|
|
|
|
|
a.cork()
|
|
|
|
|
|
|
|
a.addProtocol({
|
2021-12-27 15:09:34 +00:00
|
|
|
name: 'other',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 2
|
2021-12-30 20:13:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const ap = a.addProtocol({
|
2021-12-27 15:09:34 +00:00
|
|
|
name: 'multi',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 2
|
2021-12-30 20:13:47 +00:00
|
|
|
})
|
2021-12-27 15:09:34 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
const a1 = ap.addMessage({ encoding: c.int })
|
|
|
|
const a2 = ap.addMessage({ encoding: c.string })
|
|
|
|
|
2021-12-30 20:13:47 +00:00
|
|
|
const b = new Protomux(new SecretStream(false))
|
|
|
|
|
|
|
|
const bp = b.addProtocol({
|
2021-12-27 15:09:34 +00:00
|
|
|
name: 'multi',
|
2022-02-01 18:09:56 +00:00
|
|
|
messages: 2
|
2021-12-30 20:13:47 +00:00
|
|
|
})
|
2021-12-27 15:09:34 +00:00
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
const b1 = bp.addMessage({ encoding: c.int })
|
|
|
|
const b2 = bp.addMessage({ encoding: c.string })
|
|
|
|
|
2021-12-27 15:09:34 +00:00
|
|
|
replicate(a, b)
|
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
t.plan(4 + 1)
|
2021-12-27 15:09:34 +00:00
|
|
|
|
|
|
|
const expected = [
|
2022-02-01 18:09:56 +00:00
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
'a string'
|
2021-12-27 15:09:34 +00:00
|
|
|
]
|
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
a1.send(1)
|
|
|
|
a1.send(2)
|
|
|
|
a1.send(3)
|
|
|
|
a2.send('a string')
|
2021-12-30 20:13:47 +00:00
|
|
|
|
|
|
|
a.uncork()
|
2021-12-27 15:09:34 +00:00
|
|
|
|
|
|
|
b.stream.once('data', function (data) {
|
|
|
|
t.ok(expected.length === 0, 'received all messages in one data packet')
|
|
|
|
})
|
|
|
|
|
2022-02-01 18:09:56 +00:00
|
|
|
b1.onmessage = function (message) {
|
|
|
|
t.is(message, expected.shift())
|
|
|
|
}
|
|
|
|
|
|
|
|
b2.onmessage = function (message) {
|
|
|
|
t.is(message, expected.shift())
|
2021-12-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
})
|
2021-11-10 14:15:09 +00:00
|
|
|
|
|
|
|
function replicate (a, b) {
|
|
|
|
a.stream.rawStream.pipe(b.stream.rawStream).pipe(a.stream.rawStream)
|
|
|
|
}
|