Skip to content
Myr

chore(deno): denoify

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

authored by Yusuke Wada · committed by Yusuke Wada · parent 2e3919a · Browse files · 4 files+51 added−39 removed
This commit

State

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

Diff stats

4 files+51 added−39 removed

  • tests:+4 added−4 removed
  • other:+47 added−35 removed

4 files+51 added−39 removed

modified:deno_dist/middleware/logger/index.ts+22 added-19 removed
modified:deno_dist/context.ts+20 added-10 removed
9 unmodified lines
1011export interface Context<RequestParamKeyType extends string = string, E = Env> {12 req: Request<RequestParamKeyType>13 env: E14 event: FetchEvent | undefined15 executionCtx: ExecutionContext | undefined14 event: FetchEvent15 executionCtx: ExecutionContext16 finalized: boolean1718 get res(): Response19 set res(_res: Response)
16 unmodified lines
36 implements Context<RequestParamKeyType, E>37{38 req: Request<RequestParamKeyType>39 env: E40 event: FetchEvent | undefined41 executionCtx: ExecutionContext | undefined40 finalized: boolean4142 _status: StatusCode = 20043 private _executionCtx: FetchEvent | ExecutionContext | undefined44 private _pretty: boolean = false45 private _prettySpace: number = 246 private _map: Record<string, any> | undefined47 private _headers: Record<string, string> | undefined
2 unmodified lines
5051 constructor(52 req: Request,53 env: E | undefined = undefined,55 eventOrExecutionCtx: FetchEvent | ExecutionContext | undefined = undefined,54 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,55 notFoundHandler: NotFoundHandler = () => new Response()56 ) {57 this._executionCtx = executionCtx58 this.req = req59 this.env = env ? env : ({} as E)6061 if (eventOrExecutionCtx && 'respondWith' in eventOrExecutionCtx) {62 this.event = eventOrExecutionCtx61 this.notFoundHandler = notFoundHandler62 this.finalized = false63 }6465 get event(): FetchEvent {66 if (this._executionCtx instanceof FetchEvent) {67 return this._executionCtx68 } else {64 this.executionCtx = eventOrExecutionCtx69 throw Error('This context has no FetchEvent')70 }71 }7267 this.notFoundHandler = notFoundHandler68 this.finalized = false73 get executionCtx(): ExecutionContext {74 if (this._executionCtx) {75 return this._executionCtx76 } else {77 throw Error('This context has no ExecutionContext')78 }79 }8081 get res(): Response {82 return (this._res ||= new Response())
91 unmodified lines
modified:src/middleware/logger/index.test.ts+4 added-4 removed
modified:src/middleware/logger/index.ts+5 added-6 removed
2 unmodified lines
3import { getPathFromURL } from '../../utils/url'45enum LogPrefix {6 Outgoing = '-->',7 Incoming ='<--',7 Incoming = '<--',8 Error = 'xxx',9}1011const humanize = (times: string[]) => {12 const [delimiter, separator] = [',', '.']1314 const orderTimes = times.map(v => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter))15 14 const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter))1516 return orderTimes.join(separator)17}1819const time = (start: number) => {20 const delta = Date.now() - start21 return humanize([delta < 1000 ? delta + 'ms' : Math.round(delta / 1000) + 's'])22}232424const colorStatus = (status: number) => {25 const out: { [key: string]: string } = {26 7: `\x1b[35m${status}\x1b[0m`,27 5: `\x1b[31m${status}\x1b[0m`,
3 unmodified lines
31 1: `\x1b[32m${status}\x1b[0m`,32 0: `\x1b[33m${status}\x1b[0m`,33 }3436 const calculateStatus = (status / 100 | 0)35 const calculateStatus = (status / 100) | 03637 return out[calculateStatus]38}39
26 unmodified lines
66 await next()6768 log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start))69 }71}70}