()
| 30 | let fired = false |
| 31 | |
| 32 | export function preconnectAnthropicApi(): void { |
| 33 | if (fired) return |
| 34 | fired = true |
| 35 | |
| 36 | // Skip if using a cloud provider - different endpoint + auth |
| 37 | if ( |
| 38 | isEnvTruthy(process.env.CLAUDE_CODE_USE_BEDROCK) || |
| 39 | isEnvTruthy(process.env.CLAUDE_CODE_USE_VERTEX) || |
| 40 | isEnvTruthy(process.env.CLAUDE_CODE_USE_FOUNDRY) |
| 41 | ) { |
| 42 | return |
| 43 | } |
| 44 | // Skip if proxy/mTLS/unix - SDK's custom dispatcher won't reuse this pool |
| 45 | if ( |
| 46 | process.env.HTTPS_PROXY || |
| 47 | process.env.https_proxy || |
| 48 | process.env.HTTP_PROXY || |
| 49 | process.env.http_proxy || |
| 50 | process.env.ANTHROPIC_UNIX_SOCKET || |
| 51 | process.env.CLAUDE_CODE_CLIENT_CERT || |
| 52 | process.env.CLAUDE_CODE_CLIENT_KEY |
| 53 | ) { |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | // Use the configured first-party inference URL override when present, |
| 58 | // otherwise fall back to the OAuth-configured default host. |
| 59 | // NODE_EXTRA_CA_CERTS no longer a skip - init.ts applied it before this fires. |
| 60 | const baseUrl = |
| 61 | getFirstPartyBaseUrlOverride() || getDefaultFirstPartyInferenceBaseUrl() |
| 62 | |
| 63 | // Fire and forget. HEAD means no response body - the connection is eligible |
| 64 | // for keep-alive pool reuse immediately after headers arrive. 10s timeout |
| 65 | // so a slow network doesn't hang the process; abort is fine since the real |
| 66 | // request will handshake fresh if needed. |
| 67 | // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins |
| 68 | void fetch(baseUrl, { |
| 69 | method: 'HEAD', |
| 70 | signal: AbortSignal.timeout(10_000), |
| 71 | }).catch(() => {}) |
| 72 | } |
no test coverage detected