Improved example getCertificates for strict mode

This commit is contained in:
Alexander Cerutti
2021-12-23 18:36:30 +01:00
parent 4b9d7e099b
commit fc4a0c377b

View File

@@ -1,16 +1,26 @@
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import path from "path"; import path from "path";
const certificatesCache: Partial<{ interface Cache {
signerCert: Buffer; certificates:
signerKey: Buffer; | {
wwdr: Buffer; signerCert: Buffer | string;
signerKeyPassphrase: string; signerKey: Buffer | string;
}> = {}; wwdr: Buffer | string;
signerKeyPassphrase: string;
}
| undefined;
}
export async function getCertificates(): Promise<typeof certificatesCache> { const cache: Cache = {
if (Object.keys(certificatesCache).length) { certificates: undefined,
return certificatesCache; };
export async function getCertificates(): Promise<
Exclude<Cache["certificates"], undefined>
> {
if (cache.certificates) {
return cache.certificates;
} }
const [signerCert, signerKey, wwdr, signerKeyPassphrase] = const [signerCert, signerKey, wwdr, signerKeyPassphrase] =
@@ -30,12 +40,12 @@ export async function getCertificates(): Promise<typeof certificatesCache> {
Promise.resolve("123456"), Promise.resolve("123456"),
]); ]);
Object.assign(certificatesCache, { cache.certificates = {
signerCert, signerCert,
signerKey, signerKey,
wwdr, wwdr,
signerKeyPassphrase, signerKeyPassphrase,
}); };
return certificatesCache; return cache.certificates;
} }