mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-04-25 06:02:19 +00:00
69 lines
1.4 KiB
JavaScript
69 lines
1.4 KiB
JavaScript
'use strict'
|
|
|
|
const net = require('net')
|
|
const toPull = require('stream-to-pull-stream')
|
|
const mafmt = require('mafmt')
|
|
const contains = require('lodash.contains')
|
|
const isFunction = require('lodash.isfunction')
|
|
const Connection = require('interface-connection').Connection
|
|
const debug = require('debug')
|
|
const log = debug('libp2p:tcp:dial')
|
|
|
|
const createListener = require('./listener')
|
|
|
|
module.exports = class TCP {
|
|
dial (ma, options, cb) {
|
|
if (isFunction(options)) {
|
|
cb = options
|
|
options = {}
|
|
}
|
|
|
|
if (!cb) {
|
|
cb = () => {}
|
|
}
|
|
|
|
const cOpts = ma.toOptions()
|
|
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
|
|
|
const rawSocket = net.connect(cOpts, cb)
|
|
|
|
rawSocket.once('timeout', () => {
|
|
log('timeout')
|
|
rawSocket.emit('error', new Error('Timeout'))
|
|
})
|
|
|
|
const socket = toPull.duplex(rawSocket)
|
|
|
|
const conn = new Connection(socket)
|
|
|
|
conn.getObservedAddrs = (cb) => {
|
|
return cb(null, [ma])
|
|
}
|
|
|
|
return conn
|
|
}
|
|
|
|
createListener (options, handler) {
|
|
if (isFunction(options)) {
|
|
handler = options
|
|
options = {}
|
|
}
|
|
|
|
handler = handler || (() => {})
|
|
|
|
return createListener(handler)
|
|
}
|
|
|
|
filter (multiaddrs) {
|
|
if (!Array.isArray(multiaddrs)) {
|
|
multiaddrs = [multiaddrs]
|
|
}
|
|
return multiaddrs.filter((ma) => {
|
|
if (contains(ma.protoNames(), 'ipfs')) {
|
|
ma = ma.decapsulate('ipfs')
|
|
}
|
|
return mafmt.TCP.matches(ma)
|
|
})
|
|
}
|
|
}
|