Skip to content

Commit

Permalink
http: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #37654
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Mar 16, 2021
1 parent 2577f10 commit 77c5c2f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypeShift,
ArrayPrototypeSome,
ArrayPrototypeSplice,
FunctionPrototypeCall,
NumberIsNaN,
Expand Down Expand Up @@ -390,10 +391,10 @@ function installListeners(agent, s, options) {
// Destroy if in free list.
// TODO(ronag): Always destroy, even if not in free list.
const sockets = agent.freeSockets;
for (const name of ObjectKeys(sockets)) {
if (ArrayPrototypeIncludes(sockets[name], s)) {
return s.destroy();
}
if (ArrayPrototypeSome(ObjectKeys(sockets), (name) =>
ArrayPrototypeIncludes(sockets[name], s)
)) {
return s.destroy();
}
}
s.on('timeout', onTimeout);
Expand Down Expand Up @@ -449,7 +450,9 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
// There might be older requests in a different origin, but
// if the origin which releases the socket has pending requests
// that will be prioritized.
for (const prop of ObjectKeys(this.requests)) {
const keys = ObjectKeys(this.requests);
for (let i = 0; i < keys.length; i++) {
const prop = keys[i];
// Check whether this specific origin is already at maxSockets
if (this.sockets[prop] && this.sockets[prop].length) break;
debug('removeSocket, have a request with different origin,' +
Expand Down

0 comments on commit 77c5c2f

Please sign in to comment.