| 1 | import { globalAgent } from "https"; |
| 2 | import * as fs from "node:fs"; |
| 3 | import tls from "node:tls"; |
| 4 | |
| 5 | import { RequestOptions } from "@continuedev/config-types"; |
| 6 | |
| 7 | /** |
| 8 | * Prepares agent options based on request options and certificates |
| 9 | */ |
| 10 | export function getAgentOptions(requestOptions?: RequestOptions): { |
| 11 | [key: string]: any; |
| 12 | } { |
| 13 | const TIMEOUT = 7200; // 7200 seconds = 2 hours |
| 14 | const timeout = (requestOptions?.timeout ?? TIMEOUT) * 1000; // measured in ms |
| 15 | |
| 16 | // Get root certificates |
| 17 | let globalCerts: string[] = []; |
| 18 | if (process.env.IS_BINARY) { |
| 19 | if (Array.isArray(globalAgent.options.ca)) { |
| 20 | globalCerts = [...globalAgent.options.ca.map((cert) => cert.toString())]; |
| 21 | } else if (typeof globalAgent.options.ca !== "undefined") { |
| 22 | globalCerts.push(globalAgent.options.ca.toString()); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | const ca = Array.from(new Set([...tls.rootCertificates, ...globalCerts])); |
| 27 | const customCerts = |
| 28 | typeof requestOptions?.caBundlePath === "string" |
| 29 | ? [requestOptions?.caBundlePath] |
| 30 | : requestOptions?.caBundlePath; |
| 31 | if (customCerts) { |
| 32 | ca.push( |
| 33 | ...customCerts.map((customCert) => fs.readFileSync(customCert, "utf8")), |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | const agentOptions: { [key: string]: any } = { |
| 38 | ca, |
| 39 | rejectUnauthorized: requestOptions?.verifySsl, |
| 40 | timeout, |
| 41 | sessionTimeout: timeout, |
| 42 | keepAlive: true, |
| 43 | keepAliveMsecs: timeout, |
| 44 | }; |
| 45 | |
| 46 | // Handle ClientCertificateOptions |
| 47 | if (requestOptions?.clientCertificate) { |
| 48 | agentOptions.cert = fs.readFileSync( |
| 49 | requestOptions.clientCertificate.cert, |
| 50 | "utf8", |
| 51 | ); |
| 52 | agentOptions.key = fs.readFileSync( |
| 53 | requestOptions.clientCertificate.key, |
| 54 | "utf8", |
| 55 | ); |
| 56 | if (requestOptions.clientCertificate.passphrase) { |
| 57 | agentOptions.passphrase = requestOptions.clientCertificate.passphrase; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return agentOptions; |
| 62 | } |