Skip to content

Commit

Permalink
[agent-base] Add isSecureEndpoint() as an instance method (#186)
Browse files Browse the repository at this point in the history
Part of #169.
  • Loading branch information
TooTallNate authored May 24, 2023
1 parent d5f3d3f commit da699b1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-worms-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'agent-base': minor
---

Add `isSecureEndpoint()` as an instance method
79 changes: 40 additions & 39 deletions packages/agent-base/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import { Duplex } from 'stream';
import type { Duplex } from 'stream';

export * from './helpers';

function isSecureEndpoint(): boolean {
const { stack } = new Error();
if (typeof stack !== 'string') return false;
return stack
.split('\n')
.some(
(l) =>
l.indexOf('(https.js:') !== -1 ||
l.indexOf('node:https:') !== -1
);
}

interface HttpConnectOpts extends net.TcpNetConnectOpts {
secureEndpoint: false;
protocol?: string;
Expand Down Expand Up @@ -55,37 +43,49 @@ export abstract class Agent extends http.Agent {
options: AgentConnectOpts
): Promise<Duplex | http.Agent> | Duplex | http.Agent;

createSocket(
req: http.ClientRequest,
options: AgentConnectOpts,
cb: (err: Error | null, s?: Duplex) => void
) {
// Need to determine whether this is an `http` or `https` request.
// First check the `secureEndpoint` property explicitly, since this
// means that a parent `Agent` is "passing through" to this instance.
let secureEndpoint =
typeof options.secureEndpoint === 'boolean'
? options.secureEndpoint
: undefined;

// If no explicit `secure` endpoint, check if `protocol` property is
// set. This will usually be the case since using a full string URL
// or `URL` instance should be the most common case.
if (
typeof secureEndpoint === 'undefined' &&
typeof options.protocol === 'string'
) {
secureEndpoint = options.protocol === 'https:';
/**
* Determine whether this is an `http` or `https` request.
*/
isSecureEndpoint(options?: AgentConnectOpts): boolean {
if (options) {
// First check the `secureEndpoint` property explicitly, since this
// means that a parent `Agent` is "passing through" to this instance.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (typeof (options as any).secureEndpoint === 'boolean') {
return options.secureEndpoint;
}

// If no explicit `secure` endpoint, check if `protocol` property is
// set. This will usually be the case since using a full string URL
// or `URL` instance should be the most common usage.
if (typeof options.protocol === 'string') {
return options.protocol === 'https:';
}
}

// Finally, if no `protocol` property was set, then fall back to
// checking the stack trace of the current call stack, and try to
// detect the "https" module.
if (typeof secureEndpoint === 'undefined') {
secureEndpoint = isSecureEndpoint();
}
const { stack } = new Error();
if (typeof stack !== 'string') return false;
return stack
.split('\n')
.some(
(l) =>
l.indexOf('(https.js:') !== -1 ||
l.indexOf('node:https:') !== -1
);
}

const connectOpts = { ...options, secureEndpoint };
createSocket(
req: http.ClientRequest,
options: AgentConnectOpts,
cb: (err: Error | null, s?: Duplex) => void
) {
const connectOpts = {
...options,
secureEndpoint: this.isSecureEndpoint(options),
};
Promise.resolve()
.then(() => this.connect(req, connectOpts))
.then((socket) => {
Expand Down Expand Up @@ -125,7 +125,8 @@ export abstract class Agent extends http.Agent {

get protocol(): string {
return (
this[INTERNAL].protocol ?? (isSecureEndpoint() ? 'https:' : 'http:')
this[INTERNAL].protocol ??
(this.isSecureEndpoint() ? 'https:' : 'http:')
);
}

Expand Down

1 comment on commit da699b1

@vercel
Copy link

@vercel vercel bot commented on da699b1 May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

proxy-agents – ./

proxy-agents-git-main-tootallnate.vercel.app
proxy-agents-tootallnate.vercel.app
proxy-agents.vercel.app

Please sign in to comment.