Skip to content

Commit

Permalink
write certificate material to one JSON file and also read from that
Browse files Browse the repository at this point in the history
  • Loading branch information
Mastercuber committed Nov 6, 2023
1 parent 542cdaf commit 536868b
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/_cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ export async function resolveCertificate(

if (typeof options === "object" && options.reuse) {
// Reuse previously autogenerated certificates if exists
const certExists = await pathExists(certificateDirectory + "cert.pem");
const keyExists = await pathExists(certificateDirectory + "cert-key.pem");
const certsExists = await pathExists(certificateDirectory + "certs.json");

if (certExists && keyExists) {
const cert = await fs.readFile(certificateDirectory + "cert.pem");
const key = await fs.readFile(certificateDirectory + "cert-key.pem");
if (certsExists) {
const cert = JSON.parse(
(await fs.readFile(certificateDirectory + "certs.json")).toString(
"utf8",
),
);
return {
cert: cert.toString("utf8"),
key: key.toString("utf8"),
cert: cert.cert,
key: cert.key,
};
}
}
Expand Down Expand Up @@ -124,8 +126,6 @@ async function generateCertificates(
const ca = await generateCACert(caOptions);

await mkdir(resolve(certificateDirectory), { recursive: true });
await writeFile(resolve(certificateDirectory + "ca.pem"), ca.cert);
await writeFile(resolve(certificateDirectory + "ca-key.pem"), ca.key);

const domains = Array.isArray(options.domains)
? options.domains
Expand All @@ -140,12 +140,22 @@ async function generateCertificates(
});

await writeFile(
resolve(certificateDirectory + "cert.pem"),
cert.cert + ca.cert,
resolve(certificateDirectory + "certs.json"),
JSON.stringify({
cert: cert.cert + ca.cert,
key: cert.key,
caCert: ca.cert,
caKey: ca.key,
}),
);
await writeFile(resolve(certificateDirectory + "cert-key.pem"), cert.key);

return { ca, cert };
return {
ca,
cert: {
cert: cert.cert + ca.cert,
key: cert.key,
},
};
}

async function resolveCert(options: HTTPSOptions): Promise<Certificate> {
Expand Down

0 comments on commit 536868b

Please sign in to comment.