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

http: convert var to let or const for http #26504

Closed
wants to merge 2 commits 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
40 changes: 20 additions & 20 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function Agent(options) {
this.maxFreeSockets = this.options.maxFreeSockets || 256;

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

if (socket.writable &&
Expand All @@ -75,14 +75,14 @@ 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 &&
this.keepAlive) {
var freeSockets = this.freeSockets[name];
var freeLen = freeSockets ? freeSockets.length : 0;
var count = freeLen;
let freeSockets = this.freeSockets[name];
const freeLen = freeSockets ? freeSockets.length : 0;
let count = freeLen;
if (this.sockets[name])
count += this.sockets[name].length;

Expand Down Expand Up @@ -114,7 +114,7 @@ Agent.prototype.createConnection = net.createConnection;

// Get the key for a given set of request options
Agent.prototype.getName = function getName(options) {
var name = options.host || 'localhost';
let name = options.host || 'localhost';

name += ':';
if (options.port)
Expand Down Expand Up @@ -153,17 +153,17 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
if (!options.servername)
options.servername = calculateServerName(options, req);

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 destroy()/init() hooks.
Expand Down Expand Up @@ -200,12 +200,12 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
if (!options.servername)
options.servername = calculateServerName(options, req);

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

debug('createConnection', name, options);
options.encoding = null;
var called = false;
let called = false;

const oncreate = (err, s) => {
if (called)
Expand Down Expand Up @@ -280,19 +280,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 Down Expand Up @@ -324,12 +324,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
82 changes: 41 additions & 41 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ function ClientRequest(input, options, cb) {
options = Object.assign(input || {}, options);
}

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

var protocol = options.protocol || defaultAgent.protocol;
var expectedProtocol = defaultAgent.protocol;
const protocol = options.protocol || defaultAgent.protocol;
let expectedProtocol = defaultAgent.protocol;
if (this.agent && this.agent.protocol)
expectedProtocol = this.agent.protocol;

var path;
let path;
if (options.path) {
path = String(options.path);
if (INVALID_PATH_REGEX.test(path))
Expand All @@ -130,22 +130,22 @@ function ClientRequest(input, options, cb) {
throw new 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 || Boolean(options.setHost));
const setHost = (options.setHost === undefined || Boolean(options.setHost));

this.socketPath = options.socketPath;

if (options.timeout !== undefined)
this.timeout = validateTimerDuration(options.timeout, 'timeout');

var method = options.method;
var methodIsString = (typeof method === 'string');
let method = options.method;
const methodIsString = (typeof method === 'string');
if (method !== null && method !== undefined && !methodIsString) {
throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
}
Expand Down Expand Up @@ -182,7 +182,7 @@ function ClientRequest(input, options, cb) {
this.parser = null;
this.maxHeadersCount = null;

var called = false;
let called = false;

if (this.agent) {
// If there is an agent we should default to Connection:keep-alive,
Expand All @@ -198,23 +198,23 @@ function ClientRequest(input, options, 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]);
}
}

if (host && !this.getHeader('host') && setHost) {
var hostHeader = host;
let hostHeader = host;

// 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 @@ -245,7 +245,7 @@ function ClientRequest(input, options, cb) {
options.headers);
}

var oncreate = (err, socket) => {
const oncreate = (err, socket) => {
if (called)
return;
called = true;
Expand Down Expand Up @@ -327,15 +327,15 @@ function emitAbortNT() {

function createHangUpError() {
// eslint-disable-next-line no-restricted-syntax
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 Down Expand Up @@ -386,8 +386,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 @@ -400,7 +400,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 @@ -413,16 +413,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 @@ -438,13 +438,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 @@ -453,8 +453,8 @@ function socketOnData(d) {
req.emit('error', ret);
} else if (parser.incoming && parser.incoming.upgrade) {
// Upgrade (if status code 101) or CONNECT
var bytesParsed = ret;
var res = parser.incoming;
const bytesParsed = ret;
const res = parser.incoming;
req.res = res;

socket.removeListener('data', socketOnData);
Expand All @@ -463,9 +463,9 @@ function socketOnData(d) {
parser.finish();
freeParser(parser, req, socket);

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 @@ -506,8 +506,8 @@ function statusIsInformational(status) {

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

debug('AGENT incoming response!');

Expand Down Expand Up @@ -557,7 +557,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 @@ -573,7 +573,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 @@ -627,7 +627,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, parser[is_reused_symbol]);
Expand Down
6 changes: 3 additions & 3 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
incoming.url = url;
incoming.upgrade = upgrade;

var n = headers.length;
let n = headers.length;

// If parser.maxHeaderPairs <= 0 assume that there's no limit.
if (parser.maxHeaderPairs > 0)
Expand Down Expand Up @@ -122,8 +122,8 @@ function parserOnBody(b, start, len) {

// Pretend this was the result of a stream._read call.
if (len > 0 && !stream._dumped) {
var slice = b.slice(start, start + len);
var ret = stream.push(slice);
const slice = b.slice(start, start + len);
const ret = stream.push(slice);
if (!ret)
readStop(this.socket);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ IncomingMessage.prototype.destroy = function destroy(error) {
IncomingMessage.prototype._addHeaderLines = _addHeaderLines;
function _addHeaderLines(headers, n) {
if (headers && headers.length) {
var dest;
let dest;
if (this.complete) {
this.rawTrailers = headers;
dest = this.trailers;
Expand Down Expand Up @@ -243,7 +243,7 @@ function matchKnownFields(field, lowercased) {
IncomingMessage.prototype._addHeaderLine = _addHeaderLine;
function _addHeaderLine(field, value, dest) {
field = matchKnownFields(field);
var flag = field.charCodeAt(0);
const flag = field.charCodeAt(0);
if (flag === 0 || flag === 2) {
field = field.slice(1);
// Make a delimited list
Expand Down
Loading