summaryrefslogtreecommitdiff
path: root/src/hero.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/hero.mjs')
-rw-r--r--src/hero.mjs16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index 10fa06e..61621af 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -309,6 +309,22 @@ export const buildHttpPayload = (code, {
"\r\n\r\n" +
message;
+export const fallback404Handler = (_req, socket) =>
+ socket.end(buildHttpPayload(404)).destroySoon();
+
+export const fallback405Handler = (_req, socket) =>
+ socket.end(buildHttpPayload(405)).destroySoon();
+
+export const handlerForConnection = (table, method, path) => {
+ if (method !== WEBSOCKET_METHOD) {
+ return fallback405Handler;
+ }
+
+ const segments = pathToSegments(path);
+ const handlerFn = u.getIn(table, [WEBSOCKET_KEYWORD, WEBSOCKET_METHOD].concat(segments));
+ return handlerFn || fallback404Handler;
+};
+
export const makeRequestListener = table => async (req, res) => {
const { status, headers, body } = await handleRequest(table, req);
res.writeHead(status, headers);