mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-04-21 18:52:30 +00:00
91 lines
2.1 KiB
JavaScript
91 lines
2.1 KiB
JavaScript
'use strict'
|
|
|
|
const debug = require('debug')
|
|
const log = debug('libp2p:websockets')
|
|
const SWS = require('simple-websocket')
|
|
const mafmt = require('mafmt')
|
|
const parallel = require('run-parallel')
|
|
const contains = require('lodash.contains')
|
|
|
|
exports = module.exports = WebSockets
|
|
|
|
function WebSockets () {
|
|
if (!(this instanceof WebSockets)) {
|
|
return new WebSockets()
|
|
}
|
|
|
|
const listeners = []
|
|
|
|
this.dial = function (multiaddr, options) {
|
|
if (!options) {
|
|
options = {}
|
|
}
|
|
|
|
options.ready = options.ready || function noop () {}
|
|
const maOpts = multiaddr.toOptions()
|
|
const conn = new SWS('ws://' + maOpts.host + ':' + maOpts.port)
|
|
conn.on('connect', options.ready)
|
|
conn.getObservedAddrs = () => {
|
|
return [multiaddr]
|
|
}
|
|
return conn
|
|
}
|
|
|
|
this.createListener = (multiaddrs, options, handler, callback) => {
|
|
if (typeof options === 'function') {
|
|
callback = handler
|
|
handler = options
|
|
options = {}
|
|
}
|
|
|
|
if (!Array.isArray(multiaddrs)) {
|
|
multiaddrs = [multiaddrs]
|
|
}
|
|
|
|
var count = 0
|
|
|
|
multiaddrs.forEach((m) => {
|
|
if (contains(m.protoNames(), 'ipfs')) {
|
|
m = m.decapsulate('ipfs')
|
|
}
|
|
|
|
const listener = SWS.createServer((conn) => {
|
|
conn.getObservedAddrs = () => {
|
|
return [] // TODO think if it makes sense for WebSockets
|
|
}
|
|
handler(conn)
|
|
})
|
|
|
|
listener.listen(m.toOptions().port, () => {
|
|
if (++count === multiaddrs.length) {
|
|
callback()
|
|
}
|
|
})
|
|
listeners.push(listener)
|
|
})
|
|
}
|
|
|
|
this.close = (callback) => {
|
|
if (listeners.length === 0) {
|
|
log('Called close with no active listeners')
|
|
return callback()
|
|
}
|
|
|
|
parallel(listeners.map((listener) => {
|
|
return (cb) => listener.close(cb)
|
|
}), callback)
|
|
}
|
|
|
|
this.filter = (multiaddrs) => {
|
|
if (!Array.isArray(multiaddrs)) {
|
|
multiaddrs = [multiaddrs]
|
|
}
|
|
return multiaddrs.filter((ma) => {
|
|
if (contains(ma.protoNames(), 'ipfs')) {
|
|
ma = ma.decapsulate('ipfs')
|
|
}
|
|
return mafmt.WebSockets.matches(ma)
|
|
})
|
|
}
|
|
}
|