Skip to content

Commit c17b724

Browse files
committed
dynamically check certificate expiration
1 parent ca7042c commit c17b724

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/index.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import path from 'node:path'
22
import { promises as fsp } from 'node:fs'
33
import type { Plugin } from 'vite'
44

5+
// @ts-ignore
6+
import forge from 'node-forge/lib/forge'
7+
import 'node-forge/lib/pki'
8+
59
const defaultCacheDir = 'node_modules/.vite'
610

711
interface Options {
@@ -41,13 +45,18 @@ export async function getCertificate(
4145
const cachePath = path.join(cacheDir, '_cert.pem')
4246

4347
try {
44-
const [stat, content] = await Promise.all([
45-
fsp.stat(cachePath),
46-
fsp.readFile(cachePath, 'utf8'),
47-
])
48+
const content = await fsp.readFile(cachePath, "utf8")
49+
const certContent = content.match(
50+
/-----BEGIN CERTIFICATE-----[\s\S]+-----END CERTIFICATE-----/,
51+
)
52+
if (!certContent) {
53+
throw new Error("certificate not detected.")
54+
}
55+
56+
const cert = forge.pki.certificateFromPem(certContent[0])
4857

49-
if (Date.now() - stat.ctime.valueOf() > 30 * 24 * 60 * 60 * 1000) {
50-
throw new Error('cache is outdated.')
58+
if (new Date() > cert.validity.notAfter) {
59+
throw new Error("cache is outdated.")
5160
}
5261

5362
return content

0 commit comments

Comments
 (0)