mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-03 15:01:51 +00:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
ce4974b01c | |||
49e23f1961 | |||
10b35b22f6 | |||
5677e12592 | |||
9524e8670a |
2
.gitignore
vendored
2
.gitignore
vendored
@ -25,3 +25,5 @@ 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
|
||||||
|
|
||||||
|
docs
|
@ -27,3 +27,4 @@ build/Release
|
|||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
test
|
test
|
||||||
|
docs
|
21
package.json
21
package.json
@ -6,9 +6,10 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "aegir-lint",
|
"lint": "aegir-lint",
|
||||||
"test": "aegir-test --env node",
|
"test": "aegir-test --env node",
|
||||||
"release": "aegir-release --env no-build",
|
"docs": "aegir-docs",
|
||||||
"release-minor": "aegir-release --type minor --env no-build",
|
"release": "aegir-release --env no-build --docs",
|
||||||
"release-major": "aegir-release --type major --env no-build",
|
"release-minor": "aegir-release --type minor --env no-build --docs",
|
||||||
|
"release-major": "aegir-release --type major --env no-build --docs",
|
||||||
"coverage": "aegir-coverage",
|
"coverage": "aegir-coverage",
|
||||||
"coverage-publish": "aegir-coverage publish"
|
"coverage-publish": "aegir-coverage publish"
|
||||||
},
|
},
|
||||||
@ -33,20 +34,20 @@
|
|||||||
"node": ">=4.0.0"
|
"node": ">=4.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^9.0.1",
|
"aegir": "^9.2.0",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"interface-transport": "^0.3.3",
|
"interface-transport": "^0.3.3",
|
||||||
"lodash.isfunction": "^3.0.8",
|
"lodash.isfunction": "^3.0.8",
|
||||||
"pre-commit": "^1.1.2",
|
"pre-commit": "^1.1.3",
|
||||||
"pull-stream": "^3.4.5"
|
"pull-stream": "^3.5.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"interface-connection": "0.3.0",
|
"interface-connection": "0.3.0",
|
||||||
"ip-address": "^5.8.0",
|
"ip-address": "^5.8.2",
|
||||||
"lodash.contains": "^2.4.3",
|
"lodash.includes": "^4.3.0",
|
||||||
"mafmt": "^2.1.2",
|
"mafmt": "^2.1.2",
|
||||||
"multiaddr": "^2.0.3",
|
"multiaddr": "^2.1.1",
|
||||||
"stream-to-pull-stream": "^1.7.0"
|
"stream-to-pull-stream": "^1.7.2"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
43
src/index.js
43
src/index.js
@ -3,7 +3,7 @@
|
|||||||
const net = require('net')
|
const net = require('net')
|
||||||
const toPull = require('stream-to-pull-stream')
|
const toPull = require('stream-to-pull-stream')
|
||||||
const mafmt = require('mafmt')
|
const mafmt = require('mafmt')
|
||||||
const contains = require('lodash.contains')
|
const includes = require('lodash.includes')
|
||||||
const isFunction = require('lodash.isfunction')
|
const isFunction = require('lodash.isfunction')
|
||||||
const Connection = require('interface-connection').Connection
|
const Connection = require('interface-connection').Connection
|
||||||
const debug = require('debug')
|
const debug = require('debug')
|
||||||
@ -11,21 +11,32 @@ const log = debug('libp2p:tcp:dial')
|
|||||||
|
|
||||||
const createListener = require('./listener')
|
const createListener = require('./listener')
|
||||||
|
|
||||||
module.exports = class TCP {
|
/**
|
||||||
dial (ma, options, cb) {
|
*
|
||||||
|
*/
|
||||||
|
class TCP {
|
||||||
|
/**
|
||||||
|
* Dial to another peer.
|
||||||
|
*
|
||||||
|
* @param {Multiaddr} ma - The address of the peer we want to dial to.
|
||||||
|
* @param {Object} [options={}]
|
||||||
|
* @param {function(Error?, Array<Multiaddr>?)} [callback]
|
||||||
|
* @returns {Connection}
|
||||||
|
*/
|
||||||
|
dial (ma, options, callback) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
cb = options
|
callback = options
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cb) {
|
if (!callback) {
|
||||||
cb = () => {}
|
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, cb)
|
const rawSocket = net.connect(cOpts, callback)
|
||||||
|
|
||||||
rawSocket.once('timeout', () => {
|
rawSocket.once('timeout', () => {
|
||||||
log('timeout')
|
log('timeout')
|
||||||
@ -43,6 +54,13 @@ module.exports = class TCP {
|
|||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen for incoming `TCP` connetions.
|
||||||
|
*
|
||||||
|
* @param {Object} [options={}]
|
||||||
|
* @param {function(Connection)} [handler] - Called with newly incomin connections.
|
||||||
|
* @returns {Listener}
|
||||||
|
*/
|
||||||
createListener (options, handler) {
|
createListener (options, handler) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
handler = options
|
handler = options
|
||||||
@ -54,15 +72,24 @@ module.exports = class TCP {
|
|||||||
return createListener(handler)
|
return createListener(handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter a list of multiaddrs for those which contain
|
||||||
|
* valid `TCP` addresses.
|
||||||
|
*
|
||||||
|
* @param {Multiaddr|Array<Multiaddr>} multiaddrs
|
||||||
|
* @returns {Array<Multiaddr>}
|
||||||
|
*/
|
||||||
filter (multiaddrs) {
|
filter (multiaddrs) {
|
||||||
if (!Array.isArray(multiaddrs)) {
|
if (!Array.isArray(multiaddrs)) {
|
||||||
multiaddrs = [multiaddrs]
|
multiaddrs = [multiaddrs]
|
||||||
}
|
}
|
||||||
return multiaddrs.filter((ma) => {
|
return multiaddrs.filter((ma) => {
|
||||||
if (contains(ma.protoNames(), 'ipfs')) {
|
if (includes(ma.protoNames(), 'ipfs')) {
|
||||||
ma = ma.decapsulate('ipfs')
|
ma = ma.decapsulate('ipfs')
|
||||||
}
|
}
|
||||||
return mafmt.TCP.matches(ma)
|
return mafmt.TCP.matches(ma)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = TCP
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Connection = require('interface-connection').Connection
|
const Connection = require('interface-connection').Connection
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
const contains = require('lodash.contains')
|
const includes = require('lodash.includes')
|
||||||
const net = require('net')
|
const net = require('net')
|
||||||
const toPull = require('stream-to-pull-stream')
|
const toPull = require('stream-to-pull-stream')
|
||||||
const EventEmitter = require('events').EventEmitter
|
const EventEmitter = require('events').EventEmitter
|
||||||
@ -15,7 +15,49 @@ const getMultiaddr = require('./get-multiaddr')
|
|||||||
const IPFS_CODE = 421
|
const IPFS_CODE = 421
|
||||||
const CLOSE_TIMEOUT = 2000
|
const CLOSE_TIMEOUT = 2000
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listening for incoming connections.
|
||||||
|
*
|
||||||
|
* @event listening
|
||||||
|
* @instance
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The server closes.
|
||||||
|
*
|
||||||
|
* @event close
|
||||||
|
* @instance
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* New connection established.
|
||||||
|
*
|
||||||
|
* @event connection
|
||||||
|
* @instance
|
||||||
|
* @type {Connection}
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The underlying server encountered an error.
|
||||||
|
*
|
||||||
|
* @event error
|
||||||
|
* @instance
|
||||||
|
* @type {Error}
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
module.exports = (handler) => {
|
module.exports = (handler) => {
|
||||||
|
/**
|
||||||
|
* @alias Listener
|
||||||
|
* @type {Eventemitter}
|
||||||
|
* @fires Listener#listening
|
||||||
|
* @fires Listener#close
|
||||||
|
* @fires Listener#connection
|
||||||
|
* @fires Listener#error
|
||||||
|
*/
|
||||||
const listener = new EventEmitter()
|
const listener = new EventEmitter()
|
||||||
|
|
||||||
const server = net.createServer((socket) => {
|
const server = net.createServer((socket) => {
|
||||||
@ -78,7 +120,7 @@ module.exports = (handler) => {
|
|||||||
|
|
||||||
listener.listen = (ma, cb) => {
|
listener.listen = (ma, cb) => {
|
||||||
listeningAddr = ma
|
listeningAddr = ma
|
||||||
if (contains(ma.protoNames(), 'ipfs')) {
|
if (includes(ma.protoNames(), 'ipfs')) {
|
||||||
ipfsId = getIpfsId(ma)
|
ipfsId = getIpfsId(ma)
|
||||||
listeningAddr = ma.decapsulate('ipfs')
|
listeningAddr = ma.decapsulate('ipfs')
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user