hono 2.1.3 → 2.2.0
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/README.md +13 -14
- package/dist/cjs/compose.js +48 -0
- package/dist/cjs/context.js +148 -0
- package/dist/cjs/hono.js +154 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/middleware/basic-auth/index.js +48 -0
- package/dist/cjs/middleware/bearer-auth/index.js +57 -0
- package/dist/cjs/middleware/cache/index.js +32 -0
- package/dist/cjs/middleware/compress/index.js +19 -0
- package/dist/cjs/middleware/cors/index.js +75 -0
- package/dist/cjs/middleware/etag/index.js +27 -0
- package/dist/cjs/middleware/html/index.js +36 -0
- package/dist/cjs/middleware/jsx/index.js +193 -0
- package/dist/cjs/middleware/jsx/jsx-dev-runtime.js +10 -0
- package/dist/cjs/middleware/jsx/jsx-runtime.js +7 -0
- package/dist/cjs/middleware/jwt/index.js +63 -0
- package/dist/cjs/middleware/logger/index.js +49 -0
- package/dist/cjs/middleware/powered-by/index.js +10 -0
- package/dist/cjs/middleware/pretty-json/index.js +11 -0
- package/dist/cjs/middleware/serve-static/bun.js +41 -0
- package/dist/cjs/middleware/serve-static/index.js +5 -0
- package/dist/cjs/middleware/serve-static/serve-static.js +40 -0
- package/dist/cjs/middleware/validator/index.js +5 -0
- package/dist/cjs/middleware/validator/middleware.js +56 -0
- package/dist/cjs/middleware/validator/rule.js +66 -0
- package/dist/cjs/middleware/validator/sanitizer.js +6 -0
- package/dist/cjs/middleware/validator/validator.js +195 -0
- package/dist/cjs/request.js +120 -0
- package/dist/cjs/router/reg-exp-router/index.js +5 -0
- package/dist/cjs/router/reg-exp-router/node.js +108 -0
- package/dist/cjs/router/reg-exp-router/router.js +161 -0
- package/dist/cjs/router/reg-exp-router/trie.js +42 -0
- package/dist/cjs/router/smart-router/index.js +5 -0
- package/dist/cjs/router/smart-router/router.js +57 -0
- package/dist/cjs/router/static-router/index.js +5 -0
- package/dist/cjs/router/static-router/router.js +72 -0
- package/dist/cjs/router/trie-router/index.js +5 -0
- package/dist/cjs/router/trie-router/node.js +175 -0
- package/dist/cjs/router/trie-router/router.js +24 -0
- package/dist/cjs/router.js +9 -0
- package/dist/cjs/utils/body.js +18 -0
- package/dist/cjs/utils/buffer.js +39 -0
- package/dist/cjs/utils/cloudflare.js +39 -0
- package/dist/cjs/utils/cookie.js +40 -0
- package/dist/cjs/utils/crypto.js +53 -0
- package/dist/cjs/utils/encode.js +80 -0
- package/dist/cjs/utils/filepath.js +25 -0
- package/dist/cjs/utils/html.js +38 -0
- package/dist/cjs/utils/http-status.js +50 -0
- package/dist/cjs/utils/json.js +22 -0
- package/dist/cjs/utils/jwt/index.js +27 -0
- package/dist/cjs/utils/jwt/jwt.js +101 -0
- package/dist/cjs/utils/jwt/types.js +49 -0
- package/dist/cjs/utils/mime.js +92 -0
- package/dist/cjs/utils/url.js +94 -0
- package/dist/compose.d.ts +7 -2
- package/dist/compose.js +24 -14
- package/dist/context.d.ts +14 -9
- package/dist/context.js +56 -24
- package/dist/hono.d.ts +29 -26
- package/dist/hono.js +44 -22
- package/dist/index.js +3 -6
- package/dist/middleware/basic-auth/index.d.ts +2 -3
- package/dist/middleware/basic-auth/index.js +7 -11
- package/dist/middleware/bearer-auth/index.d.ts +2 -3
- package/dist/middleware/bearer-auth/index.js +4 -12
- package/dist/middleware/cache/index.d.ts +2 -3
- package/dist/middleware/cache/index.js +1 -5
- package/dist/middleware/compress/index.d.ts +2 -3
- package/dist/middleware/compress/index.js +1 -5
- package/dist/middleware/cors/index.d.ts +3 -4
- package/dist/middleware/cors/index.js +16 -6
- package/dist/middleware/etag/index.d.ts +2 -3
- package/dist/middleware/etag/index.js +3 -7
- package/dist/middleware/html/index.js +6 -11
- package/dist/middleware/jsx/index.js +9 -15
- package/dist/middleware/jsx/jsx-dev-runtime.js +3 -7
- package/dist/middleware/jsx/jsx-runtime.js +2 -7
- package/dist/middleware/jwt/index.d.ts +2 -3
- package/dist/middleware/jwt/index.js +3 -7
- package/dist/middleware/logger/index.d.ts +2 -3
- package/dist/middleware/logger/index.js +3 -7
- package/dist/middleware/powered-by/index.d.ts +2 -3
- package/dist/middleware/powered-by/index.js +1 -5
- package/dist/middleware/pretty-json/index.d.ts +2 -3
- package/dist/middleware/pretty-json/index.js +1 -5
- package/dist/middleware/serve-static/bun.d.ts +2 -3
- package/dist/middleware/serve-static/bun.js +18 -19
- package/dist/middleware/serve-static/index.js +1 -5
- package/dist/middleware/serve-static/module.d.mts +1 -1
- package/dist/middleware/serve-static/serve-static.d.ts +2 -3
- package/dist/middleware/serve-static/serve-static.js +8 -12
- package/dist/middleware/validator/index.d.ts +2 -0
- package/dist/middleware/validator/index.js +2 -0
- package/dist/middleware/validator/middleware.d.ts +21 -0
- package/dist/middleware/validator/middleware.js +52 -0
- package/dist/middleware/validator/rule.d.ts +21 -0
- package/dist/middleware/validator/rule.js +63 -0
- package/dist/middleware/validator/sanitizer.d.ts +3 -0
- package/dist/middleware/validator/sanitizer.js +3 -0
- package/dist/middleware/validator/validator.d.ts +75 -0
- package/dist/middleware/validator/validator.js +186 -0
- package/dist/request.d.ts +15 -5
- package/dist/request.js +63 -30
- package/dist/router/reg-exp-router/index.js +1 -5
- package/dist/router/reg-exp-router/node.d.ts +1 -3
- package/dist/router/reg-exp-router/node.js +21 -17
- package/dist/router/reg-exp-router/router.d.ts +3 -27
- package/dist/router/reg-exp-router/router.js +104 -307
- package/dist/router/reg-exp-router/trie.d.ts +0 -4
- package/dist/router/reg-exp-router/trie.js +4 -8
- package/dist/router/smart-router/index.d.ts +1 -0
- package/dist/router/smart-router/index.js +1 -0
- package/dist/router/smart-router/router.d.ts +9 -0
- package/dist/router/smart-router/router.js +53 -0
- package/dist/router/static-router/index.d.ts +1 -0
- package/dist/router/static-router/index.js +1 -0
- package/dist/router/static-router/router.d.ts +8 -0
- package/dist/router/static-router/router.js +68 -0
- package/dist/router/trie-router/index.js +1 -5
- package/dist/router/trie-router/node.js +7 -11
- package/dist/router/trie-router/router.js +11 -7
- package/dist/router.d.ts +3 -0
- package/dist/router.js +5 -5
- package/dist/utils/body.d.ts +2 -1
- package/dist/utils/body.js +1 -5
- package/dist/utils/buffer.d.ts +1 -1
- package/dist/utils/buffer.js +5 -11
- package/dist/utils/cloudflare.d.ts +1 -1
- package/dist/utils/cloudflare.js +1 -5
- package/dist/utils/cookie.js +2 -7
- package/dist/utils/crypto.js +8 -15
- package/dist/utils/encode.js +10 -20
- package/dist/utils/filepath.js +1 -5
- package/dist/utils/html.js +1 -5
- package/dist/utils/http-status.js +1 -5
- package/dist/utils/json.d.ts +1 -0
- package/dist/utils/json.js +18 -0
- package/dist/utils/jwt/index.js +1 -27
- package/dist/utils/jwt/jwt.js +22 -28
- package/dist/utils/jwt/types.js +8 -16
- package/dist/utils/mime.js +1 -5
- package/dist/utils/url.d.ts +2 -1
- package/dist/utils/url.js +21 -19
- package/package.json +122 -35
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.html = exports.raw = void 0;
|
|
4
|
+
const html_1 = require("../../utils/html");
|
|
5
|
+
const raw = (value) => {
|
|
6
|
+
const escapedString = new String(value);
|
|
7
|
+
escapedString.isEscaped = true;
|
|
8
|
+
return escapedString;
|
|
9
|
+
};
|
|
10
|
+
exports.raw = raw;
|
|
11
|
+
const html = (strings, ...values) => {
|
|
12
|
+
const buffer = [''];
|
|
13
|
+
for (let i = 0, len = strings.length - 1; i < len; i++) {
|
|
14
|
+
buffer[0] += strings[i];
|
|
15
|
+
const children = values[i] instanceof Array ? values[i].flat(Infinity) : [values[i]];
|
|
16
|
+
for (let i = 0, len = children.length; i < len; i++) {
|
|
17
|
+
const child = children[i];
|
|
18
|
+
if (typeof child === 'string') {
|
|
19
|
+
(0, html_1.escapeToBuffer)(child, buffer);
|
|
20
|
+
}
|
|
21
|
+
else if (typeof child === 'boolean' || child === null || child === undefined) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
else if ((typeof child === 'object' && child.isEscaped) ||
|
|
25
|
+
typeof child === 'number') {
|
|
26
|
+
buffer[0] += child;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
(0, html_1.escapeToBuffer)(child.toString(), buffer);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
buffer[0] += strings[strings.length - 1];
|
|
34
|
+
return (0, exports.raw)(buffer[0]);
|
|
35
|
+
};
|
|
36
|
+
exports.html = html;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Fragment = exports.memo = exports.jsx = exports.JSXNode = void 0;
|
|
4
|
+
const html_1 = require("../../utils/html");
|
|
5
|
+
const emptyTags = [
|
|
6
|
+
'area',
|
|
7
|
+
'base',
|
|
8
|
+
'br',
|
|
9
|
+
'col',
|
|
10
|
+
'embed',
|
|
11
|
+
'hr',
|
|
12
|
+
'img',
|
|
13
|
+
'input',
|
|
14
|
+
'keygen',
|
|
15
|
+
'link',
|
|
16
|
+
'meta',
|
|
17
|
+
'param',
|
|
18
|
+
'source',
|
|
19
|
+
'track',
|
|
20
|
+
'wbr',
|
|
21
|
+
];
|
|
22
|
+
const booleanAttributes = [
|
|
23
|
+
'allowfullscreen',
|
|
24
|
+
'async',
|
|
25
|
+
'autofocus',
|
|
26
|
+
'autoplay',
|
|
27
|
+
'checked',
|
|
28
|
+
'controls',
|
|
29
|
+
'default',
|
|
30
|
+
'defer',
|
|
31
|
+
'disabled',
|
|
32
|
+
'formnovalidate',
|
|
33
|
+
'hidden',
|
|
34
|
+
'inert',
|
|
35
|
+
'ismap',
|
|
36
|
+
'itemscope',
|
|
37
|
+
'loop',
|
|
38
|
+
'multiple',
|
|
39
|
+
'muted',
|
|
40
|
+
'nomodule',
|
|
41
|
+
'novalidate',
|
|
42
|
+
'open',
|
|
43
|
+
'playsinline',
|
|
44
|
+
'readonly',
|
|
45
|
+
'required',
|
|
46
|
+
'reversed',
|
|
47
|
+
'selected',
|
|
48
|
+
];
|
|
49
|
+
const childrenToStringToBuffer = (children, buffer) => {
|
|
50
|
+
for (let i = 0, len = children.length; i < len; i++) {
|
|
51
|
+
const child = children[i];
|
|
52
|
+
if (typeof child === 'string') {
|
|
53
|
+
(0, html_1.escapeToBuffer)(child, buffer);
|
|
54
|
+
}
|
|
55
|
+
else if (typeof child === 'boolean' || child === null || child === undefined) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
else if (child instanceof JSXNode) {
|
|
59
|
+
child.toStringToBuffer(buffer);
|
|
60
|
+
}
|
|
61
|
+
else if (typeof child === 'number' || child.isEscaped) {
|
|
62
|
+
buffer[0] += child;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
// `child` type is `Child[]`, so stringify recursively
|
|
66
|
+
childrenToStringToBuffer(child, buffer);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
class JSXNode {
|
|
71
|
+
constructor(tag, props, children) {
|
|
72
|
+
this.isEscaped = true;
|
|
73
|
+
this.tag = tag;
|
|
74
|
+
this.props = props;
|
|
75
|
+
this.children = children;
|
|
76
|
+
}
|
|
77
|
+
toString() {
|
|
78
|
+
const buffer = [''];
|
|
79
|
+
this.toStringToBuffer(buffer);
|
|
80
|
+
return buffer[0];
|
|
81
|
+
}
|
|
82
|
+
toStringToBuffer(buffer) {
|
|
83
|
+
const tag = this.tag;
|
|
84
|
+
const props = this.props;
|
|
85
|
+
let { children } = this;
|
|
86
|
+
buffer[0] += `<${tag}`;
|
|
87
|
+
const propsKeys = Object.keys(props || {});
|
|
88
|
+
for (let i = 0, len = propsKeys.length; i < len; i++) {
|
|
89
|
+
const v = props[propsKeys[i]];
|
|
90
|
+
if (typeof v === 'string') {
|
|
91
|
+
buffer[0] += ` ${propsKeys[i]}="`;
|
|
92
|
+
(0, html_1.escapeToBuffer)(v, buffer);
|
|
93
|
+
buffer[0] += '"';
|
|
94
|
+
}
|
|
95
|
+
else if (typeof v === 'number') {
|
|
96
|
+
buffer[0] += ` ${propsKeys[i]}="${v}"`;
|
|
97
|
+
}
|
|
98
|
+
else if (v === null || v === undefined) {
|
|
99
|
+
// Do nothing
|
|
100
|
+
}
|
|
101
|
+
else if (typeof v === 'boolean' && booleanAttributes.includes(propsKeys[i])) {
|
|
102
|
+
if (v) {
|
|
103
|
+
buffer[0] += ` ${propsKeys[i]}=""`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (propsKeys[i] === 'dangerouslySetInnerHTML') {
|
|
107
|
+
if (children.length > 0) {
|
|
108
|
+
throw 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.';
|
|
109
|
+
}
|
|
110
|
+
const escapedString = new String(v.__html);
|
|
111
|
+
escapedString.isEscaped = true;
|
|
112
|
+
children = [escapedString];
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
buffer[0] += ` ${propsKeys[i]}="`;
|
|
116
|
+
(0, html_1.escapeToBuffer)(v.toString(), buffer);
|
|
117
|
+
buffer[0] += '"';
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (emptyTags.includes(tag)) {
|
|
121
|
+
buffer[0] += '/>';
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
buffer[0] += '>';
|
|
125
|
+
childrenToStringToBuffer(children, buffer);
|
|
126
|
+
buffer[0] += `</${tag}>`;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.JSXNode = JSXNode;
|
|
130
|
+
class JSXFunctionNode extends JSXNode {
|
|
131
|
+
toStringToBuffer(buffer) {
|
|
132
|
+
const { children } = this;
|
|
133
|
+
const res = this.tag.call(null, {
|
|
134
|
+
...this.props,
|
|
135
|
+
children: children.length <= 1 ? children[0] : children,
|
|
136
|
+
});
|
|
137
|
+
if (res instanceof JSXNode) {
|
|
138
|
+
res.toStringToBuffer(buffer);
|
|
139
|
+
}
|
|
140
|
+
else if (typeof res === 'number' || res.isEscaped) {
|
|
141
|
+
buffer[0] += res;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
(0, html_1.escapeToBuffer)(res, buffer);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
class JSXFragmentNode extends JSXNode {
|
|
149
|
+
toStringToBuffer(buffer) {
|
|
150
|
+
childrenToStringToBuffer(this.children, buffer);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const jsxFn = (tag, props, ...children) => {
|
|
154
|
+
if (typeof tag === 'function') {
|
|
155
|
+
return new JSXFunctionNode(tag, props, children);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return new JSXNode(tag, props, children);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
exports.jsx = jsxFn;
|
|
162
|
+
const shallowEqual = (a, b) => {
|
|
163
|
+
if (a === b) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
const aKeys = Object.keys(a);
|
|
167
|
+
const bKeys = Object.keys(b);
|
|
168
|
+
if (aKeys.length !== bKeys.length) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
for (let i = 0, len = aKeys.length; i < len; i++) {
|
|
172
|
+
if (a[aKeys[i]] !== b[aKeys[i]]) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
};
|
|
178
|
+
const memo = (component, propsAreEqual = shallowEqual) => {
|
|
179
|
+
let computed = undefined;
|
|
180
|
+
let prevProps = undefined;
|
|
181
|
+
return ((props) => {
|
|
182
|
+
if (prevProps && !propsAreEqual(prevProps, props)) {
|
|
183
|
+
computed = undefined;
|
|
184
|
+
}
|
|
185
|
+
prevProps = props;
|
|
186
|
+
return (computed || (computed = component(props)));
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
exports.memo = memo;
|
|
190
|
+
const Fragment = (props) => {
|
|
191
|
+
return new JSXFragmentNode('', {}, props.children || []);
|
|
192
|
+
};
|
|
193
|
+
exports.Fragment = Fragment;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsxDEV = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
5
|
+
function jsxDEV(tag, props) {
|
|
6
|
+
const children = props.children ?? [];
|
|
7
|
+
delete props['children'];
|
|
8
|
+
return (0, _1.jsx)(tag, props, children);
|
|
9
|
+
}
|
|
10
|
+
exports.jsxDEV = jsxDEV;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsxs = exports.jsx = void 0;
|
|
4
|
+
var jsx_dev_runtime_1 = require("./jsx-dev-runtime");
|
|
5
|
+
Object.defineProperty(exports, "jsx", { enumerable: true, get: function () { return jsx_dev_runtime_1.jsxDEV; } });
|
|
6
|
+
var jsx_dev_runtime_2 = require("./jsx-dev-runtime");
|
|
7
|
+
Object.defineProperty(exports, "jsxs", { enumerable: true, get: function () { return jsx_dev_runtime_2.jsxDEV; } });
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jwt = void 0;
|
|
4
|
+
const jwt_1 = require("../../utils/jwt");
|
|
5
|
+
const jwt = (options) => {
|
|
6
|
+
if (!options) {
|
|
7
|
+
throw new Error('JWT auth middleware requires options for "secret');
|
|
8
|
+
}
|
|
9
|
+
if (!crypto.subtle || !crypto.subtle.importKey) {
|
|
10
|
+
throw new Error('`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.');
|
|
11
|
+
}
|
|
12
|
+
return async (ctx, next) => {
|
|
13
|
+
const credentials = ctx.req.headers.get('Authorization');
|
|
14
|
+
let token;
|
|
15
|
+
if (credentials) {
|
|
16
|
+
const parts = credentials.split(/\s+/);
|
|
17
|
+
if (parts.length !== 2) {
|
|
18
|
+
ctx.res = new Response('Unauthorized', {
|
|
19
|
+
status: 401,
|
|
20
|
+
headers: {
|
|
21
|
+
'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
token = parts[1];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else if (options.cookie) {
|
|
31
|
+
token = ctx.req.cookie(options.cookie);
|
|
32
|
+
}
|
|
33
|
+
if (!token) {
|
|
34
|
+
ctx.res = new Response('Unauthorized', {
|
|
35
|
+
status: 401,
|
|
36
|
+
headers: {
|
|
37
|
+
'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
let authorized = false;
|
|
43
|
+
let msg = '';
|
|
44
|
+
try {
|
|
45
|
+
authorized = await jwt_1.Jwt.verify(token, options.secret, options.alg);
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
msg = `${e}`;
|
|
49
|
+
}
|
|
50
|
+
if (!authorized) {
|
|
51
|
+
ctx.res = new Response('Unauthorized', {
|
|
52
|
+
status: 401,
|
|
53
|
+
statusText: msg,
|
|
54
|
+
headers: {
|
|
55
|
+
'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
await next();
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
exports.jwt = jwt;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = void 0;
|
|
4
|
+
const url_1 = require("../../utils/url");
|
|
5
|
+
var LogPrefix;
|
|
6
|
+
(function (LogPrefix) {
|
|
7
|
+
LogPrefix["Outgoing"] = "-->";
|
|
8
|
+
LogPrefix["Incoming"] = "<--";
|
|
9
|
+
LogPrefix["Error"] = "xxx";
|
|
10
|
+
})(LogPrefix || (LogPrefix = {}));
|
|
11
|
+
const humanize = (times) => {
|
|
12
|
+
const [delimiter, separator] = [',', '.'];
|
|
13
|
+
const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter));
|
|
14
|
+
return orderTimes.join(separator);
|
|
15
|
+
};
|
|
16
|
+
const time = (start) => {
|
|
17
|
+
const delta = Date.now() - start;
|
|
18
|
+
return humanize([delta < 1000 ? delta + 'ms' : Math.round(delta / 1000) + 's']);
|
|
19
|
+
};
|
|
20
|
+
const colorStatus = (status) => {
|
|
21
|
+
const out = {
|
|
22
|
+
7: `\x1b[35m${status}\x1b[0m`,
|
|
23
|
+
5: `\x1b[31m${status}\x1b[0m`,
|
|
24
|
+
4: `\x1b[33m${status}\x1b[0m`,
|
|
25
|
+
3: `\x1b[36m${status}\x1b[0m`,
|
|
26
|
+
2: `\x1b[32m${status}\x1b[0m`,
|
|
27
|
+
1: `\x1b[32m${status}\x1b[0m`,
|
|
28
|
+
0: `\x1b[33m${status}\x1b[0m`,
|
|
29
|
+
};
|
|
30
|
+
const calculateStatus = (status / 100) | 0;
|
|
31
|
+
return out[calculateStatus];
|
|
32
|
+
};
|
|
33
|
+
function log(fn, prefix, method, path, status = 0, elapsed) {
|
|
34
|
+
const out = prefix === LogPrefix.Incoming
|
|
35
|
+
? ` ${prefix} ${method} ${path}`
|
|
36
|
+
: ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
|
|
37
|
+
fn(out);
|
|
38
|
+
}
|
|
39
|
+
const logger = (fn = console.log) => {
|
|
40
|
+
return async (c, next) => {
|
|
41
|
+
const { method } = c.req;
|
|
42
|
+
const path = (0, url_1.getPathFromURL)(c.req.url);
|
|
43
|
+
log(fn, LogPrefix.Incoming, method, path);
|
|
44
|
+
const start = Date.now();
|
|
45
|
+
await next();
|
|
46
|
+
log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start));
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
exports.logger = logger;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.poweredBy = void 0;
|
|
4
|
+
const poweredBy = () => {
|
|
5
|
+
return async (c, next) => {
|
|
6
|
+
await next();
|
|
7
|
+
c.res.headers.append('X-Powered-By', 'Hono');
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
exports.poweredBy = poweredBy;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prettyJSON = void 0;
|
|
4
|
+
const prettyJSON = (options = { space: 2 }) => {
|
|
5
|
+
return async (c, next) => {
|
|
6
|
+
const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false;
|
|
7
|
+
c.pretty(pretty, options.space);
|
|
8
|
+
await next();
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
exports.prettyJSON = prettyJSON;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serveStatic = void 0;
|
|
4
|
+
// @denoify-ignore
|
|
5
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const filepath_1 = require("../../utils/filepath");
|
|
8
|
+
const mime_1 = require("../../utils/mime");
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
const { file } = Bun;
|
|
11
|
+
const DEFAULT_DOCUMENT = 'index.html';
|
|
12
|
+
const serveStatic = (options = { root: '' }) => {
|
|
13
|
+
return async (c, next) => {
|
|
14
|
+
// Do nothing if Response is already set
|
|
15
|
+
if (c.res && c.finalized) {
|
|
16
|
+
await next();
|
|
17
|
+
}
|
|
18
|
+
const url = new URL(c.req.url);
|
|
19
|
+
let path = (0, filepath_1.getFilePath)({
|
|
20
|
+
filename: options.path ?? url.pathname,
|
|
21
|
+
root: options.root,
|
|
22
|
+
defaultDocument: DEFAULT_DOCUMENT,
|
|
23
|
+
});
|
|
24
|
+
path = `./${path}`;
|
|
25
|
+
if ((0, fs_1.existsSync)(path)) {
|
|
26
|
+
const content = file(path);
|
|
27
|
+
if (content) {
|
|
28
|
+
const mimeType = (0, mime_1.getMimeType)(path);
|
|
29
|
+
if (mimeType) {
|
|
30
|
+
c.header('Content-Type', mimeType);
|
|
31
|
+
}
|
|
32
|
+
// Return Response object
|
|
33
|
+
return c.body(content);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
console.warn(`Static file: ${path} is not found`);
|
|
37
|
+
await next();
|
|
38
|
+
return;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
exports.serveStatic = serveStatic;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serveStatic = void 0;
|
|
4
|
+
var serve_static_1 = require("./serve-static");
|
|
5
|
+
Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return serve_static_1.serveStatic; } });
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serveStatic = void 0;
|
|
4
|
+
const cloudflare_1 = require("../../utils/cloudflare");
|
|
5
|
+
const filepath_1 = require("../../utils/filepath");
|
|
6
|
+
const mime_1 = require("../../utils/mime");
|
|
7
|
+
const DEFAULT_DOCUMENT = 'index.html';
|
|
8
|
+
// This middleware is available only on Cloudflare Workers.
|
|
9
|
+
const serveStatic = (options = { root: '' }) => {
|
|
10
|
+
return async (c, next) => {
|
|
11
|
+
// Do nothing if Response is already set
|
|
12
|
+
if (c.finalized) {
|
|
13
|
+
await next();
|
|
14
|
+
}
|
|
15
|
+
const url = new URL(c.req.url);
|
|
16
|
+
const path = (0, filepath_1.getFilePath)({
|
|
17
|
+
filename: options.path ?? url.pathname,
|
|
18
|
+
root: options.root,
|
|
19
|
+
defaultDocument: DEFAULT_DOCUMENT,
|
|
20
|
+
});
|
|
21
|
+
const content = await (0, cloudflare_1.getContentFromKVAsset)(path, {
|
|
22
|
+
manifest: options.manifest,
|
|
23
|
+
namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : undefined,
|
|
24
|
+
});
|
|
25
|
+
if (content) {
|
|
26
|
+
const mimeType = (0, mime_1.getMimeType)(path);
|
|
27
|
+
if (mimeType) {
|
|
28
|
+
c.header('Content-Type', mimeType);
|
|
29
|
+
}
|
|
30
|
+
// Return Response object
|
|
31
|
+
return c.body(content);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
console.warn(`Static file: ${path} is not found`);
|
|
35
|
+
await next();
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
exports.serveStatic = serveStatic;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validator = void 0;
|
|
4
|
+
const middleware_1 = require("./middleware");
|
|
5
|
+
Object.defineProperty(exports, "validator", { enumerable: true, get: function () { return middleware_1.validatorMiddleware; } });
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validatorMiddleware = void 0;
|
|
4
|
+
const validator_1 = require("./validator");
|
|
5
|
+
function getValidatorList(schema) {
|
|
6
|
+
const map = [];
|
|
7
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
8
|
+
if (value instanceof validator_1.VBase) {
|
|
9
|
+
map.push([[key], value]);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
const children = getValidatorList(value);
|
|
13
|
+
for (const [keys, validator] of children) {
|
|
14
|
+
map.push([[key, ...keys], validator]);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return map;
|
|
19
|
+
}
|
|
20
|
+
const validatorMiddleware = (validationFunction, options) => {
|
|
21
|
+
const v = new validator_1.Validator();
|
|
22
|
+
const handler = async (c, next) => {
|
|
23
|
+
const resultSet = {
|
|
24
|
+
hasError: false,
|
|
25
|
+
messages: [],
|
|
26
|
+
results: [],
|
|
27
|
+
};
|
|
28
|
+
const validatorList = getValidatorList(validationFunction(v, c));
|
|
29
|
+
for (const [keys, validator] of validatorList) {
|
|
30
|
+
const result = await validator.validate(c.req);
|
|
31
|
+
if (result.isValid) {
|
|
32
|
+
// Set data on request object
|
|
33
|
+
c.req.valid(keys, result.value);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
resultSet.hasError = true;
|
|
37
|
+
if (result.message !== undefined) {
|
|
38
|
+
resultSet.messages.push(result.message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
resultSet.results.push(result);
|
|
42
|
+
}
|
|
43
|
+
if (options && options.done) {
|
|
44
|
+
const res = options.done(resultSet, c);
|
|
45
|
+
if (res) {
|
|
46
|
+
return res;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (resultSet.hasError) {
|
|
50
|
+
return c.text(resultSet.messages.join('\n'), 400);
|
|
51
|
+
}
|
|
52
|
+
await next();
|
|
53
|
+
};
|
|
54
|
+
return handler;
|
|
55
|
+
};
|
|
56
|
+
exports.validatorMiddleware = validatorMiddleware;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Some validation rules is are based on Validator.js
|
|
3
|
+
// Validator.js
|
|
4
|
+
// License (MIT)
|
|
5
|
+
// Copyright (c) 2018 Chris O'Hara <cohara87@gmail.com>
|
|
6
|
+
// https://github.com/validatorjs/validator.js
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.rule = void 0;
|
|
9
|
+
exports.rule = {
|
|
10
|
+
// string
|
|
11
|
+
isEmpty(value, options = { ignore_whitespace: false }) {
|
|
12
|
+
return (options.ignore_whitespace ? value.trim().length : value.length) === 0;
|
|
13
|
+
},
|
|
14
|
+
isLength: (value, options, arg2) => {
|
|
15
|
+
let min;
|
|
16
|
+
let max;
|
|
17
|
+
if (typeof options === 'object') {
|
|
18
|
+
min = options.min || 0;
|
|
19
|
+
max = options.max;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
// backwards compatibility: isLength(str, min [, max])
|
|
23
|
+
min = options || 0;
|
|
24
|
+
max = arg2;
|
|
25
|
+
}
|
|
26
|
+
const presentationSequences = value.match(/(\uFE0F|\uFE0E)/g) || [];
|
|
27
|
+
const surrogatePairs = value.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
|
|
28
|
+
const len = value.length - presentationSequences.length - surrogatePairs.length;
|
|
29
|
+
return len >= min && (typeof max === 'undefined' || len <= max);
|
|
30
|
+
},
|
|
31
|
+
isAlpha: (value) => {
|
|
32
|
+
return /^[A-Z]+$/i.test(value);
|
|
33
|
+
},
|
|
34
|
+
isNumeric: (value) => {
|
|
35
|
+
return /^[0-9]+$/.test(value);
|
|
36
|
+
},
|
|
37
|
+
contains: (value, elem, options = {
|
|
38
|
+
ignoreCase: false,
|
|
39
|
+
minOccurrences: 1,
|
|
40
|
+
}) => {
|
|
41
|
+
options.ignoreCase || (options.ignoreCase = false);
|
|
42
|
+
options.minOccurrences || (options.minOccurrences = 1);
|
|
43
|
+
if (options.ignoreCase) {
|
|
44
|
+
return value.toLowerCase().split(elem.toLowerCase()).length > options.minOccurrences;
|
|
45
|
+
}
|
|
46
|
+
return value.split(elem).length > options.minOccurrences;
|
|
47
|
+
},
|
|
48
|
+
isIn: (value, options) => {
|
|
49
|
+
if (typeof options === 'object') {
|
|
50
|
+
for (const elem of options) {
|
|
51
|
+
if (elem === value)
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
},
|
|
57
|
+
match: (value, regExp) => {
|
|
58
|
+
return regExp.test(value);
|
|
59
|
+
},
|
|
60
|
+
// number
|
|
61
|
+
isGte: (value, min) => min <= value,
|
|
62
|
+
isLte: (value, max) => value <= max,
|
|
63
|
+
// boolean
|
|
64
|
+
isTrue: (value) => value === true,
|
|
65
|
+
isFalse: (value) => value === false,
|
|
66
|
+
};
|