diff --git a/.github/renovate.json5 b/.github/renovate.json5 index a589b4b..244f7f8 100644 --- a/.github/renovate.json5 +++ b/.github/renovate.json5 @@ -7,7 +7,7 @@ "packageRules": [ { "depTypeList": ["peerDependencies"], - "enabled": false - } - ] -} \ No newline at end of file + "enabled": false, + }, + ], +} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c1b7233..d9219c2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,8 +1,8 @@ -name: 'ci' +name: "ci" on: push: branches: - - '**' + - "**" pull_request: branches: - main @@ -19,7 +19,7 @@ jobs: uses: actions/setup-node@v4 with: node-version: 22 - cache: 'pnpm' + cache: "pnpm" - run: pnpm install diff --git a/.github/workflows/release-tag.yml b/.github/workflows/release-tag.yml index fe9c871..73db8a2 100644 --- a/.github/workflows/release-tag.yml +++ b/.github/workflows/release-tag.yml @@ -1,7 +1,7 @@ on: push: tags: - - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + - "v*" # Push events to matching v*, i.e. v1.0, v20.15.10 name: Create Release diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..0f4f559 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,6 @@ +CHANGELOG.md +dist/ +temp/ +LICENSE.md +pnpm-lock.yaml +pnpm-workspace.yaml diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index ef93d94..0000000 --- a/.prettierrc +++ /dev/null @@ -1,5 +0,0 @@ -semi: false -singleQuote: true -printWidth: 80 -trailingComma: 'none' -arrowParens: 'avoid' diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..6a3a2f9 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,19 @@ +{ + "semi": false, + "singleQuote": true, + "overrides": [ + { + "files": ["*.json5"], + "options": { + "singleQuote": false, + "quoteProps": "preserve" + } + }, + { + "files": ["*.yml"], + "options": { + "singleQuote": false + } + } + ] +} diff --git a/README.md b/README.md index 577f373..8f7ccfb 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,9 @@ export default { /** custom trust domains */ domains: ['*.custom.com'], /** custom certification directory */ - certDir: '/Users/.../.devServer/cert' - }) - ] + certDir: '/Users/.../.devServer/cert', + }), + ], } ``` diff --git a/build.config.ts b/build.config.ts index 6c2d340..4e5a7fb 100644 --- a/build.config.ts +++ b/build.config.ts @@ -9,12 +9,12 @@ export default defineBuildConfig({ emitCJS: true, inlineDependencies: true, esbuild: { - target: 'node14.21.3' + target: 'node14.21.3', }, output: { generatedCode: { - reservedNamesAsProps: false - } - } - } + reservedNamesAsProps: false, + }, + }, + }, }) diff --git a/package.json b/package.json index 91edcdc..db573b2 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dev": "unbuild --stub", "build": "unbuild && tsx scripts/patchCJS.ts", "test": "vitest run", + "format": "prettier --write --cache .", "release": "tsx -C import scripts/release.ts", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s" }, diff --git a/playground/index.html b/playground/index.html index f0c6ef7..40b0038 100644 --- a/playground/index.html +++ b/playground/index.html @@ -1,4 +1,4 @@ - + diff --git a/playground/style.css b/playground/style.css index 80775c1..bbba76e 100644 --- a/playground/style.css +++ b/playground/style.css @@ -1,95 +1,94 @@ :root { - font-family: Inter, Avenir, Helvetica, Arial, sans-serif; - font-size: 16px; - line-height: 24px; - font-weight: 400; - - color-scheme: light dark; - color: rgba(255, 255, 255, 0.87); - background-color: #242424; - - font-synthesis: none; - text-rendering: optimizeLegibility; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; - -webkit-text-size-adjust: 100%; - } - - a { - font-weight: 500; - color: #646cff; - text-decoration: inherit; + font-family: Inter, Avenir, Helvetica, Arial, sans-serif; + font-size: 16px; + line-height: 24px; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-text-size-adjust: 100%; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +#app { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} + +.logo { + height: 6em; + padding: 1.5em; + will-change: filter; +} +.logo:hover { + filter: drop-shadow(0 0 2em #646cffaa); +} + +.card { + padding: 2em; +} + +.read-the-docs { + color: #888; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #646cff; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; } a:hover { - color: #535bf2; - } - - body { - margin: 0; - display: flex; - place-items: center; - min-width: 320px; - min-height: 100vh; - } - - h1 { - font-size: 3.2em; - line-height: 1.1; - } - - #app { - max-width: 1280px; - margin: 0 auto; - padding: 2rem; - text-align: center; - } - - .logo { - height: 6em; - padding: 1.5em; - will-change: filter; - } - .logo:hover { - filter: drop-shadow(0 0 2em #646cffaa); + color: #747bff; } - - .card { - padding: 2em; - } - - .read-the-docs { - color: #888; - } - button { - border-radius: 8px; - border: 1px solid transparent; - padding: 0.6em 1.2em; - font-size: 1em; - font-weight: 500; - font-family: inherit; - background-color: #1a1a1a; - cursor: pointer; - transition: border-color 0.25s; - } - button:hover { - border-color: #646cff; - } - button:focus, - button:focus-visible { - outline: 4px auto -webkit-focus-ring-color; - } - - @media (prefers-color-scheme: light) { - :root { - color: #213547; - background-color: #ffffff; - } - a:hover { - color: #747bff; - } - button { - background-color: #f9f9f9; - } + background-color: #f9f9f9; } - \ No newline at end of file +} diff --git a/playground/vite.config.ts b/playground/vite.config.ts index 5284ae2..72ffea3 100644 --- a/playground/vite.config.ts +++ b/playground/vite.config.ts @@ -4,11 +4,9 @@ import basicSsl from '../src/index' const config = defineConfig({ build: { sourcemap: true, - minify: false + minify: false, }, - plugins: [ - basicSsl() - ] + plugins: [basicSsl()], }) export default config diff --git a/scripts/release.ts b/scripts/release.ts index f724c3e..64248c2 100644 --- a/scripts/release.ts +++ b/scripts/release.ts @@ -21,23 +21,23 @@ const versionIncrements = [ 'patch', 'minor', 'major', - ...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : []) + ...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : []), ] -const inc = i => semver.inc(currentVersion, i, preId) +const inc = (i) => semver.inc(currentVersion, i, preId) const run = (bin, args, opts = {}) => execa(bin, args, { stdio: 'inherit', ...opts }) const dryRun = (bin, args, opts = {}) => console.log(colors.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts) const runIfNotDry = isDryRun ? dryRun : run -const step = msg => console.log(colors.cyan(msg)) +const step = (msg) => console.log(colors.cyan(msg)) async function main() { let targetVersion: string = args._[0] if (!targetVersion) { const choices = versionIncrements - .map(i => `${i} (${inc(i)})`) + .map((i) => `${i} (${inc(i)})`) .concat(['custom']) // no explicit version, offer suggestions @@ -45,7 +45,7 @@ async function main() { type: 'select', name: 'release', message: 'Select release type', - choices: choices + choices: choices, }) if (release === 'custom') { @@ -54,7 +54,7 @@ async function main() { type: 'input', name: 'version', message: 'Input custom version', - initial: currentVersion + initial: currentVersion, }) ).version } else { @@ -73,7 +73,7 @@ async function main() { const { yes } = await prompt<{ yes: boolean }>({ type: 'confirm', name: 'yes', - message: `Releasing v${targetVersion}. Confirm?` + message: `Releasing v${targetVersion}. Confirm?`, }) if (!yes) { @@ -168,15 +168,15 @@ async function publishPackage(version, runIfNotDry) { 'publish', ...(releaseTag ? ['--tag', releaseTag] : []), '--access', - 'public' + 'public', ], { cwd: pkgRoot, - stdio: 'pipe' - } + stdio: 'pipe', + }, ) console.log( - colors.green(`Successfully published ${publishedName}@${version}`) + colors.green(`Successfully published ${publishedName}@${version}`), ) } catch (e) { if (e.stderr.match(/previously published/)) { @@ -187,7 +187,7 @@ async function publishPackage(version, runIfNotDry) { } } -main().catch(err => { +main().catch((err) => { updatePackage(currentVersion) console.error(err) }) diff --git a/src/certificate.ts b/src/certificate.ts index 71c730b..8370b6f 100644 --- a/src/certificate.ts +++ b/src/certificate.ts @@ -52,12 +52,15 @@ function toPositiveHex(hexString: string) { return mostSignificativeHexAsInt.toString() + hexString.substring(1) } -export function createCertificate(name: string = 'example.org', domains?: string[]): string { +export function createCertificate( + name: string = 'example.org', + domains?: string[], +): string { const days = 30 const keySize = 2048 const appendDomains = domains - ? domains.map(item => ({ type: 2, value: item })) + ? domains.map((item) => ({ type: 2, value: item })) : [] const extensions = [ @@ -71,14 +74,14 @@ export function createCertificate(name: string = 'example.org', domains?: string digitalSignature: true, nonRepudiation: true, keyEncipherment: true, - dataEncipherment: true + dataEncipherment: true, }, { name: 'extKeyUsage', serverAuth: true, clientAuth: true, codeSigning: true, - timeStamping: true + timeStamping: true, }, { name: 'subjectAltName', @@ -86,63 +89,63 @@ export function createCertificate(name: string = 'example.org', domains?: string { // type 2 is DNS type: 2, - value: 'localhost' + value: 'localhost', }, { type: 2, - value: 'localhost.localdomain' + value: 'localhost.localdomain', }, { type: 2, - value: 'lvh.me' + value: 'lvh.me', }, { type: 2, - value: '*.lvh.me' + value: '*.lvh.me', }, { type: 2, - value: '[::1]' + value: '[::1]', }, { // type 7 is IP type: 7, - ip: '127.0.0.1' + ip: '127.0.0.1', }, { type: 7, - ip: 'fe80::1' + ip: 'fe80::1', }, - ...appendDomains - ] - } + ...appendDomains, + ], + }, ] const attrs = [ { name: 'commonName', - value: name + value: name, }, { name: 'countryName', - value: 'US' + value: 'US', }, { shortName: 'ST', - value: 'Virginia' + value: 'Virginia', }, { name: 'localityName', - value: 'Blacksburg' + value: 'Blacksburg', }, { name: 'organizationName', - value: 'Test' + value: 'Test', }, { shortName: 'OU', - value: 'Test' - } + value: 'Test', + }, ] const keyPair = forge.pki.rsa.generateKeyPair(keySize) @@ -150,7 +153,7 @@ export function createCertificate(name: string = 'example.org', domains?: string const cert = forge.pki.createCertificate() cert.serialNumber = toPositiveHex( - forge.util.bytesToHex(forge.random.getBytesSync(9)) + forge.util.bytesToHex(forge.random.getBytesSync(9)), ) // the serial number can be decimal or hex (if preceded by 0x) cert.validity.notBefore = new Date() diff --git a/src/index.ts b/src/index.ts index 39abefc..2b44c0c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ function viteBasicSslPlugin(options?: Partial): Plugin { const certificate = await getCertificate( options?.certDir ?? (config.cacheDir ?? defaultCacheDir) + '/basic-ssl', options?.name, - options?.domains + options?.domains, ) const https = () => ({ cert: certificate, key: certificate }) if (config.server.https === undefined || !!config.server.https) { @@ -26,21 +26,21 @@ function viteBasicSslPlugin(options?: Partial): Plugin { if (config.preview.https === undefined || !!config.preview.https) { config.preview.https = Object.assign({}, config.preview.https, https()) } - } + }, } } export async function getCertificate( cacheDir: string, name?: string, - domains?: string[] + domains?: string[], ) { const cachePath = path.join(cacheDir, '_cert.pem') try { const [stat, content] = await Promise.all([ fsp.stat(cachePath), - fsp.readFile(cachePath, 'utf8') + fsp.readFile(cachePath, 'utf8'), ]) if (Date.now() - stat.ctime.valueOf() > 30 * 24 * 60 * 60 * 1000) { @@ -51,7 +51,7 @@ export async function getCertificate( } catch { const content = (await import('./certificate')).createCertificate( name, - domains + domains, ) fsp .mkdir(cacheDir, { recursive: true }) diff --git a/test/test.spec.ts b/test/test.spec.ts index a84f191..6637704 100644 --- a/test/test.spec.ts +++ b/test/test.spec.ts @@ -3,6 +3,10 @@ import { createCertificate } from '../src/certificate' test('create certificate', () => { const content = createCertificate() - expect(content).toMatch(/-----BEGIN RSA PRIVATE KEY-----(\n|\r|.)*-----END RSA PRIVATE KEY-----/) - expect(content).toMatch(/-----BEGIN CERTIFICATE-----(\n|\r|.)*-----END CERTIFICATE-----/) -}) \ No newline at end of file + expect(content).toMatch( + /-----BEGIN RSA PRIVATE KEY-----(\n|\r|.)*-----END RSA PRIVATE KEY-----/, + ) + expect(content).toMatch( + /-----BEGIN CERTIFICATE-----(\n|\r|.)*-----END CERTIFICATE-----/, + ) +}) diff --git a/test/vitest.config.ts b/test/vitest.config.ts index a9f9f2b..a013249 100644 --- a/test/vitest.config.ts +++ b/test/vitest.config.ts @@ -3,6 +3,6 @@ import { defineConfig } from 'vite' export default defineConfig({ test: { - testTimeout: 100000 - } + testTimeout: 100000, + }, })