libp2p 0.45.9-daeb43d8 → 0.45.9-e9cafd3d

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.
@@ -1,9 +1,9 @@
1
- import { symbol } from '@libp2p/interface/connection'
1
+ import { type Direction, symbol, type Connection, type Stream, type ConnectionTimeline } from '@libp2p/interface/connection'
2
2
  import { OPEN, CLOSING, CLOSED } from '@libp2p/interface/connection/status'
3
3
  import { CodeError } from '@libp2p/interface/errors'
4
4
  import { logger } from '@libp2p/logger'
5
5
  import type { AbortOptions } from '@libp2p/interface'
6
- import type { Connection, ConnectionStat, Stream } from '@libp2p/interface/connection'
6
+ import type * as Status from '@libp2p/interface/connection/status'
7
7
  import type { PeerId } from '@libp2p/interface/peer-id'
8
8
  import type { Multiaddr } from '@multiformats/multiaddr'
9
9
 
@@ -15,7 +15,11 @@ interface ConnectionInit {
15
15
  newStream: (protocols: string[], options?: AbortOptions) => Promise<Stream>
16
16
  close: () => Promise<void>
17
17
  getStreams: () => Stream[]
18
- stat: ConnectionStat
18
+ status: keyof typeof Status
19
+ direction: Direction
20
+ timeline: ConnectionTimeline
21
+ multiplexer?: string
22
+ encryption?: string
19
23
  }
20
24
 
21
25
  /**
@@ -38,10 +42,11 @@ export class ConnectionImpl implements Connection {
38
42
  */
39
43
  public readonly remotePeer: PeerId
40
44
 
41
- /**
42
- * Connection metadata
43
- */
44
- public readonly stat: ConnectionStat
45
+ public direction: Direction
46
+ public timeline: ConnectionTimeline
47
+ public multiplexer?: string
48
+ public encryption?: string
49
+ public status: keyof typeof Status
45
50
 
46
51
  /**
47
52
  * User provided tags
@@ -71,15 +76,17 @@ export class ConnectionImpl implements Connection {
71
76
  * Any libp2p transport should use an upgrader to return this connection.
72
77
  */
73
78
  constructor (init: ConnectionInit) {
74
- const { remoteAddr, remotePeer, newStream, close, getStreams, stat } = init
79
+ const { remoteAddr, remotePeer, newStream, close, getStreams } = init
75
80
 
76
81
  this.id = `${(parseInt(String(Math.random() * 1e9))).toString(36)}${Date.now()}`
77
82
  this.remoteAddr = remoteAddr
78
83
  this.remotePeer = remotePeer
79
- this.stat = {
80
- ...stat,
81
- status: OPEN
82
- }
84
+ this.direction = init.direction
85
+ this.status = OPEN
86
+ this.timeline = init.timeline
87
+ this.multiplexer = init.multiplexer
88
+ this.encryption = init.encryption
89
+
83
90
  this._newStream = newStream
84
91
  this._close = close
85
92
  this._getStreams = getStreams
@@ -102,11 +109,11 @@ export class ConnectionImpl implements Connection {
102
109
  * Create a new stream from this connection
103
110
  */
104
111
  async newStream (protocols: string | string[], options?: AbortOptions): Promise<Stream> {
105
- if (this.stat.status === CLOSING) {
112
+ if (this.status === CLOSING) {
106
113
  throw new CodeError('the connection is being closed', 'ERR_CONNECTION_BEING_CLOSED')
107
114
  }
108
115
 
109
- if (this.stat.status === CLOSED) {
116
+ if (this.status === CLOSED) {
110
117
  throw new CodeError('the connection is closed', 'ERR_CONNECTION_CLOSED')
111
118
  }
112
119
 
@@ -116,7 +123,7 @@ export class ConnectionImpl implements Connection {
116
123
 
117
124
  const stream = await this._newStream(protocols, options)
118
125
 
119
- stream.stat.direction = 'outbound'
126
+ stream.direction = 'outbound'
120
127
 
121
128
  return stream
122
129
  }
@@ -125,7 +132,7 @@ export class ConnectionImpl implements Connection {
125
132
  * Add a stream when it is opened to the registry
126
133
  */
127
134
  addStream (stream: Stream): void {
128
- stream.stat.direction = 'inbound'
135
+ stream.direction = 'inbound'
129
136
  }
130
137
 
131
138
  /**
@@ -139,11 +146,11 @@ export class ConnectionImpl implements Connection {
139
146
  * Close the connection
140
147
  */
141
148
  async close (): Promise<void> {
142
- if (this.stat.status === CLOSED || this._closing) {
149
+ if (this.status === CLOSED || this._closing) {
143
150
  return
144
151
  }
145
152
 
146
- this.stat.status = CLOSING
153
+ this.status = CLOSING
147
154
 
148
155
  // close all streams - this can throw if we're not multiplexed
149
156
  try {
@@ -157,8 +164,8 @@ export class ConnectionImpl implements Connection {
157
164
  await this._close()
158
165
  this._closing = false
159
166
 
160
- this.stat.timeline.close = Date.now()
161
- this.stat.status = CLOSED
167
+ this.timeline.close = Date.now()
168
+ this.status = CLOSED
162
169
  }
163
170
  }
164
171
 
@@ -106,8 +106,8 @@ export class ConnectionPruner {
106
106
  }
107
107
 
108
108
  // if the peers have an equal tag value then we want to close short-lived connections first
109
- const connectionALifespan = a.stat.timeline.open
110
- const connectionBLifespan = b.stat.timeline.open
109
+ const connectionALifespan = a.timeline.open
110
+ const connectionBLifespan = b.timeline.open
111
111
 
112
112
  if (connectionALifespan < connectionBLifespan) {
113
113
  return 1
@@ -273,7 +273,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
273
273
 
274
274
  for (const conns of this.connections.values()) {
275
275
  for (const conn of conns) {
276
- if (conn.stat.direction === 'inbound') {
276
+ if (conn.direction === 'inbound') {
277
277
  metric.inbound++
278
278
  } else {
279
279
  metric.outbound++
@@ -294,7 +294,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
294
294
  for (const conns of this.connections.values()) {
295
295
  for (const conn of conns) {
296
296
  for (const stream of conn.streams) {
297
- const key = `${stream.stat.direction} ${stream.stat.protocol ?? 'unnegotiated'}`
297
+ const key = `${stream.direction} ${stream.protocol ?? 'unnegotiated'}`
298
298
 
299
299
  metric[key] = (metric[key] ?? 0) + 1
300
300
  }
@@ -316,7 +316,7 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
316
316
  const streams: Record<string, number> = {}
317
317
 
318
318
  for (const stream of conn.streams) {
319
- const key = `${stream.stat.direction} ${stream.stat.protocol ?? 'unnegotiated'}`
319
+ const key = `${stream.direction} ${stream.protocol ?? 'unnegotiated'}`
320
320
 
321
321
  streams[key] = (streams[key] ?? 0) + 1
322
322
  }
@@ -23,7 +23,7 @@ import {
23
23
  MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
24
24
  } from './consts.js'
25
25
  import { Identify } from './pb/message.js'
26
- import type { IdentifyServiceComponents, IdentifyServiceInit } from './index.js'
26
+ import type { IdentifyService, IdentifyServiceComponents, IdentifyServiceInit } from './index.js'
27
27
  import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions } from '@libp2p/interface'
28
28
  import type { Connection, Stream } from '@libp2p/interface/connection'
29
29
  import type { EventEmitter } from '@libp2p/interface/events'
@@ -49,10 +49,11 @@ const defaultValues = {
49
49
  maxPushIncomingStreams: 1,
50
50
  maxPushOutgoingStreams: 1,
51
51
  maxObservedAddresses: 10,
52
- maxIdentifyMessageSize: 8192
52
+ maxIdentifyMessageSize: 8192,
53
+ runOnConnectionOpen: true
53
54
  }
54
55
 
55
- export class DefaultIdentifyService implements Startable {
56
+ export class DefaultIdentifyService implements Startable, IdentifyService {
56
57
  private readonly identifyProtocolStr: string
57
58
  private readonly identifyPushProtocolStr: string
58
59
  public readonly host: {
@@ -100,11 +101,13 @@ export class DefaultIdentifyService implements Startable {
100
101
  agentVersion: init.agentVersion ?? defaultValues.agentVersion
101
102
  }
102
103
 
103
- // When a new connection happens, trigger identify
104
- components.events.addEventListener('connection:open', (evt) => {
105
- const connection = evt.detail
106
- this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
107
- })
104
+ if (init.runOnConnectionOpen ?? defaultValues.runOnConnectionOpen) {
105
+ // When a new connection happens, trigger identify
106
+ components.events.addEventListener('connection:open', (evt) => {
107
+ const connection = evt.detail
108
+ this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
109
+ })
110
+ }
108
111
 
109
112
  // When self peer record changes, trigger identify-push
110
113
  components.events.addEventListener('self:peer:update', (evt) => {
@@ -296,7 +299,7 @@ export class DefaultIdentifyService implements Startable {
296
299
  }
297
300
  }
298
301
 
299
- async identify (connection: Connection, options: AbortOptions = {}): Promise<void> {
302
+ async identify (connection: Connection, options: AbortOptions = {}): Promise<IdentifyResult> {
300
303
  const message = await this._identify(connection, options)
301
304
  const {
302
305
  publicKey,
@@ -344,6 +347,8 @@ export class DefaultIdentifyService implements Startable {
344
347
  }
345
348
 
346
349
  this.events.safeDispatchEvent('peer:identify', { detail: result })
350
+
351
+ return result
347
352
  }
348
353
 
349
354
  /**
@@ -4,10 +4,11 @@ import {
4
4
  } from './consts.js'
5
5
  import { DefaultIdentifyService } from './identify.js'
6
6
  import { Identify } from './pb/message.js'
7
- import type { Libp2pEvents } from '@libp2p/interface'
7
+ import type { AbortOptions, IdentifyResult, Libp2pEvents } from '@libp2p/interface'
8
8
  import type { EventEmitter } from '@libp2p/interface/events'
9
9
  import type { PeerId } from '@libp2p/interface/peer-id'
10
10
  import type { PeerStore } from '@libp2p/interface/peer-store'
11
+ import type { Connection } from '@libp2p/interface/src/connection/index.js'
11
12
  import type { AddressManager } from '@libp2p/interface-internal/address-manager'
12
13
  import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
13
14
  import type { Registrar } from '@libp2p/interface-internal/registrar'
@@ -39,6 +40,11 @@ export interface IdentifyServiceInit {
39
40
  maxPushIncomingStreams?: number
40
41
  maxPushOutgoingStreams?: number
41
42
  maxObservedAddresses?: number
43
+
44
+ /**
45
+ * Whether to automatically dial identify on newly opened connections (default: true)
46
+ */
47
+ runOnConnectionOpen?: boolean
42
48
  }
43
49
 
44
50
  export interface IdentifyServiceComponents {
@@ -60,6 +66,19 @@ export const multicodecs = {
60
66
 
61
67
  export const Message = { Identify }
62
68
 
63
- export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => DefaultIdentifyService {
69
+ export interface IdentifyService {
70
+ /**
71
+ * due to the default limits on inbound/outbound streams for this protocol,
72
+ * invoking this method when runOnConnectionOpen is true can lead to unpredictable results
73
+ * as streams may be closed by the local or the remote node.
74
+ * Please use with caution. If you find yourself needing to call this method to discover other peers that support your protocol,
75
+ * you may be better off configuring a topology to be notified instead.
76
+ */
77
+ identify: (connection: Connection, options?: AbortOptions) => Promise<IdentifyResult>
78
+
79
+ push: () => Promise<void>
80
+ }
81
+
82
+ export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => IdentifyService {
64
83
  return (components) => new DefaultIdentifyService(components, init)
65
84
  }
package/src/upgrader.ts CHANGED
@@ -89,7 +89,7 @@ function countStreams (protocol: string, direction: 'inbound' | 'outbound', conn
89
89
  let streamCount = 0
90
90
 
91
91
  connection.streams.forEach(stream => {
92
- if (stream.stat.direction === direction && stream.stat.protocol === protocol) {
92
+ if (stream.direction === direction && stream.protocol === protocol) {
93
93
  streamCount++
94
94
  }
95
95
  })
@@ -403,7 +403,7 @@ export class DefaultUpgrader implements Upgrader {
403
403
  // the souce/sink
404
404
  muxedStream.source = stream.source
405
405
  muxedStream.sink = stream.sink
406
- muxedStream.stat.protocol = protocol
406
+ muxedStream.protocol = protocol
407
407
 
408
408
  // If a protocol stream has been successfully negotiated and is to be passed to the application,
409
409
  // the peerstore should ensure that the peer is registered with that protocol
@@ -419,7 +419,7 @@ export class DefaultUpgrader implements Upgrader {
419
419
  .catch(err => {
420
420
  log.error(err)
421
421
 
422
- if (muxedStream.stat.timeline.close == null) {
422
+ if (muxedStream.timeline.close == null) {
423
423
  muxedStream.close()
424
424
  }
425
425
  })
@@ -472,7 +472,7 @@ export class DefaultUpgrader implements Upgrader {
472
472
  // the souce/sink
473
473
  muxedStream.source = stream.source
474
474
  muxedStream.sink = stream.sink
475
- muxedStream.stat.protocol = protocol
475
+ muxedStream.protocol = protocol
476
476
 
477
477
  this.components.metrics?.trackProtocolStream(muxedStream, connection)
478
478
 
@@ -480,7 +480,7 @@ export class DefaultUpgrader implements Upgrader {
480
480
  } catch (err: any) {
481
481
  log.error('could not create new stream', err)
482
482
 
483
- if (muxedStream.stat.timeline.close == null) {
483
+ if (muxedStream.timeline.close == null) {
484
484
  muxedStream.close()
485
485
  }
486
486
 
@@ -508,7 +508,7 @@ export class DefaultUpgrader implements Upgrader {
508
508
  // Wait for close to finish before notifying of the closure
509
509
  (async () => {
510
510
  try {
511
- if (connection.stat.status === 'OPEN') {
511
+ if (connection.status === 'OPEN') {
512
512
  await connection.close()
513
513
  }
514
514
  } catch (err: any) {
@@ -536,13 +536,11 @@ export class DefaultUpgrader implements Upgrader {
536
536
  connection = createConnection({
537
537
  remoteAddr: maConn.remoteAddr,
538
538
  remotePeer,
539
- stat: {
540
- status: 'OPEN',
541
- direction,
542
- timeline: maConn.timeline,
543
- multiplexer: muxer?.protocol,
544
- encryption: cryptoProtocol
545
- },
539
+ status: 'OPEN',
540
+ direction,
541
+ timeline: maConn.timeline,
542
+ multiplexer: muxer?.protocol,
543
+ encryption: cryptoProtocol,
546
544
  newStream: newStream ?? errConnectionNotMultiplexed,
547
545
  getStreams: () => { if (muxer != null) { return muxer.streams } else { return errConnectionNotMultiplexed() } },
548
546
  close: async () => {
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.45.9-daeb43d8'
1
+ export const version = '0.45.9-e9cafd3d'
2
2
  export const name = 'libp2p'