Skip to content

Commit a85436c

Browse files
committed
lib: var to const
1 parent 688765a commit a85436c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+819
-819
lines changed

lib/_http_agent.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function Agent(options) {
4646

4747
EventEmitter.call(this);
4848

49-
var self = this;
49+
const self = this;
5050

5151
self.defaultPort = 80;
5252
self.protocol = 'http:';
@@ -64,7 +64,7 @@ function Agent(options) {
6464
self.maxFreeSockets = self.options.maxFreeSockets || 256;
6565

6666
self.on('free', function(socket, options) {
67-
var name = self.getName(options);
67+
const name = self.getName(options);
6868
debug('agent.on(free)', name);
6969

7070
if (socket.writable &&
@@ -77,13 +77,13 @@ function Agent(options) {
7777
} else {
7878
// If there are no pending requests, then put it in
7979
// the freeSockets pool, but only if we're allowed to do so.
80-
var req = socket._httpMessage;
80+
const req = socket._httpMessage;
8181
if (req &&
8282
req.shouldKeepAlive &&
8383
socket.writable &&
8484
self.keepAlive) {
8585
var freeSockets = self.freeSockets[name];
86-
var freeLen = freeSockets ? freeSockets.length : 0;
86+
const freeLen = freeSockets ? freeSockets.length : 0;
8787
var count = freeLen;
8888
if (self.sockets[name])
8989
count += self.sockets[name].length;
@@ -156,17 +156,17 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
156156
}
157157
}
158158

159-
var name = this.getName(options);
159+
const name = this.getName(options);
160160
if (!this.sockets[name]) {
161161
this.sockets[name] = [];
162162
}
163163

164-
var freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0;
165-
var sockLen = freeLen + this.sockets[name].length;
164+
const freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0;
165+
const sockLen = freeLen + this.sockets[name].length;
166166

167167
if (freeLen) {
168168
// we have a free socket, so use that.
169-
var socket = this.freeSockets[name].shift();
169+
const socket = this.freeSockets[name].shift();
170170
// Guard against an uninitialized or user supplied Socket.
171171
if (socket._handle && typeof socket._handle.asyncReset === 'function') {
172172
// Assign the handle a new asyncId and run any init() hooks.
@@ -196,7 +196,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
196196
};
197197

198198
Agent.prototype.createSocket = function createSocket(req, options, cb) {
199-
var self = this;
199+
const self = this;
200200
options = util._extend({}, options);
201201
util._extend(options, self.options);
202202

@@ -208,7 +208,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
208208
}
209209
}
210210

211-
var name = self.getName(options);
211+
const name = self.getName(options);
212212
options._agentKey = name;
213213

214214
debug('createConnection', name, options);
@@ -264,19 +264,19 @@ function installListeners(agent, s, options) {
264264
}
265265

266266
Agent.prototype.removeSocket = function removeSocket(s, options) {
267-
var name = this.getName(options);
267+
const name = this.getName(options);
268268
debug('removeSocket', name, 'writable:', s.writable);
269-
var sets = [this.sockets];
269+
const sets = [this.sockets];
270270

271271
// If the socket was destroyed, remove it from the free buffers too.
272272
if (!s.writable)
273273
sets.push(this.freeSockets);
274274

275275
for (var sk = 0; sk < sets.length; sk++) {
276-
var sockets = sets[sk];
276+
const sockets = sets[sk];
277277

278278
if (sockets[name]) {
279-
var index = sockets[name].indexOf(s);
279+
const index = sockets[name].indexOf(s);
280280
if (index !== -1) {
281281
sockets[name].splice(index, 1);
282282
// Don't leak
@@ -288,7 +288,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
288288

289289
if (this.requests[name] && this.requests[name].length) {
290290
debug('removeSocket, have a request, make a socket');
291-
var req = this.requests[name][0];
291+
const req = this.requests[name][0];
292292
// If we have pending requests and a socket gets closed make a new one
293293
this.createSocket(req, options, handleSocketCreation(req, false));
294294
}
@@ -307,12 +307,12 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
307307
};
308308

309309
Agent.prototype.destroy = function destroy() {
310-
var sets = [this.freeSockets, this.sockets];
310+
const sets = [this.freeSockets, this.sockets];
311311
for (var s = 0; s < sets.length; s++) {
312-
var set = sets[s];
313-
var keys = Object.keys(set);
312+
const set = sets[s];
313+
const keys = Object.keys(set);
314314
for (var v = 0; v < keys.length; v++) {
315-
var setName = set[keys[v]];
315+
const setName = set[keys[v]];
316316
for (var n = 0; n < setName.length; n++) {
317317
setName[n].destroy();
318318
}

lib/_http_client.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function ClientRequest(options, cb) {
9292
}
9393

9494
var agent = options.agent;
95-
var defaultAgent = options._defaultAgent || Agent.globalAgent;
95+
const defaultAgent = options._defaultAgent || Agent.globalAgent;
9696
if (agent === false) {
9797
agent = new defaultAgent.constructor();
9898
} else if (agent === null || agent === undefined) {
@@ -107,7 +107,7 @@ function ClientRequest(options, cb) {
107107
}
108108
this.agent = agent;
109109

110-
var protocol = options.protocol || defaultAgent.protocol;
110+
const protocol = options.protocol || defaultAgent.protocol;
111111
var expectedProtocol = defaultAgent.protocol;
112112
if (this.agent && this.agent.protocol)
113113
expectedProtocol = this.agent.protocol;
@@ -129,20 +129,20 @@ function ClientRequest(options, cb) {
129129
throw new errors.Error('ERR_INVALID_PROTOCOL', protocol, expectedProtocol);
130130
}
131131

132-
var defaultPort = options.defaultPort ||
132+
const defaultPort = options.defaultPort ||
133133
this.agent && this.agent.defaultPort;
134134

135-
var port = options.port = options.port || defaultPort || 80;
136-
var host = options.host = validateHost(options.hostname, 'hostname') ||
135+
const port = options.port = options.port || defaultPort || 80;
136+
const host = options.host = validateHost(options.hostname, 'hostname') ||
137137
validateHost(options.host, 'host') || 'localhost';
138138

139-
var setHost = (options.setHost === undefined);
139+
const setHost = (options.setHost === undefined);
140140

141141
this.socketPath = options.socketPath;
142142
this.timeout = options.timeout;
143143

144144
var method = options.method;
145-
var methodIsString = (typeof method === 'string');
145+
const methodIsString = (typeof method === 'string');
146146
if (method != null && !methodIsString) {
147147
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'method',
148148
'string', method);
@@ -162,12 +162,12 @@ function ClientRequest(options, cb) {
162162
this.once('response', cb);
163163
}
164164

165-
var headersArray = Array.isArray(options.headers);
165+
const headersArray = Array.isArray(options.headers);
166166
if (!headersArray) {
167167
if (options.headers) {
168-
var keys = Object.keys(options.headers);
168+
const keys = Object.keys(options.headers);
169169
for (var i = 0; i < keys.length; i++) {
170-
var key = keys[i];
170+
const key = keys[i];
171171
this.setHeader(key, options.headers[key]);
172172
}
173173
}
@@ -177,7 +177,7 @@ function ClientRequest(options, cb) {
177177
// For the Host header, ensure that IPv6 addresses are enclosed
178178
// in square brackets, as defined by URI formatting
179179
// https://tools.ietf.org/html/rfc3986#section-3.2.2
180-
var posColon = hostHeader.indexOf(':');
180+
const posColon = hostHeader.indexOf(':');
181181
if (posColon !== -1 &&
182182
hostHeader.indexOf(':', posColon + 1) !== -1 &&
183183
hostHeader.charCodeAt(0) !== 91/*'['*/) {
@@ -228,7 +228,7 @@ function ClientRequest(options, cb) {
228228

229229
var called = false;
230230

231-
var oncreate = (err, socket) => {
231+
const oncreate = (err, socket) => {
232232
if (called)
233233
return;
234234
called = true;
@@ -244,7 +244,7 @@ function ClientRequest(options, cb) {
244244
if (this.socketPath) {
245245
this._last = true;
246246
this.shouldKeepAlive = false;
247-
var optionsPath = {
247+
const optionsPath = {
248248
path: this.socketPath,
249249
timeout: this.timeout,
250250
rejectUnauthorized: !!options.rejectUnauthorized
@@ -341,15 +341,15 @@ function emitAbortNT() {
341341

342342

343343
function createHangUpError() {
344-
var error = new Error('socket hang up');
344+
const error = new Error('socket hang up');
345345
error.code = 'ECONNRESET';
346346
return error;
347347
}
348348

349349

350350
function socketCloseListener() {
351-
var socket = this;
352-
var req = socket._httpMessage;
351+
const socket = this;
352+
const req = socket._httpMessage;
353353
debug('HTTP socket close');
354354

355355
// Pull through final chunk, if anything is buffered.
@@ -359,12 +359,12 @@ function socketCloseListener() {
359359

360360
// NOTE: It's important to get parser here, because it could be freed by
361361
// the `socketOnData`.
362-
var parser = socket.parser;
362+
const parser = socket.parser;
363363
req.emit('close');
364364
if (req.res && req.res.readable) {
365365
// Socket closed before we emitted 'end' below.
366366
req.res.emit('aborted');
367-
var res = req.res;
367+
const res = req.res;
368368
res.on('end', function() {
369369
res.emit('close');
370370
});
@@ -392,8 +392,8 @@ function socketCloseListener() {
392392
}
393393

394394
function socketErrorListener(err) {
395-
var socket = this;
396-
var req = socket._httpMessage;
395+
const socket = this;
396+
const req = socket._httpMessage;
397397
debug('SOCKET ERROR:', err.message, err.stack);
398398

399399
if (req) {
@@ -406,7 +406,7 @@ function socketErrorListener(err) {
406406
// Handle any pending data
407407
socket.read();
408408

409-
var parser = socket.parser;
409+
const parser = socket.parser;
410410
if (parser) {
411411
parser.finish();
412412
freeParser(parser, req, socket);
@@ -419,16 +419,16 @@ function socketErrorListener(err) {
419419
}
420420

421421
function freeSocketErrorListener(err) {
422-
var socket = this;
422+
const socket = this;
423423
debug('SOCKET ERROR on FREE socket:', err.message, err.stack);
424424
socket.destroy();
425425
socket.emit('agentRemove');
426426
}
427427

428428
function socketOnEnd() {
429-
var socket = this;
430-
var req = this._httpMessage;
431-
var parser = this.parser;
429+
const socket = this;
430+
const req = this._httpMessage;
431+
const parser = this.parser;
432432

433433
if (!req.res && !req.socket._hadError) {
434434
// If we don't have a response then we know that the socket
@@ -444,13 +444,13 @@ function socketOnEnd() {
444444
}
445445

446446
function socketOnData(d) {
447-
var socket = this;
448-
var req = this._httpMessage;
449-
var parser = this.parser;
447+
const socket = this;
448+
const req = this._httpMessage;
449+
const parser = this.parser;
450450

451451
assert(parser && parser.socket === socket);
452452

453-
var ret = parser.execute(d);
453+
const ret = parser.execute(d);
454454
if (ret instanceof Error) {
455455
debug('parse error', ret);
456456
freeParser(parser, req, socket);
@@ -459,17 +459,17 @@ function socketOnData(d) {
459459
req.emit('error', ret);
460460
} else if (parser.incoming && parser.incoming.upgrade) {
461461
// Upgrade or CONNECT
462-
var bytesParsed = ret;
463-
var res = parser.incoming;
462+
const bytesParsed = ret;
463+
const res = parser.incoming;
464464
req.res = res;
465465

466466
socket.removeListener('data', socketOnData);
467467
socket.removeListener('end', socketOnEnd);
468468
parser.finish();
469469

470-
var bodyHead = d.slice(bytesParsed, d.length);
470+
const bodyHead = d.slice(bytesParsed, d.length);
471471

472-
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
472+
const eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
473473
if (req.listenerCount(eventName) > 0) {
474474
req.upgradeOrConnect = true;
475475

@@ -503,8 +503,8 @@ function socketOnData(d) {
503503

504504
// client
505505
function parserOnIncomingClient(res, shouldKeepAlive) {
506-
var socket = this.socket;
507-
var req = socket._httpMessage;
506+
const socket = this.socket;
507+
const req = socket._httpMessage;
508508

509509

510510
// propagate "domain" setting...
@@ -534,7 +534,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
534534
// but *can* have a content-length which actually corresponds
535535
// to the content-length of the entity-body had the request
536536
// been a GET.
537-
var isHeadResponse = req.method === 'HEAD';
537+
const isHeadResponse = req.method === 'HEAD';
538538
debug('AGENT isHeadResponse', isHeadResponse);
539539

540540
if (res.statusCode === 100) {
@@ -561,7 +561,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
561561
// add our listener first, so that we guarantee socket cleanup
562562
res.on('end', responseOnEnd);
563563
req.on('prefinish', requestOnPrefinish);
564-
var handled = req.emit('response', res);
564+
const handled = req.emit('response', res);
565565

566566
// If the user did not listen for the 'response' event, then they
567567
// can't possibly read the data, so we ._dump() it into the void
@@ -574,7 +574,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
574574

575575
// client
576576
function responseKeepAlive(res, req) {
577-
var socket = req.socket;
577+
const socket = req.socket;
578578

579579
if (!req.shouldKeepAlive) {
580580
if (socket.writable) {
@@ -625,7 +625,7 @@ function emitFreeNT(socket) {
625625
}
626626

627627
function tickOnSocket(req, socket) {
628-
var parser = parsers.alloc();
628+
const parser = parsers.alloc();
629629
req.socket = socket;
630630
req.connection = socket;
631631
parser.reinitialize(HTTPParser.RESPONSE);
@@ -731,7 +731,7 @@ ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
731731
// Set timeoutCb so that it'll get cleaned up on request end
732732
this.timeoutCb = emitTimeout;
733733
if (this.socket) {
734-
var sock = this.socket;
734+
const sock = this.socket;
735735
this.socket.once('connect', function() {
736736
sock.setTimeout(msecs, emitTimeout);
737737
});

0 commit comments

Comments
 (0)