Skip to content

Commit

Permalink
Allow node adapter to be configured to have node server listen to soc…
Browse files Browse the repository at this point in the history
…ket path instead of host and port
  • Loading branch information
matths committed Jul 31, 2021
1 parent 3e30a1b commit f798275
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
7 changes: 5 additions & 2 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const pipe = promisify(pipeline);
* out?: string;
* precompress?: boolean;
* env?: {
* path?: string;
* host?: string;
* port?: string;
* };
Expand All @@ -34,7 +35,7 @@ const pipe = promisify(pipeline);
export default function ({
out = 'build',
precompress,
env: { host: host_env = 'HOST', port: port_env = 'PORT' } = {},
env: { path: path_env = 'SOCKET_PATH', host: host_env = 'HOST', port: port_env = 'PORT' } = {},
esbuild: esbuildConfig
} = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
Expand All @@ -58,7 +59,9 @@ export default function ({
utils.copy(files, '.svelte-kit/node');
writeFileSync(
'.svelte-kit/node/env.js',
`export const host = process.env[${JSON.stringify(
`export const path = process.env[${JSON.stringify(
path_env
)}] || false;\nexport const host = process.env[${JSON.stringify(
host_env
)}] || '0.0.0.0';\nexport const port = process.env[${JSON.stringify(port_env)}] || 3000;`
);
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@sveltejs/kit": "workspace:*",
"c8": "^7.7.2",
"compression": "^1.7.4",
"http-proxy": "^1.18.1",
"node-fetch": "^3.0.0-beta.9",
"polka": "^1.0.0-next.15",
"rollup": "^2.55.0",
Expand Down
16 changes: 12 additions & 4 deletions packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { host, port } from './env.js'; // eslint-disable-line import/no-unresolved
import { path, host, port } from './env.js'; // eslint-disable-line import/no-unresolved
import { createServer } from './server';

init();

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

if (path) {
instance.listen(path, () => {
console.log(`Listening on ${path}`);
});
} else {
instance.listen(port, host, () => {
console.log(`Listening on ${host}:${port}`);
});
}

export { instance };
77 changes: 77 additions & 0 deletions packages/adapter-node/tests/smoke_path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { test } from 'uvu';
import { createServer } from '../src/server.js';
import * as assert from 'uvu/assert';
import fetch from 'node-fetch';
import httpProxy from 'http-proxy';
import fs from 'fs';

const PROXY_PORT = 9090;
const { SOCKET_PATH = '/tmp/socket' } = process.env;
const DEFAULT_SERVER_OPTS = { render: () => {} };

function cleanupSocketFile() {
if (fs.existsSync(SOCKET_PATH)) {
fs.unlinkSync(SOCKET_PATH);
}
}

function createProxy() {
const proxy = httpProxy.createProxyServer({
target: {
socketPath: SOCKET_PATH
},
xfwd: true,
secure: false
});
proxy.listen(PROXY_PORT);
return proxy;
}

function startServer(opts = DEFAULT_SERVER_OPTS) {
const server = createServer(opts);
return new Promise((fulfil, reject) => {
server.listen(SOCKET_PATH, (err) => {
if (err) {
reject(err);
}
fulfil(server);
});
});
}

test('starts a server listening on a path (domain socket file)', async () => {
const server = await startServer();
assert.ok('server started');
server.server.close();
cleanupSocketFile();
});

test('serves a 404 via path', async () => {
const server = await startServer();
const proxy = createProxy();
const res = await fetch(`http://localhost:${PROXY_PORT}/nothing`);
assert.equal(res.status, 404);
server.server.close();
proxy.close();
cleanupSocketFile();
});

test('responses with the rendered status code via path', async () => {
const server = await startServer({
render: () => {
return {
headers: 'wow',
status: 203,
body: 'ok'
};
}
});
const proxy = createProxy();
const res = await fetch(`http://localhost:${PROXY_PORT}/wow`);
assert.equal(res.status, 203);
server.server.close();
proxy.close();
cleanupSocketFile();
});

test.run();

0 comments on commit f798275

Please sign in to comment.