Fixed wildcard bugs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by Yusuke Wada · parent de6b5bd · Browse files · 5 files·+43 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+43 added−9 removed
- tests:+30 added−4 removed
- other:+13 added−5 removed
Provenance
- Commit
cdf31a5- Tree
bdf6d47- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
de6b5bd
5 files changed+43 added−9 removed
modified:example/basic/index.js+1 added-1 removed
11 unmodified lines121314151616171819208 unmodified lines11 unmodified lines}
// Mount middlewareapp.use('*', logger)app.use('/hello', addHeader)app.use('/hello/*', addHeader)
// Routingapp.get('/', () => new Response('Hono!!'))app.get('/hello', () => new Response('This is /hello'))8 unmodified linesmodified:src/hono.js+4 added-2 removed
2 unmodified lines345678910111253 unmodified lines6667686968707172737423 unmodified lines989910010110010210310410510635 unmodified lines2 unmodified linesconst Node = require('./node')const compose = require('./compose')const defaultFilter = require('./middleware/defaultFilter')
const METHOD_NAME_OF_ALL = 'ALL'
class Router { constructor() { this.node = new Node() this.tempPath = '/'53 unmodified lines }
use(path, middleware) { const router = new Router() router.add('all', path, middleware) router.add(METHOD_NAME_OF_ALL, path, middleware) this.middlewareRouters.push(router) }
async matchRoute(method, path) {23 unmodified lines
const middleware = [defaultFilter] // add defaultFilter later
for (const mr of this.middlewareRouters) { const mwResult = mr.match('all', path) const mwResult = mr.match(METHOD_NAME_OF_ALL, path) if (mwResult) { middleware.push(...mwResult.handler) } }35 unmodified linesmodified:src/hono.test.js+29 added-3 removed
60 unmodified lines61 62 describe('Middleware', () => {63 const app = Hono()64 65- const logger = (c, next) => {65+ const logger = async (c, next) => {66 console.log(`${c.req.method} : ${c.req.url}`)67 next()68 }69 70- const customHeader = (c, next) => {70+ const rootHeader = async (c, next) => {71 next()72- c.res.headers.append('x-message', 'custom-header')72+ await c.res.headers.append('x-custom', 'root')73 }74 75+ const customHeader = async (c, next) => {76+ next()77+ await c.res.headers.append('x-message', 'custom-header')78+ }79+ const customHeader2 = async (c, next) => {80+ next()81+ await c.res.headers.append('x-message-2', 'custom-header-2')82+ }83+84 app.use('*', logger)85+ app.use('*', rootHeader)86 app.use('/hello', customHeader)87+ app.use('/hello/*', customHeader2)88 app.get('/hello', () => {89 return new fetch.Response('hello')90 })91+ app.get('/hello/:message', (c) => {92+ const message = c.req.params('message')93+ return new fetch.Response(`${message}`)94+ })95 96 it('logging and custom header', async () => {97 let req = new fetch.Request('https://example.com/hello')98 let res = await app.dispatch(req)99 expect(res.status).toBe(200)100 expect(await res.text()).toBe('hello')101+ expect(await res.headers.get('x-custom')).toBe('root')102 expect(await res.headers.get('x-message')).toBe('custom-header')103+ expect(await res.headers.get('x-message-2')).toBe('custom-header-2')104 })105+106+ it('logging and custom header with named params', async () => {107+ let req = new fetch.Request('https://example.com/hello/message')108+ let res = await app.dispatch(req)109+ expect(res.status).toBe(200)110+ expect(await res.text()).toBe('message')111+ expect(await res.headers.get('x-custom')).toBe('root')112+ expect(await res.headers.get('x-message-2')).toBe('custom-header-2')113+ })114 })modified:src/node.js+8 added-2 removed
1 'use strict'2 3 const { splitPath, getPattern } = require('./util')4 5-const METHOD_NAME_OF_ALL = 'all'5+const METHOD_NAME_OF_ALL = 'ALL'6 7 const createResult = (handler, params) => {8 return { handler: handler, params: params }9 }7 unmodified lines17 this.method = {}18 if (method && handler) {19 this.method[method] = handler20 }21+ this.middlewares = []22 }23 24 Node.prototype.insert = function (method, path, handler) {25 let curNode = this24 unmodified lines50 curNode = nextNode51 continue52 }53 54+ let isWildcard = false55 let isParamMatch = false56 const keys = Object.keys(curNode.children)57 for (let j = 0; j < keys.length; j++) {58 const key = keys[j]59 // Wildcard60 if (key === '*') {61 curNode = curNode.children['*']60- isParamMatch = true62+ isWildcard = true63 break64 }65 const pattern = getPattern(key)66 if (pattern) {8 unmodified lines75 return noRoute()76 }77 }78 79+ if (isWildcard) {80+ break81+ }82+83 if (isParamMatch === false) {84 return noRoute()85 }86 }11 unmodified linesmodified:src/node.test.js+1 added-1 removed
121 unmodified lines122 })123 124 describe('All', () => {125 const node = new Node()126- node.insert('all', '/all-methods', 'all methods')126+ node.insert('ALL', '/all-methods', 'all methods') // ALL127 it('/all-methods', () => {128 res = node.search('get', '/all-methods')129 expect(res).not.toBeNull()130 expect(res.handler).toBe('all methods')5 unmodified lines