Skip to content

Commit

Permalink
fix(@libp2p/webtransport): remove custom WebTransport types (#2022)
Browse files Browse the repository at this point in the history
TypeScript has shipped a release with built in WebTransport types
so use these instead.
  • Loading branch information
achingbrain authored Sep 5, 2023
1 parent a6be8f0 commit 0634e3b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
16 changes: 3 additions & 13 deletions packages/transport-webtransport/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ import type { StreamMuxerFactory, StreamMuxerInit, StreamMuxer } from '@libp2p/i
import type { Source } from 'it-stream-types'
import type { MultihashDigest } from 'multiformats/hashes/interface'

declare global {
var WebTransport: any
}

// https://www.w3.org/TR/webtransport/#web-transport-close-info
interface WebTransportCloseInfo {
closeCode: number
reason: string
}

interface WebTransportSessionCleanup {
(closeInfo?: WebTransportCloseInfo): void
}
Expand Down Expand Up @@ -197,7 +187,7 @@ class WebTransportTransport implements Transport {
yield val.value
}

if (val.done === true) {
if (val.done) {
break
}
}
Expand Down Expand Up @@ -230,7 +220,7 @@ class WebTransportTransport implements Transport {
return true
}

webtransportMuxer (wt: InstanceType<typeof WebTransport>, cleanUpWTSession: WebTransportSessionCleanup): StreamMuxerFactory {
webtransportMuxer (wt: WebTransport, cleanUpWTSession: WebTransportSessionCleanup): StreamMuxerFactory {
let streamIDCounter = 0
const config = this.config
return {
Expand All @@ -252,7 +242,7 @@ class WebTransportTransport implements Transport {
while (true) {
const { done, value: wtStream } = await reader.read()

if (done === true) {
if (done) {
break
}

Expand Down
7 changes: 5 additions & 2 deletions packages/transport-webtransport/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Source } from 'it-stream-types'

const log = logger('libp2p:webtransport:stream')

export async function webtransportBiDiStreamToStream (bidiStream: any, streamId: string, direction: Direction, activeStreams: Stream[], onStreamEnd: undefined | ((s: Stream) => void)): Promise<Stream> {
export async function webtransportBiDiStreamToStream (bidiStream: WebTransportBidirectionalStream, streamId: string, direction: Direction, activeStreams: Stream[], onStreamEnd: undefined | ((s: Stream) => void)): Promise<Stream> {
const writer = bidiStream.writable.getWriter()
const reader = bidiStream.readable.getReader()
await writer.ready
Expand Down Expand Up @@ -60,6 +60,9 @@ export async function webtransportBiDiStreamToStream (bidiStream: any, streamId:
abort (err: Error) {
if (!writerClosed) {
writer.abort(err)
.catch(err => {
log.error('could not abort stream', err)
})
writerClosed = true
}
readerClosed = true
Expand Down Expand Up @@ -140,7 +143,7 @@ export async function webtransportBiDiStreamToStream (bidiStream: any, streamId:
source: (async function * () {
while (true) {
const val = await reader.read()
if (val.done === true) {
if (val.done) {
readerClosed = true
if (writerClosed) {
cleanupStreamFromActiveStreams()
Expand Down
6 changes: 0 additions & 6 deletions packages/transport-webtransport/test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import { expect } from 'aegir/chai'
import { createLibp2p, type Libp2p } from 'libp2p'
import { webTransport } from '../src/index.js'

declare global {
interface Window {
WebTransport: any
}
}

describe('libp2p-webtransport', () => {
let node: Libp2p

Expand Down

0 comments on commit 0634e3b

Please sign in to comment.