summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-15 12:51:58 -0300
committerEuAndreh <eu@euandre.org>2024-03-15 12:51:58 -0300
commit631ad28b1f7bd46a18fa477eec09af7d4746573f (patch)
treed18661c77470cb37cc00cd67bfe4a513ee6dbbb4 /src
parentsrc/hero.mjs: Add emitHeaders() (diff)
downloadpapod-631ad28b1f7bd46a18fa477eec09af7d4746573f.tar.gz
papod-631ad28b1f7bd46a18fa477eec09af7d4746573f.tar.xz
src/hero.mjs: Add buildHttpPayload()
Diffstat (limited to 'src')
-rw-r--r--src/hero.mjs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index a9397ef..daf8141 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -285,6 +285,25 @@ export const emitHeaders = obj =>
.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 makeRequestListener = table => async (req, res) => {
const { status, headers, body } = await handleRequest(table, req);
res.writeHead(status, headers);