Skip to content

Commit

Permalink
[fix] rename BASE to ORIGIN and fix config handling (#3423)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry authored Jan 20, 2022
1 parent 192a8fe commit c653aec
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-socks-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fake-scope/fake-pkg": patch
---

[fix] rename `BASE` to `ORIGIN` and fix config handling
16 changes: 8 additions & 8 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
})
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface AdapterOptions {
path?: string;
host?: string;
port?: string;
base?: string;
origin?: string;
headers?: {
protocol?: string;
host?: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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}`;
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c653aec

Please sign in to comment.