diff options
author | EuAndreh <eu@euandre.org> | 2024-03-15 12:51:58 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-03-15 12:51:58 -0300 |
commit | 631ad28b1f7bd46a18fa477eec09af7d4746573f (patch) | |
tree | d18661c77470cb37cc00cd67bfe4a513ee6dbbb4 /src | |
parent | src/hero.mjs: Add emitHeaders() (diff) | |
download | papod-631ad28b1f7bd46a18fa477eec09af7d4746573f.tar.gz papod-631ad28b1f7bd46a18fa477eec09af7d4746573f.tar.xz |
src/hero.mjs: Add buildHttpPayload()
Diffstat (limited to 'src')
-rw-r--r-- | src/hero.mjs | 19 |
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); |