@@ -58,7 +58,7 @@ const Agent = require('_http_agent');
58
58
const { Buffer } = require ( 'buffer' ) ;
59
59
const { defaultTriggerAsyncIdScope } = require ( 'internal/async_hooks' ) ;
60
60
const { URL , urlToHttpOptions, searchParamsSymbol } = require ( 'internal/url' ) ;
61
- const { kOutHeaders } = require ( 'internal/http' ) ;
61
+ const { kOutHeaders, kNeedDrain } = require ( 'internal/http' ) ;
62
62
const { connResetException, codes } = require ( 'internal/errors' ) ;
63
63
const {
64
64
ERR_HTTP_HEADERS_SENT ,
@@ -98,7 +98,7 @@ class HTTPClientAsyncResource {
98
98
}
99
99
100
100
function ClientRequest ( input , options , cb ) {
101
- FunctionPrototypeCall ( OutgoingMessage , this , { autoDestroy : false } ) ;
101
+ FunctionPrototypeCall ( OutgoingMessage , this ) ;
102
102
103
103
if ( typeof input === 'string' ) {
104
104
const urlStr = input ;
@@ -304,7 +304,7 @@ function ClientRequest(input, options, cb) {
304
304
if ( typeof optsWithoutSignal . createConnection === 'function' ) {
305
305
const oncreate = once ( ( err , socket ) => {
306
306
if ( err ) {
307
- process . nextTick ( ( ) => emitError ( this , err ) ) ;
307
+ process . nextTick ( ( ) => this . emit ( 'error' , err ) ) ;
308
308
} else {
309
309
this . onSocket ( socket ) ;
310
310
}
@@ -373,8 +373,8 @@ function emitAbortNT(req) {
373
373
374
374
function ondrain ( ) {
375
375
const msg = this . _httpMessage ;
376
- if ( msg && ! msg . finished && msg . _writableState . needDrain ) {
377
- msg . _writableState . needDrain = false ;
376
+ if ( msg && ! msg . finished && msg [ kNeedDrain ] ) {
377
+ msg [ kNeedDrain ] = false ;
378
378
msg . emit ( 'drain' ) ;
379
379
}
380
380
}
@@ -400,7 +400,8 @@ function socketCloseListener() {
400
400
if ( ! res . complete ) {
401
401
res . destroy ( connResetException ( 'aborted' ) ) ;
402
402
}
403
- emitClose ( req ) ;
403
+ req . _closed = true ;
404
+ req . emit ( 'close' ) ;
404
405
if ( ! res . aborted && res . readable ) {
405
406
res . push ( null ) ;
406
407
}
@@ -410,9 +411,10 @@ function socketCloseListener() {
410
411
// receive a response. The error needs to
411
412
// fire on the request.
412
413
req . socket . _hadError = true ;
413
- emitError ( req , connResetException ( 'socket hang up' ) ) ;
414
+ req . emit ( 'error' , connResetException ( 'socket hang up' ) ) ;
414
415
}
415
- emitClose ( req ) ;
416
+ req . _closed = true ;
417
+ req . emit ( 'close' ) ;
416
418
}
417
419
418
420
// Too bad. That output wasn't getting written.
@@ -436,7 +438,7 @@ function socketErrorListener(err) {
436
438
// For Safety. Some additional errors might fire later on
437
439
// and we need to make sure we don't double-fire the error event.
438
440
req . socket . _hadError = true ;
439
- emitError ( req , err ) ;
441
+ req . emit ( 'error' , err ) ;
440
442
}
441
443
442
444
const parser = socket . parser ;
@@ -460,7 +462,7 @@ function socketOnEnd() {
460
462
// If we don't have a response then we know that the socket
461
463
// ended prematurely and we need to emit an error on the request.
462
464
req . socket . _hadError = true ;
463
- emitError ( req , connResetException ( 'socket hang up' ) ) ;
465
+ req . emit ( 'error' , connResetException ( 'socket hang up' ) ) ;
464
466
}
465
467
if ( parser ) {
466
468
parser . finish ( ) ;
@@ -483,7 +485,7 @@ function socketOnData(d) {
483
485
freeParser ( parser , req , socket ) ;
484
486
socket . destroy ( ) ;
485
487
req . socket . _hadError = true ;
486
- emitError ( req , ret ) ;
488
+ req . emit ( 'error' , ret ) ;
487
489
} else if ( parser . incoming && parser . incoming . upgrade ) {
488
490
// Upgrade (if status code 101) or CONNECT
489
491
const bytesParsed = ret ;
@@ -515,7 +517,9 @@ function socketOnData(d) {
515
517
socket . readableFlowing = null ;
516
518
517
519
req . emit ( eventName , res , socket , bodyHead ) ;
518
- emitClose ( req ) ;
520
+ req . destroyed = true ;
521
+ req . _closed = true ;
522
+ req . emit ( 'close' ) ;
519
523
} else {
520
524
// Requested Upgrade or used CONNECT method, but have no handler.
521
525
socket . destroy ( ) ;
@@ -700,7 +704,8 @@ function requestOnPrefinish() {
700
704
}
701
705
702
706
function emitFreeNT ( req ) {
703
- emitClose ( req ) ;
707
+ req . _closed = true ;
708
+ req . emit ( 'close' ) ;
704
709
if ( req . socket ) {
705
710
req . socket . emit ( 'free' ) ;
706
711
}
@@ -781,10 +786,10 @@ function onSocketNT(req, socket, err) {
781
786
err = connResetException ( 'socket hang up' ) ;
782
787
}
783
788
if ( err ) {
784
- emitError ( req , err ) ;
789
+ req . emit ( 'error' , err ) ;
785
790
}
786
791
req . _closed = true ;
787
- emitClose ( req ) ;
792
+ req . emit ( 'close' ) ;
788
793
}
789
794
790
795
if ( socket ) {
@@ -864,23 +869,6 @@ function setSocketTimeout(sock, msecs) {
864
869
}
865
870
}
866
871
867
- function emitError ( req , err ) {
868
- req . destroyed = true ;
869
- req . _writableState . errored = err ;
870
- // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
871
- err . stack ; // eslint-disable-line no-unused-expressions
872
- req . _writableState . errorEmitted = true ;
873
- req . emit ( 'error' , err ) ;
874
- }
875
-
876
- function emitClose ( req ) {
877
- req . destroyed = true ;
878
- req . _closed = true ;
879
- req . _writableState . closed = true ;
880
- req . _writableState . closeEmitted = true ;
881
- req . emit ( 'close' ) ;
882
- }
883
-
884
872
ClientRequest . prototype . setNoDelay = function setNoDelay ( noDelay ) {
885
873
this . _deferToConnect ( 'setNoDelay' , [ noDelay ] ) ;
886
874
} ;
0 commit comments