mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-02 00:41:34 +00:00
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
541ec1fdda | |||
c3b5c132c0 | |||
4499bba514 | |||
9d778b56dd | |||
4769958b8f | |||
4d34757552 | |||
77600d3a8b | |||
3d17d40c2b | |||
8fac0f9dd6 | |||
500ca9b699 | |||
471d1da5ce | |||
b548f9d381 | |||
0b6361e27a | |||
506b4cbcfe | |||
e184baf6e4 | |||
92721a5eda | |||
857702dc56 |
11
README.md
11
README.md
@ -1,11 +1,16 @@
|
|||||||
node-libp2p-tcp
|
js-libp2p-tcp
|
||||||
===============
|
===============
|
||||||
|
|
||||||
[](http://ipn.io) [[](http://webchat.freenode.net/?channels=%23ipfs) ](https://travis-ci.org/diasdavid/node-libp2p-tcp)  [](https://david-dm.org/diasdavid/node-libp2p-tcp) [](https://github.com/feross/standard)
|
[](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)
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
> 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]() 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.
|
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.
|
||||||
|
37
package.json
37
package.json
@ -1,15 +1,23 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-tcp",
|
"name": "libp2p-tcp",
|
||||||
"version": "0.1.1",
|
"version": "0.4.0",
|
||||||
"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": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node tests/test-ac.js"
|
"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"
|
||||||
},
|
},
|
||||||
"pre-commit": [],
|
"pre-commit": [
|
||||||
|
"lint",
|
||||||
|
"test"
|
||||||
|
],
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/diasdavid/node-libp2p-tcp.git"
|
"url": "https://github.com/diasdavid/js-libp2p-tcp.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
@ -17,17 +25,22 @@
|
|||||||
"author": "David Dias <daviddias@ipfs.io>",
|
"author": "David Dias <daviddias@ipfs.io>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"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": {
|
"devDependencies": {
|
||||||
"abstract-connection": "0.0.1",
|
"chai": "^3.5.0",
|
||||||
"abstract-transport": "^0.1.0",
|
"interface-connection": "0.0.3",
|
||||||
"pre-commit": "^1.1.1",
|
"interface-transport": "^0.1.1",
|
||||||
"standard": "^5.2.2",
|
"istanbul": "^0.4.2",
|
||||||
|
"mocha": "^2.4.5",
|
||||||
|
"pre-commit": "^1.1.2",
|
||||||
|
"standard": "^6.0.7",
|
||||||
"tape": "^4.2.0"
|
"tape": "^4.2.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"multiaddr": "^1.0.0"
|
"ip-address": "^5.8.0",
|
||||||
|
"mafmt": "^1.0.1",
|
||||||
|
"multiaddr": "^1.1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
112
src/index.js
112
src/index.js
@ -1,10 +1,110 @@
|
|||||||
var tcp = require('net')
|
// 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')
|
||||||
|
|
||||||
exports = module.exports
|
exports = module.exports = TCP
|
||||||
|
|
||||||
exports.dial = function (multiaddr, options) {
|
function TCP () {
|
||||||
options.ready = options.ready || function noop () {}
|
if (!(this instanceof TCP)) {
|
||||||
return tcp.connect(multiaddr.toOptions(), options.ready)
|
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, options, handler, callback) => {
|
||||||
|
if (typeof options === 'function') {
|
||||||
|
callback = handler
|
||||||
|
handler = options
|
||||||
|
options = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(multiaddrs)) {
|
||||||
|
multiaddrs = [multiaddrs]
|
||||||
|
}
|
||||||
|
|
||||||
|
var count = 0
|
||||||
|
const freshMultiaddrs = []
|
||||||
|
|
||||||
|
multiaddrs.forEach((m) => {
|
||||||
|
const listener = tcp.createServer((conn) => {
|
||||||
|
conn.getObservedAddrs = () => {
|
||||||
|
return [getMultiaddr(conn)]
|
||||||
|
}
|
||||||
|
handler(conn)
|
||||||
|
})
|
||||||
|
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)
|
||||||
|
freshMultiaddrs.push(m)
|
||||||
|
}
|
||||||
|
if (address.family === 'IPv6') {
|
||||||
|
freshMultiaddrs.push(multiaddr('/ip6/' + address.address + '/tcp/' + address.port))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (++count === multiaddrs.length) {
|
||||||
|
callback(null, freshMultiaddrs)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
listeners.push(listener)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
this.close = (callback) => {
|
||||||
|
if (listeners.length === 0) {
|
||||||
|
throw new Error('there are no listeners')
|
||||||
|
}
|
||||||
|
var count = 0
|
||||||
|
listeners.forEach((listener) => {
|
||||||
|
listener.close(() => {
|
||||||
|
if (++count === listeners.length) {
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
this.filter = (multiaddrs) => {
|
||||||
|
if (!Array.isArray(multiaddrs)) {
|
||||||
|
multiaddrs = [multiaddrs]
|
||||||
|
}
|
||||||
|
return multiaddrs.filter((ma) => {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
92
tests/libp2p-tcp-test.js
Normal file
92
tests/libp2p-tcp-test.js
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
|
||||||
|
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('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()
|
||||||
|
})
|
||||||
|
}, () => {
|
||||||
|
const socket = net.connect({ host: '127.0.0.1', port: 9090 })
|
||||||
|
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 tcp = new TCPlibp2p()
|
||||||
|
|
||||||
|
tcp.createListener([mh1, mh2], (socket) => {}, () => {
|
||||||
|
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 valid = tcp.filter([mh1, mh2, mh3])
|
||||||
|
expect(valid.length).to.equal(1)
|
||||||
|
expect(valid[0]).to.deep.equal(mh1)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it.skip('listen on IPv6', (done) => {})
|
||||||
|
})
|
@ -1,5 +1,5 @@
|
|||||||
var tape = require('tape')
|
var tape = require('tape')
|
||||||
var tests = require('abstract-transport/tests')
|
var tests = require('interface-transport/tests')
|
||||||
var conn = require('../src')
|
var conn = require('../src')
|
||||||
|
|
||||||
var common = {
|
var common = {
|
Reference in New Issue
Block a user