hono 0.0.10 → 0.0.14

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.
Files changed (43) hide show
  1. package/README.md +212 -75
  2. package/dist/compose.d.ts +1 -0
  3. package/dist/compose.js +27 -0
  4. package/dist/context.d.ts +23 -0
  5. package/dist/context.js +65 -0
  6. package/dist/hono.d.ts +44 -0
  7. package/dist/hono.js +147 -0
  8. package/dist/index.d.ts +5 -0
  9. package/dist/index.js +9 -0
  10. package/dist/middleware/basic-auth/basic-auth.d.ts +6 -0
  11. package/dist/middleware/basic-auth/basic-auth.js +50 -0
  12. package/dist/middleware/body-parse/body-parse.d.ts +2 -0
  13. package/dist/middleware/body-parse/body-parse.js +27 -0
  14. package/dist/middleware/default.d.ts +2 -0
  15. package/dist/middleware/default.js +16 -0
  16. package/dist/middleware/logger/logger.d.ts +6 -0
  17. package/dist/middleware/logger/logger.js +58 -0
  18. package/dist/middleware/powered-by/powered-by.d.ts +2 -0
  19. package/dist/middleware/powered-by/powered-by.js +11 -0
  20. package/dist/middleware.d.ts +15 -0
  21. package/dist/middleware.js +16 -0
  22. package/dist/node.d.ts +24 -0
  23. package/dist/node.js +104 -0
  24. package/dist/util.d.ts +5 -0
  25. package/dist/util.js +72 -0
  26. package/package.json +34 -5
  27. package/CODE_OF_CONDUCT.md +0 -128
  28. package/src/compose.js +0 -21
  29. package/src/compose.test.js +0 -42
  30. package/src/hono.d.ts +0 -67
  31. package/src/hono.js +0 -141
  32. package/src/hono.test.js +0 -158
  33. package/src/methods.js +0 -30
  34. package/src/middleware/defaultFilter.js +0 -19
  35. package/src/middleware/poweredBy.js +0 -6
  36. package/src/middleware/poweredBy.test.js +0 -17
  37. package/src/middleware.js +0 -9
  38. package/src/middleware.test.js +0 -17
  39. package/src/node.js +0 -97
  40. package/src/node.test.js +0 -135
  41. package/src/router.test.js +0 -88
  42. package/src/util.js +0 -33
  43. package/src/util.test.js +0 -44
package/src/node.js DELETED
@@ -1,97 +0,0 @@
1
- 'use strict'
2
-
3
- const { splitPath, getPattern } = require('./util')
4
-
5
- const METHOD_NAME_OF_ALL = 'ALL'
6
-
7
- const createResult = (handler, params) => {
8
- return { handler: handler, params: params }
9
- }
10
-
11
- const noRoute = () => {
12
- return null
13
- }
14
-
15
- function Node(method, handler, children) {
16
- this.children = children || {}
17
- this.method = {}
18
- if (method && handler) {
19
- this.method[method] = handler
20
- }
21
- this.middlewares = []
22
- }
23
-
24
- Node.prototype.insert = function (method, path, handler) {
25
- let curNode = this
26
- const parts = splitPath(path)
27
- for (let i = 0; i < parts.length; i++) {
28
- const p = parts[i]
29
- if (Object.keys(curNode.children).includes(p)) {
30
- curNode = curNode.children[p]
31
- continue
32
- }
33
- curNode.children[p] = new Node(method, handler)
34
- curNode = curNode.children[p]
35
- }
36
- curNode.method[method] = handler
37
- return curNode
38
- }
39
-
40
- Node.prototype.search = function (method, path) {
41
- let curNode = this
42
-
43
- const params = {}
44
- const parts = splitPath(path)
45
-
46
- for (let i = 0; i < parts.length; i++) {
47
- const p = parts[i]
48
- const nextNode = curNode.children[p]
49
- if (nextNode) {
50
- curNode = nextNode
51
- continue
52
- }
53
-
54
- let isWildcard = false
55
- let isParamMatch = false
56
- const keys = Object.keys(curNode.children)
57
- for (let j = 0; j < keys.length; j++) {
58
- const key = keys[j]
59
- // Wildcard
60
- if (key === '*') {
61
- curNode = curNode.children['*']
62
- isWildcard = true
63
- break
64
- }
65
- const pattern = getPattern(key)
66
- if (pattern) {
67
- const match = p.match(new RegExp(pattern[1]))
68
- if (match) {
69
- const k = pattern[0]
70
- params[k] = match[1]
71
- curNode = curNode.children[key]
72
- isParamMatch = true
73
- break
74
- }
75
- return noRoute()
76
- }
77
- }
78
-
79
- if (isWildcard) {
80
- break
81
- }
82
-
83
- if (isParamMatch === false) {
84
- return noRoute()
85
- }
86
- }
87
-
88
- const handler = curNode.method[METHOD_NAME_OF_ALL] || curNode.method[method]
89
-
90
- if (!handler) {
91
- return noRoute()
92
- }
93
-
94
- return createResult(handler, params)
95
- }
96
-
97
- module.exports = Node
package/src/node.test.js DELETED
@@ -1,135 +0,0 @@
1
- const Node = require('./node')
2
-
3
- describe('Root Node', () => {
4
- const node = new Node()
5
- node.insert('get', '/', 'get root')
6
- it('get /', () => {
7
- let res = node.search('get', '/')
8
- expect(res).not.toBeNull()
9
- expect(res.handler).toBe('get root')
10
- expect(node.search('get', '/hello')).toBeNull()
11
- })
12
- })
13
-
14
- describe('Root Node id not defined', () => {
15
- const node = new Node()
16
- node.insert('get', '/hello', 'get hello')
17
- it('get /', () => {
18
- expect(node.search('get', '/')).toBeNull()
19
- })
20
- })
21
-
22
- describe('All with *', () => {
23
- const node = new Node()
24
- node.insert('get', '*', 'get all')
25
- it('get /', () => {
26
- expect(node.search('get', '/')).not.toBeNull()
27
- expect(node.search('get', '/hello')).not.toBeNull()
28
- })
29
- })
30
-
31
- describe('Basic Usage', () => {
32
- const node = new Node()
33
- node.insert('get', '/hello', 'get hello')
34
- node.insert('post', '/hello', 'post hello')
35
- node.insert('get', '/hello/foo', 'get hello foo')
36
-
37
- it('get, post /hello', () => {
38
- expect(node.search('get', '/')).toBeNull()
39
- expect(node.search('post', '/')).toBeNull()
40
-
41
- expect(node.search('get', '/hello').handler).toBe('get hello')
42
- expect(node.search('post', '/hello').handler).toBe('post hello')
43
- expect(node.search('put', '/hello')).toBeNull()
44
- })
45
- it('get /nothing', () => {
46
- expect(node.search('get', '/nothing')).toBeNull()
47
- })
48
- it('/hello/foo, /hello/bar', () => {
49
- expect(node.search('get', '/hello/foo').handler).toBe('get hello foo')
50
- expect(node.search('post', '/hello/foo')).toBeNull()
51
- expect(node.search('get', '/hello/bar')).toBeNull()
52
- })
53
- it('/hello/foo/bar', () => {
54
- expect(node.search('get', '/hello/foo/bar')).toBeNull()
55
- })
56
- })
57
-
58
- describe('Name path', () => {
59
- const node = new Node()
60
- it('get /entry/123', () => {
61
- node.insert('get', '/entry/:id', 'get entry')
62
- let res = node.search('get', '/entry/123')
63
- expect(res).not.toBeNull()
64
- expect(res.handler).toBe('get entry')
65
- expect(res.params).not.toBeNull()
66
- expect(res.params['id']).toBe('123')
67
- expect(res.params['id']).not.toBe('1234')
68
- })
69
-
70
- it('get /entry/456/comment', () => {
71
- node.insert('get', '/entry/:id', 'get entry')
72
- let res = node.search('get', '/entry/456/comment')
73
- expect(res).toBeNull()
74
- })
75
-
76
- it('get /entry/789/comment/123', () => {
77
- node.insert('get', '/entry/:id/comment/:comment_id', 'get comment')
78
- let res = node.search('get', '/entry/789/comment/123')
79
- expect(res).not.toBeNull()
80
- expect(res.handler).toBe('get comment')
81
- expect(res.params['id']).toBe('789')
82
- expect(res.params['comment_id']).toBe('123')
83
- })
84
-
85
- it('get /map/:location/events', () => {
86
- node.insert('get', '/map/:location/events', 'get events')
87
- let res = node.search('get', '/map/yokohama/events')
88
- expect(res).not.toBeNull()
89
- expect(res.handler).toBe('get events')
90
- expect(res.params['location']).toBe('yokohama')
91
- })
92
- })
93
-
94
- describe('Wildcard', () => {
95
- const node = new Node()
96
- it('/wildcard-abc/xxxxxx/wildcard-efg', () => {
97
- node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard')
98
- let res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg')
99
- expect(res).not.toBeNull()
100
- expect(res.handler).toBe('wildcard')
101
- })
102
- })
103
-
104
- describe('Regexp', () => {
105
- const node = new Node()
106
- node.insert('get', '/regex-abc/:id{[0-9]+}/comment/:comment_id{[a-z]+}', 'regexp')
107
- it('/regexp-abc/123/comment/abc', () => {
108
- res = node.search('get', '/regex-abc/123/comment/abc')
109
- expect(res).not.toBeNull()
110
- expect(res.handler).toBe('regexp')
111
- expect(res.params['id']).toBe('123')
112
- expect(res.params['comment_id']).toBe('abc')
113
- })
114
- it('/regexp-abc/abc', () => {
115
- res = node.search('get', '/regex-abc/abc')
116
- expect(res).toBeNull()
117
- })
118
- it('/regexp-abc/123/comment/123', () => {
119
- res = node.search('get', '/regex-abc/123/comment/123')
120
- expect(res).toBeNull()
121
- })
122
- })
123
-
124
- describe('All', () => {
125
- const node = new Node()
126
- node.insert('ALL', '/all-methods', 'all methods') // ALL
127
- it('/all-methods', () => {
128
- res = node.search('get', '/all-methods')
129
- expect(res).not.toBeNull()
130
- expect(res.handler).toBe('all methods')
131
- res = node.search('put', '/all-methods')
132
- expect(res).not.toBeNull()
133
- expect(res.handler).toBe('all methods')
134
- })
135
- })
@@ -1,88 +0,0 @@
1
- const { Hono } = require('./hono')
2
-
3
- describe('Basic Usage', () => {
4
- const router = new Hono()
5
-
6
- it('get, post hello', async () => {
7
- router.get('/hello', 'get hello')
8
- router.post('/hello', 'post hello')
9
-
10
- let res = await router.matchRoute('GET', '/hello')
11
- expect(res).not.toBeNull()
12
- expect(res.handler[0]).toBe('get hello')
13
-
14
- res = await router.matchRoute('POST', '/hello')
15
- expect(res).not.toBeNull()
16
- expect(res.handler[0]).toBe('post hello')
17
-
18
- res = await router.matchRoute('PUT', '/hello')
19
- expect(res).toBeNull()
20
-
21
- res = await router.matchRoute('GET', '/')
22
- expect(res).toBeNull()
23
- })
24
- })
25
-
26
- describe('Complex', () => {
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')
32
- expect(res).not.toBeNull()
33
- expect(res.handler[0]).toBe('get entry')
34
- expect(res.params['id']).toBe('123')
35
- })
36
-
37
- it('Wildcard', async () => {
38
- router.get('/wild/*/card', 'get wildcard')
39
- res = await router.matchRoute('GET', '/wild/xxx/card')
40
- expect(res).not.toBeNull()
41
- expect(res.handler[0]).toBe('get wildcard')
42
- })
43
-
44
- it('Regexp', async () => {
45
- router.get('/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')
46
- res = await router.matchRoute('GET', '/post/20210101/hello')
47
- expect(res).not.toBeNull()
48
- expect(res.handler[0]).toBe('get post')
49
- expect(res.params['date']).toBe('20210101')
50
- expect(res.params['title']).toBe('hello')
51
- res = await router.matchRoute('GET', '/post/onetwothree')
52
- expect(res).toBeNull()
53
- res = await router.matchRoute('GET', '/post/123/123')
54
- expect(res).toBeNull()
55
- })
56
- })
57
-
58
- describe('Chained Route', () => {
59
- let router = new Hono()
60
-
61
- it('Return rooter object', async () => {
62
- router = router.patch('/hello', 'patch hello')
63
- expect(router).not.toBeNull()
64
- router = router.delete('/hello', 'delete hello')
65
- res = await router.matchRoute('DELETE', '/hello')
66
- expect(res).not.toBeNull()
67
- expect(res.handler[0]).toBe('delete hello')
68
- })
69
-
70
- it('Chain with route method', async () => {
71
- router.route('/api/book').get('get book').post('post book').put('put book')
72
-
73
- res = await router.matchRoute('GET', '/api/book')
74
- expect(res).not.toBeNull()
75
- expect(res.handler[0]).toBe('get book')
76
-
77
- res = await router.matchRoute('POST', '/api/book')
78
- expect(res).not.toBeNull()
79
- expect(res.handler[0]).toBe('post book')
80
-
81
- res = await router.matchRoute('PUT', '/api/book')
82
- expect(res).not.toBeNull()
83
- expect(res.handler[0]).toBe('put book')
84
-
85
- res = await router.matchRoute('DELETE', '/api/book')
86
- expect(res).toBeNull()
87
- })
88
- })
package/src/util.js DELETED
@@ -1,33 +0,0 @@
1
- const splitPath = (path) => {
2
- path = path.split(/\//) // faster than path.split('/')
3
- if (path[0] === '') {
4
- path.shift()
5
- }
6
- return path
7
- }
8
-
9
- const getPattern = (label) => {
10
- // :id{[0-9]+} => ([0-9]+)
11
- // :id => (.+)
12
- //const name = ''
13
- const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/)
14
- if (match) {
15
- if (match[2]) {
16
- return [match[1], '(' + match[2] + ')']
17
- } else {
18
- return [match[1], '(.+)']
19
- }
20
- }
21
- }
22
-
23
- const getPathFromURL = (url) => {
24
- // XXX
25
- const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/)
26
- return match[5]
27
- }
28
-
29
- module.exports = {
30
- splitPath,
31
- getPattern,
32
- getPathFromURL,
33
- }
package/src/util.test.js DELETED
@@ -1,44 +0,0 @@
1
- const { splitPath, getPattern, getPathFromURL } = require('./util')
2
-
3
- describe('Utility methods', () => {
4
- it('splitPath', () => {
5
- let ps = splitPath('/')
6
- expect(ps[0]).toBe('')
7
- ps = splitPath('/hello')
8
- expect(ps[0]).toBe('hello')
9
- ps = splitPath('*')
10
- expect(ps[0]).toBe('*')
11
- ps = splitPath('/wildcard-abc/*/wildcard-efg')
12
- expect(ps[0]).toBe('wildcard-abc')
13
- expect(ps[1]).toBe('*')
14
- expect(ps[2]).toBe('wildcard-efg')
15
- ps = splitPath('/map/:location/events')
16
- expect(ps[0]).toBe('map')
17
- expect(ps[1]).toBe(':location')
18
- expect(ps[2]).toBe('events')
19
- })
20
-
21
- it('getPattern', () => {
22
- let res = getPattern(':id')
23
- expect(res[0]).toBe('id')
24
- expect(res[1]).toBe('(.+)')
25
- res = getPattern(':id{[0-9]+}')
26
- expect(res[0]).toBe('id')
27
- expect(res[1]).toBe('([0-9]+)')
28
- })
29
-
30
- it('getPathFromURL', () => {
31
- let path = getPathFromURL('https://example.com/')
32
- expect(path).toBe('/')
33
- path = getPathFromURL('https://example.com/hello')
34
- expect(path).toBe('/hello')
35
- path = getPathFromURL('https://example.com/hello/hey')
36
- expect(path).toBe('/hello/hey')
37
- path = getPathFromURL('https://example.com/hello?name=foo')
38
- expect(path).toBe('/hello')
39
- path = getPathFromURL('https://example.com/hello/hey?name=foo&name=bar')
40
- expect(path).toBe('/hello/hey')
41
- path = getPathFromURL('https://example.com/hello/hey#fragment')
42
- expect(path).toBe('/hello/hey')
43
- })
44
- })