Skip to content

Commit e1f0b30

Browse files
authored
fix: expose progress events in dial/dialProtocol types (#2614)
Accept the `onProgress` event via types.
1 parent 3805a20 commit e1f0b30

File tree

12 files changed

+50
-43
lines changed

12 files changed

+50
-43
lines changed

packages/interface-internal/src/connection-manager/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
import type { TransportManagerDialProgressEvents } from '../transport-manager/index.js'
2-
import type { AbortOptions, PendingDial, Connection, MultiaddrConnection, PeerId, IsDialableOptions, Address, OutboundConnectionUpgradeEvents } from '@libp2p/interface'
1+
import type { AbortOptions, PendingDial, Connection, MultiaddrConnection, PeerId, IsDialableOptions, OpenConnectionProgressEvents } from '@libp2p/interface'
32
import type { PeerMap } from '@libp2p/peer-collections'
43
import type { Multiaddr } from '@multiformats/multiaddr'
5-
import type { ProgressOptions, ProgressEvent } from 'progress-events'
6-
7-
export type OpenConnectionProgressEvents =
8-
TransportManagerDialProgressEvents |
9-
ProgressEvent<'dial-queue:already-connected'> |
10-
ProgressEvent<'dial-queue:already-in-dial-queue'> |
11-
ProgressEvent<'dial-queue:add-to-dial-queue'> |
12-
ProgressEvent<'dial-queue:start-dial'> |
13-
ProgressEvent<'dial-queue:calculated-addresses', Address[]> |
14-
OutboundConnectionUpgradeEvents
4+
import type { ProgressOptions } from 'progress-events'
155

166
export interface OpenConnectionOptions extends AbortOptions, ProgressOptions<OpenConnectionProgressEvents> {
177
/**

packages/interface-internal/src/transport-manager/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import type { AbortOptions, Connection, Listener, Transport } from '@libp2p/interface'
1+
import type { AbortOptions, Connection, Listener, Transport, TransportManagerDialProgressEvents } from '@libp2p/interface'
22
import type { Multiaddr } from '@multiformats/multiaddr'
3-
import type { ProgressOptions, ProgressEvent } from 'progress-events'
4-
5-
export type TransportManagerDialProgressEvents =
6-
ProgressEvent<'transport-manager:selected-transport', string>
3+
import type { ProgressOptions } from 'progress-events'
74

85
export interface TransportManagerDialOptions extends AbortOptions, ProgressOptions<TransportManagerDialProgressEvents> {
96

packages/interface/src/index.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import type { Address, Peer, PeerStore } from './peer-store/index.js'
2525
import type { Startable } from './startable.js'
2626
import type { StreamHandler, StreamHandlerOptions } from './stream-handler/index.js'
2727
import type { Topology } from './topology/index.js'
28-
import type { Listener } from './transport/index.js'
28+
import type { Listener, OutboundConnectionUpgradeEvents } from './transport/index.js'
2929
import type { Multiaddr } from '@multiformats/multiaddr'
30-
import type { ProgressOptions } from 'progress-events'
30+
import type { ProgressOptions, ProgressEvent } from 'progress-events'
3131

3232
/**
3333
* Used by the connection manager to sort addresses into order before dialling
@@ -334,6 +334,26 @@ export interface IsDialableOptions extends AbortOptions {
334334
runOnTransientConnection?: boolean
335335
}
336336

337+
export type TransportManagerDialProgressEvents =
338+
ProgressEvent<'transport-manager:selected-transport', string>
339+
340+
export type OpenConnectionProgressEvents =
341+
TransportManagerDialProgressEvents |
342+
ProgressEvent<'dial-queue:already-connected'> |
343+
ProgressEvent<'dial-queue:already-in-dial-queue'> |
344+
ProgressEvent<'dial-queue:add-to-dial-queue'> |
345+
ProgressEvent<'dial-queue:start-dial'> |
346+
ProgressEvent<'dial-queue:calculated-addresses', Address[]> |
347+
OutboundConnectionUpgradeEvents
348+
349+
export interface DialOptions extends AbortOptions, ProgressOptions {
350+
351+
}
352+
353+
export interface DialProtocolOptions extends NewStreamOptions {
354+
355+
}
356+
337357
/**
338358
* Libp2p nodes implement this interface.
339359
*/
@@ -515,7 +535,7 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
515535
* await conn.close()
516536
* ```
517537
*/
518-
dial(peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<Connection>
538+
dial(peer: PeerId | Multiaddr | Multiaddr[], options?: DialOptions): Promise<Connection>
519539

520540
/**
521541
* Dials to the provided peer and tries to handshake with the given protocols in order.
@@ -533,7 +553,7 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
533553
* pipe([1, 2, 3], stream, consume)
534554
* ```
535555
*/
536-
dialProtocol(peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: NewStreamOptions): Promise<Stream>
556+
dialProtocol(peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: DialProtocolOptions): Promise<Stream>
537557

538558
/**
539559
* Attempts to gracefully close an open connection to the given peer. If the

packages/interface/src/transport/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface CreateListenerOptions {
4040
upgrader: Upgrader
4141
}
4242

43-
export interface DialOptions<DialEvents extends ProgressEvent = ProgressEvent> extends AbortOptions, ProgressOptions<DialEvents> {
43+
export interface DialTransportOptions<DialEvents extends ProgressEvent = ProgressEvent> extends AbortOptions, ProgressOptions<DialEvents> {
4444
upgrader: Upgrader
4545
}
4646

@@ -61,7 +61,7 @@ export interface Transport<DialEvents extends ProgressEvent = ProgressEvent> {
6161
/**
6262
* Dial a given multiaddr.
6363
*/
64-
dial(ma: Multiaddr, options: DialOptions<DialEvents>): Promise<Connection>
64+
dial(ma: Multiaddr, options: DialTransportOptions<DialEvents>): Promise<Connection>
6565

6666
/**
6767
* Create transport listeners.

packages/libp2p/src/connection-manager/dial-queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import {
2020
} from './constants.js'
2121
import { resolveMultiaddrs } from './utils.js'
2222
import { DEFAULT_DIAL_PRIORITY } from './index.js'
23-
import type { AddressSorter, ComponentLogger, Logger, Connection, ConnectionGater, Metrics, PeerId, Address, PeerStore, PeerRouting, IsDialableOptions } from '@libp2p/interface'
24-
import type { OpenConnectionOptions, OpenConnectionProgressEvents, TransportManager } from '@libp2p/interface-internal'
23+
import type { AddressSorter, ComponentLogger, Logger, Connection, ConnectionGater, Metrics, PeerId, Address, PeerStore, PeerRouting, IsDialableOptions, OpenConnectionProgressEvents } from '@libp2p/interface'
24+
import type { OpenConnectionOptions, TransportManager } from '@libp2p/interface-internal'
2525
import type { DNS } from '@multiformats/dns'
2626
import type { ProgressOptions } from 'progress-events'
2727

packages/libp2p/src/libp2p.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { DefaultUpgrader } from './upgrader.js'
2424
import * as pkg from './version.js'
2525
import type { Components } from './components.js'
2626
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
27-
import type { PeerRouting, ContentRouting, Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger, Connection, NewStreamOptions, Stream, Metrics, PeerId, PeerInfo, PeerStore, Topology, Libp2pStatus, IsDialableOptions } from '@libp2p/interface'
27+
import type { PeerRouting, ContentRouting, Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger, Connection, NewStreamOptions, Stream, Metrics, PeerId, PeerInfo, PeerStore, Topology, Libp2pStatus, IsDialableOptions, DialOptions } from '@libp2p/interface'
2828
import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal'
2929

3030
export class Libp2pNode<T extends ServiceMap = ServiceMap> extends TypedEventEmitter<Libp2pEvents> implements Libp2p<T> {
@@ -272,7 +272,7 @@ export class Libp2pNode<T extends ServiceMap = ServiceMap> extends TypedEventEmi
272272
return Array.from(peerSet)
273273
}
274274

275-
async dial (peer: PeerId | Multiaddr | Multiaddr[], options: AbortOptions = {}): Promise<Connection> {
275+
async dial (peer: PeerId | Multiaddr | Multiaddr[], options: DialOptions = {}): Promise<Connection> {
276276
return this.components.connectionManager.openConnection(peer, {
277277
// ensure any userland dials take top priority in the queue
278278
priority: 75,

packages/transport-circuit-relay-v2/src/transport/transport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { RelayDiscovery } from './discovery.js'
1212
import { createListener } from './listener.js'
1313
import { ReservationStore } from './reservation-store.js'
1414
import type { CircuitRelayTransportComponents, CircuitRelayTransportInit } from './index.js'
15-
import type { Transport, CreateListenerOptions, Listener, Upgrader, ComponentLogger, Logger, Connection, Stream, ConnectionGater, PeerId, PeerStore, OutboundConnectionUpgradeEvents, DialOptions } from '@libp2p/interface'
16-
import type { AddressManager, ConnectionManager, IncomingStreamData, OpenConnectionProgressEvents, Registrar, TransportManager } from '@libp2p/interface-internal'
15+
import type { Transport, CreateListenerOptions, Listener, Upgrader, ComponentLogger, Logger, Connection, Stream, ConnectionGater, PeerId, PeerStore, OutboundConnectionUpgradeEvents, DialTransportOptions, OpenConnectionProgressEvents } from '@libp2p/interface'
16+
import type { AddressManager, ConnectionManager, IncomingStreamData, Registrar, TransportManager } from '@libp2p/interface-internal'
1717
import type { Multiaddr } from '@multiformats/multiaddr'
1818
import type { ProgressEvent, ProgressOptions } from 'progress-events'
1919

@@ -167,7 +167,7 @@ export class CircuitRelayTransport implements Transport<CircuitRelayDialEvents>
167167
/**
168168
* Dial a peer over a relay
169169
*/
170-
async dial (ma: Multiaddr, options: DialOptions<CircuitRelayDialEvents>): Promise<Connection> {
170+
async dial (ma: Multiaddr, options: DialTransportOptions<CircuitRelayDialEvents>): Promise<Connection> {
171171
if (ma.protoCodes().filter(code => code === CIRCUIT_PROTO_CODE).length !== 1) {
172172
const errMsg = 'Invalid circuit relay address'
173173
this.log.error(errMsg, ma)

packages/transport-tcp/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { CODE_CIRCUIT, CODE_P2P, CODE_UNIX } from './constants.js'
3535
import { type CloseServerOnMaxConnectionsOpts, TCPListener } from './listener.js'
3636
import { toMultiaddrConnection } from './socket-to-conn.js'
3737
import { multiaddrToNetConfig } from './utils.js'
38-
import type { ComponentLogger, Logger, Connection, CounterGroup, Metrics, CreateListenerOptions, DialOptions, Transport, Listener, OutboundConnectionUpgradeEvents } from '@libp2p/interface'
38+
import type { ComponentLogger, Logger, Connection, CounterGroup, Metrics, CreateListenerOptions, DialTransportOptions, Transport, Listener, OutboundConnectionUpgradeEvents } from '@libp2p/interface'
3939
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
4040
import type { Socket, IpcSocketConnectOpts, TcpSocketConnectOpts } from 'net'
4141
import type { ProgressEvent } from 'progress-events'
@@ -114,7 +114,7 @@ export type TCPDialEvents =
114114
OutboundConnectionUpgradeEvents |
115115
ProgressEvent<'tcp:open-connection'>
116116

117-
export interface TCPDialOptions extends DialOptions<TCPDialEvents>, TCPSocketOptions {
117+
export interface TCPDialOptions extends DialTransportOptions<TCPDialEvents>, TCPSocketOptions {
118118

119119
}
120120

packages/transport-webrtc/src/private-to-private/transport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { initiateConnection } from './initiate-connection.js'
1111
import { WebRTCPeerListener } from './listener.js'
1212
import { handleIncomingStream } from './signaling-stream-handler.js'
1313
import type { DataChannelOptions } from '../index.js'
14-
import type { OutboundConnectionUpgradeEvents, CreateListenerOptions, DialOptions, Transport, Listener, Upgrader, ComponentLogger, Logger, Connection, PeerId, CounterGroup, Metrics, Startable } from '@libp2p/interface'
15-
import type { IncomingStreamData, Registrar, ConnectionManager, TransportManager, OpenConnectionProgressEvents } from '@libp2p/interface-internal'
14+
import type { OutboundConnectionUpgradeEvents, CreateListenerOptions, DialTransportOptions, Transport, Listener, Upgrader, ComponentLogger, Logger, Connection, PeerId, CounterGroup, Metrics, Startable, OpenConnectionProgressEvents } from '@libp2p/interface'
15+
import type { IncomingStreamData, Registrar, ConnectionManager, TransportManager } from '@libp2p/interface-internal'
1616
import type { ProgressEvent } from 'progress-events'
1717

1818
const WEBRTC_TRANSPORT = '/webrtc'
@@ -145,7 +145,7 @@ export class WebRTCTransport implements Transport<WebRTCDialEvents>, Startable {
145145
* For a circuit relay, this will be of the form
146146
* <relay address>/p2p/<relay-peer>/p2p-circuit/webrtc/p2p/<destination-peer>
147147
*/
148-
async dial (ma: Multiaddr, options: DialOptions<WebRTCDialEvents>): Promise<Connection> {
148+
async dial (ma: Multiaddr, options: DialTransportOptions<WebRTCDialEvents>): Promise<Connection> {
149149
this.log.trace('dialing address: %a', ma)
150150

151151
const { remoteAddress, peerConnection, muxerFactory } = await initiateConnection({
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CreateListenerOptions, DialOptions } from '@libp2p/interface'
1+
import type { CreateListenerOptions, DialTransportOptions } from '@libp2p/interface'
22

33
export interface WebRTCListenerOptions extends CreateListenerOptions {}
4-
export interface WebRTCDialOptions extends DialOptions {}
4+
export interface WebRTCDialOptions extends DialTransportOptions {}

0 commit comments

Comments
 (0)