Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/router/reg-exp-router/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ export type Matcher<T> = [RegExp, HandlerData<T>[], StaticMap<T>]
export type MatcherMap<T> = Record<string, Matcher<T> | null>

export const emptyParam: string[] = []
export const buildAllMatchersKey = Symbol('buildAllMatchers')
export function match<R extends Router<T>, T>(this: R, method: string, path: string): Result<T> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const matchers: MatcherMap<T> = (this as any)[buildAllMatchersKey]()
const matchers: MatcherMap<T> = (this as any).buildAllMatchers()

const match = ((method, path) => {
const matcher = (matchers[method] || matchers[METHOD_NAME_ALL]) as Matcher<T>
Expand Down
13 changes: 9 additions & 4 deletions src/router/reg-exp-router/prepared-router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ParamIndexMap, Result, Router } from '../../router'
import { METHOD_NAME_ALL } from '../../router'
import type { HandlerData, Matcher, MatcherMap, StaticMap } from './matcher'
import { match, buildAllMatchersKey, emptyParam } from './matcher'
import { match, emptyParam } from './matcher'
import { RegExpRouter } from './router'
Copy link
Member

@yusukebe yusukebe Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not related to this PR, but it uses only the type so that you can write like this:

import type { RegExpRouter } from './router'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your comment.
We're using it as a base class here!

If bundling doesn't produce the intended result, I think it's possible to separate the files to minimize the impact.

https://github.com/honojs/hono/pull/4458/files/0c98e13168f338ce40553cbb7dd97d7f0b3fe131#diff-05f98af8ac89c7dd686b79c41d8167478c790c0051245d79c3b0f0554351dc63R75

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@usualoma Ah, class! Sorry, my misunderstanding. It's okay as it is.


type RelocateMap = Record<string, [(number | string)[], ParamIndexMap | undefined][]>
Expand Down Expand Up @@ -62,7 +62,7 @@ export class PreparedRegExpRouter<T> implements Router<T> {
}
}

[buildAllMatchersKey](): MatcherMap<T> {
protected buildAllMatchers(): MatcherMap<T> {
return this.#matchers
}

Expand All @@ -72,12 +72,17 @@ export class PreparedRegExpRouter<T> implements Router<T> {
export const buildInitParams: (params: {
paths: string[]
}) => ConstructorParameters<typeof PreparedRegExpRouter> = ({ paths }) => {
const router = new RegExpRouter<string>()
const RegExpRouterWithMatcherExport = class<T> extends RegExpRouter<T> {
buildAndExportAllMatchers() {
return this.buildAllMatchers()
}
}
const router = new RegExpRouterWithMatcherExport<string>()
for (const path of paths) {
router.add(METHOD_NAME_ALL, path, path)
}

const matchers = router[buildAllMatchersKey]()
const matchers = router.buildAndExportAllMatchers()
const all = matchers[METHOD_NAME_ALL] as Matcher<string>

const relocateMap: RelocateMap = {}
Expand Down
6 changes: 3 additions & 3 deletions src/router/reg-exp-router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '../../router'
import { checkOptionalParameter } from '../../utils/url'
import type { HandlerData, StaticMap, Matcher, MatcherMap } from './matcher'
import { match, emptyParam, buildAllMatchersKey } from './matcher'
import { match, emptyParam } from './matcher'
import { PATH_ERROR } from './node'
import type { ParamAssocArray } from './node'
import { Trie } from './trie'
Expand Down Expand Up @@ -203,9 +203,9 @@ export class RegExpRouter<T> implements Router<T> {
}
}

match: typeof match<Router<T>, T> = match;
match: typeof match<Router<T>, T> = match

[buildAllMatchersKey](): MatcherMap<T> {
protected buildAllMatchers(): MatcherMap<T> {
const matchers: MatcherMap<T> = Object.create(null)

Object.keys(this.#routes!)
Expand Down
Loading