Skip to content

Commit ce0494d

Browse files
authored
feat(wobe): add new router (#62)
1 parent 8750d88 commit ce0494d

File tree

15 files changed

+2242
-83
lines changed

15 files changed

+2242
-83
lines changed

bun.lock

Lines changed: 44 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
{
2-
"name": "wobe-benchmark",
3-
"version": "0.1.0",
4-
"main": "index.ts",
5-
"dependencies": {
6-
"wobe": "*"
7-
},
8-
"devDependencies": {
9-
"elysia": "1.0.16",
10-
"get-port": "7.1.0",
11-
"mitata": "0.1.11",
12-
"hono": "4.10.4",
13-
"koa-router": "12.0.1",
14-
"radix3": "1.1.2",
15-
"find-my-way": "9.3.0"
16-
},
17-
"scripts": {
18-
"bench:startup": "bun run startup/benchmark.ts",
19-
"bench:router": "bun run router/benchmark.ts",
20-
"bench:extracter": "bun run pathExtract/benchmark.ts",
21-
"bench:findHook": "bun run findHook/benchmark.ts"
22-
}
2+
"name": "wobe-benchmark",
3+
"version": "0.1.0",
4+
"main": "index.ts",
5+
"dependencies": {
6+
"wobe": "*"
7+
},
8+
"devDependencies": {
9+
"@sinclair/typebox": "0.34.41",
10+
"elysia": "1.4.18",
11+
"get-port": "7.1.0",
12+
"mitata": "1.0.34",
13+
"hono": "4.10.7",
14+
"@koa/router": "15.0.0",
15+
"radix3": "1.1.2",
16+
"find-my-way": "9.3.0"
17+
},
18+
"scripts": {
19+
"bench:startup": "bun run startup/benchmark.ts",
20+
"bench:router": "bun run router/benchmark.ts",
21+
"bench:extracter": "bun run pathExtract/benchmark.ts",
22+
"bench:findHook": "bun run findHook/benchmark.ts",
23+
"lint": "biome lint . --no-errors-on-unmatched",
24+
"format": "biome format --write ."
25+
}
2326
}

packages/wobe-benchmark/router/koaRouter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import KoaRouter from 'koa-router'
1+
import KoaRouter from '@koa/router'
22
import type { RouterInterface } from './tools'
33
import { routes, handler } from './tools'
44

@@ -7,7 +7,7 @@ const router = new KoaRouter()
77

88
for (const route of routes) {
99
if (route.method === 'GET') {
10-
router.get(route.pathToCompile.replace('*', '(.*)'), handler)
10+
router.get(route.pathToCompile.replace('*', '/*path'), handler)
1111
} else {
1212
router.post(route.pathToCompile, handler)
1313
}

packages/wobe-benchmark/router/wobe.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RadixTree } from 'wobe/src/router'
21
import { routes, type Route } from './tools'
2+
import { UrlPatternRouter, type Router } from 'wobe'
33

4-
const createWobeRouter = (name: string, radixTree: RadixTree) => {
4+
const createWobeRouter = (name: string, radixTree: Router) => {
55
for (const route of routes) {
66
radixTree.addRoute(route.method, route.pathToCompile, () =>
77
Promise.resolve(),
@@ -18,4 +18,7 @@ const createWobeRouter = (name: string, radixTree: RadixTree) => {
1818
}
1919
}
2020

21-
export const wobeRouter = createWobeRouter('Radix router', new RadixTree())
21+
export const wobeRouter = createWobeRouter(
22+
'UrlPattern router',
23+
new UrlPatternRouter(),
24+
)

packages/wobe-benchmark/startup/elysia.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import getPort from 'get-port'
33

44
export const elysiaApp = async () => {
55
const port = await getPort()
6-
const elysia = new Elysia({ precompile: true })
6+
const app = new Elysia()
77
.get('/', 'Hi')
8-
.post('/json', (c) => c.body, {
9-
type: 'json',
10-
})
11-
.get('/id/:id', ({ set, params: { id }, query: { name } }) => {
12-
set.headers['x-powered-by'] = 'benchmark'
8+
.get('/id/:id', (c) => {
9+
c.set.headers['x-powered-by'] = 'benchmark'
1310

14-
return id + ' ' + name
11+
return `${c.params.id} ${c.query.name}`
12+
})
13+
.post('/json', (c) => c.body, {
14+
parse: 'json',
1515
})
1616
.listen(port)
1717

18-
elysia.stop()
18+
app.stop()
1919
}

packages/wobe/src/Context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HttpMethod, WobeHandler, WobeHandlerOutput } from './Wobe'
22
import { WobeResponse } from './WobeResponse'
3-
import type { RadixTree } from './router'
3+
import type { Router } from './router'
44
import { extractPathnameAndSearchParams } from './utils'
55

66
export class Context {
@@ -18,14 +18,14 @@ export class Context {
1818
public beforeHandlerHook: Array<WobeHandler<any>> = []
1919
public afterHandlerHook: Array<WobeHandler<any>> = []
2020

21-
constructor(request: Request, router?: RadixTree) {
21+
constructor(request: Request, router?: Router) {
2222
this.request = request
2323
this.res = new WobeResponse(request)
2424

2525
this._findRoute(router)
2626
}
2727

28-
private _findRoute(router?: RadixTree) {
28+
private _findRoute(router?: Router) {
2929
const { pathName, searchParams } = extractPathnameAndSearchParams(
3030
this.request.url,
3131
)

packages/wobe/src/Wobe.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Server, ServerWebSocket } from 'bun'
2-
import { RadixTree } from './router'
2+
import { RadixTree, type Router } from './router'
33
import { BunAdapter, NodeAdapter, type RuntimeAdapter } from './adapters'
44
import type { Context } from './Context'
55

@@ -29,6 +29,11 @@ export interface WobeOptions {
2929
cert: string
3030
passphrase?: string
3131
}
32+
/**
33+
* Provide a custom router implementation (RadixTree, UrlPatternRouter, or compatible).
34+
* Defaults to UrlPatternRouter when not supplied.
35+
*/
36+
router?: Router
3237
}
3338

3439
export type HttpMethod = 'POST' | 'GET' | 'DELETE' | 'PUT' | 'ALL' | 'OPTIONS'
@@ -100,7 +105,7 @@ export class Wobe<T> {
100105
hook: Hook
101106
method: HttpMethod
102107
}>
103-
private router: RadixTree
108+
private router: Router
104109
private runtimeAdapter: RuntimeAdapter = factoryOfRuntime()
105110
private httpMethods: Array<HttpMethod> = [
106111
'GET',
@@ -119,7 +124,7 @@ export class Wobe<T> {
119124
this.wobeOptions = options
120125
this.hooks = []
121126
this.server = null
122-
this.router = new RadixTree()
127+
this.router = options?.router || new RadixTree()
123128
}
124129

125130
/**

packages/wobe/src/adapters/bun/bun.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { RuntimeAdapter } from '..'
22
import { Context } from '../../Context'
33
import { HttpException } from '../../HttpException'
44
import type { WobeOptions, WobeWebSocket } from '../../Wobe'
5-
import type { RadixTree } from '../../router'
5+
import type { Router } from '../../router'
66
import { bunWebSocket } from './websocket'
77
import { brotliDecompressSync, gunzipSync, inflateSync } from 'node:zlib'
88

@@ -55,7 +55,7 @@ const decompressBody = (
5555
export const BunAdapter = (): RuntimeAdapter => ({
5656
createServer: (
5757
port: number,
58-
router: RadixTree,
58+
router: Router,
5959
options?: WobeOptions,
6060
webSocket?: WobeWebSocket,
6161
) =>

packages/wobe/src/adapters/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { WobeOptions, WobeWebSocket } from '../Wobe'
2-
import type { RadixTree } from '../router'
2+
import type { Router } from '../router'
33

44
export * from './bun'
55
export * from './node'
66

77
export interface RuntimeAdapter {
88
createServer: (
99
port: number,
10-
router: RadixTree,
10+
router: Router,
1111
options?: WobeOptions,
1212
webSocket?: WobeWebSocket,
1313
) => any

packages/wobe/src/adapters/node/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { brotliDecompressSync, gunzipSync, inflateSync } from 'node:zlib'
44
import { HttpException } from '../../HttpException'
55
import { Context } from '../../Context'
66
import type { RuntimeAdapter } from '..'
7-
import type { RadixTree } from '../../router'
7+
import type { Router } from '../../router'
88
import type { WobeOptions } from '../../Wobe'
99

1010
const DEFAULT_MAX_BODY_SIZE = 1024 * 1024 // 1 MiB
@@ -85,7 +85,7 @@ const transformResponseInstanceToValidResponse = async (response: Response) => {
8585
}
8686

8787
export const NodeAdapter = (): RuntimeAdapter => ({
88-
createServer: (port: number, router: RadixTree, options?: WobeOptions) => {
88+
createServer: (port: number, router: Router, options?: WobeOptions) => {
8989
// @ts-expect-error
9090
const createServer: typeof createHttpsServer = options?.tls
9191
? createHttpsServer

0 commit comments

Comments
 (0)