From 631ad28b1f7bd46a18fa477eec09af7d4746573f Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 15 Mar 2024 12:51:58 -0300 Subject: src/hero.mjs: Add buildHttpPayload() --- src/hero.mjs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') 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); -- cgit v1.2.3