mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-06-24 06:21:50 +00:00
feat: change api to async / await (#112)
BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await. The API has also been updated according to the latest `interface-transport` version, https://github.com/libp2p/interface-transport/tree/v0.6.0#api.
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream')
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
@ -9,122 +8,133 @@ chai.use(dirtyChai)
|
||||
const TCP = require('../src')
|
||||
const net = require('net')
|
||||
const multiaddr = require('multiaddr')
|
||||
const pipe = require('it-pipe')
|
||||
const { collect, map } = require('streaming-iterables')
|
||||
const isCI = process.env.CI
|
||||
|
||||
describe('construction', () => {
|
||||
it('requires an upgrader', () => {
|
||||
expect(() => new TCP()).to.throw()
|
||||
})
|
||||
})
|
||||
|
||||
describe('listen', () => {
|
||||
let tcp
|
||||
|
||||
beforeEach(() => {
|
||||
tcp = new TCP()
|
||||
tcp = new TCP({
|
||||
upgrader: {
|
||||
upgradeOutbound: maConn => maConn,
|
||||
upgradeInbound: maConn => maConn
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('close listener with connections, through timeout', (done) => {
|
||||
it('close listener with connections, through timeout', async () => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const listener = tcp.createListener((conn) => {
|
||||
pull(conn, conn)
|
||||
pipe(conn, conn)
|
||||
})
|
||||
|
||||
listener.listen(mh, () => {
|
||||
const socket1 = net.connect(9090)
|
||||
const socket2 = net.connect(9090)
|
||||
await listener.listen(mh)
|
||||
|
||||
socket1.write('Some data that is never handled')
|
||||
socket1.end()
|
||||
socket1.on('error', () => {})
|
||||
socket2.on('error', () => {})
|
||||
socket1.on('connect', () => {
|
||||
listener.close(done)
|
||||
const socket1 = net.connect(9090)
|
||||
const socket2 = net.connect(9090)
|
||||
|
||||
socket1.write('Some data that is never handled')
|
||||
socket1.end()
|
||||
socket1.on('error', () => {})
|
||||
socket2.on('error', () => {})
|
||||
|
||||
await new Promise((resolve) => {
|
||||
socket1.on('connect', async () => {
|
||||
await listener.close({ timeout: 100 })
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('listen on port 0', (done) => {
|
||||
it('listen on port 0', async () => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||
const listener = tcp.createListener((conn) => {})
|
||||
listener.listen(mh, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
await listener.listen(mh)
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('listen on IPv6 addr', (done) => {
|
||||
if (isCI) { return done() }
|
||||
it('listen on IPv6 addr', async () => {
|
||||
if (isCI) {
|
||||
return
|
||||
}
|
||||
const mh = multiaddr('/ip6/::/tcp/9090')
|
||||
const listener = tcp.createListener((conn) => {})
|
||||
listener.listen(mh, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
await listener.listen(mh)
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('listen on any Interface', (done) => {
|
||||
it('listen on any Interface', async () => {
|
||||
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090')
|
||||
const listener = tcp.createListener((conn) => {})
|
||||
listener.listen(mh, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
await listener.listen(mh)
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs', (done) => {
|
||||
it('getAddrs', async () => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||
const listener = tcp.createListener((conn) => {})
|
||||
listener.listen(mh, () => {
|
||||
listener.getAddrs((err, multiaddrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(multiaddrs.length).to.equal(1)
|
||||
expect(multiaddrs[0]).to.deep.equal(mh)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
await listener.listen(mh)
|
||||
|
||||
const multiaddrs = listener.getAddrs()
|
||||
expect(multiaddrs.length).to.equal(1)
|
||||
expect(multiaddrs[0]).to.deep.equal(mh)
|
||||
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs on port 0 listen', (done) => {
|
||||
it('getAddrs on port 0 listen', async () => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||
const listener = tcp.createListener((conn) => {})
|
||||
listener.listen(mh, () => {
|
||||
listener.getAddrs((err, multiaddrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(multiaddrs.length).to.equal(1)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
await listener.listen(mh)
|
||||
|
||||
const multiaddrs = listener.getAddrs()
|
||||
expect(multiaddrs.length).to.equal(1)
|
||||
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs from listening on 0.0.0.0', (done) => {
|
||||
it('getAddrs from listening on 0.0.0.0', async () => {
|
||||
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090')
|
||||
const listener = tcp.createListener((conn) => {})
|
||||
listener.listen(mh, () => {
|
||||
listener.getAddrs((err, multiaddrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(multiaddrs.length > 0).to.equal(true)
|
||||
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
await listener.listen(mh)
|
||||
|
||||
const multiaddrs = listener.getAddrs()
|
||||
expect(multiaddrs.length > 0).to.equal(true)
|
||||
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
|
||||
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
|
||||
it('getAddrs from listening on 0.0.0.0 and port 0', async () => {
|
||||
const mh = multiaddr('/ip4/0.0.0.0/tcp/0')
|
||||
const listener = tcp.createListener((conn) => {})
|
||||
listener.listen(mh, () => {
|
||||
listener.getAddrs((err, multiaddrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(multiaddrs.length > 0).to.equal(true)
|
||||
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
await listener.listen(mh)
|
||||
|
||||
const multiaddrs = listener.getAddrs()
|
||||
expect(multiaddrs.length > 0).to.equal(true)
|
||||
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
|
||||
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs preserves IPFS Id', (done) => {
|
||||
it('getAddrs preserves IPFS Id', async () => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const listener = tcp.createListener((conn) => {})
|
||||
listener.listen(mh, () => {
|
||||
listener.getAddrs((err, multiaddrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(multiaddrs.length).to.equal(1)
|
||||
expect(multiaddrs[0]).to.deep.equal(mh)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
await listener.listen(mh)
|
||||
|
||||
const multiaddrs = listener.getAddrs()
|
||||
expect(multiaddrs.length).to.equal(1)
|
||||
expect(multiaddrs[0]).to.deep.equal(mh)
|
||||
|
||||
await listener.close()
|
||||
})
|
||||
})
|
||||
|
||||
@ -133,127 +143,114 @@ describe('dial', () => {
|
||||
let listener
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||
|
||||
beforeEach((done) => {
|
||||
tcp = new TCP()
|
||||
beforeEach(async () => {
|
||||
tcp = new TCP({
|
||||
upgrader: {
|
||||
upgradeOutbound: maConn => maConn,
|
||||
upgradeInbound: maConn => maConn
|
||||
}
|
||||
})
|
||||
listener = tcp.createListener((conn) => {
|
||||
pull(
|
||||
pipe(
|
||||
conn,
|
||||
pull.map((x) => Buffer.from(x.toString() + '!')),
|
||||
map((x) => Buffer.from(x.toString() + '!')),
|
||||
conn
|
||||
)
|
||||
})
|
||||
listener.listen(ma, done)
|
||||
await listener.listen(ma)
|
||||
})
|
||||
|
||||
afterEach((done) => {
|
||||
listener.close(done)
|
||||
})
|
||||
afterEach(() => listener.close())
|
||||
|
||||
it('dial on IPv4', (done) => {
|
||||
pull(
|
||||
pull.values(['hey']),
|
||||
tcp.dial(ma),
|
||||
pull.collect((err, values) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(values).to.eql([Buffer.from('hey!')])
|
||||
done()
|
||||
})
|
||||
it('dial on IPv4', async () => {
|
||||
const values = await pipe(
|
||||
['hey'],
|
||||
await tcp.dial(ma),
|
||||
collect
|
||||
)
|
||||
expect(values).to.eql([Buffer.from('hey!')])
|
||||
})
|
||||
|
||||
it('dial to non existent listener', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/8989')
|
||||
pull(
|
||||
tcp.dial(ma),
|
||||
pull.onEnd((err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('dial on IPv6', (done) => {
|
||||
if (isCI) { return done() }
|
||||
it('dial on IPv6', async () => {
|
||||
if (isCI) {
|
||||
return
|
||||
}
|
||||
|
||||
const ma = multiaddr('/ip6/::/tcp/9066')
|
||||
const listener = tcp.createListener((conn) => {
|
||||
pull(conn, conn)
|
||||
pipe(conn, conn)
|
||||
})
|
||||
listener.listen(ma, () => {
|
||||
pull(
|
||||
pull.values(['hey']),
|
||||
tcp.dial(ma),
|
||||
pull.collect((err, values) => {
|
||||
expect(err).to.not.exist()
|
||||
await listener.listen(ma)
|
||||
|
||||
expect(values).to.be.eql([Buffer.from('hey')])
|
||||
|
||||
listener.close(done)
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it('dial and destroy on listener', (done) => {
|
||||
let count = 0
|
||||
const closed = () => ++count === 2 ? finish() : null
|
||||
|
||||
const ma = multiaddr('/ip6/::/tcp/9067')
|
||||
|
||||
const listener = tcp.createListener((conn) => {
|
||||
pull(
|
||||
pull.empty(),
|
||||
conn,
|
||||
pull.onEnd(closed)
|
||||
)
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
pull(tcp.dial(ma), pull.onEnd(closed))
|
||||
})
|
||||
|
||||
function finish () {
|
||||
listener.close(done)
|
||||
}
|
||||
})
|
||||
|
||||
it('dial and destroy on dialer', (done) => {
|
||||
if (isCI) { return done() }
|
||||
|
||||
let count = 0
|
||||
const destroyed = () => ++count === 2 ? finish() : null
|
||||
|
||||
const ma = multiaddr('/ip6/::/tcp/9068')
|
||||
|
||||
const listener = tcp.createListener((conn) => {
|
||||
pull(conn, pull.onEnd(destroyed))
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
pull(
|
||||
pull.empty(),
|
||||
tcp.dial(ma),
|
||||
pull.onEnd(destroyed)
|
||||
)
|
||||
})
|
||||
|
||||
function finish () {
|
||||
listener.close(done)
|
||||
}
|
||||
})
|
||||
|
||||
it('dial on IPv4 with IPFS Id', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const conn = tcp.dial(ma)
|
||||
|
||||
pull(
|
||||
pull.values(['hey']),
|
||||
conn,
|
||||
pull.collect((err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.be.eql([Buffer.from('hey!')])
|
||||
done()
|
||||
})
|
||||
const values = await pipe(
|
||||
['hey'],
|
||||
await tcp.dial(ma),
|
||||
collect
|
||||
)
|
||||
expect(values).to.be.eql([Buffer.from('hey')])
|
||||
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('dial and destroy on listener', async () => {
|
||||
let handled
|
||||
const handledPromise = new Promise((resolve) => {
|
||||
handled = resolve
|
||||
})
|
||||
|
||||
const ma = multiaddr('/ip6/::/tcp/0')
|
||||
|
||||
const listener = tcp.createListener(async (conn) => {
|
||||
await pipe(
|
||||
[],
|
||||
conn
|
||||
)
|
||||
handled()
|
||||
})
|
||||
|
||||
await listener.listen(ma)
|
||||
const addrs = listener.getAddrs()
|
||||
await pipe(await tcp.dial(addrs[0]))
|
||||
|
||||
await handledPromise
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('dial and destroy on dialer', async () => {
|
||||
if (isCI) {
|
||||
return
|
||||
}
|
||||
|
||||
let handled
|
||||
const handledPromise = new Promise((resolve) => {
|
||||
handled = resolve
|
||||
})
|
||||
|
||||
const ma = multiaddr('/ip6/::/tcp/0')
|
||||
|
||||
const listener = tcp.createListener(async (conn) => {
|
||||
// pull(conn, pull.onEnd(destroyed))
|
||||
await pipe(conn)
|
||||
handled()
|
||||
})
|
||||
|
||||
await listener.listen(ma)
|
||||
const addrs = listener.getAddrs()
|
||||
await pipe(await tcp.dial(addrs[0]))
|
||||
|
||||
await handledPromise
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('dial on IPv4 with IPFS Id', async () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const conn = await tcp.dial(ma)
|
||||
|
||||
const res = await pipe(
|
||||
['hey'],
|
||||
conn,
|
||||
collect
|
||||
)
|
||||
expect(res).to.be.eql([Buffer.from('hey!')])
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user