mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-06-18 16:21:21 +00:00
feat(pull): migrate to pull streams
This commit is contained in:
committed by
David Dias
parent
3c3a7077f6
commit
3f58dca09a
130
src/index.js
130
src/index.js
@ -1,147 +1,55 @@
|
||||
'use strict'
|
||||
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:websockets')
|
||||
const SW = require('simple-websocket')
|
||||
const isNode = require('detect-node')
|
||||
let SWS
|
||||
if (isNode) {
|
||||
SWS = require('simple-websocket-server')
|
||||
} else {
|
||||
SWS = {}
|
||||
}
|
||||
const connect = require('pull-ws/client')
|
||||
const mafmt = require('mafmt')
|
||||
const contains = require('lodash.contains')
|
||||
const Connection = require('interface-connection').Connection
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p:websockets:dialer')
|
||||
|
||||
const CLOSE_TIMEOUT = 2000
|
||||
// const IPFS_CODE = 421
|
||||
const createListener = require('./listener')
|
||||
|
||||
exports = module.exports = WebSockets
|
||||
|
||||
function WebSockets () {
|
||||
if (!(this instanceof WebSockets)) {
|
||||
return new WebSockets()
|
||||
}
|
||||
|
||||
this.dial = function (ma, options, callback) {
|
||||
module.exports = class WebSockets {
|
||||
dial (ma, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
if (!callback) {
|
||||
callback = function noop () {}
|
||||
callback = () => {}
|
||||
}
|
||||
|
||||
const maOpts = ma.toOptions()
|
||||
|
||||
const socket = new SW('ws://' + maOpts.host + ':' + maOpts.port)
|
||||
const url = `ws://${maOpts.host}:${maOpts.port}`
|
||||
log('dialing %s', url)
|
||||
const socket = connect(url, {
|
||||
binary: true,
|
||||
onConnect: callback
|
||||
})
|
||||
|
||||
const conn = new Connection(socket)
|
||||
|
||||
socket.on('timeout', () => {
|
||||
conn.emit('timeout')
|
||||
})
|
||||
|
||||
socket.on('error', (err) => {
|
||||
callback(err)
|
||||
conn.emit('error', err)
|
||||
})
|
||||
|
||||
socket.on('connect', () => {
|
||||
callback(null, conn)
|
||||
conn.emit('connect')
|
||||
})
|
||||
|
||||
conn.getObservedAddrs = (cb) => {
|
||||
return cb(null, [ma])
|
||||
}
|
||||
conn.getObservedAddrs = (cb) => cb(null, [ma])
|
||||
conn.close = (cb) => socket.close(cb)
|
||||
|
||||
return conn
|
||||
}
|
||||
|
||||
this.createListener = (options, handler) => {
|
||||
createListener (options, handler) {
|
||||
if (typeof options === 'function') {
|
||||
handler = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
const listener = SWS.createServer((socket) => {
|
||||
const conn = new Connection(socket)
|
||||
|
||||
conn.getObservedAddrs = (cb) => {
|
||||
// TODO research if we can reuse the address in anyway
|
||||
return cb(null, [])
|
||||
}
|
||||
handler(conn)
|
||||
})
|
||||
|
||||
let listeningMultiaddr
|
||||
|
||||
listener._listen = listener.listen
|
||||
listener.listen = (ma, callback) => {
|
||||
if (!callback) {
|
||||
callback = function noop () {}
|
||||
}
|
||||
|
||||
listeningMultiaddr = ma
|
||||
|
||||
if (contains(ma.protoNames(), 'ipfs')) {
|
||||
ma = ma.decapsulate('ipfs')
|
||||
}
|
||||
|
||||
listener._listen(ma.toOptions(), callback)
|
||||
}
|
||||
|
||||
listener._close = listener.close
|
||||
listener.close = (options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = { timeout: CLOSE_TIMEOUT }
|
||||
}
|
||||
if (!callback) { callback = function noop () {} }
|
||||
if (!options) { options = { timeout: CLOSE_TIMEOUT } }
|
||||
|
||||
let closed = false
|
||||
listener.once('close', () => {
|
||||
closed = true
|
||||
})
|
||||
listener._close(callback)
|
||||
setTimeout(() => {
|
||||
if (closed) {
|
||||
return
|
||||
}
|
||||
log('unable to close graciously, destroying conns')
|
||||
Object.keys(listener.__connections).forEach((key) => {
|
||||
log('destroying %s', key)
|
||||
listener.__connections[key].destroy()
|
||||
})
|
||||
}, options.timeout || CLOSE_TIMEOUT)
|
||||
}
|
||||
|
||||
// Keep track of open connections to destroy in case of timeout
|
||||
listener.__connections = {}
|
||||
listener.on('connection', (socket) => {
|
||||
const key = (~~(Math.random() * 1e9)).toString(36) + Date.now()
|
||||
listener.__connections[key] = socket
|
||||
|
||||
socket.on('close', () => {
|
||||
delete listener.__connections[key]
|
||||
})
|
||||
})
|
||||
|
||||
listener.getAddrs = (callback) => {
|
||||
callback(null, [listeningMultiaddr])
|
||||
}
|
||||
|
||||
return listener
|
||||
return createListener(options, handler)
|
||||
}
|
||||
|
||||
this.filter = (multiaddrs) => {
|
||||
filter (multiaddrs) {
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
return multiaddrs.filter((ma) => {
|
||||
if (contains(ma.protoNames(), 'ipfs')) {
|
||||
ma = ma.decapsulate('ipfs')
|
||||
|
46
src/listener.js
Normal file
46
src/listener.js
Normal file
@ -0,0 +1,46 @@
|
||||
'use strict'
|
||||
|
||||
const isNode = require('detect-node')
|
||||
const Connection = require('interface-connection').Connection
|
||||
const contains = require('lodash.contains')
|
||||
|
||||
// const IPFS_CODE = 421
|
||||
|
||||
let createServer
|
||||
|
||||
if (isNode) {
|
||||
createServer = require('pull-ws/server')
|
||||
} else {
|
||||
createServer = () => {}
|
||||
}
|
||||
|
||||
module.exports = (options, handler) => {
|
||||
const listener = createServer((socket) => {
|
||||
socket.getObservedAddrs = (cb) => {
|
||||
// TODO research if we can reuse the address in anyway
|
||||
return cb(null, [])
|
||||
}
|
||||
|
||||
handler(new Connection(socket))
|
||||
})
|
||||
|
||||
let listeningMultiaddr
|
||||
|
||||
listener._listen = listener.listen
|
||||
listener.listen = (ma, cb) => {
|
||||
cb = cb || (() => {})
|
||||
listeningMultiaddr = ma
|
||||
|
||||
if (contains(ma.protoNames(), 'ipfs')) {
|
||||
ma = ma.decapsulate('ipfs')
|
||||
}
|
||||
|
||||
listener._listen(ma.toOptions(), cb)
|
||||
}
|
||||
|
||||
listener.getAddrs = (cb) => {
|
||||
cb(null, [listeningMultiaddr])
|
||||
}
|
||||
|
||||
return listener
|
||||
}
|
Reference in New Issue
Block a user