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

make env vars a bit easier to understand/follow in adapter-node #4360

Merged
merged 15 commits into from
Mar 17, 2022
5 changes: 5 additions & 0 deletions .changeset/thirty-kings-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

[breaking] Replace options.env with options.environment
43 changes: 20 additions & 23 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,15 @@ export default {
// default options are shown
out: 'build',
precompress: false,
env: {
path: 'SOCKET_PATH',
host: 'HOST',
port: 'PORT',
origin: 'ORIGIN',
xffDepth: 'XFF_DEPTH',
headers: {
address: 'ADDRESS_HEADER',
protocol: 'PROTOCOL_HEADER',
host: 'HOST_HEADER'
}
environment: {
SOCKET_PATH: 'SOCKET_PATH',
HOST: 'HOST',
PORT: 'PORT',
ORIGIN: 'ORIGIN',
XFF_DEPTH: 'XFF_DEPTH',
ADDRESS_HEADER: 'ADDRESS_HEADER',
PROTOCOL_HEADER: 'PROTOCOL_HEADER',
HOST_HEADER: 'HOST_HEADER'
}
})
}
Expand All @@ -114,22 +112,21 @@ If you need to change the name of the environment variables used to configure th

```js
env: {
host: 'MY_HOST_VARIABLE',
port: 'MY_PORT_VARIABLE',
origin: 'MY_ORIGINURL',
xffDepth: 'MY_XFF_DEPTH',
headers: {
address: 'MY_ADDRESS_HEADER',
protocol: 'MY_PROTOCOL_HEADER',
host: 'MY_HOST_HEADER'
}
SOCKET_PATH: 'MY_CUSTOM_SOCKET_PATH',
HOST: 'MY_CUSTOM_HOST',
PORT: 'MY_CUSTOM_PORT',
ORIGIN: 'MY_CUSTOM_ORIGIN',
XFF_DEPTH: 'MY_CUSTOM_XFF_DEPTH',
ADDRESS_HEADER: 'MY_CUSTOM_ADDRESS_HEADER',
PROTOCOL_HEADER: 'MY_CUSTOM_PROTOCOL_HEADER',
HOST_HEADER: 'MY_CUSTOM_HOST_HEADER'
}
```

```
MY_HOST_VARIABLE=127.0.0.1 \
MY_PORT_VARIABLE=4000 \
MY_ORIGINURL=https://my.site \
MY_CUSTOM_HOST_VARIABLE=127.0.0.1 \
MY_CUSTOM_PORT_VARIABLE=4000 \
MY_CUSTOM_ORIGINURL=https://my.site \
node build
```

Expand Down
31 changes: 17 additions & 14 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { Adapter } from '@sveltejs/kit';

declare global {
const HOST_ENV: string;
const PATH_ENV: string;
const PORT_ENV: string;
const SOCKET_PATH: string;
const HOST: string;
const PORT: string;
const ORIGIN: string;
const XFF_DEPTH: string;
const ADDRESS_HEADER: string;
const PROTOCOL_HEADER: string;
const HOST_HEADER: string;
}

interface AdapterOptions {
out?: string;
precompress?: boolean;
env?: {
path?: string;
host?: string;
port?: string;
origin?: string;
xffDepth?: string;
headers?: {
address?: string;
protocol?: string;
host?: string;
};
environment?: {
SOCKET_PATH?: string;
HOST?: string;
PORT?: string;
ORIGIN?: string;
XFF_DEPTH?: string;
ADDRESS_HEADER?: string;
PROTOCOL_HEADER?: string;
HOST_HEADER?: string;
};
}

Expand Down
54 changes: 31 additions & 23 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,30 @@ const pipe = promisify(pipeline);
const files = fileURLToPath(new URL('./files', import.meta.url).href);

/** @type {import('.')} */
export default function ({
out = 'build',
precompress,
env: {
path: path_env = 'SOCKET_PATH',
host: host_env = 'HOST',
port: port_env = 'PORT',
origin: origin_env = 'ORIGIN',
xffDepth: xff_depth_env = 'XFF_DEPTH',
headers: {
address: address_header_env = 'ADDRESS_HEADER',
protocol: protocol_header_env = 'PROTOCOL_HEADER',
host: host_header_env = 'HOST_HEADER'
export default function (opts = {}) {
// TODO remove for 1.0
// @ts-expect-error
if (opts.env) {
throw new Error(
`options.env has been removed in favour of options.environment, which has different properties. Consult the adapter-node README: /~https://github.com/sveltejs/kit/tree/master/packages/adapter-node`
);
}

const {
out = 'build',
precompress,
environment: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would something like envAliases make sense as a name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like to be properly accurate it would need to be envVarAliases which seems like a step too far

SOCKET_PATH = 'SOCKET_PATH',
HOST = 'HOST',
PORT = 'PORT',
ORIGIN = 'ORIGIN',
XFF_DEPTH = 'XFF_DEPTH',
ADDRESS_HEADER = 'ADDRESS_HEADER',
PROTOCOL_HEADER = 'PROTOCOL_HEADER',
HOST_HEADER = 'HOST_HEADER'
} = {}
} = {}
} = {}) {
} = opts;

return {
name: '@sveltejs/adapter-node',

Expand All @@ -49,14 +57,14 @@ export default function ({
replace: {
SERVER: './server/index.js',
MANIFEST: './manifest.js',
PATH_ENV: JSON.stringify(path_env),
HOST_ENV: JSON.stringify(host_env),
PORT_ENV: JSON.stringify(port_env),
ORIGIN: origin_env ? `process.env[${JSON.stringify(origin_env)}]` : 'undefined',
XFF_DEPTH_ENV: xff_depth_env,
PROTOCOL_HEADER: JSON.stringify(protocol_header_env),
HOST_HEADER: JSON.stringify(host_header_env),
ADDRESS_HEADER: JSON.stringify(address_header_env)
SOCKET_PATH: JSON.stringify(SOCKET_PATH),
HOST: JSON.stringify(HOST),
PORT: JSON.stringify(PORT),
ORIGIN: JSON.stringify(ORIGIN),
XFF_DEPTH: JSON.stringify(XFF_DEPTH),
PROTOCOL_HEADER: JSON.stringify(PROTOCOL_HEADER),
HOST_HEADER: JSON.stringify(HOST_HEADER),
ADDRESS_HEADER: JSON.stringify(ADDRESS_HEADER)
}
});

Expand Down
11 changes: 0 additions & 11 deletions packages/adapter-node/src/handler.d.ts

This file was deleted.

14 changes: 7 additions & 7 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { getRequest, setResponse } from '@sveltejs/kit/node';
import { Server } from 'SERVER';
import { manifest } from 'MANIFEST';

/* global ORIGIN, ADDRESS_HEADER, PROTOCOL_HEADER, HOST_HEADER, XFF_DEPTH_ENV */
/* global ORIGIN, ADDRESS_HEADER, PROTOCOL_HEADER, HOST_HEADER, XFF_DEPTH */

const server = new Server(manifest);
const origin = ORIGIN;
const xff_depth = XFF_DEPTH_ENV ? parseInt(process.env[XFF_DEPTH_ENV]) : 1;
const origin = process.env[ORIGIN] || undefined;
const xff_depth = XFF_DEPTH ? parseInt(process.env[XFF_DEPTH]) : 1;

const address_header = ADDRESS_HEADER && (process.env[ADDRESS_HEADER] || '').toLowerCase();
const protocol_header = PROTOCOL_HEADER && process.env[PROTOCOL_HEADER];
const host_header = (HOST_HEADER && process.env[HOST_HEADER]) || 'host';
const protocol_header = PROTOCOL_HEADER && (process.env[PROTOCOL_HEADER] || '').toLowerCase();
const host_header = (HOST_HEADER && (process.env[HOST_HEADER] || '').toLowerCase()) || 'host';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -66,12 +66,12 @@ const ssr = async (req, res) => {
const addresses = value.split(',');

if (xff_depth < 1) {
throw new Error(`${XFF_DEPTH_ENV} must be a positive integer`);
throw new Error(`${XFF_DEPTH} must be a positive integer`);
}

if (xff_depth > addresses.length) {
throw new Error(
`${XFF_DEPTH_ENV} is ${xff_depth}, but only found ${addresses.length} addresses`
`${XFF_DEPTH} is ${xff_depth}, but only found ${addresses.length} addresses`
);
}
return addresses[addresses.length - xff_depth].trim();
Expand Down
8 changes: 4 additions & 4 deletions packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { handler } from './handler.js';
import compression from 'compression';
import polka from 'polka';

/* global PATH_ENV, HOST_ENV, PORT_ENV */
/* global SOCKET_PATH, HOST, PORT */

export const path = process.env[PATH_ENV] || false;
export const host = process.env[HOST_ENV] || '0.0.0.0';
export const port = process.env[PORT_ENV] || (!path && '3000');
export const path = process.env[SOCKET_PATH] || false;
export const host = process.env[HOST] || '0.0.0.0';
export const port = process.env[PORT] || (!path && '3000');

const server = polka().use(
// /~https://github.com/lukeed/polka/issues/173
Expand Down
11 changes: 0 additions & 11 deletions packages/adapter-node/src/types.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/adapter-node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"@sveltejs/kit": ["../kit/types/index"]
}
},
"include": ["index.js", "src/**/*.js", "ambient.d.ts"]
"include": ["index.js", "src/**/*.js", "ambient.d.ts", "src/types.d.ts"]
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
}