Skip to content

Commit

Permalink
fix(wobe): hook on all path (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
coratgerl authored Jul 23, 2024
1 parent ea5d5fe commit 420b04b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/wobe/src/Wobe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ describe('Wobe', () => {

return ctx.res.send('2')
})
.get('/name/with/slash', (ctx) => {
return ctx.res.send('OK')
})
.get('/upload', async () => {
return new Response(
Bun.file(`${__dirname}/../fixtures/testFile.html`),
Expand Down Expand Up @@ -567,6 +570,16 @@ describe('Wobe', () => {
expect(mockAllMethod).toHaveBeenCalledTimes(4)
})

it('should handle beforeHandler hook on path with slash', async () => {
await fetch(`http://127.0.0.1:${port}/name/with/slash`)

expect(mockHook).toHaveBeenCalledTimes(1)

await fetch(`http://127.0.0.1:${port}/testPost`, { method: 'POST' })

expect(mockHook).toHaveBeenCalledTimes(2)
})

it('should handle hooks before any request', async () => {
await fetch(`http://127.0.0.1:${port}/testGet`)

Expand Down
2 changes: 2 additions & 0 deletions packages/wobe/src/adapters/bun/bun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ describe.skipIf(process.env.NODE_TEST === 'true')('Bun server', () => {
})

it('should call create server from node:https if https options is not undefined', async () => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

const port = await getPort()

const key = await Bun.file(
Expand Down
34 changes: 34 additions & 0 deletions packages/wobe/src/router/RadixTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,40 @@ describe('RadixTree', () => {
).toBe(1)
})

it('should add a hook with path * on beforeHandler', () => {
const radixTree = new RadixTree()

radixTree.addRoute('GET', '/a/simple/route', () =>
Promise.resolve(),
)

radixTree.addHook(
'beforeHandler',
'*',
() => Promise.resolve(),
'GET',
)

expect(
radixTree.root.children[0].children[0].children[0].name,
).toBe('/route')
expect(
radixTree.root.children[0].children[0].children[0].method,
).toBe('GET')
expect(
radixTree.root.children[0].children[0].children[0].handler,
).toBeDefined()

expect(
radixTree.root.children[0].children[0].children[0]
.beforeHandlerHook?.length,
).toBe(1)
expect(
radixTree.root.children[0].children[0].children[0]
.afterHandlerHook,
).toBeUndefined()
})

it('should add a hook beforeHandler to the radix tree', () => {
const radixTree = new RadixTree()

Expand Down
24 changes: 24 additions & 0 deletions packages/wobe/src/router/RadixTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,36 @@ export class RadixTree {
const pathParts = path.split('/').filter(Boolean)
let currentNode = node || this.root

// For hooks with no specific path
if (path === '*') {
const addHookToChildren = (node: Node) => {
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i]

if (
child.children.length === 0 &&
(method === child.method || method === 'ALL')
)
this._addHookToNode(child, hook, handler)

addHookToChildren(child)
}
}

for (let i = 0; i < currentNode.children.length; i++) {
const child = currentNode.children[i]

addHookToChildren(child)
}
}

for (let i = 0; i < pathParts.length; i++) {
const pathPart = pathParts[i]
const isWildcardNode = pathPart[0] === '*'

if (isWildcardNode) {
const nextPathJoin = '/' + pathParts.slice(i + 1).join('/')

for (const child of currentNode.children) {
if (child.method === method || !child.method)
this.addHook(hook, nextPathJoin, handler, method, child)
Expand Down

0 comments on commit 420b04b

Please sign in to comment.