libp2p 0.44.0-ba47c957 → 0.44.0-e3deaa40

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 (112) hide show
  1. package/dist/index.min.js +23 -23
  2. package/dist/src/autonat/index.d.ts.map +1 -1
  3. package/dist/src/autonat/index.js +15 -13
  4. package/dist/src/autonat/index.js.map +1 -1
  5. package/dist/src/components.d.ts +2 -66
  6. package/dist/src/components.d.ts.map +1 -1
  7. package/dist/src/components.js +60 -174
  8. package/dist/src/components.js.map +1 -1
  9. package/dist/src/config/connection-gater.browser.d.ts +8 -0
  10. package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
  11. package/dist/src/config/connection-gater.browser.js +27 -0
  12. package/dist/src/config/connection-gater.browser.js.map +1 -0
  13. package/dist/src/config/connection-gater.d.ts +6 -0
  14. package/dist/src/config/connection-gater.d.ts.map +1 -0
  15. package/dist/src/config/connection-gater.js +18 -0
  16. package/dist/src/config/connection-gater.js.map +1 -0
  17. package/dist/src/config.d.ts +2 -1
  18. package/dist/src/config.d.ts.map +1 -1
  19. package/dist/src/config.js +16 -23
  20. package/dist/src/config.js.map +1 -1
  21. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  22. package/dist/src/connection-manager/auto-dial.js +7 -2
  23. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  24. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  25. package/dist/src/connection-manager/dial-queue.js +8 -10
  26. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  27. package/dist/src/connection-manager/index.d.ts.map +1 -1
  28. package/dist/src/connection-manager/index.js +3 -0
  29. package/dist/src/connection-manager/index.js.map +1 -1
  30. package/dist/src/content-routing/index.d.ts +0 -10
  31. package/dist/src/content-routing/index.d.ts.map +1 -1
  32. package/dist/src/content-routing/index.js +6 -41
  33. package/dist/src/content-routing/index.js.map +1 -1
  34. package/dist/src/fetch/index.d.ts +13 -35
  35. package/dist/src/fetch/index.d.ts.map +1 -1
  36. package/dist/src/fetch/index.js +7 -9
  37. package/dist/src/fetch/index.js.map +1 -1
  38. package/dist/src/identify/index.d.ts +13 -49
  39. package/dist/src/identify/index.d.ts.map +1 -1
  40. package/dist/src/identify/index.js +122 -96
  41. package/dist/src/identify/index.js.map +1 -1
  42. package/dist/src/index.d.ts +11 -45
  43. package/dist/src/index.d.ts.map +1 -1
  44. package/dist/src/index.js.map +1 -1
  45. package/dist/src/libp2p.d.ts +10 -35
  46. package/dist/src/libp2p.d.ts.map +1 -1
  47. package/dist/src/libp2p.js +110 -171
  48. package/dist/src/libp2p.js.map +1 -1
  49. package/dist/src/peer-routing.d.ts.map +1 -1
  50. package/dist/src/peer-routing.js +7 -6
  51. package/dist/src/peer-routing.js.map +1 -1
  52. package/dist/src/ping/constants.d.ts +4 -0
  53. package/dist/src/ping/constants.d.ts.map +1 -1
  54. package/dist/src/ping/constants.js +10 -0
  55. package/dist/src/ping/constants.js.map +1 -1
  56. package/dist/src/ping/index.d.ts +10 -27
  57. package/dist/src/ping/index.d.ts.map +1 -1
  58. package/dist/src/ping/index.js +18 -21
  59. package/dist/src/ping/index.js.map +1 -1
  60. package/dist/src/registrar.js +2 -2
  61. package/dist/src/registrar.js.map +1 -1
  62. package/dist/src/transport-manager.d.ts +2 -1
  63. package/dist/src/transport-manager.d.ts.map +1 -1
  64. package/dist/src/transport-manager.js +4 -2
  65. package/dist/src/transport-manager.js.map +1 -1
  66. package/dist/src/upgrader.d.ts.map +1 -1
  67. package/dist/src/upgrader.js +7 -14
  68. package/dist/src/upgrader.js.map +1 -1
  69. package/dist/src/upnp-nat/index.d.ts +42 -0
  70. package/dist/src/upnp-nat/index.d.ts.map +1 -0
  71. package/dist/src/{nat-manager.js → upnp-nat/index.js} +11 -7
  72. package/dist/src/upnp-nat/index.js.map +1 -0
  73. package/package.json +32 -17
  74. package/src/autonat/index.ts +14 -13
  75. package/src/components.ts +67 -248
  76. package/src/config/connection-gater.browser.ts +31 -0
  77. package/src/config/connection-gater.ts +19 -0
  78. package/src/config.ts +8 -14
  79. package/src/connection-manager/auto-dial.ts +8 -2
  80. package/src/connection-manager/dial-queue.ts +10 -16
  81. package/src/connection-manager/index.ts +4 -0
  82. package/src/content-routing/index.ts +6 -54
  83. package/src/fetch/index.ts +51 -14
  84. package/src/identify/index.ts +154 -104
  85. package/src/index.ts +12 -52
  86. package/src/libp2p.ts +102 -214
  87. package/src/peer-routing.ts +10 -8
  88. package/src/ping/constants.ts +11 -0
  89. package/src/ping/index.ts +32 -28
  90. package/src/registrar.ts +2 -2
  91. package/src/transport-manager.ts +5 -3
  92. package/src/upgrader.ts +7 -13
  93. package/src/{nat-manager.ts → upnp-nat/index.ts} +15 -16
  94. package/src/version.ts +1 -1
  95. package/dist/src/dht/dht-content-routing.d.ts +0 -17
  96. package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
  97. package/dist/src/dht/dht-content-routing.js +0 -32
  98. package/dist/src/dht/dht-content-routing.js.map +0 -1
  99. package/dist/src/dht/dht-peer-routing.d.ts +0 -15
  100. package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
  101. package/dist/src/dht/dht-peer-routing.js +0 -26
  102. package/dist/src/dht/dht-peer-routing.js.map +0 -1
  103. package/dist/src/dht/dummy-dht.d.ts +0 -20
  104. package/dist/src/dht/dummy-dht.d.ts.map +0 -1
  105. package/dist/src/dht/dummy-dht.js +0 -46
  106. package/dist/src/dht/dummy-dht.js.map +0 -1
  107. package/dist/src/nat-manager.d.ts +0 -74
  108. package/dist/src/nat-manager.d.ts.map +0 -1
  109. package/dist/src/nat-manager.js.map +0 -1
  110. package/src/dht/dht-content-routing.ts +0 -44
  111. package/src/dht/dht-peer-routing.ts +0 -36
  112. package/src/dht/dummy-dht.ts +0 -60
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2p",
3
- "version": "0.44.0-ba47c957",
3
+ "version": "0.44.0-e3deaa40",
4
4
  "description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p#readme",
@@ -52,14 +52,34 @@
52
52
  "types": "./dist/src/index.d.ts",
53
53
  "import": "./dist/src/index.js"
54
54
  },
55
+ "./autonat": {
56
+ "types": "./dist/src/autonat/index.d.ts",
57
+ "import": "./dist/src/autonat/index.js"
58
+ },
55
59
  "./circuit-relay": {
56
60
  "types": "./dist/src/circuit-relay/index.d.ts",
57
61
  "import": "./dist/src/circuit-relay/index.js"
58
62
  },
63
+ "./fetch": {
64
+ "types": "./dist/src/fetch/index.d.ts",
65
+ "import": "./dist/src/fetch/index.js"
66
+ },
67
+ "./identify": {
68
+ "types": "./dist/src/identify/index.d.ts",
69
+ "import": "./dist/src/identify/index.js"
70
+ },
59
71
  "./insecure": {
60
72
  "types": "./dist/src/insecure/index.d.ts",
61
73
  "import": "./dist/src/insecure/index.js"
62
74
  },
75
+ "./upnp-nat": {
76
+ "types": "./dist/src/upnp-nat/index.d.ts",
77
+ "import": "./dist/src/upnp-nat/index.js"
78
+ },
79
+ "./ping": {
80
+ "types": "./dist/src/ping/index.d.ts",
81
+ "import": "./dist/src/ping/index.js"
82
+ },
63
83
  "./pnet": {
64
84
  "types": "./dist/src/pnet/index.d.ts",
65
85
  "import": "./dist/src/pnet/index.js"
@@ -106,15 +126,14 @@
106
126
  "@libp2p/interface-connection-encrypter": "^4.0.0",
107
127
  "@libp2p/interface-connection-gater": "^3.0.0",
108
128
  "@libp2p/interface-connection-manager": "^3.0.0",
109
- "@libp2p/interface-content-routing": "^2.0.0",
110
- "@libp2p/interface-dht": "^2.0.0",
129
+ "@libp2p/interface-content-routing": "^2.1.0",
111
130
  "@libp2p/interface-keychain": "^2.0.4",
112
- "@libp2p/interface-libp2p": "^2.0.0",
131
+ "@libp2p/interface-libp2p": "^3.0.0",
113
132
  "@libp2p/interface-metrics": "^4.0.0",
114
- "@libp2p/interface-peer-discovery": "^1.0.1",
133
+ "@libp2p/interface-peer-discovery": "^2.0.0",
115
134
  "@libp2p/interface-peer-id": "^2.0.1",
116
135
  "@libp2p/interface-peer-info": "^1.0.3",
117
- "@libp2p/interface-peer-routing": "^1.0.1",
136
+ "@libp2p/interface-peer-routing": "^1.1.0",
118
137
  "@libp2p/interface-peer-store": "^2.0.0",
119
138
  "@libp2p/interface-pubsub": "^4.0.0",
120
139
  "@libp2p/interface-record": "^2.0.6",
@@ -155,41 +174,36 @@
155
174
  "merge-options": "^3.0.4",
156
175
  "multiformats": "^11.0.0",
157
176
  "p-defer": "^4.0.0",
158
- "p-fifo": "^1.0.0",
159
177
  "p-queue": "^7.3.4",
160
178
  "p-retry": "^5.0.0",
161
179
  "private-ip": "^3.0.0",
162
180
  "protons-runtime": "^5.0.0",
163
181
  "rate-limiter-flexible": "^2.3.11",
164
- "retimer": "^3.0.0",
165
182
  "set-delayed-interval": "^1.0.0",
166
- "timeout-abort-controller": "^3.0.0",
167
183
  "uint8arraylist": "^2.3.2",
168
184
  "uint8arrays": "^4.0.2",
169
185
  "wherearewe": "^2.0.0",
170
- "why-is-node-running": "^2.2.2",
171
186
  "xsalsa20": "^1.1.0"
172
187
  },
173
188
  "devDependencies": {
174
189
  "@chainsafe/libp2p-gossipsub": "^7.0.0",
175
190
  "@chainsafe/libp2p-noise": "^11.0.0",
176
191
  "@chainsafe/libp2p-yamux": "^4.0.0",
177
- "@libp2p/bootstrap": "^7.0.0",
178
- "@libp2p/daemon-client": "^6.0.0",
179
- "@libp2p/daemon-server": "^5.0.0",
192
+ "@libp2p/bootstrap": "^8.0.0",
193
+ "@libp2p/daemon-client": "^6.0.2",
194
+ "@libp2p/daemon-server": "^5.0.2",
180
195
  "@libp2p/floodsub": "^7.0.1",
181
196
  "@libp2p/interface-compliance-tests": "^3.0.6",
182
197
  "@libp2p/interface-connection-compliance-tests": "^2.0.8",
183
198
  "@libp2p/interface-connection-encrypter-compliance-tests": "^5.0.0",
184
- "@libp2p/interface-mocks": "^11.0.0",
199
+ "@libp2p/interface-mocks": "^12.0.0",
185
200
  "@libp2p/interop": "^8.0.0",
186
- "@libp2p/kad-dht": "^9.0.0",
187
- "@libp2p/mdns": "^7.0.0",
201
+ "@libp2p/kad-dht": "^9.1.4",
202
+ "@libp2p/mdns": "^8.0.0",
188
203
  "@libp2p/mplex": "^8.0.1",
189
204
  "@libp2p/pubsub": "^7.0.1",
190
205
  "@libp2p/tcp": "^7.0.1",
191
206
  "@libp2p/websockets": "^6.0.1",
192
- "@types/p-fifo": "^1.0.0",
193
207
  "@types/varint": "^6.0.0",
194
208
  "@types/xsalsa20": "^1.1.0",
195
209
  "aegir": "^38.1.7",
@@ -208,6 +222,7 @@
208
222
  "sinon-ts": "^1.0.0"
209
223
  },
210
224
  "browser": {
225
+ "./dist/src/config/connection-gater.js": "./dist/src/config/connection-gater.browser.js",
211
226
  "nat-api": false
212
227
  }
213
228
  }
@@ -19,11 +19,11 @@ import map from 'it-map'
19
19
  import parallel from 'it-parallel'
20
20
  import { pipe } from 'it-pipe'
21
21
  import isPrivateIp from 'private-ip'
22
- import { TimeoutController } from 'timeout-abort-controller'
23
22
  import {
24
23
  PROTOCOL
25
24
  } from './constants.js'
26
25
  import { Message } from './pb/index.js'
26
+ import { anySignal } from 'any-signal'
27
27
 
28
28
  const log = logger('libp2p:autonat')
29
29
 
@@ -132,20 +132,20 @@ export class AutonatService implements Startable {
132
132
  * Handle an incoming autonat request
133
133
  */
134
134
  async handleIncomingAutonatStream (data: IncomingStreamData): Promise<void> {
135
- const controller = new TimeoutController(this._init.timeout)
135
+ const signal = anySignal([AbortSignal.timeout(this._init.timeout)])
136
136
 
137
137
  // this controller may be used while dialing lots of peers so prevent MaxListenersExceededWarning
138
138
  // appearing in the console
139
139
  try {
140
140
  // fails on node < 15.4
141
- setMaxListeners?.(Infinity, controller.signal)
141
+ setMaxListeners?.(Infinity, signal)
142
142
  } catch {}
143
143
 
144
144
  const ourHosts = this.components.addressManager.getAddresses()
145
145
  .map(ma => ma.toOptions().host)
146
146
 
147
147
  try {
148
- const source = abortableDuplex(data.stream, controller.signal)
148
+ const source = abortableDuplex(data.stream, signal)
149
149
  const self = this
150
150
 
151
151
  await pipe(
@@ -319,7 +319,7 @@ export class AutonatService implements Startable {
319
319
 
320
320
  try {
321
321
  connection = await self.components.connectionManager.openConnection(multiaddr, {
322
- signal: controller.signal
322
+ signal
323
323
  })
324
324
 
325
325
  if (!connection.remoteAddr.equals(multiaddr)) {
@@ -362,8 +362,10 @@ export class AutonatService implements Startable {
362
362
  // can't tell the remote when a dial timed out..
363
363
  data.stream
364
364
  )
365
+ } catch (err) {
366
+ log.error(err)
365
367
  } finally {
366
- controller.clear()
368
+ signal.clear()
367
369
  }
368
370
  }
369
371
 
@@ -401,13 +403,13 @@ export class AutonatService implements Startable {
401
403
  return
402
404
  }
403
405
 
404
- const controller = new TimeoutController(this._init.timeout)
406
+ const signal = AbortSignal.timeout(this._init.timeout)
405
407
 
406
408
  // this controller may be used while dialing lots of peers so prevent MaxListenersExceededWarning
407
409
  // appearing in the console
408
410
  try {
409
411
  // fails on node < 15.4
410
- setMaxListeners?.(Infinity, controller.signal)
412
+ setMaxListeners?.(Infinity, signal)
411
413
  } catch {}
412
414
 
413
415
  const self = this
@@ -436,13 +438,13 @@ export class AutonatService implements Startable {
436
438
  log('Asking %p to verify multiaddr', peer.id)
437
439
 
438
440
  const connection = await self.components.connectionManager.openConnection(peer.id, {
439
- signal: controller.signal
441
+ signal
440
442
  })
441
443
 
442
444
  const stream = await connection.newStream(PROTOCOL, {
443
- signal: controller.signal
445
+ signal
444
446
  })
445
- const source = abortableDuplex(stream, controller.signal)
447
+ const source = abortableDuplex(stream, signal)
446
448
 
447
449
  const buf = await pipe(
448
450
  [request],
@@ -493,7 +495,7 @@ export class AutonatService implements Startable {
493
495
  }
494
496
 
495
497
  for await (const dialResponse of parallel(map(this.components.peerRouting.getClosestPeers(randomCid, {
496
- signal: controller.signal
498
+ signal
497
499
  }), (peer) => async () => await verifyAddress(peer)), {
498
500
  concurrency: REQUIRED_SUCCESSFUL_DIALS
499
501
  })) {
@@ -557,7 +559,6 @@ export class AutonatService implements Startable {
557
559
  }
558
560
  }
559
561
  } finally {
560
- controller.clear()
561
562
  this.verifyAddressTimeout = setTimeout(this._verifyExternalAddresses, this.refreshInterval)
562
563
  }
563
564
  }
package/src/components.ts CHANGED
@@ -10,14 +10,12 @@ import type { PeerStore } from '@libp2p/interface-peer-store'
10
10
  import type { Registrar } from '@libp2p/interface-registrar'
11
11
  import type { TransportManager, Upgrader } from '@libp2p/interface-transport'
12
12
  import type { Datastore } from 'interface-datastore'
13
- import type { PubSub } from '@libp2p/interface-pubsub'
14
- import type { DualDHT } from '@libp2p/interface-dht'
15
13
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
16
14
  import type { ConnectionGater } from '@libp2p/interface-connection-gater'
17
15
  import type { Libp2pEvents } from '@libp2p/interface-libp2p'
18
16
  import type { EventEmitter } from '@libp2p/interfaces/events'
19
17
 
20
- export interface Components {
18
+ export interface Components extends Record<string, any>, Startable {
21
19
  peerId: PeerId
22
20
  events: EventEmitter<Libp2pEvents>
23
21
  addressManager: AddressManager
@@ -32,8 +30,6 @@ export interface Components {
32
30
  datastore: Datastore
33
31
  connectionProtector?: ConnectionProtector
34
32
  metrics?: Metrics
35
- dht?: DualDHT
36
- pubsub?: PubSub
37
33
  }
38
34
 
39
35
  export interface ComponentsInit {
@@ -51,285 +47,108 @@ export interface ComponentsInit {
51
47
  peerRouting?: PeerRouting
52
48
  datastore?: Datastore
53
49
  connectionProtector?: ConnectionProtector
54
- dht?: DualDHT
55
- pubsub?: PubSub
56
50
  }
57
51
 
58
- export class DefaultComponents implements Components, Startable {
59
- private _peerId?: PeerId
60
- private _events?: EventEmitter<Libp2pEvents>
61
- private _addressManager?: AddressManager
62
- private _peerStore?: PeerStore
63
- private _upgrader?: Upgrader
64
- private _metrics?: Metrics
65
- private _registrar?: Registrar
66
- private _connectionManager?: ConnectionManager
67
- private _transportManager?: TransportManager
68
- private _connectionGater?: ConnectionGater
69
- private _contentRouting?: ContentRouting
70
- private _peerRouting?: PeerRouting
71
- private _datastore?: Datastore
72
- private _connectionProtector?: ConnectionProtector
73
- private _dht?: DualDHT
74
- private _pubsub?: PubSub
52
+ class DefaultComponents implements Startable {
53
+ public components: Record<string, any> = {}
75
54
  private _started = false
76
55
 
77
56
  constructor (init: ComponentsInit = {}) {
78
- this._peerId = init.peerId
79
- this._events = init.events
80
- this._addressManager = init.addressManager
81
- this._peerStore = init.peerStore
82
- this._upgrader = init.upgrader
83
- this._metrics = init.metrics
84
- this._registrar = init.registrar
85
- this._connectionManager = init.connectionManager
86
- this._transportManager = init.transportManager
87
- this._connectionGater = init.connectionGater
88
- this._contentRouting = init.contentRouting
89
- this._peerRouting = init.peerRouting
90
- this._datastore = init.datastore
91
- this._connectionProtector = init.connectionProtector
92
- this._dht = init.dht
93
- this._pubsub = init.pubsub
57
+ this.components = {}
58
+
59
+ for (const [key, value] of Object.entries(init)) {
60
+ this.components[key] = value
61
+ }
94
62
  }
95
63
 
96
64
  isStarted (): boolean {
97
65
  return this._started
98
66
  }
99
67
 
100
- async beforeStart (): Promise<void> {
68
+ private async _invokeStartableMethod (methodName: 'beforeStart' | 'start' | 'afterStart' | 'beforeStop' | 'stop' | 'afterStop'): Promise<void> {
101
69
  await Promise.all(
102
- Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
103
- if (startable.beforeStart != null) {
104
- await startable.beforeStart()
105
- }
106
- })
70
+ Object.values(this.components)
71
+ .filter(obj => isStartable(obj))
72
+ .map(async (startable: Startable) => {
73
+ await startable[methodName]?.()
74
+ })
107
75
  )
108
76
  }
109
77
 
110
- async start (): Promise<void> {
111
- await Promise.all(
112
- Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
113
- await startable.start()
114
- })
115
- )
78
+ async beforeStart (): Promise<void> {
79
+ await this._invokeStartableMethod('beforeStart')
80
+ }
116
81
 
82
+ async start (): Promise<void> {
83
+ await this._invokeStartableMethod('start')
117
84
  this._started = true
118
85
  }
119
86
 
120
87
  async afterStart (): Promise<void> {
121
- await Promise.all(
122
- Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
123
- if (startable.afterStart != null) {
124
- await startable.afterStart()
125
- }
126
- })
127
- )
88
+ await this._invokeStartableMethod('afterStart')
128
89
  }
129
90
 
130
91
  async beforeStop (): Promise<void> {
131
- await Promise.all(
132
- Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
133
- if (startable.beforeStop != null) {
134
- await startable.beforeStop()
135
- }
136
- })
137
- )
92
+ await this._invokeStartableMethod('beforeStop')
138
93
  }
139
94
 
140
95
  async stop (): Promise<void> {
141
- await Promise.all(
142
- Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
143
- await startable.stop()
144
- })
145
- )
146
-
96
+ await this._invokeStartableMethod('stop')
147
97
  this._started = false
148
98
  }
149
99
 
150
100
  async afterStop (): Promise<void> {
151
- await Promise.all(
152
- Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => {
153
- if (startable.afterStop != null) {
154
- await startable.afterStop()
155
- }
156
- })
157
- )
158
- }
159
-
160
- get peerId (): PeerId {
161
- if (this._peerId == null) {
162
- throw new CodeError('peerId not set', 'ERR_SERVICE_MISSING')
163
- }
164
-
165
- return this._peerId
166
- }
167
-
168
- set peerId (peerId: PeerId) {
169
- this._peerId = peerId
170
- }
171
-
172
- get events (): EventEmitter<Libp2pEvents> {
173
- if (this._events == null) {
174
- throw new CodeError('events not set', 'ERR_SERVICE_MISSING')
175
- }
176
-
177
- return this._events
178
- }
179
-
180
- set events (events: EventEmitter<Libp2pEvents>) {
181
- this._events = events
182
- }
183
-
184
- get addressManager (): AddressManager {
185
- if (this._addressManager == null) {
186
- throw new CodeError('addressManager not set', 'ERR_SERVICE_MISSING')
187
- }
188
-
189
- return this._addressManager
190
- }
191
-
192
- set addressManager (addressManager: AddressManager) {
193
- this._addressManager = addressManager
194
- }
195
-
196
- get peerStore (): PeerStore {
197
- if (this._peerStore == null) {
198
- throw new CodeError('peerStore not set', 'ERR_SERVICE_MISSING')
199
- }
200
-
201
- return this._peerStore
202
- }
203
-
204
- set peerStore (peerStore: PeerStore) {
205
- this._peerStore = peerStore
206
- }
207
-
208
- get upgrader (): Upgrader {
209
- if (this._upgrader == null) {
210
- throw new CodeError('upgrader not set', 'ERR_SERVICE_MISSING')
211
- }
212
-
213
- return this._upgrader
214
- }
215
-
216
- set upgrader (upgrader: Upgrader) {
217
- this._upgrader = upgrader
218
- }
219
-
220
- get registrar (): Registrar {
221
- if (this._registrar == null) {
222
- throw new CodeError('registrar not set', 'ERR_SERVICE_MISSING')
223
- }
224
-
225
- return this._registrar
226
- }
227
-
228
- set registrar (registrar: Registrar) {
229
- this._registrar = registrar
230
- }
231
-
232
- get connectionManager (): ConnectionManager {
233
- if (this._connectionManager == null) {
234
- throw new CodeError('connectionManager not set', 'ERR_SERVICE_MISSING')
235
- }
236
-
237
- return this._connectionManager
238
- }
239
-
240
- set connectionManager (connectionManager: ConnectionManager) {
241
- this._connectionManager = connectionManager
242
- }
243
-
244
- get transportManager (): TransportManager {
245
- if (this._transportManager == null) {
246
- throw new CodeError('transportManager not set', 'ERR_SERVICE_MISSING')
247
- }
248
-
249
- return this._transportManager
250
- }
251
-
252
- set transportManager (transportManager: TransportManager) {
253
- this._transportManager = transportManager
254
- }
255
-
256
- get connectionGater (): ConnectionGater {
257
- if (this._connectionGater == null) {
258
- throw new CodeError('connectionGater not set', 'ERR_SERVICE_MISSING')
259
- }
260
-
261
- return this._connectionGater
262
- }
263
-
264
- set connectionGater (connectionGater: ConnectionGater) {
265
- this._connectionGater = connectionGater
266
- }
267
-
268
- get contentRouting (): ContentRouting {
269
- if (this._contentRouting == null) {
270
- throw new CodeError('contentRouting not set', 'ERR_SERVICE_MISSING')
271
- }
272
-
273
- return this._contentRouting
101
+ await this._invokeStartableMethod('afterStop')
274
102
  }
103
+ }
275
104
 
276
- set contentRouting (contentRouting: ContentRouting) {
277
- this._contentRouting = contentRouting
278
- }
105
+ const OPTIONAL_SERVICES = [
106
+ 'metrics',
107
+ 'connectionProtector'
108
+ ]
109
+
110
+ const NON_SERVICE_PROPERTIES = [
111
+ 'components',
112
+ 'isStarted',
113
+ 'beforeStart',
114
+ 'start',
115
+ 'afterStart',
116
+ 'beforeStop',
117
+ 'stop',
118
+ 'afterStop',
119
+ 'then',
120
+ '_invokeStartableMethod'
121
+ ]
122
+
123
+ export function defaultComponents (init: ComponentsInit = {}): Components {
124
+ const components = new DefaultComponents(init)
125
+
126
+ const proxy = new Proxy(components, {
127
+ get (target, prop, receiver) {
128
+ if (typeof prop === 'string' && !NON_SERVICE_PROPERTIES.includes(prop)) {
129
+ const service = components.components[prop]
130
+
131
+ if (service == null && !OPTIONAL_SERVICES.includes(prop)) {
132
+ throw new CodeError(`${prop} not set`, 'ERR_SERVICE_MISSING')
133
+ }
279
134
 
280
- get peerRouting (): PeerRouting {
281
- if (this._peerRouting == null) {
282
- throw new CodeError('peerRouting not set', 'ERR_SERVICE_MISSING')
283
- }
135
+ return service
136
+ }
284
137
 
285
- return this._peerRouting
286
- }
138
+ return Reflect.get(target, prop, receiver)
139
+ },
287
140
 
288
- set peerRouting (peerRouting: PeerRouting) {
289
- this._peerRouting = peerRouting
290
- }
141
+ set (target, prop, value) {
142
+ if (typeof prop === 'string') {
143
+ components.components[prop] = value
144
+ } else {
145
+ Reflect.set(target, prop, value)
146
+ }
291
147
 
292
- get datastore (): Datastore {
293
- if (this._datastore == null) {
294
- throw new CodeError('datastore not set', 'ERR_SERVICE_MISSING')
148
+ return true
295
149
  }
150
+ })
296
151
 
297
- return this._datastore
298
- }
299
-
300
- set datastore (datastore: Datastore) {
301
- this._datastore = datastore
302
- }
303
-
304
- get connectionProtector (): ConnectionProtector | undefined {
305
- return this._connectionProtector
306
- }
307
-
308
- set connectionProtector (connectionProtector: ConnectionProtector | undefined) {
309
- this._connectionProtector = connectionProtector
310
- }
311
-
312
- get metrics (): Metrics | undefined {
313
- return this._metrics
314
- }
315
-
316
- set metrics (metrics: Metrics | undefined) {
317
- this._metrics = metrics
318
- }
319
-
320
- get dht (): DualDHT | undefined {
321
- return this._dht
322
- }
323
-
324
- set dht (dht: DualDHT | undefined) {
325
- this._dht = dht
326
- }
327
-
328
- get pubsub (): PubSub | undefined {
329
- return this._pubsub
330
- }
331
-
332
- set pubsub (pubsub: PubSub | undefined) {
333
- this._pubsub = pubsub
334
- }
152
+ // @ts-expect-error component keys are proxied
153
+ return proxy
335
154
  }
@@ -0,0 +1,31 @@
1
+ import type { ConnectionGater } from '@libp2p/interface-connection-gater'
2
+ import type { Multiaddr } from '@multiformats/multiaddr'
3
+ import isPrivate from 'private-ip'
4
+
5
+ /**
6
+ * Returns a connection gater that disallows dialling private addresses by
7
+ * default. Browsers are severely limited in their resource usage so don't
8
+ * waste time trying to dial undiallable addresses.
9
+ */
10
+ export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
11
+ return {
12
+ denyDialPeer: async () => false,
13
+ denyDialMultiaddr: async (multiaddr: Multiaddr) => {
14
+ const tuples = multiaddr.stringTuples()
15
+
16
+ if (tuples[0][0] === 4 || tuples[0][0] === 41) {
17
+ return Boolean(isPrivate(`${tuples[0][1]}`))
18
+ }
19
+
20
+ return false
21
+ },
22
+ denyInboundConnection: async () => false,
23
+ denyOutboundConnection: async () => false,
24
+ denyInboundEncryptedConnection: async () => false,
25
+ denyOutboundEncryptedConnection: async () => false,
26
+ denyInboundUpgradedConnection: async () => false,
27
+ denyOutboundUpgradedConnection: async () => false,
28
+ filterMultiaddrForPeer: async () => true,
29
+ ...gater
30
+ }
31
+ }
@@ -0,0 +1,19 @@
1
+ import type { ConnectionGater } from '@libp2p/interface-connection-gater'
2
+
3
+ /**
4
+ * Returns a default connection gater implementation that allows everything
5
+ */
6
+ export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
7
+ return {
8
+ denyDialPeer: async () => false,
9
+ denyDialMultiaddr: async () => false,
10
+ denyInboundConnection: async () => false,
11
+ denyOutboundConnection: async () => false,
12
+ denyInboundEncryptedConnection: async () => false,
13
+ denyOutboundEncryptedConnection: async () => false,
14
+ denyInboundUpgradedConnection: async () => false,
15
+ denyOutboundUpgradedConnection: async () => false,
16
+ filterMultiaddrForPeer: async () => true,
17
+ ...gater
18
+ }
19
+ }