Compare commits

...

54 Commits

Author SHA1 Message Date
730a477bd8 chore: release version v0.6.2 2016-06-01 02:15:22 +02:00
19b29fe40b chore: update contributors 2016-06-01 02:15:22 +02:00
073e14553b Merge pull request #14 from diasdavid/close-timeout
fix: destroy hanging connections after timeout
2016-06-01 01:09:40 +01:00
2ed01e8f5b fix: address cr 2016-05-31 21:49:12 +02:00
4a121696d1 fix: destroy hanging connections after timeout 2016-05-31 14:48:18 +02:00
a008d1db34 chore: release version v0.6.1 2016-05-29 08:56:30 +01:00
f50b9bafdd chore: update contributors 2016-05-29 08:56:30 +01:00
abd71d76e4 support for tcp addrs with ipfs in the end as well 2016-05-29 08:56:24 +01:00
5c150db80b chore: release version v0.6.0 2016-05-22 17:21:04 +01:00
6d15dd94bb chore: update contributors 2016-05-22 17:21:04 +01:00
019d60cf61 fix npm scripts 2016-05-22 17:20:58 +01:00
0c97a3b191 chore: release version vnull 2016-05-22 17:20:09 +01:00
4c04e2565b chore: update contributors 2016-05-22 17:20:09 +01:00
0bc2321649 update deps 2016-05-22 17:20:00 +01:00
6267bf84c6 chore: release version v0.5.2 2016-05-09 10:55:15 +01:00
4b82e33b39 chore: update contributors 2016-05-09 10:55:15 +01:00
8adced0f4b Merge pull request #13 from dignifiedquire/next
refactor: use aegir
2016-05-09 10:51:21 +01:00
3f4f163f40 refactor: use aegir 2016-05-09 11:14:40 +02:00
11c46ac995 Release v0.5.1. 2016-05-08 21:50:30 +01:00
749db165ce do not callback with error on no listeners to close 2016-05-08 21:50:03 +01:00
3781115f42 Release v0.5.0. 2016-04-25 02:21:55 +01:00
2070a5b0a0 Merge pull request #12 from diasdavid/fix/error-handling
switch from throw to send err to callback
2016-04-25 02:16:46 +01:00
c253a44f59 switch from throw to send err to callback 2016-04-25 02:13:49 +01:00
1e350a4dc7 Merge pull request #11 from noffle/words
Fixes README example & grammar.
2016-04-24 00:48:34 +01:00
69068f5da4 Fixes README example & grammar. 2016-04-22 14:14:07 -07:00
ce6edb05a2 Merge pull request #10 from noffle/master
Adds README, coverage, clean up, CircleCI.
2016-04-22 01:45:11 +01:00
73a79c9277 Enables ipv6 on CircleCI. 2016-04-21 16:42:40 -07:00
2e05826b30 s/::1/:: 2016-04-21 16:38:26 -07:00
e8a3c35cfb Adds example and API docs. 2016-04-21 16:33:57 -07:00
3c6bce9d28 Adds README stub. 2016-04-21 16:18:39 -07:00
d582fb5f61 Adds ipv6 tests. 2016-04-21 16:14:03 -07:00
b43f6ffb6f Makes callback optional on close. 2016-04-21 15:56:38 -07:00
c456326515 Improves test coverage. 2016-04-21 15:44:26 -07:00
6d1b729fcd Removes unused options param. 2016-04-21 15:36:39 -07:00
fdfdb7a5ad Adds circle.yml config. 2016-04-21 15:21:03 -07:00
3f87b5470c Merge pull request #8 from JGAntunes/patch-1
Fixing link in README.md
2016-04-03 13:36:38 -04:00
f4dafe55d1 Fixing link in README.md 2016-04-03 03:16:27 +01:00
541ec1fdda Release v0.4.0. 2016-03-14 17:02:22 +00:00
c3b5c132c0 Merge pull request #6 from diasdavid/feature/filter-addrs
add filter feature
2016-03-14 17:00:37 +00:00
4499bba514 add filter feature 2016-03-14 16:57:54 +00:00
9d778b56dd Release v0.3.0. 2016-03-10 10:35:04 +00:00
4769958b8f update deps 2016-03-10 10:34:51 +00:00
4d34757552 Merge pull request #5 from diasdavid/feature/observed-addrs
add getObservedAddrs feature to every returned conn
2016-03-10 10:26:57 +00:00
77600d3a8b add getObservedAddrs feature to every returned conn 2016-03-10 10:24:48 +00:00
3d17d40c2b Release v0.2.1. 2016-03-04 23:30:48 +00:00
8fac0f9dd6 Merge pull request #4 from diasdavid/feature/multiaddr-freshness
Multiaddr freshness
2016-03-04 23:29:07 +00:00
500ca9b699 done 2016-03-04 23:26:28 +00:00
471d1da5ce Release v0.2.0. 2016-03-04 19:15:21 +00:00
b548f9d381 Merge pull request #3 from diasdavid/update/extra-awesomeness
multiple listeners, more tests, understand multiaddr on the listen function
2016-03-04 19:11:02 +00:00
0b6361e27a multiple listeners, more tests, understand multiaddr on the listen function 2016-03-04 19:02:25 +00:00
506b4cbcfe Merge pull request #2 from RichardLitt/patch-1
Freejs => Freenode
2015-12-29 00:29:06 +01:00
e184baf6e4 Freejs => Freenode
See https://github.com/ipfs/community/issues/93
2015-12-28 17:14:01 -05:00
92721a5eda Release v0.1.2. 2015-10-29 07:27:50 +00:00
857702dc56 update 2015-10-29 07:27:39 +00:00
11 changed files with 536 additions and 41 deletions

3
.gitignore vendored
View File

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

29
.npmignore Normal file
View 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

View File

@ -1,11 +1,25 @@
sudo: false
language: node_js
node_js:
- "4.0"
- 4
- 5
- stable
# Make sure we have new NPM.
before_install:
- npm install -g npm
script:
- npm run lint
- 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

View File

@ -1,11 +1,96 @@
node-libp2p-tcp
js-libp2p-tcp
===============
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) ![Build Status](https://travis-ci.org/diasdavid/node-libp2p-tcp.svg?style=flat-square)](https://travis-ci.org/diasdavid/node-libp2p-tcp) ![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) [![Dependency Status](https://david-dm.org/diasdavid/node-libp2p-tcp.svg?style=flat-square)](https://david-dm.org/diasdavid/node-libp2p-tcp) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://travis-ci.org/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-tcp)
![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square)
[![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p-tcp)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://raw.githubusercontent.com/diasdavid/abstract-connection/master/img/badge.png)
![](https://raw.githubusercontent.com/diasdavid/abstract-transport/master/img/badge.png)
> 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/diasdavid/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()
tcp.createListener([mh1, mh2], function handler (socket) {
console.log('connection')
socket.end('bye')
}, 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
```js
const Tcp = require('libp2p-tcp')
```
### var tcp = new Tcp()
Creates a new TCP object. This does nothing on its own, but provides access to
`dial` and `createListener`.
### tcp.createListener(multiaddrs, handler, ready)
Creates TCP servers that listen on the addresses described in the array
`multiaddrs`. Each connection will call `handler` with a connection stream.
`ready` is called once all servers are listening.
### tcp.dial(multiaddr, options={})
Connects to the multiaddress `multiaddr` using TCP, returning the socket stream.
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
MIT

12
circle.yml Normal file
View 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

View File

@ -1,15 +1,26 @@
{
"name": "libp2p-tcp",
"version": "0.1.1",
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the abstract-connection interface",
"main": "src/index.js",
"version": "0.6.2",
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"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": {
"type": "git",
"url": "https://github.com/diasdavid/node-libp2p-tcp.git"
"url": "https://github.com/diasdavid/js-libp2p-tcp.git"
},
"keywords": [
"IPFS"
@ -17,17 +28,29 @@
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/node-libp2p-tcp/issues"
"url": "https://github.com/diasdavid/js-libp2p-tcp/issues"
},
"homepage": "https://github.com/diasdavid/node-libp2p-tcp",
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
"devDependencies": {
"abstract-connection": "0.0.1",
"abstract-transport": "^0.1.0",
"pre-commit": "^1.1.1",
"standard": "^5.2.2",
"tape": "^4.2.0"
"aegir": "^3.0.4",
"chai": "^3.5.0",
"interface-connection": "0.0.3",
"interface-transport": "^0.1.1",
"pre-commit": "^1.1.2",
"tape": "^4.5.1"
},
"dependencies": {
"multiaddr": "^1.0.0"
}
}
"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>"
]
}

View File

@ -1,10 +1,148 @@
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')
exports.dial = function (multiaddr, options) {
options.ready = options.ready || function noop () {}
return tcp.connect(multiaddr.toOptions(), options.ready)
exports = module.exports = TCP
const IPFS_CODE = 421
const CLOSE_TIMEOUT = 300
function TCP () {
if (!(this instanceof TCP)) {
return new TCP()
}
const listeners = []
this.dial = function (multiaddr, options) {
if (!options) {
options = {}
}
options.ready = options.ready || function noop () {}
const conn = tcp.connect(multiaddr.toOptions(), options.ready)
conn.getObservedAddrs = () => {
return [multiaddr]
}
return conn
}
this.createListener = (multiaddrs, handler, callback) => {
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}
const freshMultiaddrs = []
parallel(multiaddrs.map((m) => (cb) => {
let ipfsHashId
if (contains(m.protoNames(), 'ipfs')) {
ipfsHashId = m.stringTuples().filter((tuple) => {
if (tuple[0] === IPFS_CODE) {
return true
}
})[0][1]
m = m.decapsulate('ipfs')
}
const listener = tcp.createServer((conn) => {
conn.getObservedAddrs = () => {
return [getMultiaddr(conn)]
}
handler(conn)
})
listener.__connections = {}
listener.on('connection', (conn) => {
const key = `${conn.remoteAddress}:${conn.remotePort}`
listener.__connections[key] = conn
conn.on('close', () => {
delete listener.__connections[key]
})
})
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)
}), (err) => {
callback(err, freshMultiaddrs)
})
}
this.close = (callback) => {
log('closing')
if (listeners.length === 0) {
log('Called close with no active listeners')
return callback()
}
parallel(listeners.map((listener) => (cb) => {
setTimeout(() => {
Object.keys(listener.__connections).forEach((key) => {
log('destroying %s', key)
listener.__connections[key].destroy()
})
}, CLOSE_TIMEOUT)
listener.close(cb)
}), callback)
}
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 (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
}

182
test/index.spec.js Normal file
View File

@ -0,0 +1,182 @@
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const TCPlibp2p = require('../src')
const net = require('net')
const multiaddr = require('multiaddr')
describe('libp2p-tcp', function () {
this.timeout(10000)
var tcp
it('create', (done) => {
tcp = new TCPlibp2p()
expect(tcp).to.exist
done()
})
it('create without new', (done) => {
tcp = TCPlibp2p()
expect(tcp).to.exist
done()
})
it('close /wo listeners', (done) => {
tcp = new TCPlibp2p()
expect(tcp).to.exist
expect(function () { tcp.close() }).to.throw(Error)
done()
})
it('listen', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
tcp.createListener(mh, (socket) => {
expect(socket).to.exist
socket.end()
tcp.close(() => {
done()
})
}, (err, freshMultiaddrs) => {
expect(err).to.not.exist
expect(mh).to.deep.equal(freshMultiaddrs[0])
const socket = net.connect({ host: '127.0.0.1', port: 9090 })
socket.end()
})
})
it('listen on addr with /ipfs/QmHASH', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/14090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
tcp.createListener(mh, (socket) => {
expect(socket).to.exist
socket.end()
tcp.close(() => {
done()
})
}, (err, freshMultiaddrs) => {
expect(err).to.not.exist
expect(mh).to.deep.equal(freshMultiaddrs[0])
const socket = net.connect({ host: '127.0.0.1', port: 14090 })
socket.end()
})
})
it('dial', (done) => {
const server = net.createServer((socket) => {
expect(socket).to.exist
socket.end()
server.close(done)
})
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()
})
})
})
it('get observed addrs', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
var dialerObsAddrs
var listenerObsAddrs
tcp.createListener(mh, (conn) => {
expect(conn).to.exist
dialerObsAddrs = conn.getObservedAddrs()
conn.end()
}, () => {
const conn = tcp.dial(mh)
conn.on('end', () => {
listenerObsAddrs = conn.getObservedAddrs()
conn.end()
tcp.close(() => {
expect(listenerObsAddrs[0]).to.deep.equal(mh)
expect(dialerObsAddrs.length).to.equal(1)
done()
})
})
})
})
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)
done()
})
it('filter a valid 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()
})
it('destroys after timeout', (done) => {
const server = new TCPlibp2p()
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
server.createListener(mh, (conn) => {
const i = setInterval(() => {
conn.read()
conn.write('hi\n')
}, 100)
i.unref()
}, () => {
let connected = 0
const connectHandler = () => {
connected++
if (connected === 10) {
setTimeout(() => {
server.close(done)
}, 1)
}
}
const errorHandler = () => {}
for (let i = 0; i < 10; i++) {
const client = net.connect(9090)
client.on('connect', connectHandler)
// just ignore the resets
client.on('error', errorHandler)
}
})
})
})

View 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)
})
})

View File

@ -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)

View File