hono 4.6.8 → 4.6.10
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/cloudflare-workers/websocket.js +34 -39
- package/dist/adapter/deno/serve-static.js +5 -1
- package/dist/adapter/lambda-edge/handler.js +1 -1
- package/dist/cjs/adapter/cloudflare-workers/websocket.js +34 -39
- package/dist/cjs/adapter/deno/serve-static.js +5 -1
- package/dist/cjs/adapter/lambda-edge/handler.js +1 -1
- package/dist/cjs/client/client.js +3 -3
- package/dist/cjs/compose.js +4 -3
- package/dist/cjs/context.js +12 -9
- package/dist/cjs/helper/html/index.js +1 -1
- package/dist/cjs/helper/ssg/ssg.js +2 -2
- package/dist/cjs/helper/ssg/utils.js +2 -2
- package/dist/cjs/hono-base.js +38 -41
- package/dist/cjs/jsx/base.js +8 -3
- package/dist/cjs/jsx/constants.js +3 -0
- package/dist/cjs/jsx/dom/index.js +8 -2
- package/dist/cjs/jsx/dom/render.js +15 -12
- package/dist/cjs/middleware/combine/index.js +15 -9
- package/dist/cjs/middleware/etag/digest.js +59 -0
- package/dist/cjs/middleware/etag/index.js +5 -2
- package/dist/cjs/middleware/ip-restriction/index.js +3 -3
- package/dist/cjs/middleware/jsx-renderer/index.js +2 -1
- package/dist/cjs/middleware/logger/index.js +13 -11
- package/dist/cjs/middleware/timing/timing.js +5 -7
- package/dist/cjs/request.js +15 -14
- package/dist/cjs/router/linear-router/router.js +4 -4
- package/dist/cjs/router/pattern-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 +13 -12
- package/dist/cjs/router/reg-exp-router/trie.js +6 -6
- package/dist/cjs/router/smart-router/router.js +12 -11
- package/dist/cjs/router/trie-router/node.js +53 -47
- package/dist/cjs/router/trie-router/router.js +5 -5
- package/dist/cjs/utils/color.js +1 -1
- package/dist/cjs/utils/cookie.js +13 -6
- package/dist/cjs/utils/crypto.js +0 -9
- package/dist/cjs/utils/jwt/jwt.js +1 -1
- package/dist/cjs/utils/mime.js +2 -1
- package/dist/cjs/utils/url.js +9 -6
- package/dist/client/client.js +3 -3
- package/dist/compose.js +4 -3
- package/dist/context.js +12 -9
- package/dist/helper/html/index.js +1 -1
- package/dist/helper/ssg/ssg.js +2 -2
- package/dist/helper/ssg/utils.js +2 -2
- package/dist/hono-base.js +38 -41
- package/dist/jsx/base.js +7 -3
- package/dist/jsx/constants.js +2 -0
- package/dist/jsx/dom/index.js +7 -1
- package/dist/jsx/dom/render.js +22 -13
- package/dist/middleware/combine/index.js +15 -9
- package/dist/middleware/etag/digest.js +36 -0
- package/dist/middleware/etag/index.js +5 -2
- package/dist/middleware/ip-restriction/index.js +3 -3
- package/dist/middleware/jsx-renderer/index.js +2 -1
- package/dist/middleware/logger/index.js +13 -11
- package/dist/middleware/timing/timing.js +5 -7
- package/dist/request.js +16 -15
- package/dist/router/linear-router/router.js +4 -4
- package/dist/router/pattern-router/router.js +4 -4
- package/dist/router/reg-exp-router/node.js +18 -18
- package/dist/router/reg-exp-router/router.js +13 -12
- package/dist/router/reg-exp-router/trie.js +6 -6
- package/dist/router/smart-router/router.js +12 -11
- package/dist/router/trie-router/node.js +53 -47
- package/dist/router/trie-router/router.js +5 -5
- package/dist/types/adapter/cloudflare-workers/websocket.d.ts +2 -2
- package/dist/types/adapter/deno/websocket.d.ts +1 -20
- package/dist/types/adapter/service-worker/index.d.ts +1 -1
- package/dist/types/compose.d.ts +5 -2
- package/dist/types/context.d.ts +1 -1
- package/dist/types/helper/websocket/index.d.ts +1 -1
- package/dist/types/hono-base.d.ts +1 -1
- package/dist/types/jsx/base.d.ts +5 -0
- package/dist/types/jsx/constants.d.ts +1 -0
- package/dist/types/jsx/dom/index.d.ts +6 -5
- package/dist/types/jsx/dom/render.d.ts +1 -0
- package/dist/types/middleware/etag/digest.d.ts +1 -0
- 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/utils/body.d.ts +2 -2
- package/dist/types/utils/crypto.d.ts +2 -1
- package/dist/types/utils/mime.d.ts +58 -1
- package/dist/types/utils/url.d.ts +3 -0
- package/dist/utils/color.js +1 -1
- package/dist/utils/cookie.js +13 -6
- package/dist/utils/crypto.js +0 -9
- package/dist/utils/jwt/jwt.js +1 -1
- package/dist/utils/mime.js +2 -1
- package/dist/utils/url.js +7 -5
- package/package.json +20 -12
|
@@ -258,13 +258,19 @@ const applyNodeObject = (node, container, isNew) => {
|
|
|
258
258
|
remove.forEach(removeNode);
|
|
259
259
|
const childNodes = isNew ? void 0 : container.childNodes;
|
|
260
260
|
let offset;
|
|
261
|
+
let insertBeforeNode = null;
|
|
261
262
|
if (isNew) {
|
|
262
263
|
offset = -1;
|
|
264
|
+
} else if (!childNodes.length) {
|
|
265
|
+
offset = 0;
|
|
263
266
|
} else {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
267
|
+
const offsetByNextNode = findChildNodeIndex(childNodes, findInsertBefore(node.nN));
|
|
268
|
+
if (offsetByNextNode !== void 0) {
|
|
269
|
+
insertBeforeNode = childNodes[offsetByNextNode];
|
|
270
|
+
offset = offsetByNextNode;
|
|
271
|
+
} else {
|
|
272
|
+
offset = findChildNodeIndex(childNodes, next.find((n) => n.tag !== HONO_PORTAL_ELEMENT && n.e)?.e) ?? -1;
|
|
273
|
+
}
|
|
268
274
|
if (offset === -1) {
|
|
269
275
|
isNew = true;
|
|
270
276
|
}
|
|
@@ -299,7 +305,7 @@ const applyNodeObject = (node, container, isNew) => {
|
|
|
299
305
|
if (childNodes[offset + 1] === el) {
|
|
300
306
|
container.appendChild(childNodes[offset]);
|
|
301
307
|
} else {
|
|
302
|
-
container.insertBefore(el, childNodes[offset] || null);
|
|
308
|
+
container.insertBefore(el, insertBeforeNode || childNodes[offset] || null);
|
|
303
309
|
}
|
|
304
310
|
}
|
|
305
311
|
}
|
|
@@ -381,12 +387,8 @@ const build = (context, node, children) => {
|
|
|
381
387
|
if (typeof child.tag === "function") {
|
|
382
388
|
oldChild[import_constants.DOM_STASH][2] = child[import_constants.DOM_STASH][2] || [];
|
|
383
389
|
oldChild[import_constants.DOM_STASH][3] = child[import_constants.DOM_STASH][3];
|
|
384
|
-
if (!oldChild.f) {
|
|
385
|
-
|
|
386
|
-
const currentProps = oldChild.props;
|
|
387
|
-
if (prevPropsKeys.length === Object.keys(currentProps).length && prevPropsKeys.every((k) => k in currentProps && currentProps[k] === pP[k])) {
|
|
388
|
-
oldChild.s = true;
|
|
389
|
-
}
|
|
390
|
+
if (!oldChild.f && ((oldChild.o || oldChild) === child.o || oldChild.tag[import_constants.DOM_MEMO]?.(pP, oldChild.props))) {
|
|
391
|
+
oldChild.s = true;
|
|
390
392
|
}
|
|
391
393
|
}
|
|
392
394
|
child = oldChild;
|
|
@@ -473,7 +475,8 @@ const buildNode = (node) => {
|
|
|
473
475
|
key: node.key,
|
|
474
476
|
f: node.f,
|
|
475
477
|
type: node.tag,
|
|
476
|
-
ref: node.props.ref
|
|
478
|
+
ref: node.props.ref,
|
|
479
|
+
o: node.o || node
|
|
477
480
|
};
|
|
478
481
|
}
|
|
479
482
|
if (typeof node.tag === "function") {
|
|
@@ -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) => {
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var digest_exports = {};
|
|
20
|
+
__export(digest_exports, {
|
|
21
|
+
generateDigest: () => generateDigest
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(digest_exports);
|
|
24
|
+
const mergeBuffers = (buffer1, buffer2) => {
|
|
25
|
+
if (!buffer1) {
|
|
26
|
+
return buffer2;
|
|
27
|
+
}
|
|
28
|
+
const merged = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
|
|
29
|
+
merged.set(new Uint8Array(buffer1), 0);
|
|
30
|
+
merged.set(buffer2, buffer1.byteLength);
|
|
31
|
+
return merged;
|
|
32
|
+
};
|
|
33
|
+
const generateDigest = async (stream) => {
|
|
34
|
+
if (!stream || !crypto || !crypto.subtle) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
let result = void 0;
|
|
38
|
+
const reader = stream.getReader();
|
|
39
|
+
for (; ; ) {
|
|
40
|
+
const { value, done } = await reader.read();
|
|
41
|
+
if (done) {
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
result = await crypto.subtle.digest(
|
|
45
|
+
{
|
|
46
|
+
name: "SHA-1"
|
|
47
|
+
},
|
|
48
|
+
mergeBuffers(result, value)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
if (!result) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return Array.prototype.map.call(new Uint8Array(result), (x) => x.toString(16).padStart(2, "0")).join("");
|
|
55
|
+
};
|
|
56
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
57
|
+
0 && (module.exports = {
|
|
58
|
+
generateDigest
|
|
59
|
+
});
|
|
@@ -22,7 +22,7 @@ __export(etag_exports, {
|
|
|
22
22
|
etag: () => etag
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(etag_exports);
|
|
25
|
-
var
|
|
25
|
+
var import_digest = require("./digest");
|
|
26
26
|
const RETAINED_304_HEADERS = [
|
|
27
27
|
"cache-control",
|
|
28
28
|
"content-location",
|
|
@@ -43,7 +43,10 @@ const etag = (options) => {
|
|
|
43
43
|
const res = c.res;
|
|
44
44
|
let etag3 = res.headers.get("ETag");
|
|
45
45
|
if (!etag3) {
|
|
46
|
-
const hash = await (0,
|
|
46
|
+
const hash = await (0, import_digest.generateDigest)(res.clone().body);
|
|
47
|
+
if (hash === null) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
47
50
|
etag3 = weak ? `W/"${hash}"` : `"${hash}"`;
|
|
48
51
|
}
|
|
49
52
|
if (etagMatches(etag3, ifNoneMatch)) {
|
|
@@ -35,14 +35,14 @@ const buildMatcher = (rules) => {
|
|
|
35
35
|
functionRules.push(rule);
|
|
36
36
|
} else {
|
|
37
37
|
if (IS_CIDR_NOTATION_REGEX.test(rule)) {
|
|
38
|
-
const
|
|
39
|
-
const addrStr =
|
|
38
|
+
const separatedRule = rule.split("/");
|
|
39
|
+
const addrStr = separatedRule[0];
|
|
40
40
|
const type2 = (0, import_ipaddr.distinctRemoteAddr)(addrStr);
|
|
41
41
|
if (type2 === void 0) {
|
|
42
42
|
throw new TypeError(`Invalid rule: ${rule}`);
|
|
43
43
|
}
|
|
44
44
|
const isIPv4 = type2 === "IPv4";
|
|
45
|
-
const prefix = parseInt(
|
|
45
|
+
const prefix = parseInt(separatedRule[1]);
|
|
46
46
|
if (isIPv4 ? prefix === 32 : prefix === 128) {
|
|
47
47
|
rule = addrStr;
|
|
48
48
|
} else {
|
|
@@ -32,7 +32,8 @@ const createRenderer = (c, Layout, component, options) => (children, props) => {
|
|
|
32
32
|
const currentLayout = component ? (0, import_jsx.jsx)(
|
|
33
33
|
(props2) => component(props2, c),
|
|
34
34
|
{
|
|
35
|
-
|
|
35
|
+
Layout,
|
|
36
|
+
...props
|
|
36
37
|
},
|
|
37
38
|
children
|
|
38
39
|
) : children;
|
|
@@ -40,17 +40,19 @@ const time = (start) => {
|
|
|
40
40
|
};
|
|
41
41
|
const colorStatus = (status) => {
|
|
42
42
|
const colorEnabled = (0, import_color.getColorEnabled)();
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
if (colorEnabled) {
|
|
44
|
+
switch (status / 100 | 0) {
|
|
45
|
+
case 5:
|
|
46
|
+
return `\x1B[31m${status}\x1B[0m`;
|
|
47
|
+
case 4:
|
|
48
|
+
return `\x1B[33m${status}\x1B[0m`;
|
|
49
|
+
case 3:
|
|
50
|
+
return `\x1B[36m${status}\x1B[0m`;
|
|
51
|
+
case 2:
|
|
52
|
+
return `\x1B[32m${status}\x1B[0m`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return `${status}`;
|
|
54
56
|
};
|
|
55
57
|
function log(fn, prefix, method, path, status = 0, elapsed) {
|
|
56
58
|
const out = prefix === "<--" /* Incoming */ ? `${prefix} ${method} ${path}` : `${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`;
|
|
@@ -34,13 +34,11 @@ const getTime = () => {
|
|
|
34
34
|
};
|
|
35
35
|
const timing = (config) => {
|
|
36
36
|
const options = {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
crossOrigin: false
|
|
43
|
-
},
|
|
37
|
+
total: true,
|
|
38
|
+
enabled: true,
|
|
39
|
+
totalDescription: "Total Response Time",
|
|
40
|
+
autoEnd: true,
|
|
41
|
+
crossOrigin: false,
|
|
44
42
|
...config
|
|
45
43
|
};
|
|
46
44
|
return async function timing2(c, next) {
|
package/dist/cjs/request.js
CHANGED
|
@@ -23,6 +23,7 @@ __export(request_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(request_exports);
|
|
24
24
|
var import_body = require("./utils/body");
|
|
25
25
|
var import_url = require("./utils/url");
|
|
26
|
+
const tryDecodeURIComponent = (str) => (0, import_url.tryDecode)(str, import_url.decodeURIComponent_);
|
|
26
27
|
class HonoRequest {
|
|
27
28
|
raw;
|
|
28
29
|
#validatedData;
|
|
@@ -37,25 +38,25 @@ class HonoRequest {
|
|
|
37
38
|
this.#validatedData = {};
|
|
38
39
|
}
|
|
39
40
|
param(key) {
|
|
40
|
-
return key ? this
|
|
41
|
+
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
41
42
|
}
|
|
42
|
-
getDecodedParam(key) {
|
|
43
|
+
#getDecodedParam(key) {
|
|
43
44
|
const paramKey = this.#matchResult[0][this.routeIndex][1][key];
|
|
44
|
-
const param = this
|
|
45
|
-
return param ? /\%/.test(param) ? (
|
|
45
|
+
const param = this.#getParamValue(paramKey);
|
|
46
|
+
return param ? /\%/.test(param) ? tryDecodeURIComponent(param) : param : void 0;
|
|
46
47
|
}
|
|
47
|
-
getAllDecodedParams() {
|
|
48
|
+
#getAllDecodedParams() {
|
|
48
49
|
const decoded = {};
|
|
49
50
|
const keys = Object.keys(this.#matchResult[0][this.routeIndex][1]);
|
|
50
51
|
for (const key of keys) {
|
|
51
|
-
const value = this
|
|
52
|
+
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
52
53
|
if (value && typeof value === "string") {
|
|
53
|
-
decoded[key] = /\%/.test(value) ? (
|
|
54
|
+
decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value;
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
return decoded;
|
|
57
58
|
}
|
|
58
|
-
getParamValue(paramKey) {
|
|
59
|
+
#getParamValue(paramKey) {
|
|
59
60
|
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
60
61
|
}
|
|
61
62
|
query(key) {
|
|
@@ -77,7 +78,7 @@ class HonoRequest {
|
|
|
77
78
|
async parseBody(options) {
|
|
78
79
|
return this.bodyCache.parsedBody ??= await (0, import_body.parseBody)(this, options);
|
|
79
80
|
}
|
|
80
|
-
cachedBody = (key) => {
|
|
81
|
+
#cachedBody = (key) => {
|
|
81
82
|
const { bodyCache, raw } = this;
|
|
82
83
|
const cachedBody = bodyCache[key];
|
|
83
84
|
if (cachedBody) {
|
|
@@ -95,19 +96,19 @@ class HonoRequest {
|
|
|
95
96
|
return bodyCache[key] = raw[key]();
|
|
96
97
|
};
|
|
97
98
|
json() {
|
|
98
|
-
return this
|
|
99
|
+
return this.#cachedBody("json");
|
|
99
100
|
}
|
|
100
101
|
text() {
|
|
101
|
-
return this
|
|
102
|
+
return this.#cachedBody("text");
|
|
102
103
|
}
|
|
103
104
|
arrayBuffer() {
|
|
104
|
-
return this
|
|
105
|
+
return this.#cachedBody("arrayBuffer");
|
|
105
106
|
}
|
|
106
107
|
blob() {
|
|
107
|
-
return this
|
|
108
|
+
return this.#cachedBody("blob");
|
|
108
109
|
}
|
|
109
110
|
formData() {
|
|
110
|
-
return this
|
|
111
|
+
return this.#cachedBody("formData");
|
|
111
112
|
}
|
|
112
113
|
addValidatedData(target, data) {
|
|
113
114
|
this.#validatedData[target] = data;
|
|
@@ -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]);
|
|
@@ -24,7 +24,7 @@ module.exports = __toCommonJS(router_exports);
|
|
|
24
24
|
var import_router = require("../../router");
|
|
25
25
|
class PatternRouter {
|
|
26
26
|
name = "PatternRouter";
|
|
27
|
-
routes = [];
|
|
27
|
+
#routes = [];
|
|
28
28
|
add(method, path, handler) {
|
|
29
29
|
const endsWithWildcard = path[path.length - 1] === "*";
|
|
30
30
|
if (endsWithWildcard) {
|
|
@@ -46,12 +46,12 @@ class PatternRouter {
|
|
|
46
46
|
} catch {
|
|
47
47
|
throw new import_router.UnsupportedPathError();
|
|
48
48
|
}
|
|
49
|
-
this
|
|
49
|
+
this.#routes.push([re, method, handler]);
|
|
50
50
|
}
|
|
51
51
|
match(method, path) {
|
|
52
52
|
const handlers = [];
|
|
53
|
-
for (let i = 0, len = this
|
|
54
|
-
const [pattern, routeMethod, handler] = this
|
|
53
|
+
for (let i = 0, len = this.#routes.length; i < len; i++) {
|
|
54
|
+
const [pattern, routeMethod, handler] = this.#routes[i];
|
|
55
55
|
if (routeMethod === method || routeMethod === import_router.METHOD_NAME_ALL) {
|
|
56
56
|
const match = pattern.exec(path);
|
|
57
57
|
if (match) {
|
|
@@ -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
|
}
|
|
@@ -173,7 +174,7 @@ class RegExpRouter {
|
|
|
173
174
|
}
|
|
174
175
|
match(method, path) {
|
|
175
176
|
clearWildcardRegExpCache();
|
|
176
|
-
const matchers = this
|
|
177
|
+
const matchers = this.#buildAllMatchers();
|
|
177
178
|
this.match = (method2, path2) => {
|
|
178
179
|
const matcher = matchers[method2] || matchers[import_router.METHOD_NAME_ALL];
|
|
179
180
|
const staticMatch = matcher[2][path2];
|
|
@@ -189,18 +190,18 @@ class RegExpRouter {
|
|
|
189
190
|
};
|
|
190
191
|
return this.match(method, path);
|
|
191
192
|
}
|
|
192
|
-
buildAllMatchers() {
|
|
193
|
+
#buildAllMatchers() {
|
|
193
194
|
const matchers = /* @__PURE__ */ Object.create(null);
|
|
194
|
-
|
|
195
|
-
matchers[method] ||= this
|
|
195
|
+
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
196
|
+
matchers[method] ||= this.#buildMatcher(method);
|
|
196
197
|
});
|
|
197
|
-
this
|
|
198
|
+
this.#middleware = this.#routes = void 0;
|
|
198
199
|
return matchers;
|
|
199
200
|
}
|
|
200
|
-
buildMatcher(method) {
|
|
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
|
}
|
|
@@ -63,11 +63,11 @@ class Trie {
|
|
|
63
63
|
const indexReplacementMap = [];
|
|
64
64
|
const paramReplacementMap = [];
|
|
65
65
|
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
|
66
|
-
if (
|
|
66
|
+
if (handlerIndex !== void 0) {
|
|
67
67
|
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
68
68
|
return "$()";
|
|
69
69
|
}
|
|
70
|
-
if (
|
|
70
|
+
if (paramIndex !== void 0) {
|
|
71
71
|
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
72
72
|
return "";
|
|
73
73
|
}
|
|
@@ -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:
|