Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read file for SSL (certs and keys) and add pfx support #2027

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-forks-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Honor HTTPS options in Preview and Dev
13 changes: 7 additions & 6 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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, {
Expand Down Expand Up @@ -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)
}
});

Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down
56 changes: 42 additions & 14 deletions packages/kit/src/core/server/index.js
Original file line number Diff line number Diff line change
@@ -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<import('net').Server>}
*/
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;
}
}

Expand All @@ -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;
}
2 changes: 2 additions & 0 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,5 @@ export interface ValidatedConfig {
};
preprocess: any;
}

export type { ViteConfig };