@@ -11,13 +11,12 @@ import type { Duplex, Source } from 'it-stream-types'
1111import type { StreamMuxerFactory , StreamMuxerInit , StreamMuxer } from '@libp2p/interface-stream-muxer'
1212import { Uint8ArrayList } from 'uint8arraylist'
1313
14- const log = logger ( 'libp2p:webtransport' )
1514declare global {
16- interface Window {
17- WebTransport : any
18- }
15+ var WebTransport : any
1916}
2017
18+ const log = logger ( 'libp2p:webtransport' )
19+
2120// @ts -expect-error - Not easy to combine these types.
2221const multibaseDecoder = Object . values ( bases ) . map ( b => b . decoder ) . reduce ( ( d , b ) => d . or ( b ) )
2322
@@ -265,7 +264,7 @@ export interface WebTransportComponents {
265264 peerId : PeerId
266265}
267266
268- class WebTransport implements Transport {
267+ class WebTransportTransport implements Transport {
269268 private readonly components : WebTransportComponents
270269 private readonly config : Required < WebTransportInit >
271270
@@ -299,7 +298,7 @@ class WebTransport implements Transport {
299298 throw new Error ( 'Expected multiaddr to contain certhashes' )
300299 }
301300
302- const wt = new window . WebTransport ( `${ url } /.well-known/libp2p-webtransport?type=noise` , {
301+ const wt = new WebTransport ( `${ url } /.well-known/libp2p-webtransport?type=noise` , {
303302 serverCertificateHashes : certhashes . map ( certhash => ( {
304303 algorithm : 'sha-256' ,
305304 value : certhash . digest
@@ -349,7 +348,7 @@ class WebTransport implements Transport {
349348 return await options . upgrader . upgradeOutbound ( maConn , { skipEncryption : true , muxerFactory : this . webtransportMuxer ( wt ) , skipProtection : true } )
350349 }
351350
352- async authenticateWebTransport ( wt : typeof window . WebTransport , localPeer : PeerId , remotePeer : PeerId , certhashes : Array < MultihashDigest < number > > ) : Promise < boolean > {
351+ async authenticateWebTransport ( wt : InstanceType < typeof WebTransport > , localPeer : PeerId , remotePeer : PeerId , certhashes : Array < MultihashDigest < number > > ) : Promise < boolean > {
353352 const stream = await wt . createBidirectionalStream ( )
354353 const writer = stream . writable . getWriter ( )
355354 const reader = stream . readable . getReader ( )
@@ -359,7 +358,14 @@ class WebTransport implements Transport {
359358 source : ( async function * ( ) {
360359 while ( true ) {
361360 const val = await reader . read ( )
362- yield val . value
361+
362+ if ( val . value != null ) {
363+ yield val . value
364+ }
365+
366+ if ( val . done === true ) {
367+ break
368+ }
363369 }
364370 } ) ( ) ,
365371 sink : async function ( source : Source < Uint8Array > ) {
@@ -390,7 +396,7 @@ class WebTransport implements Transport {
390396 return true
391397 }
392398
393- webtransportMuxer ( wt : typeof window . WebTransport ) : StreamMuxerFactory {
399+ webtransportMuxer ( wt : InstanceType < typeof WebTransport > ) : StreamMuxerFactory {
394400 let streamIDCounter = 0
395401 const config = this . config
396402 return {
@@ -411,9 +417,11 @@ class WebTransport implements Transport {
411417 const reader = wt . incomingBidirectionalStreams . getReader ( )
412418 while ( true ) {
413419 const { done, value : wtStream } = await reader . read ( )
420+
414421 if ( done === true ) {
415422 break
416423 }
424+
417425 if ( activeStreams . length >= config . maxInboundStreams ) {
418426 // We've reached our limit, close this stream.
419427 wtStream . writable . close ( ) . catch ( ( err : Error ) => {
@@ -482,5 +490,5 @@ class WebTransport implements Transport {
482490}
483491
484492export function webTransport ( init : WebTransportInit = { } ) : ( components : WebTransportComponents ) => Transport {
485- return ( components : WebTransportComponents ) => new WebTransport ( components , init )
493+ return ( components : WebTransportComponents ) => new WebTransportTransport ( components , init )
486494}
0 commit comments