@@ -41,7 +41,7 @@ const {
4141 UV_EOF
4242} = internalBinding ( 'uv' ) ;
4343
44- const { Buffer } = require ( 'buffer' ) ;
44+ const { byteLength } = require ( 'buffer' ) . Buffer ;
4545const TTYWrap = process . binding ( 'tty_wrap' ) ;
4646const { ShutdownWrap } = internalBinding ( 'stream_wrap' ) ;
4747const {
@@ -78,6 +78,7 @@ const {
7878} = errors . codes ;
7979const { validateInt32, validateString } = require ( 'internal/validators' ) ;
8080const kLastWriteQueueSize = Symbol ( 'lastWriteQueueSize' ) ;
81+ const { isArrayBufferView } = require ( 'internal/util/types' ) ;
8182
8283// Lazy loaded to improve startup performance.
8384let cluster ;
@@ -791,28 +792,28 @@ protoGetter('bytesWritten', function bytesWritten() {
791792 return undefined ;
792793
793794 this . writableBuffer . forEach ( function ( el ) {
794- if ( el . chunk instanceof Buffer )
795- bytes += el . chunk . length ;
795+ if ( isArrayBufferView ( el . chunk ) )
796+ bytes += el . chunk . byteLength ;
796797 else
797- bytes += Buffer . byteLength ( el . chunk , el . encoding ) ;
798+ bytes += byteLength ( el . chunk , el . encoding ) ;
798799 } ) ;
799800
800801 if ( Array . isArray ( data ) ) {
801802 // was a writev, iterate over chunks to get total length
802803 for ( var i = 0 ; i < data . length ; i ++ ) {
803804 const chunk = data [ i ] ;
804805
805- if ( data . allBuffers || chunk instanceof Buffer )
806- bytes += chunk . length ;
806+ if ( data . allBuffers || isArrayBufferView ( chunk ) )
807+ bytes += chunk . byteLength ;
807808 else
808- bytes += Buffer . byteLength ( chunk . chunk , chunk . encoding ) ;
809+ bytes += byteLength ( chunk . chunk , chunk . encoding ) ;
809810 }
810811 } else if ( data ) {
811812 // Writes are either a string or a Buffer.
812813 if ( typeof data !== 'string' )
813- bytes += data . length ;
814+ bytes += data . byteLength ;
814815 else
815- bytes += Buffer . byteLength ( data , encoding ) ;
816+ bytes += byteLength ( data , encoding ) ;
816817 }
817818
818819 return bytes ;
0 commit comments