perf(context): add HonoResponse for better performance (#281)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 8bbb495 · Browse files · 3 files·+62 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+62 added−7 removed
- other:+62 added−7 removed
Provenance
- Commit
18be41f- Tree
e0fbab2- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
8bbb495
3 files changed+62 added−7 removed
modified:src/context.ts+3 added-2 removed
1234518 unmodified lines242526272728293031323 unmodified lines36373839394041424344126 unmodified linesExpand allimport { HonoResponse } from './response'import type { StatusCode } from './utils/http-status'import { isAbsoluteURL } from './utils/url'
type Headers = Record<string, string>18 unmodified lines notFound: () => Response | Promise<Response>
constructor( req: Request<RequestParamKeyType>, opts: { env?: Env; event?: FetchEvent; res?: Response } = { opts: { env?: Env; event?: FetchEvent; res?: Response | HonoResponse } = { env: {}, event: undefined, res: undefined, }3 unmodified lines
Object.assign(this, opts)
if (!this.res) { const res = new Response(null, { status: 404 }) const res = new HonoResponse(null, { status: 404 }) res._finalized = false this.res = res } }126 unmodified linesExpand allmodified:src/hono.ts+3 added-5 removed
1 import { compose } from './compose'2 import { Context } from './context'3 import type { Env } from './context'4 import type { Result, Router } from './router'5-import { METHOD_NAME_ALL } from './router'6-import { METHOD_NAME_ALL_LOWERCASE } from './router'5+import { METHOD_NAME_ALL, METHOD_NAME_ALL_LOWERCASE } from './router'6 import { TrieRouter } from './router/trie-router' // Default Router7 import { getPathFromURL, mergePath } from './utils/url'8 9 declare global {74 unmodified lines84 85 export class Hono<E = Env, P extends string = '/'> extends defineDynamicClass()<E, P, Hono<E, P>> {86 readonly router: Router<Handler<string, E>> = new TrieRouter()87 readonly strict: boolean = true // strict routing - default is true89- private _tempPath: string88+ private _tempPath: string = ''89 private path: string = '/'90 91 routes: Route<E>[] = []92 20 unmodified lines113 }114 })115 116 Object.assign(this, init)118-119- this._tempPath = ''117 }118 119 private notFoundHandler: NotFoundHandler = (c: Context) => {120 const message = '404 Not Found'74 unmodified lines195 }196 }197 return null198 }) as typeof request.param199+200 const handlers = result ? result.handlers : [this.notFoundHandler]201 202 const c = new Context<string, E>(request, {203 env: env,39 unmodified linesadded:src/response.ts+56 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556/* This Response object is for better performance. This object is used in a Context before the Handler dispatches.*/
import type { Data } from './context'const errorMessage = 'Response is not finalized'
export class HonoResponse implements Response { headers: Headers ok: boolean redirected: boolean status: number statusText: string type: ResponseType url: string _finalized: boolean
constructor(_data: Data | null, init: ResponseInit) { this.headers = new Headers(init.headers) this.status = init.status ?? 404 this._finalized = false }
clone(): Response { throw new Error(errorMessage) }
body: ReadableStream<Uint8Array> | null bodyUsed: boolean arrayBuffer(): Promise<ArrayBuffer> arrayBuffer(): Promise<ArrayBuffer> arrayBuffer(): Promise<ArrayBuffer> { throw new Error(errorMessage) } blob(): Promise<Blob> blob(): Promise<Blob> blob(): Promise<Blob> { throw new Error(errorMessage) } formData(): Promise<FormData> formData(): Promise<FormData> formData(): Promise<FormData> { throw new Error(errorMessage) } json(): Promise<any> json<T>(): Promise<T> json<T>(): Promise<any> | Promise<T> { throw new Error(errorMessage) } text(): Promise<string> text(): Promise<string> text(): Promise<string> { throw new Error(errorMessage) }}