@@ -332,163 +332,168 @@ class QuicSessionStats {
332332 * @param {symbol } privateSymbol
333333 * @param {ArrayBuffer } buffer
334334 */
335- constructor ( privateSymbol , buffer ) {
335+ /** @type {number } */
336+ #offset = 0 ;
337+
338+ constructor ( privateSymbol , view , byteOffset = 0 ) {
336339// We use the kPrivateConstructor symbol to restrict the ability to
337340// create new instances of QuicSessionStats to internal code.
338341if ( privateSymbol !== kPrivateConstructor ) {
339342throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
340343}
341- if ( ! isArrayBuffer ( buffer ) ) {
342- throw new ERR_INVALID_ARG_TYPE ( 'buffer' , [ 'ArrayBuffer' ] , buffer ) ;
344+ if ( isArrayBuffer ( view ) ) {
345+ this . #handle = new BigUint64Array ( view ) ;
346+ } else {
347+ this . #handle = view ;
343348}
344- this . #handle = new BigUint64Array ( buffer ) ;
349+ this . #offset = byteOffset / 8 ;
345350}
346351
347352/** @type {bigint } */
348353get createdAt ( ) {
349- return this . #handle[ IDX_STATS_SESSION_CREATED_AT ] ;
354+ return this . #handle[ this . #offset + IDX_STATS_SESSION_CREATED_AT ] ;
350355}
351356
352357/** @type {bigint } */
353358get destroyedAt ( ) {
354- return this . #handle[ IDX_STATS_SESSION_DESTROYED_AT ] ;
359+ return this . #handle[ this . #offset + IDX_STATS_SESSION_DESTROYED_AT ] ;
355360}
356361
357362/** @type {bigint } */
358363get closingAt ( ) {
359- return this . #handle[ IDX_STATS_SESSION_CLOSING_AT ] ;
364+ return this . #handle[ this . #offset + IDX_STATS_SESSION_CLOSING_AT ] ;
360365}
361366
362367/** @type {bigint } */
363368get handshakeCompletedAt ( ) {
364- return this . #handle[ IDX_STATS_SESSION_HANDSHAKE_COMPLETED_AT ] ;
369+ return this . #handle[ this . #offset + IDX_STATS_SESSION_HANDSHAKE_COMPLETED_AT ] ;
365370}
366371
367372/** @type {bigint } */
368373get handshakeConfirmedAt ( ) {
369- return this . #handle[ IDX_STATS_SESSION_HANDSHAKE_CONFIRMED_AT ] ;
374+ return this . #handle[ this . #offset + IDX_STATS_SESSION_HANDSHAKE_CONFIRMED_AT ] ;
370375}
371376
372377/** @type {bigint } */
373378get bytesReceived ( ) {
374- return this . #handle[ IDX_STATS_SESSION_BYTES_RECEIVED ] ;
379+ return this . #handle[ this . #offset + IDX_STATS_SESSION_BYTES_RECEIVED ] ;
375380}
376381
377382/** @type {bigint } */
378383get bidiInStreamCount ( ) {
379- return this . #handle[ IDX_STATS_SESSION_BIDI_IN_STREAM_COUNT ] ;
384+ return this . #handle[ this . #offset + IDX_STATS_SESSION_BIDI_IN_STREAM_COUNT ] ;
380385}
381386
382387/** @type {bigint } */
383388get bidiOutStreamCount ( ) {
384- return this . #handle[ IDX_STATS_SESSION_BIDI_OUT_STREAM_COUNT ] ;
389+ return this . #handle[ this . #offset + IDX_STATS_SESSION_BIDI_OUT_STREAM_COUNT ] ;
385390}
386391
387392/** @type {bigint } */
388393get uniInStreamCount ( ) {
389- return this . #handle[ IDX_STATS_SESSION_UNI_IN_STREAM_COUNT ] ;
394+ return this . #handle[ this . #offset + IDX_STATS_SESSION_UNI_IN_STREAM_COUNT ] ;
390395}
391396
392397/** @type {bigint } */
393398get uniOutStreamCount ( ) {
394- return this . #handle[ IDX_STATS_SESSION_UNI_OUT_STREAM_COUNT ] ;
399+ return this . #handle[ this . #offset + IDX_STATS_SESSION_UNI_OUT_STREAM_COUNT ] ;
395400}
396401
397402/** @type {bigint } */
398403get maxBytesInFlight ( ) {
399- return this . #handle[ IDX_STATS_SESSION_MAX_BYTES_IN_FLIGHT ] ;
404+ return this . #handle[ this . #offset + IDX_STATS_SESSION_MAX_BYTES_IN_FLIGHT ] ;
400405}
401406
402407/** @type {bigint } */
403408get bytesInFlight ( ) {
404- return this . #handle[ IDX_STATS_SESSION_BYTES_IN_FLIGHT ] ;
409+ return this . #handle[ this . #offset + IDX_STATS_SESSION_BYTES_IN_FLIGHT ] ;
405410}
406411
407412/** @type {bigint } */
408413get blockCount ( ) {
409- return this . #handle[ IDX_STATS_SESSION_BLOCK_COUNT ] ;
414+ return this . #handle[ this . #offset + IDX_STATS_SESSION_BLOCK_COUNT ] ;
410415}
411416
412417/** @type {bigint } */
413418get cwnd ( ) {
414- return this . #handle[ IDX_STATS_SESSION_CWND ] ;
419+ return this . #handle[ this . #offset + IDX_STATS_SESSION_CWND ] ;
415420}
416421
417422/** @type {bigint } */
418423get latestRtt ( ) {
419- return this . #handle[ IDX_STATS_SESSION_LATEST_RTT ] ;
424+ return this . #handle[ this . #offset + IDX_STATS_SESSION_LATEST_RTT ] ;
420425}
421426
422427/** @type {bigint } */
423428get minRtt ( ) {
424- return this . #handle[ IDX_STATS_SESSION_MIN_RTT ] ;
429+ return this . #handle[ this . #offset + IDX_STATS_SESSION_MIN_RTT ] ;
425430}
426431
427432/** @type {bigint } */
428433get rttVar ( ) {
429- return this . #handle[ IDX_STATS_SESSION_RTTVAR ] ;
434+ return this . #handle[ this . #offset + IDX_STATS_SESSION_RTTVAR ] ;
430435}
431436
432437/** @type {bigint } */
433438get smoothedRtt ( ) {
434- return this . #handle[ IDX_STATS_SESSION_SMOOTHED_RTT ] ;
439+ return this . #handle[ this . #offset + IDX_STATS_SESSION_SMOOTHED_RTT ] ;
435440}
436441
437442/** @type {bigint } */
438443get ssthresh ( ) {
439- return this . #handle[ IDX_STATS_SESSION_SSTHRESH ] ;
444+ return this . #handle[ this . #offset + IDX_STATS_SESSION_SSTHRESH ] ;
440445}
441446
442447get pktSent ( ) {
443- return this . #handle[ IDX_STATS_SESSION_PKT_SENT ] ;
448+ return this . #handle[ this . #offset + IDX_STATS_SESSION_PKT_SENT ] ;
444449}
445450
446451get bytesSent ( ) {
447- return this . #handle[ IDX_STATS_SESSION_BYTES_SENT ] ;
452+ return this . #handle[ this . #offset + IDX_STATS_SESSION_BYTES_SENT ] ;
448453}
449454
450455get pktRecv ( ) {
451- return this . #handle[ IDX_STATS_SESSION_PKT_RECV ] ;
456+ return this . #handle[ this . #offset + IDX_STATS_SESSION_PKT_RECV ] ;
452457}
453458
454459get bytesRecv ( ) {
455- return this . #handle[ IDX_STATS_SESSION_BYTES_RECV ] ;
460+ return this . #handle[ this . #offset + IDX_STATS_SESSION_BYTES_RECV ] ;
456461}
457462
458463get pktLost ( ) {
459- return this . #handle[ IDX_STATS_SESSION_PKT_LOST ] ;
464+ return this . #handle[ this . #offset + IDX_STATS_SESSION_PKT_LOST ] ;
460465}
461466
462467get bytesLost ( ) {
463- return this . #handle[ IDX_STATS_SESSION_BYTES_LOST ] ;
468+ return this . #handle[ this . #offset + IDX_STATS_SESSION_BYTES_LOST ] ;
464469}
465470
466471get pingRecv ( ) {
467- return this . #handle[ IDX_STATS_SESSION_PING_RECV ] ;
472+ return this . #handle[ this . #offset + IDX_STATS_SESSION_PING_RECV ] ;
468473}
469474
470475get pktDiscarded ( ) {
471- return this . #handle[ IDX_STATS_SESSION_PKT_DISCARDED ] ;
476+ return this . #handle[ this . #offset + IDX_STATS_SESSION_PKT_DISCARDED ] ;
472477}
473478
474479/** @type {bigint } */
475480get datagramsReceived ( ) {
476- return this . #handle[ IDX_STATS_SESSION_DATAGRAMS_RECEIVED ] ;
481+ return this . #handle[ this . #offset + IDX_STATS_SESSION_DATAGRAMS_RECEIVED ] ;
477482}
478483
479484/** @type {bigint } */
480485get datagramsSent ( ) {
481- return this . #handle[ IDX_STATS_SESSION_DATAGRAMS_SENT ] ;
486+ return this . #handle[ this . #offset + IDX_STATS_SESSION_DATAGRAMS_SENT ] ;
482487}
483488
484489/** @type {bigint } */
485490get datagramsAcknowledged ( ) {
486- return this . #handle[ IDX_STATS_SESSION_DATAGRAMS_ACKNOWLEDGED ] ;
491+ return this . #handle[ this . #offset + IDX_STATS_SESSION_DATAGRAMS_ACKNOWLEDGED ] ;
487492}
488493
489494/** @type {bigint } */
490495get datagramsLost ( ) {
491- return this . #handle[ IDX_STATS_SESSION_DATAGRAMS_LOST ] ;
496+ return this . #handle[ this . #offset + IDX_STATS_SESSION_DATAGRAMS_LOST ] ;
492497}
493498
494499toString ( ) {
@@ -590,88 +595,99 @@ class QuicSessionStats {
590595}
591596
592597[ kFinishClose ] ( ) {
593- // Snapshot the stats into a new BigUint64Array since the underlying
594- // buffer will be destroyed.
595- this . #handle = new BigUint64Array ( this . #handle) ;
598+ // Snapshot this session's stats slice into a standalone BigUint64Array.
599+ const count = IDX_STATS_SESSION_DATAGRAMS_LOST + 1 ;
600+ const snapshot = new BigUint64Array ( count ) ;
601+ for ( let i = 0 ; i < count ; i ++ ) {
602+ snapshot [ i ] = this . #handle[ this . #offset + i ] ;
603+ }
604+ this . #handle = snapshot ;
605+ this . #offset = 0 ;
596606this . #disconnected = true ;
597607}
598608}
599609
600610class QuicStreamStats {
601611/** @type {BigUint64Array } */
602612 #handle;
613+ /** @type {number } */
614+ #offset = 0 ;
603615/** type {boolean} */
604616 #disconnected = false ;
605617
606618/**
607619 * @param {symbol } privateSymbol
608- * @param {ArrayBuffer } buffer
620+ * @param {BigUint64Array|ArrayBuffer } view
621+ * @param {number } [byteOffset] - byte offset into the shared page view
609622 */
610- constructor ( privateSymbol , buffer ) {
623+ constructor ( privateSymbol , view , byteOffset = 0 ) {
611624// We use the kPrivateConstructor symbol to restrict the ability to
612625// create new instances of QuicStreamStats to internal code.
613626if ( privateSymbol !== kPrivateConstructor ) {
614627throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
615628}
616- if ( ! isArrayBuffer ( buffer ) ) {
617- throw new ERR_INVALID_ARG_TYPE ( 'buffer' , [ 'ArrayBuffer' ] , buffer ) ;
629+ if ( isArrayBuffer ( view ) ) {
630+ this . #handle = new BigUint64Array ( view ) ;
631+ } else {
632+ this . #handle = view ;
618633}
619- this . #handle = new BigUint64Array ( buffer ) ;
634+ // Convert byte offset to element offset (8 bytes per uint64).
635+ this . #offset = byteOffset / 8 ;
620636}
621637
622638/** @type {bigint } */
623639get createdAt ( ) {
624- return this . #handle[ IDX_STATS_STREAM_CREATED_AT ] ;
640+ return this . #handle[ this . #offset + IDX_STATS_STREAM_CREATED_AT ] ;
625641}
626642
627643/** @type {bigint } */
628644get openedAt ( ) {
629- return this . #handle[ IDX_STATS_STREAM_OPENED_AT ] ;
645+ return this . #handle[ this . #offset + IDX_STATS_STREAM_OPENED_AT ] ;
630646}
631647
632648/** @type {bigint } */
633649get receivedAt ( ) {
634- return this . #handle[ IDX_STATS_STREAM_RECEIVED_AT ] ;
650+ return this . #handle[ this . #offset + IDX_STATS_STREAM_RECEIVED_AT ] ;
635651}
636652
637653/** @type {bigint } */
638654get ackedAt ( ) {
639- return this . #handle[ IDX_STATS_STREAM_ACKED_AT ] ;
655+ return this . #handle[ this . #offset + IDX_STATS_STREAM_ACKED_AT ] ;
640656}
641657
642658/** @type {bigint } */
643659get destroyedAt ( ) {
644- return this . #handle[ IDX_STATS_STREAM_DESTROYED_AT ] ;
660+ return this . #handle[ this . #offset + IDX_STATS_STREAM_DESTROYED_AT ] ;
645661}
646662
647663/** @type {bigint } */
648664get bytesReceived ( ) {
649- return this . #handle[ IDX_STATS_STREAM_BYTES_RECEIVED ] ;
665+ return this . #handle[ this . #offset + IDX_STATS_STREAM_BYTES_RECEIVED ] ;
650666}
651667
652668/** @type {bigint } */
653669get bytesSent ( ) {
654- return this . #handle[ IDX_STATS_STREAM_BYTES_SENT ] ;
670+ return this . #handle[ this . #offset + IDX_STATS_STREAM_BYTES_SENT ] ;
655671}
656672
657673/** @type {bigint } */
658674get maxOffset ( ) {
659- return this . #handle[ IDX_STATS_STREAM_MAX_OFFSET ] ;
675+ return this . #handle[ this . #offset + IDX_STATS_STREAM_MAX_OFFSET ] ;
660676}
661677
662678/** @type {bigint } */
663679get maxOffsetAcknowledged ( ) {
664- return this . #handle[ IDX_STATS_STREAM_MAX_OFFSET_ACK ] ;
680+ return this . #handle[ this . #offset + IDX_STATS_STREAM_MAX_OFFSET_ACK ] ;
665681}
666682
667683/** @type {bigint } */
668684get maxOffsetReceived ( ) {
669- return this . #handle[ IDX_STATS_STREAM_MAX_OFFSET_RECV ] ;
685+ return this . #handle[ this . #offset + IDX_STATS_STREAM_MAX_OFFSET_RECV ] ;
670686}
671687
672688/** @type {bigint } */
673689get finalSize ( ) {
674- return this . #handle[ IDX_STATS_STREAM_FINAL_SIZE ] ;
690+ return this . #handle[ this . #offset + IDX_STATS_STREAM_FINAL_SIZE ] ;
675691}
676692
677693toString ( ) {
@@ -735,9 +751,14 @@ class QuicStreamStats {
735751}
736752
737753[ kFinishClose ] ( ) {
738- // Snapshot the stats into a new BigUint64Array since the underlying
739- // buffer will be destroyed.
740- this . #handle = new BigUint64Array ( this . #handle) ;
754+ // Snapshot this stream's stats slice into a standalone BigUint64Array.
755+ const count = IDX_STATS_STREAM_FINAL_SIZE + 1 ;
756+ const snapshot = new BigUint64Array ( count ) ;
757+ for ( let i = 0 ; i < count ; i ++ ) {
758+ snapshot [ i ] = this . #handle[ this . #offset + i ] ;
759+ }
760+ this . #handle = snapshot ;
761+ this . #offset = 0 ;
741762this . #disconnected = true ;
742763}
743764}
0 commit comments