Skip to content

Commit 9fbb9c4

Browse files
committed
use X509Certificate to parse cert instead of node-forge
1 parent 9c68060 commit 9fbb9c4

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/index.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import path from 'node:path'
2+
import { X509Certificate } from 'node:crypto'
23
import { promises as fsp } from 'node:fs'
34
import type { Plugin } from 'vite'
45

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

118
interface Options {
@@ -46,17 +43,13 @@ export async function getCertificate(
4643

4744
try {
4845
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])
46+
const cert = new X509Certificate(content);
5747

58-
if (new Date() > cert.validity.notAfter) {
59-
throw new Error('cache is outdated.')
48+
// validTo is a nonstandard format, but it successfully parses. validToDate
49+
// is not available until node 22
50+
// https://github.com/nodejs/node/issues/52931
51+
if (Date.now() > Date.parse(cert.validTo)) {
52+
throw new Error('cache is outdated.');
6053
}
6154

6255
return content

0 commit comments

Comments
 (0)