summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-16 06:36:05 -0300
committerEuAndreh <eu@euandre.org>2024-03-16 06:36:05 -0300
commitcac533d1cc73ccd2659beb87c146254ed2887c9b (patch)
tree72dec38a2f65c0814ed14fb401c97dbebe54df53
parentsrc/hero.mjs: findHandler(): learn how to find upgrade routes (diff)
downloadpapod-cac533d1cc73ccd2659beb87c146254ed2887c9b.tar.gz
papod-cac533d1cc73ccd2659beb87c146254ed2887c9b.tar.xz
src/hero.mjs: Remove current makeUpgradeListener() and its helpers
-rw-r--r--src/hero.mjs61
-rw-r--r--tests/js/hero.mjs172
2 files changed, 0 insertions, 233 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index 7483a21..b47d459 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -415,67 +415,6 @@ export const handleRequest = async (table, reqHandle) => {
return await handlerFn(request);
};
-export const emitHeaders = obj =>
- Object.keys(obj)
- .sort(u.strSortFn)
- .map(name => `${name}: ${obj[name]}`)
- .join("\r\n");
-
-const STATUS_CODES = {
- 404: "Not Found",
- 405: "Method Not Allowed",
-};
-
-export const buildHttpPayload = (code, {
- headers = {},
- message = `${STATUS_CODES[code]}\n`,
-} = {}) =>
- `HTTP/1.1 ${code} ${STATUS_CODES[code]}\r\n` +
- emitHeaders({
- "Connection": "close",
- "Content-Type": "text/plain; charset=UTF-8",
- "Content-Length": Buffer.byteLength(message),
- ...headers,
- }) +
- "\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 makeUpgradeListener = table => async (req, socket, _head) => {
- const { method, url, headers } = req;
- const [ path, queryParams ] = url.split("?");
- const handlerFn = handlerForConnection(table, method, path);
-
- const request = {
- params: {
- path: {},
- query: extractQueryParams(queryParams),
- },
- method,
- path,
- headers,
- handler: handlerFn,
- ref: req,
- };
-
- return await handlerFn(request, socket);
-};
-
export const makeRequestListener = table => async (req, res) => {
const { status, headers, body } = await handleRequest(table, {
...req,
diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs
index 7a5738d..468f4a8 100644
--- a/tests/js/hero.mjs
+++ b/tests/js/hero.mjs
@@ -36,12 +36,6 @@ import {
handle404,
make404Handler,
handleRequest,
- emitHeaders,
- buildHttpPayload,
- fallback404Handler,
- fallback405Handler,
- handlerForConnection,
- makeUpgradeListener,
makeRequestListener,
actionsFn,
lineHandlerFn,
@@ -1456,168 +1450,6 @@ const test_handleRequest = async t => {
});
};
-const test_emitHeaders = async t => {
- t.start("emitHeaders()");
-
- await t.test("empty values", () => {
- assert.equal(emitHeaders({}), "");
- assert.equal(emitHeaders({ "": "" }), ": ");
- assert.equal(emitHeaders({ " ": " " }), " : ");
- assert.equal(emitHeaders({ "_": "_" }), "_: _");
- });
-
- await t.test("newlines are forwarded", () => {
- assert.equal(emitHeaders({"\na\n": "\nb\n"}), "\na\n: \nb\n");
- });
-
- await t.test("keys are always sorted", () => {
- assert.equal(
- emitHeaders({ "a": "one", "Z": "two" }),
- "a: one\r\nZ: two",
- );
- });
-};
-
-const test_buildHttpPayload = async t => {
- t.start("buildHttpPayload()");
-
- await t.test("empty values", () => {
- assert.equal(
- buildHttpPayload(404),
- "HTTP/1.1 404 Not Found\r\n" +
- "Connection: close\r\n" +
- "Content-Length: 10\r\n" +
- "Content-Type: text/plain; charset=UTF-8\r\n" +
- "\r\n" +
- "Not Found\n",
- );
- assert.equal(
- buildHttpPayload(405),
- "HTTP/1.1 405 Method Not Allowed\r\n" +
- "Connection: close\r\n" +
- "Content-Length: 19\r\n" +
- "Content-Type: text/plain; charset=UTF-8\r\n" +
- "\r\n" +
- "Method Not Allowed\n",
- );
- });
-
- await t.test("we can add headers and customise the message", () => {
- assert.equal(
- buildHttpPayload(404, {
- headers: {
- "X-Something": "something",
- "Aaaa": "ZzZz",
- "Content-Type": "text/plain",
- },
- message: "the message\n"
- }),
- "HTTP/1.1 404 Not Found\r\n" +
- "Aaaa: ZzZz\r\n" +
- "Connection: close\r\n" +
- "Content-Length: 12\r\n" +
- "Content-Type: text/plain\r\n" +
- "X-Something: something\r\n" +
- "\r\n" +
- "the message\n",
- );
- });
-};
-
-export const test_handlerForConnection = async t => {
- t.start("handlerForConnection()");
-
- await t.test("405 handler no matter the table when not GET method", () => {
- assert.equal(
- handlerForConnection(null, "POST", null),
- fallback405Handler,
- );
- });
-
- await t.test("404 handler when there is no route", () => {
- assert.equal(
- handlerForConnection({}, "GET", "/the/websocket"),
- fallback404Handler,
- );
- });
-
- await t.test("the declared handler when it exists", () => {
- const fn1 = () => {};
- assert.equal(
- handlerForConnection({
- websocket: {
- GET: {
- known: {
- path: {
- "": fn1,
- },
- },
- },
- },
- }, "GET", "/known/path"),
- fn1,
- );
- });
-};
-
-const test_makeUpgradeListener = async t => {
- t.start("makeUpgradeListener()");
-
- await t.test("straightforward connection stablishing", async () => {
- const calls = [];
- const fn = (_req, socket) => calls.push(socket);
- const routes = [[ "WEBSOCKET", "/sock", fn ]];
- const table = buildRoutes(routes);
- const upgradeListener = makeUpgradeListener(table);
-
- const req = {
- method: "GET",
- url: "/sock",
- };
- const socket = "the socket"
- await upgradeListener(req, socket);
- assert.deepEqual(calls, ["the socket"]);
- });
-
- await t.test("early termination cases", async () => {
- const calls = [];
- const fn = (req, socket) => calls.push(socket);
- const routes = [[ "WEBSOCKET", "/another", fn ]];
- const table = buildRoutes(routes);
- const upgradeListener = makeUpgradeListener(table);
-
- const req405 = {
- method: "PUT",
- url: "/unused",
- };
- let destroyed405 = false
- const socket405 = {
- end: s => {
- assert.equal(s, buildHttpPayload(405));
- return socket405;
- },
- destroySoon: () => destroyed405 = true,
- };
- await upgradeListener(req405, socket405);
- assert.ok(destroyed405);
-
- const req404 = {
- method: "GET",
- url: "/bad/path",
- };
- let destroyed404 = false;
- const socket404 = {
- end: s => {
- assert.equal(s, buildHttpPayload(404));
- return socket404;
- },
- destroySoon: () => destroyed404 = true,
- };
- await upgradeListener(req404, socket404);
- assert.ok(destroyed404);
- });
-};
-
const test_makeRequestListener = async t => {
t.start("makeRequestListener()");
@@ -2316,10 +2148,6 @@ await runner.runTests([
test_extractQueryParams,
test_make404Handler,
test_handleRequest,
- test_emitHeaders,
- test_buildHttpPayload,
- test_handlerForConnection,
- test_makeUpgradeListener,
test_makeRequestListener,
test_actionsFn,
test_lineHandlerFn,