From 0634e3b704e98892bd55dfd1506963d31ad4fd0b Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 5 Sep 2023 13:40:19 +0100 Subject: [PATCH] fix(@libp2p/webtransport): remove custom WebTransport types (#2022) TypeScript has shipped a release with built in WebTransport types so use these instead. --- packages/transport-webtransport/src/index.ts | 16 +++------------- packages/transport-webtransport/src/stream.ts | 7 +++++-- packages/transport-webtransport/test/browser.ts | 6 ------ 3 files changed, 8 insertions(+), 21 deletions(-) diff --git a/packages/transport-webtransport/src/index.ts b/packages/transport-webtransport/src/index.ts index e7d37dbf43..3144df92d2 100644 --- a/packages/transport-webtransport/src/index.ts +++ b/packages/transport-webtransport/src/index.ts @@ -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 } @@ -197,7 +187,7 @@ class WebTransportTransport implements Transport { yield val.value } - if (val.done === true) { + if (val.done) { break } } @@ -230,7 +220,7 @@ class WebTransportTransport implements Transport { return true } - webtransportMuxer (wt: InstanceType, cleanUpWTSession: WebTransportSessionCleanup): StreamMuxerFactory { + webtransportMuxer (wt: WebTransport, cleanUpWTSession: WebTransportSessionCleanup): StreamMuxerFactory { let streamIDCounter = 0 const config = this.config return { @@ -252,7 +242,7 @@ class WebTransportTransport implements Transport { while (true) { const { done, value: wtStream } = await reader.read() - if (done === true) { + if (done) { break } diff --git a/packages/transport-webtransport/src/stream.ts b/packages/transport-webtransport/src/stream.ts index b0fb99e32d..8d9a1e8a3a 100644 --- a/packages/transport-webtransport/src/stream.ts +++ b/packages/transport-webtransport/src/stream.ts @@ -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 { +export async function webtransportBiDiStreamToStream (bidiStream: WebTransportBidirectionalStream, streamId: string, direction: Direction, activeStreams: Stream[], onStreamEnd: undefined | ((s: Stream) => void)): Promise { const writer = bidiStream.writable.getWriter() const reader = bidiStream.readable.getReader() await writer.ready @@ -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 @@ -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() diff --git a/packages/transport-webtransport/test/browser.ts b/packages/transport-webtransport/test/browser.ts index 0cf27d2ddf..adf60845c8 100644 --- a/packages/transport-webtransport/test/browser.ts +++ b/packages/transport-webtransport/test/browser.ts @@ -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