Merge pull request #10 from yusukebe/feature/dont-use-proxy
Before adoption: this commit came in with the history the project adopted, so no landing records it.
New API
authored by Yusuke Wada · committed by GitHub · parents 16350cb, 3198fa0 · Browse files · 12 files·+116 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
12 files+116 added−38 removed
- tests:+9 added−8 removed
- other:+107 added−30 removed
Provenance
- Commit
bc34513- Tree
98087dd- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent 1
16350cb- Parent 2
3198fa0
12 files changed+116 added−38 removed
modified:benchmark/handleEvent/index.js+2 added-2 removed
1233456789101011121314106 unmodified linesExpand allimport Benchmark from 'benchmark'import { makeEdgeEnv } from 'edge-mock'import Hono from '../../src/hono.js'import { Hono } from '../../src/hono.js'import itty from 'itty-router'const { Router: IttyRouter } = ittyimport { Router as SunderRouter, Sunder } from 'sunder'
makeEdgeEnv()
const hono = Hono()const hono = new Hono()hono.get('/user', () => new Response('User'))hono.get('/user/comments', () => new Response('User Comments'))hono.get('/user/avatar', () => new Response('User Avatar'))hono.get('/user/lookup/username/:username', () => new Response('User Lookup Username'))106 unmodified linesExpand allmodified:benchmark/webapp/hono/index.js+2 added-2 removed
11233456717 unmodified linesconst Hono = require('../../../src/hono')const { Hono } = require('../../../src/hono')
const hono = Hono()const hono = new Hono()hono.get('/user', () => new Response('User'))hono.get('/user/comments', () => new Response('User Comments'))hono.get('/user/avatar', () => new Response('User Avatar'))hono.get('/user/lookup/username/:username', () => new Response('User Lookup Username'))17 unmodified linesmodified:example/basic/index.js+2 added-2 removed
1212345622 unmodified linesconst Hono = require('../../src/hono')const app = Hono()const { Hono } = require('../../src/hono')const app = new Hono()
// Middlewareconst logger = (c, next) => { console.log(`[${c.req.method}] ${c.req.url}`)22 unmodified linesmodified:example/basic/README.md+2 added-2 removed
112345678891011# Hono example for cloudflare worker# Hono example for Cloudflare Workers
Before you can start publishing your service to cloudflare worker, you must sign up for a Cloudflare Workers account first, you can check out this [document](https://developers.cloudflare.com/workers/get-started/guide)
You can update the information (`name`, `zoon_id`, etc) in wrangler file, then you can test and deploy your service by simply doing,
```sh$ npm install $ npm install$ npm run dev # Start a local server for developing your worker$ npm run publish # Publish your worker to the orange cloud```modified:example/compute-at-edge/index.js+2 added-2 removed
1-const Hono = require('hono')2-const app = Hono()1+const { Hono } = require('hono')2+const app = new Hono()3 4 app.use('*', (c, next) => {5 console.log(`[${c.req.method}] ${c.req.url}`)6 next()11 unmodified linesadded:src/hono.d.ts+42 added
1+declare class FetchEvent {}2+declare class Request {}3+declare class Response {}4+declare class Context {}5+6+declare class Node {}7+8+declare class Router {9+ tempPath: string10+ node: Node11+12+ add(method: string, path: string, handlers: any[]): Node13+ match(method: string, path: string): Node14+}15+16+export class Hono {17+ router: Router18+ middlewareRouters: Router[]19+20+ getRouter(): Router21+ addRoute(method: string, args: any[]): Hono22+ matchRoute(method: string, path: string): Node23+ createContext(req: Request, res: Response): Context24+ dispatch(req: Request, res: Response): Response25+ handleEvent(event: FetchEvent): Response26+ fire(): void27+28+ notFound(): Response29+30+ route(path: string): Hono31+32+ use(path: string, middleware: any): void33+34+ all(path: string, handler: any): Hono35+ get(path: string, handler: any): Hono36+ post(path: string, handler: any): Hono37+ put(path: string, handler: any): Hono38+ head(path: string, handler: any): Hono39+ delete(path: string, handler: any): Hono40+}41+42+export class Middleware {}modified:src/hono.js+20 added-18 removed
1 'use strict'2 3 const Node = require('./node')4 const compose = require('./compose')5+const methods = require('./methods')6 const defaultFilter = require('./middleware/defaultFilter')7+const Middleware = require('./middleware')8 9 const METHOD_NAME_OF_ALL = 'ALL'10 11 class Router {16 unmodified lines28 const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/)29 return match[5]30 }31 30-const proxyHandler = {31- get:32- (target, prop) =>33- (...args) => {34- if (target.constructor.prototype.hasOwnProperty(prop)) {35- return target[prop](...args)36- } else {37- return target.addRoute(prop, ...args)38- }39- },40-}41-32 class Hono {33 constructor() {34 this.router = new Router()35 this.middlewareRouters = []36+37+ for (const method of methods) {38+ this[method] = (...args) => {39+ return this.addRoute(method, ...args)40+ }41+ }42 }43 44+ all(...args) {45+ this.addRoute('ALL', ...args)46+ }47+48 getRouter() {49 return this.router50 }51 3 unmodified lines55 this.router.add(method, this.router.tempPath, ...args)56 } else {57 this.router.add(method, ...args)58 }59- return WrappedApp(this)59+ return this60 }61 62 route(path) {63 this.router.tempPath = path64- return WrappedApp(this)64+ return this65 }66 67 use(path, middleware) {68 const router = new Router()63 unmodified lines132 return new Response('Not Found', { status: 404 })133 }134 }135 136-const WrappedApp = (hono = new Hono()) => {137- return new Proxy(hono, proxyHandler)138-}136+// Default Export137+module.exports = Hono138+exports = module.exports139 140-module.exports = WrappedApp140+// Named Export141+exports.Hono = Hono142+exports.Middleware = Middlewaremodified:src/hono.test.js+5 added-4 removed
1-const Hono = require('./hono')1 const fetch = require('node-fetch')2+const { Hono } = require('./hono')3 4 describe('GET Request', () => {5- const app = Hono()5+ const app = new Hono()6+7 app.get('/hello', () => {8 return new fetch.Response('hello', {9 status: 200,10 })18 unmodified lines29 })30 })31 32 describe('params and query', () => {32- const app = Hono()33+ const app = new Hono()34 35 app.get('/entry/:id', async (c) => {36 const id = await c.req.params('id')37 return new fetch.Response(`id is ${id}`, {22 unmodified lines60 })61 })62 63 describe('Middleware', () => {63- const app = Hono()64+ const app = new Hono()65 66 const logger = async (c, next) => {67 console.log(`${c.req.method} : ${c.req.url}`)68 next()47 unmodified linesadded:src/methods.js+30 added
1+const methods = [2+ 'get',3+ 'post',4+ 'put',5+ 'head',6+ 'delete',7+ 'options',8+ 'trace',9+ 'copy',10+ 'lock',11+ 'mkcol',12+ 'move',13+ 'patch',14+ 'purge',15+ 'propfind',16+ 'proppatch',17+ 'unlock',18+ 'report',19+ 'mkactivity',20+ 'checkout',21+ 'merge',22+ 'm-search',23+ 'notify',24+ 'subscribe',25+ 'unsubscribe',26+ 'search',27+ 'connect',28+]29+30+module.exports = methodsadded:src/middleware.js+3 added
1+class Middleware {}2+3+module.exports = Middlewaremodified:src/router.test.js+4 added-4 removed
1-const App = require('./hono')1+const { Hono } = require('./hono')2 3 describe('Basic Usage', () => {4- const router = App()4+ const router = new Hono()5 6 it('get, post hello', async () => {7 router.get('/hello', 'get hello')8 router.post('/hello', 'post hello')14 unmodified lines23 })24 })25 26 describe('Complex', () => {27- let router = App()27+ let router = new Hono()28 29 it('Named Param', async () => {30 router.get('/entry/:id', 'get entry')31 res = await router.matchRoute('GET', '/entry/123')23 unmodified lines55 })56 })57 58 describe('Chained Route', () => {59- let router = App()59+ let router = new Hono()60 61 it('Return rooter object', async () => {62 router = router.patch('/hello', 'patch hello')63 expect(router).not.toBeNull()25 unmodified linesmodified:README.md+2 added-2 removed
1 unmodified line2 3 Hono [炎] - Tiny web framework for Cloudflare Workers and others.4 5 ```js6-const Hono = require('hono')7-const app = Hono()6+const { Hono } = require('hono')7+const app = new Hono()8 9 app.get('/', () => new Response('Hono!!'))10 11 app.fire()166 unmodified lines