Skip to content

Commit

Permalink
Allow passing HTTPS key pair in Vite section of config (#1456)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
  • Loading branch information
ryc76 and benmccann authored May 19, 2021
1 parent f59530f commit 586785d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-carrots-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Allow passing HTTPS key pair in Vite section of config
2 changes: 1 addition & 1 deletion packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Watcher extends EventEmitter {
}
};

this.server = await get_server(this.port, this.host, this.https, (req, res) => {
this.server = await get_server(this.port, this.host, this.https, user_config, (req, res) => {
this.vite.middlewares(req, res, async () => {
try {
const parsed = parse(req.url);
Expand Down
16 changes: 13 additions & 3 deletions packages/kit/src/core/server/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import http from 'http';
import https from 'https';

/**
*
* @param {number} port
* @param {string} host
* @param {boolean} use_https
* @param {any} user_config
* @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler
* @returns {Promise<http.Server | https.Server>}
*/
export async function get_server(port, host, use_https, handler) {
export async function get_server(port, host, use_https, user_config, handler) {
/** @type {https.ServerOptions} */
const https_options = {};

if (use_https) {
https_options.key = https_options.cert = (await import('./cert')).createCertificate();
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();
}
}

return new Promise((fulfil) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/start/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function start({ port, host, config, https: use_https = false, cwd
read: (file) => fs.readFileSync(join(config.kit.files.assets, file))
});

return get_server(port, host, use_https, (req, res) => {
return get_server(port, host, use_https, config.kit, (req, res) => {
const parsed = parse(req.url || '');

assets_handler(req, res, () => {
Expand Down

0 comments on commit 586785d

Please sign in to comment.