diff options
Diffstat (limited to 'src/hero.mjs')
-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); |