From a8077a73d9d4ccc04d8b17b1ffe33d9414e7ab4d Mon Sep 17 00:00:00 2001 From: Shotaro Nakamura Date: Sun, 17 Mar 2024 12:12:26 +0000 Subject: [PATCH 1/4] feat(jsr): reduce slow types --- deno_dist/context.ts | 19 +++++++++++++++---- deno_dist/hono-base.ts | 10 +++++----- deno_dist/request.ts | 4 ++-- deno_dist/router/smart-router/router.ts | 2 +- deno_dist/utils/buffer.ts | 2 +- src/context.ts | 19 +++++++++++++++---- src/hono-base.ts | 10 +++++----- src/request.ts | 4 ++-- src/router/smart-router/router.ts | 2 +- src/utils/buffer.ts | 2 +- 10 files changed, 48 insertions(+), 26 deletions(-) diff --git a/deno_dist/context.ts b/deno_dist/context.ts index a97e302f0..4d91ea823 100644 --- a/deno_dist/context.ts +++ b/deno_dist/context.ts @@ -1,8 +1,8 @@ import type { HonoRequest } from './request.ts' -import type { Env, FetchEventLike, NotFoundHandler, Input, TypedResponse } from './types.ts' -import { resolveCallback, HtmlEscapedCallbackPhase } from './utils/html.ts' +import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types.ts' +import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html.ts' import type { RedirectStatusCode, StatusCode } from './utils/http-status.ts' -import type { JSONValue, InterfaceToType, JSONParsed, IsAny } from './utils/types.ts' +import type { InterfaceToType, IsAny, JSONParsed, JSONValue } from './utils/types.ts' type HeaderRecord = Record type Data = string | ArrayBuffer | ReadableStream @@ -215,7 +215,18 @@ export class Context< */ render: Renderer = (...args) => this.renderer(...args) - setLayout = (layout: Layout) => (this.layout = layout) + setLayout: ( + layout: Layout< + PropsForRenderer & { + Layout: Layout + } + > + ) => Layout< + PropsForRenderer & { + Layout: Layout + } + > = (layout) => (this.layout = layout) + getLayout = () => this.layout /** diff --git a/deno_dist/hono-base.ts b/deno_dist/hono-base.ts index c0c41ee7a..8a1c75570 100644 --- a/deno_dist/hono-base.ts +++ b/deno_dist/hono-base.ts @@ -233,7 +233,7 @@ class Hono< * }) * ``` */ - onError = (handler: ErrorHandler) => { + onError (handler: ErrorHandler): Hono { this.errorHandler = handler return this } @@ -247,7 +247,7 @@ class Hono< * ``` * @see https://hono.dev/api/hono#not-found */ - notFound = (handler: NotFoundHandler) => { + notFound (handler: NotFoundHandler): Hono { this.notFoundHandler = handler return this } @@ -371,7 +371,7 @@ class Hono< * `.fetch()` will be entry point of your app. * @see https://hono.dev/api/hono#fetch */ - fetch = (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext) => { + fetch (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext): Response | Promise { return this.dispatch(request, executionCtx, Env, request.method) } @@ -387,12 +387,12 @@ class Hono< * ``` * @see https://hono.dev/api/hono#request */ - request = ( + request ( input: RequestInfo | URL, requestInit?: RequestInit, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext - ) => { + ): Response | Promise{ if (input instanceof Request) { if (requestInit !== undefined) { input = new Request(input, requestInit) diff --git a/deno_dist/request.ts b/deno_dist/request.ts index d68206c59..33efcddba 100644 --- a/deno_dist/request.ts +++ b/deno_dist/request.ts @@ -279,7 +279,7 @@ export class HonoRequest

{ * ``` * @see https://hono.dev/api/request#url */ - get url() { + get url(): string { return this.raw.url } @@ -293,7 +293,7 @@ export class HonoRequest

{ * ``` * @see https://hono.dev/api/request#method */ - get method() { + get method(): string { return this.raw.method } diff --git a/deno_dist/router/smart-router/router.ts b/deno_dist/router/smart-router/router.ts index 9c35f2b70..f0ade4671 100644 --- a/deno_dist/router/smart-router/router.ts +++ b/deno_dist/router/smart-router/router.ts @@ -59,7 +59,7 @@ export class SmartRouter implements Router { return res as Result } - get activeRouter() { + get activeRouter(): Router { if (this.routes || this.routers.length !== 1) { throw new Error('No active router has been determined yet.') } diff --git a/deno_dist/utils/buffer.ts b/deno_dist/utils/buffer.ts index 075c3d5ec..5288d3ae3 100644 --- a/deno_dist/utils/buffer.ts +++ b/deno_dist/utils/buffer.ts @@ -48,7 +48,7 @@ export const bufferToString = (buffer: ArrayBuffer): string => { return buffer } -export const bufferToFormData = (arrayBuffer: ArrayBuffer, contentType: string) => { +export const bufferToFormData = (arrayBuffer: ArrayBuffer, contentType: string): Promise => { const response = new Response(arrayBuffer, { headers: { 'Content-Type': contentType, diff --git a/src/context.ts b/src/context.ts index 9d9d6e8be..c1f2a5f54 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,8 +1,8 @@ import type { HonoRequest } from './request' -import type { Env, FetchEventLike, NotFoundHandler, Input, TypedResponse } from './types' -import { resolveCallback, HtmlEscapedCallbackPhase } from './utils/html' +import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types' +import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html' import type { RedirectStatusCode, StatusCode } from './utils/http-status' -import type { JSONValue, InterfaceToType, JSONParsed, IsAny } from './utils/types' +import type { InterfaceToType, IsAny, JSONParsed, JSONValue } from './utils/types' type HeaderRecord = Record type Data = string | ArrayBuffer | ReadableStream @@ -215,7 +215,18 @@ export class Context< */ render: Renderer = (...args) => this.renderer(...args) - setLayout = (layout: Layout) => (this.layout = layout) + setLayout: ( + layout: Layout< + PropsForRenderer & { + Layout: Layout + } + > + ) => Layout< + PropsForRenderer & { + Layout: Layout + } + > = (layout) => (this.layout = layout) + getLayout = () => this.layout /** diff --git a/src/hono-base.ts b/src/hono-base.ts index d849fce70..dacb8f03a 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -233,7 +233,7 @@ class Hono< * }) * ``` */ - onError = (handler: ErrorHandler) => { + onError (handler: ErrorHandler): Hono { this.errorHandler = handler return this } @@ -247,7 +247,7 @@ class Hono< * ``` * @see https://hono.dev/api/hono#not-found */ - notFound = (handler: NotFoundHandler) => { + notFound (handler: NotFoundHandler): Hono { this.notFoundHandler = handler return this } @@ -371,7 +371,7 @@ class Hono< * `.fetch()` will be entry point of your app. * @see https://hono.dev/api/hono#fetch */ - fetch = (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext) => { + fetch (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext): Response | Promise { return this.dispatch(request, executionCtx, Env, request.method) } @@ -387,12 +387,12 @@ class Hono< * ``` * @see https://hono.dev/api/hono#request */ - request = ( + request ( input: RequestInfo | URL, requestInit?: RequestInit, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext - ) => { + ): Response | Promise{ if (input instanceof Request) { if (requestInit !== undefined) { input = new Request(input, requestInit) diff --git a/src/request.ts b/src/request.ts index c82584e2e..38414bf1d 100644 --- a/src/request.ts +++ b/src/request.ts @@ -279,7 +279,7 @@ export class HonoRequest

{ * ``` * @see https://hono.dev/api/request#url */ - get url() { + get url(): string { return this.raw.url } @@ -293,7 +293,7 @@ export class HonoRequest

{ * ``` * @see https://hono.dev/api/request#method */ - get method() { + get method(): string { return this.raw.method } diff --git a/src/router/smart-router/router.ts b/src/router/smart-router/router.ts index 7b0510d87..aa1201122 100644 --- a/src/router/smart-router/router.ts +++ b/src/router/smart-router/router.ts @@ -59,7 +59,7 @@ export class SmartRouter implements Router { return res as Result } - get activeRouter() { + get activeRouter(): Router { if (this.routes || this.routers.length !== 1) { throw new Error('No active router has been determined yet.') } diff --git a/src/utils/buffer.ts b/src/utils/buffer.ts index fecfd82eb..f7ddf2618 100644 --- a/src/utils/buffer.ts +++ b/src/utils/buffer.ts @@ -48,7 +48,7 @@ export const bufferToString = (buffer: ArrayBuffer): string => { return buffer } -export const bufferToFormData = (arrayBuffer: ArrayBuffer, contentType: string) => { +export const bufferToFormData = (arrayBuffer: ArrayBuffer, contentType: string): Promise => { const response = new Response(arrayBuffer, { headers: { 'Content-Type': contentType, From 190487c6f8f51effb23fe22b4260d04d3f3aeaa1 Mon Sep 17 00:00:00 2001 From: Shotaro Nakamura Date: Sun, 17 Mar 2024 12:49:00 +0000 Subject: [PATCH 2/4] fix: use allow function --- src/hono-base.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/hono-base.ts b/src/hono-base.ts index dacb8f03a..8b0d0b9a1 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -233,7 +233,7 @@ class Hono< * }) * ``` */ - onError (handler: ErrorHandler): Hono { + onError: (handler: ErrorHandler) => Hono = (handler) => { this.errorHandler = handler return this } @@ -247,7 +247,7 @@ class Hono< * ``` * @see https://hono.dev/api/hono#not-found */ - notFound (handler: NotFoundHandler): Hono { + notFound: (handler: NotFoundHandler) => Hono = (handler) => { this.notFoundHandler = handler return this } @@ -371,7 +371,7 @@ class Hono< * `.fetch()` will be entry point of your app. * @see https://hono.dev/api/hono#fetch */ - fetch (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext): Response | Promise { + fetch: (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext) => Response | Promise = (request, Env, executionCtx) => { return this.dispatch(request, executionCtx, Env, request.method) } @@ -387,12 +387,14 @@ class Hono< * ``` * @see https://hono.dev/api/hono#request */ - request ( + request: ( input: RequestInfo | URL, requestInit?: RequestInit, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext - ): Response | Promise{ + ) => Response | Promise = ( + input, requestInit, Env, executionCtx + ) => { if (input instanceof Request) { if (requestInit !== undefined) { input = new Request(input, requestInit) From ca15f2be2776e68ec06c298183206a16f9794aa6 Mon Sep 17 00:00:00 2001 From: Shotaro Nakamura Date: Sun, 17 Mar 2024 12:50:01 +0000 Subject: [PATCH 3/4] chore: format code --- src/hono-base.ts | 12 +++++++----- src/utils/buffer.ts | 5 ++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/hono-base.ts b/src/hono-base.ts index 8b0d0b9a1..234155c44 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -233,7 +233,7 @@ class Hono< * }) * ``` */ - onError: (handler: ErrorHandler) => Hono = (handler) => { + onError: (handler: ErrorHandler) => Hono = (handler) => { this.errorHandler = handler return this } @@ -371,7 +371,11 @@ class Hono< * `.fetch()` will be entry point of your app. * @see https://hono.dev/api/hono#fetch */ - fetch: (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext) => Response | Promise = (request, Env, executionCtx) => { + fetch: ( + request: Request, + Env?: E['Bindings'] | {}, + executionCtx?: ExecutionContext + ) => Response | Promise = (request, Env, executionCtx) => { return this.dispatch(request, executionCtx, Env, request.method) } @@ -392,9 +396,7 @@ class Hono< requestInit?: RequestInit, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext - ) => Response | Promise = ( - input, requestInit, Env, executionCtx - ) => { + ) => Response | Promise = (input, requestInit, Env, executionCtx) => { if (input instanceof Request) { if (requestInit !== undefined) { input = new Request(input, requestInit) diff --git a/src/utils/buffer.ts b/src/utils/buffer.ts index f7ddf2618..6d24f692f 100644 --- a/src/utils/buffer.ts +++ b/src/utils/buffer.ts @@ -48,7 +48,10 @@ export const bufferToString = (buffer: ArrayBuffer): string => { return buffer } -export const bufferToFormData = (arrayBuffer: ArrayBuffer, contentType: string): Promise => { +export const bufferToFormData = ( + arrayBuffer: ArrayBuffer, + contentType: string +): Promise => { const response = new Response(arrayBuffer, { headers: { 'Content-Type': contentType, From 4ec4d3ce1e527da91a35723d1ba70854e6a9aab1 Mon Sep 17 00:00:00 2001 From: Shotaro Nakamura Date: Sun, 17 Mar 2024 12:52:00 +0000 Subject: [PATCH 4/4] chore: denoify --- deno_dist/hono-base.ts | 14 +++++++++----- deno_dist/utils/buffer.ts | 5 ++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/deno_dist/hono-base.ts b/deno_dist/hono-base.ts index 8a1c75570..61eef7b20 100644 --- a/deno_dist/hono-base.ts +++ b/deno_dist/hono-base.ts @@ -233,7 +233,7 @@ class Hono< * }) * ``` */ - onError (handler: ErrorHandler): Hono { + onError: (handler: ErrorHandler) => Hono = (handler) => { this.errorHandler = handler return this } @@ -247,7 +247,7 @@ class Hono< * ``` * @see https://hono.dev/api/hono#not-found */ - notFound (handler: NotFoundHandler): Hono { + notFound: (handler: NotFoundHandler) => Hono = (handler) => { this.notFoundHandler = handler return this } @@ -371,7 +371,11 @@ class Hono< * `.fetch()` will be entry point of your app. * @see https://hono.dev/api/hono#fetch */ - fetch (request: Request, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext): Response | Promise { + fetch: ( + request: Request, + Env?: E['Bindings'] | {}, + executionCtx?: ExecutionContext + ) => Response | Promise = (request, Env, executionCtx) => { return this.dispatch(request, executionCtx, Env, request.method) } @@ -387,12 +391,12 @@ class Hono< * ``` * @see https://hono.dev/api/hono#request */ - request ( + request: ( input: RequestInfo | URL, requestInit?: RequestInit, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext - ): Response | Promise{ + ) => Response | Promise = (input, requestInit, Env, executionCtx) => { if (input instanceof Request) { if (requestInit !== undefined) { input = new Request(input, requestInit) diff --git a/deno_dist/utils/buffer.ts b/deno_dist/utils/buffer.ts index 5288d3ae3..5ba55a4e0 100644 --- a/deno_dist/utils/buffer.ts +++ b/deno_dist/utils/buffer.ts @@ -48,7 +48,10 @@ export const bufferToString = (buffer: ArrayBuffer): string => { return buffer } -export const bufferToFormData = (arrayBuffer: ArrayBuffer, contentType: string): Promise => { +export const bufferToFormData = ( + arrayBuffer: ArrayBuffer, + contentType: string +): Promise => { const response = new Response(arrayBuffer, { headers: { 'Content-Type': contentType,