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

allow customisation of HOST and PORT env vars in adapter-node #1754

Merged
merged 6 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-eggs-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

Allow the environment variables containing the host and port to serve on to be customised
10 changes: 8 additions & 2 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export default {
adapter: adapter({
// default options are shown
out: 'build',
precompress: false
precompress: false,
env: {
dominikg marked this conversation as resolved.
Show resolved Hide resolved
host: 'HOST',
port: 'PORT'
}
})
}
};
Expand All @@ -31,14 +35,16 @@ The directory to build the server to. It defaults to `build` — i.e. `node buil

Enables precompressing using gzip and brotli for assets and prerendered pages. It defaults to `false`.

## Environment variables
### env

By default, the server will accept connections on `0.0.0.0` using port 3000. These can be customised with the `PORT` and `HOST` environment variables:

```
HOST=127.0.0.1 PORT=4000 node build
```

However, which environment variables are used can also be customised by setting the `env` option. If specified, TKTKTK. It defaults to `{ host: 'HOST', port: 'PORT' }`.
Conduitry marked this conversation as resolved.
Show resolved Hide resolved

## License

[MIT](LICENSE)
4 changes: 4 additions & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
declare function plugin(options?: {
out?: string;
precompress?: boolean;
env?: {
host?: string;
port?: string;
};
}): import('@sveltejs/kit').Adapter;

export = plugin;
20 changes: 17 additions & 3 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, statSync, createReadStream, createWriteStream } from 'fs';
import { readFileSync, statSync, createReadStream, createWriteStream, writeFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
import { pipeline } from 'stream';
Expand All @@ -12,10 +12,18 @@ const pipe = promisify(pipeline);
/**
* @param {{
* out?: string;
* precompress?: boolean
* precompress?: boolean;
* env?: {
* host?: string;
* port?: string;
* };
* }} options
*/
export default function ({ out = 'build', precompress } = {}) {
export default function ({
out = 'build',
precompress,
env: { host: hostEnv = 'HOST', port: portEnv = 'PORT' } = {}
Conduitry marked this conversation as resolved.
Show resolved Hide resolved
} = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-node',
Expand All @@ -34,6 +42,12 @@ export default function ({ out = 'build', precompress } = {}) {
utils.log.minor('Building server');
const files = fileURLToPath(new URL('./files', import.meta.url));
utils.copy(files, '.svelte-kit/node');
writeFileSync(
join(files, 'env.js'),
`export const host = process.env[${JSON.stringify(
hostEnv
)}] || '0.0.0.0';\nexport const port = process.env[${JSON.stringify(portEnv)}] || 3000;`
);
await esbuild.build({
entryPoints: ['.svelte-kit/node/index.js'],
outfile: join(out, 'index.js'),
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export default {
sourcemap: true
},
plugins: [nodeResolve(), commonjs(), json()],
external: ['../output/server/app.js', ...require('module').builtinModules]
external: ['../output/server/app.js', './env.js', ...require('module').builtinModules]
};
7 changes: 3 additions & 4 deletions packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import './require_shim';
import { createServer } from './server';
// TODO hardcoding the relative location makes this brittle
import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { host, port } from './env.js'; // eslint-disable-line import/no-unresolved

const { HOST = '0.0.0.0', PORT = 3000 } = process.env;

const instance = createServer({ render }).listen(PORT, HOST, () => {
console.log(`Listening on port ${PORT}`);
const instance = createServer({ render }).listen(port, host, () => {
console.log(`Listening on port ${port}`);
});

export { instance };