hono 2.5.8 → 2.5.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/cjs/router/reg-exp-router/router.js +18 -7
- package/dist/cjs/router/trie-router/node.js +7 -0
- package/dist/cjs/validator/rule.js +14 -0
- package/dist/router/reg-exp-router/router.js +18 -7
- package/dist/router/trie-router/node.d.ts +1 -0
- package/dist/router/trie-router/node.js +7 -0
- package/dist/validator/rule.js +14 -0
- package/package.json +1 -1
|
@@ -28,8 +28,14 @@ var import_trie = require("./trie");
|
|
|
28
28
|
const methodNames = [import_router.METHOD_NAME_ALL, ...import_router.METHODS].map((method) => method.toUpperCase());
|
|
29
29
|
const emptyParam = {};
|
|
30
30
|
const nullMatcher = [/^$/, []];
|
|
31
|
+
let wildcardRegExpCache = {};
|
|
31
32
|
function buildWildcardRegExp(path) {
|
|
32
|
-
return
|
|
33
|
+
return wildcardRegExpCache[path] ?? (wildcardRegExpCache[path] = new RegExp(
|
|
34
|
+
path === "*" ? "" : `^${path.replace(/\/\*/, "(?:|/.*)")}$`
|
|
35
|
+
));
|
|
36
|
+
}
|
|
37
|
+
function clearWildcardRegExpCache() {
|
|
38
|
+
wildcardRegExpCache = {};
|
|
33
39
|
}
|
|
34
40
|
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
35
41
|
const trie = new import_trie.Trie();
|
|
@@ -100,20 +106,24 @@ class RegExpRouter {
|
|
|
100
106
|
}
|
|
101
107
|
if (/\*$/.test(path)) {
|
|
102
108
|
const re = buildWildcardRegExp(path);
|
|
103
|
-
|
|
109
|
+
if (method === import_router.METHOD_NAME_ALL) {
|
|
110
|
+
Object.keys(middleware).forEach((m) => {
|
|
111
|
+
var _a2;
|
|
112
|
+
(_a2 = middleware[m])[path] || (_a2[path] = findMiddleware(middleware[m], path) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path) || []);
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
(_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[method], path) || findMiddleware(middleware[import_router.METHOD_NAME_ALL], path) || []);
|
|
116
|
+
}
|
|
104
117
|
Object.keys(middleware).forEach((m) => {
|
|
105
118
|
if (method === import_router.METHOD_NAME_ALL || method === m) {
|
|
106
119
|
Object.keys(middleware[m]).forEach((p) => {
|
|
107
|
-
;
|
|
108
|
-
(path === "*" || re.test(p)) && middleware[m][p].push(handler);
|
|
120
|
+
re.test(p) && middleware[m][p].push(handler);
|
|
109
121
|
});
|
|
110
122
|
}
|
|
111
123
|
});
|
|
112
124
|
Object.keys(routes).forEach((m) => {
|
|
113
125
|
if (method === import_router.METHOD_NAME_ALL || method === m) {
|
|
114
|
-
Object.keys(routes[m]).forEach(
|
|
115
|
-
(p) => (path === "*" || re.test(p)) && routes[m][p].push(handler)
|
|
116
|
-
);
|
|
126
|
+
Object.keys(routes[m]).forEach((p) => re.test(p) && routes[m][p].push(handler));
|
|
117
127
|
}
|
|
118
128
|
});
|
|
119
129
|
return;
|
|
@@ -133,6 +143,7 @@ class RegExpRouter {
|
|
|
133
143
|
}
|
|
134
144
|
}
|
|
135
145
|
match(method, path) {
|
|
146
|
+
clearWildcardRegExpCache();
|
|
136
147
|
const matchers = this.buildAllMatchers();
|
|
137
148
|
this.match = (method2, path2) => {
|
|
138
149
|
const matcher = matchers[method2];
|
|
@@ -40,6 +40,7 @@ function findParam(node, name) {
|
|
|
40
40
|
class Node {
|
|
41
41
|
constructor(method, handler, children) {
|
|
42
42
|
this.order = 0;
|
|
43
|
+
this.shouldCapture = false;
|
|
43
44
|
this.children = children || {};
|
|
44
45
|
this.methods = [];
|
|
45
46
|
this.name = "";
|
|
@@ -71,6 +72,7 @@ class Node {
|
|
|
71
72
|
const pattern = (0, import_url.getPattern)(p);
|
|
72
73
|
if (pattern) {
|
|
73
74
|
if (typeof pattern === "object") {
|
|
75
|
+
this.shouldCapture = true;
|
|
74
76
|
for (let j = 0, len2 = parentPatterns.length; j < len2; j++) {
|
|
75
77
|
if (typeof parentPatterns[j] === "object" && parentPatterns[j][1] === pattern[1]) {
|
|
76
78
|
throw new Error(errorMessage(pattern[1]));
|
|
@@ -85,6 +87,7 @@ class Node {
|
|
|
85
87
|
}
|
|
86
88
|
parentPatterns.push(...curNode.patterns);
|
|
87
89
|
curNode = curNode.children[p];
|
|
90
|
+
curNode.shouldCapture = this.shouldCapture;
|
|
88
91
|
}
|
|
89
92
|
if (!curNode.methods.length) {
|
|
90
93
|
curNode.methods = [];
|
|
@@ -157,6 +160,10 @@ class Node {
|
|
|
157
160
|
}
|
|
158
161
|
if (typeof name === "string" && !matched) {
|
|
159
162
|
params[name] = part;
|
|
163
|
+
} else {
|
|
164
|
+
if (node.children[part] && node.children[part].shouldCapture) {
|
|
165
|
+
params[name] = part;
|
|
166
|
+
}
|
|
160
167
|
}
|
|
161
168
|
}
|
|
162
169
|
}
|
|
@@ -23,9 +23,13 @@ __export(rule_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(rule_exports);
|
|
24
24
|
const rule = {
|
|
25
25
|
isEmpty(value, options = { ignore_whitespace: false }) {
|
|
26
|
+
if (value === void 0)
|
|
27
|
+
return false;
|
|
26
28
|
return (options.ignore_whitespace ? value.trim().length : value.length) === 0;
|
|
27
29
|
},
|
|
28
30
|
isLength: (value, options, arg2) => {
|
|
31
|
+
if (value === void 0)
|
|
32
|
+
return false;
|
|
29
33
|
let min;
|
|
30
34
|
let max;
|
|
31
35
|
if (typeof options === "object") {
|
|
@@ -41,15 +45,21 @@ const rule = {
|
|
|
41
45
|
return len >= min && (typeof max === "undefined" || len <= max);
|
|
42
46
|
},
|
|
43
47
|
isAlpha: (value) => {
|
|
48
|
+
if (value === void 0)
|
|
49
|
+
return false;
|
|
44
50
|
return /^[A-Z]+$/i.test(value);
|
|
45
51
|
},
|
|
46
52
|
isNumeric: (value) => {
|
|
53
|
+
if (value === void 0)
|
|
54
|
+
return false;
|
|
47
55
|
return /^[0-9]+$/.test(value);
|
|
48
56
|
},
|
|
49
57
|
contains: (value, elem, options = {
|
|
50
58
|
ignoreCase: false,
|
|
51
59
|
minOccurrences: 1
|
|
52
60
|
}) => {
|
|
61
|
+
if (value === void 0 || elem === void 0)
|
|
62
|
+
return false;
|
|
53
63
|
options.ignoreCase || (options.ignoreCase = false);
|
|
54
64
|
options.minOccurrences || (options.minOccurrences = 1);
|
|
55
65
|
if (options.ignoreCase) {
|
|
@@ -58,6 +68,8 @@ const rule = {
|
|
|
58
68
|
return value.split(elem).length > options.minOccurrences;
|
|
59
69
|
},
|
|
60
70
|
isIn: (value, options) => {
|
|
71
|
+
if (value === void 0)
|
|
72
|
+
return false;
|
|
61
73
|
if (typeof options === "object") {
|
|
62
74
|
for (const elem of options) {
|
|
63
75
|
if (elem === value)
|
|
@@ -67,6 +79,8 @@ const rule = {
|
|
|
67
79
|
return false;
|
|
68
80
|
},
|
|
69
81
|
match: (value, regExp) => {
|
|
82
|
+
if (value === void 0 || regExp === void 0)
|
|
83
|
+
return false;
|
|
70
84
|
return regExp.test(value);
|
|
71
85
|
},
|
|
72
86
|
isGte: (value, min) => min <= value,
|
|
@@ -6,8 +6,14 @@ import { Trie } from "./trie.js";
|
|
|
6
6
|
var methodNames = [METHOD_NAME_ALL, ...METHODS].map((method) => method.toUpperCase());
|
|
7
7
|
var emptyParam = {};
|
|
8
8
|
var nullMatcher = [/^$/, []];
|
|
9
|
+
var wildcardRegExpCache = {};
|
|
9
10
|
function buildWildcardRegExp(path) {
|
|
10
|
-
return
|
|
11
|
+
return wildcardRegExpCache[path] ?? (wildcardRegExpCache[path] = new RegExp(
|
|
12
|
+
path === "*" ? "" : `^${path.replace(/\/\*/, "(?:|/.*)")}$`
|
|
13
|
+
));
|
|
14
|
+
}
|
|
15
|
+
function clearWildcardRegExpCache() {
|
|
16
|
+
wildcardRegExpCache = {};
|
|
11
17
|
}
|
|
12
18
|
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
13
19
|
const trie = new Trie();
|
|
@@ -78,20 +84,24 @@ var RegExpRouter = class {
|
|
|
78
84
|
}
|
|
79
85
|
if (/\*$/.test(path)) {
|
|
80
86
|
const re = buildWildcardRegExp(path);
|
|
81
|
-
|
|
87
|
+
if (method === METHOD_NAME_ALL) {
|
|
88
|
+
Object.keys(middleware).forEach((m) => {
|
|
89
|
+
var _a2;
|
|
90
|
+
(_a2 = middleware[m])[path] || (_a2[path] = findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || []);
|
|
91
|
+
});
|
|
92
|
+
} else {
|
|
93
|
+
(_a = middleware[method])[path] || (_a[path] = findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || []);
|
|
94
|
+
}
|
|
82
95
|
Object.keys(middleware).forEach((m) => {
|
|
83
96
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
84
97
|
Object.keys(middleware[m]).forEach((p) => {
|
|
85
|
-
;
|
|
86
|
-
(path === "*" || re.test(p)) && middleware[m][p].push(handler);
|
|
98
|
+
re.test(p) && middleware[m][p].push(handler);
|
|
87
99
|
});
|
|
88
100
|
}
|
|
89
101
|
});
|
|
90
102
|
Object.keys(routes).forEach((m) => {
|
|
91
103
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
92
|
-
Object.keys(routes[m]).forEach(
|
|
93
|
-
(p) => (path === "*" || re.test(p)) && routes[m][p].push(handler)
|
|
94
|
-
);
|
|
104
|
+
Object.keys(routes[m]).forEach((p) => re.test(p) && routes[m][p].push(handler));
|
|
95
105
|
}
|
|
96
106
|
});
|
|
97
107
|
return;
|
|
@@ -111,6 +121,7 @@ var RegExpRouter = class {
|
|
|
111
121
|
}
|
|
112
122
|
}
|
|
113
123
|
match(method, path) {
|
|
124
|
+
clearWildcardRegExpCache();
|
|
114
125
|
const matchers = this.buildAllMatchers();
|
|
115
126
|
this.match = (method2, path2) => {
|
|
116
127
|
const matcher = matchers[method2];
|
|
@@ -12,6 +12,7 @@ export declare class Node<T> {
|
|
|
12
12
|
order: number;
|
|
13
13
|
name: string;
|
|
14
14
|
handlerSetCache: Record<string, HandlerSet<T>[]>;
|
|
15
|
+
shouldCapture: boolean;
|
|
15
16
|
constructor(method?: string, handler?: T, children?: Record<string, Node<T>>);
|
|
16
17
|
insert(method: string, path: string, handler: T): Node<T>;
|
|
17
18
|
private getHandlerSets;
|
|
@@ -18,6 +18,7 @@ function findParam(node, name) {
|
|
|
18
18
|
var Node = class {
|
|
19
19
|
constructor(method, handler, children) {
|
|
20
20
|
this.order = 0;
|
|
21
|
+
this.shouldCapture = false;
|
|
21
22
|
this.children = children || {};
|
|
22
23
|
this.methods = [];
|
|
23
24
|
this.name = "";
|
|
@@ -49,6 +50,7 @@ var Node = class {
|
|
|
49
50
|
const pattern = getPattern(p);
|
|
50
51
|
if (pattern) {
|
|
51
52
|
if (typeof pattern === "object") {
|
|
53
|
+
this.shouldCapture = true;
|
|
52
54
|
for (let j = 0, len2 = parentPatterns.length; j < len2; j++) {
|
|
53
55
|
if (typeof parentPatterns[j] === "object" && parentPatterns[j][1] === pattern[1]) {
|
|
54
56
|
throw new Error(errorMessage(pattern[1]));
|
|
@@ -63,6 +65,7 @@ var Node = class {
|
|
|
63
65
|
}
|
|
64
66
|
parentPatterns.push(...curNode.patterns);
|
|
65
67
|
curNode = curNode.children[p];
|
|
68
|
+
curNode.shouldCapture = this.shouldCapture;
|
|
66
69
|
}
|
|
67
70
|
if (!curNode.methods.length) {
|
|
68
71
|
curNode.methods = [];
|
|
@@ -135,6 +138,10 @@ var Node = class {
|
|
|
135
138
|
}
|
|
136
139
|
if (typeof name === "string" && !matched) {
|
|
137
140
|
params[name] = part;
|
|
141
|
+
} else {
|
|
142
|
+
if (node.children[part] && node.children[part].shouldCapture) {
|
|
143
|
+
params[name] = part;
|
|
144
|
+
}
|
|
138
145
|
}
|
|
139
146
|
}
|
|
140
147
|
}
|
package/dist/validator/rule.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
// src/validator/rule.ts
|
|
2
2
|
var rule = {
|
|
3
3
|
isEmpty(value, options = { ignore_whitespace: false }) {
|
|
4
|
+
if (value === void 0)
|
|
5
|
+
return false;
|
|
4
6
|
return (options.ignore_whitespace ? value.trim().length : value.length) === 0;
|
|
5
7
|
},
|
|
6
8
|
isLength: (value, options, arg2) => {
|
|
9
|
+
if (value === void 0)
|
|
10
|
+
return false;
|
|
7
11
|
let min;
|
|
8
12
|
let max;
|
|
9
13
|
if (typeof options === "object") {
|
|
@@ -19,15 +23,21 @@ var rule = {
|
|
|
19
23
|
return len >= min && (typeof max === "undefined" || len <= max);
|
|
20
24
|
},
|
|
21
25
|
isAlpha: (value) => {
|
|
26
|
+
if (value === void 0)
|
|
27
|
+
return false;
|
|
22
28
|
return /^[A-Z]+$/i.test(value);
|
|
23
29
|
},
|
|
24
30
|
isNumeric: (value) => {
|
|
31
|
+
if (value === void 0)
|
|
32
|
+
return false;
|
|
25
33
|
return /^[0-9]+$/.test(value);
|
|
26
34
|
},
|
|
27
35
|
contains: (value, elem, options = {
|
|
28
36
|
ignoreCase: false,
|
|
29
37
|
minOccurrences: 1
|
|
30
38
|
}) => {
|
|
39
|
+
if (value === void 0 || elem === void 0)
|
|
40
|
+
return false;
|
|
31
41
|
options.ignoreCase || (options.ignoreCase = false);
|
|
32
42
|
options.minOccurrences || (options.minOccurrences = 1);
|
|
33
43
|
if (options.ignoreCase) {
|
|
@@ -36,6 +46,8 @@ var rule = {
|
|
|
36
46
|
return value.split(elem).length > options.minOccurrences;
|
|
37
47
|
},
|
|
38
48
|
isIn: (value, options) => {
|
|
49
|
+
if (value === void 0)
|
|
50
|
+
return false;
|
|
39
51
|
if (typeof options === "object") {
|
|
40
52
|
for (const elem of options) {
|
|
41
53
|
if (elem === value)
|
|
@@ -45,6 +57,8 @@ var rule = {
|
|
|
45
57
|
return false;
|
|
46
58
|
},
|
|
47
59
|
match: (value, regExp) => {
|
|
60
|
+
if (value === void 0 || regExp === void 0)
|
|
61
|
+
return false;
|
|
48
62
|
return regExp.test(value);
|
|
49
63
|
},
|
|
50
64
|
isGte: (value, min) => min <= value,
|