-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add HTTPS and HTTP/2 support for adapter-node
#10183
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/adapter-node': minor | ||
| --- | ||
|
|
||
| feat: add opt-in HTTPS and HTTP/2 support |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,42 @@ | ||
| import { handler } from 'HANDLER'; | ||
| import { env } from 'ENV'; | ||
| import polka from 'polka'; | ||
| import http from 'node:http'; | ||
| import https from 'node:https'; | ||
| import http2 from 'node:http2'; | ||
| import fs from 'node:fs'; | ||
|
|
||
| export const path = env('SOCKET_PATH', false); | ||
| export const host = env('HOST', '0.0.0.0'); | ||
| export const port = env('PORT', !path && '3000'); | ||
| export const certPath = env('CERT_PATH', false); | ||
| export const certKeyPath = env('CERT_KEY_PATH', false); | ||
| export const httpsPort = env('HTTPS_PORT', !path && '3001'); | ||
| export const onlyHttps = env('ONLY_HTTPS', false); | ||
| export const noHttp2 = env('NO_HTTP2', false); | ||
|
|
||
| const server = polka().use(handler); | ||
| const app = polka().use(handler); | ||
ghostdevv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| server.listen({ path, host, port }, () => { | ||
| console.log(`Listening on ${path ? path : host + ':' + port}`); | ||
| }); | ||
| if (!onlyHttps) { | ||
| // TODO Remove the `@ts-expect-error`s below once https://github.com/lukeed/polka/issues/194 is fixed | ||
| // @ts-expect-error | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure you can bypass the ts-expect-errors by creating let server = certPath && certKeyPath
? (noHttp2 ? https.createServer({ ... }) : http2.createServer({ ... })
: http.createServer();
let app = polka({ server });
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw but it's invalid because there's only 1 Polka instance & 1 server needed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lukeed Not that it matters anymore but that's incorrect, you could have 2 servers, a plain HTTP one and an HTTPS one; hence the intention to reuse the Polka instance. |
||
| http.createServer(app.handler).listen({ path, host, port }, () => { | ||
| console.log(`Listening on http://${path ? path : host + ':' + port}`); | ||
| }); | ||
| } | ||
|
|
||
| export { server }; | ||
| if (certPath && certKeyPath) { | ||
| const cert = fs.readFileSync(certPath); | ||
| const key = fs.readFileSync(certKeyPath); | ||
| const https_server = noHttp2 | ||
| ? // @ts-expect-error | ||
| https.createServer({ cert, key }, app.handler) | ||
| : // @ts-expect-error | ||
| http2.createSecureServer({ allowHTTP1: true, cert, key }, app.handler); | ||
|
|
||
| https_server.listen({ path, host, port: httpsPort }, () => { | ||
| console.log(`Listening on https://${path ? path : host + ':' + httpsPort}`); | ||
| }); | ||
| } | ||
|
|
||
| export { app as server }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a slightly reworded version of this in 083af9a
@dummdidumm Also, one little question: I just noticed that variables like
export const cert_path = env('CERT_PATH', false);are actually exported, so according to the style guide they should be camelCase, am I right?Do these count as "external APIs"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they should be camel case here yes, good catch
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally I think the recommendation should be in the opposite direction. this seems better than a reverse proxy to me as I explained here: #10183 (comment)
perhaps we should just remove this section if we can't agree on a recommendation? or perhaps we can list pros and cons rather than just making a blanket statement?