hono 2.3.1 → 2.4.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/dist/cjs/compose.js +82 -66
- package/dist/cjs/context.js +159 -144
- package/dist/cjs/hono.js +173 -150
- package/dist/cjs/index.js +31 -11
- package/dist/cjs/middleware/basic-auth/index.js +70 -39
- package/dist/cjs/middleware/bearer-auth/index.js +69 -51
- package/dist/cjs/middleware/cache/index.js +49 -28
- package/dist/cjs/middleware/compress/index.js +39 -16
- package/dist/cjs/middleware/cors/index.js +89 -71
- package/dist/cjs/middleware/etag/index.js +46 -24
- package/dist/cjs/middleware/html/index.js +50 -30
- package/dist/cjs/middleware/jsx/index.js +178 -166
- package/dist/cjs/middleware/jsx/jsx-dev-runtime.js +30 -7
- package/dist/cjs/middleware/jsx/jsx-runtime.js +30 -6
- package/dist/cjs/middleware/jwt/index.js +74 -54
- package/dist/cjs/middleware/logger/index.js +59 -38
- package/dist/cjs/middleware/powered-by/index.js +30 -7
- package/dist/cjs/middleware/pretty-json/index.js +31 -8
- package/dist/cjs/middleware/serve-static/bun.js +56 -38
- package/dist/cjs/middleware/serve-static/index.js +27 -4
- package/dist/cjs/middleware/serve-static/module.js +42 -0
- package/dist/cjs/middleware/serve-static/serve-static.js +58 -39
- package/dist/cjs/middleware/validator/index.js +27 -4
- package/dist/cjs/middleware/validator/middleware.js +125 -91
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/request.js +129 -116
- package/dist/cjs/router/reg-exp-router/index.js +27 -4
- package/dist/cjs/router/reg-exp-router/node.js +108 -97
- package/dist/cjs/router/reg-exp-router/router.js +158 -141
- package/dist/cjs/router/reg-exp-router/trie.js +55 -39
- package/dist/cjs/router/smart-router/index.js +27 -4
- package/dist/cjs/router/smart-router/router.js +70 -49
- package/dist/cjs/router/static-router/index.js +27 -4
- package/dist/cjs/router/static-router/router.js +78 -64
- package/dist/cjs/router/trie-router/index.js +27 -4
- package/dist/cjs/router/trie-router/node.js +167 -159
- package/dist/cjs/router/trie-router/router.js +43 -20
- package/dist/cjs/router.js +35 -6
- package/dist/cjs/types.js +16 -0
- package/dist/cjs/utils/body.js +36 -15
- package/dist/cjs/utils/buffer.js +56 -31
- package/dist/cjs/utils/cloudflare.js +53 -34
- package/dist/cjs/utils/cookie.js +59 -35
- package/dist/cjs/utils/crypto.js +69 -43
- package/dist/cjs/utils/encode.js +92 -65
- package/dist/cjs/utils/filepath.js +39 -22
- package/dist/cjs/utils/html.js +54 -33
- package/dist/cjs/utils/http-status.js +68 -45
- package/dist/cjs/utils/json.js +91 -76
- package/dist/cjs/utils/jwt/index.js +32 -25
- package/dist/cjs/utils/jwt/jwt.js +124 -93
- package/dist/cjs/utils/jwt/types.js +65 -38
- package/dist/cjs/utils/mime.js +110 -87
- package/dist/cjs/utils/object.js +53 -35
- package/dist/cjs/utils/types.js +16 -0
- package/dist/cjs/utils/url.js +89 -78
- package/dist/cjs/validator/rule.js +80 -0
- package/dist/cjs/validator/sanitizer.js +30 -0
- package/dist/cjs/validator/schema.js +16 -0
- package/dist/cjs/validator/validator.js +439 -0
- package/dist/compose.d.ts +4 -3
- package/dist/compose.js +61 -64
- package/dist/context.d.ts +10 -9
- package/dist/context.js +139 -143
- package/dist/hono.d.ts +27 -42
- package/dist/hono.js +153 -149
- package/dist/index.d.ts +4 -1
- package/dist/index.js +9 -9
- package/dist/middleware/basic-auth/index.d.ts +1 -1
- package/dist/middleware/basic-auth/index.js +52 -40
- package/dist/middleware/bearer-auth/index.d.ts +1 -1
- package/dist/middleware/bearer-auth/index.js +48 -49
- package/dist/middleware/cache/index.d.ts +1 -1
- package/dist/middleware/cache/index.js +28 -26
- package/dist/middleware/compress/index.d.ts +1 -1
- package/dist/middleware/compress/index.js +18 -14
- package/dist/middleware/cors/index.d.ts +1 -1
- package/dist/middleware/cors/index.js +68 -69
- package/dist/middleware/etag/index.d.ts +1 -1
- package/dist/middleware/etag/index.js +25 -22
- package/dist/middleware/html/index.js +29 -28
- package/dist/middleware/jsx/index.js +164 -172
- package/dist/middleware/jsx/jsx-dev-runtime.js +9 -5
- package/dist/middleware/jsx/jsx-runtime.js +7 -2
- package/dist/middleware/jwt/index.d.ts +1 -1
- package/dist/middleware/jwt/index.js +53 -52
- package/dist/middleware/logger/index.d.ts +1 -1
- package/dist/middleware/logger/index.js +35 -39
- package/dist/middleware/powered-by/index.d.ts +1 -1
- package/dist/middleware/powered-by/index.js +9 -5
- package/dist/middleware/pretty-json/index.d.ts +1 -1
- package/dist/middleware/pretty-json/index.js +10 -6
- package/dist/middleware/serve-static/bun.d.ts +1 -1
- package/dist/middleware/serve-static/bun.js +35 -36
- package/dist/middleware/serve-static/index.js +5 -1
- package/dist/middleware/serve-static/module.d.ts +3 -0
- package/dist/middleware/serve-static/module.js +13 -0
- package/dist/middleware/serve-static/serve-static.d.ts +2 -3
- package/dist/middleware/serve-static/serve-static.js +36 -36
- package/dist/middleware/validator/index.js +5 -2
- package/dist/middleware/validator/middleware.d.ts +9 -15
- package/dist/middleware/validator/middleware.js +104 -89
- package/dist/request.d.ts +2 -2
- package/dist/request.js +108 -114
- package/dist/router/reg-exp-router/index.js +5 -1
- package/dist/router/reg-exp-router/node.js +87 -96
- package/dist/router/reg-exp-router/router.js +140 -142
- package/dist/router/reg-exp-router/trie.js +35 -38
- package/dist/router/smart-router/index.js +5 -1
- package/dist/router/smart-router/router.js +50 -48
- package/dist/router/static-router/index.js +5 -1
- package/dist/router/static-router/router.js +58 -63
- package/dist/router/trie-router/index.js +5 -1
- package/dist/router/trie-router/node.js +147 -158
- package/dist/router/trie-router/router.js +23 -19
- package/dist/router.js +12 -5
- package/dist/types.d.ts +22 -0
- package/dist/types.js +0 -0
- package/dist/utils/body.js +15 -13
- package/dist/utils/buffer.js +35 -29
- package/dist/utils/cloudflare.js +32 -32
- package/dist/utils/cookie.js +38 -33
- package/dist/utils/crypto.js +48 -41
- package/dist/utils/encode.js +70 -62
- package/dist/utils/filepath.js +18 -20
- package/dist/utils/html.js +34 -32
- package/dist/utils/http-status.js +48 -44
- package/dist/utils/json.js +71 -75
- package/dist/utils/jwt/index.js +5 -1
- package/dist/utils/jwt/jwt.js +95 -90
- package/dist/utils/jwt/types.js +47 -41
- package/dist/utils/mime.js +90 -86
- package/dist/utils/object.js +31 -32
- package/dist/utils/types.d.ts +3 -0
- package/dist/utils/types.js +0 -0
- package/dist/utils/url.js +69 -77
- package/dist/{middleware/validator → validator}/rule.d.ts +0 -0
- package/dist/validator/rule.js +57 -0
- package/dist/{middleware/validator → validator}/sanitizer.d.ts +0 -0
- package/dist/validator/sanitizer.js +7 -0
- package/dist/validator/schema.d.ts +7 -0
- package/dist/validator/schema.js +0 -0
- package/dist/{middleware/validator → validator}/validator.d.ts +10 -7
- package/dist/validator/validator.js +406 -0
- package/package.json +14 -8
- package/dist/cjs/middleware/validator/rule.js +0 -66
- package/dist/cjs/middleware/validator/sanitizer.js +0 -6
- package/dist/cjs/middleware/validator/validator.js +0 -417
- package/dist/middleware/serve-static/module.d.mts +0 -3
- package/dist/middleware/serve-static/module.mjs +0 -13
- package/dist/middleware/validator/rule.js +0 -63
- package/dist/middleware/validator/sanitizer.js +0 -3
- package/dist/middleware/validator/validator.js +0 -403
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// src/middleware/jsx/jsx-dev-runtime.ts
|
|
2
|
+
import { jsx } from "./index.js";
|
|
3
|
+
function jsxDEV(tag, props) {
|
|
4
|
+
const children = props.children ?? [];
|
|
5
|
+
delete props["children"];
|
|
6
|
+
return jsx(tag, props, children);
|
|
6
7
|
}
|
|
8
|
+
export {
|
|
9
|
+
jsxDEV
|
|
10
|
+
};
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/middleware/jsx/jsx-runtime.ts
|
|
2
|
+
import { jsxDEV } from "./jsx-dev-runtime.js";
|
|
3
|
+
import { jsxDEV as jsxDEV2 } from "./jsx-dev-runtime.js";
|
|
4
|
+
export {
|
|
5
|
+
jsxDEV as jsx,
|
|
6
|
+
jsxDEV2 as jsxs
|
|
7
|
+
};
|
|
@@ -1,56 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/middleware/jwt/index.ts
|
|
2
|
+
import { Jwt } from "../../utils/jwt/index.js";
|
|
3
|
+
var jwt = (options) => {
|
|
4
|
+
if (!options) {
|
|
5
|
+
throw new Error('JWT auth middleware requires options for "secret');
|
|
6
|
+
}
|
|
7
|
+
if (!crypto.subtle || !crypto.subtle.importKey) {
|
|
8
|
+
throw new Error("`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.");
|
|
9
|
+
}
|
|
10
|
+
return async (ctx, next) => {
|
|
11
|
+
const credentials = ctx.req.headers.get("Authorization");
|
|
12
|
+
let token;
|
|
13
|
+
if (credentials) {
|
|
14
|
+
const parts = credentials.split(/\s+/);
|
|
15
|
+
if (parts.length !== 2) {
|
|
16
|
+
return new Response("Unauthorized", {
|
|
17
|
+
status: 401,
|
|
18
|
+
headers: {
|
|
19
|
+
"WWW-Authenticate": `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
} else {
|
|
23
|
+
token = parts[1];
|
|
24
|
+
}
|
|
25
|
+
} else if (options.cookie) {
|
|
26
|
+
token = ctx.req.cookie(options.cookie);
|
|
5
27
|
}
|
|
6
|
-
if (!
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
let token;
|
|
12
|
-
if (credentials) {
|
|
13
|
-
const parts = credentials.split(/\s+/);
|
|
14
|
-
if (parts.length !== 2) {
|
|
15
|
-
return new Response('Unauthorized', {
|
|
16
|
-
status: 401,
|
|
17
|
-
headers: {
|
|
18
|
-
'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`,
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
token = parts[1];
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
else if (options.cookie) {
|
|
27
|
-
token = ctx.req.cookie(options.cookie);
|
|
28
|
-
}
|
|
29
|
-
if (!token) {
|
|
30
|
-
return new Response('Unauthorized', {
|
|
31
|
-
status: 401,
|
|
32
|
-
headers: {
|
|
33
|
-
'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
let authorized = false;
|
|
38
|
-
let msg = '';
|
|
39
|
-
try {
|
|
40
|
-
authorized = await Jwt.verify(token, options.secret, options.alg);
|
|
41
|
-
}
|
|
42
|
-
catch (e) {
|
|
43
|
-
msg = `${e}`;
|
|
28
|
+
if (!token) {
|
|
29
|
+
return new Response("Unauthorized", {
|
|
30
|
+
status: 401,
|
|
31
|
+
headers: {
|
|
32
|
+
"WWW-Authenticate": `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`
|
|
44
33
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
let authorized = false;
|
|
37
|
+
let msg = "";
|
|
38
|
+
try {
|
|
39
|
+
authorized = await Jwt.verify(token, options.secret, options.alg);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
msg = `${e}`;
|
|
42
|
+
}
|
|
43
|
+
if (!authorized) {
|
|
44
|
+
return new Response("Unauthorized", {
|
|
45
|
+
status: 401,
|
|
46
|
+
statusText: msg,
|
|
47
|
+
headers: {
|
|
48
|
+
"WWW-Authenticate": `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`
|
|
53
49
|
}
|
|
54
|
-
|
|
55
|
-
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
await next();
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
export {
|
|
56
|
+
jwt
|
|
56
57
|
};
|
|
@@ -1,45 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
})(LogPrefix || (LogPrefix = {}));
|
|
8
|
-
const humanize = (times) => {
|
|
9
|
-
const [delimiter, separator] = [',', '.'];
|
|
10
|
-
const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter));
|
|
11
|
-
return orderTimes.join(separator);
|
|
1
|
+
// src/middleware/logger/index.ts
|
|
2
|
+
import { getPathFromURL } from "../../utils/url.js";
|
|
3
|
+
var humanize = (times) => {
|
|
4
|
+
const [delimiter, separator] = [",", "."];
|
|
5
|
+
const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1" + delimiter));
|
|
6
|
+
return orderTimes.join(separator);
|
|
12
7
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
var time = (start) => {
|
|
9
|
+
const delta = Date.now() - start;
|
|
10
|
+
return humanize([delta < 1e3 ? delta + "ms" : Math.round(delta / 1e3) + "s"]);
|
|
16
11
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
var colorStatus = (status) => {
|
|
13
|
+
const out = {
|
|
14
|
+
7: `\x1B[35m${status}\x1B[0m`,
|
|
15
|
+
5: `\x1B[31m${status}\x1B[0m`,
|
|
16
|
+
4: `\x1B[33m${status}\x1B[0m`,
|
|
17
|
+
3: `\x1B[36m${status}\x1B[0m`,
|
|
18
|
+
2: `\x1B[32m${status}\x1B[0m`,
|
|
19
|
+
1: `\x1B[32m${status}\x1B[0m`,
|
|
20
|
+
0: `\x1B[33m${status}\x1B[0m`
|
|
21
|
+
};
|
|
22
|
+
const calculateStatus = status / 100 | 0;
|
|
23
|
+
return out[calculateStatus];
|
|
29
24
|
};
|
|
30
25
|
function log(fn, prefix, method, path, status = 0, elapsed) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
: ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
|
|
34
|
-
fn(out);
|
|
26
|
+
const out = prefix === "<--" /* Incoming */ ? ` ${prefix} ${method} ${path}` : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
|
|
27
|
+
fn(out);
|
|
35
28
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
29
|
+
var logger = (fn = console.log) => {
|
|
30
|
+
return async (c, next) => {
|
|
31
|
+
const { method } = c.req;
|
|
32
|
+
const path = getPathFromURL(c.req.url);
|
|
33
|
+
log(fn, "<--" /* Incoming */, method, path);
|
|
34
|
+
const start = Date.now();
|
|
35
|
+
await next();
|
|
36
|
+
log(fn, "-->" /* Outgoing */, method, path, c.res.status, time(start));
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export {
|
|
40
|
+
logger
|
|
45
41
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { MiddlewareHandler } from '../../
|
|
1
|
+
import type { MiddlewareHandler } from '../../types';
|
|
2
2
|
export declare const poweredBy: () => MiddlewareHandler;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// src/middleware/powered-by/index.ts
|
|
2
|
+
var poweredBy = () => {
|
|
3
|
+
return async (c, next) => {
|
|
4
|
+
await next();
|
|
5
|
+
c.res.headers.append("X-Powered-By", "Hono");
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
poweredBy
|
|
6
10
|
};
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
// src/middleware/pretty-json/index.ts
|
|
2
|
+
var prettyJSON = (options = { space: 2 }) => {
|
|
3
|
+
return async (c, next) => {
|
|
4
|
+
const pretty = c.req.query("pretty") || c.req.query("pretty") === "" ? true : false;
|
|
5
|
+
c.pretty(pretty, options.space);
|
|
6
|
+
await next();
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export {
|
|
10
|
+
prettyJSON
|
|
7
11
|
};
|
|
@@ -1,38 +1,37 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
// src/middleware/serve-static/bun.ts
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
import { getFilePath } from "../../utils/filepath.js";
|
|
4
|
+
import { getMimeType } from "../../utils/mime.js";
|
|
5
|
+
var { file } = Bun;
|
|
6
|
+
var DEFAULT_DOCUMENT = "index.html";
|
|
7
|
+
var serveStatic = (options = { root: "" }) => {
|
|
8
|
+
return async (c, next) => {
|
|
9
|
+
if (c.finalized) {
|
|
10
|
+
await next();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const url = new URL(c.req.url);
|
|
14
|
+
let path = getFilePath({
|
|
15
|
+
filename: options.path ?? url.pathname,
|
|
16
|
+
root: options.root,
|
|
17
|
+
defaultDocument: DEFAULT_DOCUMENT
|
|
18
|
+
});
|
|
19
|
+
path = `./${path}`;
|
|
20
|
+
if (existsSync(path)) {
|
|
21
|
+
const content = file(path);
|
|
22
|
+
if (content) {
|
|
23
|
+
const mimeType = getMimeType(path);
|
|
24
|
+
if (mimeType) {
|
|
25
|
+
c.header("Content-Type", mimeType);
|
|
15
26
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const mimeType = getMimeType(path);
|
|
27
|
-
if (mimeType) {
|
|
28
|
-
c.header('Content-Type', mimeType);
|
|
29
|
-
}
|
|
30
|
-
// Return Response object
|
|
31
|
-
return c.body(content);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
console.warn(`Static file: ${path} is not found`);
|
|
35
|
-
await next();
|
|
36
|
-
return;
|
|
37
|
-
};
|
|
27
|
+
return c.body(content);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
console.warn(`Static file: ${path} is not found`);
|
|
31
|
+
await next();
|
|
32
|
+
return;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export {
|
|
36
|
+
serveStatic
|
|
38
37
|
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/middleware/serve-static/module.ts
|
|
2
|
+
import manifest from "__STATIC_CONTENT_MANIFEST";
|
|
3
|
+
import { serveStatic } from "./serve-static.js";
|
|
4
|
+
var module = (options = { root: "" }) => {
|
|
5
|
+
return serveStatic({
|
|
6
|
+
root: options.root,
|
|
7
|
+
path: options.path,
|
|
8
|
+
manifest: options.manifest ? options.manifest : manifest
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
module as serveStatic
|
|
13
|
+
};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import type {
|
|
3
|
-
import type { Next } from '../../hono';
|
|
2
|
+
import type { MiddlewareHandler } from '../../types';
|
|
4
3
|
export declare type ServeStaticOptions = {
|
|
5
4
|
root?: string;
|
|
6
5
|
path?: string;
|
|
7
6
|
manifest?: object | string;
|
|
8
7
|
namespace?: KVNamespace;
|
|
9
8
|
};
|
|
10
|
-
export declare const serveStatic: (options?: ServeStaticOptions) =>
|
|
9
|
+
export declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
// src/middleware/serve-static/serve-static.ts
|
|
2
|
+
import { getContentFromKVAsset } from "../../utils/cloudflare.js";
|
|
3
|
+
import { getFilePath } from "../../utils/filepath.js";
|
|
4
|
+
import { getMimeType } from "../../utils/mime.js";
|
|
5
|
+
var DEFAULT_DOCUMENT = "index.html";
|
|
6
|
+
var serveStatic = (options = { root: "" }) => {
|
|
7
|
+
return async (c, next) => {
|
|
8
|
+
if (c.finalized) {
|
|
9
|
+
await next();
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const url = new URL(c.req.url);
|
|
13
|
+
const path = getFilePath({
|
|
14
|
+
filename: options.path ?? url.pathname,
|
|
15
|
+
root: options.root,
|
|
16
|
+
defaultDocument: DEFAULT_DOCUMENT
|
|
17
|
+
});
|
|
18
|
+
const content = await getContentFromKVAsset(path, {
|
|
19
|
+
manifest: options.manifest,
|
|
20
|
+
namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : void 0
|
|
21
|
+
});
|
|
22
|
+
if (content) {
|
|
23
|
+
const mimeType = getMimeType(path);
|
|
24
|
+
if (mimeType) {
|
|
25
|
+
c.header("Content-Type", mimeType);
|
|
26
|
+
}
|
|
27
|
+
return c.body(content);
|
|
28
|
+
} else {
|
|
29
|
+
console.warn(`Static file: ${path} is not found`);
|
|
30
|
+
await next();
|
|
31
|
+
}
|
|
32
|
+
return;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export {
|
|
36
|
+
serveStatic
|
|
37
37
|
};
|
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
import type { Context } from '../../context';
|
|
2
|
-
import type { Environment,
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
[key: string]: VString | VNumber | VBoolean | VStringArray | VNumberArray | VBooleanArray | Schema | VObject<Schema> | VArray<Schema>;
|
|
7
|
-
};
|
|
8
|
-
declare type SchemaToProp<T> = {
|
|
9
|
-
[K in keyof T]: T[K] extends VNumberArray ? number[] : T[K] extends VBooleanArray ? boolean[] : T[K] extends VStringArray ? string[] : T[K] extends VNumber ? number : T[K] extends VBoolean ? boolean : T[K] extends VString ? string : T[K] extends VObjectBase<Schema> ? T[K]['container'] extends VNumber ? number : T[K]['container'] extends VString ? string : T[K]['container'] extends VBoolean ? boolean : T[K] extends VArray<Schema> ? SchemaToProp<ReadonlyArray<T[K]['container']>> : T[K] extends VObject<Schema> ? SchemaToProp<T[K]['container']> : T[K] extends Schema ? SchemaToProp<T[K]> : never : SchemaToProp<T[K]>;
|
|
10
|
-
};
|
|
2
|
+
import type { Environment, MiddlewareHandler } from '../../types';
|
|
3
|
+
import type { Schema } from '../../validator/schema';
|
|
4
|
+
import type { ValidateResult } from '../../validator/validator';
|
|
5
|
+
import { Validator } from '../../validator/validator';
|
|
11
6
|
declare type ResultSet = {
|
|
12
7
|
hasError: boolean;
|
|
13
8
|
messages: string[];
|
|
14
9
|
results: ValidateResult[];
|
|
15
10
|
};
|
|
16
|
-
declare type Done<
|
|
17
|
-
declare type ValidationFunction<
|
|
18
|
-
declare
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} | undefined) => MiddlewareHandler<SchemaToProp<T>, Env>;
|
|
11
|
+
declare type Done<P extends string, E extends Partial<Environment> = Environment> = (resultSet: ResultSet, c: Context<P, E>) => Response | void;
|
|
12
|
+
declare type ValidationFunction<P extends string, E extends Partial<Environment> = Environment, S extends Schema = Schema> = (v: Validator, c: Context<P, E>) => S;
|
|
13
|
+
export declare const validatorMiddleware: <P extends string, E extends Partial<Environment> = Environment, S extends Schema = Schema>(validationFunction: ValidationFunction<P, E, S>, options?: {
|
|
14
|
+
done?: Done<P, E> | undefined;
|
|
15
|
+
} | undefined) => MiddlewareHandler<string, E, S>;
|
|
22
16
|
export {};
|