hono 0.0.8 → 0.0.9
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/CODE_OF_CONDUCT.md +128 -0
- package/README.md +13 -0
- package/package.json +1 -1
- package/src/hono.d.ts +1 -0
- package/src/hono.js +8 -11
- package/src/hono.test.js +46 -7
- package/src/middleware/poweredBy.js +2 -2
- package/src/middleware.js +2 -0
- package/src/util.js +7 -0
- package/src/util.test.js +16 -1
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
+
and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the
|
|
26
|
+
overall community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
31
|
+
advances of any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email
|
|
35
|
+
address, without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official e-mail address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
yusuke@kamawada.com.
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series
|
|
86
|
+
of actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or
|
|
93
|
+
permanent ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
|
113
|
+
the community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.0, available at
|
|
119
|
+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
|
122
|
+
enforcement ladder](https://github.com/mozilla/diversity).
|
|
123
|
+
|
|
124
|
+
[homepage]: https://www.contributor-covenant.org
|
|
125
|
+
|
|
126
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
127
|
+
https://www.contributor-covenant.org/faq. Translations are available at
|
|
128
|
+
https://www.contributor-covenant.org/translations.
|
package/README.md
CHANGED
|
@@ -132,6 +132,19 @@ app.use('/message/*', addHeader)
|
|
|
132
132
|
app.get('/message/hello', () => 'Hello Middleware!')
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
+
### Custom 404 Response
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
const customNotFound = (c, next) => {
|
|
139
|
+
next()
|
|
140
|
+
if (c.res.status === 404) {
|
|
141
|
+
c.res = new Response('Custom 404 Not Found', { status: 404 })
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
app.use('*', customNotFound)
|
|
146
|
+
```
|
|
147
|
+
|
|
135
148
|
## Context
|
|
136
149
|
|
|
137
150
|
### req
|
package/package.json
CHANGED
package/src/hono.d.ts
CHANGED
package/src/hono.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const Node = require('./node')
|
|
4
|
+
const Middleware = require('./middleware')
|
|
4
5
|
const compose = require('./compose')
|
|
5
6
|
const methods = require('./methods')
|
|
6
|
-
const
|
|
7
|
-
const Middleware = require('./middleware')
|
|
7
|
+
const { getPathFromURL } = require('./util')
|
|
8
8
|
|
|
9
9
|
const METHOD_NAME_OF_ALL = 'ALL'
|
|
10
10
|
|
|
@@ -23,12 +23,6 @@ class Router {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
const getPathFromURL = (url) => {
|
|
27
|
-
// XXX
|
|
28
|
-
const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/)
|
|
29
|
-
return match[5]
|
|
30
|
-
}
|
|
31
|
-
|
|
32
26
|
class Hono {
|
|
33
27
|
constructor() {
|
|
34
28
|
this.router = new Router()
|
|
@@ -65,6 +59,10 @@ class Hono {
|
|
|
65
59
|
}
|
|
66
60
|
|
|
67
61
|
use(path, middleware) {
|
|
62
|
+
if (middleware.constructor.name !== 'Function') {
|
|
63
|
+
throw new TypeError('middleware must be a function!')
|
|
64
|
+
}
|
|
65
|
+
|
|
68
66
|
const router = new Router()
|
|
69
67
|
router.add(METHOD_NAME_OF_ALL, path, middleware)
|
|
70
68
|
this.middlewareRouters.push(router)
|
|
@@ -89,13 +87,12 @@ class Hono {
|
|
|
89
87
|
const [method, path] = [request.method, getPathFromURL(request.url)]
|
|
90
88
|
|
|
91
89
|
const result = await this.matchRoute(method, path)
|
|
92
|
-
if (!result) return this.notFound()
|
|
93
90
|
|
|
94
91
|
request.params = (key) => result.params[key]
|
|
95
92
|
|
|
96
|
-
let handler = result.handler[0] // XXX
|
|
93
|
+
let handler = result ? result.handler[0] : this.notFound // XXX
|
|
97
94
|
|
|
98
|
-
const middleware = [defaultFilter] // add defaultFilter later
|
|
95
|
+
const middleware = [Middleware.defaultFilter] // add defaultFilter later
|
|
99
96
|
|
|
100
97
|
for (const mr of this.middlewareRouters) {
|
|
101
98
|
const mwResult = mr.match(METHOD_NAME_OF_ALL, path)
|
package/src/hono.test.js
CHANGED
|
@@ -63,23 +63,23 @@ describe('params and query', () => {
|
|
|
63
63
|
describe('Middleware', () => {
|
|
64
64
|
const app = new Hono()
|
|
65
65
|
|
|
66
|
-
const logger =
|
|
66
|
+
const logger = (c, next) => {
|
|
67
67
|
console.log(`${c.req.method} : ${c.req.url}`)
|
|
68
68
|
next()
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
const rootHeader =
|
|
71
|
+
const rootHeader = (c, next) => {
|
|
72
72
|
next()
|
|
73
|
-
|
|
73
|
+
c.res.headers.append('x-custom', 'root')
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
const customHeader =
|
|
76
|
+
const customHeader = (c, next) => {
|
|
77
77
|
next()
|
|
78
|
-
|
|
78
|
+
c.res.headers.append('x-message', 'custom-header')
|
|
79
79
|
}
|
|
80
|
-
const customHeader2 =
|
|
80
|
+
const customHeader2 = (c, next) => {
|
|
81
81
|
next()
|
|
82
|
-
|
|
82
|
+
c.res.headers.append('x-message-2', 'custom-header-2')
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
app.use('*', logger)
|
|
@@ -113,3 +113,42 @@ describe('Middleware', () => {
|
|
|
113
113
|
expect(await res.headers.get('x-message-2')).toBe('custom-header-2')
|
|
114
114
|
})
|
|
115
115
|
})
|
|
116
|
+
|
|
117
|
+
describe('Custom 404', () => {
|
|
118
|
+
const app = new Hono()
|
|
119
|
+
|
|
120
|
+
const customNotFound = (c, next) => {
|
|
121
|
+
next()
|
|
122
|
+
if (c.res.status === 404) {
|
|
123
|
+
c.res = new fetch.Response('Custom 404 Not Found', { status: 404 })
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
app.notFound = () => {
|
|
128
|
+
return new fetch.Response('Default 404 Nout Found', { status: 404 })
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
app.use('*', customNotFound)
|
|
132
|
+
app.get('/hello', () => {
|
|
133
|
+
return new fetch.Response('hello')
|
|
134
|
+
})
|
|
135
|
+
it('Custom 404 Not Found', async () => {
|
|
136
|
+
let req = new fetch.Request('https://example.com/hello')
|
|
137
|
+
let res = await app.dispatch(req)
|
|
138
|
+
expect(res.status).toBe(200)
|
|
139
|
+
req = new fetch.Request('https://example.com/foo')
|
|
140
|
+
res = await app.dispatch(req)
|
|
141
|
+
expect(res.status).toBe(404)
|
|
142
|
+
expect(await res.text()).toBe('Custom 404 Not Found')
|
|
143
|
+
})
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
describe('Error Handling', () => {
|
|
147
|
+
const app = new Hono()
|
|
148
|
+
|
|
149
|
+
it('Middleware must be function', () => {
|
|
150
|
+
expect(() => {
|
|
151
|
+
app.use('*', {})
|
|
152
|
+
}).toThrow(TypeError)
|
|
153
|
+
})
|
|
154
|
+
})
|
package/src/middleware.js
CHANGED
package/src/util.js
CHANGED
|
@@ -20,7 +20,14 @@ const getPattern = (label) => {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
const getPathFromURL = (url) => {
|
|
24
|
+
// XXX
|
|
25
|
+
const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/)
|
|
26
|
+
return match[5]
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
module.exports = {
|
|
24
30
|
splitPath,
|
|
25
31
|
getPattern,
|
|
32
|
+
getPathFromURL,
|
|
26
33
|
}
|
package/src/util.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { splitPath, getPattern } = require('./util')
|
|
1
|
+
const { splitPath, getPattern, getPathFromURL } = require('./util')
|
|
2
2
|
|
|
3
3
|
describe('Utility methods', () => {
|
|
4
4
|
it('splitPath', () => {
|
|
@@ -26,4 +26,19 @@ describe('Utility methods', () => {
|
|
|
26
26
|
expect(res[0]).toBe('id')
|
|
27
27
|
expect(res[1]).toBe('([0-9]+)')
|
|
28
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
|
+
})
|
|
29
44
|
})
|