Skip to content

Commit

Permalink
#844 fix for SSL HMR websockets (#1517)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben McCann <[email protected]>
  • Loading branch information
JBusillo and benmccann authored Jun 3, 2021
1 parent fbd5f8a commit 5aa64ab
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-bugs-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: SSL for HMR websockets #844
17 changes: 14 additions & 3 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class Watcher extends EventEmitter {
/** @type {any} */
const user_config = (this.config.kit.vite && this.config.kit.vite()) || {};

/** @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));

/**
* @type {vite.ViteDevServer}
*/
Expand All @@ -104,7 +109,11 @@ class Watcher extends EventEmitter {
publicDir: this.config.kit.files.assets,
server: {
...user_config.server,
middlewareMode: true
middlewareMode: true,
hmr: {
...(user_config.server && user_config.server.hmr),
...(this.https ? { server: this.server, port: this.port } : {})
}
},
optimizeDeps: {
...user_config.optimizeDeps,
Expand All @@ -131,7 +140,7 @@ class Watcher extends EventEmitter {
}
};

this.server = await get_server(this.port, this.host, this.https, user_config, (req, res) => {
handler = (req, res) => {
this.vite.middlewares(req, res, async () => {
try {
const parsed = parse(req.url);
Expand Down Expand Up @@ -317,7 +326,9 @@ class Watcher extends EventEmitter {
res.end(e.stack);
}
});
});
};

await this.server.listen(this.port, this.host || '0.0.0.0');
}

update() {
Expand Down
18 changes: 6 additions & 12 deletions packages/kit/src/core/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ 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>}
* @returns {Promise<import('net').Server>}
*/
export async function get_server(port, host, use_https, user_config, handler) {
export async function get_server(use_https, user_config, handler) {
/** @type {https.ServerOptions} */
const https_options = {};

Expand All @@ -27,13 +25,9 @@ export async function get_server(port, host, use_https, user_config, handler) {
}
}

return new Promise((fulfil) => {
const server = use_https
return Promise.resolve(
use_https
? https.createServer(/** @type {https.ServerOptions} */ (https_options), handler)
: http.createServer(handler);

server.listen(port, host || '0.0.0.0', () => {
fulfil(server);
});
});
: http.createServer(handler)
);
}
6 changes: 5 additions & 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, config.kit, (req, res) => {
const server = await get_server(use_https, config.kit, (req, res) => {
const parsed = parse(req.url || '');

assets_handler(req, res, () => {
Expand Down Expand Up @@ -83,4 +83,8 @@ export async function start({ port, host, config, https: use_https = false, cwd
});
});
});

await server.listen(port, host || '0.0.0.0');

return Promise.resolve(server);
}
2 changes: 1 addition & 1 deletion packages/kit/test/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type TestContext = {
};

watcher: any; // watcher type is not exposed
server: import('http').Server;
server: import('net').Server;
reset: () => Promise<void>;
unpatch: () => void;
};
Expand Down

0 comments on commit 5aa64ab

Please sign in to comment.