Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit acc94e0

Browse files
committed
feat: layers
1 parent eb07d15 commit acc94e0

File tree

4 files changed

+55
-25
lines changed

4 files changed

+55
-25
lines changed

results/results.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
| Framework | Runtime | Average | Ping | Query | Body |
32
| ---------------- | ------- | ------- | ---------- | ---------- | ---------- |
43
| elysia | bun | 143,717.933 | 299,444.38 | 78,088.78 | 53,620.64 |
54
| lemmih | bun | 74,235.703 | 126,405.7 | 49,198.34 | 47,103.07 |
6-
| hono | bun | 52,641.26 | 71,254.38 | 48,320.05 | 38,349.35 |
5+
| hono | bun | 52,641.26 | 71,254.38 | 48,320.05 | 38,349.35 |

src/bun.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { Handler, MaybePromise } from './types'
1+
import type { Handler, MaybePromise, Next, StrictHandler } from './types'
22

33
import { App } from './core/app'
4-
import { handle } from './core/handle'
54
import { Router } from './core/router'
65

76
export interface LeMMIHBuildResult {
@@ -23,30 +22,37 @@ class BunApp extends App {
2322
acc.dynamicRoutes.set(path, handler)
2423
}
2524
else {
26-
acc.staticRoutes[path] = handler as (req: Request) => MaybePromise<Response>
25+
acc.staticRoutes[path] = this.withLayers(handler)
2726
}
2827
return acc
2928
}, {
3029
dynamicRoutes: new Map<string, Handler>(),
31-
staticRoutes: {} as Record<string, (req: Request) => MaybePromise<Response>>,
30+
staticRoutes: {} as Record<string, StrictHandler>,
3231
})
3332

3433
const router = new Router<Handler>()
3534
dynamicRoutes.forEach((handler, path) => router.insert(path, handler))
3635

3736
return {
38-
fetch: async (req) => {
39-
const result = router.find(new URL(req.url).pathname)
40-
41-
return handle(
42-
result?.value ?? this.fallback,
43-
req,
44-
result?.params,
45-
)
46-
},
37+
fetch: async req => this.handle(req, router),
4738
routes: staticRoutes,
4839
}
4940
}
41+
42+
private withLayers(handler: Handler): StrictHandler {
43+
return async (req: Request) => {
44+
const next: Next = async () => (handler as StrictHandler)(req)
45+
46+
return (
47+
this.layers.length > 0
48+
? this.layers.reduceRight<Next>(
49+
(currentNext, layer) => async () => layer(req, currentNext),
50+
next,
51+
)
52+
: next
53+
)()
54+
}
55+
}
5056
}
5157

5258
export { BunApp as App }

src/core/app.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
import type { Handler } from '../types'
1+
import type { Handler, Layer, Next } from '../types'
22

33
import { status } from '../res'
44
import { handle } from './handle'
55
import { Router } from './router'
66

77
export class App {
88
fallback: Handler = status(404)
9+
layers: Layer[] = []
910
routes: Map<string, Handler> = new Map()
1011

1112
get fetch(): (req: Request) => Promise<Response> {
1213
const router = new Router<Handler>()
1314
this.routes.forEach((handler, path) => router.insert(path, handler))
1415

15-
return async (req) => {
16-
const result = router.find(new URL(req.url).pathname)
17-
18-
return handle(
19-
result?.value ?? this.fallback,
20-
req,
21-
result?.params,
22-
)
23-
}
16+
return async req => this.handle(req, router)
2417
}
2518

2619
constructor(fallback?: Handler) {
@@ -29,8 +22,34 @@ export class App {
2922
this.fallback = fallback
3023
}
3124

25+
public async handle(req: Request, router: Router<Handler>) {
26+
const result = router.find(new URL(req.url).pathname)
27+
28+
const next = async () => handle(
29+
result?.value ?? this.fallback,
30+
req,
31+
result?.params,
32+
)
33+
34+
return (
35+
this.layers.length > 0
36+
? this.layers.reduceRight<Next>(
37+
(next, layer) => async () => layer(req, next),
38+
next,
39+
)
40+
: next
41+
)()
42+
}
43+
44+
public layer(...layers: Layer[]): this {
45+
this.layers.push(...layers)
46+
47+
return this
48+
}
49+
3250
public merge(app: App): this {
3351
app.routes.forEach((handler, path) => this.routes.set(path, handler))
52+
app.layers.forEach(layer => this.layers.push(layer))
3453

3554
// TODO: merge fallback
3655

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export type Handler = ((req: Request, params?: Record<string, string>) => MaybePromise<Response>) | MaybePromise<Response>
22

3+
export type Layer = (req: Request, next: Next) => MaybePromise<Response>
4+
35
export type MaybePromise<T> = Promise<T> | T
6+
7+
export type Next = () => MaybePromise<Response>
8+
9+
export type StrictHandler = (req: Request) => Promise<Response>

0 commit comments

Comments
 (0)