hono 4.6.9 â 4.6.11
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 +1 -1
- package/dist/adapter/deno/serve-static.js +3 -0
- package/dist/cjs/adapter/deno/serve-static.js +3 -0
- package/dist/cjs/hono-base.js +16 -16
- package/dist/cjs/middleware/combine/index.js +15 -9
- package/dist/cjs/router/linear-router/router.js +4 -4
- package/dist/cjs/router/reg-exp-router/node.js +18 -18
- package/dist/cjs/router/reg-exp-router/router.js +9 -8
- package/dist/cjs/router/reg-exp-router/trie.js +4 -4
- package/dist/cjs/router/smart-router/router.js +12 -11
- package/dist/cjs/router/trie-router/node.js +53 -49
- package/dist/cjs/router/trie-router/router.js +5 -5
- package/dist/hono-base.js +16 -16
- package/dist/middleware/combine/index.js +15 -9
- package/dist/router/linear-router/router.js +4 -4
- package/dist/router/reg-exp-router/node.js +18 -18
- package/dist/router/reg-exp-router/router.js +9 -8
- package/dist/router/reg-exp-router/trie.js +4 -4
- package/dist/router/smart-router/router.js +12 -11
- package/dist/router/trie-router/node.js +53 -49
- package/dist/router/trie-router/router.js +5 -5
- package/dist/types/adapter/service-worker/index.d.ts +1 -1
- package/dist/types/compose.d.ts +5 -2
- package/dist/types/helper/factory/index.d.ts +129 -66
- package/dist/types/router/linear-router/router.d.ts +0 -5
- package/dist/types/router/reg-exp-router/node.d.ts +0 -3
- package/dist/types/router/reg-exp-router/router.d.ts +0 -7
- package/dist/types/router/reg-exp-router/trie.d.ts +1 -4
- package/dist/types/router/smart-router/router.d.ts +3 -7
- package/dist/types/router/trie-router/node.d.ts +0 -12
- package/dist/types/router/trie-router/router.d.ts +0 -2
- package/dist/types/types.d.ts +1 -1
- package/package.json +24 -16
- package/perf-measures/bundle-check/generated/.gitkeep +0 -0
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ npm create hono@latest
|
|
|
47
47
|
## Features
|
|
48
48
|
|
|
49
49
|
- **Ultrafast** ð - The router `RegExpRouter` is really fast. Not using linear loops. Fast.
|
|
50
|
-
- **Lightweight** ðŠķ - The `hono/tiny` preset is under
|
|
50
|
+
- **Lightweight** ðŠķ - The `hono/tiny` preset is under 12kB. Hono has zero dependencies and uses only the Web Standard API.
|
|
51
51
|
- **Multi-runtime** ð - Works on Cloudflare Workers, Fastly Compute, Deno, Bun, AWS Lambda, Lambda@Edge, or Node.js. The same code runs on all platforms.
|
|
52
52
|
- **Batteries Included** ð - Hono has built-in middleware, custom middleware, and third-party middleware. Batteries included.
|
|
53
53
|
- **Delightful DX** ð - Super clean APIs. First-class TypeScript support. Now, we've got "Types".
|
package/dist/cjs/hono-base.js
CHANGED
|
@@ -62,9 +62,7 @@ class Hono {
|
|
|
62
62
|
this.#addRoute(method, this.#path, args1);
|
|
63
63
|
}
|
|
64
64
|
args.forEach((handler) => {
|
|
65
|
-
|
|
66
|
-
this.#addRoute(method, this.#path, handler);
|
|
67
|
-
}
|
|
65
|
+
this.#addRoute(method, this.#path, handler);
|
|
68
66
|
});
|
|
69
67
|
return this;
|
|
70
68
|
};
|
|
@@ -106,15 +104,15 @@ class Hono {
|
|
|
106
104
|
return clone;
|
|
107
105
|
}
|
|
108
106
|
#notFoundHandler = notFoundHandler;
|
|
109
|
-
|
|
107
|
+
errorHandler = errorHandler;
|
|
110
108
|
route(path, app) {
|
|
111
109
|
const subApp = this.basePath(path);
|
|
112
110
|
app.routes.map((r) => {
|
|
113
111
|
let handler;
|
|
114
|
-
if (app
|
|
112
|
+
if (app.errorHandler === errorHandler) {
|
|
115
113
|
handler = r.handler;
|
|
116
114
|
} else {
|
|
117
|
-
handler = async (c, next) => (await (0, import_compose.compose)([], app
|
|
115
|
+
handler = async (c, next) => (await (0, import_compose.compose)([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
118
116
|
handler[COMPOSED_HANDLER] = r.handler;
|
|
119
117
|
}
|
|
120
118
|
subApp.#addRoute(r.method, r.path, handler);
|
|
@@ -127,7 +125,7 @@ class Hono {
|
|
|
127
125
|
return subApp;
|
|
128
126
|
}
|
|
129
127
|
onError = (handler) => {
|
|
130
|
-
this
|
|
128
|
+
this.errorHandler = handler;
|
|
131
129
|
return this;
|
|
132
130
|
};
|
|
133
131
|
notFound = (handler) => {
|
|
@@ -184,7 +182,7 @@ class Hono {
|
|
|
184
182
|
}
|
|
185
183
|
#handleError(err, c) {
|
|
186
184
|
if (err instanceof Error) {
|
|
187
|
-
return this
|
|
185
|
+
return this.errorHandler(err, c);
|
|
188
186
|
}
|
|
189
187
|
throw err;
|
|
190
188
|
}
|
|
@@ -214,7 +212,7 @@ class Hono {
|
|
|
214
212
|
(resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c))
|
|
215
213
|
).catch((err) => this.#handleError(err, c)) : res ?? this.#notFoundHandler(c);
|
|
216
214
|
}
|
|
217
|
-
const composed = (0, import_compose.compose)(matchResult[0], this
|
|
215
|
+
const composed = (0, import_compose.compose)(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
218
216
|
return (async () => {
|
|
219
217
|
try {
|
|
220
218
|
const context = await composed(c);
|
|
@@ -234,15 +232,17 @@ class Hono {
|
|
|
234
232
|
};
|
|
235
233
|
request = (input, requestInit, Env, executionCtx) => {
|
|
236
234
|
if (input instanceof Request) {
|
|
237
|
-
|
|
238
|
-
input = new Request(input, requestInit);
|
|
239
|
-
}
|
|
240
|
-
return this.fetch(input, Env, executionCtx);
|
|
235
|
+
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
241
236
|
}
|
|
242
237
|
input = input.toString();
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
238
|
+
return this.fetch(
|
|
239
|
+
new Request(
|
|
240
|
+
/^https?:\/\//.test(input) ? input : `http://localhost${(0, import_url.mergePath)("/", input)}`,
|
|
241
|
+
requestInit
|
|
242
|
+
),
|
|
243
|
+
Env,
|
|
244
|
+
executionCtx
|
|
245
|
+
);
|
|
246
246
|
};
|
|
247
247
|
fire = () => {
|
|
248
248
|
addEventListener("fetch", (event) => {
|
|
@@ -51,16 +51,22 @@ const some = (...middleware) => {
|
|
|
51
51
|
};
|
|
52
52
|
};
|
|
53
53
|
const every = (...middleware) => {
|
|
54
|
-
const wrappedMiddleware = middleware.map((m) => async (c, next) => {
|
|
55
|
-
const res = await m(c, next);
|
|
56
|
-
if (res === false) {
|
|
57
|
-
throw new Error("Unmet condition");
|
|
58
|
-
}
|
|
59
|
-
return res;
|
|
60
|
-
});
|
|
61
|
-
const handler = async (c, next) => (0, import_compose.compose)(wrappedMiddleware.map((m) => [[m, void 0], c.req.param()]))(c, next);
|
|
62
54
|
return async function every2(c, next) {
|
|
63
|
-
|
|
55
|
+
const currentRouteIndex = c.req.routeIndex;
|
|
56
|
+
await (0, import_compose.compose)(
|
|
57
|
+
middleware.map((m) => [
|
|
58
|
+
[
|
|
59
|
+
async (c2, next2) => {
|
|
60
|
+
c2.req.routeIndex = currentRouteIndex;
|
|
61
|
+
const res = await m(c2, next2);
|
|
62
|
+
if (res === false) {
|
|
63
|
+
throw new Error("Unmet condition");
|
|
64
|
+
}
|
|
65
|
+
return res;
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
])
|
|
69
|
+
)(c, next);
|
|
64
70
|
};
|
|
65
71
|
};
|
|
66
72
|
const except = (condition, ...middleware) => {
|
|
@@ -28,17 +28,17 @@ const splitPathRe = /\/(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/[^\/\?]+|(\?)/g;
|
|
|
28
28
|
const splitByStarRe = /\*/;
|
|
29
29
|
class LinearRouter {
|
|
30
30
|
name = "LinearRouter";
|
|
31
|
-
routes = [];
|
|
31
|
+
#routes = [];
|
|
32
32
|
add(method, path, handler) {
|
|
33
33
|
for (let i = 0, paths = (0, import_url.checkOptionalParameter)(path) || [path], len = paths.length; i < len; i++) {
|
|
34
|
-
this
|
|
34
|
+
this.#routes.push([method, paths[i], handler]);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
match(method, path) {
|
|
38
38
|
const handlers = [];
|
|
39
39
|
ROUTES_LOOP:
|
|
40
|
-
for (let i = 0, len = this
|
|
41
|
-
const [routeMethod, routePath, handler] = this
|
|
40
|
+
for (let i = 0, len = this.#routes.length; i < len; i++) {
|
|
41
|
+
const [routeMethod, routePath, handler] = this.#routes[i];
|
|
42
42
|
if (routeMethod === method || routeMethod === import_router.METHOD_NAME_ALL) {
|
|
43
43
|
if (routePath === "*" || routePath === "/*") {
|
|
44
44
|
handlers.push([handler, emptyParams]);
|
|
@@ -47,18 +47,18 @@ function compareKey(a, b) {
|
|
|
47
47
|
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
48
48
|
}
|
|
49
49
|
class Node {
|
|
50
|
-
index;
|
|
51
|
-
varIndex;
|
|
52
|
-
children = /* @__PURE__ */ Object.create(null);
|
|
50
|
+
#index;
|
|
51
|
+
#varIndex;
|
|
52
|
+
#children = /* @__PURE__ */ Object.create(null);
|
|
53
53
|
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
54
54
|
if (tokens.length === 0) {
|
|
55
|
-
if (this
|
|
55
|
+
if (this.#index !== void 0) {
|
|
56
56
|
throw PATH_ERROR;
|
|
57
57
|
}
|
|
58
58
|
if (pathErrorCheckOnly) {
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
|
-
this
|
|
61
|
+
this.#index = index;
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
const [token, ...restTokens] = tokens;
|
|
@@ -73,9 +73,9 @@ class Node {
|
|
|
73
73
|
throw PATH_ERROR;
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
-
node = this
|
|
76
|
+
node = this.#children[regexpStr];
|
|
77
77
|
if (!node) {
|
|
78
|
-
if (Object.keys(this
|
|
78
|
+
if (Object.keys(this.#children).some(
|
|
79
79
|
(k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
|
80
80
|
)) {
|
|
81
81
|
throw PATH_ERROR;
|
|
@@ -83,18 +83,18 @@ class Node {
|
|
|
83
83
|
if (pathErrorCheckOnly) {
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
|
-
node = this
|
|
86
|
+
node = this.#children[regexpStr] = new Node();
|
|
87
87
|
if (name !== "") {
|
|
88
|
-
node
|
|
88
|
+
node.#varIndex = context.varIndex++;
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
if (!pathErrorCheckOnly && name !== "") {
|
|
92
|
-
paramMap.push([name, node
|
|
92
|
+
paramMap.push([name, node.#varIndex]);
|
|
93
93
|
}
|
|
94
94
|
} else {
|
|
95
|
-
node = this
|
|
95
|
+
node = this.#children[token];
|
|
96
96
|
if (!node) {
|
|
97
|
-
if (Object.keys(this
|
|
97
|
+
if (Object.keys(this.#children).some(
|
|
98
98
|
(k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
|
99
99
|
)) {
|
|
100
100
|
throw PATH_ERROR;
|
|
@@ -102,19 +102,19 @@ class Node {
|
|
|
102
102
|
if (pathErrorCheckOnly) {
|
|
103
103
|
return;
|
|
104
104
|
}
|
|
105
|
-
node = this
|
|
105
|
+
node = this.#children[token] = new Node();
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
|
109
109
|
}
|
|
110
110
|
buildRegExpStr() {
|
|
111
|
-
const childKeys = Object.keys(this
|
|
111
|
+
const childKeys = Object.keys(this.#children).sort(compareKey);
|
|
112
112
|
const strList = childKeys.map((k) => {
|
|
113
|
-
const c = this
|
|
114
|
-
return (typeof c
|
|
113
|
+
const c = this.#children[k];
|
|
114
|
+
return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
|
|
115
115
|
});
|
|
116
|
-
if (typeof this
|
|
117
|
-
strList.unshift(`#${this
|
|
116
|
+
if (typeof this.#index === "number") {
|
|
117
|
+
strList.unshift(`#${this.#index}`);
|
|
118
118
|
}
|
|
119
119
|
if (strList.length === 0) {
|
|
120
120
|
return "";
|
|
@@ -109,14 +109,15 @@ function findMiddleware(middleware, path) {
|
|
|
109
109
|
}
|
|
110
110
|
class RegExpRouter {
|
|
111
111
|
name = "RegExpRouter";
|
|
112
|
-
middleware;
|
|
113
|
-
routes;
|
|
112
|
+
#middleware;
|
|
113
|
+
#routes;
|
|
114
114
|
constructor() {
|
|
115
|
-
this
|
|
116
|
-
this
|
|
115
|
+
this.#middleware = { [import_router.METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
116
|
+
this.#routes = { [import_router.METHOD_NAME_ALL]: /* @__PURE__ */ Object.create(null) };
|
|
117
117
|
}
|
|
118
118
|
add(method, path, handler) {
|
|
119
|
-
const
|
|
119
|
+
const middleware = this.#middleware;
|
|
120
|
+
const routes = this.#routes;
|
|
120
121
|
if (!middleware || !routes) {
|
|
121
122
|
throw new Error(import_router.MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
122
123
|
}
|
|
@@ -191,16 +192,16 @@ class RegExpRouter {
|
|
|
191
192
|
}
|
|
192
193
|
#buildAllMatchers() {
|
|
193
194
|
const matchers = /* @__PURE__ */ Object.create(null);
|
|
194
|
-
Object.keys(this
|
|
195
|
+
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
195
196
|
matchers[method] ||= this.#buildMatcher(method);
|
|
196
197
|
});
|
|
197
|
-
this
|
|
198
|
+
this.#middleware = this.#routes = void 0;
|
|
198
199
|
return matchers;
|
|
199
200
|
}
|
|
200
201
|
#buildMatcher(method) {
|
|
201
202
|
const routes = [];
|
|
202
203
|
let hasOwnRoute = method === import_router.METHOD_NAME_ALL;
|
|
203
|
-
[this
|
|
204
|
+
[this.#middleware, this.#routes].forEach((r) => {
|
|
204
205
|
const ownRoute = r[method] ? Object.keys(r[method]).map((path) => [path, r[method][path]]) : [];
|
|
205
206
|
if (ownRoute.length !== 0) {
|
|
206
207
|
hasOwnRoute ||= true;
|
|
@@ -23,8 +23,8 @@ __export(trie_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(trie_exports);
|
|
24
24
|
var import_node = require("./node");
|
|
25
25
|
class Trie {
|
|
26
|
-
context = { varIndex: 0 };
|
|
27
|
-
root = new import_node.Node();
|
|
26
|
+
#context = { varIndex: 0 };
|
|
27
|
+
#root = new import_node.Node();
|
|
28
28
|
insert(path, index, pathErrorCheckOnly) {
|
|
29
29
|
const paramAssoc = [];
|
|
30
30
|
const groups = [];
|
|
@@ -51,11 +51,11 @@ class Trie {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
this
|
|
54
|
+
this.#root.insert(tokens, index, paramAssoc, this.#context, pathErrorCheckOnly);
|
|
55
55
|
return paramAssoc;
|
|
56
56
|
}
|
|
57
57
|
buildRegExp() {
|
|
58
|
-
let regexp = this
|
|
58
|
+
let regexp = this.#root.buildRegExpStr();
|
|
59
59
|
if (regexp === "") {
|
|
60
60
|
return [/^$/, [], []];
|
|
61
61
|
}
|
|
@@ -24,22 +24,23 @@ module.exports = __toCommonJS(router_exports);
|
|
|
24
24
|
var import_router = require("../../router");
|
|
25
25
|
class SmartRouter {
|
|
26
26
|
name = "SmartRouter";
|
|
27
|
-
routers = [];
|
|
28
|
-
routes = [];
|
|
27
|
+
#routers = [];
|
|
28
|
+
#routes = [];
|
|
29
29
|
constructor(init) {
|
|
30
|
-
|
|
30
|
+
this.#routers = init.routers;
|
|
31
31
|
}
|
|
32
32
|
add(method, path, handler) {
|
|
33
|
-
if (!this
|
|
33
|
+
if (!this.#routes) {
|
|
34
34
|
throw new Error(import_router.MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
35
35
|
}
|
|
36
|
-
this
|
|
36
|
+
this.#routes.push([method, path, handler]);
|
|
37
37
|
}
|
|
38
38
|
match(method, path) {
|
|
39
|
-
if (!this
|
|
39
|
+
if (!this.#routes) {
|
|
40
40
|
throw new Error("Fatal error");
|
|
41
41
|
}
|
|
42
|
-
const
|
|
42
|
+
const routers = this.#routers;
|
|
43
|
+
const routes = this.#routes;
|
|
43
44
|
const len = routers.length;
|
|
44
45
|
let i = 0;
|
|
45
46
|
let res;
|
|
@@ -57,8 +58,8 @@ class SmartRouter {
|
|
|
57
58
|
throw e;
|
|
58
59
|
}
|
|
59
60
|
this.match = router.match.bind(router);
|
|
60
|
-
this
|
|
61
|
-
this
|
|
61
|
+
this.#routers = [router];
|
|
62
|
+
this.#routes = void 0;
|
|
62
63
|
break;
|
|
63
64
|
}
|
|
64
65
|
if (i === len) {
|
|
@@ -68,10 +69,10 @@ class SmartRouter {
|
|
|
68
69
|
return res;
|
|
69
70
|
}
|
|
70
71
|
get activeRouter() {
|
|
71
|
-
if (this
|
|
72
|
+
if (this.#routes || this.#routers.length !== 1) {
|
|
72
73
|
throw new Error("No active router has been determined yet.");
|
|
73
74
|
}
|
|
74
|
-
return this
|
|
75
|
+
return this.#routers[0];
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -24,63 +24,60 @@ module.exports = __toCommonJS(node_exports);
|
|
|
24
24
|
var import_router = require("../../router");
|
|
25
25
|
var import_url = require("../../utils/url");
|
|
26
26
|
class Node {
|
|
27
|
-
methods;
|
|
28
|
-
children;
|
|
29
|
-
patterns;
|
|
30
|
-
order = 0;
|
|
31
|
-
params = /* @__PURE__ */ Object.create(null);
|
|
27
|
+
#methods;
|
|
28
|
+
#children;
|
|
29
|
+
#patterns;
|
|
30
|
+
#order = 0;
|
|
31
|
+
#params = /* @__PURE__ */ Object.create(null);
|
|
32
32
|
constructor(method, handler, children) {
|
|
33
|
-
this
|
|
34
|
-
this
|
|
33
|
+
this.#children = children || /* @__PURE__ */ Object.create(null);
|
|
34
|
+
this.#methods = [];
|
|
35
35
|
if (method && handler) {
|
|
36
36
|
const m = /* @__PURE__ */ Object.create(null);
|
|
37
37
|
m[method] = { handler, possibleKeys: [], score: 0 };
|
|
38
|
-
this
|
|
38
|
+
this.#methods = [m];
|
|
39
39
|
}
|
|
40
|
-
this
|
|
40
|
+
this.#patterns = [];
|
|
41
41
|
}
|
|
42
42
|
insert(method, path, handler) {
|
|
43
|
-
this
|
|
43
|
+
this.#order = ++this.#order;
|
|
44
44
|
let curNode = this;
|
|
45
45
|
const parts = (0, import_url.splitRoutingPath)(path);
|
|
46
46
|
const possibleKeys = [];
|
|
47
47
|
for (let i = 0, len = parts.length; i < len; i++) {
|
|
48
48
|
const p = parts[i];
|
|
49
|
-
if (Object.keys(curNode
|
|
50
|
-
curNode = curNode
|
|
49
|
+
if (Object.keys(curNode.#children).includes(p)) {
|
|
50
|
+
curNode = curNode.#children[p];
|
|
51
51
|
const pattern2 = (0, import_url.getPattern)(p);
|
|
52
52
|
if (pattern2) {
|
|
53
53
|
possibleKeys.push(pattern2[1]);
|
|
54
54
|
}
|
|
55
55
|
continue;
|
|
56
56
|
}
|
|
57
|
-
curNode
|
|
57
|
+
curNode.#children[p] = new Node();
|
|
58
58
|
const pattern = (0, import_url.getPattern)(p);
|
|
59
59
|
if (pattern) {
|
|
60
|
-
curNode
|
|
60
|
+
curNode.#patterns.push(pattern);
|
|
61
61
|
possibleKeys.push(pattern[1]);
|
|
62
62
|
}
|
|
63
|
-
curNode = curNode
|
|
64
|
-
}
|
|
65
|
-
if (!curNode.methods.length) {
|
|
66
|
-
curNode.methods = [];
|
|
63
|
+
curNode = curNode.#children[p];
|
|
67
64
|
}
|
|
68
65
|
const m = /* @__PURE__ */ Object.create(null);
|
|
69
66
|
const handlerSet = {
|
|
70
67
|
handler,
|
|
71
68
|
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
72
|
-
score: this
|
|
69
|
+
score: this.#order
|
|
73
70
|
};
|
|
74
71
|
m[method] = handlerSet;
|
|
75
|
-
curNode
|
|
72
|
+
curNode.#methods.push(m);
|
|
76
73
|
return curNode;
|
|
77
74
|
}
|
|
78
|
-
#
|
|
75
|
+
#getHandlerSets(node, method, nodeParams, params) {
|
|
79
76
|
const handlerSets = [];
|
|
80
|
-
for (let i = 0, len = node
|
|
81
|
-
const m = node
|
|
77
|
+
for (let i = 0, len = node.#methods.length; i < len; i++) {
|
|
78
|
+
const m = node.#methods[i];
|
|
82
79
|
const handlerSet = m[method] || m[import_router.METHOD_NAME_ALL];
|
|
83
|
-
const processedSet =
|
|
80
|
+
const processedSet = {};
|
|
84
81
|
if (handlerSet !== void 0) {
|
|
85
82
|
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
86
83
|
for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
|
|
@@ -96,7 +93,7 @@ class Node {
|
|
|
96
93
|
}
|
|
97
94
|
search(method, path) {
|
|
98
95
|
const handlerSets = [];
|
|
99
|
-
this
|
|
96
|
+
this.#params = /* @__PURE__ */ Object.create(null);
|
|
100
97
|
const curNode = this;
|
|
101
98
|
let curNodes = [curNode];
|
|
102
99
|
const parts = (0, import_url.splitPath)(path);
|
|
@@ -106,27 +103,36 @@ class Node {
|
|
|
106
103
|
const tempNodes = [];
|
|
107
104
|
for (let j = 0, len2 = curNodes.length; j < len2; j++) {
|
|
108
105
|
const node = curNodes[j];
|
|
109
|
-
const nextNode = node
|
|
106
|
+
const nextNode = node.#children[part];
|
|
110
107
|
if (nextNode) {
|
|
111
|
-
nextNode
|
|
108
|
+
nextNode.#params = node.#params;
|
|
112
109
|
if (isLast) {
|
|
113
|
-
if (nextNode
|
|
110
|
+
if (nextNode.#children["*"]) {
|
|
114
111
|
handlerSets.push(
|
|
115
|
-
...this.#
|
|
112
|
+
...this.#getHandlerSets(
|
|
113
|
+
nextNode.#children["*"],
|
|
114
|
+
method,
|
|
115
|
+
node.#params,
|
|
116
|
+
/* @__PURE__ */ Object.create(null)
|
|
117
|
+
)
|
|
116
118
|
);
|
|
117
119
|
}
|
|
118
|
-
handlerSets.push(
|
|
120
|
+
handlerSets.push(
|
|
121
|
+
...this.#getHandlerSets(nextNode, method, node.#params, /* @__PURE__ */ Object.create(null))
|
|
122
|
+
);
|
|
119
123
|
} else {
|
|
120
124
|
tempNodes.push(nextNode);
|
|
121
125
|
}
|
|
122
126
|
}
|
|
123
|
-
for (let k = 0, len3 = node
|
|
124
|
-
const pattern = node
|
|
125
|
-
const params = { ...node
|
|
127
|
+
for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {
|
|
128
|
+
const pattern = node.#patterns[k];
|
|
129
|
+
const params = { ...node.#params };
|
|
126
130
|
if (pattern === "*") {
|
|
127
|
-
const astNode = node
|
|
131
|
+
const astNode = node.#children["*"];
|
|
128
132
|
if (astNode) {
|
|
129
|
-
handlerSets.push(
|
|
133
|
+
handlerSets.push(
|
|
134
|
+
...this.#getHandlerSets(astNode, method, node.#params, /* @__PURE__ */ Object.create(null))
|
|
135
|
+
);
|
|
130
136
|
tempNodes.push(astNode);
|
|
131
137
|
}
|
|
132
138
|
continue;
|
|
@@ -135,27 +141,25 @@ class Node {
|
|
|
135
141
|
continue;
|
|
136
142
|
}
|
|
137
143
|
const [key, name, matcher] = pattern;
|
|
138
|
-
const child = node
|
|
144
|
+
const child = node.#children[key];
|
|
139
145
|
const restPathString = parts.slice(i).join("/");
|
|
140
146
|
if (matcher instanceof RegExp && matcher.test(restPathString)) {
|
|
141
147
|
params[name] = restPathString;
|
|
142
|
-
handlerSets.push(...this.#
|
|
148
|
+
handlerSets.push(...this.#getHandlerSets(child, method, node.#params, params));
|
|
143
149
|
continue;
|
|
144
150
|
}
|
|
145
151
|
if (matcher === true || matcher.test(part)) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
} else {
|
|
156
|
-
child.params = params;
|
|
157
|
-
tempNodes.push(child);
|
|
152
|
+
params[name] = part;
|
|
153
|
+
if (isLast) {
|
|
154
|
+
handlerSets.push(...this.#getHandlerSets(child, method, params, node.#params));
|
|
155
|
+
if (child.#children["*"]) {
|
|
156
|
+
handlerSets.push(
|
|
157
|
+
...this.#getHandlerSets(child.#children["*"], method, params, node.#params)
|
|
158
|
+
);
|
|
158
159
|
}
|
|
160
|
+
} else {
|
|
161
|
+
child.#params = params;
|
|
162
|
+
tempNodes.push(child);
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
}
|
|
@@ -25,22 +25,22 @@ var import_url = require("../../utils/url");
|
|
|
25
25
|
var import_node = require("./node");
|
|
26
26
|
class TrieRouter {
|
|
27
27
|
name = "TrieRouter";
|
|
28
|
-
node;
|
|
28
|
+
#node;
|
|
29
29
|
constructor() {
|
|
30
|
-
this
|
|
30
|
+
this.#node = new import_node.Node();
|
|
31
31
|
}
|
|
32
32
|
add(method, path, handler) {
|
|
33
33
|
const results = (0, import_url.checkOptionalParameter)(path);
|
|
34
34
|
if (results) {
|
|
35
35
|
for (let i = 0, len = results.length; i < len; i++) {
|
|
36
|
-
this
|
|
36
|
+
this.#node.insert(method, results[i], handler);
|
|
37
37
|
}
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
|
-
this
|
|
40
|
+
this.#node.insert(method, path, handler);
|
|
41
41
|
}
|
|
42
42
|
match(method, path) {
|
|
43
|
-
return this
|
|
43
|
+
return this.#node.search(method, path);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/hono-base.js
CHANGED
|
@@ -39,9 +39,7 @@ var Hono = class {
|
|
|
39
39
|
this.#addRoute(method, this.#path, args1);
|
|
40
40
|
}
|
|
41
41
|
args.forEach((handler) => {
|
|
42
|
-
|
|
43
|
-
this.#addRoute(method, this.#path, handler);
|
|
44
|
-
}
|
|
42
|
+
this.#addRoute(method, this.#path, handler);
|
|
45
43
|
});
|
|
46
44
|
return this;
|
|
47
45
|
};
|
|
@@ -83,15 +81,15 @@ var Hono = class {
|
|
|
83
81
|
return clone;
|
|
84
82
|
}
|
|
85
83
|
#notFoundHandler = notFoundHandler;
|
|
86
|
-
|
|
84
|
+
errorHandler = errorHandler;
|
|
87
85
|
route(path, app) {
|
|
88
86
|
const subApp = this.basePath(path);
|
|
89
87
|
app.routes.map((r) => {
|
|
90
88
|
let handler;
|
|
91
|
-
if (app
|
|
89
|
+
if (app.errorHandler === errorHandler) {
|
|
92
90
|
handler = r.handler;
|
|
93
91
|
} else {
|
|
94
|
-
handler = async (c, next) => (await compose([], app
|
|
92
|
+
handler = async (c, next) => (await compose([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
95
93
|
handler[COMPOSED_HANDLER] = r.handler;
|
|
96
94
|
}
|
|
97
95
|
subApp.#addRoute(r.method, r.path, handler);
|
|
@@ -104,7 +102,7 @@ var Hono = class {
|
|
|
104
102
|
return subApp;
|
|
105
103
|
}
|
|
106
104
|
onError = (handler) => {
|
|
107
|
-
this
|
|
105
|
+
this.errorHandler = handler;
|
|
108
106
|
return this;
|
|
109
107
|
};
|
|
110
108
|
notFound = (handler) => {
|
|
@@ -161,7 +159,7 @@ var Hono = class {
|
|
|
161
159
|
}
|
|
162
160
|
#handleError(err, c) {
|
|
163
161
|
if (err instanceof Error) {
|
|
164
|
-
return this
|
|
162
|
+
return this.errorHandler(err, c);
|
|
165
163
|
}
|
|
166
164
|
throw err;
|
|
167
165
|
}
|
|
@@ -191,7 +189,7 @@ var Hono = class {
|
|
|
191
189
|
(resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c))
|
|
192
190
|
).catch((err) => this.#handleError(err, c)) : res ?? this.#notFoundHandler(c);
|
|
193
191
|
}
|
|
194
|
-
const composed = compose(matchResult[0], this
|
|
192
|
+
const composed = compose(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
195
193
|
return (async () => {
|
|
196
194
|
try {
|
|
197
195
|
const context = await composed(c);
|
|
@@ -211,15 +209,17 @@ var Hono = class {
|
|
|
211
209
|
};
|
|
212
210
|
request = (input, requestInit, Env, executionCtx) => {
|
|
213
211
|
if (input instanceof Request) {
|
|
214
|
-
|
|
215
|
-
input = new Request(input, requestInit);
|
|
216
|
-
}
|
|
217
|
-
return this.fetch(input, Env, executionCtx);
|
|
212
|
+
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
218
213
|
}
|
|
219
214
|
input = input.toString();
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
215
|
+
return this.fetch(
|
|
216
|
+
new Request(
|
|
217
|
+
/^https?:\/\//.test(input) ? input : `http://localhost${mergePath("/", input)}`,
|
|
218
|
+
requestInit
|
|
219
|
+
),
|
|
220
|
+
Env,
|
|
221
|
+
executionCtx
|
|
222
|
+
);
|
|
223
223
|
};
|
|
224
224
|
fire = () => {
|
|
225
225
|
addEventListener("fetch", (event) => {
|