Skip to content

Commit da699b1

Browse files
authored
[agent-base] Add isSecureEndpoint() as an instance method (#186)
Part of #169.
1 parent d5f3d3f commit da699b1

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

.changeset/dirty-worms-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'agent-base': minor
3+
---
4+
5+
Add `isSecureEndpoint()` as an instance method

packages/agent-base/src/index.ts

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
import * as net from 'net';
22
import * as tls from 'tls';
33
import * as http from 'http';
4-
import { Duplex } from 'stream';
4+
import type { Duplex } from 'stream';
55

66
export * from './helpers';
77

8-
function isSecureEndpoint(): boolean {
9-
const { stack } = new Error();
10-
if (typeof stack !== 'string') return false;
11-
return stack
12-
.split('\n')
13-
.some(
14-
(l) =>
15-
l.indexOf('(https.js:') !== -1 ||
16-
l.indexOf('node:https:') !== -1
17-
);
18-
}
19-
208
interface HttpConnectOpts extends net.TcpNetConnectOpts {
219
secureEndpoint: false;
2210
protocol?: string;
@@ -55,37 +43,49 @@ export abstract class Agent extends http.Agent {
5543
options: AgentConnectOpts
5644
): Promise<Duplex | http.Agent> | Duplex | http.Agent;
5745

58-
createSocket(
59-
req: http.ClientRequest,
60-
options: AgentConnectOpts,
61-
cb: (err: Error | null, s?: Duplex) => void
62-
) {
63-
// Need to determine whether this is an `http` or `https` request.
64-
// First check the `secureEndpoint` property explicitly, since this
65-
// means that a parent `Agent` is "passing through" to this instance.
66-
let secureEndpoint =
67-
typeof options.secureEndpoint === 'boolean'
68-
? options.secureEndpoint
69-
: undefined;
70-
71-
// If no explicit `secure` endpoint, check if `protocol` property is
72-
// set. This will usually be the case since using a full string URL
73-
// or `URL` instance should be the most common case.
74-
if (
75-
typeof secureEndpoint === 'undefined' &&
76-
typeof options.protocol === 'string'
77-
) {
78-
secureEndpoint = options.protocol === 'https:';
46+
/**
47+
* Determine whether this is an `http` or `https` request.
48+
*/
49+
isSecureEndpoint(options?: AgentConnectOpts): boolean {
50+
if (options) {
51+
// First check the `secureEndpoint` property explicitly, since this
52+
// means that a parent `Agent` is "passing through" to this instance.
53+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
54+
if (typeof (options as any).secureEndpoint === 'boolean') {
55+
return options.secureEndpoint;
56+
}
57+
58+
// If no explicit `secure` endpoint, check if `protocol` property is
59+
// set. This will usually be the case since using a full string URL
60+
// or `URL` instance should be the most common usage.
61+
if (typeof options.protocol === 'string') {
62+
return options.protocol === 'https:';
63+
}
7964
}
8065

8166
// Finally, if no `protocol` property was set, then fall back to
8267
// checking the stack trace of the current call stack, and try to
8368
// detect the "https" module.
84-
if (typeof secureEndpoint === 'undefined') {
85-
secureEndpoint = isSecureEndpoint();
86-
}
69+
const { stack } = new Error();
70+
if (typeof stack !== 'string') return false;
71+
return stack
72+
.split('\n')
73+
.some(
74+
(l) =>
75+
l.indexOf('(https.js:') !== -1 ||
76+
l.indexOf('node:https:') !== -1
77+
);
78+
}
8779

88-
const connectOpts = { ...options, secureEndpoint };
80+
createSocket(
81+
req: http.ClientRequest,
82+
options: AgentConnectOpts,
83+
cb: (err: Error | null, s?: Duplex) => void
84+
) {
85+
const connectOpts = {
86+
...options,
87+
secureEndpoint: this.isSecureEndpoint(options),
88+
};
8989
Promise.resolve()
9090
.then(() => this.connect(req, connectOpts))
9191
.then((socket) => {
@@ -125,7 +125,8 @@ export abstract class Agent extends http.Agent {
125125

126126
get protocol(): string {
127127
return (
128-
this[INTERNAL].protocol ?? (isSecureEndpoint() ? 'https:' : 'http:')
128+
this[INTERNAL].protocol ??
129+
(this.isSecureEndpoint() ? 'https:' : 'http:')
129130
);
130131
}
131132

0 commit comments

Comments
 (0)