Skip to content

Commit 80051ab

Browse files
ronagruyadorno
authored andcommitted
http: cleanup ClientRequest oncreate
PR-URL: #36862 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daniele Belardi <dwon.dnl@gmail.com>
1 parent eaf378a commit 80051ab

1 file changed

Lines changed: 17 additions & 22 deletions

File tree

‎lib/_http_client.js‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const {
4444
constnet=require('net');
4545
consturl=require('url');
4646
constassert=require('internal/assert');
47+
const{ once }=require('internal/util');
4748
const{
4849
_checkIsHttpToken: checkIsHttpToken,
4950
debug,
@@ -240,8 +241,6 @@ function ClientRequest(input, options, cb) {
240241
this.host=host;
241242
this.protocol=protocol;
242243

243-
letcalled=false;
244-
245244
if(this.agent){
246245
// If there is an agent we should default to Connection:keep-alive,
247246
// but only if the Agent will actually reuse the connection!
@@ -305,18 +304,6 @@ function ClientRequest(input, options, cb) {
305304
options.headers);
306305
}
307306

308-
constoncreate=(err,socket)=>{
309-
if(called)
310-
return;
311-
called=true;
312-
if(err){
313-
process.nextTick(()=>this.emit('error',err));
314-
return;
315-
}
316-
this.onSocket(socket);
317-
this._deferToConnect(null,null,()=>this._flush());
318-
};
319-
320307
// initiate connection
321308
if(this.agent){
322309
this.agent.addRequest(this,options);
@@ -325,20 +312,27 @@ function ClientRequest(input, options, cb) {
325312
this._last=true;
326313
this.shouldKeepAlive=false;
327314
if(typeofoptions.createConnection==='function'){
328-
constnewSocket=options.createConnection(options,oncreate);
329-
if(newSocket&&!called){
330-
called=true;
331-
this.onSocket(newSocket);
332-
}else{
333-
return;
315+
constoncreate=once((err,socket)=>{
316+
if(err){
317+
process.nextTick(()=>this.emit('error',err));
318+
}else{
319+
this.onSocket(socket);
320+
}
321+
});
322+
323+
try{
324+
constnewSocket=options.createConnection(options,oncreate);
325+
if(newSocket){
326+
oncreate(null,newSocket);
327+
}
328+
}catch(err){
329+
oncreate(err);
334330
}
335331
}else{
336332
debug('CLIENT use net.createConnection',options);
337333
this.onSocket(net.createConnection(options));
338334
}
339335
}
340-
341-
this._deferToConnect(null,null,()=>this._flush());
342336
}
343337
ObjectSetPrototypeOf(ClientRequest.prototype,OutgoingMessage.prototype);
344338
ObjectSetPrototypeOf(ClientRequest,OutgoingMessage);
@@ -843,6 +837,7 @@ function onSocketNT(req, socket, err) {
843837
_destroy(req,null,err);
844838
}else{
845839
tickOnSocket(req,socket);
840+
req._flush();
846841
}
847842
}
848843

0 commit comments

Comments
 (0)