Compare commits

...

3 Commits

Author SHA1 Message Date
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
3 changed files with 49 additions and 5 deletions

View File

@ -1,6 +1,6 @@
{
"name": "libp2p-tcp",
"version": "0.6.0",
"version": "0.6.1",
"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",
@ -41,6 +41,7 @@
},
"dependencies": {
"ip-address": "^5.8.0",
"lodash.contains": "^2.4.3",
"mafmt": "^2.1.0",
"multiaddr": "^2.0.2",
"run-parallel": "^1.1.6"

View File

@ -7,9 +7,12 @@ 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 = module.exports = TCP
const IPFS_CODE = 421
function TCP () {
if (!(this instanceof TCP)) {
return new TCP()
@ -37,6 +40,16 @@ function TCP () {
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)]
@ -49,11 +62,19 @@ function TCP () {
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') {
freshMultiaddrs.push(multiaddr('/ip6/' + address.address + '/tcp/' + address.port))
let mh = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
if (ipfsHashId) {
mh = mh.encapsulate('/ipfs/' + ipfsHashId)
}
freshMultiaddrs.push(mh)
}
cb()
@ -80,6 +101,9 @@ function TCP () {
multiaddrs = [multiaddrs]
}
return multiaddrs.filter((ma) => {
if (contains(ma.protoNames(), 'ipfs')) {
ma = ma.decapsulate('ipfs')
}
return mafmt.TCP.matches(ma)
})
}

View File

@ -37,12 +37,30 @@ describe('libp2p-tcp', function () {
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
@ -114,9 +132,10 @@ describe('libp2p-tcp', function () {
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])
expect(valid.length).to.equal(1)
const valid = tcp.filter([mh1, mh2, mh3, mh4])
expect(valid.length).to.equal(2)
expect(valid[0]).to.deep.equal(mh1)
done()
})