Skip to content

Commit ca7042c

Browse files
committed
add optional ttlDays parameter
1 parent 3b57894 commit ca7042c

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export default {
1717
name: 'test',
1818
/** custom trust domains */
1919
domains: ['*.custom.com'],
20+
/** optional, days before certificate expires */
21+
ttlDays: 30,
2022
/** custom certification directory */
2123
certDir: '/Users/.../.devServer/cert',
2224
}),

src/certificate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ function toPositiveHex(hexString: string) {
5555
export function createCertificate(
5656
name: string = 'example.org',
5757
domains?: string[],
58+
ttlDays = 30,
5859
): string {
59-
const days = 30
6060
const keySize = 2048
6161

6262
const appendDomains = domains
@@ -146,7 +146,7 @@ export function createCertificate(
146146

147147
cert.validity.notBefore = new Date()
148148
cert.validity.notAfter = new Date()
149-
cert.validity.notAfter.setDate(cert.validity.notBefore.getDate() + days)
149+
cert.validity.notAfter.setDate(cert.validity.notBefore.getDate() + ttlDays)
150150

151151
cert.setSubject(attrs)
152152
cert.setIssuer(attrs)

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface Options {
88
certDir: string
99
domains: string[]
1010
name: string
11+
ttlDays: number
1112
}
1213

1314
function viteBasicSslPlugin(options?: Partial<Options>): Plugin {
@@ -18,6 +19,7 @@ function viteBasicSslPlugin(options?: Partial<Options>): Plugin {
1819
options?.certDir ?? (config.cacheDir ?? defaultCacheDir) + '/basic-ssl',
1920
options?.name,
2021
options?.domains,
22+
options?.ttlDays,
2123
)
2224
const https = () => ({ cert: certificate, key: certificate })
2325
if (config.server.https === undefined || !!config.server.https) {
@@ -34,6 +36,7 @@ export async function getCertificate(
3436
cacheDir: string,
3537
name?: string,
3638
domains?: string[],
39+
ttlDays?: number,
3740
) {
3841
const cachePath = path.join(cacheDir, '_cert.pem')
3942

@@ -52,6 +55,7 @@ export async function getCertificate(
5255
const content = (await import('./certificate')).createCertificate(
5356
name,
5457
domains,
58+
ttlDays,
5559
)
5660
fsp
5761
.mkdir(cacheDir, { recursive: true })

0 commit comments

Comments
 (0)