mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-02 04:41:45 +00:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
7572279838 | |||
181ce4eef2 | |||
f8075be92a | |||
a54bb83657 | |||
ef45cc9a34 | |||
ce5f023508 | |||
2f9b1fe7fe | |||
46672b669f | |||
31f69ec3c2 | |||
afed19439a | |||
4b78faae3f | |||
e204517a51 |
19
.gitignore
vendored
19
.gitignore
vendored
@ -1,7 +1,14 @@
|
|||||||
|
**/node_modules/
|
||||||
|
**/*.log
|
||||||
|
test/repo-tests*
|
||||||
|
**/bundle.js
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
*.log
|
*.log
|
||||||
|
|
||||||
|
coverage
|
||||||
|
|
||||||
# Runtime data
|
# Runtime data
|
||||||
pids
|
pids
|
||||||
*.pid
|
*.pid
|
||||||
@ -19,9 +26,17 @@ coverage
|
|||||||
# node-waf configuration
|
# node-waf configuration
|
||||||
.lock-wscript
|
.lock-wscript
|
||||||
|
|
||||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
build
|
||||||
build/Release
|
|
||||||
|
|
||||||
# Dependency directory
|
# Dependency directory
|
||||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
|
lib
|
||||||
|
dist
|
||||||
|
test/test-data/go-ipfs-repo/LOCK
|
||||||
|
test/test-data/go-ipfs-repo/LOG
|
||||||
|
test/test-data/go-ipfs-repo/LOG.old
|
||||||
|
|
||||||
|
# while testing npm5
|
||||||
|
package-lock.json
|
||||||
|
15
.travis.yml
15
.travis.yml
@ -1,22 +1,20 @@
|
|||||||
sudo: false
|
sudo: false
|
||||||
language: node_js
|
language: node_js
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- node_js: 4
|
|
||||||
env: CXX=g++-4.8
|
|
||||||
- node_js: 6
|
- node_js: 6
|
||||||
env: CXX=g++-4.8
|
env: CXX=g++-4.8
|
||||||
- node_js: stable
|
- node_js: 8
|
||||||
env: CXX=g++-4.8
|
env: CXX=g++-4.8
|
||||||
|
# - node_js: stable
|
||||||
# Make sure we have new NPM.
|
# env: CXX=g++-4.8
|
||||||
before_install:
|
|
||||||
- npm install -g npm
|
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- npm run lint
|
- npm run lint
|
||||||
- npm test
|
- npm run test
|
||||||
- npm run coverage
|
- npm run coverage
|
||||||
|
- make test
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- export DISPLAY=:99.0
|
- export DISPLAY=:99.0
|
||||||
@ -26,6 +24,7 @@ after_success:
|
|||||||
- npm run coverage-publish
|
- npm run coverage-publish
|
||||||
|
|
||||||
addons:
|
addons:
|
||||||
|
firefox: 'latest'
|
||||||
apt:
|
apt:
|
||||||
sources:
|
sources:
|
||||||
- ubuntu-toolchain-r-test
|
- ubuntu-toolchain-r-test
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
[](https://david-dm.org/libp2p/js-libp2p-tcp)
|
[](https://david-dm.org/libp2p/js-libp2p-tcp)
|
||||||
[](https://github.com/feross/standard)
|
[](https://github.com/feross/standard)
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|
@ -5,8 +5,10 @@ machine:
|
|||||||
dependencies:
|
dependencies:
|
||||||
pre:
|
pre:
|
||||||
- google-chrome --version
|
- google-chrome --version
|
||||||
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
|
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
|
||||||
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
|
- sudo dpkg -i google-chrome.deb || true
|
||||||
- sudo apt-get update
|
- sudo apt-get update
|
||||||
- sudo apt-get --only-upgrade install google-chrome-stable
|
- sudo apt-get install -f
|
||||||
|
- sudo apt-get install --only-upgrade lsb-base
|
||||||
|
- sudo dpkg -i google-chrome.deb
|
||||||
- google-chrome --version
|
- google-chrome --version
|
||||||
|
19
package.json
19
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-tcp",
|
"name": "libp2p-tcp",
|
||||||
"version": "0.10.0",
|
"version": "0.11.0",
|
||||||
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
|
"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": {
|
||||||
@ -30,24 +30,25 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/libp2p/js-libp2p-tcp",
|
"homepage": "https://github.com/libp2p/js-libp2p-tcp",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=4.0.0"
|
"node": ">=6.0.0",
|
||||||
|
"npm": ">=3.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^11.0.1",
|
"aegir": "^11.0.2",
|
||||||
"chai": "^3.5.0",
|
"chai": "^4.1.2",
|
||||||
"dirty-chai": "^1.2.2",
|
"dirty-chai": "^2.0.1",
|
||||||
"interface-transport": "~0.3.5",
|
"interface-transport": "~0.3.5",
|
||||||
"lodash.isfunction": "^3.0.8",
|
"lodash.isfunction": "^3.0.8",
|
||||||
"pre-commit": "^1.2.2",
|
"pre-commit": "^1.2.2",
|
||||||
"pull-stream": "^3.5.0"
|
"pull-stream": "^3.6.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"interface-connection": "~0.3.2",
|
"interface-connection": "~0.3.2",
|
||||||
"ip-address": "^5.8.6",
|
"ip-address": "^5.8.8",
|
||||||
"lodash.includes": "^4.3.0",
|
"lodash.includes": "^4.3.0",
|
||||||
"lodash.isfunction": "^3.0.8",
|
"lodash.isfunction": "^3.0.8",
|
||||||
"mafmt": "^2.1.7",
|
"mafmt": "^3.0.0",
|
||||||
"multiaddr": "^2.2.3",
|
"multiaddr": "^3.0.0",
|
||||||
"once": "^1.4.0",
|
"once": "^1.4.0",
|
||||||
"stream-to-pull-stream": "^1.7.2"
|
"stream-to-pull-stream": "^1.7.2"
|
||||||
},
|
},
|
||||||
|
@ -4,7 +4,7 @@ const multiaddr = require('multiaddr')
|
|||||||
const Address6 = require('ip-address').Address6
|
const Address6 = require('ip-address').Address6
|
||||||
|
|
||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
var mh
|
let mh
|
||||||
|
|
||||||
if (socket.remoteFamily === 'IPv6') {
|
if (socket.remoteFamily === 'IPv6') {
|
||||||
var addr = new Address6(socket.remoteAddress)
|
var addr = new Address6(socket.remoteAddress)
|
||||||
|
27
src/index.js
27
src/index.js
@ -12,40 +12,41 @@ const log = debug('libp2p:tcp:dial')
|
|||||||
|
|
||||||
const createListener = require('./listener')
|
const createListener = require('./listener')
|
||||||
|
|
||||||
module.exports = class TCP {
|
function noop () {}
|
||||||
dial (ma, options, cb) {
|
|
||||||
|
class TCP {
|
||||||
|
dial (ma, options, callback) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
cb = options
|
callback = options
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cb) {
|
callback = callback || noop
|
||||||
cb = () => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
cb = once(cb)
|
callback = once(callback)
|
||||||
const cOpts = ma.toOptions()
|
const cOpts = ma.toOptions()
|
||||||
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
||||||
|
|
||||||
const rawSocket = net.connect(cOpts)
|
const rawSocket = net.connect(cOpts)
|
||||||
|
|
||||||
rawSocket.once('timeout', () => {
|
rawSocket.once('timeout', () => {
|
||||||
log('timeout')
|
log('timeout')
|
||||||
rawSocket.emit('error', new Error('Timeout'))
|
rawSocket.emit('error', new Error('Timeout'))
|
||||||
})
|
})
|
||||||
|
|
||||||
rawSocket.once('error', cb)
|
rawSocket.once('error', callback)
|
||||||
|
|
||||||
rawSocket.once('connect', () => {
|
rawSocket.once('connect', () => {
|
||||||
rawSocket.removeListener('error', cb)
|
rawSocket.removeListener('error', callback)
|
||||||
cb()
|
callback()
|
||||||
})
|
})
|
||||||
|
|
||||||
const socket = toPull.duplex(rawSocket)
|
const socket = toPull.duplex(rawSocket)
|
||||||
|
|
||||||
const conn = new Connection(socket)
|
const conn = new Connection(socket)
|
||||||
|
|
||||||
conn.getObservedAddrs = (cb) => {
|
conn.getObservedAddrs = (callback) => {
|
||||||
return cb(null, [ma])
|
return callback(null, [ma])
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn
|
return conn
|
||||||
@ -74,3 +75,5 @@ module.exports = class TCP {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = TCP
|
||||||
|
@ -15,16 +15,22 @@ const getMultiaddr = require('./get-multiaddr')
|
|||||||
const IPFS_CODE = 421
|
const IPFS_CODE = 421
|
||||||
const CLOSE_TIMEOUT = 2000
|
const CLOSE_TIMEOUT = 2000
|
||||||
|
|
||||||
|
function noop () {}
|
||||||
|
|
||||||
module.exports = (handler) => {
|
module.exports = (handler) => {
|
||||||
const listener = new EventEmitter()
|
const listener = new EventEmitter()
|
||||||
|
|
||||||
const server = net.createServer((socket) => {
|
const server = net.createServer((socket) => {
|
||||||
|
// Avoid uncaught errors cause by unstable connections
|
||||||
|
socket.on('error', noop)
|
||||||
|
|
||||||
const addr = getMultiaddr(socket)
|
const addr = getMultiaddr(socket)
|
||||||
log('new connection', addr.toString())
|
log('new connection', addr.toString())
|
||||||
|
|
||||||
const s = toPull.duplex(socket)
|
const s = toPull.duplex(socket)
|
||||||
|
|
||||||
s.getObservedAddrs = (cb) => {
|
s.getObservedAddrs = (cb) => {
|
||||||
return cb(null, [addr])
|
cb(null, [addr])
|
||||||
}
|
}
|
||||||
|
|
||||||
trackSocket(server, socket)
|
trackSocket(server, socket)
|
||||||
@ -34,36 +40,31 @@ module.exports = (handler) => {
|
|||||||
listener.emit('connection', conn)
|
listener.emit('connection', conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
server.on('listening', () => {
|
server.on('listening', () => listener.emit('listening'))
|
||||||
listener.emit('listening')
|
server.on('error', (err) => listener.emit('error', err))
|
||||||
})
|
server.on('close', () => listener.emit('close'))
|
||||||
|
|
||||||
server.on('error', (err) => {
|
|
||||||
listener.emit('error', err)
|
|
||||||
})
|
|
||||||
|
|
||||||
server.on('close', () => {
|
|
||||||
listener.emit('close')
|
|
||||||
})
|
|
||||||
|
|
||||||
// Keep track of open connections to destroy in case of timeout
|
// Keep track of open connections to destroy in case of timeout
|
||||||
server.__connections = {}
|
server.__connections = {}
|
||||||
|
|
||||||
listener.close = (options, cb) => {
|
listener.close = (options, callback) => {
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
cb = options
|
callback = options
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
cb = cb || (() => {})
|
callback = callback || noop
|
||||||
options = options || {}
|
options = options || {}
|
||||||
|
|
||||||
let closed = false
|
let closed = false
|
||||||
server.close(cb)
|
server.close(callback)
|
||||||
|
|
||||||
server.once('close', () => {
|
server.once('close', () => {
|
||||||
closed = true
|
closed = true
|
||||||
})
|
})
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (closed) return
|
if (closed) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log('unable to close graciously, destroying conns')
|
log('unable to close graciously, destroying conns')
|
||||||
Object.keys(server.__connections).forEach((key) => {
|
Object.keys(server.__connections).forEach((key) => {
|
||||||
@ -76,7 +77,7 @@ module.exports = (handler) => {
|
|||||||
let ipfsId
|
let ipfsId
|
||||||
let listeningAddr
|
let listeningAddr
|
||||||
|
|
||||||
listener.listen = (ma, cb) => {
|
listener.listen = (ma, callback) => {
|
||||||
listeningAddr = ma
|
listeningAddr = ma
|
||||||
if (includes(ma.protoNames(), 'ipfs')) {
|
if (includes(ma.protoNames(), 'ipfs')) {
|
||||||
ipfsId = getIpfsId(ma)
|
ipfsId = getIpfsId(ma)
|
||||||
@ -85,15 +86,15 @@ module.exports = (handler) => {
|
|||||||
|
|
||||||
const lOpts = listeningAddr.toOptions()
|
const lOpts = listeningAddr.toOptions()
|
||||||
log('Listening on %s %s', lOpts.port, lOpts.host)
|
log('Listening on %s %s', lOpts.port, lOpts.host)
|
||||||
return server.listen(lOpts.port, lOpts.host, cb)
|
return server.listen(lOpts.port, lOpts.host, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
listener.getAddrs = (cb) => {
|
listener.getAddrs = (callback) => {
|
||||||
const multiaddrs = []
|
const multiaddrs = []
|
||||||
const address = server.address()
|
const address = server.address()
|
||||||
|
|
||||||
if (!address) {
|
if (!address) {
|
||||||
return cb(new Error('Listener is not ready yet'))
|
return callback(new Error('Listener is not ready yet'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Because TCP will only return the IPv6 version
|
// Because TCP will only return the IPv6 version
|
||||||
@ -128,7 +129,7 @@ module.exports = (handler) => {
|
|||||||
multiaddrs.push(ma)
|
multiaddrs.push(ma)
|
||||||
}
|
}
|
||||||
|
|
||||||
cb(null, multiaddrs)
|
callback(null, multiaddrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return listener
|
return listener
|
||||||
|
Reference in New Issue
Block a user