@@ -7,6 +7,7 @@ import type {
77 Hostname ,
88 ServerCrypto ,
99} from './types' ;
10+ import type { Connection } from '@/native' ;
1011import dns from 'dns' ;
1112import { IPv4 , IPv6 , Validator } from 'ip-num' ;
1213import QUICConnectionId from './QUICConnectionId' ;
@@ -387,6 +388,46 @@ async function sleep(ms: number): Promise<void> {
387388 return await new Promise < void > ( ( r ) => setTimeout ( r , ms ) ) ;
388389}
389390
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+
390431export {
391432 isIPv4 ,
392433 isIPv6 ,
@@ -413,4 +454,5 @@ export {
413454 mintToken ,
414455 validateToken ,
415456 sleep ,
457+ streamStats ,
416458} ;
0 commit comments