-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow node adapter to be configured to have node server listen to soc…
…ket path instead of host and port
- Loading branch information
Showing
4 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |