perf(context): add `init` flag (#1284)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* perf(context): add `init` flag * denoify
authored by Yusuke Wada · committed by GitHub · parent d5cfe04 · Browse files · 2 files·+12 added−4 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+12 added−4 removed
- other:+12 added−4 removed
Provenance
- Commit
47ae014- Tree
bc96fee- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
d5cfe04
2 files changed+12 added−4 removed
modified:deno_dist/context.ts+6 added-2 removed
101 unmodified lines102 private _pH: Record<string, string> | undefined = undefined // _preparedHeaders103 private _res: Response | undefined104 private _path: string = '/'105 private _params?: Record<string, string> | null106+ private _init = true107 private rawRequest?: Request | null108 private notFoundHandler: NotFoundHandler<E> = () => new Response()109 110 constructor(req: Request, options?: ContextOptions<E>) {37 unmodified lines148 }149 }150 151 get res(): Response {152+ this._init = false153 return (this._res ||= new Response('404 Not Found', { status: 404 }))154 }155 156 set res(_res: Response | undefined) {157+ this._init = false158 if (this._res && _res) {159 this._res.headers.delete('content-type')160 this._res.headers.forEach((v, k) => {161 _res.headers.set(k, v)18 unmodified lines180 }181 182 if (options?.append) {183 if (!this._h) {184+ this._init = false185 this._h = new Headers(this._pH)186 this._pH = {}187 }188 this._h.append(name, value)38 unmodified lines227 arg?: StatusCode | ResponseInit,228 headers?: HeaderRecord229 ): Response => {230 // Optimized227- if (!headers && !this._h && !this._res && !arg && this._status === 200) {231+ if (this._init && !headers && !arg && this._status === 200) {232 return new Response(data, {233 headers: this._pH,234 })235 }60 unmodified lines296 ): Response => {297 // If the header is empty, return Response immediately.298 // Content-Type will be added automatically as `text/plain`.299 if (!this._pH) {296- if (!headers && !this._res && !this._h && !arg) {300+ if (this._init && !headers && !arg) {301 return new Response(text)302 }303 this._pH = {}304 }118 unmodified linesmodified:src/context.ts+6 added-2 removed
101 unmodified lines102 private _pH: Record<string, string> | undefined = undefined // _preparedHeaders103 private _res: Response | undefined104 private _path: string = '/'105 private _params?: Record<string, string> | null106+ private _init = true107 private rawRequest?: Request | null108 private notFoundHandler: NotFoundHandler<E> = () => new Response()109 110 constructor(req: Request, options?: ContextOptions<E>) {37 unmodified lines148 }149 }150 151 get res(): Response {152+ this._init = false153 return (this._res ||= new Response('404 Not Found', { status: 404 }))154 }155 156 set res(_res: Response | undefined) {157+ this._init = false158 if (this._res && _res) {159 this._res.headers.delete('content-type')160 this._res.headers.forEach((v, k) => {161 _res.headers.set(k, v)18 unmodified lines180 }181 182 if (options?.append) {183 if (!this._h) {184+ this._init = false185 this._h = new Headers(this._pH)186 this._pH = {}187 }188 this._h.append(name, value)38 unmodified lines227 arg?: StatusCode | ResponseInit,228 headers?: HeaderRecord229 ): Response => {230 // Optimized227- if (!headers && !this._h && !this._res && !arg && this._status === 200) {231+ if (this._init && !headers && !arg && this._status === 200) {232 return new Response(data, {233 headers: this._pH,234 })235 }60 unmodified lines296 ): Response => {297 // If the header is empty, return Response immediately.298 // Content-Type will be added automatically as `text/plain`.299 if (!this._pH) {296- if (!headers && !this._res && !this._h && !arg) {300+ if (this._init && !headers && !arg) {301 return new Response(text)302 }303 this._pH = {}304 }118 unmodified lines