| 1 | import { RequestOptions } from "@continuedev/config-types"; |
| 2 | import { CertsCache, getCertificateContent } from "./certs.js"; |
| 3 | |
| 4 | /** |
| 5 | * Prepares agent options based on request options and certificates |
| 6 | */ |
| 7 | export async function getAgentOptions( |
| 8 | requestOptions?: RequestOptions, |
| 9 | ): Promise<{ |
| 10 | [key: string]: any; |
| 11 | }> { |
| 12 | const TIMEOUT = 7200; // 7200 seconds = 2 hours |
| 13 | const timeout = (requestOptions?.timeout ?? TIMEOUT) * 1000; // measured in ms |
| 14 | |
| 15 | const certsCache = CertsCache.getInstance(); |
| 16 | const ca = await certsCache.getCa(requestOptions?.caBundlePath); |
| 17 | |
| 18 | const agentOptions: { [key: string]: any } = { |
| 19 | ca, |
| 20 | rejectUnauthorized: requestOptions?.verifySsl, |
| 21 | timeout, |
| 22 | sessionTimeout: timeout, |
| 23 | keepAlive: true, |
| 24 | keepAliveMsecs: timeout, |
| 25 | }; |
| 26 | |
| 27 | // Handle ClientCertificateOptions |
| 28 | if (requestOptions?.clientCertificate) { |
| 29 | const { cert, key, passphrase } = requestOptions.clientCertificate; |
| 30 | |
| 31 | agentOptions.cert = getCertificateContent(cert); |
| 32 | agentOptions.key = getCertificateContent(key); |
| 33 | |
| 34 | if (requestOptions.clientCertificate.passphrase) { |
| 35 | agentOptions.passphrase = passphrase; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | if (process.env.VERBOSE_FETCH) { |
| 40 | console.log(`Fetch agent options:`); |
| 41 | console.log( |
| 42 | `\tTimeout (sessionTimeout/keepAliveMsecs): ${agentOptions.timeout}`, |
| 43 | ); |
| 44 | console.log(`\tTotal CA certs: ${ca.length}`); |
| 45 | console.log(`\tGlobal/Root CA certs: ${certsCache.fixedCa.length}`); |
| 46 | console.log(`\tCustom CA certs: ${ca.length - certsCache.fixedCa.length}`); |
| 47 | console.log( |
| 48 | `\tClient certificate: ${requestOptions?.clientCertificate ? "Yes" : "No"}`, |
| 49 | ); |
| 50 | console.log( |
| 51 | `\trejectUnauthorized/verifySsl: ${agentOptions.rejectUnauthorized ?? "not set (defaults to true)"}`, |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | return agentOptions; |
| 56 | } |