From 2e705fe8e47ea437ea99176571f73ab5bbdb8a16 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 08:32:41 -0500 Subject: [PATCH 01/10] http: for of in lib/_http_agent.js --- lib/_http_agent.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 69fdb819a3a330..e47cbb2106fc8f 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -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 (socket of ObjectValues(this.sockets)) { + socket.on('keylog', this[kOnKeylog]); } } } @@ -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 (sockets of sets) { if (sockets[name]) { const index = sockets[name].indexOf(s); if (index !== -1) { @@ -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 (set of [this.freeSockets, this.sockets]) { + for (key of ObjectKeys(set)) { + for (setName of set[key]) { + setName.destroy(); } } } From 63f73b9a8ffe7ef8f03058460751ffcf329bb5b3 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 08:39:27 -0500 Subject: [PATCH 02/10] http: for of in lib/_http_client.js --- lib/_http_client.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 7888ce27d570ac..7f668b022ff774 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -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 (key of ObjectKeys(options.headers)) { this.setHeader(key, options.headers[key]); } } From 3ba1da23078b7ffd3a73a164e807095e42a3e837 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 08:44:54 -0500 Subject: [PATCH 03/10] http: for of in lib/_http_server.js --- lib/_http_server.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 453cf9005363dc..173c43b370a2c8 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -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 (key of ObjectKeys(obj)) { + k = key; if (k) this.setHeader(k, obj[k]); } } From b4b4d3da28e841eb986ff39840b0b763f3bd5569 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 08:51:55 -0500 Subject: [PATCH 04/10] http: for of in lib/_http_outgoing.js --- lib/_http_outgoing.js | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index f40cf923f9b377..066edc58626eec 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -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 (name of ObjectKeys(val)) { headers[name.toLowerCase()] = [name, val[name]]; } } @@ -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 (key of ObjectKeys(headers)) { const val = headers[key][0]; out[key] = val; } @@ -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 (key of ObjectKeys(val)) { + const header = headers[key]; if (header) - header[0] = val[keys[i]]; + header[0] = val[key]; } } }, 'OutgoingMessage.prototype._headerNames is deprecated', 'DEP0066') @@ -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 (key of ObjectKeys(headersMap)) { headers[headersMap[key][0]] = headersMap[key][1]; } } @@ -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 (keyValue of value) + storeHeader(self, state, key, keyValue, validate); return; } value = value.join('; '); @@ -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 (key of ObjectKeys(headers)) { const val = headers[key][1]; ret[key] = val; } @@ -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 (key of ObjectKeys(headers)) { if (isArray) { field = headers[key][0]; value = headers[key][1]; @@ -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]; + for (outputDataItem of this.outputData) { + const { data, encoding, callback } = outputDataItem; ret = socket.write(data, encoding, callback); } socket.uncork(); From 3d87c3df77d6cb5a8afa73d19b633802647fb10c Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 09:00:04 -0500 Subject: [PATCH 05/10] http: added missing const in for loops --- lib/_http_agent.js | 10 +++++----- lib/_http_client.js | 2 +- lib/_http_outgoing.js | 16 ++++++++-------- lib/_http_server.js | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index e47cbb2106fc8f..5e3298b594d949 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -146,7 +146,7 @@ function maybeEnableKeylog(eventName) { agent.emit('keylog', keylog, this); }; // Existing sockets will start listening on keylog now. - for (socket of ObjectValues(this.sockets)) { + for (const socket of ObjectValues(this.sockets)) { socket.on('keylog', this[kOnKeylog]); } } @@ -345,7 +345,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) { if (!s.writable) sets.push(this.freeSockets); - for (sockets of sets) { + for (const sockets of sets) { if (sockets[name]) { const index = sockets[name].indexOf(s); if (index !== -1) { @@ -380,9 +380,9 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) { }; Agent.prototype.destroy = function destroy() { - for (set of [this.freeSockets, this.sockets]) { - for (key of ObjectKeys(set)) { - for (setName of set[key]) { + for (const set of [this.freeSockets, this.sockets]) { + for (const key of ObjectKeys(set)) { + for (const setName of set[key]) { setName.destroy(); } } diff --git a/lib/_http_client.js b/lib/_http_client.js index 7f668b022ff774..932e6600bfa29a 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -230,7 +230,7 @@ function ClientRequest(input, options, cb) { const headersArray = ArrayIsArray(options.headers); if (!headersArray) { if (options.headers) { - for (key of ObjectKeys(options.headers)) { + for (const key of ObjectKeys(options.headers)) { this.setHeader(key, options.headers[key]); } } diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 066edc58626eec..434c0dbc9d3f3e 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -165,7 +165,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headers', { this[kOutHeaders] = null; } else if (typeof val === 'object') { const headers = this[kOutHeaders] = ObjectCreate(null); - for (name of ObjectKeys(val)) { + for (const name of ObjectKeys(val)) { headers[name.toLowerCase()] = [name, val[name]]; } } @@ -186,7 +186,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', { const headers = this[kOutHeaders]; if (headers !== null) { const out = ObjectCreate(null); - for (key of ObjectKeys(headers)) { + for (const key of ObjectKeys(headers)) { const val = headers[key][0]; out[key] = val; } @@ -199,7 +199,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', { const headers = this[kOutHeaders]; if (!headers) return; - for (key of ObjectKeys(val)) { + for (const key of ObjectKeys(val)) { const header = headers[key]; if (header) header[0] = val[key]; @@ -218,7 +218,7 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() { const headers = {}; if (headersMap !== null) { - for (key of ObjectKeys(headersMap)) { + for (const key of ObjectKeys(headersMap)) { headers[headersMap[key][0]] = headersMap[key][1]; } } @@ -451,7 +451,7 @@ function processHeader(self, state, key, value, validate) { validateHeaderName(key); if (ArrayIsArray(value)) { if (value.length < 2 || !isCookieField(key)) { - for (keyValue of value) + for (const keyValue of value) storeHeader(self, state, key, keyValue, validate); return; } @@ -551,7 +551,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() { const headers = this[kOutHeaders]; const ret = ObjectCreate(null); if (headers) { - for (key of ObjectKeys(headers)) { + for (const key of ObjectKeys(headers)) { const val = headers[key][1]; ret[key] = val; } @@ -690,7 +690,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) { this._trailer = ''; const isArray = ArrayIsArray(headers); var field, value; - for (key of ObjectKeys(headers)) { + for (const key of ObjectKeys(headers)) { if (isArray) { field = headers[key][0]; value = headers[key][1]; @@ -841,7 +841,7 @@ OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) { socket.cork(); let ret; - for (outputDataItem of this.outputData) { + for (const outputDataItem of this.outputData) { const { data, encoding, callback } = outputDataItem; ret = socket.write(data, encoding, callback); } diff --git a/lib/_http_server.js b/lib/_http_server.js index 173c43b370a2c8..3670a81800c8d9 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -262,7 +262,7 @@ function writeHead(statusCode, reason, obj) { // Slow-case: when progressive API and header fields are passed. let k; if (obj) { - for (key of ObjectKeys(obj)) { + for (const key of ObjectKeys(obj)) { k = key; if (k) this.setHeader(k, obj[k]); } From 23fc285ced06e27f1249600387156c629b2c0224 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 09:02:50 -0500 Subject: [PATCH 06/10] f rename keyValue -> valueItem --- lib/_http_outgoing.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 434c0dbc9d3f3e..d1c8c7b57e12c9 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -451,8 +451,8 @@ function processHeader(self, state, key, value, validate) { validateHeaderName(key); if (ArrayIsArray(value)) { if (value.length < 2 || !isCookieField(key)) { - for (const keyValue of value) - storeHeader(self, state, key, keyValue, validate); + for (const valueItem of value) + storeHeader(self, state, key, valueItem, validate); return; } value = value.join('; '); From c5c391602db21045dc7b483d54f0ee91bffa1559 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 11:01:26 -0500 Subject: [PATCH 07/10] f destructure outputDataItem --- lib/_http_outgoing.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index d1c8c7b57e12c9..eb023b9401516c 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -841,8 +841,7 @@ OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) { socket.cork(); let ret; - for (const outputDataItem of this.outputData) { - const { data, encoding, callback } = outputDataItem; + for (const { data, encoding, callback } of this.outputData) { ret = socket.write(data, encoding, callback); } socket.uncork(); From 8d435801647cc57a6b14be4b484c1bb3de781413 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 18:39:10 -0500 Subject: [PATCH 08/10] Revert "http: for of in lib/_http_client.js" This reverts commit 63f73b9a8ffe7ef8f03058460751ffcf329bb5b3. --- lib/_http_client.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 932e6600bfa29a..9c5b18bc21e1f2 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -230,7 +230,11 @@ function ClientRequest(input, options, cb) { const headersArray = ArrayIsArray(options.headers); if (!headersArray) { if (options.headers) { - for (const key of ObjectKeys(options.headers)) { + const keys = ObjectKeys(options.headers); + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; this.setHeader(key, options.headers[key]); } } From c2f4695d1f19b5841a162c92e4e041545a782186 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 18:52:02 -0500 Subject: [PATCH 09/10] Revert "http: for of in lib/_http_server.js" This reverts commit 3ba1da23078b7ffd3a73a164e807095e42a3e837. --- lib/_http_server.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 3670a81800c8d9..af860c88064b4e 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -262,8 +262,11 @@ function writeHead(statusCode, reason, obj) { // Slow-case: when progressive API and header fields are passed. let k; if (obj) { - for (const key of ObjectKeys(obj)) { - k = key; + const keys = ObjectKeys(obj); + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (let i = 0; i < keys.length; i++) { + k = keys[i]; if (k) this.setHeader(k, obj[k]); } } From 17049d56621869d8776a922d5e56b6c5ec35be7b Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Sat, 14 Dec 2019 18:55:08 -0500 Subject: [PATCH 10/10] Revert "http: for of in lib/_http_outgoing.js" This reverts commit b4b4d3da28e841eb986ff39840b0b763f3bd5569. --- lib/_http_outgoing.js | 54 +++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index eb023b9401516c..36d3054eab0663 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -165,7 +165,11 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headers', { this[kOutHeaders] = null; } else if (typeof val === 'object') { const headers = this[kOutHeaders] = ObjectCreate(null); - for (const name of ObjectKeys(val)) { + const keys = ObjectKeys(val); + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (var i = 0; i < keys.length; ++i) { + const name = keys[i]; headers[name.toLowerCase()] = [name, val[name]]; } } @@ -186,7 +190,11 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', { const headers = this[kOutHeaders]; if (headers !== null) { const out = ObjectCreate(null); - for (const key of ObjectKeys(headers)) { + const keys = ObjectKeys(headers); + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (var i = 0; i < keys.length; ++i) { + const key = keys[i]; const val = headers[key][0]; out[key] = val; } @@ -199,10 +207,13 @@ ObjectDefineProperty(OutgoingMessage.prototype, '_headerNames', { const headers = this[kOutHeaders]; if (!headers) return; - for (const key of ObjectKeys(val)) { - const header = headers[key]; + const keys = ObjectKeys(val); + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (var i = 0; i < keys.length; ++i) { + const header = headers[keys[i]]; if (header) - header[0] = val[key]; + header[0] = val[keys[i]]; } } }, 'OutgoingMessage.prototype._headerNames is deprecated', 'DEP0066') @@ -218,7 +229,11 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() { const headers = {}; if (headersMap !== null) { - for (const key of ObjectKeys(headersMap)) { + const keys = ObjectKeys(headersMap); + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (var i = 0, l = keys.length; i < l; i++) { + const key = keys[i]; headers[headersMap[key][0]] = headersMap[key][1]; } } @@ -451,8 +466,10 @@ function processHeader(self, state, key, value, validate) { validateHeaderName(key); if (ArrayIsArray(value)) { if (value.length < 2 || !isCookieField(key)) { - for (const valueItem of value) - storeHeader(self, state, key, valueItem, validate); + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (var i = 0; i < value.length; i++) + storeHeader(self, state, key, value[i], validate); return; } value = value.join('; '); @@ -551,7 +568,11 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() { const headers = this[kOutHeaders]; const ret = ObjectCreate(null); if (headers) { - for (const key of ObjectKeys(headers)) { + const keys = ObjectKeys(headers); + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (var i = 0; i < keys.length; ++i) { + const key = keys[i]; const val = headers[key][1]; ret[key] = val; } @@ -688,9 +709,13 @@ function connectionCorkNT(conn) { OutgoingMessage.prototype.addTrailers = function addTrailers(headers) { this._trailer = ''; + const keys = ObjectKeys(headers); const isArray = ArrayIsArray(headers); var field, value; - for (const key of ObjectKeys(headers)) { + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (var i = 0, l = keys.length; i < l; i++) { + var key = keys[i]; if (isArray) { field = headers[key][0]; value = headers[key][1]; @@ -836,12 +861,17 @@ OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) { socket.cork(); } - if (this.outputData.length <= 0) + const outputLength = this.outputData.length; + if (outputLength <= 0) return undefined; + const outputData = this.outputData; socket.cork(); let ret; - for (const { data, encoding, callback } of this.outputData) { + // Retain for(;;) loop for performance reasons + // Refs: https://github.com/nodejs/node/pull/30958 + for (var i = 0; i < outputLength; i++) { + const { data, encoding, callback } = outputData[i]; ret = socket.write(data, encoding, callback); } socket.uncork();