Compare commits

...

2 Commits

Author SHA1 Message Date
270ac46090 fix: close by array
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
2018-11-08 17:03:23 +01:00
1cd0066755 chore: update dependencies and cleanup usage
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
2018-11-08 16:32:33 +01:00
3 changed files with 21 additions and 30 deletions

View File

@ -34,22 +34,19 @@
"npm": ">=3.0.0" "npm": ">=3.0.0"
}, },
"devDependencies": { "devDependencies": {
"aegir": "^15.1.0", "aegir": "^17.0.1",
"chai": "^4.1.2", "chai": "^4.2.0",
"dirty-chai": "^2.0.1", "dirty-chai": "^2.0.1",
"interface-transport": "~0.3.6", "interface-transport": "~0.3.6",
"lodash.isfunction": "^3.0.9",
"pull-stream": "^3.6.9" "pull-stream": "^3.6.9"
}, },
"dependencies": { "dependencies": {
"class-is": "^1.1.0", "class-is": "^1.1.0",
"debug": "^3.1.0", "debug": "^4.1.0",
"interface-connection": "~0.3.2", "interface-connection": "~0.3.2",
"ip-address": "^5.8.9", "ip-address": "^5.8.9",
"lodash.includes": "^4.3.0",
"lodash.isfunction": "^3.0.9",
"mafmt": "^6.0.2", "mafmt": "^6.0.2",
"multiaddr": "^5.0.0", "multiaddr": "^5.0.2",
"once": "^1.4.0", "once": "^1.4.0",
"stream-to-pull-stream": "^1.7.2" "stream-to-pull-stream": "^1.7.2"
}, },

View File

@ -4,8 +4,6 @@ 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 withIs = require('class-is') const withIs = require('class-is')
const includes = require('lodash.includes')
const isFunction = require('lodash.isfunction')
const Connection = require('interface-connection').Connection const Connection = require('interface-connection').Connection
const once = require('once') const once = require('once')
const debug = require('debug') const debug = require('debug')
@ -17,7 +15,7 @@ function noop () {}
class TCP { class TCP {
dial (ma, options, callback) { dial (ma, options, callback) {
if (isFunction(options)) { if (typeof options === 'function') {
callback = options callback = options
options = {} options = {}
} }
@ -53,7 +51,7 @@ class TCP {
} }
createListener (options, handler) { createListener (options, handler) {
if (isFunction(options)) { if (typeof options === 'function') {
handler = options handler = options
options = {} options = {}
} }
@ -69,11 +67,11 @@ class TCP {
} }
return multiaddrs.filter((ma) => { return multiaddrs.filter((ma) => {
if (includes(ma.protoNames(), 'p2p-circuit')) { if (ma.protoNames().includes('p2p-circuit')) {
return false return false
} }
if (includes(ma.protoNames(), 'ipfs')) { if (ma.protoNames().includes('ipfs')) {
ma = ma.decapsulate('ipfs') ma = ma.decapsulate('ipfs')
} }

View File

@ -3,7 +3,6 @@
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 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
@ -42,8 +41,6 @@ module.exports = (handler) => {
cb(null, [addr]) cb(null, [addr])
} }
trackSocket(server, socket)
const conn = new Connection(s) const conn = new Connection(s)
handler(conn) handler(conn)
listener.emit('connection', conn) listener.emit('connection', conn)
@ -52,9 +49,17 @@ module.exports = (handler) => {
server.on('listening', () => listener.emit('listening')) server.on('listening', () => listener.emit('listening'))
server.on('error', (err) => listener.emit('error', err)) server.on('error', (err) => listener.emit('error', err))
server.on('close', () => listener.emit('close')) server.on('close', () => listener.emit('close'))
server.on('connection', (conn) => {
server.__connections.push(conn)
conn.once('close', () => {
server.__connections = server.__connections.filter((c) => {
return conn !== c
})
})
})
// 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, callback) => { listener.close = (options, callback) => {
if (typeof options === 'function') { if (typeof options === 'function') {
@ -66,9 +71,9 @@ module.exports = (handler) => {
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
log('unable to close graciously, destroying conns') log('unable to close graciously, destroying conns')
Object.keys(server.__connections).forEach((key) => { server.__connections.forEach((conn) => {
log('destroying %s', key) log('destroying %s', `${conn.remoteAddress}:${conn.remotePort}`)
server.__connections[key].destroy() conn.destroy()
}) })
}, options.timeout || CLOSE_TIMEOUT) }, options.timeout || CLOSE_TIMEOUT)
@ -84,7 +89,7 @@ module.exports = (handler) => {
listener.listen = (ma, callback) => { listener.listen = (ma, callback) => {
listeningAddr = ma listeningAddr = ma
if (includes(ma.protoNames(), 'ipfs')) { if (ma.protoNames().includes('ipfs')) {
ipfsId = getIpfsId(ma) ipfsId = getIpfsId(ma)
listeningAddr = ma.decapsulate('ipfs') listeningAddr = ma.decapsulate('ipfs')
} }
@ -145,12 +150,3 @@ function getIpfsId (ma) {
return tuple[0] === IPFS_CODE return tuple[0] === IPFS_CODE
})[0][1] })[0][1]
} }
function trackSocket (server, socket) {
const key = `${socket.remoteAddress}:${socket.remotePort}`
server.__connections[key] = socket
socket.on('close', () => {
delete server.__connections[key]
})
}