mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-03 15:01:51 +00:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
ce4974b01c |
19
.gitignore
vendored
19
.gitignore
vendored
@ -1,14 +1,7 @@
|
|||||||
**/node_modules/
|
|
||||||
**/*.log
|
|
||||||
test/repo-tests*
|
|
||||||
**/bundle.js
|
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
*.log
|
*.log
|
||||||
|
|
||||||
coverage
|
|
||||||
|
|
||||||
# Runtime data
|
# Runtime data
|
||||||
pids
|
pids
|
||||||
*.pid
|
*.pid
|
||||||
@ -26,17 +19,11 @@ coverage
|
|||||||
# node-waf configuration
|
# node-waf configuration
|
||||||
.lock-wscript
|
.lock-wscript
|
||||||
|
|
||||||
build
|
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||||
|
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
|
docs
|
||||||
dist
|
|
||||||
test/test-data/go-ipfs-repo/LOCK
|
|
||||||
test/test-data/go-ipfs-repo/LOG
|
|
||||||
test/test-data/go-ipfs-repo/LOG.old
|
|
||||||
|
|
||||||
# while testing npm5
|
|
||||||
package-lock.json
|
|
@ -26,4 +26,5 @@ build/Release
|
|||||||
# 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
|
||||||
|
|
||||||
test
|
test
|
||||||
|
docs
|
17
.travis.yml
17
.travis.yml
@ -1,20 +1,22 @@
|
|||||||
sudo: false
|
sudo: false
|
||||||
language: node_js
|
language: node_js
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
|
- node_js: 4
|
||||||
|
env: CXX=g++-4.8
|
||||||
- node_js: 6
|
- node_js: 6
|
||||||
env: CXX=g++-4.8
|
env: CXX=g++-4.8
|
||||||
- node_js: 8
|
- node_js: stable
|
||||||
env: CXX=g++-4.8
|
env: CXX=g++-4.8
|
||||||
# - node_js: stable
|
|
||||||
# env: CXX=g++-4.8
|
# Make sure we have new NPM.
|
||||||
|
before_install:
|
||||||
|
- npm install -g npm
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- npm run lint
|
- npm run lint
|
||||||
- npm run test
|
- npm test
|
||||||
- npm run coverage
|
- npm run coverage
|
||||||
- make test
|
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- export DISPLAY=:99.0
|
- export DISPLAY=:99.0
|
||||||
@ -24,9 +26,8 @@ after_success:
|
|||||||
- npm run coverage-publish
|
- npm run coverage-publish
|
||||||
|
|
||||||
addons:
|
addons:
|
||||||
firefox: 'latest'
|
|
||||||
apt:
|
apt:
|
||||||
sources:
|
sources:
|
||||||
- ubuntu-toolchain-r-test
|
- ubuntu-toolchain-r-test
|
||||||
packages:
|
packages:
|
||||||
- g++-4.8
|
- g++-4.8
|
@ -7,7 +7,7 @@
|
|||||||
[](https://david-dm.org/libp2p/js-libp2p-tcp)
|
[](https://david-dm.org/libp2p/js-libp2p-tcp)
|
||||||
[](https://github.com/feross/standard)
|
[](https://github.com/feross/standard)
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|
@ -5,10 +5,8 @@ machine:
|
|||||||
dependencies:
|
dependencies:
|
||||||
pre:
|
pre:
|
||||||
- google-chrome --version
|
- google-chrome --version
|
||||||
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
|
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
|
||||||
- sudo dpkg -i google-chrome.deb || true
|
- 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 update
|
||||||
- sudo apt-get install -f
|
- sudo apt-get --only-upgrade install google-chrome-stable
|
||||||
- sudo apt-get install --only-upgrade lsb-base
|
|
||||||
- sudo dpkg -i google-chrome.deb
|
|
||||||
- google-chrome --version
|
- google-chrome --version
|
||||||
|
40
package.json
40
package.json
@ -1,14 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-tcp",
|
"name": "libp2p-tcp",
|
||||||
"version": "0.11.0",
|
"version": "0.9.1",
|
||||||
"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": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "aegir-lint",
|
"lint": "aegir-lint",
|
||||||
"test": "aegir-test --env node",
|
"test": "aegir-test --env node",
|
||||||
"release": "aegir-release --env no-build",
|
"docs": "aegir-docs",
|
||||||
"release-minor": "aegir-release --type minor --env no-build",
|
"release": "aegir-release --env no-build --docs",
|
||||||
"release-major": "aegir-release --type major --env no-build",
|
"release-minor": "aegir-release --type minor --env no-build --docs",
|
||||||
|
"release-major": "aegir-release --type major --env no-build --docs",
|
||||||
"coverage": "aegir-coverage",
|
"coverage": "aegir-coverage",
|
||||||
"coverage-publish": "aegir-coverage publish"
|
"coverage-publish": "aegir-coverage publish"
|
||||||
},
|
},
|
||||||
@ -30,36 +31,31 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/libp2p/js-libp2p-tcp",
|
"homepage": "https://github.com/libp2p/js-libp2p-tcp",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=6.0.0",
|
"node": ">=4.0.0"
|
||||||
"npm": ">=3.0.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^11.0.2",
|
"aegir": "^9.2.0",
|
||||||
"chai": "^4.1.2",
|
"chai": "^3.5.0",
|
||||||
"dirty-chai": "^2.0.1",
|
"interface-transport": "^0.3.3",
|
||||||
"interface-transport": "~0.3.5",
|
|
||||||
"lodash.isfunction": "^3.0.8",
|
"lodash.isfunction": "^3.0.8",
|
||||||
"pre-commit": "^1.2.2",
|
"pre-commit": "^1.1.3",
|
||||||
"pull-stream": "^3.6.0"
|
"pull-stream": "^3.5.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"interface-connection": "~0.3.2",
|
"interface-connection": "0.3.0",
|
||||||
"ip-address": "^5.8.8",
|
"ip-address": "^5.8.2",
|
||||||
"lodash.includes": "^4.3.0",
|
"lodash.includes": "^4.3.0",
|
||||||
"lodash.isfunction": "^3.0.8",
|
"mafmt": "^2.1.2",
|
||||||
"mafmt": "^3.0.0",
|
"multiaddr": "^2.1.1",
|
||||||
"multiaddr": "^3.0.0",
|
|
||||||
"once": "^1.4.0",
|
|
||||||
"stream-to-pull-stream": "^1.7.2"
|
"stream-to-pull-stream": "^1.7.2"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
|
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
|
||||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
"Greenkeeper <support@greenkeeper.io>",
|
|
||||||
"João Antunes <j.goncalo.antunes@gmail.com>",
|
"João Antunes <j.goncalo.antunes@gmail.com>",
|
||||||
"Prashanth Chandra <coolshanth94@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>",
|
||||||
|
"greenkeeperio-bot <support@greenkeeper.io>"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ const multiaddr = require('multiaddr')
|
|||||||
const Address6 = require('ip-address').Address6
|
const Address6 = require('ip-address').Address6
|
||||||
|
|
||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
let mh
|
var mh
|
||||||
|
|
||||||
if (socket.remoteFamily === 'IPv6') {
|
if (socket.remoteFamily === 'IPv6') {
|
||||||
var addr = new Address6(socket.remoteAddress)
|
var addr = new Address6(socket.remoteAddress)
|
||||||
|
46
src/index.js
46
src/index.js
@ -6,52 +6,61 @@ const mafmt = require('mafmt')
|
|||||||
const includes = require('lodash.includes')
|
const includes = require('lodash.includes')
|
||||||
const isFunction = require('lodash.isfunction')
|
const isFunction = require('lodash.isfunction')
|
||||||
const Connection = require('interface-connection').Connection
|
const Connection = require('interface-connection').Connection
|
||||||
const once = require('once')
|
|
||||||
const debug = require('debug')
|
const debug = require('debug')
|
||||||
const log = debug('libp2p:tcp:dial')
|
const log = debug('libp2p:tcp:dial')
|
||||||
|
|
||||||
const createListener = require('./listener')
|
const createListener = require('./listener')
|
||||||
|
|
||||||
function noop () {}
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
class TCP {
|
class TCP {
|
||||||
|
/**
|
||||||
|
* Dial to another peer.
|
||||||
|
*
|
||||||
|
* @param {Multiaddr} ma - The address of the peer we want to dial to.
|
||||||
|
* @param {Object} [options={}]
|
||||||
|
* @param {function(Error?, Array<Multiaddr>?)} [callback]
|
||||||
|
* @returns {Connection}
|
||||||
|
*/
|
||||||
dial (ma, options, callback) {
|
dial (ma, options, callback) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
callback = options
|
callback = options
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
callback = callback || noop
|
if (!callback) {
|
||||||
|
callback = () => {}
|
||||||
|
}
|
||||||
|
|
||||||
callback = once(callback)
|
|
||||||
const cOpts = ma.toOptions()
|
const cOpts = ma.toOptions()
|
||||||
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
||||||
|
|
||||||
const rawSocket = net.connect(cOpts)
|
const rawSocket = net.connect(cOpts, callback)
|
||||||
|
|
||||||
rawSocket.once('timeout', () => {
|
rawSocket.once('timeout', () => {
|
||||||
log('timeout')
|
log('timeout')
|
||||||
rawSocket.emit('error', new Error('Timeout'))
|
rawSocket.emit('error', new Error('Timeout'))
|
||||||
})
|
})
|
||||||
|
|
||||||
rawSocket.once('error', callback)
|
|
||||||
|
|
||||||
rawSocket.once('connect', () => {
|
|
||||||
rawSocket.removeListener('error', callback)
|
|
||||||
callback()
|
|
||||||
})
|
|
||||||
|
|
||||||
const socket = toPull.duplex(rawSocket)
|
const socket = toPull.duplex(rawSocket)
|
||||||
|
|
||||||
const conn = new Connection(socket)
|
const conn = new Connection(socket)
|
||||||
|
|
||||||
conn.getObservedAddrs = (callback) => {
|
conn.getObservedAddrs = (cb) => {
|
||||||
return callback(null, [ma])
|
return cb(null, [ma])
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen for incoming `TCP` connetions.
|
||||||
|
*
|
||||||
|
* @param {Object} [options={}]
|
||||||
|
* @param {function(Connection)} [handler] - Called with newly incomin connections.
|
||||||
|
* @returns {Listener}
|
||||||
|
*/
|
||||||
createListener (options, handler) {
|
createListener (options, handler) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
handler = options
|
handler = options
|
||||||
@ -63,6 +72,13 @@ class TCP {
|
|||||||
return createListener(handler)
|
return createListener(handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter a list of multiaddrs for those which contain
|
||||||
|
* valid `TCP` addresses.
|
||||||
|
*
|
||||||
|
* @param {Multiaddr|Array<Multiaddr>} multiaddrs
|
||||||
|
* @returns {Array<Multiaddr>}
|
||||||
|
*/
|
||||||
filter (multiaddrs) {
|
filter (multiaddrs) {
|
||||||
if (!Array.isArray(multiaddrs)) {
|
if (!Array.isArray(multiaddrs)) {
|
||||||
multiaddrs = [multiaddrs]
|
multiaddrs = [multiaddrs]
|
||||||
|
@ -15,22 +15,58 @@ const getMultiaddr = require('./get-multiaddr')
|
|||||||
const IPFS_CODE = 421
|
const IPFS_CODE = 421
|
||||||
const CLOSE_TIMEOUT = 2000
|
const CLOSE_TIMEOUT = 2000
|
||||||
|
|
||||||
function noop () {}
|
/**
|
||||||
|
* Listening for incoming connections.
|
||||||
|
*
|
||||||
|
* @event listening
|
||||||
|
* @instance
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The server closes.
|
||||||
|
*
|
||||||
|
* @event close
|
||||||
|
* @instance
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* New connection established.
|
||||||
|
*
|
||||||
|
* @event connection
|
||||||
|
* @instance
|
||||||
|
* @type {Connection}
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The underlying server encountered an error.
|
||||||
|
*
|
||||||
|
* @event error
|
||||||
|
* @instance
|
||||||
|
* @type {Error}
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
module.exports = (handler) => {
|
module.exports = (handler) => {
|
||||||
|
/**
|
||||||
|
* @alias Listener
|
||||||
|
* @type {Eventemitter}
|
||||||
|
* @fires Listener#listening
|
||||||
|
* @fires Listener#close
|
||||||
|
* @fires Listener#connection
|
||||||
|
* @fires Listener#error
|
||||||
|
*/
|
||||||
const listener = new EventEmitter()
|
const listener = new EventEmitter()
|
||||||
|
|
||||||
const server = net.createServer((socket) => {
|
const server = net.createServer((socket) => {
|
||||||
// Avoid uncaught errors cause by unstable connections
|
|
||||||
socket.on('error', noop)
|
|
||||||
|
|
||||||
const addr = getMultiaddr(socket)
|
const addr = getMultiaddr(socket)
|
||||||
log('new connection', addr.toString())
|
log('new connection', addr.toString())
|
||||||
|
|
||||||
const s = toPull.duplex(socket)
|
const s = toPull.duplex(socket)
|
||||||
|
|
||||||
s.getObservedAddrs = (cb) => {
|
s.getObservedAddrs = (cb) => {
|
||||||
cb(null, [addr])
|
return cb(null, [addr])
|
||||||
}
|
}
|
||||||
|
|
||||||
trackSocket(server, socket)
|
trackSocket(server, socket)
|
||||||
@ -40,31 +76,36 @@ module.exports = (handler) => {
|
|||||||
listener.emit('connection', conn)
|
listener.emit('connection', conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
server.on('listening', () => listener.emit('listening'))
|
server.on('listening', () => {
|
||||||
server.on('error', (err) => listener.emit('error', err))
|
listener.emit('listening')
|
||||||
server.on('close', () => listener.emit('close'))
|
})
|
||||||
|
|
||||||
|
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
|
// Keep track of open connections to destroy in case of timeout
|
||||||
server.__connections = {}
|
server.__connections = {}
|
||||||
|
|
||||||
listener.close = (options, callback) => {
|
listener.close = (options, cb) => {
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
callback = options
|
cb = options
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
callback = callback || noop
|
cb = cb || (() => {})
|
||||||
options = options || {}
|
options = options || {}
|
||||||
|
|
||||||
let closed = false
|
let closed = false
|
||||||
server.close(callback)
|
server.close(cb)
|
||||||
|
|
||||||
server.once('close', () => {
|
server.once('close', () => {
|
||||||
closed = true
|
closed = true
|
||||||
})
|
})
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (closed) {
|
if (closed) return
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log('unable to close graciously, destroying conns')
|
log('unable to close graciously, destroying conns')
|
||||||
Object.keys(server.__connections).forEach((key) => {
|
Object.keys(server.__connections).forEach((key) => {
|
||||||
@ -77,7 +118,7 @@ module.exports = (handler) => {
|
|||||||
let ipfsId
|
let ipfsId
|
||||||
let listeningAddr
|
let listeningAddr
|
||||||
|
|
||||||
listener.listen = (ma, callback) => {
|
listener.listen = (ma, cb) => {
|
||||||
listeningAddr = ma
|
listeningAddr = ma
|
||||||
if (includes(ma.protoNames(), 'ipfs')) {
|
if (includes(ma.protoNames(), 'ipfs')) {
|
||||||
ipfsId = getIpfsId(ma)
|
ipfsId = getIpfsId(ma)
|
||||||
@ -86,15 +127,15 @@ module.exports = (handler) => {
|
|||||||
|
|
||||||
const lOpts = listeningAddr.toOptions()
|
const lOpts = listeningAddr.toOptions()
|
||||||
log('Listening on %s %s', lOpts.port, lOpts.host)
|
log('Listening on %s %s', lOpts.port, lOpts.host)
|
||||||
return server.listen(lOpts.port, lOpts.host, callback)
|
return server.listen(lOpts.port, lOpts.host, cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
listener.getAddrs = (callback) => {
|
listener.getAddrs = (cb) => {
|
||||||
const multiaddrs = []
|
const multiaddrs = []
|
||||||
const address = server.address()
|
const address = server.address()
|
||||||
|
|
||||||
if (!address) {
|
if (!address) {
|
||||||
return callback(new Error('Listener is not ready yet'))
|
return cb(new Error('Listener is not ready yet'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Because TCP will only return the IPv6 version
|
// Because TCP will only return the IPv6 version
|
||||||
@ -129,7 +170,7 @@ module.exports = (handler) => {
|
|||||||
multiaddrs.push(ma)
|
multiaddrs.push(ma)
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, multiaddrs)
|
cb(null, multiaddrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return listener
|
return listener
|
||||||
|
@ -2,10 +2,7 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const pull = require('pull-stream')
|
const pull = require('pull-stream')
|
||||||
const chai = require('chai')
|
const expect = require('chai').expect
|
||||||
const dirtyChai = require('dirty-chai')
|
|
||||||
const expect = chai.expect
|
|
||||||
chai.use(dirtyChai)
|
|
||||||
const TCP = require('../src')
|
const TCP = require('../src')
|
||||||
const net = require('net')
|
const net = require('net')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
@ -14,7 +11,7 @@ const Connection = require('interface-connection').Connection
|
|||||||
describe('instantiate the transport', () => {
|
describe('instantiate the transport', () => {
|
||||||
it('create', () => {
|
it('create', () => {
|
||||||
const tcp = new TCP()
|
const tcp = new TCP()
|
||||||
expect(tcp).to.exist()
|
expect(tcp).to.exist
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -74,7 +71,7 @@ describe('listen', () => {
|
|||||||
const listener = tcp.createListener((conn) => {})
|
const listener = tcp.createListener((conn) => {})
|
||||||
listener.listen(mh, () => {
|
listener.listen(mh, () => {
|
||||||
listener.getAddrs((err, multiaddrs) => {
|
listener.getAddrs((err, multiaddrs) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(multiaddrs.length).to.equal(1)
|
expect(multiaddrs.length).to.equal(1)
|
||||||
expect(multiaddrs[0]).to.deep.equal(mh)
|
expect(multiaddrs[0]).to.deep.equal(mh)
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
@ -87,7 +84,7 @@ describe('listen', () => {
|
|||||||
const listener = tcp.createListener((conn) => {})
|
const listener = tcp.createListener((conn) => {})
|
||||||
listener.listen(mh, () => {
|
listener.listen(mh, () => {
|
||||||
listener.getAddrs((err, multiaddrs) => {
|
listener.getAddrs((err, multiaddrs) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(multiaddrs.length).to.equal(1)
|
expect(multiaddrs.length).to.equal(1)
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
})
|
})
|
||||||
@ -99,7 +96,7 @@ describe('listen', () => {
|
|||||||
const listener = tcp.createListener((conn) => {})
|
const listener = tcp.createListener((conn) => {})
|
||||||
listener.listen(mh, () => {
|
listener.listen(mh, () => {
|
||||||
listener.getAddrs((err, multiaddrs) => {
|
listener.getAddrs((err, multiaddrs) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(multiaddrs.length > 0).to.equal(true)
|
expect(multiaddrs.length > 0).to.equal(true)
|
||||||
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
|
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
@ -112,7 +109,7 @@ describe('listen', () => {
|
|||||||
const listener = tcp.createListener((conn) => {})
|
const listener = tcp.createListener((conn) => {})
|
||||||
listener.listen(mh, () => {
|
listener.listen(mh, () => {
|
||||||
listener.getAddrs((err, multiaddrs) => {
|
listener.getAddrs((err, multiaddrs) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(multiaddrs.length > 0).to.equal(true)
|
expect(multiaddrs.length > 0).to.equal(true)
|
||||||
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
|
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
@ -125,7 +122,7 @@ describe('listen', () => {
|
|||||||
const listener = tcp.createListener((conn) => {})
|
const listener = tcp.createListener((conn) => {})
|
||||||
listener.listen(mh, () => {
|
listener.listen(mh, () => {
|
||||||
listener.getAddrs((err, multiaddrs) => {
|
listener.getAddrs((err, multiaddrs) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(multiaddrs.length).to.equal(1)
|
expect(multiaddrs.length).to.equal(1)
|
||||||
expect(multiaddrs[0]).to.deep.equal(mh)
|
expect(multiaddrs[0]).to.deep.equal(mh)
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
@ -160,8 +157,12 @@ describe('dial', () => {
|
|||||||
pull.values(['hey']),
|
pull.values(['hey']),
|
||||||
tcp.dial(ma),
|
tcp.dial(ma),
|
||||||
pull.collect((err, values) => {
|
pull.collect((err, values) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(values).to.eql([new Buffer('hey!')])
|
expect(
|
||||||
|
values
|
||||||
|
).to.be.eql(
|
||||||
|
[new Buffer('hey!')]
|
||||||
|
)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -172,7 +173,7 @@ describe('dial', () => {
|
|||||||
pull(
|
pull(
|
||||||
tcp.dial(ma),
|
tcp.dial(ma),
|
||||||
pull.onEnd((err) => {
|
pull.onEnd((err) => {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -188,9 +189,13 @@ describe('dial', () => {
|
|||||||
pull.values(['hey']),
|
pull.values(['hey']),
|
||||||
tcp.dial(ma),
|
tcp.dial(ma),
|
||||||
pull.collect((err, values) => {
|
pull.collect((err, values) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
|
|
||||||
expect(values).to.be.eql([new Buffer('hey')])
|
expect(
|
||||||
|
values
|
||||||
|
).to.be.eql([
|
||||||
|
new Buffer('hey')
|
||||||
|
])
|
||||||
|
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
})
|
})
|
||||||
@ -253,7 +258,7 @@ describe('dial', () => {
|
|||||||
pull.values(['hey']),
|
pull.values(['hey']),
|
||||||
conn,
|
conn,
|
||||||
pull.collect((err, res) => {
|
pull.collect((err, res) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(res).to.be.eql([new Buffer('hey!')])
|
expect(res).to.be.eql([new Buffer('hey!')])
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
@ -302,9 +307,9 @@ describe('valid Connection', () => {
|
|||||||
let dialerObsAddrs
|
let dialerObsAddrs
|
||||||
|
|
||||||
const listener = tcp.createListener((conn) => {
|
const listener = tcp.createListener((conn) => {
|
||||||
expect(conn).to.exist()
|
expect(conn).to.exist
|
||||||
conn.getObservedAddrs((err, addrs) => {
|
conn.getObservedAddrs((err, addrs) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
dialerObsAddrs = addrs
|
dialerObsAddrs = addrs
|
||||||
pull(pull.empty(), conn)
|
pull(pull.empty(), conn)
|
||||||
})
|
})
|
||||||
@ -319,7 +324,7 @@ describe('valid Connection', () => {
|
|||||||
|
|
||||||
function endHandler () {
|
function endHandler () {
|
||||||
conn.getObservedAddrs((err, addrs) => {
|
conn.getObservedAddrs((err, addrs) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
pull(pull.empty(), conn)
|
pull(pull.empty(), conn)
|
||||||
closeAndAssert(listener, addrs)
|
closeAndAssert(listener, addrs)
|
||||||
})
|
})
|
||||||
@ -337,10 +342,10 @@ describe('valid Connection', () => {
|
|||||||
|
|
||||||
it('get Peer Info', (done) => {
|
it('get Peer Info', (done) => {
|
||||||
const listener = tcp.createListener((conn) => {
|
const listener = tcp.createListener((conn) => {
|
||||||
expect(conn).to.exist()
|
expect(conn).to.exist
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist
|
||||||
expect(peerInfo).to.not.exist()
|
expect(peerInfo).to.not.exist
|
||||||
pull(pull.empty(), conn)
|
pull(pull.empty(), conn)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -351,8 +356,8 @@ describe('valid Connection', () => {
|
|||||||
pull(conn, pull.onEnd(endHandler))
|
pull(conn, pull.onEnd(endHandler))
|
||||||
function endHandler () {
|
function endHandler () {
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist
|
||||||
expect(peerInfo).to.not.exist()
|
expect(peerInfo).to.not.exist
|
||||||
|
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
})
|
})
|
||||||
@ -362,10 +367,10 @@ describe('valid Connection', () => {
|
|||||||
|
|
||||||
it('set Peer Info', (done) => {
|
it('set Peer Info', (done) => {
|
||||||
const listener = tcp.createListener((conn) => {
|
const listener = tcp.createListener((conn) => {
|
||||||
expect(conn).to.exist()
|
expect(conn).to.exist
|
||||||
conn.setPeerInfo('batatas')
|
conn.setPeerInfo('batatas')
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(peerInfo).to.equal('batatas')
|
expect(peerInfo).to.equal('batatas')
|
||||||
pull(pull.empty(), conn)
|
pull(pull.empty(), conn)
|
||||||
})
|
})
|
||||||
@ -378,7 +383,7 @@ describe('valid Connection', () => {
|
|||||||
function endHandler () {
|
function endHandler () {
|
||||||
conn.setPeerInfo('arroz')
|
conn.setPeerInfo('arroz')
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(peerInfo).to.equal('arroz')
|
expect(peerInfo).to.equal('arroz')
|
||||||
|
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
@ -419,11 +424,11 @@ describe('Connection wrap', () => {
|
|||||||
pull.values(['hey']),
|
pull.values(['hey']),
|
||||||
connWrap,
|
connWrap,
|
||||||
pull.collect((err, chunks) => {
|
pull.collect((err, chunks) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(chunks).to.be.eql([new Buffer('hey')])
|
expect(chunks).to.be.eql([new Buffer('hey')])
|
||||||
|
|
||||||
connWrap.getPeerInfo((err, peerInfo) => {
|
connWrap.getPeerInfo((err, peerInfo) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(peerInfo).to.equal('peerInfo')
|
expect(peerInfo).to.equal('peerInfo')
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
@ -438,7 +443,7 @@ describe('Connection wrap', () => {
|
|||||||
pull.values(['hey']),
|
pull.values(['hey']),
|
||||||
connWrap,
|
connWrap,
|
||||||
pull.collect((err, chunks) => {
|
pull.collect((err, chunks) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(chunks).to.be.eql([new Buffer('hey')])
|
expect(chunks).to.be.eql([new Buffer('hey')])
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
@ -454,30 +459,23 @@ describe('Connection wrap', () => {
|
|||||||
callback(null, 'none')
|
callback(null, 'none')
|
||||||
}
|
}
|
||||||
conn.getPeerInfo((err, peerInfo) => {
|
conn.getPeerInfo((err, peerInfo) => {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist
|
||||||
})
|
})
|
||||||
connWrap.getPeerInfo((err, peerInfo) => {
|
connWrap.getPeerInfo((err, peerInfo) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(peerInfo).to.equal('none')
|
expect(peerInfo).to.equal('none')
|
||||||
})
|
})
|
||||||
pull(
|
pull(
|
||||||
pull.values(['hey']),
|
pull.values(['hey']),
|
||||||
connWrap,
|
connWrap,
|
||||||
pull.collect((err, chunks) => {
|
pull.collect((err, chunks) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(chunks).to.be.eql([new Buffer('hey')])
|
expect(chunks).to.be.eql([new Buffer('hey')])
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('dial error', (done) => {
|
|
||||||
tcp.dial(multiaddr('/ip4/999.0.0.1/tcp/1234'), (err) => {
|
|
||||||
expect(err).to.exist()
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('matryoshka wrap', (done) => {
|
it('matryoshka wrap', (done) => {
|
||||||
const conn = tcp.dial(ma)
|
const conn = tcp.dial(ma)
|
||||||
const connWrap1 = new Connection(conn)
|
const connWrap1 = new Connection(conn)
|
||||||
@ -491,10 +489,10 @@ describe('Connection wrap', () => {
|
|||||||
pull.values(['hey']),
|
pull.values(['hey']),
|
||||||
connWrap3,
|
connWrap3,
|
||||||
pull.collect((err, chunks) => {
|
pull.collect((err, chunks) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(chunks).to.be.eql([new Buffer('hey')])
|
expect(chunks).to.be.eql([new Buffer('hey')])
|
||||||
connWrap3.getPeerInfo((err, peerInfo) => {
|
connWrap3.getPeerInfo((err, peerInfo) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist
|
||||||
expect(peerInfo).to.equal('inner doll')
|
expect(peerInfo).to.equal('inner doll')
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user