Skip to content
Myr

perf(middleware/logger): optimize color status (#3603)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor(logger): optimize colors status * refactor(logger): add failing test * refactor(logger): remove unreachable cases. Response accepts 200-599 * refactor(logger): added fallback * perf(middleware/logger): cleanup tests * perf(middleware/logger): test unsupported status code forcibly

authored by TATSUNO “Taz” Yasuhiro · committed by GitHub · parent 9f21a95 · Browse files · 2 files+58 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+58 added−12 removed

  • tests:+43 added−0 removed
  • other:+15 added−12 removed

2 files+58 added−12 removed

modified:src/middleware/logger/index.test.ts+43 added
modified:src/middleware/logger/index.ts+15 added-12 removed
26 unmodified lines
27}2829const colorStatus = (status: number) => {30 const colorEnabled = getColorEnabled()31 const out: { [key: string]: string } = {32 7: colorEnabled ? `\x1b[35m${status}\x1b[0m` : `${status}`,33 5: colorEnabled ? `\x1b[31m${status}\x1b[0m` : `${status}`,34 4: colorEnabled ? `\x1b[33m${status}\x1b[0m` : `${status}`,35 3: colorEnabled ? `\x1b[36m${status}\x1b[0m` : `${status}`,36 2: colorEnabled ? `\x1b[32m${status}\x1b[0m` : `${status}`,37 1: colorEnabled ? `\x1b[32m${status}\x1b[0m` : `${status}`,38 0: colorEnabled ? `\x1b[33m${status}\x1b[0m` : `${status}`,31 if (colorEnabled) {32 switch ((status / 100) | 0) {33 case 5: // red = error34 return `\x1b[31m${status}\x1b[0m`35 case 4: // yellow = warning36 return `\x1b[33m${status}\x1b[0m`37 case 3: // cyan = redirect38 return `\x1b[36m${status}\x1b[0m`39 case 2: // green = success40 return `\x1b[32m${status}\x1b[0m`41 }42 }4041 const calculateStatus = (status / 100) | 04243 return out[calculateStatus]43 // Fallback to unsupported status code.44 // E.g.) Bun and Deno supports new Response with 101, but Node.js does not.45 // And those may evolve to accept more status.46 return `${status}`47}4849type PrintFunc = (str: string, ...rest: string[]) => void50
46 unmodified lines