@@ -206,7 +206,6 @@ function normalizeArgs(args) {
206
206
// called when creating new Socket, or when re-using a closed Socket
207
207
function initSocketHandle ( self ) {
208
208
self . _undestroy ( ) ;
209
- self . _bytesDispatched = 0 ;
210
209
self . _sockname = null ;
211
210
212
211
// Handle creation may be deferred to bind() or connect() time.
@@ -222,7 +221,8 @@ function initSocketHandle(self) {
222
221
}
223
222
224
223
225
- const BYTES_READ = Symbol ( 'bytesRead' ) ;
224
+ const kBytesRead = Symbol ( 'kBytesRead' ) ;
225
+ const kBytesWritten = Symbol ( 'kBytesWritten' ) ;
226
226
227
227
228
228
function Socket ( options ) {
@@ -278,6 +278,11 @@ function Socket(options) {
278
278
279
279
this . _writev = null ;
280
280
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
+ } ) ;
281
286
}
282
287
} else {
283
288
// these will be set once there is a connection
@@ -316,7 +321,8 @@ function Socket(options) {
316
321
this . _server = null ;
317
322
318
323
// Used after `.destroy()`
319
- this [ BYTES_READ ] = 0 ;
324
+ this [ kBytesRead ] = 0 ;
325
+ this [ kBytesWritten ] = 0 ;
320
326
}
321
327
util . inherits ( Socket , stream . Duplex ) ;
322
328
@@ -588,8 +594,9 @@ Socket.prototype._destroy = function(exception, cb) {
588
594
if ( this !== process . stderr )
589
595
debug ( 'close handle' ) ;
590
596
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 ;
593
600
594
601
this . _handle . close ( ( ) => {
595
602
debug ( 'emit close' ) ;
@@ -689,7 +696,7 @@ function protoGetter(name, callback) {
689
696
}
690
697
691
698
protoGetter ( 'bytesRead' , function bytesRead ( ) {
692
- return this . _handle ? this . _handle . bytesRead : this [ BYTES_READ ] ;
699
+ return this . _handle ? this . _handle . bytesRead : this [ kBytesRead ] ;
693
700
} ) ;
694
701
695
702
protoGetter ( 'remoteAddress' , function remoteAddress ( ) {
@@ -761,8 +768,6 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
761
768
// Bail out if handle.write* returned an error
762
769
if ( ret ) return ret ;
763
770
764
- this . _bytesDispatched += req . bytes ;
765
-
766
771
if ( ! req . async ) {
767
772
cb ( ) ;
768
773
return ;
@@ -782,6 +787,13 @@ Socket.prototype._write = function(data, encoding, cb) {
782
787
this . _writeGeneric ( false , data , encoding , cb ) ;
783
788
} ;
784
789
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
+
785
797
protoGetter ( 'bytesWritten' , function bytesWritten ( ) {
786
798
var bytes = this . _bytesDispatched ;
787
799
const state = this . _writableState ;
0 commit comments