@@ -7,6 +7,7 @@ import type {
7
7
Hostname ,
8
8
ServerCrypto ,
9
9
} from './types' ;
10
+ import type { Connection } from '@/native' ;
10
11
import dns from 'dns' ;
11
12
import { IPv4 , IPv6 , Validator } from 'ip-num' ;
12
13
import QUICConnectionId from './QUICConnectionId' ;
@@ -387,6 +388,46 @@ async function sleep(ms: number): Promise<void> {
387
388
return await new Promise < void > ( ( r ) => setTimeout ( r , ms ) ) ;
388
389
}
389
390
391
+ /**
392
+ * Useful for debug printing stream state
393
+ */
394
+ function streamStats (
395
+ connection : Connection ,
396
+ streamId : number ,
397
+ label : string ,
398
+ ) : string {
399
+ let streamWritable : string ;
400
+ try {
401
+ streamWritable = `${ connection . streamWritable ( streamId , 0 ) } ` ;
402
+ } catch ( e ) {
403
+ streamWritable = `threw ${ e . message } ` ;
404
+ }
405
+ let streamCapacity : string ;
406
+ try {
407
+ streamCapacity = `${ connection . streamCapacity ( streamId ) } ` ;
408
+ } catch ( e ) {
409
+ streamCapacity = `threw ${ e . message } ` ;
410
+ }
411
+ let readableIterator = false ;
412
+ for ( const streamIterElement of connection . readable ( ) ) {
413
+ if ( streamIterElement === streamId ) readableIterator = true ;
414
+ }
415
+ let writableIterator = false ;
416
+ for ( const streamIterElement of connection . writable ( ) ) {
417
+ if ( streamIterElement === streamId ) writableIterator = true ;
418
+ }
419
+ return `
420
+ ---${ label } ---
421
+ isReadable: ${ connection . isReadable ( ) } ,
422
+ readable iterator: ${ readableIterator } ,
423
+ streamReadable: ${ connection . streamReadable ( streamId ) } ,
424
+ streamFinished: ${ connection . streamFinished ( streamId ) } ,
425
+ writable iterator: ${ writableIterator } ,
426
+ streamWritable: ${ streamWritable } ,
427
+ streamCapacity: ${ streamCapacity } ,
428
+ ` ;
429
+ }
430
+
390
431
export {
391
432
isIPv4 ,
392
433
isIPv6 ,
@@ -413,4 +454,5 @@ export {
413
454
mintToken ,
414
455
validateToken ,
415
456
sleep ,
457
+ streamStats ,
416
458
} ;
0 commit comments