Skip to content

Commit 9c39d79

Browse files
benglcjihrig
authored andcommitted
http: use arrow fns for lexical this in Agent
PR-URL: #16475 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 87b4e3e commit 9c39d79

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

lib/_http_agent.js

+39-41
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,31 @@ function Agent(options) {
4646

4747
EventEmitter.call(this);
4848

49-
var self = this;
49+
this.defaultPort = 80;
50+
this.protocol = 'http:';
5051

51-
self.defaultPort = 80;
52-
self.protocol = 'http:';
53-
54-
self.options = util._extend({}, options);
52+
this.options = util._extend({}, options);
5553

5654
// don't confuse net and make it think that we're connecting to a pipe
57-
self.options.path = null;
58-
self.requests = {};
59-
self.sockets = {};
60-
self.freeSockets = {};
61-
self.keepAliveMsecs = self.options.keepAliveMsecs || 1000;
62-
self.keepAlive = self.options.keepAlive || false;
63-
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
64-
self.maxFreeSockets = self.options.maxFreeSockets || 256;
65-
66-
self.on('free', function(socket, options) {
67-
var name = self.getName(options);
55+
this.options.path = null;
56+
this.requests = {};
57+
this.sockets = {};
58+
this.freeSockets = {};
59+
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
60+
this.keepAlive = this.options.keepAlive || false;
61+
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
62+
this.maxFreeSockets = this.options.maxFreeSockets || 256;
63+
64+
this.on('free', (socket, options) => {
65+
var name = this.getName(options);
6866
debug('agent.on(free)', name);
6967

7068
if (socket.writable &&
71-
self.requests[name] && self.requests[name].length) {
72-
self.requests[name].shift().onSocket(socket);
73-
if (self.requests[name].length === 0) {
69+
this.requests[name] && this.requests[name].length) {
70+
this.requests[name].shift().onSocket(socket);
71+
if (this.requests[name].length === 0) {
7472
// don't leak
75-
delete self.requests[name];
73+
delete this.requests[name];
7674
}
7775
} else {
7876
// If there are no pending requests, then put it in
@@ -81,21 +79,21 @@ function Agent(options) {
8179
if (req &&
8280
req.shouldKeepAlive &&
8381
socket.writable &&
84-
self.keepAlive) {
85-
var freeSockets = self.freeSockets[name];
82+
this.keepAlive) {
83+
var freeSockets = this.freeSockets[name];
8684
var freeLen = freeSockets ? freeSockets.length : 0;
8785
var count = freeLen;
88-
if (self.sockets[name])
89-
count += self.sockets[name].length;
86+
if (this.sockets[name])
87+
count += this.sockets[name].length;
9088

91-
if (count > self.maxSockets || freeLen >= self.maxFreeSockets) {
89+
if (count > this.maxSockets || freeLen >= this.maxFreeSockets) {
9290
socket.destroy();
93-
} else if (self.keepSocketAlive(socket)) {
91+
} else if (this.keepSocketAlive(socket)) {
9492
freeSockets = freeSockets || [];
95-
self.freeSockets[name] = freeSockets;
93+
this.freeSockets[name] = freeSockets;
9694
socket[async_id_symbol] = -1;
9795
socket._httpMessage = null;
98-
self.removeSocket(socket, options);
96+
this.removeSocket(socket, options);
9997
freeSockets.push(socket);
10098
} else {
10199
// Implementation doesn't want to keep socket alive
@@ -196,39 +194,39 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
196194
};
197195

198196
Agent.prototype.createSocket = function createSocket(req, options, cb) {
199-
var self = this;
200197
options = util._extend({}, options);
201-
util._extend(options, self.options);
198+
util._extend(options, this.options);
202199
if (options.socketPath)
203200
options.path = options.socketPath;
204201

205202
if (!options.servername)
206203
options.servername = calculateServerName(options, req);
207204

208-
var name = self.getName(options);
205+
var name = this.getName(options);
209206
options._agentKey = name;
210207

211208
debug('createConnection', name, options);
212209
options.encoding = null;
213210
var called = false;
214-
const newSocket = self.createConnection(options, oncreate);
215-
if (newSocket)
216-
oncreate(null, newSocket);
217211

218-
function oncreate(err, s) {
212+
const oncreate = (err, s) => {
219213
if (called)
220214
return;
221215
called = true;
222216
if (err)
223217
return cb(err);
224-
if (!self.sockets[name]) {
225-
self.sockets[name] = [];
218+
if (!this.sockets[name]) {
219+
this.sockets[name] = [];
226220
}
227-
self.sockets[name].push(s);
228-
debug('sockets', name, self.sockets[name].length);
229-
installListeners(self, s, options);
221+
this.sockets[name].push(s);
222+
debug('sockets', name, this.sockets[name].length);
223+
installListeners(this, s, options);
230224
cb(null, s);
231-
}
225+
};
226+
227+
const newSocket = this.createConnection(options, oncreate);
228+
if (newSocket)
229+
oncreate(null, newSocket);
232230
};
233231

234232
function calculateServerName(options, req) {

0 commit comments

Comments
 (0)