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: use for...of #30958

Closed
wants to merge 10 commits into from
21 changes: 7 additions & 14 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ function maybeEnableKeylog(eventName) {
agent.emit('keylog', keylog, this);
};
// Existing sockets will start listening on keylog now.
const sockets = ObjectValues(this.sockets);
for (let i = 0; i < sockets.length; i++) {
sockets[i].on('keylog', this[kOnKeylog]);
for (const socket of ObjectValues(this.sockets)) {
socket.on('keylog', this[kOnKeylog]);
}
}
}
Expand Down Expand Up @@ -346,9 +345,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
if (!s.writable)
sets.push(this.freeSockets);

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

for (const sockets of sets) {
if (sockets[name]) {
const index = sockets[name].indexOf(s);
if (index !== -1) {
Expand Down Expand Up @@ -383,14 +380,10 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
};

Agent.prototype.destroy = function destroy() {
const sets = [this.freeSockets, this.sockets];
for (let s = 0; s < sets.length; s++) {
const set = sets[s];
const keys = ObjectKeys(set);
for (let v = 0; v < keys.length; v++) {
const setName = set[keys[v]];
for (let n = 0; n < setName.length; n++) {
setName[n].destroy();
for (const set of [this.freeSockets, this.sockets]) {
for (const key of ObjectKeys(set)) {
for (const setName of set[key]) {
setName.destroy();
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ function ClientRequest(input, options, cb) {
const headersArray = ArrayIsArray(options.headers);
if (!headersArray) {
if (options.headers) {
const keys = ObjectKeys(options.headers);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
for (const key of ObjectKeys(options.headers)) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
this.setHeader(key, options.headers[key]);
}
}
Expand Down
39 changes: 13 additions & 26 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headers', {
this[kOutHeaders] = null;
} else if (typeof val === 'object') {
const headers = this[kOutHeaders] = ObjectCreate(null);
const keys = ObjectKeys(val);
for (var i = 0; i < keys.length; ++i) {
const name = keys[i];
for (const name of ObjectKeys(val)) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
headers[name.toLowerCase()] = [name, val[name]];
}
}
Expand All @@ -188,9 +186,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
const headers = this[kOutHeaders];
if (headers !== null) {
const out = ObjectCreate(null);
const keys = ObjectKeys(headers);
for (var i = 0; i < keys.length; ++i) {
const key = keys[i];
for (const key of ObjectKeys(headers)) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
const val = headers[key][0];
out[key] = val;
}
Expand All @@ -203,11 +199,10 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', {
const headers = this[kOutHeaders];
if (!headers)
return;
const keys = ObjectKeys(val);
for (var i = 0; i < keys.length; ++i) {
const header = headers[keys[i]];
for (const key of ObjectKeys(val)) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
const header = headers[key];
if (header)
header[0] = val[keys[i]];
header[0] = val[key];
}
}
}, 'OutgoingMessage.prototype._headerNames is deprecated', 'DEP0066')
Expand All @@ -223,9 +218,7 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
const headers = {};

if (headersMap !== null) {
const keys = ObjectKeys(headersMap);
for (var i = 0, l = keys.length; i < l; i++) {
const key = keys[i];
for (const key of ObjectKeys(headersMap)) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
headers[headersMap[key][0]] = headersMap[key][1];
}
}
Expand Down Expand Up @@ -458,8 +451,8 @@ function processHeader(self, state, key, value, validate) {
validateHeaderName(key);
if (ArrayIsArray(value)) {
if (value.length < 2 || !isCookieField(key)) {
for (var i = 0; i < value.length; i++)
storeHeader(self, state, key, value[i], validate);
for (const valueItem of value)
trivikr marked this conversation as resolved.
Show resolved Hide resolved
storeHeader(self, state, key, valueItem, validate);
return;
}
value = value.join('; ');
Expand Down Expand Up @@ -558,9 +551,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
const headers = this[kOutHeaders];
const ret = ObjectCreate(null);
if (headers) {
const keys = ObjectKeys(headers);
for (var i = 0; i < keys.length; ++i) {
const key = keys[i];
for (const key of ObjectKeys(headers)) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
const val = headers[key][1];
ret[key] = val;
}
Expand Down Expand Up @@ -697,11 +688,9 @@ function connectionCorkNT(conn) {

OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
this._trailer = '';
const keys = ObjectKeys(headers);
const isArray = ArrayIsArray(headers);
var field, value;
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
for (const key of ObjectKeys(headers)) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
if (isArray) {
field = headers[key][0];
value = headers[key][1];
Expand Down Expand Up @@ -847,15 +836,13 @@ OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
socket.cork();
}

const outputLength = this.outputData.length;
if (outputLength <= 0)
if (this.outputData.length <= 0)
return undefined;

const outputData = this.outputData;
socket.cork();
let ret;
for (var i = 0; i < outputLength; i++) {
const { data, encoding, callback } = outputData[i];
trivikr marked this conversation as resolved.
Show resolved Hide resolved
for (const outputDataItem of this.outputData) {
const { data, encoding, callback } = outputDataItem;
trivikr marked this conversation as resolved.
Show resolved Hide resolved
ret = socket.write(data, encoding, callback);
}
socket.uncork();
Expand Down
5 changes: 2 additions & 3 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ function writeHead(statusCode, reason, obj) {
// Slow-case: when progressive API and header fields are passed.
let k;
if (obj) {
const keys = ObjectKeys(obj);
for (let i = 0; i < keys.length; i++) {
k = keys[i];
for (const key of ObjectKeys(obj)) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
k = key;
addaleax marked this conversation as resolved.
Show resolved Hide resolved
if (k) this.setHeader(k, obj[k]);
}
}
Expand Down