Skip to content

Commit

Permalink
Implement getName method to get both http/https names
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Mar 29, 2024
1 parent 634b88b commit cdad2e1
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions packages/agent-base/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import { Agent as HttpsAgent } from 'https';
import type { Duplex } from 'stream';

export * from './helpers';
Expand Down Expand Up @@ -78,19 +79,21 @@ export abstract class Agent extends http.Agent {
}

// In order to support async signatures in `connect()` and Node's native
// connection pooling in `http.Agent`, the array of sockets for each origin has
// to be updated synchronously. This is so the length of the array is accurate
// when `addRequest()` is next called. We achieve this by creating a fake socket
// and adding it to `sockets[origin]` and incrementing `totalSocketCount`.
// connection pooling in `http.Agent`, the array of sockets for each origin
// has to be updated synchronously. This is so the length of the array is
// accurate when `addRequest()` is next called. We achieve this by creating a
// fake socket and adding it to `sockets[origin]` and incrementing
// `totalSocketCount`.
private incrementSockets(name: string) {
// If `maxSockets` and `maxTotalSockets` are both Infinity then there is no need
// to create a fake socket because Node.js native connection pooling will
// never be invoked.
// If `maxSockets` and `maxTotalSockets` are both Infinity then there is no
// need to create a fake socket because Node.js native connection pooling
// will never be invoked.
if (this.maxSockets === Infinity && this.maxTotalSockets === Infinity) {
return null;
}
// All instances of `sockets` are expected TypeScript errors. The alternative is to
// add it as a private property of this class but that will break TypeScript subclassing.
// All instances of `sockets` are expected TypeScript errors. The
// alternative is to add it as a private property of this class but that
// will break TypeScript subclassing.
if (!this.sockets[name]) {
// @ts-expect-error `sockets` is readonly in `@types/node`
this.sockets[name] = [];
Expand Down Expand Up @@ -119,18 +122,27 @@ export abstract class Agent extends http.Agent {
}
}

// In order to properly update the socket pool, we need to call `getName()` on
// the core `https.Agent` if it is a secureEndpoint.
private getName({ secureEndpoint, ...options }: AgentConnectOpts) {
return secureEndpoint
? // @ts-expect-error `getName()` isn't defined in `@types/node`
HttpsAgent.prototype.getName.call(this, options)

Check failure on line 130 in packages/agent-base/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Mixed spaces and tabs
: // @ts-expect-error `getName()` isn't defined in `@types/node`
super.getName(options);

Check failure on line 132 in packages/agent-base/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Mixed spaces and tabs
}

createSocket(
req: http.ClientRequest,
options: AgentConnectOpts,
cb: (err: Error | null, s?: Duplex) => void
) {
// @ts-expect-error `getName()` isn't defined in `@types/node`
const name = this.getName(options);
const fakeSocket = this.incrementSockets(name);
const connectOpts = {
...options,
secureEndpoint: this.isSecureEndpoint(options),
};
const name = this.getName(connectOpts);
const fakeSocket = this.incrementSockets(name);
Promise.resolve()
.then(() => this.connect(req, connectOpts))
.then(
Expand Down

0 comments on commit cdad2e1

Please sign in to comment.