File tree Expand file tree Collapse file tree 1 file changed +15
-6
lines changed
Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,10 @@ import path from 'node:path'
22import { promises as fsp } from 'node:fs'
33import type { Plugin } from 'vite'
44
5+ // @ts -ignore
6+ import forge from 'node-forge/lib/forge'
7+ import 'node-forge/lib/pki'
8+
59const defaultCacheDir = 'node_modules/.vite'
610
711interface 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+ / - - - - - B E G I N C E R T I F I C A T E - - - - - [ \s \S ] + - - - - - E N D C E R T I F I C A T E - - - - - / ,
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
You can’t perform that action at this time.
0 commit comments