Comparing version 0.0.5 to 0.0.6
{ | ||
"name": "hono", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Minimal web framework for Cloudflare Workers", | ||
@@ -20,2 +20,2 @@ "main": "src/hono.js", | ||
} | ||
} | ||
} |
@@ -27,3 +27,3 @@ # Hono | ||
itty-router x 160,415 ops/sec ±3.31% (85 runs sampled) | ||
sundar x 307,438 ops/sec ±4.77% (73 runs sampled) | ||
sunder x 307,438 ops/sec ±4.77% (73 runs sampled) | ||
Fastest is hono | ||
@@ -30,0 +30,0 @@ ✨ Done in 37.03s. |
@@ -7,2 +7,4 @@ 'use strict' | ||
const METHOD_NAME_OF_ALL = 'ALL' | ||
class Router { | ||
@@ -29,2 +31,14 @@ constructor() { | ||
const proxyHandler = { | ||
get: | ||
(target, prop) => | ||
(...args) => { | ||
if (target.constructor.prototype.hasOwnProperty(prop)) { | ||
return target[prop](...args) | ||
} else { | ||
return target.addRoute(prop, ...args) | ||
} | ||
}, | ||
} | ||
class Hono { | ||
@@ -37,17 +51,5 @@ constructor() { | ||
get(...args) { | ||
return this.addRoute('GET', ...args) | ||
getRouter() { | ||
return this.router | ||
} | ||
post(...args) { | ||
return this.addRoute('POST', ...args) | ||
} | ||
put(...args) { | ||
return this.addRoute('PUT', ...args) | ||
} | ||
delete(...args) { | ||
return this.addRoute('DELETE', ...args) | ||
} | ||
patch(...args) { | ||
return this.addRoute('PATCH', ...args) | ||
} | ||
@@ -61,12 +63,8 @@ addRoute(method, ...args) { | ||
} | ||
return this | ||
return WrappedApp(this) | ||
} | ||
getRouter() { | ||
return this.router | ||
} | ||
route(path) { | ||
this.router.tempPath = path | ||
return this | ||
return WrappedApp(this) | ||
} | ||
@@ -76,3 +74,3 @@ | ||
const router = new Router() | ||
router.add('all', path, middleware) | ||
router.add(METHOD_NAME_OF_ALL, path, middleware) | ||
this.middlewareRouters.push(router) | ||
@@ -82,4 +80,3 @@ } | ||
async matchRoute(method, path) { | ||
const res = this.router.match(method, path) | ||
return res | ||
return this.router.match(method, path) | ||
} | ||
@@ -111,3 +108,3 @@ | ||
for (const mr of this.middlewareRouters) { | ||
const mwResult = mr.match('all', path) | ||
const mwResult = mr.match(METHOD_NAME_OF_ALL, path) | ||
if (mwResult) { | ||
@@ -147,6 +144,6 @@ middleware.push(...mwResult.handler) | ||
const CreateApp = () => { | ||
return new Hono() | ||
const WrappedApp = (hono = new Hono()) => { | ||
return new Proxy(hono, proxyHandler) | ||
} | ||
module.exports = CreateApp | ||
module.exports = WrappedApp |
@@ -65,3 +65,3 @@ const Hono = require('./hono') | ||
const logger = (c, next) => { | ||
const logger = async (c, next) => { | ||
console.log(`${c.req.method} : ${c.req.url}`) | ||
@@ -71,12 +71,27 @@ next() | ||
const customHeader = (c, next) => { | ||
const rootHeader = async (c, next) => { | ||
next() | ||
c.res.headers.append('x-message', 'custom-header') | ||
await c.res.headers.append('x-custom', 'root') | ||
} | ||
const customHeader = async (c, next) => { | ||
next() | ||
await c.res.headers.append('x-message', 'custom-header') | ||
} | ||
const customHeader2 = async (c, next) => { | ||
next() | ||
await c.res.headers.append('x-message-2', 'custom-header-2') | ||
} | ||
app.use('*', logger) | ||
app.use('*', rootHeader) | ||
app.use('/hello', customHeader) | ||
app.use('/hello/*', customHeader2) | ||
app.get('/hello', () => { | ||
return new fetch.Response('hello') | ||
}) | ||
app.get('/hello/:message', (c) => { | ||
const message = c.req.params('message') | ||
return new fetch.Response(`${message}`) | ||
}) | ||
@@ -88,4 +103,15 @@ it('logging and custom header', async () => { | ||
expect(await res.text()).toBe('hello') | ||
expect(await res.headers.get('x-custom')).toBe('root') | ||
expect(await res.headers.get('x-message')).toBe('custom-header') | ||
expect(await res.headers.get('x-message-2')).toBe('custom-header-2') | ||
}) | ||
it('logging and custom header with named params', async () => { | ||
let req = new fetch.Request('https://example.com/hello/message') | ||
let res = await app.dispatch(req) | ||
expect(res.status).toBe(200) | ||
expect(await res.text()).toBe('message') | ||
expect(await res.headers.get('x-custom')).toBe('root') | ||
expect(await res.headers.get('x-message-2')).toBe('custom-header-2') | ||
}) | ||
}) |
@@ -5,3 +5,3 @@ 'use strict' | ||
const METHOD_NAME_OF_ALL = 'all' | ||
const METHOD_NAME_OF_ALL = 'ALL' | ||
@@ -22,2 +22,3 @@ const createResult = (handler, params) => { | ||
} | ||
this.middlewares = [] | ||
} | ||
@@ -55,2 +56,3 @@ | ||
let isWildcard = false | ||
let isParamMatch = false | ||
@@ -63,3 +65,3 @@ const keys = Object.keys(curNode.children) | ||
curNode = curNode.children['*'] | ||
isParamMatch = true | ||
isWildcard = true | ||
break | ||
@@ -81,2 +83,6 @@ } | ||
if (isWildcard) { | ||
break | ||
} | ||
if (isParamMatch === false) { | ||
@@ -83,0 +89,0 @@ return noRoute() |
@@ -126,3 +126,3 @@ const Node = require('./node') | ||
const node = new Node() | ||
node.insert('all', '/all-methods', 'all methods') | ||
node.insert('ALL', '/all-methods', 'all methods') // ALL | ||
it('/all-methods', () => { | ||
@@ -129,0 +129,0 @@ res = node.search('get', '/all-methods') |
@@ -1,2 +0,2 @@ | ||
const { splitPath, getPattern, getParamName } = require('./util') | ||
const { splitPath, getPattern } = require('./util') | ||
@@ -3,0 +3,0 @@ describe('Utility methods', () => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22865
612
14