Skip to content

Commit 4b60c71

Browse files
committed
fixup: store final result of computation in cache
1 parent a791a96 commit 4b60c71

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

packages/devtools-proxy-support/src/system-ca.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export function mergeCA(...args: (NodeJSCAOption | undefined)[]): string {
5353

5454
const pemWithParsedCache = new WeakMap<
5555
string[],
56-
{ pem: string; parsed: X509Certificate | null }[]
56+
{
57+
ca: string[];
58+
messages: string[];
59+
}
5760
>();
5861
// TODO(COMPASS-8253): Remove this in favor of OpenSSL's X509_V_FLAG_PARTIAL_CHAIN
5962
// See linked tickets for details on why we need this (tl;dr: the system certificate
@@ -63,25 +66,29 @@ export function removeCertificatesWithoutIssuer(ca: string[]): {
6366
ca: string[];
6467
messages: string[];
6568
} {
66-
const messages: string[] = [];
67-
let caWithParsedCerts =
68-
pemWithParsedCache.get(ca) ??
69-
ca.map((pem) => {
70-
let parsed: X509Certificate | null = null;
71-
try {
72-
parsed = new X509Certificate(pem);
73-
} catch (err: unknown) {
74-
messages.push(
75-
`Unable to parse certificate: ${
76-
err && typeof err === 'object' && 'message' in err
77-
? String(err.message)
78-
: String(err)
79-
}`
80-
);
69+
let result:
70+
| {
71+
ca: string[];
72+
messages: string[];
8173
}
82-
return { pem, parsed };
83-
});
84-
pemWithParsedCache.set(ca, caWithParsedCerts);
74+
| undefined = pemWithParsedCache.get(ca);
75+
76+
const messages: string[] = [];
77+
let caWithParsedCerts = ca.map((pem) => {
78+
let parsed: X509Certificate | null = null;
79+
try {
80+
parsed = new X509Certificate(pem);
81+
} catch (err: unknown) {
82+
messages.push(
83+
`Unable to parse certificate: ${
84+
err && typeof err === 'object' && 'message' in err
85+
? String(err.message)
86+
: String(err)
87+
}`
88+
);
89+
}
90+
return { pem, parsed };
91+
});
8592
caWithParsedCerts = caWithParsedCerts.filter(({ parsed }) => {
8693
const keep =
8794
!parsed ||
@@ -96,7 +103,9 @@ export function removeCertificatesWithoutIssuer(ca: string[]): {
96103
}
97104
return keep;
98105
});
99-
return { ca: caWithParsedCerts.map(({ pem }) => pem), messages };
106+
result = { ca: caWithParsedCerts.map(({ pem }) => pem), messages };
107+
pemWithParsedCache.set(ca, result);
108+
return result;
100109
}
101110

102111
// Thin wrapper around system-ca, which merges:

0 commit comments

Comments
 (0)