Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

fix: make connection upgrade and encryption abortable #120

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/interfaces/src/crypto/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PeerId } from '../peer-id/types'
import { MultiaddrConnection } from '../transport/types'
import { AbortOptions } from '../types'

/**
* A libp2p crypto module must be compliant to this interface
Expand All @@ -10,11 +11,11 @@ export interface Crypto {
/**
* Encrypt outgoing data to the remote party.
*/
secureOutbound(localPeer: PeerId, connection: MultiaddrConnection, remotePeer: PeerId): Promise<SecureOutbound>;
secureOutbound(localPeer: PeerId, connection: MultiaddrConnection, remotePeer: PeerId, options?: AbortOptions): Promise<SecureOutbound>;
/**
* Decrypt incoming data.
*/
secureInbound(localPeer: PeerId, connection: MultiaddrConnection, remotePeer?: PeerId): Promise<SecureOutbound>;
secureInbound(localPeer: PeerId, connection: MultiaddrConnection, remotePeer?: PeerId, options?: AbortOptions): Promise<SecureOutbound>;
}

export type SecureOutbound = {
Expand Down
9 changes: 5 additions & 4 deletions packages/interfaces/src/transport/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import events from 'events'
import { Multiaddr } from 'multiaddr'
import Connection from '../connection/connection'
import { Sink } from '../stream-muxer/types'
import { AbortOptions } from '../types'

export interface TransportFactory<DialOptions extends { signal?: AbortSignal }, ListenerOptions> {
export interface TransportFactory<DialOptions extends AbortOptions, ListenerOptions> {
new(upgrader: Upgrader): Transport<DialOptions, ListenerOptions>;
}

/**
* A libp2p transport is understood as something that offers a dial and listen interface to establish connections.
*/
export interface Transport <DialOptions extends { signal?: AbortSignal }, ListenerOptions> {
export interface Transport <DialOptions extends AbortOptions, ListenerOptions> {
/**
* Dial a given multiaddr.
*/
Expand Down Expand Up @@ -47,12 +48,12 @@ export interface Upgrader {
/**
* Upgrades an outbound connection on `transport.dial`.
*/
upgradeOutbound(maConn: MultiaddrConnection): Promise<Connection>;
upgradeOutbound(maConn: MultiaddrConnection, options?: AbortOptions): Promise<Connection>;

/**
* Upgrades an inbound connection on transport listener.
*/
upgradeInbound(maConn: MultiaddrConnection): Promise<Connection>;
upgradeInbound(maConn: MultiaddrConnection, options?: AbortOptions): Promise<Connection>;
}

export type MultiaddrConnectionTimeline = {
Expand Down
4 changes: 4 additions & 0 deletions packages/interfaces/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export type ValidateFn = (a: Uint8Array, b: Uint8Array) => Promise<void>

export type DhtSelectors = { [key: string]: SelectFn }
export type DhtValidators = { [key: string]: { func: ValidateFn } }

export interface AbortOptions {
signal?: AbortSignal
}