diff options
author | EuAndreh <eu@euandre.org> | 2024-03-18 07:31:13 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-03-18 07:31:13 -0300 |
commit | 48e1e933be9c14130c50bb90956187119781b14b (patch) | |
tree | 1550b823b1757dd15197f10bea1a97a583c71ef5 /src | |
parent | src/hero.mjs: Log to stdout instead of stderr (diff) | |
download | papod-48e1e933be9c14130c50bb90956187119781b14b.tar.gz papod-48e1e933be9c14130c50bb90956187119781b14b.tar.xz |
src/hero.mjs: Add statusMessage() and statusResponse()
Diffstat (limited to 'src')
-rw-r--r-- | src/hero.mjs | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/src/hero.mjs b/src/hero.mjs index c36da68..21bc4ba 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -45,6 +45,14 @@ export const makeLogger = ({ export const log = makeLogger(); +export const statusMessage = code => + `${http.STATUS_CODES[code]}\n`; + +export const statusResponse = code => ({ + status: code, + body: statusMessage(code), +}); + export const isValidMethod = method => method === "GET"; @@ -69,9 +77,7 @@ export const validateUpgrade = (method, headers) => { /// before the request gets here. return { isValid: false, - response: { - status: 405, - }, + response: statusResponse(405), }; } @@ -184,9 +190,9 @@ export const interceptorsFn = ({ const { status, body, headers } = response; assert.equal(typeof status, "number"); const mappings = { - string: () => [ "text/html", body ], - undefined: () => [ "text/plain", http.STATUS_CODES[status] + "\n" ], - FALLBACK: () => [ "application/json", JSON.stringify(body) ], + string: () => [ "text/html", body ], + undefined: () => [ "text/plain", statusMessage(status) ], + FALLBACK: () => [ "application/json", JSON.stringify(body) ], }; const type = typeof body; assert.notEqual(type, "FALLBACK"); @@ -214,10 +220,7 @@ export const interceptorsFn = ({ message: error.message, stacktrace: error.stack }); - return { - status: 500, - body: "Internal Server Error\n", - }; + return statusResponse(500); } }, websocketHandshake: async (req, next) => { @@ -407,14 +410,9 @@ export const buildHeader = (status, headers) => export const writeHead = (socket, status, headers) => socket.write(buildHeader(status, headers)); -export const handle404 = _req => ({ - status: 404, - body: "Not Found\n", -}); - export const make404Handler = interceptors => ({ params: {}, - handlerFn: wrapHandler(handle404, interceptors), + handlerFn: wrapHandler(_ => statusResponse(404), interceptors), }); export const handleRequest = async (table, reqHandle) => { |