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
Provenance
- Commit
b0b6f7f- Tree
cd187e8- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
9c397fb
2 files changed+34 added−34 removed
modified:src/hono.ts+29 added-29 removed
53 unmodified lines54 strict?: boolean55 }56 57 export class Hono {58- router: Router<Handler[]>58+ router: Router<Handler>59 middlewareRouters: Router<MiddlewareHandler>[]60 tempPath: string61 strict: boolean62 4 unmodified lines67 this.strict = init.strict // strict routing - default is true68 }69 70 /* 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 }75 76- 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 }80 81- 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 }85 86- 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 }90 91- 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 }95 96- 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 }100 101- 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 }105 106 /* 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 }111 112 route(path: string): Hono {113 const newHono: Hono = new Hono()11 unmodified lines125 this.middlewareRouters.push(router)126 }127 128 // 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 }137 138- 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 }141 142 async dispatch(request: Request, env?: Env, event?: FetchEvent): Promise<Response> {15 unmodified lines158 const url = new URL(c.req.url)159 return url.searchParams.get(key)160 }161 162- const handler = result ? result.handler[0] : this.notFound // XXX162+ const handler = result ? result.handler : this.notFound // XXX163 164 const middleware = []165 166 for (const mr of this.middlewareRouters) {63 unmodified linesmodified:README.md+5 added-5 removed
21 unmodified lines22 23 **Hono is fastest** compared to other routers for Cloudflare Workers.24 25 ```plain26-hono x 708,671 ops/sec ±2.58% (58 runs sampled)27-itty-router x 159,610 ops/sec ±2.86% (87 runs sampled)28-sunder x 322,846 ops/sec ±2.24% (86 runs sampled)29-worktop x 223,625 ops/sec ±2.01% (95 runs sampled)26+hono x 723,296 ops/sec ±5.97% (72 runs sampled)27+itty-router x 160,811 ops/sec ±3.35% (91 runs sampled)28+sunder x 302,867 ops/sec ±5.02% (81 runs sampled)29+worktop x 199,306 ops/sec ±5.12% (78 runs sampled)30 Fastest is hono31-✨ Done in 57.83s.31+✨ Done in 50.19s.32 ```33 34 ## Hono in 1 minute35 429 unmodified lines