@@ -11,13 +11,12 @@ import type { Duplex, Source } from 'it-stream-types'
11
11
import type { StreamMuxerFactory , StreamMuxerInit , StreamMuxer } from '@libp2p/interface-stream-muxer'
12
12
import { Uint8ArrayList } from 'uint8arraylist'
13
13
14
- const log = logger ( 'libp2p:webtransport' )
15
14
declare global {
16
- interface Window {
17
- WebTransport : any
18
- }
15
+ var WebTransport : any
19
16
}
20
17
18
+ const log = logger ( 'libp2p:webtransport' )
19
+
21
20
// @ts -expect-error - Not easy to combine these types.
22
21
const multibaseDecoder = Object . values ( bases ) . map ( b => b . decoder ) . reduce ( ( d , b ) => d . or ( b ) )
23
22
@@ -265,7 +264,7 @@ export interface WebTransportComponents {
265
264
peerId : PeerId
266
265
}
267
266
268
- class WebTransport implements Transport {
267
+ class WebTransportTransport implements Transport {
269
268
private readonly components : WebTransportComponents
270
269
private readonly config : Required < WebTransportInit >
271
270
@@ -299,7 +298,7 @@ class WebTransport implements Transport {
299
298
throw new Error ( 'Expected multiaddr to contain certhashes' )
300
299
}
301
300
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` , {
303
302
serverCertificateHashes : certhashes . map ( certhash => ( {
304
303
algorithm : 'sha-256' ,
305
304
value : certhash . digest
@@ -349,7 +348,7 @@ class WebTransport implements Transport {
349
348
return await options . upgrader . upgradeOutbound ( maConn , { skipEncryption : true , muxerFactory : this . webtransportMuxer ( wt ) , skipProtection : true } )
350
349
}
351
350
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 > {
353
352
const stream = await wt . createBidirectionalStream ( )
354
353
const writer = stream . writable . getWriter ( )
355
354
const reader = stream . readable . getReader ( )
@@ -359,7 +358,14 @@ class WebTransport implements Transport {
359
358
source : ( async function * ( ) {
360
359
while ( true ) {
361
360
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
+ }
363
369
}
364
370
} ) ( ) ,
365
371
sink : async function ( source : Source < Uint8Array > ) {
@@ -390,7 +396,7 @@ class WebTransport implements Transport {
390
396
return true
391
397
}
392
398
393
- webtransportMuxer ( wt : typeof window . WebTransport ) : StreamMuxerFactory {
399
+ webtransportMuxer ( wt : InstanceType < typeof WebTransport > ) : StreamMuxerFactory {
394
400
let streamIDCounter = 0
395
401
const config = this . config
396
402
return {
@@ -411,9 +417,11 @@ class WebTransport implements Transport {
411
417
const reader = wt . incomingBidirectionalStreams . getReader ( )
412
418
while ( true ) {
413
419
const { done, value : wtStream } = await reader . read ( )
420
+
414
421
if ( done === true ) {
415
422
break
416
423
}
424
+
417
425
if ( activeStreams . length >= config . maxInboundStreams ) {
418
426
// We've reached our limit, close this stream.
419
427
wtStream . writable . close ( ) . catch ( ( err : Error ) => {
@@ -482,5 +490,5 @@ class WebTransport implements Transport {
482
490
}
483
491
484
492
export function webTransport ( init : WebTransportInit = { } ) : ( components : WebTransportComponents ) => Transport {
485
- return ( components : WebTransportComponents ) => new WebTransport ( components , init )
493
+ return ( components : WebTransportComponents ) => new WebTransportTransport ( components , init )
486
494
}
0 commit comments