Skip to content

Commit

Permalink
skip generating cert each time
Browse files Browse the repository at this point in the history
  • Loading branch information
r34son committed Jun 22, 2024
1 parent 891ca7a commit 8758302
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions packages/next/src/lib/mkcert.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fs from 'fs'
import path from 'path'
import fs from 'node:fs'
import path from 'node:path'
import { X509Certificate, createPublicKey } from 'node:crypto'
import { getCacheDirectory } from './helpers/get-cache-directory'
import * as Log from '../build/output/log'
import { execSync } from 'child_process'
import { execSync } from 'node:child_process'
const { WritableStream } = require('node:stream/web') as {
WritableStream: typeof global.WritableStream
}
Expand Down Expand Up @@ -112,6 +113,22 @@ export async function createSelfSignedCertificate(
const keyPath = path.resolve(resolvedCertDir, 'localhost-key.pem')
const certPath = path.resolve(resolvedCertDir, 'localhost.pem')

if (host && fs.existsSync(keyPath) && fs.existsSync(certPath)) {
const cert = new X509Certificate(fs.readFileSync(certPath))
const key = fs.readFileSync(keyPath)

if (cert.checkHost(host) && cert.checkPrivateKey(createPublicKey(key))) {
Log.info('Using already generated self signed certificate')
const caLocation = execSync(`"${binaryPath}" -CAROOT`).toString().trim()

return {
key: keyPath,
cert: certPath,
rootCA: `${caLocation}/rootCA.pem`,
}
}
}

Log.info(
'Attempting to generate self signed certificate. This may prompt for your password'
)
Expand Down

0 comments on commit 8758302

Please sign in to comment.