Skip to content

Commit 1f13e3b

Browse files
committed
refactor(api): tighten getHttpsAgent return type to HttpsAgent
getHttpsAgent now always creates an agent on first call, so its return type is HttpsAgent (was HttpsAgent | undefined) and the _httpsRequestFetch agent parameter drops | undefined. The cached _httpsAgent keeps | undefined since it is the lazy-init sentinel (undefined only before the first call). The _httpsAgentResolved flag is removed: a set _httpsAgent is itself the "resolved" signal. Pure polish from review; no behavior change.
1 parent b3f07bd commit 1f13e3b

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/utils/api.mts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ import type {
5454
const MAX_REDIRECTS = 20
5555
const NO_ERROR_MESSAGE = 'No error message returned'
5656

57-
// Cached HTTPS agent for direct API calls.
57+
// Cached HTTPS agent for direct API calls. Undefined only until the first
58+
// getHttpsAgent() call lazily creates it.
5859
let _httpsAgent: HttpsAgent | undefined
59-
let _httpsAgentResolved = false
6060

6161
// Returns an explicit HTTPS agent for direct API calls, carrying extra CA
6262
// certificates when SSL_CERT_FILE is set but NODE_EXTRA_CA_CERTS is not. An
@@ -66,14 +66,14 @@ let _httpsAgentResolved = false
6666
// down after 5s of socket inactivity, prematurely dropping slow or idle-gapped
6767
// requests (e.g. streaming full-scan responses, large downloads) even when no
6868
// timeout was requested. A fresh Agent carries no timeout.
69-
function getHttpsAgent(): HttpsAgent | undefined {
70-
if (_httpsAgentResolved) {
69+
function getHttpsAgent(): HttpsAgent {
70+
if (_httpsAgent) {
7171
return _httpsAgent
7272
}
73-
_httpsAgentResolved = true
7473
const ca = getExtraCaCerts()
75-
_httpsAgent = ca ? new HttpsAgent({ ca }) : new HttpsAgent()
76-
return _httpsAgent
74+
const agent = ca ? new HttpsAgent({ ca }) : new HttpsAgent()
75+
_httpsAgent = agent
76+
return agent
7777
}
7878

7979
// All outbound API requests use node:https.request rather than global fetch.
@@ -92,7 +92,7 @@ export type ApiFetchInit = {
9292
function _httpsRequestFetch(
9393
url: string,
9494
init: ApiFetchInit,
95-
agent: HttpsAgent | undefined,
95+
agent: HttpsAgent,
9696
redirectCount: number,
9797
): Promise<Response> {
9898
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)