Elffuss BraidHub

kikocis/continue public

0ec6b66ae2bc · 2.2 KiBRawPermalink
1import { globalAgent } from "https";
2import * as fs from "node:fs";
3import tls from "node:tls";
4
5import { RequestOptions } from "@continuedev/config-types";
6
7/**
8 * Extracts content from either a file path or data URI
9 */
10function getCertificateContent(input: string): string {
11 if (input.startsWith("data:")) {
12 // Parse data URI: data:[<mediatype>][;base64],<data>
13 const [header, data] = input.split(",");
14 if (header.includes("base64")) {
15 return Buffer.from(data, "base64").toString("utf8");
16 } else {
17 return decodeURIComponent(data);
18 }
19 } else {
20 // Assume it's a file path
21 return fs.readFileSync(input, "utf8");
22 }
23}
24
25/**
26 * Prepares agent options based on request options and certificates
27 */
28export function getAgentOptions(requestOptions?: RequestOptions): {
29 [key: string]: any;
30} {
31 const TIMEOUT = 7200; // 7200 seconds = 2 hours
32 const timeout = (requestOptions?.timeout ?? TIMEOUT) * 1000; // measured in ms
33
34 // Get root certificates
35 let globalCerts: string[] = [];
36 if (process.env.IS_BINARY) {
37 if (Array.isArray(globalAgent.options.ca)) {
38 globalCerts = [...globalAgent.options.ca.map((cert) => cert.toString())];
39 } else if (typeof globalAgent.options.ca !== "undefined") {
40 globalCerts.push(globalAgent.options.ca.toString());
41 }
42 }
43
44 const ca = Array.from(new Set([...tls.rootCertificates, ...globalCerts]));
45 const customCerts =
46 typeof requestOptions?.caBundlePath === "string"
47 ? [requestOptions?.caBundlePath]
48 : requestOptions?.caBundlePath;
49 if (customCerts) {
50 ca.push(
51 ...customCerts.map((customCert) => getCertificateContent(customCert)),
52 );
53 }
54
55 const agentOptions: { [key: string]: any } = {
56 ca,
57 rejectUnauthorized: requestOptions?.verifySsl,
58 timeout,
59 sessionTimeout: timeout,
60 keepAlive: true,
61 keepAliveMsecs: timeout,
62 };
63
64 // Handle ClientCertificateOptions
65 if (requestOptions?.clientCertificate) {
66 const { cert, key, passphrase } = requestOptions.clientCertificate;
67
68 agentOptions.cert = getCertificateContent(cert);
69 agentOptions.key = getCertificateContent(key);
70
71 if (requestOptions.clientCertificate.passphrase) {
72 agentOptions.passphrase = passphrase;
73 }
74 }
75
76 return agentOptions;
77}