|
1 | 1 | import * as net from 'net';
|
2 | 2 | import * as tls from 'tls';
|
3 | 3 | import * as http from 'http';
|
4 |
| -import { Duplex } from 'stream'; |
| 4 | +import type { Duplex } from 'stream'; |
5 | 5 |
|
6 | 6 | export * from './helpers';
|
7 | 7 |
|
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 |
| - |
20 | 8 | interface HttpConnectOpts extends net.TcpNetConnectOpts {
|
21 | 9 | secureEndpoint: false;
|
22 | 10 | protocol?: string;
|
@@ -55,37 +43,49 @@ export abstract class Agent extends http.Agent {
|
55 | 43 | options: AgentConnectOpts
|
56 | 44 | ): Promise<Duplex | http.Agent> | Duplex | http.Agent;
|
57 | 45 |
|
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 | + } |
79 | 64 | }
|
80 | 65 |
|
81 | 66 | // Finally, if no `protocol` property was set, then fall back to
|
82 | 67 | // checking the stack trace of the current call stack, and try to
|
83 | 68 | // 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 | + } |
87 | 79 |
|
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 | + }; |
89 | 89 | Promise.resolve()
|
90 | 90 | .then(() => this.connect(req, connectOpts))
|
91 | 91 | .then((socket) => {
|
@@ -125,7 +125,8 @@ export abstract class Agent extends http.Agent {
|
125 | 125 |
|
126 | 126 | get protocol(): string {
|
127 | 127 | return (
|
128 |
| - this[INTERNAL].protocol ?? (isSecureEndpoint() ? 'https:' : 'http:') |
| 128 | + this[INTERNAL].protocol ?? |
| 129 | + (this.isSecureEndpoint() ? 'https:' : 'http:') |
129 | 130 | );
|
130 | 131 | }
|
131 | 132 |
|
|
0 commit comments