Compare commits

..

1 Commits

Author SHA1 Message Date
ce4974b01c docs(api): first pass 2016-12-08 12:27:08 +01:00
6 changed files with 134 additions and 59 deletions

2
.gitignore vendored
View File

@ -25,3 +25,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
docs

View File

@ -26,4 +26,5 @@ build/Release
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
test
test
docs

View File

@ -1,14 +1,15 @@
{
"name": "libp2p-tcp",
"version": "0.9.4",
"version": "0.9.1",
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
"main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"test": "aegir-test --env node",
"release": "aegir-release --env no-build",
"release-minor": "aegir-release --type minor --env no-build",
"release-major": "aegir-release --type major --env no-build",
"docs": "aegir-docs",
"release": "aegir-release --env no-build --docs",
"release-minor": "aegir-release --type minor --env no-build --docs",
"release-major": "aegir-release --type major --env no-build --docs",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
@ -33,31 +34,28 @@
"node": ">=4.0.0"
},
"devDependencies": {
"aegir": "^11.0.0",
"aegir": "^9.2.0",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"interface-transport": "~0.3.5",
"interface-transport": "^0.3.3",
"lodash.isfunction": "^3.0.8",
"pre-commit": "^1.2.2",
"pre-commit": "^1.1.3",
"pull-stream": "^3.5.0"
},
"dependencies": {
"interface-connection": "~0.3.2",
"ip-address": "^5.8.6",
"interface-connection": "0.3.0",
"ip-address": "^5.8.2",
"lodash.includes": "^4.3.0",
"lodash.isfunction": "^3.0.8",
"mafmt": "^2.1.6",
"multiaddr": "^2.2.2",
"mafmt": "^2.1.2",
"multiaddr": "^2.1.1",
"stream-to-pull-stream": "^1.7.2"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Greenkeeper <support@greenkeeper.io>",
"João Antunes <j.goncalo.antunes@gmail.com>",
"Prashanth Chandra <coolshanth94@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>"
]
}
}

View File

@ -11,21 +11,32 @@ const log = debug('libp2p:tcp:dial')
const createListener = require('./listener')
module.exports = class TCP {
dial (ma, options, cb) {
/**
*
*/
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) {
if (isFunction(options)) {
cb = options
callback = options
options = {}
}
if (!cb) {
cb = () => {}
if (!callback) {
callback = () => {}
}
const cOpts = ma.toOptions()
log('Connecting to %s %s', cOpts.port, cOpts.host)
const rawSocket = net.connect(cOpts, cb)
const rawSocket = net.connect(cOpts, callback)
rawSocket.once('timeout', () => {
log('timeout')
@ -43,6 +54,13 @@ module.exports = class TCP {
return conn
}
/**
* Listen for incoming `TCP` connetions.
*
* @param {Object} [options={}]
* @param {function(Connection)} [handler] - Called with newly incomin connections.
* @returns {Listener}
*/
createListener (options, handler) {
if (isFunction(options)) {
handler = options
@ -54,6 +72,13 @@ module.exports = class TCP {
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) {
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
@ -66,3 +91,5 @@ module.exports = class TCP {
})
}
}
module.exports = TCP

View File

@ -15,7 +15,49 @@ const getMultiaddr = require('./get-multiaddr')
const IPFS_CODE = 421
const CLOSE_TIMEOUT = 2000
/**
* 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) => {
/**
* @alias Listener
* @type {Eventemitter}
* @fires Listener#listening
* @fires Listener#close
* @fires Listener#connection
* @fires Listener#error
*/
const listener = new EventEmitter()
const server = net.createServer((socket) => {

View File

@ -2,10 +2,7 @@
'use strict'
const pull = require('pull-stream')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const expect = require('chai').expect
const TCP = require('../src')
const net = require('net')
const multiaddr = require('multiaddr')
@ -14,7 +11,7 @@ const Connection = require('interface-connection').Connection
describe('instantiate the transport', () => {
it('create', () => {
const tcp = new TCP()
expect(tcp).to.exist()
expect(tcp).to.exist
})
})
@ -74,7 +71,7 @@ describe('listen', () => {
const listener = tcp.createListener((conn) => {})
listener.listen(mh, () => {
listener.getAddrs((err, multiaddrs) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(multiaddrs.length).to.equal(1)
expect(multiaddrs[0]).to.deep.equal(mh)
listener.close(done)
@ -87,7 +84,7 @@ describe('listen', () => {
const listener = tcp.createListener((conn) => {})
listener.listen(mh, () => {
listener.getAddrs((err, multiaddrs) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(multiaddrs.length).to.equal(1)
listener.close(done)
})
@ -99,7 +96,7 @@ describe('listen', () => {
const listener = tcp.createListener((conn) => {})
listener.listen(mh, () => {
listener.getAddrs((err, multiaddrs) => {
expect(err).to.not.exist()
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)
@ -112,7 +109,7 @@ describe('listen', () => {
const listener = tcp.createListener((conn) => {})
listener.listen(mh, () => {
listener.getAddrs((err, multiaddrs) => {
expect(err).to.not.exist()
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)
@ -125,7 +122,7 @@ describe('listen', () => {
const listener = tcp.createListener((conn) => {})
listener.listen(mh, () => {
listener.getAddrs((err, multiaddrs) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(multiaddrs.length).to.equal(1)
expect(multiaddrs[0]).to.deep.equal(mh)
listener.close(done)
@ -160,8 +157,12 @@ describe('dial', () => {
pull.values(['hey']),
tcp.dial(ma),
pull.collect((err, values) => {
expect(err).to.not.exist()
expect(values).to.eql([new Buffer('hey!')])
expect(err).to.not.exist
expect(
values
).to.be.eql(
[new Buffer('hey!')]
)
done()
})
)
@ -172,7 +173,7 @@ describe('dial', () => {
pull(
tcp.dial(ma),
pull.onEnd((err) => {
expect(err).to.exist()
expect(err).to.exist
done()
})
)
@ -188,9 +189,13 @@ describe('dial', () => {
pull.values(['hey']),
tcp.dial(ma),
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)
})
@ -253,7 +258,7 @@ describe('dial', () => {
pull.values(['hey']),
conn,
pull.collect((err, res) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(res).to.be.eql([new Buffer('hey!')])
done()
})
@ -302,9 +307,9 @@ describe('valid Connection', () => {
let dialerObsAddrs
const listener = tcp.createListener((conn) => {
expect(conn).to.exist()
expect(conn).to.exist
conn.getObservedAddrs((err, addrs) => {
expect(err).to.not.exist()
expect(err).to.not.exist
dialerObsAddrs = addrs
pull(pull.empty(), conn)
})
@ -319,7 +324,7 @@ describe('valid Connection', () => {
function endHandler () {
conn.getObservedAddrs((err, addrs) => {
expect(err).to.not.exist()
expect(err).to.not.exist
pull(pull.empty(), conn)
closeAndAssert(listener, addrs)
})
@ -337,10 +342,10 @@ describe('valid Connection', () => {
it('get Peer Info', (done) => {
const listener = tcp.createListener((conn) => {
expect(conn).to.exist()
expect(conn).to.exist
conn.getPeerInfo((err, peerInfo) => {
expect(err).to.exist()
expect(peerInfo).to.not.exist()
expect(err).to.exist
expect(peerInfo).to.not.exist
pull(pull.empty(), conn)
})
})
@ -351,8 +356,8 @@ describe('valid Connection', () => {
pull(conn, pull.onEnd(endHandler))
function endHandler () {
conn.getPeerInfo((err, peerInfo) => {
expect(err).to.exist()
expect(peerInfo).to.not.exist()
expect(err).to.exist
expect(peerInfo).to.not.exist
listener.close(done)
})
@ -362,10 +367,10 @@ describe('valid Connection', () => {
it('set Peer Info', (done) => {
const listener = tcp.createListener((conn) => {
expect(conn).to.exist()
expect(conn).to.exist
conn.setPeerInfo('batatas')
conn.getPeerInfo((err, peerInfo) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(peerInfo).to.equal('batatas')
pull(pull.empty(), conn)
})
@ -378,7 +383,7 @@ describe('valid Connection', () => {
function endHandler () {
conn.setPeerInfo('arroz')
conn.getPeerInfo((err, peerInfo) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(peerInfo).to.equal('arroz')
listener.close(done)
@ -419,11 +424,11 @@ describe('Connection wrap', () => {
pull.values(['hey']),
connWrap,
pull.collect((err, chunks) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(chunks).to.be.eql([new Buffer('hey')])
connWrap.getPeerInfo((err, peerInfo) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(peerInfo).to.equal('peerInfo')
done()
})
@ -438,7 +443,7 @@ describe('Connection wrap', () => {
pull.values(['hey']),
connWrap,
pull.collect((err, chunks) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(chunks).to.be.eql([new Buffer('hey')])
done()
})
@ -454,17 +459,17 @@ describe('Connection wrap', () => {
callback(null, 'none')
}
conn.getPeerInfo((err, peerInfo) => {
expect(err).to.exist()
expect(err).to.exist
})
connWrap.getPeerInfo((err, peerInfo) => {
expect(err).to.not.exist()
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(err).to.not.exist
expect(chunks).to.be.eql([new Buffer('hey')])
done()
})
@ -484,10 +489,10 @@ describe('Connection wrap', () => {
pull.values(['hey']),
connWrap3,
pull.collect((err, chunks) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(chunks).to.be.eql([new Buffer('hey')])
connWrap3.getPeerInfo((err, peerInfo) => {
expect(err).to.not.exist()
expect(err).to.not.exist
expect(peerInfo).to.equal('inner doll')
done()
})