hono 4.12.18 → 4.12.26
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/adapter/aws-lambda/handler.js +5 -3
- package/dist/adapter/bun/serve-static.js +1 -1
- package/dist/adapter/cloudflare-workers/serve-static.js +1 -1
- package/dist/adapter/deno/serve-static.js +1 -1
- package/dist/adapter/deno/websocket.js +5 -1
- package/dist/adapter/lambda-edge/handler.js +8 -2
- package/dist/cjs/adapter/aws-lambda/handler.js +5 -3
- package/dist/cjs/adapter/bun/serve-static.js +1 -1
- package/dist/cjs/adapter/cloudflare-workers/serve-static.js +1 -1
- package/dist/cjs/adapter/deno/serve-static.js +1 -1
- package/dist/cjs/adapter/deno/websocket.js +5 -1
- package/dist/cjs/adapter/lambda-edge/handler.js +8 -2
- package/dist/cjs/hono-base.js +9 -4
- package/dist/cjs/index.js +3 -0
- package/dist/cjs/middleware/bearer-auth/index.js +1 -1
- package/dist/cjs/middleware/cache/index.js +30 -25
- package/dist/cjs/middleware/compress/index.js +31 -5
- package/dist/cjs/middleware/cors/index.js +2 -5
- package/dist/cjs/middleware/ip-restriction/index.js +58 -28
- package/dist/cjs/middleware/jwk/jwk.js +1 -1
- package/dist/cjs/middleware/jwt/jwt.js +1 -1
- package/dist/cjs/middleware/language/language.js +10 -32
- package/dist/cjs/middleware/serve-static/index.js +1 -1
- package/dist/cjs/middleware/timing/timing.js +3 -1
- package/dist/cjs/request.js +15 -0
- package/dist/cjs/utils/compress.js +1 -1
- package/dist/cjs/utils/cookie.js +4 -4
- package/dist/cjs/utils/filepath.js +1 -1
- package/dist/cjs/utils/ipaddr.js +193 -10
- package/dist/cjs/utils/mime.js +15 -17
- package/dist/cjs/utils/stream.js +4 -2
- package/dist/hono-base.js +9 -4
- package/dist/index.js +2 -0
- package/dist/middleware/bearer-auth/index.js +1 -1
- package/dist/middleware/cache/index.js +30 -25
- package/dist/middleware/compress/index.js +30 -5
- package/dist/middleware/cors/index.js +2 -5
- package/dist/middleware/ip-restriction/index.js +60 -29
- package/dist/middleware/jwk/jwk.js +1 -1
- package/dist/middleware/jwt/jwt.js +1 -1
- package/dist/middleware/language/language.js +10 -32
- package/dist/middleware/serve-static/index.js +1 -1
- package/dist/middleware/timing/timing.js +3 -1
- package/dist/request.js +15 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/adapter/aws-lambda/handler.d.ts +1 -1
- package/dist/types/adapter/bun/serve-static.d.ts +1 -1
- package/dist/types/adapter/cloudflare-workers/serve-static.d.ts +2 -2
- package/dist/types/adapter/deno/serve-static.d.ts +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/jsx/base.d.ts +2 -2
- package/dist/types/jsx/index.d.ts +1 -1
- package/dist/types/middleware/bearer-auth/index.d.ts +6 -5
- package/dist/types/middleware/cache/index.d.ts +1 -1
- package/dist/types/middleware/compress/index.d.ts +13 -2
- package/dist/types/request.d.ts +13 -0
- package/dist/types/utils/ipaddr.d.ts +4 -0
- package/dist/types/utils/mime.d.ts +11 -11
- package/dist/utils/compress.js +1 -1
- package/dist/utils/cookie.js +4 -4
- package/dist/utils/filepath.js +1 -1
- package/dist/utils/ipaddr.js +192 -10
- package/dist/utils/mime.js +15 -17
- package/dist/utils/stream.js +4 -2
- package/package.json +22 -16
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
// src/middleware/cache/index.ts
|
|
2
2
|
var defaultCacheableStatusCodes = [200];
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
var shouldSkipCacheControl = (cacheControl) => !!cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl);
|
|
4
|
+
var parseVaryDirectives = (vary) => {
|
|
5
|
+
if (vary == null) {
|
|
6
|
+
return [];
|
|
6
7
|
}
|
|
7
|
-
|
|
8
|
-
if (cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl)) {
|
|
9
|
-
return true;
|
|
10
|
-
}
|
|
11
|
-
if (res.headers.has("Set-Cookie")) {
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
return false;
|
|
8
|
+
return (Array.isArray(vary) ? vary : vary.split(",")).map((directive) => directive.trim().toLowerCase()).filter(Boolean);
|
|
15
9
|
};
|
|
10
|
+
var shouldSkipCache = (res, optionsVaryDirectives, responseVary) => responseVary.length && (!optionsVaryDirectives || responseVary.some((name) => !optionsVaryDirectives.has(name))) || shouldSkipCacheControl(res.headers.get("Cache-Control")) || res.headers.has("Set-Cookie");
|
|
16
11
|
var cache = (options) => {
|
|
17
12
|
if (!globalThis.caches) {
|
|
18
13
|
if (options.onCacheNotAvailable === false) {
|
|
@@ -27,8 +22,9 @@ var cache = (options) => {
|
|
|
27
22
|
options.wait = false;
|
|
28
23
|
}
|
|
29
24
|
const cacheControlDirectives = options.cacheControl?.split(",").map((directive) => directive.toLowerCase());
|
|
30
|
-
const
|
|
31
|
-
|
|
25
|
+
const optionsVaryList = parseVaryDirectives(options.vary);
|
|
26
|
+
const varyDirectives = optionsVaryList.length ? new Set(optionsVaryList) : void 0;
|
|
27
|
+
if (varyDirectives?.has("*")) {
|
|
32
28
|
throw new Error(
|
|
33
29
|
'Middleware vary configuration cannot include "*", as it disallows effective caching.'
|
|
34
30
|
);
|
|
@@ -36,7 +32,7 @@ var cache = (options) => {
|
|
|
36
32
|
const cacheableStatusCodes = new Set(
|
|
37
33
|
options.cacheableStatusCodes ?? defaultCacheableStatusCodes
|
|
38
34
|
);
|
|
39
|
-
const addHeader = (c) => {
|
|
35
|
+
const addHeader = (c, responseVary) => {
|
|
40
36
|
if (cacheControlDirectives) {
|
|
41
37
|
const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0]) ?? [];
|
|
42
38
|
for (const directive of cacheControlDirectives) {
|
|
@@ -48,16 +44,18 @@ var cache = (options) => {
|
|
|
48
44
|
}
|
|
49
45
|
}
|
|
50
46
|
if (varyDirectives) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
new Set(
|
|
54
|
-
[...existingDirectives, ...varyDirectives].map((directive) => directive.toLowerCase())
|
|
55
|
-
)
|
|
56
|
-
).sort();
|
|
57
|
-
if (vary.includes("*")) {
|
|
58
|
-
c.header("Vary", "*");
|
|
47
|
+
if (responseVary.length === 0) {
|
|
48
|
+
c.header("Vary", Array.from(varyDirectives).join(", "));
|
|
59
49
|
} else {
|
|
60
|
-
|
|
50
|
+
const merged = new Set(varyDirectives);
|
|
51
|
+
for (const directive of responseVary) {
|
|
52
|
+
merged.add(directive);
|
|
53
|
+
}
|
|
54
|
+
if (merged.has("*")) {
|
|
55
|
+
c.header("Vary", "*");
|
|
56
|
+
} else {
|
|
57
|
+
c.header("Vary", Array.from(merged).join(", "));
|
|
58
|
+
}
|
|
61
59
|
}
|
|
62
60
|
}
|
|
63
61
|
};
|
|
@@ -70,6 +68,12 @@ var cache = (options) => {
|
|
|
70
68
|
if (options.keyGenerator) {
|
|
71
69
|
key = await options.keyGenerator(c);
|
|
72
70
|
}
|
|
71
|
+
if (varyDirectives) {
|
|
72
|
+
for (const directive of varyDirectives) {
|
|
73
|
+
const value = c.req.raw.headers.get(directive) ?? "";
|
|
74
|
+
key += `::${directive}=${encodeURIComponent(value)}`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
73
77
|
const cacheName = typeof options.cacheName === "function" ? await options.cacheName(c) : options.cacheName;
|
|
74
78
|
const cache3 = await caches.open(cacheName);
|
|
75
79
|
const response = await cache3.match(key);
|
|
@@ -80,8 +84,9 @@ var cache = (options) => {
|
|
|
80
84
|
if (!cacheableStatusCodes.has(c.res.status)) {
|
|
81
85
|
return;
|
|
82
86
|
}
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
const responseVary = parseVaryDirectives(c.res.headers.get("Vary"));
|
|
88
|
+
addHeader(c, responseVary);
|
|
89
|
+
if (shouldSkipCache(c.res, varyDirectives, responseVary)) {
|
|
85
90
|
return;
|
|
86
91
|
}
|
|
87
92
|
const res = c.res.clone();
|
|
@@ -1,9 +1,37 @@
|
|
|
1
1
|
// src/middleware/compress/index.ts
|
|
2
|
+
import { parseAccept } from "../../utils/accept.js";
|
|
2
3
|
import { COMPRESSIBLE_CONTENT_TYPE_REGEX } from "../../utils/compress.js";
|
|
3
4
|
var ENCODING_TYPES = ["gzip", "deflate"];
|
|
4
5
|
var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/i;
|
|
6
|
+
var selectEncoding = (header, candidates) => {
|
|
7
|
+
if (header === void 0) {
|
|
8
|
+
return void 0;
|
|
9
|
+
}
|
|
10
|
+
const accepts = parseAccept(header);
|
|
11
|
+
const wildcardQ = accepts.find((a) => a.type === "*")?.q;
|
|
12
|
+
let best;
|
|
13
|
+
for (const enc of candidates) {
|
|
14
|
+
const explicit = accepts.find((a) => a.type.toLowerCase() === enc);
|
|
15
|
+
const q = explicit ? explicit.q : wildcardQ ?? 0;
|
|
16
|
+
if (q === 1) {
|
|
17
|
+
return enc;
|
|
18
|
+
} else if (q > 0 && (!best || q > best.q)) {
|
|
19
|
+
best = { encoding: enc, q };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return best?.encoding;
|
|
23
|
+
};
|
|
5
24
|
var compress = (options) => {
|
|
6
25
|
const threshold = options?.threshold ?? 1024;
|
|
26
|
+
const candidates = options?.encoding ? [options.encoding] : ENCODING_TYPES;
|
|
27
|
+
const contentTypeFilter = options?.contentTypeFilter ?? COMPRESSIBLE_CONTENT_TYPE_REGEX;
|
|
28
|
+
const shouldCompress = typeof contentTypeFilter === "function" ? (res) => {
|
|
29
|
+
const type = res.headers.get("Content-Type");
|
|
30
|
+
return type && contentTypeFilter(type);
|
|
31
|
+
} : (res) => {
|
|
32
|
+
const type = res.headers.get("Content-Type");
|
|
33
|
+
return type && contentTypeFilter.test(type);
|
|
34
|
+
};
|
|
7
35
|
return async function compress2(ctx, next) {
|
|
8
36
|
await next();
|
|
9
37
|
const contentLength = ctx.res.headers.get("Content-Length");
|
|
@@ -16,7 +44,7 @@ var compress = (options) => {
|
|
|
16
44
|
return;
|
|
17
45
|
}
|
|
18
46
|
const accepted = ctx.req.header("Accept-Encoding");
|
|
19
|
-
const encoding =
|
|
47
|
+
const encoding = selectEncoding(accepted, candidates);
|
|
20
48
|
if (!encoding || !ctx.res.body) {
|
|
21
49
|
return;
|
|
22
50
|
}
|
|
@@ -30,14 +58,11 @@ var compress = (options) => {
|
|
|
30
58
|
}
|
|
31
59
|
};
|
|
32
60
|
};
|
|
33
|
-
var shouldCompress = (res) => {
|
|
34
|
-
const type = res.headers.get("Content-Type");
|
|
35
|
-
return type && COMPRESSIBLE_CONTENT_TYPE_REGEX.test(type);
|
|
36
|
-
};
|
|
37
61
|
var shouldTransform = (res) => {
|
|
38
62
|
const cacheControl = res.headers.get("Cache-Control");
|
|
39
63
|
return !cacheControl || !cacheControlNoTransformRegExp.test(cacheControl);
|
|
40
64
|
};
|
|
41
65
|
export {
|
|
66
|
+
COMPRESSIBLE_CONTENT_TYPE_REGEX,
|
|
42
67
|
compress
|
|
43
68
|
};
|
|
@@ -10,9 +10,6 @@ var cors = (options) => {
|
|
|
10
10
|
const findAllowOrigin = ((optsOrigin) => {
|
|
11
11
|
if (typeof optsOrigin === "string") {
|
|
12
12
|
if (optsOrigin === "*") {
|
|
13
|
-
if (opts.credentials) {
|
|
14
|
-
return (origin) => origin || null;
|
|
15
|
-
}
|
|
16
13
|
return () => optsOrigin;
|
|
17
14
|
} else {
|
|
18
15
|
return (origin) => optsOrigin === origin ? origin : null;
|
|
@@ -47,7 +44,7 @@ var cors = (options) => {
|
|
|
47
44
|
set("Access-Control-Expose-Headers", opts.exposeHeaders.join(","));
|
|
48
45
|
}
|
|
49
46
|
if (c.req.method === "OPTIONS") {
|
|
50
|
-
if (opts.origin !== "*"
|
|
47
|
+
if (opts.origin !== "*") {
|
|
51
48
|
set("Vary", "Origin");
|
|
52
49
|
}
|
|
53
50
|
if (opts.maxAge != null) {
|
|
@@ -77,7 +74,7 @@ var cors = (options) => {
|
|
|
77
74
|
});
|
|
78
75
|
}
|
|
79
76
|
await next();
|
|
80
|
-
if (opts.origin !== "*"
|
|
77
|
+
if (opts.origin !== "*") {
|
|
81
78
|
c.header("Vary", "Origin", { append: true });
|
|
82
79
|
}
|
|
83
80
|
};
|
|
@@ -6,13 +6,48 @@ import {
|
|
|
6
6
|
convertIPv6BinaryToString,
|
|
7
7
|
convertIPv6ToBinary,
|
|
8
8
|
distinctRemoteAddr,
|
|
9
|
-
isIPv4MappedIPv6
|
|
9
|
+
isIPv4MappedIPv6,
|
|
10
|
+
INVALID_IP_ADDRESS_ERROR_CODE
|
|
10
11
|
} from "../../utils/ipaddr.js";
|
|
11
|
-
var IS_CIDR_NOTATION_REGEX = /\/[
|
|
12
|
+
var IS_CIDR_NOTATION_REGEX = /\/[^/]*$/;
|
|
13
|
+
var parseCidrPrefix = (rule, prefix, max) => {
|
|
14
|
+
if (!/^[0-9]{1,3}$/.test(prefix)) {
|
|
15
|
+
throw new TypeError(`Invalid rule: ${rule}`);
|
|
16
|
+
}
|
|
17
|
+
const parsedPrefix = parseInt(prefix);
|
|
18
|
+
if (parsedPrefix > max) {
|
|
19
|
+
throw new TypeError(`Invalid rule: ${rule}`);
|
|
20
|
+
}
|
|
21
|
+
return parsedPrefix;
|
|
22
|
+
};
|
|
12
23
|
var buildMatcher = (rules) => {
|
|
13
24
|
const functionRules = [];
|
|
14
25
|
const staticRules = /* @__PURE__ */ new Set();
|
|
26
|
+
const staticIPv4Rules = /* @__PURE__ */ new Set();
|
|
27
|
+
const staticIPv6Rules = /* @__PURE__ */ new Set();
|
|
15
28
|
const cidrRules = [];
|
|
29
|
+
const registerStaticRule = (rule) => {
|
|
30
|
+
const type = distinctRemoteAddr(rule);
|
|
31
|
+
if (type === void 0) {
|
|
32
|
+
throw new TypeError(`Invalid rule: ${rule}`);
|
|
33
|
+
}
|
|
34
|
+
if (type === "IPv4") {
|
|
35
|
+
const ipv4binary = convertIPv4ToBinary(rule);
|
|
36
|
+
staticRules.add(rule);
|
|
37
|
+
staticRules.add(`::ffff:${rule}`);
|
|
38
|
+
staticIPv4Rules.add(ipv4binary);
|
|
39
|
+
staticIPv6Rules.add(0xffffn << 32n | ipv4binary);
|
|
40
|
+
} else {
|
|
41
|
+
const ipv6binary = convertIPv6ToBinary(rule);
|
|
42
|
+
const ipv6Addr = convertIPv6BinaryToString(ipv6binary);
|
|
43
|
+
staticRules.add(ipv6Addr);
|
|
44
|
+
staticIPv6Rules.add(ipv6binary);
|
|
45
|
+
if (isIPv4MappedIPv6(ipv6binary)) {
|
|
46
|
+
staticRules.add(ipv6Addr.substring(7));
|
|
47
|
+
staticIPv4Rules.add(convertIPv4MappedIPv6ToIPv4(ipv6binary));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
16
51
|
for (let rule of rules) {
|
|
17
52
|
if (rule === "*") {
|
|
18
53
|
return () => true;
|
|
@@ -22,17 +57,17 @@ var buildMatcher = (rules) => {
|
|
|
22
57
|
if (IS_CIDR_NOTATION_REGEX.test(rule)) {
|
|
23
58
|
const separatedRule = rule.split("/");
|
|
24
59
|
const addrStr = separatedRule[0];
|
|
25
|
-
const
|
|
26
|
-
if (
|
|
60
|
+
const type = distinctRemoteAddr(addrStr);
|
|
61
|
+
if (type === void 0) {
|
|
27
62
|
throw new TypeError(`Invalid rule: ${rule}`);
|
|
28
63
|
}
|
|
29
|
-
let isIPv4 =
|
|
30
|
-
let prefix =
|
|
64
|
+
let isIPv4 = type === "IPv4";
|
|
65
|
+
let prefix = parseCidrPrefix(rule, separatedRule[1], isIPv4 ? 32 : 128);
|
|
31
66
|
if (isIPv4 ? prefix === 32 : prefix === 128) {
|
|
32
67
|
rule = addrStr;
|
|
33
68
|
} else {
|
|
34
69
|
let addr = (isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary)(addrStr);
|
|
35
|
-
if (
|
|
70
|
+
if (type === "IPv6" && isIPv4MappedIPv6(addr) && prefix >= 96) {
|
|
36
71
|
isIPv4 = true;
|
|
37
72
|
addr = convertIPv4MappedIPv6ToIPv4(addr);
|
|
38
73
|
prefix -= 96;
|
|
@@ -42,21 +77,7 @@ var buildMatcher = (rules) => {
|
|
|
42
77
|
continue;
|
|
43
78
|
}
|
|
44
79
|
}
|
|
45
|
-
|
|
46
|
-
if (type === void 0) {
|
|
47
|
-
throw new TypeError(`Invalid rule: ${rule}`);
|
|
48
|
-
}
|
|
49
|
-
if (type === "IPv4") {
|
|
50
|
-
staticRules.add(rule);
|
|
51
|
-
staticRules.add(`::ffff:${rule}`);
|
|
52
|
-
} else {
|
|
53
|
-
const ipv6binary = convertIPv6ToBinary(rule);
|
|
54
|
-
const ipv6Addr = convertIPv6BinaryToString(ipv6binary);
|
|
55
|
-
staticRules.add(ipv6Addr);
|
|
56
|
-
if (isIPv4MappedIPv6(ipv6binary)) {
|
|
57
|
-
staticRules.add(ipv6Addr.substring(7));
|
|
58
|
-
}
|
|
59
|
-
}
|
|
80
|
+
registerStaticRule(rule);
|
|
60
81
|
}
|
|
61
82
|
}
|
|
62
83
|
return (remote) => {
|
|
@@ -65,6 +86,9 @@ var buildMatcher = (rules) => {
|
|
|
65
86
|
}
|
|
66
87
|
const remoteAddr = remote.binaryAddr ||= (remote.isIPv4 ? convertIPv4ToBinary : convertIPv6ToBinary)(remote.addr);
|
|
67
88
|
const remoteIPv4Addr = remote.isIPv4 || isIPv4MappedIPv6(remoteAddr) ? remote.isIPv4 ? remoteAddr : convertIPv4MappedIPv6ToIPv4(remoteAddr) : void 0;
|
|
89
|
+
if ((remote.isIPv4 ? staticIPv4Rules : staticIPv6Rules).has(remoteAddr)) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
68
92
|
for (const [isIPv4, addr, mask] of cidrRules) {
|
|
69
93
|
if (isIPv4) {
|
|
70
94
|
if (remoteIPv4Addr === void 0) {
|
|
@@ -107,14 +131,21 @@ var ipRestriction = (getIP, { denyList = [], allowList = [] }, onError) => {
|
|
|
107
131
|
}
|
|
108
132
|
const type = typeof connInfo !== "string" && connInfo.remote.addressType || distinctRemoteAddr(addr);
|
|
109
133
|
const remoteData = { addr, type, isIPv4: type === "IPv4" };
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
|
|
134
|
+
try {
|
|
135
|
+
if (denyMatcher(remoteData)) {
|
|
136
|
+
if (onError) {
|
|
137
|
+
return onError({ addr, type }, c);
|
|
138
|
+
}
|
|
139
|
+
throw blockError(c);
|
|
113
140
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
141
|
+
if (allowMatcher(remoteData)) {
|
|
142
|
+
return await next();
|
|
143
|
+
}
|
|
144
|
+
} catch (e) {
|
|
145
|
+
if (e instanceof TypeError && e.code === INVALID_IP_ADDRESS_ERROR_CODE) {
|
|
146
|
+
throw blockError(c);
|
|
147
|
+
}
|
|
148
|
+
throw e;
|
|
118
149
|
}
|
|
119
150
|
if (allowLength === 0) {
|
|
120
151
|
return await next();
|
|
@@ -17,7 +17,7 @@ var jwk = (options, init) => {
|
|
|
17
17
|
let token;
|
|
18
18
|
if (credentials) {
|
|
19
19
|
const parts = credentials.split(/\s+/);
|
|
20
|
-
if (parts.length !== 2) {
|
|
20
|
+
if (parts.length !== 2 || parts[0].toLowerCase() !== "bearer") {
|
|
21
21
|
const errDescription = "invalid credentials structure";
|
|
22
22
|
throw new HTTPException(401, {
|
|
23
23
|
message: errDescription,
|
|
@@ -20,7 +20,7 @@ var jwt = (options) => {
|
|
|
20
20
|
let token;
|
|
21
21
|
if (credentials) {
|
|
22
22
|
const parts = credentials.split(/\s+/);
|
|
23
|
-
if (parts.length !== 2) {
|
|
23
|
+
if (parts.length !== 2 || parts[0].toLowerCase() !== "bearer") {
|
|
24
24
|
const errDescription = "invalid credentials structure";
|
|
25
25
|
throw new HTTPException(401, {
|
|
26
26
|
message: errDescription,
|
|
@@ -53,20 +53,12 @@ var normalizeLanguage = (lang, options) => {
|
|
|
53
53
|
}
|
|
54
54
|
};
|
|
55
55
|
var detectFromQuery = (c, options) => {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return normalizeLanguage(query, options);
|
|
59
|
-
} catch {
|
|
60
|
-
return void 0;
|
|
61
|
-
}
|
|
56
|
+
const query = c.req.query(options.lookupQueryString);
|
|
57
|
+
return normalizeLanguage(query, options);
|
|
62
58
|
};
|
|
63
59
|
var detectFromCookie = (c, options) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return normalizeLanguage(cookie, options);
|
|
67
|
-
} catch {
|
|
68
|
-
return void 0;
|
|
69
|
-
}
|
|
60
|
+
const cookie = getCookie(c, options.lookupCookie);
|
|
61
|
+
return normalizeLanguage(cookie, options);
|
|
70
62
|
};
|
|
71
63
|
function detectFromHeader(c, options) {
|
|
72
64
|
try {
|
|
@@ -87,14 +79,10 @@ function detectFromHeader(c, options) {
|
|
|
87
79
|
}
|
|
88
80
|
}
|
|
89
81
|
function detectFromPath(c, options) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return normalizeLanguage(langSegment, options);
|
|
95
|
-
} catch {
|
|
96
|
-
return void 0;
|
|
97
|
-
}
|
|
82
|
+
const url = new URL(c.req.url);
|
|
83
|
+
const pathSegments = url.pathname.split("/").filter(Boolean);
|
|
84
|
+
const langSegment = pathSegments[options.lookupFromPathIndex];
|
|
85
|
+
return normalizeLanguage(langSegment, options);
|
|
98
86
|
}
|
|
99
87
|
var detectors = {
|
|
100
88
|
querystring: detectFromQuery,
|
|
@@ -129,9 +117,6 @@ var detectLanguage = (c, options) => {
|
|
|
129
117
|
let detectedLang;
|
|
130
118
|
for (const detectorName of options.order) {
|
|
131
119
|
const detector = detectors[detectorName];
|
|
132
|
-
if (!detector) {
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
120
|
try {
|
|
136
121
|
detectedLang = detector(c, options);
|
|
137
122
|
if (detectedLang) {
|
|
@@ -164,15 +149,8 @@ var languageDetector = (userOptions) => {
|
|
|
164
149
|
};
|
|
165
150
|
validateOptions(options);
|
|
166
151
|
return async function languageDetector2(ctx, next) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
ctx.set("language", lang);
|
|
170
|
-
} catch (error) {
|
|
171
|
-
if (options.debug) {
|
|
172
|
-
console.error("Language detection failed:", error);
|
|
173
|
-
}
|
|
174
|
-
ctx.set("language", options.fallbackLanguage);
|
|
175
|
-
}
|
|
152
|
+
const lang = detectLanguage(ctx, options);
|
|
153
|
+
ctx.set("language", lang);
|
|
176
154
|
await next();
|
|
177
155
|
};
|
|
178
156
|
};
|
|
@@ -24,7 +24,7 @@ var serveStatic = (options) => {
|
|
|
24
24
|
} else {
|
|
25
25
|
try {
|
|
26
26
|
filename = tryDecodeURI(c.req.path);
|
|
27
|
-
if (/(?:^|[\/\\])\.{1,2}(?:$|[\/\\])|[\/\\]{2,}
|
|
27
|
+
if (/(?:^|[\/\\])\.{1,2}(?:$|[\/\\])|[\/\\]{2,}|\\/.test(filename)) {
|
|
28
28
|
throw new Error();
|
|
29
29
|
}
|
|
30
30
|
} catch {
|
|
@@ -31,7 +31,9 @@ var timing = (config) => {
|
|
|
31
31
|
endTime(c, "total");
|
|
32
32
|
}
|
|
33
33
|
if (options.autoEnd) {
|
|
34
|
-
timers.forEach((_, key) =>
|
|
34
|
+
timers.forEach((_, key) => {
|
|
35
|
+
endTime(c, key);
|
|
36
|
+
});
|
|
35
37
|
}
|
|
36
38
|
const enabled = typeof options.enabled === "function" ? options.enabled(c) : options.enabled;
|
|
37
39
|
if (enabled) {
|
package/dist/request.js
CHANGED
|
@@ -147,6 +147,21 @@ var HonoRequest = class {
|
|
|
147
147
|
arrayBuffer() {
|
|
148
148
|
return this.#cachedBody("arrayBuffer");
|
|
149
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* `.bytes()` parses the request body as a `Uint8Array`.
|
|
152
|
+
*
|
|
153
|
+
* @see {@link https://hono.dev/docs/api/request#bytes}
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* app.post('/entry', async (c) => {
|
|
158
|
+
* const body = await c.req.bytes()
|
|
159
|
+
* })
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
bytes() {
|
|
163
|
+
return this.#cachedBody("arrayBuffer").then((buffer) => new Uint8Array(buffer));
|
|
164
|
+
}
|
|
150
165
|
/**
|
|
151
166
|
* Parses the request body as a `Blob`.
|
|
152
167
|
* @example
|