libp2p 0.46.8 → 0.46.9

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 (48) hide show
  1. package/dist/index.min.js +11 -11
  2. package/dist/src/autonat/index.d.ts.map +1 -1
  3. package/dist/src/autonat/index.js +18 -9
  4. package/dist/src/autonat/index.js.map +1 -1
  5. package/dist/src/circuit-relay/transport/reservation-store.js +1 -1
  6. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  7. package/dist/src/circuit-relay/utils.d.ts.map +1 -1
  8. package/dist/src/circuit-relay/utils.js +14 -7
  9. package/dist/src/circuit-relay/utils.js.map +1 -1
  10. package/dist/src/connection-manager/auto-dial.d.ts +2 -0
  11. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  12. package/dist/src/connection-manager/auto-dial.js +19 -2
  13. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  14. package/dist/src/connection-manager/constants.defaults.d.ts +4 -0
  15. package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
  16. package/dist/src/connection-manager/constants.defaults.js +4 -0
  17. package/dist/src/connection-manager/constants.defaults.js.map +1 -1
  18. package/dist/src/connection-manager/index.d.ts +7 -0
  19. package/dist/src/connection-manager/index.d.ts.map +1 -1
  20. package/dist/src/connection-manager/index.js.map +1 -1
  21. package/dist/src/fetch/index.d.ts.map +1 -1
  22. package/dist/src/fetch/index.js +7 -3
  23. package/dist/src/fetch/index.js.map +1 -1
  24. package/dist/src/identify/consts.d.ts +1 -1
  25. package/dist/src/identify/consts.d.ts.map +1 -1
  26. package/dist/src/libp2p.js +1 -1
  27. package/dist/src/libp2p.js.map +1 -1
  28. package/dist/src/ping/index.d.ts.map +1 -1
  29. package/dist/src/ping/index.js +7 -3
  30. package/dist/src/ping/index.js.map +1 -1
  31. package/dist/src/upgrader.d.ts.map +1 -1
  32. package/dist/src/upgrader.js +5 -4
  33. package/dist/src/upgrader.js.map +1 -1
  34. package/dist/src/version.d.ts +1 -1
  35. package/dist/src/version.js +1 -1
  36. package/dist/typedoc-urls.json +34 -21
  37. package/package.json +1 -2
  38. package/src/autonat/index.ts +20 -7
  39. package/src/circuit-relay/transport/reservation-store.ts +1 -1
  40. package/src/circuit-relay/utils.ts +18 -7
  41. package/src/connection-manager/auto-dial.ts +22 -2
  42. package/src/connection-manager/constants.defaults.ts +5 -0
  43. package/src/connection-manager/index.ts +8 -0
  44. package/src/fetch/index.ts +8 -3
  45. package/src/libp2p.ts +1 -1
  46. package/src/ping/index.ts +8 -3
  47. package/src/upgrader.ts +8 -5
  48. package/src/version.ts +1 -1
@@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger'
2
2
  import { PeerMap, PeerSet } from '@libp2p/peer-collections'
3
3
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
4
  import { PeerJobQueue } from '../utils/peer-job-queue.js'
5
- import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PEER_RETRY_THRESHOLD, AUTO_DIAL_PRIORITY, LAST_DIAL_FAILURE_KEY, MIN_CONNECTIONS } from './constants.js'
5
+ import { AUTO_DIAL_CONCURRENCY, AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE, AUTO_DIAL_INTERVAL, AUTO_DIAL_MAX_QUEUE_LENGTH, AUTO_DIAL_PEER_RETRY_THRESHOLD, AUTO_DIAL_PRIORITY, LAST_DIAL_FAILURE_KEY, MIN_CONNECTIONS } from './constants.js'
6
6
  import type { Libp2pEvents } from '@libp2p/interface'
7
7
  import type { EventEmitter } from '@libp2p/interface/events'
8
8
  import type { PeerStore } from '@libp2p/interface/peer-store'
@@ -18,6 +18,7 @@ interface AutoDialInit {
18
18
  autoDialPriority?: number
19
19
  autoDialInterval?: number
20
20
  autoDialPeerRetryThreshold?: number
21
+ autoDialDiscoveredPeersDebounce?: number
21
22
  }
22
23
 
23
24
  interface AutoDialComponents {
@@ -32,7 +33,8 @@ const defaultOptions = {
32
33
  autoDialConcurrency: AUTO_DIAL_CONCURRENCY,
33
34
  autoDialPriority: AUTO_DIAL_PRIORITY,
34
35
  autoDialInterval: AUTO_DIAL_INTERVAL,
35
- autoDialPeerRetryThreshold: AUTO_DIAL_PEER_RETRY_THRESHOLD
36
+ autoDialPeerRetryThreshold: AUTO_DIAL_PEER_RETRY_THRESHOLD,
37
+ autoDialDiscoveredPeersDebounce: AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE
36
38
  }
37
39
 
38
40
  export class AutoDial implements Startable {
@@ -44,6 +46,7 @@ export class AutoDial implements Startable {
44
46
  private readonly autoDialIntervalMs: number
45
47
  private readonly autoDialMaxQueueLength: number
46
48
  private readonly autoDialPeerRetryThresholdMs: number
49
+ private readonly autoDialDiscoveredPeersDebounce: number
47
50
  private autoDialInterval?: ReturnType<typeof setInterval>
48
51
  private started: boolean
49
52
  private running: boolean
@@ -61,6 +64,7 @@ export class AutoDial implements Startable {
61
64
  this.autoDialIntervalMs = init.autoDialInterval ?? defaultOptions.autoDialInterval
62
65
  this.autoDialMaxQueueLength = init.maxQueueLength ?? defaultOptions.maxQueueLength
63
66
  this.autoDialPeerRetryThresholdMs = init.autoDialPeerRetryThreshold ?? defaultOptions.autoDialPeerRetryThreshold
67
+ this.autoDialDiscoveredPeersDebounce = init.autoDialDiscoveredPeersDebounce ?? defaultOptions.autoDialDiscoveredPeersDebounce
64
68
  this.started = false
65
69
  this.running = false
66
70
  this.queue = new PeerJobQueue({
@@ -77,6 +81,22 @@ export class AutoDial implements Startable {
77
81
  log.error(err)
78
82
  })
79
83
  })
84
+
85
+ // sometimes peers are discovered in quick succession so add a small
86
+ // debounce to ensure all eligible peers are autodialed
87
+ let debounce: ReturnType<typeof setTimeout>
88
+
89
+ // when new peers are discovered, dial them if we don't have
90
+ // enough connections
91
+ components.events.addEventListener('peer:discovery', () => {
92
+ clearTimeout(debounce)
93
+ debounce = setTimeout(() => {
94
+ this.autoDial()
95
+ .catch(err => {
96
+ log.error(err)
97
+ })
98
+ }, this.autoDialDiscoveredPeersDebounce)
99
+ })
80
100
  }
81
101
 
82
102
  isStarted (): boolean {
@@ -38,6 +38,11 @@ export const AUTO_DIAL_MAX_QUEUE_LENGTH = 100
38
38
  */
39
39
  export const AUTO_DIAL_PEER_RETRY_THRESHOLD = 1000 * 60
40
40
 
41
+ /**
42
+ * @see https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.unknown.ConnectionManagerInit.html#autoDialDiscoveredPeersDebounce
43
+ */
44
+ export const AUTO_DIAL_DISCOVERED_PEERS_DEBOUNCE = 10
45
+
41
46
  /**
42
47
  * @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundConnectionThreshold
43
48
  */
@@ -72,6 +72,14 @@ export interface ConnectionManagerInit {
72
72
  */
73
73
  autoDialPeerRetryThreshold?: number
74
74
 
75
+ /**
76
+ * Newly discovered peers may be auto-dialed to increase the number of open
77
+ * connections, but they can be discovered in quick succession so add a small
78
+ * delay before attempting to dial them in case more peers have been
79
+ * discovered. (default: 10ms)
80
+ */
81
+ autoDialDiscoveredPeersDebounce?: number
82
+
75
83
  /**
76
84
  * Sort the known addresses of a peer before trying to dial, By default public
77
85
  * addresses will be dialled before private (e.g. loopback or LAN) addresses.
@@ -1,7 +1,6 @@
1
1
  import { setMaxListeners } from 'events'
2
2
  import { CodeError } from '@libp2p/interface/errors'
3
3
  import { logger } from '@libp2p/logger'
4
- import { abortableDuplex } from 'abortable-iterator'
5
4
  import first from 'it-first'
6
5
  import * as lp from 'it-length-prefixed'
7
6
  import { pipe } from 'it-pipe'
@@ -140,6 +139,7 @@ class DefaultFetchService implements Startable, FetchService {
140
139
  const connection = await this.components.connectionManager.openConnection(peer, options)
141
140
  let signal = options.signal
142
141
  let stream: Stream | undefined
142
+ let onAbort = (): void => {}
143
143
 
144
144
  // create a timeout if no abort signal passed
145
145
  if (signal == null) {
@@ -157,15 +157,19 @@ class DefaultFetchService implements Startable, FetchService {
157
157
  signal
158
158
  })
159
159
 
160
+ onAbort = () => {
161
+ stream?.abort(new CodeError('fetch timeout', codes.ERR_TIMEOUT))
162
+ }
163
+
160
164
  // make stream abortable
161
- const source = abortableDuplex(stream, signal)
165
+ signal.addEventListener('abort', onAbort, { once: true })
162
166
 
163
167
  log('fetch %s', key)
164
168
 
165
169
  const result = await pipe(
166
170
  [FetchRequest.encode({ identifier: key })],
167
171
  (source) => lp.encode(source),
168
- source,
172
+ stream,
169
173
  (source) => lp.decode(source),
170
174
  async function (source) {
171
175
  const buf = await first(source)
@@ -200,6 +204,7 @@ class DefaultFetchService implements Startable, FetchService {
200
204
 
201
205
  return result ?? null
202
206
  } finally {
207
+ signal.removeEventListener('abort', onAbort)
203
208
  if (stream != null) {
204
209
  await stream.close()
205
210
  }
package/src/libp2p.ts CHANGED
@@ -105,7 +105,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
105
105
  protocols: evt.detail.peer.protocols
106
106
  }
107
107
 
108
- this.safeDispatchEvent('peer:discovery', { detail: peerInfo })
108
+ components.events.safeDispatchEvent('peer:discovery', { detail: peerInfo })
109
109
  }
110
110
  })
111
111
 
package/src/ping/index.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { randomBytes } from '@libp2p/crypto'
2
2
  import { CodeError } from '@libp2p/interface/errors'
3
3
  import { logger } from '@libp2p/logger'
4
- import { abortableDuplex } from 'abortable-iterator'
5
4
  import first from 'it-first'
6
5
  import { pipe } from 'it-pipe'
7
6
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
@@ -108,6 +107,7 @@ class DefaultPingService implements Startable, PingService {
108
107
  const data = randomBytes(PING_LENGTH)
109
108
  const connection = await this.components.connectionManager.openConnection(peer, options)
110
109
  let stream: Stream | undefined
110
+ let onAbort = (): void => {}
111
111
 
112
112
  options.signal = options.signal ?? AbortSignal.timeout(this.timeout)
113
113
 
@@ -117,12 +117,16 @@ class DefaultPingService implements Startable, PingService {
117
117
  runOnTransientConnection: this.runOnTransientConnection
118
118
  })
119
119
 
120
+ onAbort = () => {
121
+ stream?.abort(new CodeError('ping timeout', codes.ERR_TIMEOUT))
122
+ }
123
+
120
124
  // make stream abortable
121
- const source = abortableDuplex(stream, options.signal)
125
+ options.signal.addEventListener('abort', onAbort, { once: true })
122
126
 
123
127
  const result = await pipe(
124
128
  [data],
125
- source,
129
+ stream,
126
130
  async (source) => first(source)
127
131
  )
128
132
 
@@ -146,6 +150,7 @@ class DefaultPingService implements Startable, PingService {
146
150
 
147
151
  throw err
148
152
  } finally {
153
+ options.signal.removeEventListener('abort', onAbort)
149
154
  if (stream != null) {
150
155
  await stream.close()
151
156
  }
package/src/upgrader.ts CHANGED
@@ -3,7 +3,6 @@ import { CodeError } from '@libp2p/interface/errors'
3
3
  import { logger } from '@libp2p/logger'
4
4
  import * as mss from '@libp2p/multistream-select'
5
5
  import { peerIdFromString } from '@libp2p/peer-id'
6
- import { abortableDuplex } from 'abortable-iterator'
7
6
  import { createConnection } from './connection/index.js'
8
7
  import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
9
8
  import { codes } from './errors.js'
@@ -165,16 +164,18 @@ export class DefaultUpgrader implements Upgrader {
165
164
 
166
165
  const signal = AbortSignal.timeout(this.inboundUpgradeTimeout)
167
166
 
167
+ const onAbort = (): void => {
168
+ maConn.abort(new CodeError('inbound upgrade timeout', codes.ERR_TIMEOUT))
169
+ }
170
+
171
+ signal.addEventListener('abort', onAbort, { once: true })
172
+
168
173
  try {
169
174
  // fails on node < 15.4
170
175
  setMaxListeners?.(Infinity, signal)
171
176
  } catch { }
172
177
 
173
178
  try {
174
- const abortableStream = abortableDuplex(maConn, signal)
175
- maConn.source = abortableStream.source
176
- maConn.sink = abortableStream.sink
177
-
178
179
  if ((await this.components.connectionGater.denyInboundConnection?.(maConn)) === true) {
179
180
  throw new CodeError('The multiaddr connection is blocked by gater.acceptConnection', codes.ERR_CONNECTION_INTERCEPTED)
180
181
  }
@@ -255,6 +256,8 @@ export class DefaultUpgrader implements Upgrader {
255
256
  transient: opts?.transient
256
257
  })
257
258
  } finally {
259
+ signal.removeEventListener('abort', onAbort)
260
+
258
261
  this.components.connectionManager.afterUpgradeInbound()
259
262
  }
260
263
  }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.46.8'
1
+ export const version = '0.46.9'
2
2
  export const name = 'libp2p'