Skip to content

Commit 6688569

Browse files
aduh95danielleadams
authored andcommitted
http: refactor to avoid unsafe array iteration
PR-URL: #37654 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
1 parent 5731977 commit 6688569

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/_http_agent.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
ArrayPrototypePop,
2828
ArrayPrototypePush,
2929
ArrayPrototypeShift,
30+
ArrayPrototypeSome,
3031
ArrayPrototypeSplice,
3132
FunctionPrototypeCall,
3233
NumberIsNaN,
@@ -390,10 +391,10 @@ function installListeners(agent, s, options) {
390391
// Destroy if in free list.
391392
// TODO(ronag): Always destroy, even if not in free list.
392393
const sockets = agent.freeSockets;
393-
for (const name of ObjectKeys(sockets)) {
394-
if (ArrayPrototypeIncludes(sockets[name], s)) {
395-
return s.destroy();
396-
}
394+
if (ArrayPrototypeSome(ObjectKeys(sockets), (name) =>
395+
ArrayPrototypeIncludes(sockets[name], s)
396+
)) {
397+
return s.destroy();
397398
}
398399
}
399400
s.on('timeout', onTimeout);
@@ -449,7 +450,9 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
449450
// There might be older requests in a different origin, but
450451
// if the origin which releases the socket has pending requests
451452
// that will be prioritized.
452-
for (const prop of ObjectKeys(this.requests)) {
453+
const keys = ObjectKeys(this.requests);
454+
for (let i = 0; i < keys.length; i++) {
455+
const prop = keys[i];
453456
// Check whether this specific origin is already at maxSockets
454457
if (this.sockets[prop] && this.sockets[prop].length) break;
455458
debug('removeSocket, have a request with different origin,' +

0 commit comments

Comments
 (0)