mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-01 21:31:52 +00:00
Compare commits
62 Commits
Author | SHA1 | Date | |
---|---|---|---|
d2f91814b6 | |||
405f552c77 | |||
cba4b8b94a | |||
ae6a808fc7 | |||
eae6819b02 | |||
9ac5cca946 | |||
cdfd473cac | |||
5344ea33aa | |||
c2654206d3 | |||
539a007031 | |||
730a477bd8 | |||
19b29fe40b | |||
073e14553b | |||
2ed01e8f5b | |||
4a121696d1 | |||
a008d1db34 | |||
f50b9bafdd | |||
abd71d76e4 | |||
5c150db80b | |||
6d15dd94bb | |||
019d60cf61 | |||
0c97a3b191 | |||
4c04e2565b | |||
0bc2321649 | |||
6267bf84c6 | |||
4b82e33b39 | |||
8adced0f4b | |||
3f4f163f40 | |||
11c46ac995 | |||
749db165ce | |||
3781115f42 | |||
2070a5b0a0 | |||
c253a44f59 | |||
1e350a4dc7 | |||
69068f5da4 | |||
ce6edb05a2 | |||
73a79c9277 | |||
2e05826b30 | |||
e8a3c35cfb | |||
3c6bce9d28 | |||
d582fb5f61 | |||
b43f6ffb6f | |||
c456326515 | |||
6d1b729fcd | |||
fdfdb7a5ad | |||
3f87b5470c | |||
f4dafe55d1 | |||
541ec1fdda | |||
c3b5c132c0 | |||
4499bba514 | |||
9d778b56dd | |||
4769958b8f | |||
4d34757552 | |||
77600d3a8b | |||
3d17d40c2b | |||
8fac0f9dd6 | |||
500ca9b699 | |||
471d1da5ce | |||
b548f9d381 | |||
0b6361e27a | |||
506b4cbcfe | |||
e184baf6e4 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -25,3 +25,6 @@ build/Release
|
|||||||
# Dependency directory
|
# Dependency directory
|
||||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
|
lib
|
||||||
|
dist
|
29
.npmignore
Normal file
29
.npmignore
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
|
||||||
|
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directory
|
||||||
|
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||||
|
node_modules
|
||||||
|
|
||||||
|
test
|
16
.travis.yml
16
.travis.yml
@ -1,11 +1,25 @@
|
|||||||
sudo: false
|
sudo: false
|
||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "4.0"
|
- 4
|
||||||
|
- 5
|
||||||
|
- stable
|
||||||
|
|
||||||
# Make sure we have new NPM.
|
# Make sure we have new NPM.
|
||||||
before_install:
|
before_install:
|
||||||
- npm install -g npm
|
- npm install -g npm
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
- npm run lint
|
||||||
- npm test
|
- npm test
|
||||||
|
- npm run coverage
|
||||||
|
|
||||||
|
addons:
|
||||||
|
firefox: 'latest'
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- export DISPLAY=:99.0
|
||||||
|
- sh -e /etc/init.d/xvfb start
|
||||||
|
|
||||||
|
after_success:
|
||||||
|
- npm run coverage-publish
|
||||||
|
80
README.md
80
README.md
@ -1,11 +1,81 @@
|
|||||||
js-libp2p-tcp
|
js-libp2p-tcp
|
||||||
===============
|
===============
|
||||||
|
|
||||||
[](http://ipn.io) [[](http://webchat.freenode.net/?channels=%23ipfs) ](https://travis-ci.org/diasdavid/js-libp2p-tcp)  [](https://david-dm.org/diasdavid/js-libp2p-tcp) [](https://github.com/feross/standard)
|
[](http://ipn.io)
|
||||||
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||
|
[](https://travis-ci.org/libp2p/js-libp2p-tcp)
|
||||||
|

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

|

|
||||||

|

|
||||||
|
|
||||||
> Node.js implementation of the TCP module that libp2p uses, which implements the [abstract-connection]() interface for dial/listen.
|
> 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.
|
||||||
|
|
||||||
note: libp2p-tcp in Node.js is a very thin shim that adds the support to dial to a `multiaddr`. This small shim will enable libp2p to use other different transports.
|
## Description
|
||||||
|
|
||||||
|
`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.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
const TCP = require('libp2p-tcp')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
const mh2 = multiaddr('/ip6/::/tcp/9092')
|
||||||
|
|
||||||
|
const tcp = new Tcp()
|
||||||
|
|
||||||
|
var listener = tcp.createListener(mh1, function handler (socket) {
|
||||||
|
console.log('connection')
|
||||||
|
socket.end('bye')
|
||||||
|
})
|
||||||
|
|
||||||
|
var listener.listen(function ready () {
|
||||||
|
console.log('ready')
|
||||||
|
|
||||||
|
const client = tcp.dial(mh1)
|
||||||
|
client.pipe(process.stdout)
|
||||||
|
client.on('end', () => {
|
||||||
|
tcp.close()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
outputs
|
||||||
|
|
||||||
|
```
|
||||||
|
ready
|
||||||
|
connection
|
||||||
|
bye
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### npm
|
||||||
|
|
||||||
|
```sh
|
||||||
|
> npm i libp2p-tcp
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
[](https://github.com/diasdavid/interface-transport)
|
||||||
|
|
||||||
|
`libp2p-tcp` accepts TCP addresses both IPFS and non IPFS encapsulated addresses, i.e:
|
||||||
|
|
||||||
|
`/ip4/127.0.0.1/tcp/4001`
|
||||||
|
`/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
|
||||||
|
|
||||||
|
Both for dialing and listening.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT
|
||||||
|
12
circle.yml
Normal file
12
circle.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
machine:
|
||||||
|
node:
|
||||||
|
version: stable
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
pre:
|
||||||
|
- google-chrome --version
|
||||||
|
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
|
||||||
|
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
|
||||||
|
- sudo apt-get update
|
||||||
|
- sudo apt-get --only-upgrade install google-chrome-stable
|
||||||
|
- google-chrome --version
|
50
package.json
50
package.json
@ -1,12 +1,23 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-tcp",
|
"name": "libp2p-tcp",
|
||||||
"version": "0.1.2",
|
"version": "0.7.2",
|
||||||
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the abstract-connection interface",
|
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
|
||||||
"main": "src/index.js",
|
"main": "lib/index.js",
|
||||||
|
"jsnext:main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node tests/test-ac.js"
|
"lint": "aegir-lint",
|
||||||
|
"build": "aegir-build --env node",
|
||||||
|
"test": "aegir-test --env node",
|
||||||
|
"release": "aegir-release --env node",
|
||||||
|
"release-minor": "aegir-release --type minor --env node",
|
||||||
|
"release-major": "aegir-release --type major --env node",
|
||||||
|
"coverage": "aegir-coverage",
|
||||||
|
"coverage-publish": "aegir-coverage publish"
|
||||||
},
|
},
|
||||||
"pre-commit": [],
|
"pre-commit": [
|
||||||
|
"lint",
|
||||||
|
"test"
|
||||||
|
],
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/diasdavid/js-libp2p-tcp.git"
|
"url": "https://github.com/diasdavid/js-libp2p-tcp.git"
|
||||||
@ -21,13 +32,26 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
|
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"abstract-connection": "0.0.1",
|
"aegir": "^3.0.4",
|
||||||
"abstract-transport": "^0.1.0",
|
"chai": "^3.5.0",
|
||||||
"pre-commit": "^1.1.1",
|
"interface-transport": "^0.2.0",
|
||||||
"standard": "^5.2.2",
|
"pre-commit": "^1.1.2",
|
||||||
"tape": "^4.2.0"
|
"tape": "^4.5.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"multiaddr": "^1.0.0"
|
"duplexify": "^3.4.3",
|
||||||
}
|
"interface-connection": "0.1.2",
|
||||||
}
|
"ip-address": "^5.8.0",
|
||||||
|
"lodash.contains": "^2.4.3",
|
||||||
|
"mafmt": "^2.1.0",
|
||||||
|
"multiaddr": "^2.0.2",
|
||||||
|
"run-parallel": "^1.1.6"
|
||||||
|
},
|
||||||
|
"contributors": [
|
||||||
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
"João Antunes <j.goncalo.antunes@gmail.com>",
|
||||||
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
|
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
||||||
|
"dignifiedquire <dignifiedquire@gmail.com>"
|
||||||
|
]
|
||||||
|
}
|
226
src/index.js
226
src/index.js
@ -1,10 +1,224 @@
|
|||||||
var tcp = require('net')
|
'use strict'
|
||||||
|
|
||||||
exports = module.exports
|
const debug = require('debug')
|
||||||
|
const log = debug('libp2p:tcp')
|
||||||
|
const tcp = require('net')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
const Address6 = require('ip-address').Address6
|
||||||
|
const mafmt = require('mafmt')
|
||||||
|
// const parallel = require('run-parallel')
|
||||||
|
const contains = require('lodash.contains')
|
||||||
|
const os = require('os')
|
||||||
|
const Connection = require('interface-connection').Connection
|
||||||
|
|
||||||
exports.dial = function (multiaddr, options) {
|
exports = module.exports = TCP
|
||||||
options.ready = options.ready || function noop () {}
|
|
||||||
return tcp.connect(multiaddr.toOptions(), options.ready)
|
const IPFS_CODE = 421
|
||||||
|
const CLOSE_TIMEOUT = 2000
|
||||||
|
|
||||||
|
function TCP () {
|
||||||
|
if (!(this instanceof TCP)) {
|
||||||
|
return new TCP()
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dial = function (ma, options, callback) {
|
||||||
|
if (typeof options === 'function') {
|
||||||
|
callback = options
|
||||||
|
options = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!callback) {
|
||||||
|
callback = function noop () {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const socket = tcp.connect(ma.toOptions())
|
||||||
|
const conn = new Connection(socket)
|
||||||
|
|
||||||
|
socket.on('timeout', () => {
|
||||||
|
conn.emit('timeout')
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.once('error', (err) => {
|
||||||
|
callback(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('connect', () => {
|
||||||
|
callback(null, conn)
|
||||||
|
conn.emit('connect')
|
||||||
|
})
|
||||||
|
|
||||||
|
conn.getObservedAddrs = (cb) => {
|
||||||
|
return cb(null, [ma])
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
|
||||||
|
this.createListener = (options, handler) => {
|
||||||
|
if (typeof options === 'function') {
|
||||||
|
handler = options
|
||||||
|
options = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const listener = tcp.createServer((socket) => {
|
||||||
|
const conn = new Connection(socket)
|
||||||
|
|
||||||
|
conn.getObservedAddrs = (cb) => {
|
||||||
|
return cb(null, [getMultiaddr(socket)])
|
||||||
|
}
|
||||||
|
handler(conn)
|
||||||
|
})
|
||||||
|
|
||||||
|
let ipfsId
|
||||||
|
let listeningMultiaddr
|
||||||
|
|
||||||
|
listener._listen = listener.listen
|
||||||
|
listener.listen = (ma, callback) => {
|
||||||
|
listeningMultiaddr = ma
|
||||||
|
if (contains(ma.protoNames(), 'ipfs')) {
|
||||||
|
ipfsId = ma.stringTuples().filter((tuple) => {
|
||||||
|
if (tuple[0] === IPFS_CODE) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})[0][1]
|
||||||
|
listeningMultiaddr = ma.decapsulate('ipfs')
|
||||||
|
}
|
||||||
|
|
||||||
|
listener._listen(listeningMultiaddr.toOptions(), callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
listener._close = listener.close
|
||||||
|
listener.close = (options, callback) => {
|
||||||
|
if (typeof options === 'function') {
|
||||||
|
callback = options
|
||||||
|
options = {}
|
||||||
|
}
|
||||||
|
if (!callback) { callback = function noop () {} }
|
||||||
|
if (!options) { options = {} }
|
||||||
|
|
||||||
|
let closed = false
|
||||||
|
listener._close(callback)
|
||||||
|
listener.once('close', () => {
|
||||||
|
closed = true
|
||||||
|
})
|
||||||
|
setTimeout(() => {
|
||||||
|
if (closed) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log('unable to close graciously, destroying conns')
|
||||||
|
Object.keys(listener.__connections).forEach((key) => {
|
||||||
|
log('destroying %s', key)
|
||||||
|
listener.__connections[key].destroy()
|
||||||
|
})
|
||||||
|
}, options.timeout || CLOSE_TIMEOUT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep track of open connections to destroy in case of timeout
|
||||||
|
listener.__connections = {}
|
||||||
|
listener.on('connection', (socket) => {
|
||||||
|
const key = `${socket.remoteAddress}:${socket.remotePort}`
|
||||||
|
listener.__connections[key] = socket
|
||||||
|
|
||||||
|
socket.on('close', () => {
|
||||||
|
delete listener.__connections[key]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.getAddrs = (callback) => {
|
||||||
|
const multiaddrs = []
|
||||||
|
const address = listener.address()
|
||||||
|
|
||||||
|
// Because TCP will only return the IPv6 version
|
||||||
|
// we need to capture from the passed multiaddr
|
||||||
|
if (listeningMultiaddr.toString().indexOf('ip4') !== -1) {
|
||||||
|
let m = listeningMultiaddr.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)
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, multiaddrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
return listener
|
||||||
|
/*
|
||||||
|
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)
|
||||||
|
if (ipfsHashId) {
|
||||||
|
m = m.encapsulate('/ipfs/' + ipfsHashId)
|
||||||
|
}
|
||||||
|
freshMultiaddrs.push(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (address.family === 'IPv6') {
|
||||||
|
let mh = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
|
||||||
|
if (ipfsHashId) {
|
||||||
|
mh = mh.encapsulate('/ipfs/' + ipfsHashId)
|
||||||
|
}
|
||||||
|
|
||||||
|
freshMultiaddrs.push(mh)
|
||||||
|
}
|
||||||
|
|
||||||
|
cb()
|
||||||
|
})
|
||||||
|
listeners.push(listener)
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
this.filter = (multiaddrs) => {
|
||||||
|
if (!Array.isArray(multiaddrs)) {
|
||||||
|
multiaddrs = [multiaddrs]
|
||||||
|
}
|
||||||
|
return multiaddrs.filter((ma) => {
|
||||||
|
if (contains(ma.protoNames(), 'ipfs')) {
|
||||||
|
ma = ma.decapsulate('ipfs')
|
||||||
|
}
|
||||||
|
return mafmt.TCP.matches(ma)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createListener = tcp.createServer
|
function getMultiaddr (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
|
||||||
|
}
|
||||||
|
23
test/interface-transport.spec.js
Normal file
23
test/interface-transport.spec.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* 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)
|
||||||
|
})
|
||||||
|
})
|
483
test/libp2p-tcp.spec.js
Normal file
483
test/libp2p-tcp.spec.js
Normal file
@ -0,0 +1,483 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const TCP = require('../src')
|
||||||
|
const net = require('net')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
const Connection = require('interface-connection').Connection
|
||||||
|
|
||||||
|
describe('instantiate the transport', () => {
|
||||||
|
it('create', (done) => {
|
||||||
|
const tcp = new TCP()
|
||||||
|
expect(tcp).to.exist
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('create without new', (done) => {
|
||||||
|
const tcp = TCP()
|
||||||
|
expect(tcp).to.exist
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('listen', () => {
|
||||||
|
let tcp
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
tcp = new TCP()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen, check for callback', (done) => {
|
||||||
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
const listener = tcp.createListener((conn) => {})
|
||||||
|
listener.listen(mh, () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen, check for listening event', (done) => {
|
||||||
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
const listener = tcp.createListener((conn) => {})
|
||||||
|
listener.on('listening', () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
listener.listen(mh)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen, check for the close event', (done) => {
|
||||||
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
const listener = tcp.createListener((conn) => {})
|
||||||
|
listener.on('close', done)
|
||||||
|
listener.on('listening', () => {
|
||||||
|
listener.close()
|
||||||
|
})
|
||||||
|
listener.listen(mh)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen on addr with /ipfs/QmHASH', (done) => {
|
||||||
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||||
|
const listener = tcp.createListener((conn) => {})
|
||||||
|
listener.listen(mh, () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('close listener with connections, through timeout', (done) => {
|
||||||
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9091/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
conn.pipe(conn)
|
||||||
|
})
|
||||||
|
listener.listen(mh, () => {
|
||||||
|
const socket1 = net.connect(9091)
|
||||||
|
const socket2 = net.connect(9091)
|
||||||
|
socket1.write('Some data that is never handled')
|
||||||
|
socket1.end()
|
||||||
|
socket1.on('error', () => {})
|
||||||
|
socket2.on('error', () => {})
|
||||||
|
socket1.on('connect', () => {
|
||||||
|
listener.close(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 listener = tcp.createListener((conn) => {})
|
||||||
|
listener.listen(mh, () => {
|
||||||
|
listener.getAddrs((err, multiaddrs) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(multiaddrs.length).to.equal(1)
|
||||||
|
// multiaddrs.forEach((ma) => {
|
||||||
|
// console.log(ma.toString())
|
||||||
|
// })
|
||||||
|
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)
|
||||||
|
// multiaddrs.forEach((ma) => {
|
||||||
|
// console.log(ma.toString())
|
||||||
|
// })
|
||||||
|
|
||||||
|
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)
|
||||||
|
// multiaddrs.forEach((ma) => {
|
||||||
|
// console.log(ma.toString())
|
||||||
|
// })
|
||||||
|
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)
|
||||||
|
// multiaddrs.forEach((ma) => {
|
||||||
|
// console.log(ma.toString())
|
||||||
|
// })
|
||||||
|
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)
|
||||||
|
// multiaddrs.forEach((ma) => {
|
||||||
|
// console.log(ma.toString())
|
||||||
|
// })
|
||||||
|
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) => {
|
||||||
|
conn.pipe(conn)
|
||||||
|
})
|
||||||
|
listener.on('listening', done)
|
||||||
|
listener.listen(ma)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach((done) => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('dial on IPv4', (done) => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
conn.write('hey')
|
||||||
|
conn.end()
|
||||||
|
conn.on('data', (chunk) => {
|
||||||
|
expect(chunk.toString()).to.equal('hey')
|
||||||
|
})
|
||||||
|
conn.on('end', done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('dial to non existent listener', (done) => {
|
||||||
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/8989')
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
conn.on('error', (err) => {
|
||||||
|
expect(err).to.exist
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('dial on IPv6', (done) => {
|
||||||
|
const ma = multiaddr('/ip6/::/tcp/9066')
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
conn.pipe(conn)
|
||||||
|
})
|
||||||
|
listener.listen(ma, dialStep)
|
||||||
|
|
||||||
|
function dialStep () {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
conn.write('hey')
|
||||||
|
conn.end()
|
||||||
|
conn.on('data', (chunk) => {
|
||||||
|
expect(chunk.toString()).to.equal('hey')
|
||||||
|
})
|
||||||
|
conn.on('end', () => {
|
||||||
|
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)
|
||||||
|
conn.write('hey')
|
||||||
|
conn.end()
|
||||||
|
conn.on('data', (chunk) => {
|
||||||
|
expect(chunk.toString()).to.equal('hey')
|
||||||
|
})
|
||||||
|
conn.on('end', done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('filter addrs', () => {
|
||||||
|
let tcp
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
tcp = new TCP()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('filter valid addrs for this transport', (done) => {
|
||||||
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
|
||||||
|
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, mh4])
|
||||||
|
expect(valid.length).to.equal(2)
|
||||||
|
expect(valid[0]).to.deep.equal(mh1)
|
||||||
|
expect(valid[1]).to.deep.equal(mh4)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('filter a single addr for this transport', (done) => {
|
||||||
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
|
||||||
|
const valid = tcp.filter(mh1)
|
||||||
|
expect(valid.length).to.equal(1)
|
||||||
|
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) => {
|
||||||
|
var dialerObsAddrs
|
||||||
|
var listenerObsAddrs
|
||||||
|
|
||||||
|
const listener = tcp.createListener((conn) => {
|
||||||
|
expect(conn).to.exist
|
||||||
|
conn.getObservedAddrs((err, addrs) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
dialerObsAddrs = addrs
|
||||||
|
conn.end()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
|
||||||
|
conn.resume()
|
||||||
|
conn.on('end', () => {
|
||||||
|
conn.getObservedAddrs((err, addrs) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
listenerObsAddrs = addrs
|
||||||
|
conn.end()
|
||||||
|
|
||||||
|
listener.close(() => {
|
||||||
|
expect(listenerObsAddrs[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
|
||||||
|
conn.end()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
|
||||||
|
conn.resume()
|
||||||
|
conn.on('end', () => {
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.exist
|
||||||
|
expect(peerInfo).to.not.exist
|
||||||
|
conn.end()
|
||||||
|
|
||||||
|
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')
|
||||||
|
conn.end()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
const conn = tcp.dial(ma)
|
||||||
|
|
||||||
|
conn.resume()
|
||||||
|
conn.on('end', () => {
|
||||||
|
conn.setPeerInfo('arroz')
|
||||||
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(peerInfo).to.equal('arroz')
|
||||||
|
conn.end()
|
||||||
|
|
||||||
|
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) => {
|
||||||
|
conn.pipe(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)
|
||||||
|
connWrap.write('hey')
|
||||||
|
connWrap.end()
|
||||||
|
connWrap.on('data', (chunk) => {
|
||||||
|
expect(chunk.toString()).to.equal('hey')
|
||||||
|
})
|
||||||
|
connWrap.on('end', () => {
|
||||||
|
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()
|
||||||
|
connWrap.write('hey')
|
||||||
|
connWrap.end()
|
||||||
|
connWrap.on('data', (chunk) => {
|
||||||
|
expect(chunk.toString()).to.equal('hey')
|
||||||
|
})
|
||||||
|
connWrap.on('end', 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')
|
||||||
|
})
|
||||||
|
connWrap.write('hey')
|
||||||
|
connWrap.end()
|
||||||
|
connWrap.on('data', (chunk) => {
|
||||||
|
expect(chunk.toString()).to.equal('hey')
|
||||||
|
})
|
||||||
|
connWrap.on('end', 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')
|
||||||
|
}
|
||||||
|
|
||||||
|
connWrap3.write('hey')
|
||||||
|
connWrap3.end()
|
||||||
|
connWrap3.on('data', (chunk) => {
|
||||||
|
expect(chunk.toString()).to.equal('hey')
|
||||||
|
})
|
||||||
|
connWrap3.on('end', () => {
|
||||||
|
connWrap3.getPeerInfo((err, peerInfo) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(peerInfo).to.equal('inner doll')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -1,14 +0,0 @@
|
|||||||
var tape = require('tape')
|
|
||||||
var tests = require('abstract-transport/tests')
|
|
||||||
var conn = require('../src')
|
|
||||||
|
|
||||||
var common = {
|
|
||||||
setup: function (t, cb) {
|
|
||||||
cb(null, conn)
|
|
||||||
},
|
|
||||||
teardown: function (t, cb) {
|
|
||||||
cb()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tests(tape, common)
|
|
Reference in New Issue
Block a user