Skip to content
Myr

Use proxy handler

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 21f682d · Browse files · 3 files+22 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+22 added−27 removed

  • tests:+1 added−1 removed
  • other:+21 added−26 removed

3 files+22 added−27 removed

modified:benchmark/handleEvent/index.js+1 added-1 removed
modified:src/hono.js+20 added-25 removed
23 unmodified lines
24 const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/)25 return match[5]26}2728const proxyHandler = {29 get:30 (target, prop) =>31 (...args) => {32 if (target.constructor.prototype.hasOwnProperty(prop)) {33 return target[prop](...args)34 } else {35 return target.addRoute(prop, ...args)36 }37 },38}3940class Hono {41 constructor() {42 this.router = new Router()43 this.middlewareRouter = new Router()44 this.middlewareRouters = []45 }4635 get(...args) {36 return this.addRoute('GET', ...args)47 getRouter() {48 return this.router49 }38 post(...args) {39 return this.addRoute('POST', ...args)40 }41 put(...args) {42 return this.addRoute('PUT', ...args)43 }44 delete(...args) {45 return this.addRoute('DELETE', ...args)46 }47 patch(...args) {48 return this.addRoute('PATCH', ...args)49 }5051 addRoute(method, ...args) {52 method = method.toUpperCase()53 if (args.length === 1) {54 this.router.add(method, this.router.tempPath, ...args)55 } else {56 this.router.add(method, ...args)57 }58 return this58 return WrappedApp(this)59 }6061 getRouter() {62 return this.router63 }6461 route(path) {62 this.router.tempPath = path67 return this63 return WrappedApp(this)64 }6566 use(path, middleware) {67 const router = new Router()
1 unmodified line
69 this.middlewareRouters.push(router)70 }7172 async matchRoute(method, path) {77 const res = this.router.match(method, path)78 return res73 return this.router.match(method, path)74 }7576 // XXX77 async createContext(req, res) {
53 unmodified lines
131 return new Response('Not Found', { status: 404 })132 }133}134140const CreateApp = () => {141 return new Hono()135const WrappedApp = (hono = new Hono()) => {136 return new Proxy(hono, proxyHandler)137}138144module.exports = CreateApp139module.exports = WrappedApp
modified:src/util.test.js+1 added-1 removed