@esportsplus/routing 0.0.49 → 0.1.1
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/build/browser.d.ts +2 -3
- package/build/browser.js +8 -9
- package/build/constants.d.ts +5 -1
- package/build/constants.js +5 -1
- package/build/index.d.ts +3 -5
- package/build/index.js +3 -5
- package/build/router/index.d.ts +15 -13
- package/build/router/index.js +33 -36
- package/build/router/node.d.ts +2 -2
- package/build/router/node.js +1 -1
- package/build/types.d.ts +13 -3
- package/build/types.js +2 -2
- package/package.json +2 -2
- package/src/browser.ts +9 -11
- package/src/constants.ts +10 -1
- package/src/index.ts +3 -7
- package/src/router/index.ts +47 -51
- package/src/router/node.ts +3 -3
- package/src/types.ts +16 -3
- package/build/router/route.d.ts +0 -11
- package/build/router/route.js +0 -18
- package/build/router2/constants.d.ts +0 -3
- package/build/router2/constants.js +0 -3
- package/build/router2/index.d.ts +0 -11
- package/build/router2/index.js +0 -226
- package/build/router2/node.d.ts +0 -10
- package/build/router2/node.js +0 -117
- package/build/router2/trie.d.ts +0 -11
- package/build/router2/trie.js +0 -53
- package/build/router2/types.d.ts +0 -11
- package/build/router2/types.js +0 -1
- package/src/router/route.ts +0 -28
- package/src/router2/constants.ts +0 -6
- package/src/router2/index.ts +0 -330
- package/src/router2/node.ts +0 -184
- package/src/router2/trie.ts +0 -88
- package/src/router2/types.ts +0 -59
package/src/types.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { NeverAsync } from '@esportsplus/typescript';
|
|
2
|
-
import {
|
|
2
|
+
import { Router } from './router';
|
|
3
|
+
import pipeline from '@esportsplus/pipeline';
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
type Middleware<T> = NeverAsync<(input: Request<T>, next: Next<T>) => T>;
|
|
6
7
|
|
|
8
|
+
type Name = string;
|
|
9
|
+
|
|
7
10
|
type Next<T> = NeverAsync<(input: Request<T>) => T>;
|
|
8
11
|
|
|
9
12
|
type Options<T> = {
|
|
10
13
|
middleware?: Middleware<T>[];
|
|
11
14
|
name?: string;
|
|
12
15
|
path?: string;
|
|
13
|
-
responder: Next<T>;
|
|
14
16
|
subdomain?: string;
|
|
15
17
|
};
|
|
16
18
|
|
|
@@ -27,5 +29,16 @@ type Request<T> = {
|
|
|
27
29
|
subdomain?: string;
|
|
28
30
|
};
|
|
29
31
|
|
|
32
|
+
type Route<T> = {
|
|
33
|
+
name: Name | null;
|
|
34
|
+
path: string | null;
|
|
35
|
+
pipeline: ReturnType<typeof pipeline<Request<T>, T>>,
|
|
36
|
+
subdomain: string | null;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type RouteOptions<T> = Options<T> & {
|
|
40
|
+
responder: Next<T>;
|
|
41
|
+
};
|
|
42
|
+
|
|
30
43
|
|
|
31
|
-
export { Middleware, Next, Options, Request, Route, Router };
|
|
44
|
+
export { Middleware, Name, Next, Options, Request, Route, RouteOptions, Router };
|
package/build/router/route.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Middleware, Next, Request } from '../types';
|
|
2
|
-
declare class Route<T> {
|
|
3
|
-
middleware: Middleware<T>[] | null;
|
|
4
|
-
name: string | null;
|
|
5
|
-
path: string | null;
|
|
6
|
-
responder: Next<T>;
|
|
7
|
-
subdomain: string | null;
|
|
8
|
-
constructor(responder: Next<T>);
|
|
9
|
-
dispatch(request: Request<T>): T;
|
|
10
|
-
}
|
|
11
|
-
export { Route };
|
package/build/router/route.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import pipeline from '@esportsplus/pipeline';
|
|
2
|
-
class Route {
|
|
3
|
-
middleware = null;
|
|
4
|
-
name = null;
|
|
5
|
-
path = null;
|
|
6
|
-
responder;
|
|
7
|
-
subdomain = null;
|
|
8
|
-
constructor(responder) {
|
|
9
|
-
this.responder = responder;
|
|
10
|
-
}
|
|
11
|
-
dispatch(request) {
|
|
12
|
-
if (this.middleware === null) {
|
|
13
|
-
return this.responder(request);
|
|
14
|
-
}
|
|
15
|
-
return pipeline(...this.middleware, (request) => this.responder(request))(request);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
export { Route };
|
package/build/router2/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { HandlerMap, Method, Path, Result } from './types';
|
|
2
|
-
declare class Router<T> {
|
|
3
|
-
middleware?: Record<Method, Record<Path, HandlerMap<T>[]>>;
|
|
4
|
-
routes?: Record<Method, Record<Path, HandlerMap<T>[]>>;
|
|
5
|
-
constructor();
|
|
6
|
-
private buildAllMatchers;
|
|
7
|
-
private buildMatcher;
|
|
8
|
-
add(method: Method, path: Path, handler: T): void;
|
|
9
|
-
match(method: Method, path: Path): Result<T>;
|
|
10
|
-
}
|
|
11
|
-
export { Router };
|
package/build/router2/index.js
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { METHOD_NAME_ALL, PATH_ERROR } from './constants';
|
|
2
|
-
import { Trie } from './trie';
|
|
3
|
-
const NULL_MATCHER = [/^$/, [], Object.create(null)];
|
|
4
|
-
let empty = [], wildcardCache = Object.create(null);
|
|
5
|
-
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
6
|
-
if (routes.length === 0) {
|
|
7
|
-
return NULL_MATCHER;
|
|
8
|
-
}
|
|
9
|
-
let handlerMetadata = [], routesWithStaticPathFlag = routes
|
|
10
|
-
.map((route) => [!/\*|\/:/.test(route[0]), ...route])
|
|
11
|
-
.sort(([isStaticA, pathA], [isStaticB, pathB]) => isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length), staticMap = Object.create(null), trie = new Trie();
|
|
12
|
-
for (let i = 0, j = -1, n = routesWithStaticPathFlag.length; i < n; i++) {
|
|
13
|
-
let [validatePathOnly, path, handlers] = routesWithStaticPathFlag[i];
|
|
14
|
-
if (validatePathOnly) {
|
|
15
|
-
staticMap[path] = [handlers.map(([h]) => [h, Object.create(null)]), empty];
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
j++;
|
|
19
|
-
}
|
|
20
|
-
let parameterMetadata;
|
|
21
|
-
try {
|
|
22
|
-
parameterMetadata = trie.insert(path, j, validatePathOnly);
|
|
23
|
-
}
|
|
24
|
-
catch (e) {
|
|
25
|
-
throw e === PATH_ERROR ? new Error(`Routing: '${path}' is not supported`) : e;
|
|
26
|
-
}
|
|
27
|
-
if (validatePathOnly) {
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
handlerMetadata[j] = handlers.map(([h, i]) => {
|
|
31
|
-
let paramIndexMap = Object.create(null);
|
|
32
|
-
i -= 1;
|
|
33
|
-
for (; i >= 0; i--) {
|
|
34
|
-
let [key, value] = parameterMetadata[i];
|
|
35
|
-
paramIndexMap[key] = value;
|
|
36
|
-
}
|
|
37
|
-
return [h, paramIndexMap];
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
let [regexp, handlerReplacementMap, parameterReplacementMap] = trie.build();
|
|
41
|
-
for (let i = 0, n = handlerMetadata.length; i < n; i++) {
|
|
42
|
-
let metadata = handlerMetadata[i];
|
|
43
|
-
for (let j = 0, n = metadata.length; j < n; j++) {
|
|
44
|
-
let map = metadata[j]?.[1];
|
|
45
|
-
if (!map) {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
for (let key in map) {
|
|
49
|
-
map[key] = parameterReplacementMap[map[key]];
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
let handlerMap = [];
|
|
54
|
-
for (let i = 0, n = handlerReplacementMap.length; i < n; i++) {
|
|
55
|
-
handlerMap[i] = handlerMetadata[handlerReplacementMap[i]];
|
|
56
|
-
}
|
|
57
|
-
return [regexp, handlerMap, staticMap];
|
|
58
|
-
}
|
|
59
|
-
function buildWildcardRegExp(path) {
|
|
60
|
-
return (wildcardCache[path] ??= new RegExp(path === '*'
|
|
61
|
-
? ''
|
|
62
|
-
: `^${path.replace(/\/\*$|([.\\+*[^\]$()])/g, (_, char) => char ? `\\${char}` : '(?:|/.*)')}$`));
|
|
63
|
-
}
|
|
64
|
-
function clearWildcardCache() {
|
|
65
|
-
wildcardCache = Object.create(null);
|
|
66
|
-
}
|
|
67
|
-
function findMiddleware(middleware, path) {
|
|
68
|
-
if (!middleware) {
|
|
69
|
-
return undefined;
|
|
70
|
-
}
|
|
71
|
-
let keys = Object.keys(middleware).sort((a, b) => b.length - a.length);
|
|
72
|
-
for (let i = 0, n = keys.length; i < n; i++) {
|
|
73
|
-
if (buildWildcardRegExp(keys[i]).test(path)) {
|
|
74
|
-
return [...middleware[keys[i]]];
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return undefined;
|
|
78
|
-
}
|
|
79
|
-
function findOptionalParameters(path) {
|
|
80
|
-
if (!path.match(/\:.+\?$/)) {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
let base = '', results = [], segments = path.split('/');
|
|
84
|
-
for (let i = 0, n = segments.length; i < n; i++) {
|
|
85
|
-
let segment = segments[i];
|
|
86
|
-
if (segment !== '' && !/\:/.test(segment)) {
|
|
87
|
-
base += '/' + segment;
|
|
88
|
-
}
|
|
89
|
-
else if (/\:/.test(segment)) {
|
|
90
|
-
if (/\?/.test(segment)) {
|
|
91
|
-
if (results.length === 0 && base === '') {
|
|
92
|
-
results.push('/');
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
results.push(base);
|
|
96
|
-
}
|
|
97
|
-
base += '/' + segment.replace('?', '');
|
|
98
|
-
results.push(base);
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
base += '/' + segment;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return results.filter((v, i, a) => a.indexOf(v) === i);
|
|
106
|
-
}
|
|
107
|
-
class Router {
|
|
108
|
-
middleware;
|
|
109
|
-
routes;
|
|
110
|
-
constructor() {
|
|
111
|
-
this.middleware = {
|
|
112
|
-
[METHOD_NAME_ALL]: Object.create(null)
|
|
113
|
-
};
|
|
114
|
-
this.routes = {
|
|
115
|
-
[METHOD_NAME_ALL]: Object.create(null)
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
buildAllMatchers() {
|
|
119
|
-
let matchers = Object.create(null);
|
|
120
|
-
for (let method in this.middleware) {
|
|
121
|
-
matchers[method] ||= this.buildMatcher(method);
|
|
122
|
-
}
|
|
123
|
-
for (let method in this.routes) {
|
|
124
|
-
matchers[method] ||= this.buildMatcher(method);
|
|
125
|
-
}
|
|
126
|
-
this.middleware = this.routes = undefined;
|
|
127
|
-
return matchers;
|
|
128
|
-
}
|
|
129
|
-
buildMatcher(method) {
|
|
130
|
-
let isAll = method === METHOD_NAME_ALL, properties = [this.middleware, this.routes], property, routes = [];
|
|
131
|
-
while (property = properties.pop()) {
|
|
132
|
-
let values = property[method]
|
|
133
|
-
? Object.keys(property[method]).map((path) => [path, property[method][path]])
|
|
134
|
-
: [];
|
|
135
|
-
if (values.length !== 0) {
|
|
136
|
-
isAll ||= true;
|
|
137
|
-
routes.push(...values);
|
|
138
|
-
}
|
|
139
|
-
else if (method !== METHOD_NAME_ALL) {
|
|
140
|
-
routes.push(...Object.keys(property[METHOD_NAME_ALL]).map((path) => [path, property[METHOD_NAME_ALL][path]]));
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return isAll === true ? buildMatcherFromPreprocessedRoutes(routes) : null;
|
|
144
|
-
}
|
|
145
|
-
add(method, path, handler) {
|
|
146
|
-
let { middleware, routes } = this;
|
|
147
|
-
if (!middleware || !routes) {
|
|
148
|
-
throw new Error('Routing: Cannot add route after matcher has been built.');
|
|
149
|
-
}
|
|
150
|
-
if (!middleware[method]) {
|
|
151
|
-
let properties = [middleware, routes], property;
|
|
152
|
-
while (property = properties.pop()) {
|
|
153
|
-
let copy = property[METHOD_NAME_ALL], into = property[method];
|
|
154
|
-
property[method] = Object.create(null);
|
|
155
|
-
for (let path in copy) {
|
|
156
|
-
into[path] = [...copy[path]];
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
if (path === '/*') {
|
|
161
|
-
path = '*';
|
|
162
|
-
}
|
|
163
|
-
let parameters = (path.match(/\/:/g) || []).length;
|
|
164
|
-
if (/\*$/.test(path)) {
|
|
165
|
-
let regex = buildWildcardRegExp(path);
|
|
166
|
-
if (method === METHOD_NAME_ALL) {
|
|
167
|
-
for (let m in middleware) {
|
|
168
|
-
middleware[m][path] ||=
|
|
169
|
-
findMiddleware(middleware[m], path) ||
|
|
170
|
-
findMiddleware(middleware[METHOD_NAME_ALL], path) ||
|
|
171
|
-
[];
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
middleware[method][path] ||=
|
|
176
|
-
findMiddleware(middleware[method], path) ||
|
|
177
|
-
findMiddleware(middleware[METHOD_NAME_ALL], path) ||
|
|
178
|
-
[];
|
|
179
|
-
}
|
|
180
|
-
let properties = [middleware, routes], property;
|
|
181
|
-
while (property = properties.pop()) {
|
|
182
|
-
for (let m in property) {
|
|
183
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
184
|
-
let routes = property[m];
|
|
185
|
-
for (let path in routes) {
|
|
186
|
-
regex.test(path) && routes[path].push([handler, parameters]);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
let paths = findOptionalParameters(path) || [path];
|
|
194
|
-
for (let i = 0, n = paths.length; i < n; i++) {
|
|
195
|
-
let path = paths[i];
|
|
196
|
-
for (let m in routes) {
|
|
197
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
198
|
-
let r = routes[m];
|
|
199
|
-
r[path] ||= [
|
|
200
|
-
...(findMiddleware(middleware[m], path) ||
|
|
201
|
-
findMiddleware(middleware[METHOD_NAME_ALL], path) ||
|
|
202
|
-
[]),
|
|
203
|
-
];
|
|
204
|
-
r[path].push([handler, parameters - n + i + 1]);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
match(method, path) {
|
|
210
|
-
clearWildcardCache();
|
|
211
|
-
let matchers = this.buildAllMatchers();
|
|
212
|
-
this.match = (method, path) => {
|
|
213
|
-
let matcher = (matchers[method] || matchers[METHOD_NAME_ALL]), staticMatch = matcher[2][path];
|
|
214
|
-
if (staticMatch) {
|
|
215
|
-
return staticMatch;
|
|
216
|
-
}
|
|
217
|
-
let match = path.match(matcher[0]);
|
|
218
|
-
if (!match) {
|
|
219
|
-
return [[], empty];
|
|
220
|
-
}
|
|
221
|
-
return [matcher[1][match.indexOf('', 1)], match];
|
|
222
|
-
};
|
|
223
|
-
return this.match(method, path);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
export { Router };
|
package/build/router2/node.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { Trie } from './trie';
|
|
2
|
-
import { ParameterMetadata, Path } from './types';
|
|
3
|
-
declare class Node {
|
|
4
|
-
children: Record<Path, Node>;
|
|
5
|
-
index?: number;
|
|
6
|
-
slot?: number;
|
|
7
|
-
buildRegexString(): Path;
|
|
8
|
-
insert(tokens: readonly Path[], index: number, parameters: ParameterMetadata, context: Trie['context'], validatePathOnly: boolean): void;
|
|
9
|
-
}
|
|
10
|
-
export { Node };
|
package/build/router2/node.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { PATH_ERROR } from './constants';
|
|
2
|
-
const LABEL_REGEXP = '[^/]+';
|
|
3
|
-
const REGEXP_CHARACTERS = new Set('.\\+*[^]$()');
|
|
4
|
-
const WILDCARD_ONLY_REGEXP = '.*';
|
|
5
|
-
const WILDCARD_TAIL_REGEXP = '(?:|/.*)';
|
|
6
|
-
const MATCH_WILDCARD_ONLY = ['', '', WILDCARD_ONLY_REGEXP];
|
|
7
|
-
const MATCH_LABEL = ['', '', LABEL_REGEXP];
|
|
8
|
-
const MATCH_WILDCARD_TAIL = ['', '', WILDCARD_TAIL_REGEXP];
|
|
9
|
-
function sort(a, b) {
|
|
10
|
-
if (a.length === 1) {
|
|
11
|
-
return b.length === 1 ? (a < b ? -1 : 1) : -1;
|
|
12
|
-
}
|
|
13
|
-
else if (b.length === 1) {
|
|
14
|
-
return 1;
|
|
15
|
-
}
|
|
16
|
-
if (a === WILDCARD_ONLY_REGEXP || a === WILDCARD_TAIL_REGEXP) {
|
|
17
|
-
return 1;
|
|
18
|
-
}
|
|
19
|
-
else if (b === WILDCARD_ONLY_REGEXP || b === WILDCARD_TAIL_REGEXP) {
|
|
20
|
-
return -1;
|
|
21
|
-
}
|
|
22
|
-
if (a === LABEL_REGEXP) {
|
|
23
|
-
return 1;
|
|
24
|
-
}
|
|
25
|
-
else if (b === LABEL_REGEXP) {
|
|
26
|
-
return -1;
|
|
27
|
-
}
|
|
28
|
-
return a.length === b.length ? (a < b ? -1 : 1) : b.length - a.length;
|
|
29
|
-
}
|
|
30
|
-
class Node {
|
|
31
|
-
children = Object.create(null);
|
|
32
|
-
index;
|
|
33
|
-
slot;
|
|
34
|
-
buildRegexString() {
|
|
35
|
-
let children = this.children, path, paths = Object.keys(children).sort(sort);
|
|
36
|
-
for (let i = 0, n = paths.length; i < n; i++) {
|
|
37
|
-
let child = children[path = paths[i]];
|
|
38
|
-
paths[i] = (typeof child.slot === 'number'
|
|
39
|
-
? `(${path})@${child.slot}`
|
|
40
|
-
: REGEXP_CHARACTERS.has(path)
|
|
41
|
-
? `\\${path}`
|
|
42
|
-
: path) + child.buildRegexString();
|
|
43
|
-
}
|
|
44
|
-
if (typeof this.index === 'number') {
|
|
45
|
-
paths.unshift(`#${this.index}`);
|
|
46
|
-
}
|
|
47
|
-
if (paths.length === 0) {
|
|
48
|
-
return '';
|
|
49
|
-
}
|
|
50
|
-
else if (paths.length === 1) {
|
|
51
|
-
return paths[0];
|
|
52
|
-
}
|
|
53
|
-
return '(?:' + paths.join('|') + ')';
|
|
54
|
-
}
|
|
55
|
-
insert(tokens, index, parameters, context, validatePathOnly) {
|
|
56
|
-
if (tokens.length === 0) {
|
|
57
|
-
if (this.index !== undefined) {
|
|
58
|
-
throw PATH_ERROR;
|
|
59
|
-
}
|
|
60
|
-
else if (validatePathOnly) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
this.index = index;
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
let [token, ...remainingTokens] = tokens, node, pattern = token === '*'
|
|
67
|
-
? remainingTokens.length === 0
|
|
68
|
-
? MATCH_WILDCARD_ONLY
|
|
69
|
-
: MATCH_LABEL
|
|
70
|
-
: token === '/*'
|
|
71
|
-
? MATCH_WILDCARD_TAIL
|
|
72
|
-
: token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
|
73
|
-
if (pattern) {
|
|
74
|
-
let name = pattern[1], path = pattern[2] || LABEL_REGEXP;
|
|
75
|
-
if (name && pattern[2]) {
|
|
76
|
-
path = path.replace(/^\((?!\?:)(?=[^)]+\)$)/, '(?:');
|
|
77
|
-
if (/\((?!\?:)/.test(path)) {
|
|
78
|
-
throw PATH_ERROR;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
node = this.children[path];
|
|
82
|
-
if (!node) {
|
|
83
|
-
for (let key in this.children) {
|
|
84
|
-
if (key !== WILDCARD_ONLY_REGEXP && key !== WILDCARD_TAIL_REGEXP) {
|
|
85
|
-
throw PATH_ERROR;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (validatePathOnly) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
node = this.children[path] = new Node();
|
|
92
|
-
if (name !== '') {
|
|
93
|
-
node.slot = context.slot++;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (name !== '' && validatePathOnly === false) {
|
|
97
|
-
parameters.push([name, node.slot]);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
node = this.children[token];
|
|
102
|
-
if (!node) {
|
|
103
|
-
for (let key in this.children) {
|
|
104
|
-
if (key.length > 1 && key !== WILDCARD_ONLY_REGEXP && key !== WILDCARD_TAIL_REGEXP) {
|
|
105
|
-
throw PATH_ERROR;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (validatePathOnly) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
node = this.children[token] = new Node();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
node.insert(remainingTokens, index, parameters, context, validatePathOnly);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
export { Node };
|
package/build/router2/trie.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Node } from './node';
|
|
2
|
-
import { Indexes, ParameterMetadata, Path } from './types';
|
|
3
|
-
declare class Trie {
|
|
4
|
-
context: {
|
|
5
|
-
slot: number;
|
|
6
|
-
};
|
|
7
|
-
root: Node;
|
|
8
|
-
build(): [RegExp, Indexes, Indexes];
|
|
9
|
-
insert(path: Path, index: number, pathErrorCheckOnly: boolean): ParameterMetadata;
|
|
10
|
-
}
|
|
11
|
-
export { Trie };
|
package/build/router2/trie.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { Node } from './node';
|
|
2
|
-
const NEVER_MATCH = [/^$/, [], []];
|
|
3
|
-
class Trie {
|
|
4
|
-
context = { slot: 0 };
|
|
5
|
-
root = new Node();
|
|
6
|
-
build() {
|
|
7
|
-
let regex = this.root.buildRegexString();
|
|
8
|
-
if (regex === '') {
|
|
9
|
-
return NEVER_MATCH;
|
|
10
|
-
}
|
|
11
|
-
let handlers = [], i = 0, parameters = [];
|
|
12
|
-
regex = regex.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handler, parameter) => {
|
|
13
|
-
if (typeof handler !== 'undefined') {
|
|
14
|
-
handlers[++i] = Number(handler);
|
|
15
|
-
return '$()';
|
|
16
|
-
}
|
|
17
|
-
else if (typeof parameter !== 'undefined') {
|
|
18
|
-
parameters[Number(parameter)] = ++i;
|
|
19
|
-
}
|
|
20
|
-
return '';
|
|
21
|
-
});
|
|
22
|
-
return [new RegExp(`^${regex}`), handlers, parameters];
|
|
23
|
-
}
|
|
24
|
-
insert(path, index, pathErrorCheckOnly) {
|
|
25
|
-
let groups = [], parameters = [];
|
|
26
|
-
for (let i = 0;;) {
|
|
27
|
-
let replaced = false;
|
|
28
|
-
path = path.replace(/\{[^}]+\}/g, (m) => {
|
|
29
|
-
let mark = `@\\${i}`;
|
|
30
|
-
groups[i++] = [mark, m];
|
|
31
|
-
replaced = true;
|
|
32
|
-
return mark;
|
|
33
|
-
});
|
|
34
|
-
if (!replaced) {
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
let tokens = path.match(/(?::[^\/]+)|(?:\/\*$)|./g) || [];
|
|
39
|
-
for (let i = groups.length - 1; i >= 0; i--) {
|
|
40
|
-
let [mark, replacement] = groups[i], token;
|
|
41
|
-
for (let j = tokens.length - 1; j >= 0; j--) {
|
|
42
|
-
token = tokens[j];
|
|
43
|
-
if (token.indexOf(mark) !== -1) {
|
|
44
|
-
token = token.replace(mark, replacement);
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
this.root.insert(tokens, index, parameters, this.context, pathErrorCheckOnly);
|
|
50
|
-
return parameters;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
export { Trie };
|
package/build/router2/types.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
type HandlerMap<T> = [T, number];
|
|
2
|
-
type HandlerMetadata<T> = [T, ParameterMap][];
|
|
3
|
-
type Indexes = number[];
|
|
4
|
-
type Matcher<T> = [RegExp, HandlerMetadata<T>[], StaticMap<T>];
|
|
5
|
-
type Method = string;
|
|
6
|
-
type ParameterMap = Record<string, number>;
|
|
7
|
-
type ParameterMetadata = [string, number][];
|
|
8
|
-
type Path = string;
|
|
9
|
-
type Result<T> = [[T, ParameterMap][], string[]] | [[T, Record<string, string>][]];
|
|
10
|
-
type StaticMap<T> = Record<Path, Result<T>>;
|
|
11
|
-
export { HandlerMap, HandlerMetadata, Indexes, Matcher, Method, ParameterMap, ParameterMetadata, Path, Result, StaticMap };
|
package/build/router2/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/src/router/route.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { Middleware, Next, Request } from '~/types';
|
|
2
|
-
import pipeline from '@esportsplus/pipeline';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class Route<T> {
|
|
6
|
-
middleware: Middleware<T>[] | null = null;
|
|
7
|
-
name: string | null = null;
|
|
8
|
-
path: string | null = null;
|
|
9
|
-
responder: Next<T>;
|
|
10
|
-
subdomain: string | null = null;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
constructor(responder: Next<T>) {
|
|
14
|
-
this.responder = responder;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
dispatch(request: Request<T>) {
|
|
19
|
-
if (this.middleware === null) {
|
|
20
|
-
return this.responder(request);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return pipeline(...this.middleware, (request) => this.responder(request))(request);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
export { Route };
|