From c653aec2555cd542dc98a15b3138dee4eedf3d80 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Thu, 20 Jan 2022 08:56:28 -0500 Subject: [PATCH] [fix] rename `BASE` to `ORIGIN` and fix config handling (#3423) --- .changeset/perfect-socks-sparkle.md | 5 +++++ packages/adapter-node/README.md | 16 ++++++++-------- packages/adapter-node/index.d.ts | 2 +- packages/adapter-node/index.js | 6 +++--- packages/adapter-node/src/handler.js | 8 ++++---- packages/adapter-node/src/types.d.ts | 2 +- 6 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 .changeset/perfect-socks-sparkle.md diff --git a/.changeset/perfect-socks-sparkle.md b/.changeset/perfect-socks-sparkle.md new file mode 100644 index 000000000000..0edd43abd3bb --- /dev/null +++ b/.changeset/perfect-socks-sparkle.md @@ -0,0 +1,5 @@ +--- +"@fake-scope/fake-pkg": patch +--- + +[fix] rename `BASE` to `ORIGIN` and fix config handling diff --git a/packages/adapter-node/README.md b/packages/adapter-node/README.md index a343608b9a20..7e7bfb47a827 100644 --- a/packages/adapter-node/README.md +++ b/packages/adapter-node/README.md @@ -20,10 +20,10 @@ export default { path: 'SOCKET_PATH', host: 'HOST', port: 'PORT', - base: undefined, + origin: 'ORIGIN', headers: { - protocol: undefined, - host: 'host' + protocol: 'PROTOCOL_HEADER', + host: 'HOST_HEADER' } } }) @@ -49,13 +49,13 @@ By default, the server will accept connections on `0.0.0.0` using port 3000. The HOST=127.0.0.1 PORT=4000 node build ``` -HTTP doesn't give SvelteKit a reliable way to know the URL that is currently being requested. The simplest way to tell SvelteKit where the app is being served is to set the `BASE` environment variable: +HTTP doesn't give SvelteKit a reliable way to know the URL that is currently being requested. The simplest way to tell SvelteKit where the app is being served is to set the `ORIGIN` environment variable: ``` -BASE=https://my.site node build +ORIGIN=https://my.site node build ``` -With this, a request for the `/stuff` pathname will correctly resolve to `https://my.site/stuff`. Alternatively, you can specify headers that tell SvelteKit about the request protocol and host, from which it can construct the base URL: +With this, a request for the `/stuff` pathname will correctly resolve to `https://my.site/stuff`. Alternatively, you can specify headers that tell SvelteKit about the request protocol and host, from which it can construct the origin URL: ``` PROTOCOL_HEADER=x-forwarded-proto HOST_HEADER=x-forwarded-host node build @@ -69,7 +69,7 @@ All of these environment variables can be changed, if necessary, using the `env` env: { host: 'MY_HOST_VARIABLE', port: 'MY_PORT_VARIABLE', - base: 'MY_BASEURL', + origin: 'MY_ORIGINURL', headers: { protocol: 'MY_PROTOCOL_HEADER', host: 'MY_HOST_HEADER' @@ -80,7 +80,7 @@ env: { ``` MY_HOST_VARIABLE=127.0.0.1 \ MY_PORT_VARIABLE=4000 \ -MY_BASEURL=https://my.site \ +MY_ORIGINURL=https://my.site \ node build ``` diff --git a/packages/adapter-node/index.d.ts b/packages/adapter-node/index.d.ts index 7564c68954d4..e7169b54b799 100644 --- a/packages/adapter-node/index.d.ts +++ b/packages/adapter-node/index.d.ts @@ -7,7 +7,7 @@ interface AdapterOptions { path?: string; host?: string; port?: string; - base?: string; + origin?: string; headers?: { protocol?: string; host?: string; diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index af1c59dd5127..785c4ea0d137 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -17,11 +17,11 @@ export default function ({ path: path_env = 'SOCKET_PATH', host: host_env = 'HOST', port: port_env = 'PORT', - base: base_env, + origin: origin_env = 'ORIGIN', headers: { protocol: protocol_header_env = 'PROTOCOL_HEADER', host: host_header_env = 'HOST_HEADER' - } + } = {} } = {} } = {}) { return { @@ -54,7 +54,7 @@ export default function ({ PATH_ENV: JSON.stringify(path_env), HOST_ENV: JSON.stringify(host_env), PORT_ENV: JSON.stringify(port_env), - BASE: base_env ? `process.env[${JSON.stringify(base_env)}]` : 'undefined', + ORIGIN_ENV: origin_env ? `process.env[${JSON.stringify(origin_env)}]` : 'undefined', PROTOCOL_HEADER: JSON.stringify(protocol_header_env), HOST_HEADER: JSON.stringify(host_header_env) } diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 33c5529ba772..82dd3b746cf5 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -7,10 +7,10 @@ import { getRequest, setResponse } from '@sveltejs/kit/node'; import { App } from 'APP'; import { manifest } from 'MANIFEST'; -/* global BASE_ENV, PROTOCOL_HEADER, HOST_HEADER */ +/* global ORIGIN_ENV, PROTOCOL_HEADER, HOST_HEADER */ const app = new App(manifest); -const base = BASE_ENV && process.env[BASE_ENV]; +const origin = ORIGIN_ENV && process.env[ORIGIN_ENV]; const protocol_header = PROTOCOL_HEADER && process.env[PROTOCOL_HEADER]; const host_header = (HOST_HEADER && process.env[HOST_HEADER]) || 'host'; @@ -39,7 +39,7 @@ const ssr = async (req, res) => { let request; try { - request = await getRequest(base || get_base(req.headers), req); + request = await getRequest(origin || get_origin(req.headers), req); } catch (err) { res.statusCode = err.status || 400; return res.end(err.reason || 'Invalid request body'); @@ -68,7 +68,7 @@ function sequence(handlers) { * @param {import('http').IncomingHttpHeaders} headers * @returns */ -function get_base(headers) { +function get_origin(headers) { const protocol = (protocol_header && headers[protocol_header]) || 'https'; const host = headers[host_header]; return `${protocol}://${host}`; diff --git a/packages/adapter-node/src/types.d.ts b/packages/adapter-node/src/types.d.ts index 66ab40e7a8cf..b3230761ef2b 100644 --- a/packages/adapter-node/src/types.d.ts +++ b/packages/adapter-node/src/types.d.ts @@ -2,7 +2,7 @@ declare global { const PATH_ENV: string; const HOST_ENV: string; const PORT_ENV: string; - const BASE_ENV: string; + const ORIGIN_ENV: string; const PROTOCOL_HEADER: string; const HOST_HEADER: string;