hono 4.9.10 → 4.9.12
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/index.js +9 -2
- package/dist/cjs/router/reg-exp-router/matcher.js +49 -0
- package/dist/cjs/router/reg-exp-router/prepared-router.js +167 -0
- package/dist/cjs/router/reg-exp-router/router.js +5 -21
- package/dist/router/reg-exp-router/index.js +5 -1
- package/dist/router/reg-exp-router/matcher.js +25 -0
- package/dist/router/reg-exp-router/prepared-router.js +142 -0
- package/dist/router/reg-exp-router/router.js +4 -20
- package/dist/types/router/reg-exp-router/index.d.ts +1 -0
- package/dist/types/router/reg-exp-router/matcher.d.ts +14 -0
- package/dist/types/router/reg-exp-router/prepared-router.d.ts +21 -0
- package/dist/types/router/reg-exp-router/router.d.ts +5 -2
- package/dist/types/utils/types.d.ts +2 -2
- package/package.json +1 -1
|
@@ -18,11 +18,18 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var reg_exp_router_exports = {};
|
|
20
20
|
__export(reg_exp_router_exports, {
|
|
21
|
-
|
|
21
|
+
PreparedRegExpRouter: () => import_prepared_router.PreparedRegExpRouter,
|
|
22
|
+
RegExpRouter: () => import_router.RegExpRouter,
|
|
23
|
+
buildInitParams: () => import_prepared_router.buildInitParams,
|
|
24
|
+
serializeInitParams: () => import_prepared_router.serializeInitParams
|
|
22
25
|
});
|
|
23
26
|
module.exports = __toCommonJS(reg_exp_router_exports);
|
|
24
27
|
var import_router = require("./router");
|
|
28
|
+
var import_prepared_router = require("./prepared-router");
|
|
25
29
|
// Annotate the CommonJS export names for ESM import in node:
|
|
26
30
|
0 && (module.exports = {
|
|
27
|
-
|
|
31
|
+
PreparedRegExpRouter,
|
|
32
|
+
RegExpRouter,
|
|
33
|
+
buildInitParams,
|
|
34
|
+
serializeInitParams
|
|
28
35
|
});
|
|
@@ -0,0 +1,49 @@
|
|
|
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 matcher_exports = {};
|
|
20
|
+
__export(matcher_exports, {
|
|
21
|
+
emptyParam: () => emptyParam,
|
|
22
|
+
match: () => match
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(matcher_exports);
|
|
25
|
+
var import_router = require("../../router");
|
|
26
|
+
const emptyParam = [];
|
|
27
|
+
function match(method, path) {
|
|
28
|
+
const matchers = this.buildAllMatchers();
|
|
29
|
+
const match2 = (method2, path2) => {
|
|
30
|
+
const matcher = matchers[method2] || matchers[import_router.METHOD_NAME_ALL];
|
|
31
|
+
const staticMatch = matcher[2][path2];
|
|
32
|
+
if (staticMatch) {
|
|
33
|
+
return staticMatch;
|
|
34
|
+
}
|
|
35
|
+
const match3 = path2.match(matcher[0]);
|
|
36
|
+
if (!match3) {
|
|
37
|
+
return [[], emptyParam];
|
|
38
|
+
}
|
|
39
|
+
const index = match3.indexOf("", 1);
|
|
40
|
+
return [matcher[1][index], match3];
|
|
41
|
+
};
|
|
42
|
+
this.match = match2;
|
|
43
|
+
return match2(method, path);
|
|
44
|
+
}
|
|
45
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
46
|
+
0 && (module.exports = {
|
|
47
|
+
emptyParam,
|
|
48
|
+
match
|
|
49
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
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 prepared_router_exports = {};
|
|
20
|
+
__export(prepared_router_exports, {
|
|
21
|
+
PreparedRegExpRouter: () => PreparedRegExpRouter,
|
|
22
|
+
buildInitParams: () => buildInitParams,
|
|
23
|
+
serializeInitParams: () => serializeInitParams
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(prepared_router_exports);
|
|
26
|
+
var import_router = require("../../router");
|
|
27
|
+
var import_matcher = require("./matcher");
|
|
28
|
+
var import_router2 = require("./router");
|
|
29
|
+
class PreparedRegExpRouter {
|
|
30
|
+
name = "PreparedRegExpRouter";
|
|
31
|
+
#matchers;
|
|
32
|
+
#relocateMap;
|
|
33
|
+
constructor(matchers, relocateMap) {
|
|
34
|
+
this.#matchers = matchers;
|
|
35
|
+
this.#relocateMap = relocateMap;
|
|
36
|
+
}
|
|
37
|
+
#addWildcard(method, handlerData) {
|
|
38
|
+
const matcher = this.#matchers[method];
|
|
39
|
+
matcher[1].forEach((list) => list && list.push(handlerData));
|
|
40
|
+
Object.values(matcher[2]).forEach((list) => list[0].push(handlerData));
|
|
41
|
+
}
|
|
42
|
+
#addPath(method, path, handler, indexes, map) {
|
|
43
|
+
const matcher = this.#matchers[method];
|
|
44
|
+
if (!map) {
|
|
45
|
+
matcher[2][path][0].push([handler, {}]);
|
|
46
|
+
} else {
|
|
47
|
+
indexes.forEach((index) => {
|
|
48
|
+
if (typeof index === "number") {
|
|
49
|
+
matcher[1][index].push([handler, map]);
|
|
50
|
+
} else {
|
|
51
|
+
;
|
|
52
|
+
matcher[2][index || path][0].push([handler, map]);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
add(method, path, handler) {
|
|
58
|
+
if (!this.#matchers[method]) {
|
|
59
|
+
const all = this.#matchers[import_router.METHOD_NAME_ALL];
|
|
60
|
+
const staticMap = {};
|
|
61
|
+
for (const key in all[2]) {
|
|
62
|
+
staticMap[key] = [all[2][key][0].slice(), import_matcher.emptyParam];
|
|
63
|
+
}
|
|
64
|
+
this.#matchers[method] = [
|
|
65
|
+
all[0],
|
|
66
|
+
all[1].map((list) => Array.isArray(list) ? list.slice() : 0),
|
|
67
|
+
staticMap
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
if (path === "/*" || path === "*") {
|
|
71
|
+
const handlerData = [handler, {}];
|
|
72
|
+
if (method === import_router.METHOD_NAME_ALL) {
|
|
73
|
+
for (const m in this.#matchers) {
|
|
74
|
+
this.#addWildcard(m, handlerData);
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
this.#addWildcard(method, handlerData);
|
|
78
|
+
}
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const data = this.#relocateMap[path];
|
|
82
|
+
if (!data) {
|
|
83
|
+
throw new Error(`Path ${path} is not registered`);
|
|
84
|
+
}
|
|
85
|
+
for (const [indexes, map] of data) {
|
|
86
|
+
if (method === import_router.METHOD_NAME_ALL) {
|
|
87
|
+
for (const m in this.#matchers) {
|
|
88
|
+
this.#addPath(m, path, handler, indexes, map);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
this.#addPath(method, path, handler, indexes, map);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
buildAllMatchers() {
|
|
96
|
+
return this.#matchers;
|
|
97
|
+
}
|
|
98
|
+
match = import_matcher.match;
|
|
99
|
+
}
|
|
100
|
+
const buildInitParams = ({ paths }) => {
|
|
101
|
+
const RegExpRouterWithMatcherExport = class extends import_router2.RegExpRouter {
|
|
102
|
+
buildAndExportAllMatchers() {
|
|
103
|
+
return this.buildAllMatchers();
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const router = new RegExpRouterWithMatcherExport();
|
|
107
|
+
for (const path of paths) {
|
|
108
|
+
router.add(import_router.METHOD_NAME_ALL, path, path);
|
|
109
|
+
}
|
|
110
|
+
const matchers = router.buildAndExportAllMatchers();
|
|
111
|
+
const all = matchers[import_router.METHOD_NAME_ALL];
|
|
112
|
+
const relocateMap = {};
|
|
113
|
+
for (const path of paths) {
|
|
114
|
+
if (path === "/*" || path === "*") {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
all[1].forEach((list, i) => {
|
|
118
|
+
list.forEach(([p, map]) => {
|
|
119
|
+
if (p === path) {
|
|
120
|
+
if (relocateMap[path]) {
|
|
121
|
+
relocateMap[path][0][1] = {
|
|
122
|
+
...relocateMap[path][0][1],
|
|
123
|
+
...map
|
|
124
|
+
};
|
|
125
|
+
} else {
|
|
126
|
+
relocateMap[path] = [[[], map]];
|
|
127
|
+
}
|
|
128
|
+
if (relocateMap[path][0][0].findIndex((j) => j === i) === -1) {
|
|
129
|
+
relocateMap[path][0][0].push(i);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
for (const path2 in all[2]) {
|
|
135
|
+
all[2][path2][0].forEach(([p]) => {
|
|
136
|
+
if (p === path) {
|
|
137
|
+
relocateMap[path] ||= [[[]]];
|
|
138
|
+
const value = path2 === path ? "" : path2;
|
|
139
|
+
if (relocateMap[path][0][0].findIndex((v) => v === value) === -1) {
|
|
140
|
+
relocateMap[path][0][0].push(value);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (let i = 0, len = all[1].length; i < len; i++) {
|
|
147
|
+
all[1][i] = all[1][i] ? [] : 0;
|
|
148
|
+
}
|
|
149
|
+
for (const path in all[2]) {
|
|
150
|
+
all[2][path][0] = [];
|
|
151
|
+
}
|
|
152
|
+
return [matchers, relocateMap];
|
|
153
|
+
};
|
|
154
|
+
const serializeInitParams = ([matchers, relocateMap]) => {
|
|
155
|
+
const matchersStr = JSON.stringify(
|
|
156
|
+
matchers,
|
|
157
|
+
(_, value) => value instanceof RegExp ? `##${value.toString()}##` : value
|
|
158
|
+
).replace(/"##(.+?)##"/g, (_, str) => str.replace(/\\\\/g, "\\"));
|
|
159
|
+
const relocateMapStr = JSON.stringify(relocateMap);
|
|
160
|
+
return `[${matchersStr},${relocateMapStr}]`;
|
|
161
|
+
};
|
|
162
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
163
|
+
0 && (module.exports = {
|
|
164
|
+
PreparedRegExpRouter,
|
|
165
|
+
buildInitParams,
|
|
166
|
+
serializeInitParams
|
|
167
|
+
});
|
|
@@ -23,9 +23,9 @@ __export(router_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(router_exports);
|
|
24
24
|
var import_router = require("../../router");
|
|
25
25
|
var import_url = require("../../utils/url");
|
|
26
|
+
var import_matcher = require("./matcher");
|
|
26
27
|
var import_node = require("./node");
|
|
27
28
|
var import_trie = require("./trie");
|
|
28
|
-
const emptyParam = [];
|
|
29
29
|
const nullMatcher = [/^$/, [], /* @__PURE__ */ Object.create(null)];
|
|
30
30
|
let wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
31
31
|
function buildWildcardRegExp(path) {
|
|
@@ -54,7 +54,7 @@ function buildMatcherFromPreprocessedRoutes(routes) {
|
|
|
54
54
|
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {
|
|
55
55
|
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i];
|
|
56
56
|
if (pathErrorCheckOnly) {
|
|
57
|
-
staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
|
57
|
+
staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), import_matcher.emptyParam];
|
|
58
58
|
} else {
|
|
59
59
|
j++;
|
|
60
60
|
}
|
|
@@ -172,30 +172,14 @@ class RegExpRouter {
|
|
|
172
172
|
});
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
|
-
match
|
|
176
|
-
|
|
177
|
-
const matchers = this.#buildAllMatchers();
|
|
178
|
-
this.match = (method2, path2) => {
|
|
179
|
-
const matcher = matchers[method2] || matchers[import_router.METHOD_NAME_ALL];
|
|
180
|
-
const staticMatch = matcher[2][path2];
|
|
181
|
-
if (staticMatch) {
|
|
182
|
-
return staticMatch;
|
|
183
|
-
}
|
|
184
|
-
const match = path2.match(matcher[0]);
|
|
185
|
-
if (!match) {
|
|
186
|
-
return [[], emptyParam];
|
|
187
|
-
}
|
|
188
|
-
const index = match.indexOf("", 1);
|
|
189
|
-
return [matcher[1][index], match];
|
|
190
|
-
};
|
|
191
|
-
return this.match(method, path);
|
|
192
|
-
}
|
|
193
|
-
#buildAllMatchers() {
|
|
175
|
+
match = import_matcher.match;
|
|
176
|
+
buildAllMatchers() {
|
|
194
177
|
const matchers = /* @__PURE__ */ Object.create(null);
|
|
195
178
|
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
196
179
|
matchers[method] ||= this.#buildMatcher(method);
|
|
197
180
|
});
|
|
198
181
|
this.#middleware = this.#routes = void 0;
|
|
182
|
+
clearWildcardRegExpCache();
|
|
199
183
|
return matchers;
|
|
200
184
|
}
|
|
201
185
|
#buildMatcher(method) {
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
// src/router/reg-exp-router/index.ts
|
|
2
2
|
import { RegExpRouter } from "./router.js";
|
|
3
|
+
import { PreparedRegExpRouter, buildInitParams, serializeInitParams } from "./prepared-router.js";
|
|
3
4
|
export {
|
|
4
|
-
|
|
5
|
+
PreparedRegExpRouter,
|
|
6
|
+
RegExpRouter,
|
|
7
|
+
buildInitParams,
|
|
8
|
+
serializeInitParams
|
|
5
9
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/router/reg-exp-router/matcher.ts
|
|
2
|
+
import { METHOD_NAME_ALL } from "../../router.js";
|
|
3
|
+
var emptyParam = [];
|
|
4
|
+
function match(method, path) {
|
|
5
|
+
const matchers = this.buildAllMatchers();
|
|
6
|
+
const match2 = (method2, path2) => {
|
|
7
|
+
const matcher = matchers[method2] || matchers[METHOD_NAME_ALL];
|
|
8
|
+
const staticMatch = matcher[2][path2];
|
|
9
|
+
if (staticMatch) {
|
|
10
|
+
return staticMatch;
|
|
11
|
+
}
|
|
12
|
+
const match3 = path2.match(matcher[0]);
|
|
13
|
+
if (!match3) {
|
|
14
|
+
return [[], emptyParam];
|
|
15
|
+
}
|
|
16
|
+
const index = match3.indexOf("", 1);
|
|
17
|
+
return [matcher[1][index], match3];
|
|
18
|
+
};
|
|
19
|
+
this.match = match2;
|
|
20
|
+
return match2(method, path);
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
emptyParam,
|
|
24
|
+
match
|
|
25
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// src/router/reg-exp-router/prepared-router.ts
|
|
2
|
+
import { METHOD_NAME_ALL } from "../../router.js";
|
|
3
|
+
import { match, emptyParam } from "./matcher.js";
|
|
4
|
+
import { RegExpRouter } from "./router.js";
|
|
5
|
+
var PreparedRegExpRouter = class {
|
|
6
|
+
name = "PreparedRegExpRouter";
|
|
7
|
+
#matchers;
|
|
8
|
+
#relocateMap;
|
|
9
|
+
constructor(matchers, relocateMap) {
|
|
10
|
+
this.#matchers = matchers;
|
|
11
|
+
this.#relocateMap = relocateMap;
|
|
12
|
+
}
|
|
13
|
+
#addWildcard(method, handlerData) {
|
|
14
|
+
const matcher = this.#matchers[method];
|
|
15
|
+
matcher[1].forEach((list) => list && list.push(handlerData));
|
|
16
|
+
Object.values(matcher[2]).forEach((list) => list[0].push(handlerData));
|
|
17
|
+
}
|
|
18
|
+
#addPath(method, path, handler, indexes, map) {
|
|
19
|
+
const matcher = this.#matchers[method];
|
|
20
|
+
if (!map) {
|
|
21
|
+
matcher[2][path][0].push([handler, {}]);
|
|
22
|
+
} else {
|
|
23
|
+
indexes.forEach((index) => {
|
|
24
|
+
if (typeof index === "number") {
|
|
25
|
+
matcher[1][index].push([handler, map]);
|
|
26
|
+
} else {
|
|
27
|
+
;
|
|
28
|
+
matcher[2][index || path][0].push([handler, map]);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
add(method, path, handler) {
|
|
34
|
+
if (!this.#matchers[method]) {
|
|
35
|
+
const all = this.#matchers[METHOD_NAME_ALL];
|
|
36
|
+
const staticMap = {};
|
|
37
|
+
for (const key in all[2]) {
|
|
38
|
+
staticMap[key] = [all[2][key][0].slice(), emptyParam];
|
|
39
|
+
}
|
|
40
|
+
this.#matchers[method] = [
|
|
41
|
+
all[0],
|
|
42
|
+
all[1].map((list) => Array.isArray(list) ? list.slice() : 0),
|
|
43
|
+
staticMap
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
if (path === "/*" || path === "*") {
|
|
47
|
+
const handlerData = [handler, {}];
|
|
48
|
+
if (method === METHOD_NAME_ALL) {
|
|
49
|
+
for (const m in this.#matchers) {
|
|
50
|
+
this.#addWildcard(m, handlerData);
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
this.#addWildcard(method, handlerData);
|
|
54
|
+
}
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const data = this.#relocateMap[path];
|
|
58
|
+
if (!data) {
|
|
59
|
+
throw new Error(`Path ${path} is not registered`);
|
|
60
|
+
}
|
|
61
|
+
for (const [indexes, map] of data) {
|
|
62
|
+
if (method === METHOD_NAME_ALL) {
|
|
63
|
+
for (const m in this.#matchers) {
|
|
64
|
+
this.#addPath(m, path, handler, indexes, map);
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
this.#addPath(method, path, handler, indexes, map);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
buildAllMatchers() {
|
|
72
|
+
return this.#matchers;
|
|
73
|
+
}
|
|
74
|
+
match = match;
|
|
75
|
+
};
|
|
76
|
+
var buildInitParams = ({ paths }) => {
|
|
77
|
+
const RegExpRouterWithMatcherExport = class extends RegExpRouter {
|
|
78
|
+
buildAndExportAllMatchers() {
|
|
79
|
+
return this.buildAllMatchers();
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const router = new RegExpRouterWithMatcherExport();
|
|
83
|
+
for (const path of paths) {
|
|
84
|
+
router.add(METHOD_NAME_ALL, path, path);
|
|
85
|
+
}
|
|
86
|
+
const matchers = router.buildAndExportAllMatchers();
|
|
87
|
+
const all = matchers[METHOD_NAME_ALL];
|
|
88
|
+
const relocateMap = {};
|
|
89
|
+
for (const path of paths) {
|
|
90
|
+
if (path === "/*" || path === "*") {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
all[1].forEach((list, i) => {
|
|
94
|
+
list.forEach(([p, map]) => {
|
|
95
|
+
if (p === path) {
|
|
96
|
+
if (relocateMap[path]) {
|
|
97
|
+
relocateMap[path][0][1] = {
|
|
98
|
+
...relocateMap[path][0][1],
|
|
99
|
+
...map
|
|
100
|
+
};
|
|
101
|
+
} else {
|
|
102
|
+
relocateMap[path] = [[[], map]];
|
|
103
|
+
}
|
|
104
|
+
if (relocateMap[path][0][0].findIndex((j) => j === i) === -1) {
|
|
105
|
+
relocateMap[path][0][0].push(i);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
for (const path2 in all[2]) {
|
|
111
|
+
all[2][path2][0].forEach(([p]) => {
|
|
112
|
+
if (p === path) {
|
|
113
|
+
relocateMap[path] ||= [[[]]];
|
|
114
|
+
const value = path2 === path ? "" : path2;
|
|
115
|
+
if (relocateMap[path][0][0].findIndex((v) => v === value) === -1) {
|
|
116
|
+
relocateMap[path][0][0].push(value);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
for (let i = 0, len = all[1].length; i < len; i++) {
|
|
123
|
+
all[1][i] = all[1][i] ? [] : 0;
|
|
124
|
+
}
|
|
125
|
+
for (const path in all[2]) {
|
|
126
|
+
all[2][path][0] = [];
|
|
127
|
+
}
|
|
128
|
+
return [matchers, relocateMap];
|
|
129
|
+
};
|
|
130
|
+
var serializeInitParams = ([matchers, relocateMap]) => {
|
|
131
|
+
const matchersStr = JSON.stringify(
|
|
132
|
+
matchers,
|
|
133
|
+
(_, value) => value instanceof RegExp ? `##${value.toString()}##` : value
|
|
134
|
+
).replace(/"##(.+?)##"/g, (_, str) => str.replace(/\\\\/g, "\\"));
|
|
135
|
+
const relocateMapStr = JSON.stringify(relocateMap);
|
|
136
|
+
return `[${matchersStr},${relocateMapStr}]`;
|
|
137
|
+
};
|
|
138
|
+
export {
|
|
139
|
+
PreparedRegExpRouter,
|
|
140
|
+
buildInitParams,
|
|
141
|
+
serializeInitParams
|
|
142
|
+
};
|
|
@@ -5,9 +5,9 @@ import {
|
|
|
5
5
|
UnsupportedPathError
|
|
6
6
|
} from "../../router.js";
|
|
7
7
|
import { checkOptionalParameter } from "../../utils/url.js";
|
|
8
|
+
import { match, emptyParam } from "./matcher.js";
|
|
8
9
|
import { PATH_ERROR } from "./node.js";
|
|
9
10
|
import { Trie } from "./trie.js";
|
|
10
|
-
var emptyParam = [];
|
|
11
11
|
var nullMatcher = [/^$/, [], /* @__PURE__ */ Object.create(null)];
|
|
12
12
|
var wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
13
13
|
function buildWildcardRegExp(path) {
|
|
@@ -154,30 +154,14 @@ var RegExpRouter = class {
|
|
|
154
154
|
});
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
|
-
match
|
|
158
|
-
|
|
159
|
-
const matchers = this.#buildAllMatchers();
|
|
160
|
-
this.match = (method2, path2) => {
|
|
161
|
-
const matcher = matchers[method2] || matchers[METHOD_NAME_ALL];
|
|
162
|
-
const staticMatch = matcher[2][path2];
|
|
163
|
-
if (staticMatch) {
|
|
164
|
-
return staticMatch;
|
|
165
|
-
}
|
|
166
|
-
const match = path2.match(matcher[0]);
|
|
167
|
-
if (!match) {
|
|
168
|
-
return [[], emptyParam];
|
|
169
|
-
}
|
|
170
|
-
const index = match.indexOf("", 1);
|
|
171
|
-
return [matcher[1][index], match];
|
|
172
|
-
};
|
|
173
|
-
return this.match(method, path);
|
|
174
|
-
}
|
|
175
|
-
#buildAllMatchers() {
|
|
157
|
+
match = match;
|
|
158
|
+
buildAllMatchers() {
|
|
176
159
|
const matchers = /* @__PURE__ */ Object.create(null);
|
|
177
160
|
Object.keys(this.#routes).concat(Object.keys(this.#middleware)).forEach((method) => {
|
|
178
161
|
matchers[method] ||= this.#buildMatcher(method);
|
|
179
162
|
});
|
|
180
163
|
this.#middleware = this.#routes = void 0;
|
|
164
|
+
clearWildcardRegExpCache();
|
|
181
165
|
return matchers;
|
|
182
166
|
}
|
|
183
167
|
#buildMatcher(method) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ParamIndexMap, Result, Router } from '../../router';
|
|
2
|
+
export type HandlerData<T> = [
|
|
3
|
+
T,
|
|
4
|
+
ParamIndexMap
|
|
5
|
+
][];
|
|
6
|
+
export type StaticMap<T> = Record<string, Result<T>>;
|
|
7
|
+
export type Matcher<T> = [
|
|
8
|
+
RegExp,
|
|
9
|
+
HandlerData<T>[],
|
|
10
|
+
StaticMap<T>
|
|
11
|
+
];
|
|
12
|
+
export type MatcherMap<T> = Record<string, Matcher<T> | null>;
|
|
13
|
+
export declare const emptyParam: string[];
|
|
14
|
+
export declare function match<R extends Router<T>, T>(this: R, method: string, path: string): Result<T>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ParamIndexMap, Router } from '../../router';
|
|
2
|
+
import type { MatcherMap } from './matcher';
|
|
3
|
+
import { match } from './matcher';
|
|
4
|
+
type RelocateMap = Record<string, ([
|
|
5
|
+
(number | string)[],
|
|
6
|
+
ParamIndexMap
|
|
7
|
+
] | [
|
|
8
|
+
(number | string)[]
|
|
9
|
+
])[]>;
|
|
10
|
+
export declare class PreparedRegExpRouter<T> implements Router<T> {
|
|
11
|
+
name: string;
|
|
12
|
+
constructor(matchers: MatcherMap<T>, relocateMap: RelocateMap);
|
|
13
|
+
add(method: string, path: string, handler: T): void;
|
|
14
|
+
protected buildAllMatchers(): MatcherMap<T>;
|
|
15
|
+
match: typeof match<Router<T>, T>;
|
|
16
|
+
}
|
|
17
|
+
export declare const buildInitParams: (params: {
|
|
18
|
+
paths: string[];
|
|
19
|
+
}) => ConstructorParameters<typeof PreparedRegExpRouter>;
|
|
20
|
+
export declare const serializeInitParams: (params: ConstructorParameters<typeof PreparedRegExpRouter>) => string;
|
|
21
|
+
export {};
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Router } from '../../router';
|
|
2
|
+
import type { MatcherMap } from './matcher';
|
|
3
|
+
import { match } from './matcher';
|
|
2
4
|
export declare class RegExpRouter<T> implements Router<T> {
|
|
3
5
|
name: string;
|
|
4
6
|
constructor();
|
|
5
7
|
add(method: string, path: string, handler: T): void;
|
|
6
|
-
match
|
|
8
|
+
match: typeof match<Router<T>, T>;
|
|
9
|
+
protected buildAllMatchers(): MatcherMap<T>;
|
|
7
10
|
}
|
|
@@ -36,7 +36,7 @@ export type JSONValue = JSONObject | JSONArray | JSONPrimitive;
|
|
|
36
36
|
* which defaults to `bigint | ReadonlyArray<bigint>`.
|
|
37
37
|
* You can set it to `never` to disable this check.
|
|
38
38
|
*/
|
|
39
|
-
export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends
|
|
39
|
+
export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends {
|
|
40
40
|
toJSON(): infer J;
|
|
41
41
|
} ? (() => J) extends () => JSONPrimitive ? J : (() => J) extends () => {
|
|
42
42
|
toJSON(): unknown;
|
|
@@ -44,7 +44,7 @@ export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends T
|
|
|
44
44
|
[K in keyof T]: JSONParsed<InvalidToNull<T[K]>, TError>;
|
|
45
45
|
} : T extends Set<unknown> | Map<unknown, unknown> | Record<string, never> ? {} : T extends object ? T[keyof T] extends TError ? never : {
|
|
46
46
|
[K in keyof OmitSymbolKeys<T> as IsInvalid<T[K]> extends true ? never : K]: boolean extends IsInvalid<T[K]> ? JSONParsed<T[K], TError> | undefined : JSONParsed<T[K], TError>;
|
|
47
|
-
} : T extends unknown ? JSONValue : never;
|
|
47
|
+
} : T extends unknown ? T extends TError ? never : JSONValue : never;
|
|
48
48
|
/**
|
|
49
49
|
* Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
50
50
|
* @copyright from sindresorhus/type-fest
|