libp2p 0.35.8 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/dist/index.min.js +55 -0
  2. package/dist/src/circuit/auto-relay.d.ts +8 -10
  3. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  4. package/dist/src/config.d.ts +2 -0
  5. package/dist/src/config.d.ts.map +1 -1
  6. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  7. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  8. package/dist/src/connection-manager/index.d.ts +1 -2
  9. package/dist/src/connection-manager/index.d.ts.map +1 -1
  10. package/dist/src/content-routing/utils.d.ts +3 -3
  11. package/dist/src/content-routing/utils.d.ts.map +1 -1
  12. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  13. package/dist/src/dialer/index.d.ts +12 -7
  14. package/dist/src/dialer/index.d.ts.map +1 -1
  15. package/dist/src/errors.d.ts +2 -0
  16. package/dist/src/fetch/constants.d.ts +2 -0
  17. package/dist/src/fetch/constants.d.ts.map +1 -0
  18. package/dist/src/fetch/index.d.ts +76 -0
  19. package/dist/src/fetch/index.d.ts.map +1 -0
  20. package/dist/src/fetch/proto.d.ts +134 -0
  21. package/dist/src/identify/index.d.ts +4 -4
  22. package/dist/src/identify/index.d.ts.map +1 -1
  23. package/dist/src/index.d.ts +116 -7
  24. package/dist/src/index.d.ts.map +1 -1
  25. package/dist/src/metrics/index.d.ts +9 -4
  26. package/dist/src/metrics/index.d.ts.map +1 -1
  27. package/dist/src/metrics/tracked-map.d.ts +6 -1
  28. package/dist/src/metrics/tracked-map.d.ts.map +1 -1
  29. package/dist/src/peer-routing.d.ts +1 -1
  30. package/dist/src/peer-routing.d.ts.map +1 -1
  31. package/dist/src/peer-store/address-book.d.ts +30 -84
  32. package/dist/src/peer-store/address-book.d.ts.map +1 -1
  33. package/dist/src/peer-store/index.d.ts +28 -94
  34. package/dist/src/peer-store/index.d.ts.map +1 -1
  35. package/dist/src/peer-store/key-book.d.ts +31 -15
  36. package/dist/src/peer-store/key-book.d.ts.map +1 -1
  37. package/dist/src/peer-store/metadata-book.d.ts +33 -29
  38. package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
  39. package/dist/src/peer-store/pb/peer.d.ts +222 -0
  40. package/dist/src/peer-store/proto-book.d.ts +27 -28
  41. package/dist/src/peer-store/proto-book.d.ts.map +1 -1
  42. package/dist/src/peer-store/store.d.ts +73 -0
  43. package/dist/src/peer-store/store.d.ts.map +1 -0
  44. package/dist/src/peer-store/types.d.ts +202 -0
  45. package/dist/src/peer-store/types.d.ts.map +1 -0
  46. package/dist/src/ping/index.d.ts.map +1 -1
  47. package/dist/src/record/peer-record/index.d.ts +2 -2
  48. package/dist/src/record/peer-record/index.d.ts.map +1 -1
  49. package/dist/src/registrar.d.ts +5 -5
  50. package/dist/src/registrar.d.ts.map +1 -1
  51. package/dist/src/types.d.ts +90 -0
  52. package/dist/src/types.d.ts.map +1 -0
  53. package/dist/src/upgrader.d.ts +8 -2
  54. package/dist/src/upgrader.d.ts.map +1 -1
  55. package/package.json +23 -26
  56. package/src/circuit/auto-relay.js +52 -38
  57. package/src/config.js +2 -0
  58. package/src/connection-manager/auto-dialler.js +20 -8
  59. package/src/connection-manager/index.js +42 -22
  60. package/src/content-routing/utils.js +4 -4
  61. package/src/dialer/dial-request.js +4 -1
  62. package/src/dialer/index.js +38 -8
  63. package/src/errors.js +2 -0
  64. package/src/fetch/README.md +36 -0
  65. package/src/fetch/constants.js +6 -0
  66. package/src/fetch/index.js +159 -0
  67. package/src/fetch/proto.d.ts +134 -0
  68. package/src/fetch/proto.js +333 -0
  69. package/src/fetch/proto.proto +15 -0
  70. package/src/identify/index.js +47 -38
  71. package/src/index.js +72 -27
  72. package/src/metrics/index.js +15 -8
  73. package/src/metrics/tracked-map.js +40 -14
  74. package/src/peer-routing.js +4 -1
  75. package/src/peer-store/address-book.js +254 -226
  76. package/src/peer-store/index.js +79 -110
  77. package/src/peer-store/key-book.js +99 -54
  78. package/src/peer-store/metadata-book.js +171 -101
  79. package/src/peer-store/pb/peer.d.ts +222 -0
  80. package/src/peer-store/pb/peer.js +643 -0
  81. package/src/peer-store/pb/peer.proto +31 -0
  82. package/src/peer-store/proto-book.js +171 -105
  83. package/src/peer-store/store.js +263 -0
  84. package/src/peer-store/types.ts +245 -0
  85. package/src/ping/index.js +6 -0
  86. package/src/record/peer-record/index.js +1 -1
  87. package/src/record/utils.js +1 -1
  88. package/src/registrar.js +4 -4
  89. package/src/types.ts +98 -0
  90. package/src/upgrader.js +32 -2
  91. package/dist/src/peer-store/book.d.ts +0 -81
  92. package/dist/src/peer-store/book.d.ts.map +0 -1
  93. package/dist/src/peer-store/persistent/consts.d.ts +0 -6
  94. package/dist/src/peer-store/persistent/consts.d.ts.map +0 -1
  95. package/dist/src/peer-store/persistent/index.d.ts +0 -129
  96. package/dist/src/peer-store/persistent/index.d.ts.map +0 -1
  97. package/dist/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  98. package/dist/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  99. package/src/peer-store/book.js +0 -124
  100. package/src/peer-store/persistent/consts.js +0 -15
  101. package/src/peer-store/persistent/index.js +0 -408
  102. package/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  103. package/src/peer-store/persistent/pb/address-book.js +0 -522
  104. package/src/peer-store/persistent/pb/address-book.proto +0 -27
  105. package/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  106. package/src/peer-store/persistent/pb/proto-book.js +0 -157
  107. package/src/peer-store/persistent/pb/proto-book.proto +0 -5
@@ -0,0 +1,245 @@
1
+ import type PeerId from 'peer-id'
2
+ import type { Multiaddr } from 'multiaddr'
3
+ import type Envelope from '../record/envelope'
4
+ import type { PublicKey } from 'libp2p-interfaces/src/keys/types'
5
+
6
+ export interface Address {
7
+ /**
8
+ * Peer multiaddr
9
+ */
10
+ multiaddr: Multiaddr
11
+
12
+ /**
13
+ * Obtained from a signed peer record
14
+ */
15
+ isCertified: boolean
16
+ }
17
+
18
+ export interface Peer {
19
+ /**
20
+ * Peer's peer-id instance
21
+ */
22
+ id: PeerId
23
+
24
+ /**
25
+ * Peer's addresses containing its multiaddrs and metadata
26
+ */
27
+ addresses: Address[]
28
+
29
+ /**
30
+ * Peer's supported protocols
31
+ */
32
+ protocols: string[]
33
+
34
+ /**
35
+ * Peer's metadata map
36
+ */
37
+ metadata: Map<string, Uint8Array>
38
+
39
+ /**
40
+ * May be set if the key that this Peer has is an RSA key
41
+ */
42
+ pubKey?: PublicKey
43
+
44
+ /**
45
+ * The last peer record envelope received
46
+ */
47
+ peerRecordEnvelope?: Uint8Array
48
+ }
49
+
50
+ export interface CertifiedRecord {
51
+ raw: Uint8Array
52
+ seqNumber: number
53
+ }
54
+
55
+ export interface AddressBookEntry {
56
+ addresses: Address[]
57
+ record: CertifiedRecord
58
+ }
59
+
60
+ export interface Book<Type> {
61
+ /**
62
+ * Get the known data of a peer
63
+ */
64
+ get: (peerId: PeerId) => Promise<Type>
65
+
66
+ /**
67
+ * Set the known data of a peer
68
+ */
69
+ set: (peerId: PeerId, data: Type) => Promise<void>
70
+
71
+ /**
72
+ * Remove the known data of a peer
73
+ */
74
+ delete: (peerId: PeerId) => Promise<void>
75
+ }
76
+
77
+ /**
78
+ * AddressBook containing a map of peerIdStr to Address.
79
+ */
80
+ export interface AddressBook {
81
+ /**
82
+ * ConsumePeerRecord adds addresses from a signed peer record contained in a record envelope.
83
+ * This will return a boolean that indicates if the record was successfully processed and added
84
+ * into the AddressBook
85
+ */
86
+ consumePeerRecord: (envelope: Envelope) => Promise<boolean>
87
+
88
+ /**
89
+ * Get the raw Envelope for a peer. Returns
90
+ * undefined if no Envelope is found
91
+ */
92
+ getRawEnvelope: (peerId: PeerId) => Promise<Uint8Array | undefined>
93
+
94
+ /**
95
+ * Get an Envelope containing a PeerRecord for the given peer.
96
+ * Returns undefined if no record exists.
97
+ */
98
+ getPeerRecord: (peerId: PeerId) => Promise<Envelope | undefined>
99
+
100
+ /**
101
+ * Add known addresses of a provided peer.
102
+ * If the peer is not known, it is set with the given addresses.
103
+ */
104
+ add: (peerId: PeerId, multiaddrs: Multiaddr[]) => Promise<void>
105
+
106
+ /**
107
+ * Set the known addresses of a peer
108
+ */
109
+ set: (peerId: PeerId, data: Multiaddr[]) => Promise<void>
110
+
111
+ /**
112
+ * Return the known addresses of a peer
113
+ */
114
+ get: (peerId: PeerId) => Promise<Address[]>
115
+
116
+ /**
117
+ * Get the known multiaddrs for a given peer. All returned multiaddrs
118
+ * will include the encapsulated `PeerId` of the peer.
119
+ */
120
+ getMultiaddrsForPeer: (peerId: PeerId, addressSorter?: (ms: Address[]) => Address[]) => Promise<Multiaddr[]>
121
+ }
122
+
123
+ /**
124
+ * KeyBook containing a map of peerIdStr to their PeerId with public keys.
125
+ */
126
+ export interface KeyBook {
127
+ /**
128
+ * Get the known data of a peer
129
+ */
130
+ get: (peerId: PeerId) => Promise<PublicKey | undefined>
131
+
132
+ /**
133
+ * Set the known data of a peer
134
+ */
135
+ set: (peerId: PeerId, data: PublicKey) => Promise<void>
136
+
137
+ /**
138
+ * Remove the known data of a peer
139
+ */
140
+ delete: (peerId: PeerId) => Promise<void>
141
+ }
142
+
143
+ /**
144
+ * MetadataBook containing a map of peerIdStr to their metadata Map.
145
+ */
146
+ export interface MetadataBook extends Book<Map<string, Uint8Array>> {
147
+ /**
148
+ * Set a specific metadata value
149
+ */
150
+ setValue: (peerId: PeerId, key: string, value: Uint8Array) => Promise<void>
151
+
152
+ /**
153
+ * Get specific metadata value, if it exists
154
+ */
155
+ getValue: (peerId: PeerId, key: string) => Promise<Uint8Array | undefined>
156
+
157
+ /**
158
+ * Deletes the provided peer metadata key from the book
159
+ */
160
+ deleteValue: (peerId: PeerId, key: string) => Promise<void>
161
+ }
162
+
163
+ /**
164
+ * ProtoBook containing a map of peerIdStr to supported protocols.
165
+ */
166
+ export interface ProtoBook extends Book<string[]> {
167
+ /**
168
+ * Adds known protocols of a provided peer.
169
+ * If the peer was not known before, it will be added.
170
+ */
171
+ add: (peerId: PeerId, protocols: string[]) => Promise<void>
172
+
173
+ /**
174
+ * Removes known protocols of a provided peer.
175
+ * If the protocols did not exist before, nothing will be done.
176
+ */
177
+ remove: (peerId: PeerId, protocols: string[]) => Promise<void>
178
+ }
179
+
180
+ export interface PeerProtocolsChangeEvent {
181
+ peerId: PeerId
182
+ protocols: string[]
183
+ }
184
+
185
+ export interface PeerMultiaddrsChangeEvent {
186
+ peerId: PeerId
187
+ multiaddrs: Multiaddr[]
188
+ }
189
+
190
+ export interface PeerPublicKeyChangeEvent {
191
+ peerId: PeerId
192
+ pubKey?: PublicKey
193
+ }
194
+
195
+ export interface PeerMetadataChangeEvent {
196
+ peerId: PeerId
197
+ metadata: Map<string, Uint8Array>
198
+ }
199
+
200
+ export type EventName = 'peer' | 'change:protocols' | 'change:multiaddrs' | 'change:pubkey' | 'change:metadata'
201
+
202
+ export interface PeerStoreEvents {
203
+ 'peer': (event: PeerId) => void
204
+ 'change:protocols': (event: PeerProtocolsChangeEvent) => void
205
+ 'change:multiaddrs': (event: PeerMultiaddrsChangeEvent) => void
206
+ 'change:pubkey': (event: PeerPublicKeyChangeEvent) => void
207
+ 'change:metadata': (event: PeerMetadataChangeEvent) => void
208
+ }
209
+
210
+ export interface PeerStore {
211
+ addressBook: AddressBook
212
+ keyBook: KeyBook
213
+ metadataBook: MetadataBook
214
+ protoBook: ProtoBook
215
+
216
+ getPeers: () => AsyncIterable<Peer>
217
+ delete: (peerId: PeerId) => Promise<void>
218
+ has: (peerId: PeerId) => Promise<boolean>
219
+ get: (peerId: PeerId) => Promise<Peer>
220
+ on: <U extends keyof PeerStoreEvents>(
221
+ event: U, listener: PeerStoreEvents[U]
222
+ ) => this
223
+ once: <U extends keyof PeerStoreEvents>(
224
+ event: U, listener: PeerStoreEvents[U]
225
+ ) => this
226
+ emit: <U extends keyof PeerStoreEvents>(
227
+ event: U, ...args: Parameters<PeerStoreEvents[U]>
228
+ ) => boolean
229
+ }
230
+
231
+ export interface Store {
232
+ has: (peerId: PeerId) => Promise<boolean>
233
+ save: (peer: Peer) => Promise<Peer>
234
+ load: (peerId: PeerId) => Promise<Peer>
235
+ merge: (peerId: PeerId, data: Partial<Peer>) => Promise<Peer>
236
+ mergeOrCreate: (peerId: PeerId, data: Partial<Peer>) => Promise<Peer>
237
+ patch: (peerId: PeerId, data: Partial<Peer>) => Promise<Peer>
238
+ patchOrCreate: (peerId: PeerId, data: Partial<Peer>) => Promise<Peer>
239
+ all: () => AsyncIterable<Peer>
240
+
241
+ lock: {
242
+ readLock: () => Promise<() => void>
243
+ writeLock: () => Promise<() => void>
244
+ }
245
+ }
package/src/ping/index.js CHANGED
@@ -63,6 +63,9 @@ async function ping (node, peer) {
63
63
  */
64
64
  function mount (node) {
65
65
  node.handle(`/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`, ({ stream }) => pipe(stream, stream))
66
+ .catch(err => {
67
+ log.error(err)
68
+ })
66
69
  }
67
70
 
68
71
  /**
@@ -72,6 +75,9 @@ function mount (node) {
72
75
  */
73
76
  function unmount (node) {
74
77
  node.unhandle(`/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`)
78
+ .catch(err => {
79
+ log.error(err)
80
+ })
75
81
  }
76
82
 
77
83
  exports = module.exports = ping
@@ -11,7 +11,7 @@ const {
11
11
  } = require('./consts')
12
12
 
13
13
  /**
14
- * @typedef {import('../../peer-store/address-book.js').Address} Address
14
+ * @typedef {import('../../peer-store/types').Address} Address
15
15
  * @typedef {import('libp2p-interfaces/src/record/types').Record} Record
16
16
  */
17
17
 
@@ -19,7 +19,7 @@ async function updateSelfPeerRecord (libp2p) {
19
19
  multiaddrs: libp2p.multiaddrs
20
20
  })
21
21
  const envelope = await Envelope.seal(peerRecord, libp2p.peerId)
22
- libp2p.peerStore.addressBook.consumePeerRecord(envelope)
22
+ await libp2p.peerStore.addressBook.consumePeerRecord(envelope)
23
23
  }
24
24
 
25
25
  module.exports.updateSelfPeerRecord = updateSelfPeerRecord
package/src/registrar.js CHANGED
@@ -13,7 +13,7 @@ const Topology = require('libp2p-interfaces/src/topology')
13
13
 
14
14
  /**
15
15
  * @typedef {import('peer-id')} PeerId
16
- * @typedef {import('./peer-store')} PeerStore
16
+ * @typedef {import('./peer-store/types').PeerStore} PeerStore
17
17
  * @typedef {import('./connection-manager')} ConnectionManager
18
18
  * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
19
19
  * @typedef {import('./').HandlerProps} HandlerProps
@@ -82,9 +82,9 @@ class Registrar {
82
82
  * Register handlers for a set of multicodecs given
83
83
  *
84
84
  * @param {Topology} topology - protocol topology
85
- * @returns {string} registrar identifier
85
+ * @returns {Promise<string>} registrar identifier
86
86
  */
87
- register (topology) {
87
+ async register (topology) {
88
88
  if (!Topology.isTopology(topology)) {
89
89
  log.error('topology must be an instance of interfaces/topology')
90
90
  throw errcode(new Error('topology must be an instance of interfaces/topology'), ERR_INVALID_PARAMETERS)
@@ -96,7 +96,7 @@ class Registrar {
96
96
  this.topologies.set(id, topology)
97
97
 
98
98
  // Set registrar
99
- topology.registrar = this
99
+ await topology.setRegistrar(this)
100
100
 
101
101
  return id
102
102
  }
package/src/types.ts ADDED
@@ -0,0 +1,98 @@
1
+ import type PeerId from 'peer-id'
2
+ import type { Multiaddr } from 'multiaddr'
3
+ import type { MultiaddrConnection } from 'libp2p-interfaces/src/transport/types'
4
+
5
+ export interface ConnectionGater {
6
+ /**
7
+ * denyDialMultiaddr tests whether we're permitted to Dial the
8
+ * specified peer.
9
+ *
10
+ * This is called by the dialer.connectToPeer implementation before
11
+ * dialling a peer.
12
+ *
13
+ * Return true to prevent dialing the passed peer.
14
+ */
15
+ denyDialPeer: (peerId: PeerId) => Promise<boolean>
16
+
17
+ /**
18
+ * denyDialMultiaddr tests whether we're permitted to dial the specified
19
+ * multiaddr for the given peer.
20
+ *
21
+ * This is called by the dialer.connectToPeer implementation after it has
22
+ * resolved the peer's addrs, and prior to dialling each.
23
+ *
24
+ * Return true to prevent dialing the passed peer on the passed multiaddr.
25
+ */
26
+ denyDialMultiaddr: (peerId: PeerId, multiaddr: Multiaddr) => Promise<boolean>
27
+
28
+ /**
29
+ * denyInboundConnection tests whether an incipient inbound connection is allowed.
30
+ *
31
+ * This is called by the upgrader, or by the transport directly (e.g. QUIC,
32
+ * Bluetooth), straight after it has accepted a connection from its socket.
33
+ *
34
+ * Return true to deny the incoming passed connection.
35
+ */
36
+ denyInboundConnection: (maConn: MultiaddrConnection) => Promise<boolean>
37
+
38
+ /**
39
+ * denyOutboundConnection tests whether an incipient outbound connection is allowed.
40
+ *
41
+ * This is called by the upgrader, or by the transport directly (e.g. QUIC,
42
+ * Bluetooth), straight after it has created a connection with its socket.
43
+ *
44
+ * Return true to deny the incoming passed connection.
45
+ */
46
+ denyOutboundConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
47
+
48
+ /**
49
+ * denyInboundEncryptedConnection tests whether a given connection, now encrypted,
50
+ * is allowed.
51
+ *
52
+ * This is called by the upgrader, after it has performed the security
53
+ * handshake, and before it negotiates the muxer, or by the directly by the
54
+ * transport, at the exact same checkpoint.
55
+ *
56
+ * Return true to deny the passed secured connection.
57
+ */
58
+ denyInboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
59
+
60
+ /**
61
+ * denyOutboundEncryptedConnection tests whether a given connection, now encrypted,
62
+ * is allowed.
63
+ *
64
+ * This is called by the upgrader, after it has performed the security
65
+ * handshake, and before it negotiates the muxer, or by the directly by the
66
+ * transport, at the exact same checkpoint.
67
+ *
68
+ * Return true to deny the passed secured connection.
69
+ */
70
+ denyOutboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
71
+
72
+ /**
73
+ * denyInboundUpgradedConnection tests whether a fully capable connection is allowed.
74
+ *
75
+ * This is called after encryption has been negotiated and the connection has been
76
+ * multiplexed, if a multiplexer is configured.
77
+ *
78
+ * Return true to deny the passed upgraded connection.
79
+ */
80
+ denyInboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
81
+
82
+ /**
83
+ * denyOutboundUpgradedConnection tests whether a fully capable connection is allowed.
84
+ *
85
+ * This is called after encryption has been negotiated and the connection has been
86
+ * multiplexed, if a multiplexer is configured.
87
+ *
88
+ * Return true to deny the passed upgraded connection.
89
+ */
90
+ denyOutboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
91
+
92
+ /**
93
+ * Used by the address book to filter passed addresses.
94
+ *
95
+ * Return true to allow storing the passed multiaddr for the passed peer.
96
+ */
97
+ filterMultiaddrForPeer: (peer: PeerId, multiaddr: Multiaddr) => Promise<boolean>
98
+ }
package/src/upgrader.js CHANGED
@@ -5,7 +5,6 @@ const log = Object.assign(debug('libp2p:upgrader'), {
5
5
  error: debug('libp2p:upgrader:err')
6
6
  })
7
7
  const errCode = require('err-code')
8
- // @ts-ignore multistream-select does not export types
9
8
  const Multistream = require('multistream-select')
10
9
  const { Connection } = require('libp2p-interfaces/src/connection')
11
10
  const PeerId = require('peer-id')
@@ -23,6 +22,7 @@ const { codes } = require('./errors')
23
22
  * @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto
24
23
  * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
25
24
  * @typedef {import('multiaddr').Multiaddr} Multiaddr
25
+ * @typedef {import('./types').ConnectionGater} ConnectionGater
26
26
  */
27
27
 
28
28
  /**
@@ -36,6 +36,8 @@ class Upgrader {
36
36
  /**
37
37
  * @param {object} options
38
38
  * @param {PeerId} options.localPeer
39
+ * @param {ConnectionGater} options.connectionGater
40
+ *
39
41
  * @param {import('./metrics')} [options.metrics]
40
42
  * @param {Map<string, Crypto>} [options.cryptos]
41
43
  * @param {Map<string, MuxerFactory>} [options.muxers]
@@ -45,11 +47,13 @@ class Upgrader {
45
47
  constructor ({
46
48
  localPeer,
47
49
  metrics,
50
+ connectionGater,
48
51
  cryptos = new Map(),
49
52
  muxers = new Map(),
50
53
  onConnectionEnd = () => {},
51
54
  onConnection = () => {}
52
55
  }) {
56
+ this.connectionGater = connectionGater
53
57
  this.localPeer = localPeer
54
58
  this.metrics = metrics
55
59
  this.cryptos = cryptos
@@ -77,6 +81,10 @@ class Upgrader {
77
81
  let setPeer
78
82
  let proxyPeer
79
83
 
84
+ if (await this.connectionGater.denyInboundConnection(maConn)) {
85
+ throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
86
+ }
87
+
80
88
  if (this.metrics) {
81
89
  ({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
82
90
  const idString = (Math.random() * 1e9).toString(36) + Date.now()
@@ -100,6 +108,10 @@ class Upgrader {
100
108
  protocol: cryptoProtocol
101
109
  } = await this._encryptInbound(this.localPeer, protectedConn, this.cryptos))
102
110
 
111
+ if (await this.connectionGater.denyInboundEncryptedConnection(remotePeer, encryptedConn)) {
112
+ throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
113
+ }
114
+
103
115
  // Multiplex the connection
104
116
  if (this.muxers.size) {
105
117
  ({ stream: upgradedConn, Muxer } = await this._multiplexInbound(encryptedConn, this.muxers))
@@ -112,6 +124,10 @@ class Upgrader {
112
124
  throw err
113
125
  }
114
126
 
127
+ if (await this.connectionGater.denyInboundUpgradedConnection(remotePeer, encryptedConn)) {
128
+ throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
129
+ }
130
+
115
131
  if (this.metrics) {
116
132
  this.metrics.updatePlaceholder(proxyPeer, remotePeer)
117
133
  setPeer(remotePeer)
@@ -144,6 +160,10 @@ class Upgrader {
144
160
 
145
161
  const remotePeerId = PeerId.createFromB58String(idStr)
146
162
 
163
+ if (await this.connectionGater.denyOutboundConnection(remotePeerId, maConn)) {
164
+ throw errCode(new Error('The multiaddr connection is blocked by connectionGater.denyOutboundConnection'), codes.ERR_CONNECTION_INTERCEPTED)
165
+ }
166
+
147
167
  let encryptedConn
148
168
  let remotePeer
149
169
  let upgradedConn
@@ -175,6 +195,10 @@ class Upgrader {
175
195
  protocol: cryptoProtocol
176
196
  } = await this._encryptOutbound(this.localPeer, protectedConn, remotePeerId, this.cryptos))
177
197
 
198
+ if (await this.connectionGater.denyOutboundEncryptedConnection(remotePeer, encryptedConn)) {
199
+ throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
200
+ }
201
+
178
202
  // Multiplex the connection
179
203
  if (this.muxers.size) {
180
204
  ({ stream: upgradedConn, Muxer } = await this._multiplexOutbound(encryptedConn, this.muxers))
@@ -187,6 +211,10 @@ class Upgrader {
187
211
  throw err
188
212
  }
189
213
 
214
+ if (await this.connectionGater.denyOutboundUpgradedConnection(remotePeer, encryptedConn)) {
215
+ throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
216
+ }
217
+
190
218
  if (this.metrics) {
191
219
  this.metrics.updatePlaceholder(proxyPeer, remotePeer)
192
220
  setPeer(remotePeer)
@@ -288,7 +316,9 @@ class Upgrader {
288
316
  } finally {
289
317
  this.onConnectionEnd(connection)
290
318
  }
291
- })()
319
+ })().catch(err => {
320
+ log.error(err)
321
+ })
292
322
  }
293
323
 
294
324
  return Reflect.set(...args)
@@ -1,81 +0,0 @@
1
- export = Book;
2
- /**
3
- * @typedef {import('./')} PeerStore
4
- */
5
- declare class Book {
6
- /**
7
- * The Book is the skeleton for the PeerStore books.
8
- *
9
- * @class
10
- * @param {Object} properties
11
- * @param {PeerStore} properties.peerStore - PeerStore instance.
12
- * @param {string} properties.eventName - Name of the event to emit by the PeerStore.
13
- * @param {string} properties.eventProperty - Name of the property to emit by the PeerStore.
14
- * @param {(data: any) => any[]} [properties.eventTransformer] - Transformer function of the provided data for being emitted.
15
- */
16
- constructor({ peerStore, eventName, eventProperty, eventTransformer }: {
17
- peerStore: PeerStore;
18
- eventName: string;
19
- eventProperty: string;
20
- eventTransformer?: ((data: any) => any[]) | undefined;
21
- });
22
- _ps: import("./");
23
- eventName: string;
24
- eventProperty: string;
25
- eventTransformer: (data: any) => any[];
26
- /**
27
- * Map known peers to their data.
28
- *
29
- * @type {Map<string, any[]|any>}
30
- */
31
- data: Map<string, any[] | any>;
32
- /**
33
- * Set known data of a provided peer.
34
- *
35
- * @param {PeerId} peerId
36
- * @param {any[]|any} data
37
- */
38
- set(peerId: PeerId, data: any[] | any): void;
39
- /**
40
- * Set data into the datastructure, persistence and emit it using the provided transformers.
41
- *
42
- * @protected
43
- * @param {PeerId} peerId - peerId of the data to store
44
- * @param {any} data - data to store.
45
- * @param {Object} [options] - storing options.
46
- * @param {boolean} [options.emit = true] - emit the provided data.
47
- * @returns {void}
48
- */
49
- protected _setData(peerId: PeerId, data: any, { emit }?: {
50
- emit?: boolean | undefined;
51
- } | undefined): void;
52
- /**
53
- * Emit data.
54
- *
55
- * @protected
56
- * @param {PeerId} peerId
57
- * @param {any} [data]
58
- */
59
- protected _emit(peerId: PeerId, data?: any): void;
60
- /**
61
- * Get the known data of a provided peer.
62
- * Returns `undefined` if there is no available data for the given peer.
63
- *
64
- * @param {PeerId} peerId
65
- * @returns {any[]|any|undefined}
66
- */
67
- get(peerId: PeerId): any[] | any | undefined;
68
- /**
69
- * Deletes the provided peer from the book.
70
- *
71
- * @param {PeerId} peerId
72
- * @returns {boolean}
73
- */
74
- delete(peerId: PeerId): boolean;
75
- }
76
- declare namespace Book {
77
- export { PeerStore };
78
- }
79
- import PeerId = require("peer-id");
80
- type PeerStore = import('./');
81
- //# sourceMappingURL=book.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"book.d.ts","sourceRoot":"","sources":["../../../src/peer-store/book.js"],"names":[],"mappings":";AAWA;;GAEG;AAEH;IACE;;;;;;;;;OASG;IACH;QALiC,SAAS,EAA/B,SAAS;QACU,SAAS,EAA5B,MAAM;QACa,aAAa,EAAhC,MAAM;QAC4B,gBAAgB,WAA3C,GAAG,KAAK,GAAG,EAAE;OAc9B;IAXC,kBAAoB;IACpB,kBAA0B;IAC1B,sBAAkC;IAClC,yBANgB,GAAG,KAAK,GAAG,EAAE,CAMW;IAExC;;;;OAIG;IACH,MAFU,IAAI,MAAM,EAAE,GAAG,EAAE,GAAC,GAAG,CAAC,CAEX;IAGvB;;;;;OAKG;IACH,YAHW,MAAM,QACN,GAAG,EAAE,GAAC,GAAG,QAInB;IAED;;;;;;;;;OASG;IACH,2BANW,MAAM,QACN,GAAG;;oBAGD,IAAI,CAUhB;IAED;;;;;;OAMG;IACH,wBAHW,MAAM,SACN,GAAG,QAOb;IAED;;;;;;OAMG;IACH,YAHW,MAAM,GACJ,GAAG,EAAE,GAAC,GAAG,GAAC,SAAS,CAW/B;IAED;;;;;OAKG;IACH,eAHW,MAAM,GACJ,OAAO,CAcnB;CACF;;;;;iBA7GY,OAAO,IAAI,CAAC"}
@@ -1,6 +0,0 @@
1
- export var NAMESPACE_COMMON: string;
2
- export var NAMESPACE_ADDRESS: string;
3
- export var NAMESPACE_KEYS: string;
4
- export var NAMESPACE_METADATA: string;
5
- export var NAMESPACE_PROTOCOL: string;
6
- //# sourceMappingURL=consts.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../../../../src/peer-store/persistent/consts.js"],"names":[],"mappings":""}