mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-02 04:41:45 +00:00
Compare commits
36 Commits
Author | SHA1 | Date | |
---|---|---|---|
1d1d091082 | |||
0d007ff5fe | |||
2f2ca0d97b | |||
da8ee216e1 | |||
d9f65e0b0c | |||
5e89a2608b | |||
caa8d6dbb1 | |||
9b61bbc040 | |||
915fdc818f | |||
2dc52d0a5c | |||
e01046eb03 | |||
918b9df322 | |||
31b47fbfd2 | |||
26e9268bb5 | |||
676b5f3dbf | |||
ad7a331d9a | |||
a00c38437e | |||
cf0e9a1a8f | |||
d2f91814b6 | |||
405f552c77 | |||
cba4b8b94a | |||
ae6a808fc7 | |||
eae6819b02 | |||
9ac5cca946 | |||
cdfd473cac | |||
5344ea33aa | |||
c2654206d3 | |||
539a007031 | |||
730a477bd8 | |||
19b29fe40b | |||
073e14553b | |||
2ed01e8f5b | |||
4a121696d1 | |||
a008d1db34 | |||
f50b9bafdd | |||
abd71d76e4 |
105
README.md
105
README.md
@ -1,58 +1,62 @@
|
|||||||
js-libp2p-tcp
|
# js-libp2p-tcp
|
||||||
===============
|
|
||||||
|
|
||||||
[](http://ipn.io)
|
[](http://ipn.io)
|
||||||
[](http://webchat.freenode.net/?channels=%23ipfs)
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||
[](https://travis-ci.org/diasdavid/js-libp2p-tcp)
|
[](https://travis-ci.org/libp2p/js-libp2p-tcp)
|
||||||

|

|
||||||
[](https://david-dm.org/diasdavid/js-libp2p-tcp)
|
[](https://david-dm.org/libp2p/js-libp2p-tcp)
|
||||||
[](https://github.com/feross/standard)
|
[](https://github.com/feross/standard)
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
> Node.js implementation of the TCP module that libp2p uses, which implements
|
> Node.js implementation of the TCP module that libp2p uses, which implements the [interface-connection](https://github.com/libp2p/interface-connection) interface for dial/listen.
|
||||||
> the [interface-connection](https://github.com/diasdavid/interface-connection)
|
|
||||||
> interface for dial/listen.
|
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
`libp2p-tcp` in Node.js is a very thin shim that adds support for dialing to a
|
`libp2p-tcp` in Node.js is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other different transports.
|
||||||
`multiaddr`. This small shim will enable libp2p to use other different
|
|
||||||
transports.
|
**Note:** This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const Tcp = require('libp2p-tcp')
|
const TCP = require('libp2p-tcp')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
|
||||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
const mh2 = multiaddr('/ip6/::/tcp/9092')
|
const mh2 = multiaddr('/ip6/::/tcp/9092')
|
||||||
|
|
||||||
const tcp = new Tcp()
|
const tcp = new TCP()
|
||||||
|
|
||||||
tcp.createListener([mh1, mh2], function handler (socket) {
|
const listener = tcp.createListener(mh1, (socket) => {
|
||||||
console.log('connection')
|
console.log('new connection opened')
|
||||||
socket.end('bye')
|
pull(
|
||||||
}, function ready () {
|
pull.values(['hello']),
|
||||||
console.log('ready')
|
socket
|
||||||
|
)
|
||||||
const client = tcp.dial(mh1)
|
|
||||||
client.pipe(process.stdout)
|
|
||||||
client.on('end', () => {
|
|
||||||
tcp.close()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
listener.listen(() => {
|
||||||
|
console.log('listening')
|
||||||
|
|
||||||
|
pull(
|
||||||
|
tcp.dial(mh1),
|
||||||
|
pull.log,
|
||||||
|
pull.onEnd(() => {
|
||||||
|
tcp.close()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
outputs
|
outputs
|
||||||
|
|
||||||
```
|
```
|
||||||
ready
|
listening
|
||||||
connection
|
new connection opened
|
||||||
bye
|
hello
|
||||||
```
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@ -63,33 +67,40 @@ bye
|
|||||||
> npm i libp2p-tcp
|
> npm i libp2p-tcp
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## This module uses `pull-streams`
|
||||||
|
|
||||||
```js
|
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about what took us to make this migration, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
|
||||||
const Tcp = require('libp2p-tcp')
|
|
||||||
|
You can learn more about pull-streams at:
|
||||||
|
|
||||||
|
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
|
||||||
|
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
|
||||||
|
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
|
||||||
|
- [pull-streams documentation](https://pull-stream.github.io/)
|
||||||
|
|
||||||
|
### Converting `pull-streams` to Node.js Streams
|
||||||
|
|
||||||
|
If you are a Node.js streams user, you can convert a pull-stream to Node.js Stream using the module `pull-stream-to-stream`, giving you an instance of a Node.js stream that is linked to the pull-stream. Example:
|
||||||
|
|
||||||
|
```
|
||||||
|
const pullToStream = require('pull-stream-to-stream')
|
||||||
|
|
||||||
|
const nodeStreamInstance = pullToStream(pullStreamInstance)
|
||||||
|
// nodeStreamInstance is an instance of a Node.js Stream
|
||||||
```
|
```
|
||||||
|
|
||||||
### var tcp = new Tcp()
|
To learn more about his utility, visit https://pull-stream.github.io/#pull-stream-to-stream
|
||||||
|
|
||||||
Creates a new TCP object. This does nothing on its own, but provides access to
|
## API
|
||||||
`dial` and `createListener`.
|
|
||||||
|
|
||||||
### tcp.createListener(multiaddrs, handler, ready)
|
[](https://github.com/diasdavid/interface-transport)
|
||||||
|
|
||||||
Creates TCP servers that listen on the addresses described in the array
|
`libp2p-tcp` accepts TCP addresses both IPFS and non IPFS encapsulated addresses, i.e:
|
||||||
`multiaddrs`. Each connection will call `handler` with a connection stream.
|
|
||||||
`ready` is called once all servers are listening.
|
|
||||||
|
|
||||||
### tcp.dial(multiaddr, options={})
|
`/ip4/127.0.0.1/tcp/4001`
|
||||||
|
`/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
|
||||||
|
|
||||||
Connects to the multiaddress `multiaddr` using TCP, returning the socket stream.
|
Both for dialing and listening.
|
||||||
If `options.ready` is set to a function, it is called when a connection is
|
|
||||||
established.
|
|
||||||
|
|
||||||
### tcp.close(callback)
|
|
||||||
|
|
||||||
Closes all the listening TCP servers, calling `callback` once all of them have
|
|
||||||
been shut down.
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
24
package.json
24
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-tcp",
|
"name": "libp2p-tcp",
|
||||||
"version": "0.6.0",
|
"version": "0.8.0",
|
||||||
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
|
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"jsnext:main": "src/index.js",
|
"jsnext:main": "src/index.js",
|
||||||
@ -32,24 +32,28 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
|
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^3.0.4",
|
"aegir": "^6.0.1",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"interface-connection": "0.0.3",
|
"interface-transport": "^0.3.3",
|
||||||
"interface-transport": "^0.1.1",
|
"lodash.isfunction": "^3.0.8",
|
||||||
"pre-commit": "^1.1.2",
|
"pre-commit": "^1.1.2"
|
||||||
"tape": "^4.5.1"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"interface-connection": "0.2.1",
|
||||||
"ip-address": "^5.8.0",
|
"ip-address": "^5.8.0",
|
||||||
"mafmt": "^2.1.0",
|
"lodash.contains": "^2.4.3",
|
||||||
|
"mafmt": "^2.1.2",
|
||||||
"multiaddr": "^2.0.2",
|
"multiaddr": "^2.0.2",
|
||||||
"run-parallel": "^1.1.6"
|
"pull": "^2.1.1",
|
||||||
|
"stream-to-pull-stream": "^1.7.0"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
|
||||||
"João Antunes <j.goncalo.antunes@gmail.com>",
|
"João Antunes <j.goncalo.antunes@gmail.com>",
|
||||||
"Richard Littauer <richard.littauer@gmail.com>",
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
"Stephen Whitmore <stephen.whitmore@gmail.com>"
|
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
||||||
|
"dignifiedquire <dignifiedquire@gmail.com>",
|
||||||
|
"greenkeeperio-bot <support@greenkeeper.io>"
|
||||||
]
|
]
|
||||||
}
|
}
|
22
src/get-multiaddr.js
Normal file
22
src/get-multiaddr.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
const Address6 = require('ip-address').Address6
|
||||||
|
|
||||||
|
module.exports = (socket) => {
|
||||||
|
var mh
|
||||||
|
|
||||||
|
if (socket.remoteFamily === 'IPv6') {
|
||||||
|
var addr = new Address6(socket.remoteAddress)
|
||||||
|
if (addr.v4) {
|
||||||
|
var ip4 = addr.to4().correctForm()
|
||||||
|
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + socket.remotePort)
|
||||||
|
} else {
|
||||||
|
mh = multiaddr('/ip6/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mh = multiaddr('/ip4/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mh
|
||||||
|
}
|
124
src/index.js
124
src/index.js
@ -1,104 +1,68 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const debug = require('debug')
|
const net = require('net')
|
||||||
const log = debug('libp2p:tcp')
|
const toPull = require('stream-to-pull-stream')
|
||||||
const tcp = require('net')
|
|
||||||
const multiaddr = require('multiaddr')
|
|
||||||
const Address6 = require('ip-address').Address6
|
|
||||||
const mafmt = require('mafmt')
|
const mafmt = require('mafmt')
|
||||||
const parallel = require('run-parallel')
|
const contains = require('lodash.contains')
|
||||||
|
const isFunction = require('lodash.isfunction')
|
||||||
|
const Connection = require('interface-connection').Connection
|
||||||
|
const debug = require('debug')
|
||||||
|
const log = debug('libp2p:tcp:dial')
|
||||||
|
|
||||||
exports = module.exports = TCP
|
const createListener = require('./listener')
|
||||||
|
|
||||||
function TCP () {
|
module.exports = class TCP {
|
||||||
if (!(this instanceof TCP)) {
|
dial (ma, options, cb) {
|
||||||
return new TCP()
|
if (isFunction(options)) {
|
||||||
}
|
cb = options
|
||||||
|
|
||||||
const listeners = []
|
|
||||||
|
|
||||||
this.dial = function (multiaddr, options) {
|
|
||||||
if (!options) {
|
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
options.ready = options.ready || function noop () {}
|
|
||||||
const conn = tcp.connect(multiaddr.toOptions(), options.ready)
|
if (!cb) {
|
||||||
conn.getObservedAddrs = () => {
|
cb = () => {}
|
||||||
return [multiaddr]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cOpts = ma.toOptions()
|
||||||
|
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
||||||
|
|
||||||
|
const rawSocket = net.connect(cOpts, cb)
|
||||||
|
|
||||||
|
rawSocket.once('timeout', () => {
|
||||||
|
log('timeout')
|
||||||
|
rawSocket.emit('error', new Error('Timeout'))
|
||||||
|
})
|
||||||
|
|
||||||
|
const socket = toPull.duplex(rawSocket)
|
||||||
|
|
||||||
|
const conn = new Connection(socket)
|
||||||
|
|
||||||
|
conn.getObservedAddrs = (cb) => {
|
||||||
|
return cb(null, [ma])
|
||||||
|
}
|
||||||
|
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
this.createListener = (multiaddrs, handler, callback) => {
|
createListener (options, handler) {
|
||||||
if (!Array.isArray(multiaddrs)) {
|
if (isFunction(options)) {
|
||||||
multiaddrs = [multiaddrs]
|
handler = options
|
||||||
|
options = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const freshMultiaddrs = []
|
handler = handler || (() => {})
|
||||||
|
|
||||||
parallel(multiaddrs.map((m) => (cb) => {
|
return createListener(handler)
|
||||||
const listener = tcp.createServer((conn) => {
|
|
||||||
conn.getObservedAddrs = () => {
|
|
||||||
return [getMultiaddr(conn)]
|
|
||||||
}
|
|
||||||
handler(conn)
|
|
||||||
})
|
|
||||||
listener.listen(m.toOptions(), () => {
|
|
||||||
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
|
|
||||||
const address = listener.address()
|
|
||||||
if (m.toString().indexOf('ip4')) {
|
|
||||||
m = m.decapsulate('tcp')
|
|
||||||
m = m.encapsulate('/tcp/' + address.port)
|
|
||||||
freshMultiaddrs.push(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (address.family === 'IPv6') {
|
filter (multiaddrs) {
|
||||||
freshMultiaddrs.push(multiaddr('/ip6/' + address.address + '/tcp/' + address.port))
|
|
||||||
}
|
|
||||||
|
|
||||||
cb()
|
|
||||||
})
|
|
||||||
listeners.push(listener)
|
|
||||||
}), (err) => {
|
|
||||||
callback(err, freshMultiaddrs)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
this.close = (callback) => {
|
|
||||||
if (listeners.length === 0) {
|
|
||||||
log('Called close with no active listeners')
|
|
||||||
return callback()
|
|
||||||
}
|
|
||||||
|
|
||||||
parallel(listeners.map((listener) => {
|
|
||||||
return (cb) => listener.close(cb)
|
|
||||||
}), callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.filter = (multiaddrs) => {
|
|
||||||
if (!Array.isArray(multiaddrs)) {
|
if (!Array.isArray(multiaddrs)) {
|
||||||
multiaddrs = [multiaddrs]
|
multiaddrs = [multiaddrs]
|
||||||
}
|
}
|
||||||
return multiaddrs.filter((ma) => {
|
return multiaddrs.filter((ma) => {
|
||||||
|
if (contains(ma.protoNames(), 'ipfs')) {
|
||||||
|
ma = ma.decapsulate('ipfs')
|
||||||
|
}
|
||||||
return mafmt.TCP.matches(ma)
|
return mafmt.TCP.matches(ma)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMultiaddr (conn) {
|
|
||||||
var mh
|
|
||||||
|
|
||||||
if (conn.remoteFamily === 'IPv6') {
|
|
||||||
var addr = new Address6(conn.remoteAddress)
|
|
||||||
if (addr.v4) {
|
|
||||||
var ip4 = addr.to4().correctForm()
|
|
||||||
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + conn.remotePort)
|
|
||||||
} else {
|
|
||||||
mh = multiaddr('/ip6/' + conn.remoteAddress + '/tcp/' + conn.remotePort)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
mh = multiaddr('/ip4/' + conn.remoteAddress + '/tcp/' + conn.remotePort)
|
|
||||||
}
|
|
||||||
|
|
||||||
return mh
|
|
||||||
}
|
|
||||||
|
150
src/listener.js
Normal file
150
src/listener.js
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
const Connection = require('interface-connection').Connection
|
||||||
|
const os = require('os')
|
||||||
|
const contains = require('lodash.contains')
|
||||||
|
const net = require('net')
|
||||||
|
const toPull = require('stream-to-pull-stream')
|
||||||
|
const EventEmitter = require('events').EventEmitter
|
||||||
|
const debug = require('debug')
|
||||||
|
const log = debug('libp2p:tcp:listen')
|
||||||
|
|
||||||
|
const getMultiaddr = require('./get-multiaddr')
|
||||||
|
|
||||||
|
const IPFS_CODE = 421
|
||||||
|
const CLOSE_TIMEOUT = 2000
|
||||||
|
|
||||||
|
module.exports = (handler) => {
|
||||||
|
const listener = new EventEmitter()
|
||||||
|
|
||||||
|
const server = net.createServer((socket) => {
|
||||||
|
const addr = getMultiaddr(socket)
|
||||||
|
log('new connection', addr.toString())
|
||||||
|
|
||||||
|
const s = toPull.duplex(socket)
|
||||||
|
s.getObservedAddrs = (cb) => {
|
||||||
|
return cb(null, [addr])
|
||||||
|
}
|
||||||
|
|
||||||
|
trackSocket(server, socket)
|
||||||
|
|
||||||
|
const conn = new Connection(s)
|
||||||
|
handler(conn)
|
||||||
|
listener.emit('connection', conn)
|
||||||
|
})
|
||||||
|
|
||||||
|
server.on('listening', () => {
|
||||||
|
listener.emit('listening')
|
||||||
|
})
|
||||||
|
|
||||||
|
server.on('error', (err) => {
|
||||||
|
listener.emit('error', err)
|
||||||
|
})
|
||||||
|
|
||||||
|
server.on('close', () => {
|
||||||
|
listener.emit('close')
|
||||||
|
})
|
||||||
|
|
||||||
|
// Keep track of open connections to destroy in case of timeout
|
||||||
|
server.__connections = {}
|
||||||
|
|
||||||
|
listener.close = (options, cb) => {
|
||||||
|
if (typeof options === 'function') {
|
||||||
|
cb = options
|
||||||
|
options = {}
|
||||||
|
}
|
||||||
|
cb = cb || (() => {})
|
||||||
|
options = options || {}
|
||||||
|
|
||||||
|
let closed = false
|
||||||
|
server.close(cb)
|
||||||
|
server.once('close', () => {
|
||||||
|
closed = true
|
||||||
|
})
|
||||||
|
setTimeout(() => {
|
||||||
|
if (closed) return
|
||||||
|
|
||||||
|
log('unable to close graciously, destroying conns')
|
||||||
|
Object.keys(server.__connections).forEach((key) => {
|
||||||
|
log('destroying %s', key)
|
||||||
|
server.__connections[key].destroy()
|
||||||
|
})
|
||||||
|
}, options.timeout || CLOSE_TIMEOUT)
|
||||||
|
}
|
||||||
|
|
||||||
|
let ipfsId
|
||||||
|
let listeningAddr
|
||||||
|
|
||||||
|
listener.listen = (ma, cb) => {
|
||||||
|
listeningAddr = ma
|
||||||
|
if (contains(ma.protoNames(), 'ipfs')) {
|
||||||
|
ipfsId = getIpfsId(ma)
|
||||||
|
listeningAddr = ma.decapsulate('ipfs')
|
||||||
|
}
|
||||||
|
|
||||||
|
const lOpts = listeningAddr.toOptions()
|
||||||
|
log('Listening on %s %s', lOpts.port, lOpts.host)
|
||||||
|
return server.listen(lOpts.port, lOpts.host, cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
listener.getAddrs = (cb) => {
|
||||||
|
const multiaddrs = []
|
||||||
|
const address = server.address()
|
||||||
|
|
||||||
|
if (!address) {
|
||||||
|
return cb(new Error('Listener is not ready yet'))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Because TCP will only return the IPv6 version
|
||||||
|
// we need to capture from the passed multiaddr
|
||||||
|
if (listeningAddr.toString().indexOf('ip4') !== -1) {
|
||||||
|
let m = listeningAddr.decapsulate('tcp')
|
||||||
|
m = m.encapsulate('/tcp/' + address.port)
|
||||||
|
if (ipfsId) {
|
||||||
|
m = m.encapsulate('/ipfs/' + ipfsId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m.toString().indexOf('0.0.0.0') !== -1) {
|
||||||
|
const netInterfaces = os.networkInterfaces()
|
||||||
|
Object.keys(netInterfaces).forEach((niKey) => {
|
||||||
|
netInterfaces[niKey].forEach((ni) => {
|
||||||
|
if (ni.family === 'IPv4') {
|
||||||
|
multiaddrs.push(multiaddr(m.toString().replace('0.0.0.0', ni.address)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
multiaddrs.push(m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (address.family === 'IPv6') {
|
||||||
|
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
|
||||||
|
if (ipfsId) {
|
||||||
|
ma = ma.encapsulate('/ipfs/' + ipfsId)
|
||||||
|
}
|
||||||
|
|
||||||
|
multiaddrs.push(ma)
|
||||||
|
}
|
||||||
|
|
||||||
|
cb(null, multiaddrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
return listener
|
||||||
|
}
|
||||||
|
|
||||||
|
function getIpfsId (ma) {
|
||||||
|
return ma.stringTuples().filter((tuple) => {
|
||||||
|
return tuple[0] === IPFS_CODE
|
||||||
|
})[0][1]
|
||||||
|
}
|
||||||
|
|
||||||
|
function trackSocket (server, socket) {
|
||||||
|
const key = `${socket.remoteAddress}:${socket.remotePort}`
|
||||||
|
server.__connections[key] = socket
|
||||||
|
|
||||||
|
socket.on('close', () => {
|
||||||
|
delete server.__connections[key]
|
||||||
|
})
|
||||||
|
}
|
23
test/compliance.spec.js
Normal file
23
test/compliance.spec.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const tests = require('interface-transport')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
const Tcp = require('../src')
|
||||||
|
|
||||||
|
describe('interface-transport compliance', () => {
|
||||||
|
tests({
|
||||||
|
setup (cb) {
|
||||||
|
let tcp = new Tcp()
|
||||||
|
const addrs = [
|
||||||
|
multiaddr('/ip4/127.0.0.1/tcp/9091'),
|
||||||
|
multiaddr('/ip4/127.0.0.1/tcp/9092'),
|
||||||
|
multiaddr('/ip4/127.0.0.1/tcp/9093')
|
||||||
|
]
|
||||||
|
cb(null, tcp, addrs)
|
||||||
|
},
|
||||||
|
teardown (cb) {
|
||||||
|
cb()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
@ -1,132 +1,502 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const pull = require('pull-stream')
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
const TCPlibp2p = require('../src')
|
const TCP = require('../src')
|
||||||
const net = require('net')
|
const net = require('net')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
|
const Connection = require('interface-connection').Connection
|
||||||
|
|
||||||
describe('libp2p-tcp', function () {
|
describe('instantiate the transport', () => {
|
||||||
this.timeout(10000)
|
it('create', () => {
|
||||||
var tcp
|
const tcp = new TCP()
|
||||||
|
|
||||||
it('create', (done) => {
|
|
||||||
tcp = new TCPlibp2p()
|
|
||||||
expect(tcp).to.exist
|
expect(tcp).to.exist
|
||||||
done()
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('listen', () => {
|
||||||
|
let tcp
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
tcp = new TCP()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('create without new', (done) => {
|
it('close listener with connections, through timeout', (done) => {
|
||||||
tcp = TCPlibp2p()
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||||
expect(tcp).to.exist
|
const listener = tcp.createListener((conn) => {
|
||||||
done()
|
pull(conn, conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('close /wo listeners', (done) => {
|
listener.listen(mh, () => {
|
||||||
tcp = new TCPlibp2p()
|
const socket1 = net.connect(9090)
|
||||||
expect(tcp).to.exist
|
const socket2 = net.connect(9090)
|
||||||
expect(function () { tcp.close() }).to.throw(Error)
|
|
||||||
done()
|
socket1.write('Some data that is never handled')
|
||||||
|
socket1.end()
|
||||||
|
socket1.on('error', () => {})
|
||||||
|
socket2.on('error', () => {})
|
||||||
|
socket1.on('connect', () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('listen', (done) => {
|
it('listen on port 0', (done) => {
|
||||||
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
|
const listener = tcp.createListener((conn) => {})
|
||||||
|
listener.listen(mh, () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen on IPv6 addr', (done) => {
|
||||||
|
const mh = multiaddr('/ip6/::/tcp/9090')
|
||||||
|
const listener = tcp.createListener((conn) => {})
|
||||||
|
listener.listen(mh, () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen on any Interface', (done) => {
|
||||||
|
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090')
|
||||||
|
const listener = tcp.createListener((conn) => {})
|
||||||
|
listener.listen(mh, () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getAddrs', (done) => {
|
||||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
tcp.createListener(mh, (socket) => {
|
const listener = tcp.createListener((conn) => {})
|
||||||
expect(socket).to.exist
|
listener.listen(mh, () => {
|
||||||
socket.end()
|
listener.getAddrs((err, multiaddrs) => {
|
||||||
tcp.close(() => {
|
expect(err).to.not.exist
|
||||||
|
expect(multiaddrs.length).to.equal(1)
|
||||||
|
expect(multiaddrs[0]).to.deep.equal(mh)
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getAddrs on port 0 listen', (done) => {
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getAddrs from listening on 0.0.0.0', (done) => {
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getAddrs preserves IPFS Id', (done) => {
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('dial', () => {
|
||||||
|
let tcp
|
||||||
|
let listener
|
||||||
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
|
||||||
|
beforeEach((done) => {
|
||||||
|
tcp = new TCP()
|
||||||
|
listener = tcp.createListener((conn) => {
|
||||||
|
pull(
|
||||||
|
conn,
|
||||||
|
pull.map((x) => new Buffer(x.toString() + '!')),
|
||||||
|
conn
|
||||||
|
)
|
||||||
|
})
|
||||||
|
listener.listen(ma, done)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach((done) => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('dial on IPv4', (done) => {
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
tcp.dial(ma),
|
||||||
|
pull.collect((err, values) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(
|
||||||
|
values
|
||||||
|
).to.be.eql(
|
||||||
|
[new Buffer('hey!')]
|
||||||
|
)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
}, () => {
|
)
|
||||||
const socket = net.connect({ host: '127.0.0.1', port: 9090 })
|
|
||||||
socket.end()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('dial', (done) => {
|
it('dial to non existent listener', (done) => {
|
||||||
const server = net.createServer((socket) => {
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/8989')
|
||||||
expect(socket).to.exist
|
pull(
|
||||||
socket.end()
|
tcp.dial(ma),
|
||||||
server.close(done)
|
pull.onEnd((err) => {
|
||||||
})
|
expect(err).to.exist
|
||||||
|
|
||||||
server.listen(9090, () => {
|
|
||||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
|
||||||
const socket = tcp.dial(mh)
|
|
||||||
socket.end()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('listen on several', (done) => {
|
|
||||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
|
||||||
const mh2 = multiaddr('/ip4/127.0.0.1/tcp/9091')
|
|
||||||
const mh3 = multiaddr('/ip6/::/tcp/9092')
|
|
||||||
const tcp = new TCPlibp2p()
|
|
||||||
|
|
||||||
tcp.createListener([mh1, mh2, mh3], (socket) => {}, () => {
|
|
||||||
tcp.close(done)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('dial ipv6', (done) => {
|
|
||||||
const mh = multiaddr('/ip6/::/tcp/9091')
|
|
||||||
var dialerObsAddrs
|
|
||||||
|
|
||||||
tcp.createListener(mh, (conn) => {
|
|
||||||
expect(conn).to.exist
|
|
||||||
dialerObsAddrs = conn.getObservedAddrs()
|
|
||||||
conn.end()
|
|
||||||
}, () => {
|
|
||||||
const conn = tcp.dial(mh)
|
|
||||||
conn.on('end', () => {
|
|
||||||
expect(dialerObsAddrs.length).to.equal(1)
|
|
||||||
tcp.close()
|
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('dial on IPv6', (done) => {
|
||||||
|
const ma = multiaddr('/ip6/::/tcp/9066')
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
pull(conn, conn)
|
||||||
|
})
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
tcp.dial(ma),
|
||||||
|
pull.collect((err, values) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
|
||||||
|
expect(
|
||||||
|
values
|
||||||
|
).to.be.eql([
|
||||||
|
new Buffer('hey')
|
||||||
|
])
|
||||||
|
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('get observed addrs', (done) => {
|
it.skip('dial and destroy on listener', (done) => {
|
||||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
// TODO: why is this failing
|
||||||
var dialerObsAddrs
|
let count = 0
|
||||||
var listenerObsAddrs
|
const closed = ++count === 2 ? finish() : null
|
||||||
|
|
||||||
tcp.createListener(mh, (conn) => {
|
const ma = multiaddr('/ip6/::/tcp/9067')
|
||||||
expect(conn).to.exist
|
|
||||||
dialerObsAddrs = conn.getObservedAddrs()
|
|
||||||
conn.end()
|
|
||||||
}, () => {
|
|
||||||
const conn = tcp.dial(mh)
|
|
||||||
conn.on('end', () => {
|
|
||||||
listenerObsAddrs = conn.getObservedAddrs()
|
|
||||||
conn.end()
|
|
||||||
|
|
||||||
tcp.close(() => {
|
const listener = tcp.createListener((conn) => {
|
||||||
expect(listenerObsAddrs[0]).to.deep.equal(mh)
|
pull(
|
||||||
expect(dialerObsAddrs.length).to.equal(1)
|
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) => {
|
||||||
|
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([new Buffer('hey!')])
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('filter addrs', () => {
|
||||||
|
let tcp
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
tcp = new TCP()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('filter valid addrs for this transport', (done) => {
|
it('filter valid addrs for this transport', () => {
|
||||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
|
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
|
||||||
const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/http')
|
const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/http')
|
||||||
|
const mh4 = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||||
|
|
||||||
const valid = tcp.filter([mh1, mh2, mh3])
|
const valid = tcp.filter([mh1, mh2, mh3, mh4])
|
||||||
expect(valid.length).to.equal(1)
|
expect(valid.length).to.equal(2)
|
||||||
expect(valid[0]).to.deep.equal(mh1)
|
expect(valid[0]).to.deep.equal(mh1)
|
||||||
done()
|
expect(valid[1]).to.deep.equal(mh4)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('filter a valid addr for this transport', (done) => {
|
it('filter a single addr for this transport', () => {
|
||||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
|
||||||
const valid = tcp.filter(mh1)
|
const valid = tcp.filter(mh1)
|
||||||
expect(valid.length).to.equal(1)
|
expect(valid.length).to.equal(1)
|
||||||
expect(valid[0]).to.deep.equal(mh1)
|
expect(valid[0]).to.deep.equal(mh1)
|
||||||
done()
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('valid Connection', () => {
|
||||||
|
let tcp
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
tcp = new TCP()
|
||||||
|
})
|
||||||
|
|
||||||
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
|
||||||
|
it('get observed addrs', (done) => {
|
||||||
|
let dialerObsAddrs
|
||||||
|
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
expect(conn).to.exist
|
||||||
|
conn.getObservedAddrs((err, addrs) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
dialerObsAddrs = addrs
|
||||||
|
pull(pull.empty(), conn)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
pull(
|
||||||
|
conn,
|
||||||
|
pull.onEnd(endHandler)
|
||||||
|
)
|
||||||
|
|
||||||
|
function endHandler () {
|
||||||
|
conn.getObservedAddrs((err, addrs) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
pull(pull.empty(), conn)
|
||||||
|
closeAndAssert(listener, addrs)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeAndAssert (listener, addrs) {
|
||||||
|
listener.close(() => {
|
||||||
|
expect(addrs[0]).to.deep.equal(ma)
|
||||||
|
expect(dialerObsAddrs.length).to.equal(1)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('get Peer Info', (done) => {
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
expect(conn).to.exist
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.exist
|
||||||
|
expect(peerInfo).to.not.exist
|
||||||
|
pull(pull.empty(), conn)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
|
||||||
|
pull(conn, pull.onEnd(endHandler))
|
||||||
|
function endHandler () {
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.exist
|
||||||
|
expect(peerInfo).to.not.exist
|
||||||
|
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('set Peer Info', (done) => {
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
expect(conn).to.exist
|
||||||
|
conn.setPeerInfo('batatas')
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(peerInfo).to.equal('batatas')
|
||||||
|
pull(pull.empty(), conn)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
|
||||||
|
pull(conn, pull.onEnd(endHandler))
|
||||||
|
function endHandler () {
|
||||||
|
conn.setPeerInfo('arroz')
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(peerInfo).to.equal('arroz')
|
||||||
|
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe.skip('turbolence', () => {
|
||||||
|
it('dialer - emits error on the other end is terminated abruptly', (done) => {})
|
||||||
|
it('listener - emits error on the other end is terminated abruptly', (done) => {})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Connection wrap', () => {
|
||||||
|
let tcp
|
||||||
|
let listener
|
||||||
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
|
||||||
|
beforeEach((done) => {
|
||||||
|
tcp = new TCP()
|
||||||
|
listener = tcp.createListener((conn) => {
|
||||||
|
pull(conn, conn)
|
||||||
|
})
|
||||||
|
listener.on('listening', done)
|
||||||
|
listener.listen(ma)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach((done) => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('simple wrap', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
conn.setPeerInfo('peerInfo')
|
||||||
|
const connWrap = new Connection(conn)
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
connWrap,
|
||||||
|
pull.collect((err, chunks) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(chunks).to.be.eql([new Buffer('hey')])
|
||||||
|
|
||||||
|
connWrap.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(peerInfo).to.equal('peerInfo')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('buffer wrap', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
const connWrap = new Connection()
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
connWrap,
|
||||||
|
pull.collect((err, chunks) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(chunks).to.be.eql([new Buffer('hey')])
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
connWrap.setInnerConn(conn)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('overload wrap', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
const connWrap = new Connection(conn)
|
||||||
|
connWrap.getPeerInfo = (callback) => {
|
||||||
|
callback(null, 'none')
|
||||||
|
}
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.exist
|
||||||
|
})
|
||||||
|
connWrap.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(peerInfo).to.equal('none')
|
||||||
|
})
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
connWrap,
|
||||||
|
pull.collect((err, chunks) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(chunks).to.be.eql([new Buffer('hey')])
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('matryoshka wrap', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
const connWrap1 = new Connection(conn)
|
||||||
|
const connWrap2 = new Connection(connWrap1)
|
||||||
|
const connWrap3 = new Connection(connWrap2)
|
||||||
|
|
||||||
|
conn.getPeerInfo = (callback) => {
|
||||||
|
callback(null, 'inner doll')
|
||||||
|
}
|
||||||
|
pull(
|
||||||
|
pull.values(['hey']),
|
||||||
|
connWrap3,
|
||||||
|
pull.collect((err, chunks) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(chunks).to.be.eql([new Buffer('hey')])
|
||||||
|
connWrap3.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(peerInfo).to.equal('inner doll')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
/* eslint-env mocha */
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
const tape = require('tape')
|
|
||||||
const tests = require('interface-transport/tests')
|
|
||||||
const TCP = require('../src')
|
|
||||||
|
|
||||||
// Not adhering to this interface anymore!
|
|
||||||
describe.skip('interface-transport', () => {
|
|
||||||
it('works', (done) => {
|
|
||||||
const common = {
|
|
||||||
setup (t, cb) {
|
|
||||||
cb(null, new TCP())
|
|
||||||
},
|
|
||||||
teardown (t, cb) {
|
|
||||||
cb()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tape.onFinish(done)
|
|
||||||
tests(tape, common)
|
|
||||||
})
|
|
||||||
})
|
|
Reference in New Issue
Block a user