Elffuss BraidHub

kikocis/continue public

main · 3.5 KiBRawPermalink
1import { globalAgent } from "https";
2import * as fs from "node:fs";
3import tls from "node:tls";
4
5/**
6 * Extracts content from either a file path or data URI
7 */
8export function getCertificateContent(input: string): string {
9 if (input.startsWith("data:")) {
10 // Parse data URI: data:[<mediatype>][;base64],<data>
11 const [header, data] = input.split(",");
12 if (header.includes("base64")) {
13 return Buffer.from(data, "base64").toString("utf8");
14 } else {
15 return decodeURIComponent(data);
16 }
17 } else {
18 // Assume it's a file path
19 return fs.readFileSync(input, "utf8");
20 }
21}
22
23export class CertsCache {
24 private static instance: CertsCache;
25 private _fixedCa: string[] = [];
26 private _initialized: boolean = false;
27 private _customCerts: Map<string, string> = new Map();
28
29 private constructor() {}
30
31 public static getInstance(): CertsCache {
32 if (!CertsCache.instance) {
33 CertsCache.instance = new CertsCache();
34 }
35 return CertsCache.instance;
36 }
37
38 get fixedCa(): string[] {
39 if (this._initialized) {
40 return this._fixedCa;
41 }
42
43 const globalCerts: string[] = [];
44 if (Boolean(process.env.IS_BINARY)) {
45 if (Array.isArray(globalAgent.options.ca)) {
46 globalCerts.push(
47 ...globalAgent.options.ca.map((cert) => cert.toString()),
48 );
49 } else if (typeof globalAgent.options.ca !== "undefined") {
50 globalCerts.push(globalAgent.options.ca.toString());
51 }
52 }
53
54 const extraCerts: string[] = [];
55 if (process.env.NODE_EXTRA_CA_CERTS) {
56 try {
57 const content = fs.readFileSync(
58 process.env.NODE_EXTRA_CA_CERTS,
59 "utf8",
60 );
61 extraCerts.push(content);
62 } catch (error) {
63 if (process.env.VERBOSE_FETCH) {
64 console.error(
65 `Error reading NODE_EXTRA_CA_CERTS file: ${process.env.NODE_EXTRA_CA_CERTS}`,
66 error,
67 );
68 }
69 }
70 }
71
72 this._fixedCa = Array.from(
73 new Set([...tls.rootCertificates, ...globalCerts, ...extraCerts]),
74 );
75 this._initialized = true;
76 return this._fixedCa;
77 }
78
79 async getCachedCustomCert(path: string): Promise<string | undefined> {
80 if (this._customCerts.has(path)) {
81 return this._customCerts.get(path);
82 }
83 const certContent = getCertificateContent(path);
84 this._customCerts.set(path, certContent);
85 return certContent;
86 }
87
88 async getAllCachedCustomCerts(
89 caBundlePath: string[] | string,
90 ): Promise<string[]> {
91 const paths = Array.isArray(caBundlePath) ? caBundlePath : [caBundlePath];
92 const certs: string[] = [];
93 await Promise.all(
94 paths.map(async (path) => {
95 try {
96 const certContent = await this.getCachedCustomCert(path);
97 if (certContent) {
98 certs.push(certContent);
99 } else if (process.env.VERBOSE_FETCH) {
100 console.warn(`Empty certificate found at ${path}`);
101 }
102 } catch (error) {
103 if (process.env.VERBOSE_FETCH) {
104 console.error(
105 `Error loading custom certificate from ${path}:`,
106 error,
107 );
108 }
109 }
110 }),
111 );
112 return certs;
113 }
114
115 async getCa(caBundlePath: undefined | string | string[]): Promise<string[]> {
116 if (!caBundlePath) {
117 return this.fixedCa;
118 }
119
120 const customCerts = await this.getAllCachedCustomCerts(caBundlePath);
121 return [...this.fixedCa, ...customCerts];
122 }
123
124 async clear(): Promise<void> {
125 this._customCerts.clear();
126 this._initialized = false;
127 this._fixedCa = [];
128 }
129}