This repository has been archived by the owner on Feb 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
multiaddr-set.js
107 lines (89 loc) · 2.57 KB
/
multiaddr-set.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
const { ensureMultiaddr } = require('./utils')
const uniqBy = require('unique-by')
// Because JavaScript doesn't let you overload the compare in Set()..
class MultiaddrSet {
constructor (multiaddrs) {
this._multiaddrs = multiaddrs || []
this._observedMultiaddrs = []
}
add (ma) {
ma = ensureMultiaddr(ma)
if (!this.has(ma)) {
this._multiaddrs.push(ma)
}
}
// addSafe - prevent multiaddr explosion™
// Multiaddr explosion is when you dial to a bunch of nodes and every node
// gives you a different observed address and you start storing them all to
// share with other peers. This seems like a good idea until you realize that
// most of those addresses are unique to the subnet that peer is in and so,
// they are completely worthless for all the other peers. This method is
// exclusively used by identify.
addSafe (ma) {
ma = ensureMultiaddr(ma)
const check = this._observedMultiaddrs.some((m, i) => {
if (m.equals(ma)) {
this.add(ma)
this._observedMultiaddrs.splice(i, 1)
return true
}
})
if (!check) {
this._observedMultiaddrs.push(ma)
}
}
toArray () {
return this._multiaddrs.slice()
}
get size () {
return this._multiaddrs.length
}
forEach (fn) {
return this._multiaddrs.forEach(fn)
}
filterBy (maFmt) {
if (typeof maFmt !== 'object' ||
typeof maFmt.matches !== 'function' ||
typeof maFmt.partialMatch !== 'function' ||
typeof maFmt.toString !== 'function') return []
return this._multiaddrs.filter((ma) => maFmt.matches(ma))
}
has (ma) {
ma = ensureMultiaddr(ma)
return this._multiaddrs.some((m) => m.equals(ma))
}
delete (ma) {
ma = ensureMultiaddr(ma)
this._multiaddrs.some((m, i) => {
if (m.equals(ma)) {
this._multiaddrs.splice(i, 1)
return true
}
})
}
// replaces selected existing multiaddrs with new ones
replace (existing, fresh) {
if (!Array.isArray(existing)) {
existing = [existing]
}
if (!Array.isArray(fresh)) {
fresh = [fresh]
}
existing.forEach((m) => this.delete(m))
fresh.forEach((m) => this.add(m))
}
clear () {
this._multiaddrs = []
}
// this only really helps make ip6 and ip4 multiaddrs distinct if they are
// different
// TODO this is not an ideal solution, probably this code should just be
// in libp2p-tcp
distinct () {
return uniqBy(this._multiaddrs, (ma) => {
return [ma.toOptions().port, ma.toOptions().transport].join()
})
}
}
module.exports = MultiaddrSet