From 65127c3ca15dc849b7961de7ce204bbb0600a3cf Mon Sep 17 00:00:00 2001 From: gz65555 Date: Fri, 29 Dec 2023 18:40:23 +0800 Subject: [PATCH 1/3] feat: support custom domains --- package.json | 1 + src/certificate.ts | 23 ++++++++--------------- src/index.ts | 26 ++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 8640ead..18d1c81 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/plugin-basic-ssl", "version": "1.0.2", "license": "MIT", + "type": "module", "author": "Evan You and Vite Contributors", "files": [ "dist" diff --git a/src/certificate.ts b/src/certificate.ts index d8ce82d..ddeb6b5 100644 --- a/src/certificate.ts +++ b/src/certificate.ts @@ -52,15 +52,15 @@ function toPositiveHex(hexString: string) { return mostSignificativeHexAsInt.toString() + hexString.substring(1) } -export function createCertificate(): string { +export function createCertificate(name: string, domains?: string[]): string { const days = 30 const keySize = 2048 + const appendDomains = domains + ? domains.map(item => ({ type: 2, value: item })) + : [] + const extensions = [ - // { - // name: 'basicConstraints', - // cA: true, - // }, { name: 'keyUsage', keyCertSign: true, @@ -88,14 +88,6 @@ export function createCertificate(): string { type: 2, value: 'localhost.localdomain' }, - { - type: 2, - value: 'lvh.me' - }, - { - type: 2, - value: '*.lvh.me' - }, { type: 2, value: '[::1]' @@ -108,7 +100,8 @@ export function createCertificate(): string { { type: 7, ip: 'fe80::1' - } + }, + ...appendDomains ] } ] @@ -116,7 +109,7 @@ export function createCertificate(): string { const attrs = [ { name: 'commonName', - value: 'example.org' + value: name }, { name: 'countryName', diff --git a/src/index.ts b/src/index.ts index c9cad38..e593de2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,12 +3,23 @@ import { promises as fsp } from 'node:fs' import type { Plugin } from 'vite' const defaultCacheDir = 'node_modules/.vite' +const defaultName = 'example.org' -function viteBasicSslPlugin(): Plugin { +interface Options { + certDir: string + domains: string[] + name: string +} + +function viteBasicSslPlugin(options?: Partial): Plugin { return { name: 'vite:basic-ssl', async configResolved(config) { - const certificate = await getCertificate((config.cacheDir ?? defaultCacheDir) + '/basic-ssl') + const certificate = await getCertificate( + options?.certDir ?? (config.cacheDir ?? defaultCacheDir) + '/basic-ssl', + options?.name ?? defaultName, + options?.domains + ) const https = () => ({ cert: certificate, key: certificate }) config.server.https = Object.assign({}, config.server.https, https()) config.preview.https = Object.assign({}, config.preview.https, https()) @@ -16,7 +27,11 @@ function viteBasicSslPlugin(): Plugin { } } -export async function getCertificate(cacheDir: string) { +export async function getCertificate( + cacheDir: string, + name: string, + domains?: string[] +) { const cachePath = path.join(cacheDir, '_cert.pem') try { @@ -31,7 +46,10 @@ export async function getCertificate(cacheDir: string) { return content } catch { - const content = (await import('./certificate')).createCertificate() + const content = (await import('./certificate')).createCertificate( + name, + domains + ) fsp .mkdir(cacheDir, { recursive: true }) .then(() => fsp.writeFile(cachePath, content)) From 5806239a6edc43ba295fabee6a6587576b696386 Mon Sep 17 00:00:00 2001 From: gz65555 Date: Wed, 10 Jan 2024 17:52:39 +0800 Subject: [PATCH 2/3] chore: revert file to original case --- package.json | 1 - src/certificate.ts | 12 ++++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 18d1c81..8640ead 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "@vitejs/plugin-basic-ssl", "version": "1.0.2", "license": "MIT", - "type": "module", "author": "Evan You and Vite Contributors", "files": [ "dist" diff --git a/src/certificate.ts b/src/certificate.ts index ddeb6b5..ca4d41b 100644 --- a/src/certificate.ts +++ b/src/certificate.ts @@ -61,6 +61,10 @@ export function createCertificate(name: string, domains?: string[]): string { : [] const extensions = [ + // { + // name: 'basicConstraints', + // cA: true, + // }, { name: 'keyUsage', keyCertSign: true, @@ -88,6 +92,14 @@ export function createCertificate(name: string, domains?: string[]): string { type: 2, value: 'localhost.localdomain' }, + { + type: 2, + value: 'lvh.me' + }, + { + type: 2, + value: '*.lvh.me' + }, { type: 2, value: '[::1]' From 35696cb5e367a5e70ee48702839b108b73436468 Mon Sep 17 00:00:00 2001 From: gz65555 Date: Thu, 11 Jan 2024 11:24:44 +0800 Subject: [PATCH 3/3] chore: update readme and fix test type error --- README.md | 11 +++++++++-- src/certificate.ts | 2 +- src/index.ts | 5 ++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d976db7..577f373 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,18 @@ import basicSsl from '@vitejs/plugin-basic-ssl' export default { plugins: [ - basicSsl() + basicSsl({ + /** name of certification */ + name: 'test', + /** custom trust domains */ + domains: ['*.custom.com'], + /** custom certification directory */ + certDir: '/Users/.../.devServer/cert' + }) ] } ``` - + ## License MIT diff --git a/src/certificate.ts b/src/certificate.ts index ca4d41b..71c730b 100644 --- a/src/certificate.ts +++ b/src/certificate.ts @@ -52,7 +52,7 @@ function toPositiveHex(hexString: string) { return mostSignificativeHexAsInt.toString() + hexString.substring(1) } -export function createCertificate(name: string, domains?: string[]): string { +export function createCertificate(name: string = 'example.org', domains?: string[]): string { const days = 30 const keySize = 2048 diff --git a/src/index.ts b/src/index.ts index e593de2..a5b9751 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import { promises as fsp } from 'node:fs' import type { Plugin } from 'vite' const defaultCacheDir = 'node_modules/.vite' -const defaultName = 'example.org' interface Options { certDir: string @@ -17,7 +16,7 @@ function viteBasicSslPlugin(options?: Partial): Plugin { async configResolved(config) { const certificate = await getCertificate( options?.certDir ?? (config.cacheDir ?? defaultCacheDir) + '/basic-ssl', - options?.name ?? defaultName, + options?.name, options?.domains ) const https = () => ({ cert: certificate, key: certificate }) @@ -29,7 +28,7 @@ function viteBasicSslPlugin(options?: Partial): Plugin { export async function getCertificate( cacheDir: string, - name: string, + name?: string, domains?: string[] ) { const cachePath = path.join(cacheDir, '_cert.pem')