@@ -66,8 +66,7 @@ function isInvalidPath(s) {
66
66
}
67
67
68
68
function ClientRequest ( options , cb ) {
69
- var self = this ;
70
- OutgoingMessage . call ( self ) ;
69
+ OutgoingMessage . call ( this ) ;
71
70
72
71
if ( typeof options === 'string' ) {
73
72
options = url . parse ( options ) ;
@@ -95,12 +94,12 @@ function ClientRequest(options, cb) {
95
94
'Agent option must be an instance of http.Agent, undefined or false.'
96
95
) ;
97
96
}
98
- self . agent = agent ;
97
+ this . agent = agent ;
99
98
100
99
var protocol = options . protocol || defaultAgent . protocol ;
101
100
var expectedProtocol = defaultAgent . protocol ;
102
- if ( self . agent && self . agent . protocol )
103
- expectedProtocol = self . agent . protocol ;
101
+ if ( this . agent && this . agent . protocol )
102
+ expectedProtocol = this . agent . protocol ;
104
103
105
104
var path ;
106
105
if ( options . path ) {
@@ -121,15 +120,15 @@ function ClientRequest(options, cb) {
121
120
}
122
121
123
122
const defaultPort = options . defaultPort ||
124
- self . agent && self . agent . defaultPort ;
123
+ this . agent && this . agent . defaultPort ;
125
124
126
125
var port = options . port = options . port || defaultPort || 80 ;
127
126
var host = options . host = options . hostname || options . host || 'localhost' ;
128
127
129
128
var setHost = ( options . setHost === undefined ) ;
130
129
131
- self . socketPath = options . socketPath ;
132
- self . timeout = options . timeout ;
130
+ this . socketPath = options . socketPath ;
131
+ this . timeout = options . timeout ;
133
132
134
133
var method = options . method ;
135
134
var methodIsString = ( typeof method === 'string' ) ;
@@ -141,14 +140,14 @@ function ClientRequest(options, cb) {
141
140
if ( ! common . _checkIsHttpToken ( method ) ) {
142
141
throw new TypeError ( 'Method must be a valid HTTP token' ) ;
143
142
}
144
- method = self . method = method . toUpperCase ( ) ;
143
+ method = this . method = method . toUpperCase ( ) ;
145
144
} else {
146
- method = self . method = 'GET' ;
145
+ method = this . method = 'GET' ;
147
146
}
148
147
149
- self . path = options . path || '/' ;
148
+ this . path = options . path || '/' ;
150
149
if ( cb ) {
151
- self . once ( 'response' , cb ) ;
150
+ this . once ( 'response' , cb ) ;
152
151
}
153
152
154
153
var headersArray = Array . isArray ( options . headers ) ;
@@ -157,7 +156,7 @@ function ClientRequest(options, cb) {
157
156
var keys = Object . keys ( options . headers ) ;
158
157
for ( var i = 0 ; i < keys . length ; i ++ ) {
159
158
var key = keys [ i ] ;
160
- self . setHeader ( key , options . headers [ key ] ) ;
159
+ this . setHeader ( key , options . headers [ key ] ) ;
161
160
}
162
161
}
163
162
if ( host && ! this . getHeader ( 'host' ) && setHost ) {
@@ -190,21 +189,22 @@ function ClientRequest(options, cb) {
190
189
method === 'DELETE' ||
191
190
method === 'OPTIONS' ||
192
191
method === 'CONNECT' ) {
193
- self . useChunkedEncodingByDefault = false ;
192
+ this . useChunkedEncodingByDefault = false ;
194
193
} else {
195
- self . useChunkedEncodingByDefault = true ;
194
+ this . useChunkedEncodingByDefault = true ;
196
195
}
197
196
198
197
if ( headersArray ) {
199
- self . _storeHeader ( self . method + ' ' + self . path + ' HTTP/1.1\r\n' ,
198
+ this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
200
199
options . headers ) ;
201
- } else if ( self . getHeader ( 'expect' ) ) {
202
- if ( self . _header ) {
200
+ } else if ( this . getHeader ( 'expect' ) ) {
201
+ if ( this . _header ) {
203
202
throw new Error ( 'Can\'t render headers after they are sent to the ' +
204
203
'client' ) ;
205
204
}
206
- self . _storeHeader ( self . method + ' ' + self . path + ' HTTP/1.1\r\n' ,
207
- self [ outHeadersKey ] ) ;
205
+
206
+ this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
207
+ this [ outHeadersKey ] ) ;
208
208
}
209
209
210
210
this . _ended = false ;
@@ -216,72 +216,65 @@ function ClientRequest(options, cb) {
216
216
this . maxHeadersCount = null ;
217
217
218
218
var called = false ;
219
- if ( self . socketPath ) {
220
- self . _last = true ;
221
- self . shouldKeepAlive = false ;
219
+
220
+ const oncreate = ( err , socket ) => {
221
+ if ( called )
222
+ return ;
223
+ called = true ;
224
+ if ( err ) {
225
+ process . nextTick ( ( ) => this . emit ( 'error' , err ) ) ;
226
+ return ;
227
+ }
228
+ this . onSocket ( socket ) ;
229
+ this . _deferToConnect ( null , null , ( ) => this . _flush ( ) ) ;
230
+ } ;
231
+
232
+ if ( this . socketPath ) {
233
+ this . _last = true ;
234
+ this . shouldKeepAlive = false ;
222
235
const optionsPath = {
223
- path : self . socketPath ,
224
- timeout : self . timeout
236
+ path : this . socketPath ,
237
+ timeout : this . timeout
225
238
} ;
226
- const newSocket = self . agent . createConnection ( optionsPath , oncreate ) ;
239
+ const newSocket = this . agent . createConnection ( optionsPath , oncreate ) ;
227
240
if ( newSocket && ! called ) {
228
241
called = true ;
229
- self . onSocket ( newSocket ) ;
242
+ this . onSocket ( newSocket ) ;
230
243
} else {
231
244
return ;
232
245
}
233
- } else if ( self . agent ) {
246
+ } else if ( this . agent ) {
234
247
// If there is an agent we should default to Connection:keep-alive,
235
248
// but only if the Agent will actually reuse the connection!
236
249
// If it's not a keepAlive agent, and the maxSockets==Infinity, then
237
250
// there's never a case where this socket will actually be reused
238
- if ( ! self . agent . keepAlive && ! Number . isFinite ( self . agent . maxSockets ) ) {
239
- self . _last = true ;
240
- self . shouldKeepAlive = false ;
251
+ if ( ! this . agent . keepAlive && ! Number . isFinite ( this . agent . maxSockets ) ) {
252
+ this . _last = true ;
253
+ this . shouldKeepAlive = false ;
241
254
} else {
242
- self . _last = false ;
243
- self . shouldKeepAlive = true ;
255
+ this . _last = false ;
256
+ this . shouldKeepAlive = true ;
244
257
}
245
- self . agent . addRequest ( self , options ) ;
258
+ this . agent . addRequest ( this , options ) ;
246
259
} else {
247
260
// No agent, default to Connection:close.
248
- self . _last = true ;
249
- self . shouldKeepAlive = false ;
261
+ this . _last = true ;
262
+ this . shouldKeepAlive = false ;
250
263
if ( typeof options . createConnection === 'function' ) {
251
264
const newSocket = options . createConnection ( options , oncreate ) ;
252
265
if ( newSocket && ! called ) {
253
266
called = true ;
254
- self . onSocket ( newSocket ) ;
267
+ this . onSocket ( newSocket ) ;
255
268
} else {
256
269
return ;
257
270
}
258
271
} else {
259
272
debug ( 'CLIENT use net.createConnection' , options ) ;
260
- self . onSocket ( net . createConnection ( options ) ) ;
261
- }
262
- }
263
-
264
- function oncreate ( err , socket ) {
265
- if ( called )
266
- return ;
267
- called = true ;
268
- if ( err ) {
269
- process . nextTick ( function ( ) {
270
- self . emit ( 'error' , err ) ;
271
- } ) ;
272
- return ;
273
+ this . onSocket ( net . createConnection ( options ) ) ;
273
274
}
274
- self . onSocket ( socket ) ;
275
- self . _deferToConnect ( null , null , function ( ) {
276
- self . _flush ( ) ;
277
- self = null ;
278
- } ) ;
279
275
}
280
276
281
- self . _deferToConnect ( null , null , function ( ) {
282
- self . _flush ( ) ;
283
- self = null ;
284
- } ) ;
277
+ this . _deferToConnect ( null , null , ( ) => this . _flush ( ) ) ;
285
278
}
286
279
287
280
util . inherits ( ClientRequest , OutgoingMessage ) ;
@@ -304,7 +297,7 @@ ClientRequest.prototype._implicitHeader = function _implicitHeader() {
304
297
305
298
ClientRequest . prototype . abort = function abort ( ) {
306
299
if ( ! this . aborted ) {
307
- process . nextTick ( emitAbortNT , this ) ;
300
+ process . nextTick ( emitAbortNT . bind ( this ) ) ;
308
301
}
309
302
// Mark as aborting so we can avoid sending queued request data
310
303
// This is used as a truthy flag elsewhere. The use of Date.now is for
@@ -328,8 +321,8 @@ ClientRequest.prototype.abort = function abort() {
328
321
} ;
329
322
330
323
331
- function emitAbortNT ( self ) {
332
- self . emit ( 'abort' ) ;
324
+ function emitAbortNT ( ) {
325
+ this . emit ( 'abort' ) ;
333
326
}
334
327
335
328
@@ -681,26 +674,25 @@ function _deferToConnect(method, arguments_, cb) {
681
674
// calls that happen either now (when a socket is assigned) or
682
675
// in the future (when a socket gets assigned out of the pool and is
683
676
// eventually writable).
684
- var self = this ;
685
677
686
- function callSocketMethod ( ) {
678
+ const callSocketMethod = ( ) => {
687
679
if ( method )
688
- self . socket [ method ] . apply ( self . socket , arguments_ ) ;
680
+ this . socket [ method ] . apply ( this . socket , arguments_ ) ;
689
681
690
682
if ( typeof cb === 'function' )
691
683
cb ( ) ;
692
- }
684
+ } ;
693
685
694
- var onSocket = function onSocket ( ) {
695
- if ( self . socket . writable ) {
686
+ const onSocket = ( ) => {
687
+ if ( this . socket . writable ) {
696
688
callSocketMethod ( ) ;
697
689
} else {
698
- self . socket . once ( 'connect' , callSocketMethod ) ;
690
+ this . socket . once ( 'connect' , callSocketMethod ) ;
699
691
}
700
692
} ;
701
693
702
- if ( ! self . socket ) {
703
- self . once ( 'socket' , onSocket ) ;
694
+ if ( ! this . socket ) {
695
+ this . once ( 'socket' , onSocket ) ;
704
696
} else {
705
697
onSocket ( ) ;
706
698
}
@@ -709,10 +701,7 @@ function _deferToConnect(method, arguments_, cb) {
709
701
ClientRequest . prototype . setTimeout = function setTimeout ( msecs , callback ) {
710
702
if ( callback ) this . once ( 'timeout' , callback ) ;
711
703
712
- var self = this ;
713
- function emitTimeout ( ) {
714
- self . emit ( 'timeout' ) ;
715
- }
704
+ const emitTimeout = ( ) => this . emit ( 'timeout' ) ;
716
705
717
706
if ( this . socket && this . socket . writable ) {
718
707
if ( this . timeoutCb )
0 commit comments