Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jsr): reduce slow types #2369

Merged
merged 5 commits into from
May 12, 2024
Merged

Conversation

nakasyou
Copy link
Contributor

@nakasyou nakasyou commented Mar 17, 2024

I reduced JSR slow types.
It make a piece for creating the JSR version of Hono.

Author should do the followings, if applicable

  • Add tests
  • Run tests
  • yarn denoify to generate files for Deno

@nakasyou
Copy link
Contributor Author

reference: #2201

@nakasyou nakasyou marked this pull request as draft March 17, 2024 12:16
@yusukebe
Copy link
Member

cc: @Kyiro

@nakasyou
Copy link
Contributor Author

In my skill, I can't reduce it any more.
I think we should make some PRs and take steps to JSR including slow types.

@nakasyou nakasyou marked this pull request as ready for review March 17, 2024 13:05
@alexgleason
Copy link

I would just pass --allow-slow-types into it. It's just a warning,

@yusukebe
Copy link
Member

yusukebe commented May 11, 2024

I could publish it with this setting and the diff. But we have to consider making the workflow to publish.

https://jsr.io/@hono/do-not-use-this

{
  "name": "@hono/do-not-use-this",
  "version": "0.0.0",
  "compilerOptions": {
    "lib": [
      "dom",
      "dom.iterable",
      "deno.ns"
    ],
    "jsx": "react",
    "jsxFactory": "jsx"
  },
  "exports": {
    ".": "./deno_dist/mod.ts"
  },
  "publish": {
    "include": [
      "deno.json",
      "LICENSE",
      "README.md",
      "deno_dist/**/*.ts"
    ],
    "exclude": [
      "deno_dist/**/*.test.ts",
      "deno_dist/**/*.test.tsx"
    ]
  }
}
diff --git a/deno_dist/context.ts b/deno_dist/context.ts
index 99fe18c..0cc8a2a 100644
--- a/deno_dist/context.ts
+++ b/deno_dist/context.ts
@@ -224,7 +224,9 @@ export class Context<
    */
   render: Renderer = (...args) => this.renderer(...args)
 
-  setLayout = (layout: Layout<PropsForRenderer & { Layout: Layout }>) => (this.layout = layout)
+  setLayout: (layout: Layout<PropsForRenderer & { Layout: Layout }>) => void = (
+    layout: Layout<PropsForRenderer & { Layout: Layout }>
+  ) => (this.layout = layout)
   getLayout = () => this.layout
 
   /**
diff --git a/deno_dist/hono-base.ts b/deno_dist/hono-base.ts
index b49a6fc..1ff3117 100644
--- a/deno_dist/hono-base.ts
+++ b/deno_dist/hono-base.ts
@@ -1,3 +1,4 @@
+/* eslint-disable @typescript-eslint/no-explicit-any */
 import { compose } from './compose.ts'
 import { Context } from './context.ts'
 import type { ExecutionContext } from './context.ts'
@@ -25,20 +26,6 @@ import { getPath, getPathNoStrict, getQueryStrings, mergePath } from './utils/ur
 
 export const COMPOSED_HANDLER = Symbol('composedHandler')
 
-type Methods = (typeof METHODS)[number] | typeof METHOD_NAME_ALL_LOWERCASE
-
-function defineDynamicClass(): {
-  new <E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(): {
-    [M in Methods]: HandlerInterface<E, M, S, BasePath>
-  } & {
-    on: OnHandlerInterface<E, S, BasePath>
-  } & {
-    use: MiddlewareHandlerInterface<E, S, BasePath>
-  }
-} {
-  return class {} as never
-}
-
 const notFoundHandler = (c: Context) => {
   return c.text('404 Not Found', 404)
 }
@@ -89,11 +76,27 @@ export type HonoOptions<E extends Env> = {
   getPath?: GetPath<E>
 }
 
+abstract class SuperClass<
+  E extends Env = Env,
+  S extends Schema = {},
+  BasePath extends string = '/'
+> {
+  get: HandlerInterface<E, 'get', S, BasePath> = () => new Hono<any, any, any>()
+  post: HandlerInterface<E, 'post', S, BasePath> = () => new Hono<any, any, any>()
+  put: HandlerInterface<E, 'put', S, BasePath> = () => new Hono<any, any, any>()
+  delete: HandlerInterface<E, 'delete', S, BasePath> = () => new Hono<any, any, any>()
+  options: HandlerInterface<E, 'options', S, BasePath> = () => new Hono<any, any, any>()
+  patch: HandlerInterface<E, 'patch', S, BasePath> = () => new Hono<any, any, any>()
+  all: HandlerInterface<E, 'all', S, BasePath> = () => new Hono<any, any, any>()
+  on: OnHandlerInterface<E, S, BasePath> = () => new Hono<any, any, any>()
+  use: MiddlewareHandlerInterface<E, S, BasePath> = () => new Hono<any, any, any>()
+}
+
 class Hono<
   E extends Env = Env,
   S extends Schema = {},
   BasePath extends string = '/'
-> extends defineDynamicClass()<E, S, BasePath> {
+> extends SuperClass<E, S, BasePath> {
   /*
     This class is like an abstract class and does not have a router.
     To use it, inherit the class and implement router in the constructor.
@@ -233,7 +236,7 @@ class Hono<
    * })
    * ```
    */
-  onError = (handler: ErrorHandler<E>) => {
+  onError = (handler: ErrorHandler<E>): Hono<E, S, BasePath> => {
     this.errorHandler = handler
     return this
   }
@@ -247,7 +250,7 @@ class Hono<
    * ```
    * @see https://hono.dev/api/hono#not-found
    */
-  notFound = (handler: NotFoundHandler<E>) => {
+  notFound = (handler: NotFoundHandler<E>): Hono<E, S, BasePath> => {
     this.notFoundHandler = handler
     return this
   }
@@ -396,7 +399,7 @@ class Hono<
     requestInit?: RequestInit,
     Env?: E['Bindings'] | {},
     executionCtx?: ExecutionContext
-  ) => {
+  ): Response | Promise<Response> => {
     if (input instanceof Request) {
       if (requestInit !== undefined) {
         input = new Request(input, requestInit)
diff --git a/deno_dist/hono.ts b/deno_dist/hono.ts
index 522c189..5af1533 100644
--- a/deno_dist/hono.ts
+++ b/deno_dist/hono.ts
@@ -19,3 +19,15 @@ export class Hono<
       })
   }
 }
+
diff --git a/deno_dist/jsx/base.ts b/deno_dist/jsx/base.ts
index 01afff6..2b17b7f 100644
--- a/deno_dist/jsx/base.ts
+++ b/deno_dist/jsx/base.ts
@@ -15,19 +15,6 @@ export type FC<P = Props> = {
 }
 export type DOMAttributes = Hono.HTMLAttributes
 
-declare global {
-  // eslint-disable-next-line @typescript-eslint/no-namespace
-  namespace JSX {
-    type Element = HtmlEscapedString | Promise<HtmlEscapedString>
-    interface ElementChildrenAttribute {
-      children: Child
-    }
-    interface IntrinsicElements extends IntrinsicElementsDefined {
-      [tagName: string]: Props
-    }
-  }
-}
-
 const emptyTags = [
   'area',
   'base',
@@ -348,7 +335,7 @@ export const isValidElement = (element: unknown): element is JSXNode => {
   return !!(element && typeof element === 'object' && 'tag' in element && 'props' in element)
 }
 
-export const cloneElement = <T extends JSXNode | JSX.Element>(
+export const cloneElement = <T extends JSXNode>(
   element: T,
   props: Partial<Props>,
   ...children: Child[]
diff --git a/deno_dist/jsx/hooks/index.ts b/deno_dist/jsx/hooks/index.ts
index 2756699..dc58552 100644
--- a/deno_dist/jsx/hooks/index.ts
+++ b/deno_dist/jsx/hooks/index.ts
@@ -367,8 +367,8 @@ export const createRef = <T>(): RefObject<T> => {
 }
 
 export const forwardRef = <T, P = {}>(
-  Component: (props: P, ref: RefObject<T>) => JSX.Element
-): ((props: P & { ref: RefObject<T> }) => JSX.Element) => {
+  Component: (props: P, ref: RefObject<T>) => {}
+): ((props: P & { ref: RefObject<T> }) => {}) => {
   return (props) => {
     const { ref, ...rest } = props
     return Component(rest as P, ref)
diff --git a/deno_dist/middleware/jwt/index.ts b/deno_dist/middleware/jwt/index.ts
index 490177f..d1bb8dd 100644
--- a/deno_dist/middleware/jwt/index.ts
+++ b/deno_dist/middleware/jwt/index.ts
@@ -6,13 +6,6 @@ import { Jwt } from '../../utils/jwt/index.ts'
 import '../../context.ts'
 import type { SignatureAlgorithm } from '../../utils/jwt/jwa.ts'
 
-declare module '../../context.ts' {
-  interface ContextVariableMap {
-    // eslint-disable-next-line @typescript-eslint/no-explicit-any
-    jwtPayload: any
-  }
-}
-
 export const jwt = (options: {
   secret: string
   cookie?: string
diff --git a/deno_dist/middleware/method-override/index.ts b/deno_dist/middleware/method-override/index.ts
index 60e399a..64ee58f 100644
--- a/deno_dist/middleware/method-override/index.ts
+++ b/deno_dist/middleware/method-override/index.ts
@@ -124,7 +124,7 @@ export const methodOverride = (options: MethodOverrideOptions): MiddlewareHandle
   }
 
 const getExecutionCtx = (c: Context) => {
-  let executionCtx: ExecutionContext | undefined
+  let executionCtx: undefined | any
   try {
     executionCtx = c.executionCtx
   } catch {
diff --git a/deno_dist/middleware/secure-headers/index.ts b/deno_dist/middleware/secure-headers/index.ts
index 3d978ee..f60dc74 100644
--- a/deno_dist/middleware/secure-headers/index.ts
+++ b/deno_dist/middleware/secure-headers/index.ts
@@ -1,13 +1,7 @@
-import { Buffer } from "node:buffer";
+import { Buffer } from 'node:buffer'
 import type { Context } from '../../context.ts'
 import type { MiddlewareHandler } from '../../types.ts'
 
-declare module '../../context.ts' {
-  interface ContextVariableMap {
-    secureHeadersNonce?: string
-  }
-}
-
 export type ContentSecurityPolicyOptionHandler = (ctx: Context, directive: string) => string
 type ContentSecurityPolicyOptionValue = (string | ContentSecurityPolicyOptionHandler)[]
 
diff --git a/deno_dist/middleware/timing/index.ts b/deno_dist/middleware/timing/index.ts
index 429929c..4204ae9 100644
--- a/deno_dist/middleware/timing/index.ts
+++ b/deno_dist/middleware/timing/index.ts
@@ -2,15 +2,6 @@ import type { Context } from '../../context.ts'
 import type { MiddlewareHandler } from '../../types.ts'
 import '../../context.ts'
 
-declare module '../../context.ts' {
-  interface ContextVariableMap {
-    metric?: {
-      headers: string[]
-      timers: Map<string, Timer>
-    }
-  }
-}
-
 interface Timer {
   description?: string
   start: number
diff --git a/deno_dist/mod.ts b/deno_dist/mod.ts
index 4f0441b..fe45b66 100644
--- a/deno_dist/mod.ts
+++ b/deno_dist/mod.ts
@@ -1,12 +1,5 @@
 import { Hono } from './hono.ts'
 
-declare global {
-  interface ExecutionContext {
-    waitUntil(promise: Promise<void>): void
-    passThroughOnException(): void
-  }
-}
-
 export type {
   Env,
   ErrorHandler,
diff --git a/deno_dist/request.ts b/deno_dist/request.ts
index ea18253..6fd80d4 100644
--- a/deno_dist/request.ts
+++ b/deno_dist/request.ts
@@ -289,7 +289,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
    * ```
    * @see https://hono.dev/api/request#url
    */
-  get url() {
+  get url(): string {
     return this.raw.url
   }
 
@@ -303,7 +303,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
    * ```
    * @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 9c35f2b..f0ade46 100644
--- a/deno_dist/router/smart-router/router.ts
+++ b/deno_dist/router/smart-router/router.ts
@@ -59,7 +59,7 @@ export class SmartRouter<T> implements Router<T> {
     return res as Result<T>
   }
 
-  get activeRouter() {
+  get activeRouter(): Router<T> {
     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 075c3d5..5ba55a4 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) => {
+export const bufferToFormData = (
+  arrayBuffer: ArrayBuffer,
+  contentType: string
+): Promise<FormData> => {
   const response = new Response(arrayBuffer, {
     headers: {
       'Content-Type': contentType,
diff --git a/src/hono-base.ts b/src/hono-base.ts
index cb4092e..1666319 100644
--- a/src/hono-base.ts
+++ b/src/hono-base.ts
@@ -233,7 +233,7 @@ class Hono<
    * })
    * ```
    */
-  onError = (handler: ErrorHandler<E>) => {
+  onError = (handler: ErrorHandler<E>): Hono<E, S, BasePath> => {
     this.errorHandler = handler
     return this
   }
@@ -247,7 +247,7 @@ class Hono<
    * ```
    * @see https://hono.dev/api/hono#not-found
    */
-  notFound = (handler: NotFoundHandler<E>) => {
+  notFound = (handler: NotFoundHandler<E>): Hono<E, S, BasePath> => {
     this.notFoundHandler = handler
     return this
   }

@yusukebe yusukebe changed the base branch from main to feat/jsr May 12, 2024 01:03
@yusukebe yusukebe merged commit 24dde8a into honojs:feat/jsr May 12, 2024
10 checks passed
@nakasyou nakasyou deleted the feat/reduce-slow-types branch May 12, 2024 02:04
yusukebe added a commit that referenced this pull request May 12, 2024
* feat(jsr): reduce slow types (#2369)

* feat(jsr): reduce slow types

* fix: use allow function

* chore: format code

* chore: denoify

* use `fn = () => {}` syntax for backwards compatible

* add type annotations and denoify

* add type annotations for helpers and denoify

* tweak

---------

Co-authored-by: Shotaro Nakamura <[email protected]>
yusukebe added a commit that referenced this pull request May 24, 2024
* feat(jsr): reduce slow types (#2369)

* feat(jsr): reduce slow types

* fix: use allow function

* chore: format code

* chore: denoify

* add `deno.json`

* add `jsr-dry-run` command for CI

* don't put `JSX` on `global`

* fix test settings for deno

* don't use `dynamicClass`

* don't declare `ExecutionContext` in `global`

* goodbye denoify

* exports `./middleware`

* exports `./helper`

* exports each helper and middleware

* remove the `awslambda` implementation which is not enough

* feat(jsr): remove helper.ts and middleware.ts (#2667)

* feat(jsr): remove helper.ts and middleware.ts

* fix: fix test

* dont' use `SuperClass`

Co-authored-by: Taku Amano <[email protected]>

* feat(jsr): delete `mod.ts` (#2669)

* rename `deno.json` to `jsr.json`

* lint

* remove slow type in lambda adapter

* fixed runtime test for deno

* export all utils

* add a GitHub action to publish the package to JSR

* fixed declaring `ContextVariableMap`

* fixed the type error

* include `jsr.json` in `jsr.json`

* update `jsr.json`

---------

Co-authored-by: Shotaro Nakamura <[email protected]>
Co-authored-by: Taku Amano <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants