hono 4.7.1 → 4.7.3
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 +8 -14
- package/dist/cjs/helper/proxy/index.js +23 -9
- package/dist/cjs/hono-base.js +3 -4
- package/dist/cjs/router/pattern-router/router.js +5 -3
- package/dist/cjs/utils/jwt/jwt.js +1 -1
- package/dist/cjs/utils/url.js +1 -1
- package/dist/compose.js +8 -14
- package/dist/helper/proxy/index.js +23 -9
- package/dist/hono-base.js +3 -4
- package/dist/router/pattern-router/router.js +5 -3
- package/dist/types/adapter/bun/websocket.d.ts +1 -1
- package/dist/types/compose.d.ts +4 -19
- package/dist/types/helper/websocket/index.d.ts +1 -1
- package/dist/utils/jwt/jwt.js +1 -1
- package/dist/utils/url.js +1 -1
- package/package.json +1 -1
package/dist/cjs/compose.js
CHANGED
|
@@ -21,11 +21,9 @@ __export(compose_exports, {
|
|
|
21
21
|
compose: () => compose
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(compose_exports);
|
|
24
|
-
var import_context = require("./context");
|
|
25
24
|
const compose = (middleware, onError, onNotFound) => {
|
|
26
25
|
return (context, next) => {
|
|
27
26
|
let index = -1;
|
|
28
|
-
const isContext = context instanceof import_context.Context;
|
|
29
27
|
return dispatch(0);
|
|
30
28
|
async function dispatch(i) {
|
|
31
29
|
if (i <= index) {
|
|
@@ -37,23 +35,15 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
37
35
|
let handler;
|
|
38
36
|
if (middleware[i]) {
|
|
39
37
|
handler = middleware[i][0][0];
|
|
40
|
-
|
|
41
|
-
context.req.routeIndex = i;
|
|
42
|
-
}
|
|
38
|
+
context.req.routeIndex = i;
|
|
43
39
|
} else {
|
|
44
40
|
handler = i === middleware.length && next || void 0;
|
|
45
41
|
}
|
|
46
|
-
if (
|
|
47
|
-
if (isContext && context.finalized === false && onNotFound) {
|
|
48
|
-
res = await onNotFound(context);
|
|
49
|
-
}
|
|
50
|
-
} else {
|
|
42
|
+
if (handler) {
|
|
51
43
|
try {
|
|
52
|
-
res = await handler(context, () =>
|
|
53
|
-
return dispatch(i + 1);
|
|
54
|
-
});
|
|
44
|
+
res = await handler(context, () => dispatch(i + 1));
|
|
55
45
|
} catch (err) {
|
|
56
|
-
if (err instanceof Error &&
|
|
46
|
+
if (err instanceof Error && onError) {
|
|
57
47
|
context.error = err;
|
|
58
48
|
res = await onError(err, context);
|
|
59
49
|
isError = true;
|
|
@@ -61,6 +51,10 @@ const compose = (middleware, onError, onNotFound) => {
|
|
|
61
51
|
throw err;
|
|
62
52
|
}
|
|
63
53
|
}
|
|
54
|
+
} else {
|
|
55
|
+
if (context.finalized === false && onNotFound) {
|
|
56
|
+
res = await onNotFound(context);
|
|
57
|
+
}
|
|
64
58
|
}
|
|
65
59
|
if (res && (context.finalized === false || isError)) {
|
|
66
60
|
context.res = res;
|
|
@@ -42,18 +42,31 @@ const buildRequestInitFromRequest = (request) => {
|
|
|
42
42
|
method: request.method,
|
|
43
43
|
body: request.body,
|
|
44
44
|
duplex: request.body ? "half" : void 0,
|
|
45
|
-
headers
|
|
45
|
+
headers,
|
|
46
|
+
signal: request.signal
|
|
46
47
|
};
|
|
47
48
|
};
|
|
49
|
+
const preprocessRequestInit = (requestInit) => {
|
|
50
|
+
if (!requestInit.headers || Array.isArray(requestInit.headers) || requestInit.headers instanceof Headers) {
|
|
51
|
+
return requestInit;
|
|
52
|
+
}
|
|
53
|
+
const headers = new Headers();
|
|
54
|
+
for (const [key, value] of Object.entries(requestInit.headers)) {
|
|
55
|
+
if (value == null) {
|
|
56
|
+
headers.delete(key);
|
|
57
|
+
} else {
|
|
58
|
+
headers.set(key, value);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
requestInit.headers = headers;
|
|
62
|
+
return requestInit;
|
|
63
|
+
};
|
|
48
64
|
const proxy = async (input, proxyInit) => {
|
|
49
65
|
const { raw, ...requestInit } = proxyInit ?? {};
|
|
50
|
-
const req = new Request(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...requestInit
|
|
55
|
-
}
|
|
56
|
-
);
|
|
66
|
+
const req = new Request(input, {
|
|
67
|
+
...buildRequestInitFromRequest(raw),
|
|
68
|
+
...preprocessRequestInit(requestInit)
|
|
69
|
+
});
|
|
57
70
|
req.headers.delete("accept-encoding");
|
|
58
71
|
const res = await fetch(req);
|
|
59
72
|
const resHeaders = new Headers(res.headers);
|
|
@@ -65,7 +78,8 @@ const proxy = async (input, proxyInit) => {
|
|
|
65
78
|
resHeaders.delete("content-length");
|
|
66
79
|
}
|
|
67
80
|
return new Response(res.body, {
|
|
68
|
-
|
|
81
|
+
status: res.status,
|
|
82
|
+
statusText: res.statusText,
|
|
69
83
|
headers: resHeaders
|
|
70
84
|
});
|
|
71
85
|
};
|
package/dist/cjs/hono-base.js
CHANGED
|
@@ -89,10 +89,9 @@ class Hono {
|
|
|
89
89
|
});
|
|
90
90
|
return this;
|
|
91
91
|
};
|
|
92
|
-
const strict = options
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
this.getPath = strict ? options.getPath ?? import_url.getPath : import_url.getPathNoStrict;
|
|
92
|
+
const { strict, ...optionsWithoutStrict } = options;
|
|
93
|
+
Object.assign(this, optionsWithoutStrict);
|
|
94
|
+
this.getPath = strict ?? true ? options.getPath ?? import_url.getPath : import_url.getPathNoStrict;
|
|
96
95
|
}
|
|
97
96
|
#clone() {
|
|
98
97
|
const clone = new Hono({
|
|
@@ -41,13 +41,15 @@ class PatternRouter {
|
|
|
41
41
|
return match ? `/(?<${match[1]}>${match[2] || "[^/]+"})` : part === "/*" ? "/[^/]+" : part.replace(/[.\\+*[^\]$()]/g, "\\$&");
|
|
42
42
|
}
|
|
43
43
|
);
|
|
44
|
-
let re;
|
|
45
44
|
try {
|
|
46
|
-
|
|
45
|
+
this.#routes.push([
|
|
46
|
+
new RegExp(`^${parts.join("")}${endsWithWildcard ? "" : "/?$"}`),
|
|
47
|
+
method,
|
|
48
|
+
handler
|
|
49
|
+
]);
|
|
47
50
|
} catch {
|
|
48
51
|
throw new import_router.UnsupportedPathError();
|
|
49
52
|
}
|
|
50
|
-
this.#routes.push([re, method, handler]);
|
|
51
53
|
}
|
|
52
54
|
match(method, path) {
|
|
53
55
|
const handlers = [];
|
|
@@ -31,7 +31,7 @@ var import_jwa = require("./jwa");
|
|
|
31
31
|
var import_jws = require("./jws");
|
|
32
32
|
var import_types = require("./types");
|
|
33
33
|
var import_utf8 = require("./utf8");
|
|
34
|
-
const encodeJwtPart = (part) => (0, import_encode.encodeBase64Url)(import_utf8.utf8Encoder.encode(JSON.stringify(part))).replace(/=/g, "");
|
|
34
|
+
const encodeJwtPart = (part) => (0, import_encode.encodeBase64Url)(import_utf8.utf8Encoder.encode(JSON.stringify(part)).buffer).replace(/=/g, "");
|
|
35
35
|
const encodeSignaturePart = (buf) => (0, import_encode.encodeBase64Url)(buf).replace(/=/g, "");
|
|
36
36
|
const decodeJwtPart = (part) => JSON.parse(import_utf8.utf8Decoder.decode((0, import_encode.decodeBase64Url)(part)));
|
|
37
37
|
function isTokenHeader(obj) {
|
package/dist/cjs/utils/url.js
CHANGED
|
@@ -129,7 +129,7 @@ const mergePath = (base, sub, ...rest) => {
|
|
|
129
129
|
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
130
130
|
};
|
|
131
131
|
const checkOptionalParameter = (path) => {
|
|
132
|
-
if (!path.
|
|
132
|
+
if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) {
|
|
133
133
|
return null;
|
|
134
134
|
}
|
|
135
135
|
const segments = path.split("/");
|
package/dist/compose.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
// src/compose.ts
|
|
2
|
-
import { Context } from "./context.js";
|
|
3
2
|
var compose = (middleware, onError, onNotFound) => {
|
|
4
3
|
return (context, next) => {
|
|
5
4
|
let index = -1;
|
|
6
|
-
const isContext = context instanceof Context;
|
|
7
5
|
return dispatch(0);
|
|
8
6
|
async function dispatch(i) {
|
|
9
7
|
if (i <= index) {
|
|
@@ -15,23 +13,15 @@ var compose = (middleware, onError, onNotFound) => {
|
|
|
15
13
|
let handler;
|
|
16
14
|
if (middleware[i]) {
|
|
17
15
|
handler = middleware[i][0][0];
|
|
18
|
-
|
|
19
|
-
context.req.routeIndex = i;
|
|
20
|
-
}
|
|
16
|
+
context.req.routeIndex = i;
|
|
21
17
|
} else {
|
|
22
18
|
handler = i === middleware.length && next || void 0;
|
|
23
19
|
}
|
|
24
|
-
if (
|
|
25
|
-
if (isContext && context.finalized === false && onNotFound) {
|
|
26
|
-
res = await onNotFound(context);
|
|
27
|
-
}
|
|
28
|
-
} else {
|
|
20
|
+
if (handler) {
|
|
29
21
|
try {
|
|
30
|
-
res = await handler(context, () =>
|
|
31
|
-
return dispatch(i + 1);
|
|
32
|
-
});
|
|
22
|
+
res = await handler(context, () => dispatch(i + 1));
|
|
33
23
|
} catch (err) {
|
|
34
|
-
if (err instanceof Error &&
|
|
24
|
+
if (err instanceof Error && onError) {
|
|
35
25
|
context.error = err;
|
|
36
26
|
res = await onError(err, context);
|
|
37
27
|
isError = true;
|
|
@@ -39,6 +29,10 @@ var compose = (middleware, onError, onNotFound) => {
|
|
|
39
29
|
throw err;
|
|
40
30
|
}
|
|
41
31
|
}
|
|
32
|
+
} else {
|
|
33
|
+
if (context.finalized === false && onNotFound) {
|
|
34
|
+
res = await onNotFound(context);
|
|
35
|
+
}
|
|
42
36
|
}
|
|
43
37
|
if (res && (context.finalized === false || isError)) {
|
|
44
38
|
context.res = res;
|
|
@@ -20,18 +20,31 @@ var buildRequestInitFromRequest = (request) => {
|
|
|
20
20
|
method: request.method,
|
|
21
21
|
body: request.body,
|
|
22
22
|
duplex: request.body ? "half" : void 0,
|
|
23
|
-
headers
|
|
23
|
+
headers,
|
|
24
|
+
signal: request.signal
|
|
24
25
|
};
|
|
25
26
|
};
|
|
27
|
+
var preprocessRequestInit = (requestInit) => {
|
|
28
|
+
if (!requestInit.headers || Array.isArray(requestInit.headers) || requestInit.headers instanceof Headers) {
|
|
29
|
+
return requestInit;
|
|
30
|
+
}
|
|
31
|
+
const headers = new Headers();
|
|
32
|
+
for (const [key, value] of Object.entries(requestInit.headers)) {
|
|
33
|
+
if (value == null) {
|
|
34
|
+
headers.delete(key);
|
|
35
|
+
} else {
|
|
36
|
+
headers.set(key, value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
requestInit.headers = headers;
|
|
40
|
+
return requestInit;
|
|
41
|
+
};
|
|
26
42
|
var proxy = async (input, proxyInit) => {
|
|
27
43
|
const { raw, ...requestInit } = proxyInit ?? {};
|
|
28
|
-
const req = new Request(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
...requestInit
|
|
33
|
-
}
|
|
34
|
-
);
|
|
44
|
+
const req = new Request(input, {
|
|
45
|
+
...buildRequestInitFromRequest(raw),
|
|
46
|
+
...preprocessRequestInit(requestInit)
|
|
47
|
+
});
|
|
35
48
|
req.headers.delete("accept-encoding");
|
|
36
49
|
const res = await fetch(req);
|
|
37
50
|
const resHeaders = new Headers(res.headers);
|
|
@@ -43,7 +56,8 @@ var proxy = async (input, proxyInit) => {
|
|
|
43
56
|
resHeaders.delete("content-length");
|
|
44
57
|
}
|
|
45
58
|
return new Response(res.body, {
|
|
46
|
-
|
|
59
|
+
status: res.status,
|
|
60
|
+
statusText: res.statusText,
|
|
47
61
|
headers: resHeaders
|
|
48
62
|
});
|
|
49
63
|
};
|
package/dist/hono-base.js
CHANGED
|
@@ -67,10 +67,9 @@ var Hono = class {
|
|
|
67
67
|
});
|
|
68
68
|
return this;
|
|
69
69
|
};
|
|
70
|
-
const strict = options
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.getPath = strict ? options.getPath ?? getPath : getPathNoStrict;
|
|
70
|
+
const { strict, ...optionsWithoutStrict } = options;
|
|
71
|
+
Object.assign(this, optionsWithoutStrict);
|
|
72
|
+
this.getPath = strict ?? true ? options.getPath ?? getPath : getPathNoStrict;
|
|
74
73
|
}
|
|
75
74
|
#clone() {
|
|
76
75
|
const clone = new Hono({
|
|
@@ -19,13 +19,15 @@ var PatternRouter = class {
|
|
|
19
19
|
return match ? `/(?<${match[1]}>${match[2] || "[^/]+"})` : part === "/*" ? "/[^/]+" : part.replace(/[.\\+*[^\]$()]/g, "\\$&");
|
|
20
20
|
}
|
|
21
21
|
);
|
|
22
|
-
let re;
|
|
23
22
|
try {
|
|
24
|
-
|
|
23
|
+
this.#routes.push([
|
|
24
|
+
new RegExp(`^${parts.join("")}${endsWithWildcard ? "" : "/?$"}`),
|
|
25
|
+
method,
|
|
26
|
+
handler
|
|
27
|
+
]);
|
|
25
28
|
} catch {
|
|
26
29
|
throw new UnsupportedPathError();
|
|
27
30
|
}
|
|
28
|
-
this.#routes.push([re, method, handler]);
|
|
29
31
|
}
|
|
30
32
|
match(method, path) {
|
|
31
33
|
const handlers = [];
|
|
@@ -9,7 +9,7 @@ export interface BunServerWebSocket<T> {
|
|
|
9
9
|
data: T;
|
|
10
10
|
readyState: 0 | 1 | 2 | 3;
|
|
11
11
|
}
|
|
12
|
-
interface BunWebSocketHandler<T> {
|
|
12
|
+
export interface BunWebSocketHandler<T> {
|
|
13
13
|
open(ws: BunServerWebSocket<T>): void;
|
|
14
14
|
close(ws: BunServerWebSocket<T>, code?: number, reason?: string): void;
|
|
15
15
|
message(ws: BunServerWebSocket<T>, message: string | {
|
package/dist/types/compose.d.ts
CHANGED
|
@@ -1,31 +1,17 @@
|
|
|
1
|
+
import type { Context } from './context';
|
|
1
2
|
import type { Env, ErrorHandler, NotFoundHandler } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Interface representing the context for a composition operation.
|
|
4
|
-
*/
|
|
5
|
-
interface ComposeContext {
|
|
6
|
-
/**
|
|
7
|
-
* Indicates whether the composition process has been finalized.
|
|
8
|
-
*/
|
|
9
|
-
finalized: boolean;
|
|
10
|
-
/**
|
|
11
|
-
* The result of the composition process. The type is unknown and should be
|
|
12
|
-
* specified based on the context where this interface is used.
|
|
13
|
-
*/
|
|
14
|
-
res: unknown;
|
|
15
|
-
}
|
|
16
3
|
/**
|
|
17
4
|
* Compose middleware functions into a single function based on `koa-compose` package.
|
|
18
5
|
*
|
|
19
|
-
* @template C - The context type.
|
|
20
6
|
* @template E - The environment type.
|
|
21
7
|
*
|
|
22
8
|
* @param {[[Function, unknown], ParamIndexMap | Params][]} middleware - An array of middleware functions and their corresponding parameters.
|
|
23
9
|
* @param {ErrorHandler<E>} [onError] - An optional error handler function.
|
|
24
10
|
* @param {NotFoundHandler<E>} [onNotFound] - An optional not-found handler function.
|
|
25
11
|
*
|
|
26
|
-
* @returns {(context:
|
|
12
|
+
* @returns {(context: Context, next?: Function) => Promise<>} - A composed middleware function.
|
|
27
13
|
*/
|
|
28
|
-
export declare const compose: <
|
|
14
|
+
export declare const compose: <E extends Env = Env>(middleware: [
|
|
29
15
|
[
|
|
30
16
|
Function,
|
|
31
17
|
unknown
|
|
@@ -35,5 +21,4 @@ export declare const compose: <C extends ComposeContext, E extends Env = Env>(mi
|
|
|
35
21
|
[
|
|
36
22
|
Function
|
|
37
23
|
]
|
|
38
|
-
][], onError?: ErrorHandler<E>, onNotFound?: NotFoundHandler<E>) => ((context:
|
|
39
|
-
export {};
|
|
24
|
+
][], onError?: ErrorHandler<E>, onNotFound?: NotFoundHandler<E>) => ((context: Context, next?: Function) => Promise<Context>);
|
|
@@ -27,7 +27,7 @@ export type WSReadyState = 0 | 1 | 2 | 3;
|
|
|
27
27
|
* An argument for WSContext class
|
|
28
28
|
*/
|
|
29
29
|
export interface WSContextInit<T = unknown> {
|
|
30
|
-
send(data: string | ArrayBuffer, options: SendOptions): void;
|
|
30
|
+
send(data: string | ArrayBuffer | Uint8Array, options: SendOptions): void;
|
|
31
31
|
close(code?: number, reason?: string): void;
|
|
32
32
|
raw?: T;
|
|
33
33
|
readyState: WSReadyState;
|
package/dist/utils/jwt/jwt.js
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
JwtTokenSignatureMismatched
|
|
13
13
|
} from "./types.js";
|
|
14
14
|
import { utf8Decoder, utf8Encoder } from "./utf8.js";
|
|
15
|
-
var encodeJwtPart = (part) => encodeBase64Url(utf8Encoder.encode(JSON.stringify(part))).replace(/=/g, "");
|
|
15
|
+
var encodeJwtPart = (part) => encodeBase64Url(utf8Encoder.encode(JSON.stringify(part)).buffer).replace(/=/g, "");
|
|
16
16
|
var encodeSignaturePart = (buf) => encodeBase64Url(buf).replace(/=/g, "");
|
|
17
17
|
var decodeJwtPart = (part) => JSON.parse(utf8Decoder.decode(decodeBase64Url(part)));
|
|
18
18
|
function isTokenHeader(obj) {
|
package/dist/utils/url.js
CHANGED
|
@@ -96,7 +96,7 @@ var mergePath = (base, sub, ...rest) => {
|
|
|
96
96
|
return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
|
|
97
97
|
};
|
|
98
98
|
var checkOptionalParameter = (path) => {
|
|
99
|
-
if (!path.
|
|
99
|
+
if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) {
|
|
100
100
|
return null;
|
|
101
101
|
}
|
|
102
102
|
const segments = path.split("/");
|