diff --git a/.changeset/shy-forks-grin.md b/.changeset/shy-forks-grin.md new file mode 100644 index 000000000000..769992c95435 --- /dev/null +++ b/.changeset/shy-forks-grin.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Honor HTTPS options in Preview and Dev diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index ba8cf1c53768..e6c8b29f31b1 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -91,8 +91,8 @@ class Watcher extends EventEmitter { async init_server() { if (!this.manifest) throw new Error('Must call init() before init_server()'); - /** @type {any} */ - const user_config = (this.config.kit.vite && this.config.kit.vite()) || {}; + /** @type {import('types/config').ViteConfig} ViteConfig */ + const vite_config = (this.config.kit.vite && this.config.kit.vite()) || {}; const default_config = { server: { @@ -105,12 +105,12 @@ class Watcher extends EventEmitter { /** @type {(req: import("http").IncomingMessage, res: import("http").ServerResponse) => void} */ let handler = (req, res) => {}; - this.server = await get_server(this.https, user_config, (req, res) => handler(req, res)); + this.server = await get_server(this.https, vite_config, (req, res) => handler(req, res)); - const alias = user_config.resolve && user_config.resolve.alias; + const alias = vite_config.resolve && vite_config.resolve.alias; // don't warn on overriding defaults - const [modified_user_config] = deep_merge(default_config, user_config); + const [modified_user_config] = deep_merge(default_config, vite_config); /** @type {[any, string[]]} */ const [merged_config, conflicts] = deep_merge(modified_user_config, { @@ -153,7 +153,8 @@ class Watcher extends EventEmitter { entries: [] }, ssr: { - noExternal: get_no_external(this.cwd, user_config.ssr && user_config.ssr.noExternal) + // @ts-ignore // ssr not defined in vite config.ts + noExternal: get_no_external(this.cwd, vite_config.ssr && vite_config.ssr.noExternal) } }); diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index 2c9a43471464..88bc709d01c9 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -59,7 +59,11 @@ export async function preview({ read: (file) => fs.readFileSync(join(config.kit.files.assets, file)) }); - const server = await get_server(use_https, config.kit, (req, res) => { + const vite_config = + /** @type {import('types/config').ViteConfig} ViteConfig */ + (config.kit.vite && config.kit.vite()) || {}; + + const server = await get_server(use_https, vite_config, (req, res) => { const parsed = parse(req.url || ''); assets_handler(req, res, () => { diff --git a/packages/kit/src/core/server/index.js b/packages/kit/src/core/server/index.js index 4463f840d673..876be11fc9e5 100644 --- a/packages/kit/src/core/server/index.js +++ b/packages/kit/src/core/server/index.js @@ -1,27 +1,39 @@ import http from 'http'; import https from 'https'; +import fs from 'fs'; +import path from 'path'; /** * * @param {boolean} use_https - * @param {any} user_config + * @param {import('types/config').ViteConfig} vite_config * @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler * @returns {Promise} */ -export async function get_server(use_https, user_config, handler) { - /** @type {https.ServerOptions} */ - const https_options = {}; +export async function get_server(use_https, vite_config, handler) { + /** @type {https.ServerOptions | undefined} */ + let https_options; if (use_https) { - if ( - user_config.server && - user_config.server.https && - user_config.server.https.key && - user_config.server.https.cert - ) { - https_options.key = user_config.server.https.key.toString(); - https_options.cert = user_config.server.https.cert.toString(); - } else { - https_options.key = https_options.cert = (await import('./cert')).createCertificate(); + https_options = + vite_config && vite_config.server && typeof vite_config.server.https === 'object' + ? vite_config.server.https + : {}; + + if (https_options.ca) https_options.ca = readFileIfExists(https_options.ca); + if (https_options.cert) https_options.cert = readFileIfExists(https_options.cert); + if (https_options.key) https_options.key = readFileIfExists(https_options.key); + if (https_options.pfx) { + https_options.pfx = readFileIfExists(https_options.pfx); + delete https_options.cert; + delete https_options.key; + } + + if (!https_options.pfx && (!https_options.key || !https_options.cert)) { + https_options.cert = https_options.key = (await import('./cert')).createCertificate(); + } + } else { + if (vite_config && vite_config.server) { + delete vite_config.server.https; } } @@ -31,3 +43,19 @@ export async function get_server(use_https, user_config, handler) { : http.createServer(handler) ); } + +/** + * + * @param {string | Buffer | any[]} value + * @returns {string | Buffer | Buffer[]} value + */ +function readFileIfExists(value) { + if (typeof value === 'string') { + try { + return fs.readFileSync(path.resolve(value)); + } catch (e) { + return value; + } + } + return value; +} diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index b4107317d2f9..e7245f12127f 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -156,3 +156,5 @@ export interface ValidatedConfig { }; preprocess: any; } + +export type { ViteConfig };