Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: var to const #13756

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Agent(options) {

EventEmitter.call(this);

var self = this;
const self = this;

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

self.on('free', function(socket, options) {
var name = self.getName(options);
const name = self.getName(options);
debug('agent.on(free)', name);

if (socket.writable &&
Expand All @@ -77,13 +77,13 @@ function Agent(options) {
} else {
// If there are no pending requests, then put it in
// the freeSockets pool, but only if we're allowed to do so.
var req = socket._httpMessage;
const req = socket._httpMessage;
if (req &&
req.shouldKeepAlive &&
socket.writable &&
self.keepAlive) {
var freeSockets = self.freeSockets[name];
var freeLen = freeSockets ? freeSockets.length : 0;
const freeLen = freeSockets ? freeSockets.length : 0;
var count = freeLen;
if (self.sockets[name])
count += self.sockets[name].length;
Expand Down Expand Up @@ -156,17 +156,17 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
}
}

var name = this.getName(options);
const name = this.getName(options);
if (!this.sockets[name]) {
this.sockets[name] = [];
}

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

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

Agent.prototype.createSocket = function createSocket(req, options, cb) {
var self = this;
const self = this;
options = util._extend({}, options);
util._extend(options, self.options);

Expand All @@ -208,7 +208,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
}
}

var name = self.getName(options);
const name = self.getName(options);
options._agentKey = name;

debug('createConnection', name, options);
Expand Down Expand Up @@ -264,19 +264,19 @@ function installListeners(agent, s, options) {
}

Agent.prototype.removeSocket = function removeSocket(s, options) {
var name = this.getName(options);
const name = this.getName(options);
debug('removeSocket', name, 'writable:', s.writable);
var sets = [this.sockets];
const sets = [this.sockets];

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

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

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

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

Agent.prototype.destroy = function destroy() {
var sets = [this.freeSockets, this.sockets];
const sets = [this.freeSockets, this.sockets];
for (var s = 0; s < sets.length; s++) {
var set = sets[s];
var keys = Object.keys(set);
const set = sets[s];
const keys = Object.keys(set);
for (var v = 0; v < keys.length; v++) {
var setName = set[keys[v]];
const setName = set[keys[v]];
for (var n = 0; n < setName.length; n++) {
setName[n].destroy();
}
Expand Down
80 changes: 40 additions & 40 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function ClientRequest(options, cb) {
}

var agent = options.agent;
var defaultAgent = options._defaultAgent || Agent.globalAgent;
const defaultAgent = options._defaultAgent || Agent.globalAgent;
if (agent === false) {
agent = new defaultAgent.constructor();
} else if (agent === null || agent === undefined) {
Expand All @@ -107,7 +107,7 @@ function ClientRequest(options, cb) {
}
this.agent = agent;

var protocol = options.protocol || defaultAgent.protocol;
const protocol = options.protocol || defaultAgent.protocol;
var expectedProtocol = defaultAgent.protocol;
if (this.agent && this.agent.protocol)
expectedProtocol = this.agent.protocol;
Expand All @@ -129,20 +129,20 @@ function ClientRequest(options, cb) {
throw new errors.Error('ERR_INVALID_PROTOCOL', protocol, expectedProtocol);
}

var defaultPort = options.defaultPort ||
const defaultPort = options.defaultPort ||
this.agent && this.agent.defaultPort;

var port = options.port = options.port || defaultPort || 80;
var host = options.host = validateHost(options.hostname, 'hostname') ||
const port = options.port = options.port || defaultPort || 80;
const host = options.host = validateHost(options.hostname, 'hostname') ||
validateHost(options.host, 'host') || 'localhost';

var setHost = (options.setHost === undefined);
const setHost = (options.setHost === undefined);

this.socketPath = options.socketPath;
this.timeout = options.timeout;

var method = options.method;
var methodIsString = (typeof method === 'string');
const methodIsString = (typeof method === 'string');
if (method != null && !methodIsString) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'method',
'string', method);
Expand All @@ -162,12 +162,12 @@ function ClientRequest(options, cb) {
this.once('response', cb);
}

var headersArray = Array.isArray(options.headers);
const headersArray = Array.isArray(options.headers);
if (!headersArray) {
if (options.headers) {
var keys = Object.keys(options.headers);
const keys = Object.keys(options.headers);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
const key = keys[i];
this.setHeader(key, options.headers[key]);
}
}
Expand All @@ -177,7 +177,7 @@ function ClientRequest(options, cb) {
// For the Host header, ensure that IPv6 addresses are enclosed
// in square brackets, as defined by URI formatting
// https://tools.ietf.org/html/rfc3986#section-3.2.2
var posColon = hostHeader.indexOf(':');
const posColon = hostHeader.indexOf(':');
if (posColon !== -1 &&
hostHeader.indexOf(':', posColon + 1) !== -1 &&
hostHeader.charCodeAt(0) !== 91/*'['*/) {
Expand Down Expand Up @@ -228,7 +228,7 @@ function ClientRequest(options, cb) {

var called = false;

var oncreate = (err, socket) => {
const oncreate = (err, socket) => {
if (called)
return;
called = true;
Expand All @@ -244,7 +244,7 @@ function ClientRequest(options, cb) {
if (this.socketPath) {
this._last = true;
this.shouldKeepAlive = false;
var optionsPath = {
const optionsPath = {
path: this.socketPath,
timeout: this.timeout,
rejectUnauthorized: !!options.rejectUnauthorized
Expand Down Expand Up @@ -341,15 +341,15 @@ function emitAbortNT() {


function createHangUpError() {
var error = new Error('socket hang up');
const error = new Error('socket hang up');
error.code = 'ECONNRESET';
return error;
}


function socketCloseListener() {
var socket = this;
var req = socket._httpMessage;
const socket = this;
const req = socket._httpMessage;
debug('HTTP socket close');

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

// NOTE: It's important to get parser here, because it could be freed by
// the `socketOnData`.
var parser = socket.parser;
const parser = socket.parser;
req.emit('close');
if (req.res && req.res.readable) {
// Socket closed before we emitted 'end' below.
req.res.emit('aborted');
var res = req.res;
const res = req.res;
res.on('end', function() {
res.emit('close');
});
Expand Down Expand Up @@ -392,8 +392,8 @@ function socketCloseListener() {
}

function socketErrorListener(err) {
var socket = this;
var req = socket._httpMessage;
const socket = this;
const req = socket._httpMessage;
debug('SOCKET ERROR:', err.message, err.stack);

if (req) {
Expand All @@ -406,7 +406,7 @@ function socketErrorListener(err) {
// Handle any pending data
socket.read();

var parser = socket.parser;
const parser = socket.parser;
if (parser) {
parser.finish();
freeParser(parser, req, socket);
Expand All @@ -419,16 +419,16 @@ function socketErrorListener(err) {
}

function freeSocketErrorListener(err) {
var socket = this;
const socket = this;
debug('SOCKET ERROR on FREE socket:', err.message, err.stack);
socket.destroy();
socket.emit('agentRemove');
}

function socketOnEnd() {
var socket = this;
var req = this._httpMessage;
var parser = this.parser;
const socket = this;
const req = this._httpMessage;
const parser = this.parser;

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

function socketOnData(d) {
var socket = this;
var req = this._httpMessage;
var parser = this.parser;
const socket = this;
const req = this._httpMessage;
const parser = this.parser;

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

var ret = parser.execute(d);
const ret = parser.execute(d);
if (ret instanceof Error) {
debug('parse error', ret);
freeParser(parser, req, socket);
Expand All @@ -459,17 +459,17 @@ function socketOnData(d) {
req.emit('error', ret);
} else if (parser.incoming && parser.incoming.upgrade) {
// Upgrade or CONNECT
var bytesParsed = ret;
var res = parser.incoming;
const bytesParsed = ret;
const res = parser.incoming;
req.res = res;

socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
parser.finish();

var bodyHead = d.slice(bytesParsed, d.length);
const bodyHead = d.slice(bytesParsed, d.length);

var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
const eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
if (req.listenerCount(eventName) > 0) {
req.upgradeOrConnect = true;

Expand Down Expand Up @@ -503,8 +503,8 @@ function socketOnData(d) {

// client
function parserOnIncomingClient(res, shouldKeepAlive) {
var socket = this.socket;
var req = socket._httpMessage;
const socket = this.socket;
const req = socket._httpMessage;


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

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

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

// client
function responseKeepAlive(res, req) {
var socket = req.socket;
const socket = req.socket;

if (!req.shouldKeepAlive) {
if (socket.writable) {
Expand Down Expand Up @@ -625,7 +625,7 @@ function emitFreeNT(socket) {
}

function tickOnSocket(req, socket) {
var parser = parsers.alloc();
const parser = parsers.alloc();
req.socket = socket;
req.connection = socket;
parser.reinitialize(HTTPParser.RESPONSE);
Expand Down Expand Up @@ -731,7 +731,7 @@ ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
// Set timeoutCb so that it'll get cleaned up on request end
this.timeoutCb = emitTimeout;
if (this.socket) {
var sock = this.socket;
const sock = this.socket;
this.socket.once('connect', function() {
sock.setTimeout(msecs, emitTimeout);
});
Expand Down
Loading