-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logging for requests to miniflare
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Colorize, blue, bold, green, grey, red, yellow } from "kleur/colors"; | ||
import { Request } from "../../http"; | ||
|
||
export interface ResponseInfo { | ||
status: number; | ||
statusText: string; | ||
method: string; | ||
url: string; | ||
time: number; | ||
} | ||
|
||
export async function formatResponse(request: Request) { | ||
const info: ResponseInfo = JSON.parse(await request.text()); | ||
const url = require("node:url").parse(info.url); | ||
|
||
const responseTime = Date.now() - info.time; | ||
|
||
return [ | ||
`${bold(info.method)} ${url.pathname} `, | ||
colourFromHTTPStatus(info.status)( | ||
`${bold(info.status)} ${info.statusText} ` | ||
), | ||
grey(`(${responseTime}ms`), | ||
// Only include waitUntilTime if there were waitUntil promises | ||
grey(")"), | ||
].join(""); | ||
} | ||
|
||
function colourFromHTTPStatus(status: number): Colorize { | ||
if (200 <= status && status < 300) return green; | ||
if (400 <= status && status < 500) return yellow; | ||
if (500 <= status) return red; | ||
return blue; | ||
} |