mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-02 05:21:51 +00:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
6267bf84c6 | |||
4b82e33b39 | |||
8adced0f4b | |||
3f4f163f40 | |||
11c46ac995 | |||
749db165ce | |||
3781115f42 | |||
2070a5b0a0 | |||
c253a44f59 | |||
1e350a4dc7 | |||
69068f5da4 | |||
ce6edb05a2 | |||
73a79c9277 | |||
2e05826b30 | |||
e8a3c35cfb | |||
3c6bce9d28 | |||
d582fb5f61 | |||
b43f6ffb6f | |||
c456326515 | |||
6d1b729fcd | |||
fdfdb7a5ad | |||
3f87b5470c | |||
f4dafe55d1 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -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
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
|
||||
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
|
||||
|
84
README.md
84
README.md
@ -11,6 +11,86 @@ js-libp2p-tcp
|
||||

|
||||

|
||||
|
||||
> Node.js implementation of the TCP module that libp2p uses, which implements the [interface-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
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
|
39
package.json
39
package.json
@ -1,15 +1,18 @@
|
||||
{
|
||||
"name": "libp2p-tcp",
|
||||
"version": "0.4.0",
|
||||
"version": "0.5.2",
|
||||
"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": {
|
||||
"test:compliance:connection": "node tests/connection.js",
|
||||
"test:compliance:transport": "node tests/transport.js",
|
||||
"test:specific": "mocha tests/*-test.js",
|
||||
"test": "npm run test:specific",
|
||||
"test-2": "npm run test:specific && npm run test:compliance:transport && npm run test:compliance:connection",
|
||||
"lint": "standard"
|
||||
"lint": "aegir-lint",
|
||||
"build": "aegir-build --env node",
|
||||
"test": "aegir-test --env node",
|
||||
"release": "aegir-release --env node",
|
||||
"release-minor": "aegir-release --type minor",
|
||||
"release-major": "aegir-release --type major",
|
||||
"coverage": "aegir-coverage",
|
||||
"coverage-publish": "aegir-coverage publish"
|
||||
},
|
||||
"pre-commit": [
|
||||
"lint",
|
||||
@ -29,18 +32,24 @@
|
||||
},
|
||||
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
|
||||
"devDependencies": {
|
||||
"aegir": "^3.0.1",
|
||||
"chai": "^3.5.0",
|
||||
"interface-connection": "0.0.3",
|
||||
"interface-transport": "^0.1.1",
|
||||
"istanbul": "^0.4.2",
|
||||
"mocha": "^2.4.5",
|
||||
"pre-commit": "^1.1.2",
|
||||
"standard": "^6.0.7",
|
||||
"tape": "^4.2.0"
|
||||
"tape": "^4.5.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"ip-address": "^5.8.0",
|
||||
"mafmt": "^1.0.1",
|
||||
"multiaddr": "^1.1.1"
|
||||
}
|
||||
}
|
||||
"multiaddr": "^1.1.1",
|
||||
"run-parallel": "^1.1.6"
|
||||
},
|
||||
"contributors": [
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"João Antunes <j.goncalo.antunes@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"Stephen Whitmore <stephen.whitmore@gmail.com>"
|
||||
]
|
||||
}
|
40
src/index.js
40
src/index.js
@ -1,9 +1,12 @@
|
||||
// const debug = require('debug')
|
||||
// const log = debug('libp2p:tcp')
|
||||
'use strict'
|
||||
|
||||
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')
|
||||
|
||||
exports = module.exports = TCP
|
||||
|
||||
@ -26,21 +29,14 @@ function TCP () {
|
||||
return conn
|
||||
}
|
||||
|
||||
this.createListener = (multiaddrs, options, handler, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = handler
|
||||
handler = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
this.createListener = (multiaddrs, handler, callback) => {
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
var count = 0
|
||||
const freshMultiaddrs = []
|
||||
|
||||
multiaddrs.forEach((m) => {
|
||||
parallel(multiaddrs.map((m) => (cb) => {
|
||||
const listener = tcp.createServer((conn) => {
|
||||
conn.getObservedAddrs = () => {
|
||||
return [getMultiaddr(conn)]
|
||||
@ -55,30 +51,28 @@ function TCP () {
|
||||
m = m.encapsulate('/tcp/' + address.port)
|
||||
freshMultiaddrs.push(m)
|
||||
}
|
||||
|
||||
if (address.family === 'IPv6') {
|
||||
freshMultiaddrs.push(multiaddr('/ip6/' + address.address + '/tcp/' + address.port))
|
||||
}
|
||||
|
||||
if (++count === multiaddrs.length) {
|
||||
callback(null, freshMultiaddrs)
|
||||
}
|
||||
cb()
|
||||
})
|
||||
listeners.push(listener)
|
||||
}), (err) => {
|
||||
callback(err, freshMultiaddrs)
|
||||
})
|
||||
}
|
||||
|
||||
this.close = (callback) => {
|
||||
if (listeners.length === 0) {
|
||||
throw new Error('there are no listeners')
|
||||
log('Called close with no active listeners')
|
||||
return callback()
|
||||
}
|
||||
var count = 0
|
||||
listeners.forEach((listener) => {
|
||||
listener.close(() => {
|
||||
if (++count === listeners.length) {
|
||||
callback()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
parallel(listeners.map((listener) => {
|
||||
return (cb) => listener.close(cb)
|
||||
}), callback)
|
||||
}
|
||||
|
||||
this.filter = (multiaddrs) => {
|
||||
|
@ -1,4 +1,5 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const expect = require('chai').expect
|
||||
const TCPlibp2p = require('../src')
|
||||
@ -15,6 +16,19 @@ describe('libp2p-tcp', function () {
|
||||
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) => {
|
||||
@ -46,13 +60,32 @@ describe('libp2p-tcp', function () {
|
||||
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], (socket) => {}, () => {
|
||||
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
|
||||
@ -88,5 +121,12 @@ describe('libp2p-tcp', function () {
|
||||
done()
|
||||
})
|
||||
|
||||
it.skip('listen on IPv6', (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()
|
||||
})
|
||||
})
|
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)
|
||||
})
|
||||
})
|
@ -1,14 +0,0 @@
|
||||
var tape = require('tape')
|
||||
var tests = require('interface-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