@@ -206,7 +206,6 @@ function normalizeArgs(args) {
206206// called when creating new Socket, or when re-using a closed Socket
207207function initSocketHandle ( self ) {
208208 self . _undestroy ( ) ;
209- self . _bytesDispatched = 0 ;
210209 self . _sockname = null ;
211210
212211 // Handle creation may be deferred to bind() or connect() time.
@@ -222,7 +221,8 @@ function initSocketHandle(self) {
222221}
223222
224223
225- const BYTES_READ = Symbol ( 'bytesRead' ) ;
224+ const kBytesRead = Symbol ( 'kBytesRead' ) ;
225+ const kBytesWritten = Symbol ( 'kBytesWritten' ) ;
226226
227227
228228function Socket ( options ) {
@@ -278,6 +278,11 @@ function Socket(options) {
278278
279279 this . _writev = null ;
280280 this . _write = makeSyncWrite ( fd ) ;
281+ // makeSyncWrite adjusts this value like the original handle would, so
282+ // we need to let it do that by turning it into a writable, own property.
283+ Object . defineProperty ( this . _handle , 'bytesWritten' , {
284+ value : 0 , writable : true
285+ } ) ;
281286 }
282287 } else {
283288 // these will be set once there is a connection
@@ -316,7 +321,8 @@ function Socket(options) {
316321 this . _server = null ;
317322
318323 // Used after `.destroy()`
319- this [ BYTES_READ ] = 0 ;
324+ this [ kBytesRead ] = 0 ;
325+ this [ kBytesWritten ] = 0 ;
320326}
321327util . inherits ( Socket , stream . Duplex ) ;
322328
@@ -588,8 +594,9 @@ Socket.prototype._destroy = function(exception, cb) {
588594 if ( this !== process . stderr )
589595 debug ( 'close handle' ) ;
590596 var isException = exception ? true : false ;
591- // `bytesRead` should be accessible after `.destroy()`
592- this [ BYTES_READ ] = this . _handle . bytesRead ;
597+ // `bytesRead` and `kBytesWritten` should be accessible after `.destroy()`
598+ this [ kBytesRead ] = this . _handle . bytesRead ;
599+ this [ kBytesWritten ] = this . _handle . bytesWritten ;
593600
594601 this . _handle . close ( ( ) => {
595602 debug ( 'emit close' ) ;
@@ -689,7 +696,7 @@ function protoGetter(name, callback) {
689696}
690697
691698protoGetter ( 'bytesRead' , function bytesRead ( ) {
692- return this . _handle ? this . _handle . bytesRead : this [ BYTES_READ ] ;
699+ return this . _handle ? this . _handle . bytesRead : this [ kBytesRead ] ;
693700} ) ;
694701
695702protoGetter ( 'remoteAddress' , function remoteAddress ( ) {
@@ -761,8 +768,6 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
761768 // Bail out if handle.write* returned an error
762769 if ( ret ) return ret ;
763770
764- this . _bytesDispatched += req . bytes ;
765-
766771 if ( ! req . async ) {
767772 cb ( ) ;
768773 return ;
@@ -782,6 +787,13 @@ Socket.prototype._write = function(data, encoding, cb) {
782787 this . _writeGeneric ( false , data , encoding , cb ) ;
783788} ;
784789
790+
791+ // Legacy alias. Having this is probably being overly cautious, but it doesn't
792+ // really hurt anyone either. This can probably be removed safely if desired.
793+ protoGetter ( '_bytesDispatched' , function _bytesDispatched ( ) {
794+ return this . _handle ? this . _handle . bytesWritten : this [ kBytesWritten ] ;
795+ } ) ;
796+
785797protoGetter ( 'bytesWritten' , function bytesWritten ( ) {
786798 var bytes = this . _bytesDispatched ;
787799 const state = this . _writableState ;
0 commit comments