Skip to content
Myr

refactor: router must have only one hander (#107)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor: router must have only one hander * docs: update benchmark

authored by Yusuke Wada · committed by GitHub · parent 9c397fb · Browse files · 2 files+34 added−34 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+34 added−34 removed

  • other:+34 added−34 removed

2 files+34 added−34 removed

modified:src/hono.ts+29 added-29 removed
53 unmodified lines
54 strict?: boolean55}5657export class Hono {58 router: Router<Handler[]>58 router: Router<Handler>59 middlewareRouters: Router<MiddlewareHandler>[]60 tempPath: string61 strict: boolean62
4 unmodified lines
67 this.strict = init.strict // strict routing - default is true68 }6970 /* HTTP METHODS */71 get<Path extends string>(path: Path, ...args: Handler<ParamKeys<Path>>[]): Hono72 get(path: string, ...args: Handler[]): Hono {73 return this.addRoute('get', path, ...args)71 get<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono72 get(path: string, handler: Handler): Hono {73 return this.addRoute('get', path, handler)74 }7576 post<Path extends string>(path: Path, ...args: Handler<ParamKeys<Path>>[]): Hono77 post(path: string, ...args: Handler[]): Hono {78 return this.addRoute('post', path, ...args)76 post<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono77 post(path: string, handler: Handler): Hono {78 return this.addRoute('post', path, handler)79 }8081 put<Path extends string>(path: Path, ...args: Handler<ParamKeys<Path>>[]): Hono82 put(path: string, ...args: Handler[]): Hono {83 return this.addRoute('put', path, ...args)81 put<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono82 put(path: string, handler: Handler): Hono {83 return this.addRoute('put', path, handler)84 }8586 head<Path extends string>(path: Path, ...args: Handler<ParamKeys<Path>>[]): Hono87 head(path: string, ...args: Handler[]): Hono {88 return this.addRoute('head', path, ...args)86 head<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono87 head(path: string, handler: Handler): Hono {88 return this.addRoute('head', path, handler)89 }9091 delete<Path extends string>(path: Path, ...args: Handler<ParamKeys<Path>>[]): Hono92 delete(path: string, ...args: Handler[]): Hono {93 return this.addRoute('delete', path, ...args)91 delete<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono92 delete(path: string, handler: Handler): Hono {93 return this.addRoute('delete', path, handler)94 }9596 options<Path extends string>(path: Path, ...args: Handler<ParamKeys<Path>>[]): Hono97 options(path: string, ...args: Handler[]): Hono {98 return this.addRoute('options', path, ...args)96 options<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono97 options(path: string, handler: Handler): Hono {98 return this.addRoute('options', path, handler)99 }100101 patch<Path extends string>(path: Path, ...args: Handler<ParamKeys<Path>>[]): Hono102 patch(path: string, ...args: Handler[]): Hono {103 return this.addRoute('patch', path, ...args)101 patch<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono102 patch(path: string, handler: Handler): Hono {103 return this.addRoute('patch', path, handler)104 }105106 /* Any methods */107 all<Path extends string>(path: Path, ...args: Handler<ParamKeys<Path>>[]): Hono108 all(path: string, ...args: Handler[]): Hono {109 return this.addRoute('all', path, ...args)107 all<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono108 all(path: string, handler: Handler): Hono {109 return this.addRoute('all', path, handler)110 }111112 route(path: string): Hono {113 const newHono: Hono = new Hono()
11 unmodified lines
125 this.middlewareRouters.push(router)126 }127128 // addRoute('get', '/', handler)129 addRoute(method: string, path: string, ...args: Handler[]): Hono {129 addRoute(method: string, path: string, handler: Handler): Hono {130 method = method.toUpperCase()131 if (this.tempPath) {132 path = mergePath(this.tempPath, path)133 }134 this.router.add(method, path, args)134 this.router.add(method, path, handler)135 return this136 }137138 async matchRoute(method: string, path: string): Promise<Result<Handler[]>> {138 async matchRoute(method: string, path: string): Promise<Result<Handler>> {139 return this.router.match(method, path)140 }141142 async dispatch(request: Request, env?: Env, event?: FetchEvent): Promise<Response> {
15 unmodified lines
158 const url = new URL(c.req.url)159 return url.searchParams.get(key)160 }161162 const handler = result ? result.handler[0] : this.notFound // XXX162 const handler = result ? result.handler : this.notFound // XXX163164 const middleware = []165166 for (const mr of this.middlewareRouters) {
63 unmodified lines
modified:README.md+5 added-5 removed
21 unmodified lines
2223**Hono is fastest** compared to other routers for Cloudflare Workers.2425```plain26hono x 708,671 ops/sec ±2.58% (58 runs sampled)27itty-router x 159,610 ops/sec ±2.86% (87 runs sampled)28sunder x 322,846 ops/sec ±2.24% (86 runs sampled)29worktop x 223,625 ops/sec ±2.01% (95 runs sampled)26hono x 723,296 ops/sec ±5.97% (72 runs sampled)27itty-router x 160,811 ops/sec ±3.35% (91 runs sampled)28sunder x 302,867 ops/sec ±5.02% (81 runs sampled)29worktop x 199,306 ops/sec ±5.12% (78 runs sampled)30Fastest is hono31✨ Done in 57.83s.31✨ Done in 50.19s.32```3334## Hono in 1 minute35
429 unmodified lines