-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
router.ts
363 lines (327 loc) · 16.4 KB
/
router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import {
NormalizedRoute,
RequestParams,
Route,
RouteHandler,
RouteOptions,
MatchedRoute
} from './types.ts';
export type RouteOptionsHasHandler = RouteOptions & Required<Pick<RouteOptions, 'handler'>>;
export type RouteOptionsHasMethod = RouteOptions & Required<Pick<RouteOptions, 'method'>>;
export type RouteOptionsHasPath = RouteOptions & Required<Pick<RouteOptions, 'path'>>;
export type RouteOptionsHasHandlerAndMethod = RouteOptions & Required<Pick<RouteOptions, 'handler' | 'method'>>;
export type RouteOptionsHasHandlerAndPath = RouteOptions & Required<Pick<RouteOptions, 'handler' | 'path'>>;
export type RouteOptionsHasMethodAndPath = RouteOptions & Required<Pick<RouteOptions, 'method' | 'path'>>;
export type RequiredRouteOptions = RouteOptions & Required<Pick<RouteOptions, 'method' | 'path' | 'handler'>>;
export type RoutesList = RequiredRouteOptions | Router | Iterable<RoutesList>;
export type RoutesListHasHandler = RouteOptionsHasHandler | Router | Iterable<RoutesListHasHandler>;
export type RoutesListHasMethod = RouteOptionsHasMethod | Router | Iterable<RoutesListHasMethod>;
export type RoutesListHasPath = RouteOptionsHasPath | Router | string | Iterable<RoutesListHasPath>;
export type RoutesListHasHandlerAndMethod = RouteOptionsHasHandlerAndMethod | Router | Iterable<RoutesListHasHandlerAndMethod>;
export type RoutesListHasHandlerAndPath = RouteOptionsHasHandlerAndPath | Router | Iterable<RoutesListHasHandlerAndPath>;
export type RoutesListHasMethodAndPath = RouteOptionsHasMethodAndPath | Router | Iterable<RoutesListHasMethodAndPath>;
const paramPattern = /\{(\w+)(?:\?|\*(?:[1-9]\d*)?)?\}/u;
const paramsPattern = new RegExp(paramPattern, paramPattern.flags + 'g');
const expandPath = (path: string): Array<string> => {
return Array.from(path.matchAll(paramsPattern) as Iterable<RegExpExecArray>).flatMap((match) => {
const [param, name] = match;
const before = match.input.slice(0, match.index);
const after = match.input.slice(match.index + param.length);
// Optional param, expand to paths WITH and WITHOUT the param
if (param.endsWith('?}')) {
const isWholeSegment = before.endsWith('/') && after.startsWith('/');
const withParam = before + `{${name}}` + after;
const withoutParam = before + (isWholeSegment ? after.slice(1) : after)
return [withParam, withoutParam];
}
return [];
});
};
const isDynamicSegment = (segment: string): boolean => {
return segment.startsWith('{') && segment.endsWith('}') && paramPattern.test(segment);
};
const getParamName = (segment: string): string => {
const param = segment.match(paramPattern);
return param ? param[1] : '';
};
const toPathfinder = (segments: Array<string>): string => {
const replacePart = (str: string) => {
return str && '.';
};
return segments.map(replacePart).join('/');
};
/**
* Returns a human friendly text representation of the given route, such as GET /foo
*/
const toSignature = (route: NormalizedRoute): string => {
return route.method + ' ' + (route.vhost || '') + route.path;
};
const fingerprintPath = (path: string): string => {
return path.replace(paramsPattern, (param) => {
return param.endsWith('*}') ? '#' : '?';
});
};
const toConflictId = (route: NormalizedRoute): string => {
return toSignature({
...route,
path : fingerprintPath(route.path)
});
}
const sortRoutes = (left: NormalizedRoute, right: NormalizedRoute): number => {
const leftFirst = -1;
const rightFirst = 1;
const unchanged = 0;
if (left.segments.filter(isDynamicSegment).length <
right.segments.filter(isDynamicSegment).length) {
return leftFirst;
}
if (left.segments.filter(isDynamicSegment).length >
right.segments.filter(isDynamicSegment).length) {
return rightFirst;
}
if (left.segments.length < right.segments.length) {
return leftFirst;
}
if (left.segments.length > right.segments.length) {
return rightFirst;
}
if (left.path < right.path) {
return leftFirst;
}
if (left.path > right.path) {
return rightFirst;
}
return unchanged;
};
interface RoutingTable {
conflictIds: Map<string, NormalizedRoute>,
list: Array<NormalizedRoute>,
pathfinders: Map<string, Array<NormalizedRoute>>,
wildcards: Array<NormalizedRoute>
}
/**
* A router represents a collection of routes and determines which route will handle a given HTTP request.
* Use `pogo.router()` to create a router instance.
*/
export default class Router {
routes: RoutingTable;
constructor(route?: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler) {
this.routes = {
conflictIds : new Map(),
list : [],
pathfinders : new Map(),
wildcards : []
};
if (route) {
this.add(route, options, handler);
}
}
add(route: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this;
add(route: RoutesListHasMethodAndPath, options: RouteOptionsHasHandler | RouteHandler, handler?: RouteHandler): this;
add(route: RoutesListHasHandlerAndMethod, options: RouteOptionsHasPath, handler?: RouteHandler): this;
add(route: RoutesListHasHandlerAndPath, options: RouteOptionsHasMethod, handler?: RouteHandler): this;
add(route: RoutesListHasHandler, options: RouteOptionsHasMethodAndPath, handler?: RouteHandler): this;
add(route: RoutesListHasPath, options: RouteOptionsHasHandlerAndMethod, handler?: RouteHandler): this;
add(route: RoutesListHasPath, options: RouteOptionsHasMethod, handler: RouteHandler): this;
add(route: RoutesListHasMethod, options: RouteOptionsHasHandlerAndPath, handler?: RouteHandler): this;
add(route: RoutesListHasMethod, options: RouteOptionsHasPath, handler: RouteHandler): this;
add(route: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this {
if (route && typeof route === 'object' && Symbol.iterator in route) {
for (const settings of route as Iterable<RoutesList>) {
this.add(settings, options, handler);
}
return this;
}
if (route instanceof Router) {
this.add(route.routes.list);
return this;
}
const normalizedRoute = {
...(typeof route === 'string' ? { path : route } : route),
...(typeof options === 'function' ? { handler : options } : options),
...(handler ? { handler } : null)
} as Route;
if (typeof normalizedRoute.method === 'object' && Symbol.iterator in normalizedRoute.method) {
for (const method of normalizedRoute.method as Iterable<string>) {
this.add({
...normalizedRoute,
method
});
}
return this;
}
if (typeof normalizedRoute.path === 'object' && Symbol.iterator in normalizedRoute.path) {
for (const path of normalizedRoute.path as Iterable<string>) {
this.add({
...normalizedRoute,
path
});
}
return this;
}
const expandedPaths = expandPath(normalizedRoute.path);
if (expandedPaths.length > 0) {
this.add({
...normalizedRoute,
path : expandedPaths
});
return this;
}
const record: NormalizedRoute = {
...normalizedRoute,
method : normalizedRoute.method.toUpperCase(),
paramNames : Array.from(normalizedRoute.path.matchAll(paramsPattern), (match) => {
return match[1];
}),
segments : normalizedRoute.path.split('/'),
};
const conflictId = toConflictId(record);
const existingRoute = this.routes.conflictIds.get(conflictId);
if (existingRoute) {
const newRoute = toSignature(record);
const oldRoute = toSignature(existingRoute);
throw new Error(`Route conflict: new route "${newRoute}" conflicts with existing route "${oldRoute}"`);
}
this.routes.conflictIds.set(conflictId, record);
const hasWildcardParam = /\{\w+\*\}/u.test(record.path);
if (hasWildcardParam) {
this.routes.wildcards.push(record);
this.routes.wildcards.sort(sortRoutes);
}
else {
const pathfinder = toPathfinder(record.segments);
const pathfinderRoutes = this.routes.pathfinders.get(pathfinder) ?? [];
pathfinderRoutes.push(record);
pathfinderRoutes.sort(sortRoutes);
this.routes.pathfinders.set(pathfinder, pathfinderRoutes);
}
this.routes.list.push(record);
this.routes.list.sort(sortRoutes);
return this;
}
all(route: RoutesListHasHandlerAndPath, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this;
all(route: RoutesListHasHandler, options: RouteOptionsHasPath, handler?: RouteHandler): this;
all(route: RoutesListHasPath, options: RouteOptionsHasHandler | RouteHandler, handler?: RouteHandler): this;
all(route: RoutesListHasPath, options: RouteOptions, handler: RouteHandler): this;
all(route: RoutesListHasMethod, options: RouteOptionsHasHandlerAndPath, handler?: RouteHandler): this;
all(route: RoutesListHasMethod, options: RouteOptionsHasPath, handler: RouteHandler): this;
all(route: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this {
const config = {
...(typeof options === 'function' ? { handler : options } : options),
method : '*'
};
this.add(route, config, handler);
return this;
}
delete(route: RoutesListHasHandlerAndPath, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this;
delete(route: RoutesListHasHandler, options: RouteOptionsHasPath, handler?: RouteHandler): this;
delete(route: RoutesListHasPath, options: RouteOptionsHasHandler | RouteHandler, handler?: RouteHandler): this;
delete(route: RoutesListHasPath, options: RouteOptions, handler: RouteHandler): this;
delete(route: RoutesListHasMethod, options: RouteOptionsHasHandlerAndPath, handler?: RouteHandler): this;
delete(route: RoutesListHasMethod, options: RouteOptionsHasPath, handler: RouteHandler): this;
delete(route: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this {
const config = {
...(typeof options === 'function' ? { handler : options } : options),
method : 'DELETE'
};
this.add(route, config, handler);
return this;
}
get(route: RoutesListHasHandlerAndPath, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this;
get(route: RoutesListHasHandler, options: RouteOptionsHasPath, handler?: RouteHandler): this;
get(route: RoutesListHasPath, options: RouteOptionsHasHandler | RouteHandler, handler?: RouteHandler): this;
get(route: RoutesListHasPath, options: RouteOptions, handler: RouteHandler): this;
get(route: RoutesListHasMethod, options: RouteOptionsHasHandlerAndPath, handler?: RouteHandler): this;
get(route: RoutesListHasMethod, options: RouteOptionsHasPath, handler: RouteHandler): this;
get(route: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this {
const config = {
...(typeof options === 'function' ? { handler : options } : options),
method : 'GET'
};
this.add(route, config, handler);
return this;
}
patch(route: RoutesListHasHandlerAndPath, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this;
patch(route: RoutesListHasHandler, options: RouteOptionsHasPath, handler?: RouteHandler): this;
patch(route: RoutesListHasPath, options: RouteOptionsHasHandler | RouteHandler, handler?: RouteHandler): this;
patch(route: RoutesListHasPath, options: RouteOptions, handler: RouteHandler): this;
patch(route: RoutesListHasMethod, options: RouteOptionsHasHandlerAndPath, handler?: RouteHandler): this;
patch(route: RoutesListHasMethod, options: RouteOptionsHasPath, handler: RouteHandler): this;
patch(route: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this {
const config = {
...(typeof options === 'function' ? { handler : options } : options),
method : 'PATCH'
};
this.add(route, config, handler);
return this;
}
post(route: RoutesListHasHandlerAndPath, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this;
post(route: RoutesListHasHandler, options: RouteOptionsHasPath, handler?: RouteHandler): this;
post(route: RoutesListHasPath, options: RouteOptionsHasHandler | RouteHandler, handler?: RouteHandler): this;
post(route: RoutesListHasPath, options: RouteOptions, handler: RouteHandler): this;
post(route: RoutesListHasMethod, options: RouteOptionsHasHandlerAndPath, handler?: RouteHandler): this;
post(route: RoutesListHasMethod, options: RouteOptionsHasPath, handler: RouteHandler): this;
post(route: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this {
const config = {
...(typeof options === 'function' ? { handler : options } : options),
method : 'POST'
};
this.add(route, config, handler);
return this;
}
put(route: RoutesListHasHandlerAndPath, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this;
put(route: RoutesListHasHandler, options: RouteOptionsHasPath, handler?: RouteHandler): this;
put(route: RoutesListHasPath, options: RouteOptionsHasHandler | RouteHandler, handler?: RouteHandler): this;
put(route: RoutesListHasPath, options: RouteOptions, handler: RouteHandler): this;
put(route: RoutesListHasMethod, options: RouteOptionsHasHandlerAndPath, handler?: RouteHandler): this;
put(route: RoutesListHasMethod, options: RouteOptionsHasPath, handler: RouteHandler): this;
put(route: RoutesList, options?: RouteOptions | RouteHandler, handler?: RouteHandler): this {
const config = {
...(typeof options === 'function' ? { handler : options } : options),
method : 'PUT'
};
this.add(route, config, handler);
return this;
}
lookup(method: string, path: string, host?: string): MatchedRoute | undefined {
const pathSegments = path.split('/');
const pathfinder = toPathfinder(pathSegments);
const matchRoute = (list: Array<NormalizedRoute> = []): NormalizedRoute | undefined => {
return list.find((route: NormalizedRoute) => {
const isMethodMatch = route.method === method || route.method === '*';
if (!isMethodMatch) {
return false;
}
const isHostMatch = !host || !route.vhost || route.vhost === host;
if (!isHostMatch) {
return false;
}
const isStaticPath = route.paramNames.length === 0;
if (isStaticPath) {
return route.path === path;
}
const matchesAllSegments = route.segments.every((routeSegment: string, index: number): boolean => {
return isDynamicSegment(routeSegment) || (routeSegment === pathSegments[index]);
});
const isPathMatch = matchesAllSegments && ((route.segments.length === pathSegments.length) || route.segments[route.segments.length - 1].endsWith('*}'));
return isPathMatch;
});
}
const candidates = this.routes.pathfinders.get(pathfinder);
const wildcardRoutes = this.routes.wildcards;
const route = matchRoute(candidates) || matchRoute(wildcardRoutes);
return route && {
...route,
params : route.segments.reduce((params: RequestParams, routeSegment: string, index: number) => {
if (isDynamicSegment(routeSegment)) {
const name = getParamName(routeSegment);
params[name] = routeSegment.endsWith('*}') ? pathSegments.slice(index).join('/') : pathSegments[index];
}
return params;
}, {})
};
}
}
export {
toSignature
};