hono 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Yusuke Wada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ ## hono
2
+
3
+ ## Author
4
+
5
+ Yusuke Wada <https://github.com/yusukebe>
6
+
7
+ ## License
8
+
9
+ MIT
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "hono",
3
+ "version": "0.0.1",
4
+ "description": "Minimal web framework for Cloudflare Workers",
5
+ "main": "src/hono.js",
6
+ "scripts": {
7
+ "test": "jest --verbose"
8
+ },
9
+ "author": "Yusuke Wada <yusuke@kamawada.com> (https://github.com/yusukebe)",
10
+ "license": "MIT",
11
+ "devDependencies": {
12
+ "jest": "^27.4.5",
13
+ "node-fetch": "^2.6.6"
14
+ }
15
+ }
package/src/hono.js ADDED
@@ -0,0 +1,77 @@
1
+ const Router = require('./router')
2
+
3
+ class Route {
4
+ constructor(method, handler) {
5
+ this.method = method;
6
+ this.handler = handler;
7
+ }
8
+ }
9
+
10
+ class App {
11
+ constructor() {
12
+ this.router = new Router();
13
+ }
14
+
15
+ addRoute(method, path, handler) {
16
+ this.router.add(path, new Route(method, handler))
17
+ }
18
+
19
+ handle(event) {
20
+ const response = this.dispatch(event.request)
21
+ return event.respondWith(response)
22
+ }
23
+
24
+ dispatch(request) {
25
+ const url = new URL(request.url)
26
+ const path = url.pathname
27
+ const match = this.router.match(path)
28
+
29
+ if (!match) {
30
+ return this.notFound()
31
+ }
32
+
33
+ const method = request.method.toLowerCase()
34
+ const route = match[0]
35
+ if (route.method == method) {
36
+ const handler = route.handler
37
+ return handler(request)
38
+ }
39
+ return this.notFound()
40
+ }
41
+
42
+ notFound() {
43
+ return new Response('Not Found', {
44
+ status: 404,
45
+ headers: {
46
+ 'content-type': 'text/plain'
47
+ }
48
+ })
49
+ }
50
+
51
+ fire() {
52
+ addEventListener("fetch", (event) => {
53
+ this.handle(event)
54
+ })
55
+ }
56
+ }
57
+
58
+ const proxyHandler = {
59
+ get: (target, prop) => (...args) => {
60
+ if (target.constructor.prototype.hasOwnProperty(prop)) {
61
+ return target[prop](args[0])
62
+ } else {
63
+ target.addRoute(prop, args[0], args[1])
64
+ return
65
+ }
66
+ }
67
+ }
68
+
69
+ const app = new App()
70
+
71
+ function Hono() {
72
+ return new Proxy(
73
+ app, proxyHandler
74
+ )
75
+ }
76
+
77
+ module.exports = Hono
@@ -0,0 +1,29 @@
1
+ const Hono = require('./hono')
2
+ const fetch = require('node-fetch')
3
+
4
+ const app = new Hono()
5
+
6
+ describe('GET match', () => {
7
+ app.get('/hello', () => {
8
+ return new fetch.Response('hello', {
9
+ status: 200
10
+ })
11
+ })
12
+ app.notFound = () => {
13
+ return new fetch.Response('not found', {
14
+ status: 404
15
+ })
16
+ }
17
+ it('GET /hello is ok', () => {
18
+ let req = new fetch.Request('https://example.com/hello')
19
+ let res = app.dispatch(req)
20
+ expect(res).not.toBeNull()
21
+ expect(res.status).toBe(200)
22
+ })
23
+ it('GET / is not found', () => {
24
+ let req = new fetch.Request('https://example.com/')
25
+ let res = app.dispatch(req)
26
+ expect(res).not.toBeNull()
27
+ expect(res.status).toBe(404)
28
+ })
29
+ })
package/src/router.js ADDED
@@ -0,0 +1,116 @@
1
+ // Ref: https://github.com/bmf-san/goblin
2
+
3
+ class Router {
4
+ constructor() {
5
+ this.node = new Node({ label: "/" })
6
+ }
7
+ add(path, stuff) {
8
+ this.node.insert(path, stuff);
9
+ }
10
+ match(path) {
11
+ return this.node.search(path);
12
+ }
13
+ }
14
+
15
+ class Node {
16
+ constructor({ label, stuff, children } = {}) {
17
+ this.label = label || "";
18
+ this.stuff = stuff || {};
19
+ this.children = children || [];
20
+ }
21
+
22
+ insert(path, stuff) {
23
+ let curNode = this
24
+ if (path == '/') {
25
+ curNode.label = path
26
+ curNode.stuff = stuff
27
+ }
28
+ const ps = this.splitPath(path)
29
+ for (const p of ps) {
30
+ let nextNode = curNode.children[p]
31
+ if (nextNode) {
32
+ curNode = nextNode
33
+ } else {
34
+ curNode.children[p] = new Node({ label: p, stuff: stuff, children: [] })
35
+ curNode = curNode.children[p]
36
+ }
37
+ }
38
+ }
39
+
40
+ splitPath(path) {
41
+ const ps = []
42
+ for (const p of path.split('/')) {
43
+ if (p) {
44
+ ps.push(p)
45
+ }
46
+ }
47
+ return ps
48
+ }
49
+
50
+ getPattern(label) {
51
+ // :id{[0-9]+} → [0-9]+$
52
+ // :id → (.+)
53
+ const match = label.match(/^\:.+?\{(.+)\}$/)
54
+ if (match) {
55
+ return '(' + match[1] + ')'
56
+ }
57
+ return '(.+)'
58
+ }
59
+
60
+ getParamName(label) {
61
+ const match = label.match(/^\:([^\{\}]+)/)
62
+ if (match) {
63
+ return match[1]
64
+ }
65
+ }
66
+
67
+ noRoute() {
68
+ return null
69
+ }
70
+
71
+ search(path) {
72
+
73
+ let curNode = this
74
+ const params = {}
75
+
76
+ for (const p of this.splitPath(path)) {
77
+ const nextNode = curNode.children[p]
78
+ if (nextNode) {
79
+ curNode = nextNode
80
+ continue
81
+ }
82
+ if (Object.keys(curNode.children).length == 0) {
83
+ if (curNode.label != p) {
84
+ return this.noRoute()
85
+ }
86
+ break
87
+ }
88
+ let isParamMatch = false
89
+ for (const key in curNode.children) {
90
+ if (key == "*") { // Wildcard
91
+ curNode = curNode.children[key]
92
+ isParamMatch = true
93
+ break
94
+ }
95
+ if (key.match(/^:/)) {
96
+ const pattern = this.getPattern(key)
97
+ const match = p.match(new RegExp(pattern))
98
+ if (match) {
99
+ const k = this.getParamName(key)
100
+ params[k] = match[0]
101
+ curNode = curNode.children[key]
102
+ isParamMatch = true
103
+ break
104
+ }
105
+ return this.noRoute()
106
+ }
107
+ }
108
+ if (isParamMatch == false) {
109
+ return this.noRoute()
110
+ }
111
+ }
112
+ return [curNode.stuff, params]
113
+ }
114
+ }
115
+
116
+ module.exports = Router
@@ -0,0 +1,66 @@
1
+ const Router = require('./router')
2
+
3
+ let router = new Router()
4
+
5
+ describe('root match', () => {
6
+ it('/ match', () => {
7
+ router.add('/', 'root')
8
+ let match = router.match('/')
9
+ expect(match).not.toBeNull()
10
+ expect(match[0]).toBe('root')
11
+ match = router.match('/foo')
12
+ expect(match).toBeNull()
13
+ })
14
+
15
+ it('/hello match', () => {
16
+ router.add('/hello', 'hello')
17
+ match = router.match('/foo')
18
+ expect(match).toBeNull()
19
+ match = router.match('/hello')
20
+ expect(match[0]).toBe('hello')
21
+ })
22
+ })
23
+
24
+ describe('path match', () => {
25
+ router.add('/entry/:id', 'entry-id')
26
+ router.add('/entry/:id/:comment', 'entry-id-comment')
27
+ router.add('/year/:year{[0-9]{4}}/:month{[0-9]{2}}', 'date-regex')
28
+
29
+ it('entry id match', () => {
30
+ const match = router.match('/entry/123')
31
+ expect(match[0]).toBe('entry-id')
32
+ expect(match[1]['id']).toBe('123')
33
+ })
34
+
35
+ it('entry id and comment match', () => {
36
+ const match = router.match('/entry/123/45678')
37
+ expect(match[0]).toBe('entry-id-comment')
38
+ expect(match[1]['id']).toBe('123')
39
+ expect(match[1]['comment']).toBe('45678')
40
+ })
41
+
42
+ it('date-regex', () => {
43
+ const match = router.match('/year/2021/12')
44
+ expect(match[0]).toBe('date-regex')
45
+ expect(match[1]['year']).toBe('2021')
46
+ expect(match[1]['month']).toBe('12')
47
+ })
48
+
49
+ it('not match', () => {
50
+ let match = router.match('/foo')
51
+ expect(match).toBeNull()
52
+ match = router.match('/year/abc')
53
+ expect(match).toBeNull()
54
+ })
55
+ })
56
+
57
+ describe('wildcard', () => {
58
+ it('match', () => {
59
+ router = new Router()
60
+ router.add('/abc/*/def')
61
+ let match = router.match('/abc/xxx/def')
62
+ expect(match).not.toBeNull()
63
+ match = router.match('/abc/xxx/abc')
64
+ expect(match).toBeNull()
65
+ })
66
+ })