Skip to content

Commit

Permalink
chore: check connection streams
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Nov 14, 2024
1 parent b898a82 commit e566d76
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
10 changes: 5 additions & 5 deletions packages/interface-compliance-tests/src/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default (common: TestSetup<TransportTestFixtures>): void => {
.with.property('name', 'AbortError')
})

it('should close all streams when the connection closes', async () => {
it.only('should close all streams when the connection closes', async () => {
({ dialer, listener, dialAddrs } = await getSetup(common))

let incomingConnectionPromise: DeferredPromise<Connection> | undefined
Expand All @@ -164,14 +164,14 @@ export default (common: TestSetup<TransportTestFixtures>): void => {
remoteConn = await incomingConnectionPromise.promise
}

const streams: Stream[] = []

for (let i = 0; i < 5; i++) {
streams.push(await connection.newStream('/echo/1.0.0', {
await connection.newStream('/echo/1.0.0', {
maxOutboundStreams: 5
}))
})
}

const streams = connection.streams

// Close the connection and verify all streams have been closed
await connection.close()

Expand Down
13 changes: 10 additions & 3 deletions packages/transport-webrtc/src/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,16 @@ export class DataChannelMuxer implements StreamMuxer {
return
}

// lib-datachannel throws if `.getId` is called on a closed channel so
// memoize it
const id = channel.id

const stream = createStream({
channel,
direction: 'inbound',
onEnd: () => {
this.log('incoming channel %s ended with state %s', channel.id, channel.readyState)
this.#onStreamEnd(stream, channel)
this.log('incoming channel %s ended', id)
},
logger: this.logger,
...this.dataChannelOptions
Expand Down Expand Up @@ -241,15 +245,18 @@ export class DataChannelMuxer implements StreamMuxer {
newStream (): Stream {
// The spec says the label SHOULD be an empty string: https://github.com/libp2p/specs/blob/master/webrtc/README.md#rtcdatachannel-label
const channel = this.peerConnection.createDataChannel('')
// lib-datachannel throws if `.getId` is called on a closed channel so
// memoize it
const id = channel.id

this.log.trace('opened outgoing datachannel with channel id %s', channel.id)
this.log.trace('opened outgoing datachannel with channel id %s', id)

const stream = createStream({
channel,
direction: 'outbound',
onEnd: () => {
this.log('outgoing channel %s ended with state %s', channel.id, channel.readyState)
this.#onStreamEnd(stream, channel)
this.log('outgoing channel %s ended', id)
},
logger: this.logger,
...this.dataChannelOptions
Expand Down
8 changes: 6 additions & 2 deletions packages/transport-webrtc/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@ export class WebRTCStream extends AbstractStream {
}

async sendReset (): Promise<void> {
await this._sendFlag(Message.Flag.RESET)
try {
await this._sendFlag(Message.Flag.RESET)
} catch (err) {
this.log.error('failed to send reset - %e', err)
}
}

async sendCloseWrite (options: AbortOptions): Promise<void> {
Expand Down Expand Up @@ -362,7 +366,7 @@ export class WebRTCStream extends AbstractStream {

return true
} catch (err: any) {
this.log.error('could not send flag %s', flag.toString(), err)
this.log.error('could not send flag %s - %e', flag.toString(), err)
}

return false
Expand Down

0 comments on commit e566d76

Please sign in to comment.