This repository was archived by the owner on Jun 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (75 loc) · 2.64 KB
/
index.js
File metadata and controls
85 lines (75 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict'
const debug = require('debug')
const log = debug('libp2p:websocket-star:multi')
const once = require('once')
const EE = require('events').EventEmitter
const map = require('async/map')
const parallel = require('async/parallel')
const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const WSStar = require('libp2p-websocket-star')
class WebsocketStarMulti { // listen on multiple websocket star servers without having to worry about one being down.
// NOTE: if no servers are reachable or provided an error is thrown
constructor (opt) {
this.opt = opt || {}
this.servers = opt.servers || []
this.ws = new WSStar(this.opt)
this.discovery = this.ws.discovery
}
dial (ma, opt, cb) {
log('dial', ma)
return this.ws.dial(ma, opt, cb)
}
createListener (options, handler) {
if (typeof options === 'function') {
handler = options
options = {}
}
const listener = new EE()
listener.servers = {}
listener.online = []
this.servers.forEach(ser => {
const s = this.ws.createListener(options, handler)
s.once('error', () => {})
s.url = ser
listener.servers[ser] = s
})
listener.listen = (ma, cb) => {
const id = ma.toString().split('ipfs/').pop()
log('listen on %s server(s) with id %s', this.servers.length, id)
parallel(this.servers.map(url => listener.servers[url]).map(server =>
cb => {
log('listen %s', server.url)
const next = once(err => {
log('listen %s ok %s', server.url, !err)
if (err) return cb(log(err))
listener.online.push(server)
return cb()
})
setTimeout(next, this.opt.timeout || 5000, new Error('Timeout'))
server.listen(multiaddr(server.url).encapsulate('/ipfs/' + id), next)
}), () => {
if (!listener.online.length && !this.opt.ignore_no_online) {
const e = new Error("Couldn't listen on any of the servers")
listener.emit('error', e)
cb(e)
} else {
listener.emit('listening')
cb()
}
})
}
listener.close = cb =>
parallel(listener.online.map(s => cb => s.close(cb)), err => cb(err, (listener.online = [])))
listener.getAddrs = cb => map(listener.online, (s, n) => s.getAddrs(n), (err, res) => {
if (err) return cb(err)
return cb(null, res.reduce((a, b) => a.concat(b), []))
})
return listener
}
filter (ma) {
if (!Array.isArray(ma)) ma = [ma]
return ma.filter(ma => ma.toString().startsWith('/p2p-websocket-star') || mafmt.WebSocketStar.matches(ma))
}
}
module.exports = WebsocketStarMulti