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

Commit 8bfcbc9

Browse files
authored
fix: accept abort options in connection.newStream (#219)
In order to support aborting during multistream-select, accept an instance of `AbortOptions` as an argument to `connection.newStream`
1 parent d7675b4 commit 8bfcbc9

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

packages/libp2p-connection/src/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { symbol } from '@libp2p/interfaces/connection'
55
import type { Connection, ConnectionStat, Metadata, ProtocolStream, Stream } from '@libp2p/interfaces/connection'
66
import type { PeerId } from '@libp2p/interfaces/peer-id'
77
import { logger } from '@libp2p/logger'
8+
import type { AbortOptions } from '@libp2p/interfaces'
89

910
const log = logger('libp2p:connection')
1011

1112
interface ConnectionInit {
1213
remoteAddr: Multiaddr
1314
remotePeer: PeerId
14-
newStream: (protocols: string[]) => Promise<ProtocolStream>
15+
newStream: (protocols: string[], options?: AbortOptions) => Promise<ProtocolStream>
1516
close: () => Promise<void>
1617
getStreams: () => Stream[]
1718
stat: ConnectionStat
@@ -47,7 +48,7 @@ export class ConnectionImpl implements Connection {
4748
/**
4849
* Reference to the new stream function of the multiplexer
4950
*/
50-
private readonly _newStream: (protocols: string[]) => Promise<ProtocolStream>
51+
private readonly _newStream: (protocols: string[], options?: AbortOptions) => Promise<ProtocolStream>
5152
/**
5253
* Reference to the close function of the raw connection
5354
*/
@@ -102,7 +103,7 @@ export class ConnectionImpl implements Connection {
102103
/**
103104
* Create a new stream from this connection
104105
*/
105-
async newStream (protocols: string | string[]) {
106+
async newStream (protocols: string | string[], options?: AbortOptions) {
106107
if (this.stat.status === CLOSING) {
107108
throw errCode(new Error('the connection is being closed'), 'ERR_CONNECTION_BEING_CLOSED')
108109
}
@@ -115,7 +116,7 @@ export class ConnectionImpl implements Connection {
115116
protocols = [protocols]
116117
}
117118

118-
const { stream, protocol } = await this._newStream(protocols)
119+
const { stream, protocol } = await this._newStream(protocols, options)
119120

120121
this.addStream(stream, { protocol, metadata: {} })
121122

packages/libp2p-interface-compliance-tests/src/mocks/connection.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as STATUS from '@libp2p/interfaces/connection/status'
1515
import type { Multiaddr } from '@multiformats/multiaddr'
1616
import type { StreamMuxer } from '@libp2p/interfaces/stream-muxer'
1717
import { Components } from '@libp2p/interfaces/components'
18+
import type { AbortOptions } from '@libp2p/interfaces'
1819

1920
const log = logger('libp2p:mock-connection')
2021

@@ -65,7 +66,7 @@ class MockConnection implements Connection {
6566
this.maConn = maConn
6667
}
6768

68-
async newStream (protocols: string | string[]) {
69+
async newStream (protocols: string | string[], options?: AbortOptions) {
6970
if (!Array.isArray(protocols)) {
7071
protocols = [protocols]
7172
}
@@ -77,7 +78,7 @@ class MockConnection implements Connection {
7778
const id = `${Math.random()}`
7879
const stream: Stream = this.muxer.newStream(id)
7980
const mss = new Dialer(stream)
80-
const result = await mss.select(protocols)
81+
const result = await mss.select(protocols, options)
8182

8283
const streamData: ProtocolStream = {
8384
protocol: result.protocol,

packages/libp2p-interfaces/src/connection/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PeerId } from '../peer-id'
33
import type * as Status from './status.js'
44
import type { Duplex } from 'it-stream-types'
55
import type { MultiaddrConnection } from '../transport'
6+
import type { AbortOptions } from '..'
67

78
export interface Timeline {
89
open: number
@@ -87,7 +88,7 @@ export interface Connection {
8788
tags: string[]
8889
streams: Stream[]
8990

90-
newStream: (multicodecs: string | string[]) => Promise<ProtocolStream>
91+
newStream: (multicodecs: string | string[], options?: AbortOptions) => Promise<ProtocolStream>
9192
addStream: (stream: Stream, data: Partial<Metadata>) => void
9293
removeStream: (id: string) => void
9394
close: () => Promise<void>

0 commit comments

Comments
 (0)