-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
aurelia-route-recognizer.js
608 lines (509 loc) · 16.8 KB
/
aurelia-route-recognizer.js
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
import {buildQueryString,parseQueryString} from 'aurelia-path';
// A State has a character specification and (`charSpec`) and a list of possible
// subsequent states (`nextStates`).
//
// If a State is an accepting state, it will also have several additional
// properties:
//
// * `regex`: A regular expression that is used to extract parameters from paths
// that reached this accepting state.
// * `handlers`: Information on how to convert the list of captures into calls
// to registered handlers with the specified parameters.
// * `types`: How many static, dynamic, or star segments in this route. Used to
// decide which route to use if multiple registered routes match a path.
//
// Currently, State is implemented naively by looping over `nextStates` and
// comparing a character specification against a character. A more efficient
// implementation would use a hash of keys pointing at one or more next states.
export class State {
constructor(charSpec: CharSpec) {
this.charSpec = charSpec;
this.nextStates = [];
}
get(charSpec: CharSpec): State {
for (let child of this.nextStates) {
let isEqual = child.charSpec.validChars === charSpec.validChars
&& child.charSpec.invalidChars === charSpec.invalidChars;
if (isEqual) {
return child;
}
}
return undefined;
}
put(charSpec: CharSpec): State {
let state = this.get(charSpec);
// If the character specification already exists in a child of the current
// state, just return that state.
if (state) {
return state;
}
// Make a new state for the character spec
state = new State(charSpec);
// Insert the new state as a child of the current state
this.nextStates.push(state);
// If this character specification repeats, insert the new state as a child
// of itself. Note that this will not trigger an infinite loop because each
// transition during recognition consumes a character.
if (charSpec.repeat) {
state.nextStates.push(state);
}
// Return the new state
return state;
}
// Find a list of child states matching the next character
match(ch: string): State[] {
let nextStates = this.nextStates;
let results = [];
for (let i = 0, l = nextStates.length; i < l; i++) {
let child = nextStates[i];
let charSpec = child.charSpec;
if (charSpec.validChars !== undefined) {
if (charSpec.validChars.indexOf(ch) !== -1) {
results.push(child);
}
} else if (charSpec.invalidChars !== undefined) {
if (charSpec.invalidChars.indexOf(ch) === -1) {
results.push(child);
}
}
}
return results;
}
}
const specials = [
'/', '.', '*', '+', '?', '|',
'(', ')', '[', ']', '{', '}', '\\'
];
const escapeRegex = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
// A Segment represents a segment in the original route description.
// Each Segment type provides an `eachChar` and `regex` method.
//
// The `eachChar` method invokes the callback with one or more character
// specifications. A character specification consumes one or more input
// characters.
//
// The `regex` method returns a regex fragment for the segment. If the
// segment is a dynamic or star segment, the regex fragment also includes
// a capture.
//
// A character specification contains:
//
// * `validChars`: a String with a list of all valid characters, or
// * `invalidChars`: a String with a list of all invalid characters
// * `repeat`: true if the character specification can repeat
export class StaticSegment {
constructor(string: string, caseSensitive: boolean) {
this.string = string;
this.caseSensitive = caseSensitive;
}
eachChar(callback: (spec: CharSpec) => void): void {
let s = this.string;
for (let i = 0, ii = s.length; i < ii; ++i) {
let ch = s[i];
callback({ validChars: this.caseSensitive ? ch : ch.toUpperCase() + ch.toLowerCase() });
}
}
regex(): string {
return this.string.replace(escapeRegex, '\\$1');
}
generate(): string {
return this.string;
}
}
export class DynamicSegment {
constructor(name: string, optional: boolean) {
this.name = name;
this.optional = optional;
}
eachChar(callback: (spec: CharSpec) => void): void {
callback({ invalidChars: '/', repeat: true });
}
regex(): string {
return '([^/]+)';
}
generate(params: Object, consumed: Object): string {
consumed[this.name] = true;
return params[this.name];
}
}
export class StarSegment {
constructor(name: string) {
this.name = name;
}
eachChar(callback: (spec: CharSpec) => void): void {
callback({ invalidChars: '', repeat: true });
}
regex(): string {
return '(.+)';
}
generate(params: Object, consumed: Object): string {
consumed[this.name] = true;
return params[this.name];
}
}
export class EpsilonSegment {
eachChar(): void {
}
regex(): string {
return '';
}
generate(): string {
return '';
}
}
/*
* An object that is indexed and used for route generation, particularly for dynamic routes.
*/
interface RouteGenerator {
segments: Array<StaticSegment | DynamicSegment | StarSegment | EpsilonSegment>;
handlers: HandlerEntry[];
}
interface RouteHandler {
name: string;
}
interface ConfigurableRoute {
path: string;
handler: RouteHandler;
caseSensitive: boolean;
}
interface HandlerEntry {
handler: RouteHandler;
names: string[];
}
interface RecognizedRoute {
handler: RouteHandler;
params: Object;
isDynamic: boolean;
}
interface CharSpec {
invalidChars?: string;
validChars?: string;
repeat?: boolean;
}
/**
* Class that parses route patterns and matches path strings.
*
* @class RouteRecognizer
* @constructor
*/
export class RouteRecognizer {
constructor() {
this.rootState = new State();
this.names = {};
this.routes = new Map();
}
/**
* Parse a route pattern and add it to the collection of recognized routes.
*
* @param route The route to add.
*/
add(route: ConfigurableRoute|ConfigurableRoute[]): State {
if (Array.isArray(route)) {
route.forEach(r => this.add(r));
return undefined;
}
let currentState = this.rootState;
let skippableStates = [];
let regex = '^';
let types = { statics: 0, dynamics: 0, stars: 0 };
let names = [];
let routeName = route.handler.name;
let isEmpty = true;
let segments = parse(route.path, names, types, route.caseSensitive);
for (let i = 0, ii = segments.length; i < ii; i++) {
let segment = segments[i];
if (segment instanceof EpsilonSegment) {
continue;
}
// Add a representation of the segment to the NFA and regex
let [firstState, nextState] = addSegment(currentState, segment);
// add the first part of the next segment to the end of any skipped states
for (let j = 0, jj = skippableStates.length; j < jj; j++) {
skippableStates[j].nextStates.push(firstState);
}
// If the segment was optional we don't fast forward to the end of the
// segment, but we do hold on to a reference to the end of the segment
// for adding future segments. Multiple consecutive optional segments
// will accumulate.
if (segment.optional) {
skippableStates.push(nextState);
regex += `(?:/${segment.regex()})?`;
// Otherwise, we fast forward to the end of the segment and remove any
// references to skipped segments since we don't need them anymore.
} else {
currentState = nextState;
regex += `/${segment.regex()}`;
skippableStates.length = 0;
isEmpty = false;
}
}
// An "all optional" path is technically empty since currentState is this.rootState
if (isEmpty) {
currentState = currentState.put({ validChars: '/' });
regex += '/?';
}
let handlers = [{ handler: route.handler, names: names }];
this.routes.set(route.handler, { segments, handlers });
if (routeName) {
let routeNames = Array.isArray(routeName) ? routeName : [routeName];
for (let i = 0; i < routeNames.length; i++) {
if (!(routeNames[i] in this.names)) {
this.names[routeNames[i]] = { segments, handlers };
}
}
}
// Any trailing skipped states need to be endpoints and need to have
// handlers attached.
for (let i = 0; i < skippableStates.length; i++) {
let state = skippableStates[i];
state.handlers = handlers;
state.regex = new RegExp(regex + '$', route.caseSensitive ? '' : 'i');
state.types = types;
}
currentState.handlers = handlers;
currentState.regex = new RegExp(regex + '$', route.caseSensitive ? '' : 'i');
currentState.types = types;
return currentState;
}
/**
* Retrieve a RouteGenerator for a route by name or RouteConfig (RouteHandler).
*
* @param nameOrRoute The name of the route or RouteConfig object.
* @returns The RouteGenerator for that route.
*/
getRoute(nameOrRoute: string | RouteHandler): RouteGenerator {
return typeof(nameOrRoute) === 'string' ? this.names[nameOrRoute] : this.routes.get(nameOrRoute);
}
/**
* Retrieve the handlers registered for the route by name or RouteConfig (RouteHandler).
*
* @param nameOrRoute The name of the route or RouteConfig object.
* @returns The handlers.
*/
handlersFor(nameOrRoute: string | RouteHandler): HandlerEntry[] {
let route = this.getRoute(nameOrRoute);
if (!route) {
throw new Error(`There is no route named ${nameOrRoute}`);
}
return [...route.handlers];
}
/**
* Check if this RouteRecognizer recognizes a route by name or RouteConfig (RouteHandler).
*
* @param nameOrRoute The name of the route or RouteConfig object.
* @returns True if the named route is recognized.
*/
hasRoute(nameOrRoute: string | RouteHandler): boolean {
return !!this.getRoute(nameOrRoute);
}
/**
* Generate a path and query string from a route name or RouteConfig (RouteHandler) and params object.
*
* @param nameOrRoute The name of the route or RouteConfig object.
* @param params The route params to use when populating the pattern.
* Properties not required by the pattern will be appended to the query string.
* @returns The generated absolute path and query string.
*/
generate(nameOrRoute: string | RouteHandler, params: object): string {
let route = this.getRoute(nameOrRoute);
if (!route) {
throw new Error(`There is no route named ${nameOrRoute}`);
}
let handler = route.handlers[0].handler;
if (handler.generationUsesHref) {
return handler.href;
}
let routeParams = Object.assign({}, params);
let segments = route.segments;
let consumed = {};
let output = '';
for (let i = 0, l = segments.length; i < l; i++) {
let segment = segments[i];
if (segment instanceof EpsilonSegment) {
continue;
}
let segmentValue = segment.generate(routeParams, consumed);
if (segmentValue === null || segmentValue === undefined) {
if (!segment.optional) {
throw new Error(`A value is required for route parameter '${segment.name}' in route '${nameOrRoute}'.`);
}
} else {
output += '/';
output += segmentValue;
}
}
if (output.charAt(0) !== '/') {
output = '/' + output;
}
// remove params used in the path and add the rest to the querystring
for (let param in consumed) {
delete routeParams[param];
}
let queryString = buildQueryString(routeParams);
output += queryString ? `?${queryString}` : '';
return output;
}
/**
* Match a path string against registered route patterns.
*
* @param path The path to attempt to match.
* @returns Array of objects containing `handler`, `params`, and
* `isDynanic` values for the matched route(s), or undefined if no match
* was found.
*/
recognize(path: string): RecognizedRoute[] | void {
let states = [this.rootState];
let queryParams = {};
let isSlashDropped = false;
let normalizedPath = path;
let queryStart = normalizedPath.indexOf('?');
if (queryStart !== -1) {
let queryString = normalizedPath.substr(queryStart + 1, normalizedPath.length);
normalizedPath = normalizedPath.substr(0, queryStart);
queryParams = parseQueryString(queryString);
}
normalizedPath = decodeURI(normalizedPath);
if (normalizedPath.charAt(0) !== '/') {
normalizedPath = '/' + normalizedPath;
}
let pathLen = normalizedPath.length;
if (pathLen > 1 && normalizedPath.charAt(pathLen - 1) === '/') {
normalizedPath = normalizedPath.substr(0, pathLen - 1);
isSlashDropped = true;
}
for (let i = 0, l = normalizedPath.length; i < l; i++) {
states = recognizeChar(states, normalizedPath.charAt(i));
if (!states.length) {
break;
}
}
let solutions = [];
for (let i = 0, l = states.length; i < l; i++) {
if (states[i].handlers) {
solutions.push(states[i]);
}
}
states = sortSolutions(solutions);
let state = solutions[0];
if (state && state.handlers) {
// if a trailing slash was dropped and a star segment is the last segment
// specified, put the trailing slash back
if (isSlashDropped && state.regex.source.slice(-5) === '(.+)$') {
normalizedPath = normalizedPath + '/';
}
return findHandler(state, normalizedPath, queryParams);
}
}
}
class RecognizeResults {
constructor(queryParams: Object) {
this.splice = Array.prototype.splice;
this.slice = Array.prototype.slice;
this.push = Array.prototype.push;
this.length = 0;
this.queryParams = queryParams || {};
}
}
function parse(route, names, types, caseSensitive) {
// normalize route as not starting with a '/'. Recognition will
// also normalize.
let normalizedRoute = route;
if (route.charAt(0) === '/') {
normalizedRoute = route.substr(1);
}
let results = [];
let splitRoute = normalizedRoute.split('/');
for (let i = 0, ii = splitRoute.length; i < ii; ++i) {
let segment = splitRoute[i];
// Try to parse a parameter :param?
let match = segment.match(/^:([^?]+)(\?)?$/);
if (match) {
let [, name, optional] = match;
if (name.indexOf('=') !== -1) {
throw new Error(`Parameter ${name} in route ${route} has a default value, which is not supported.`);
}
results.push(new DynamicSegment(name, !!optional));
names.push(name);
types.dynamics++;
continue;
}
// Try to parse a star segment *whatever
match = segment.match(/^\*(.+)$/);
if (match) {
results.push(new StarSegment(match[1]));
names.push(match[1]);
types.stars++;
} else if (segment === '') {
results.push(new EpsilonSegment());
} else {
results.push(new StaticSegment(segment, caseSensitive));
types.statics++;
}
}
return results;
}
// This is a somewhat naive strategy, but should work in a lot of cases
// A better strategy would properly resolve /posts/:id/new and /posts/edit/:id.
//
// This strategy generally prefers more static and less dynamic matching.
// Specifically, it
//
// * prefers fewer stars to more, then
// * prefers using stars for less of the match to more, then
// * prefers fewer dynamic segments to more, then
// * prefers more static segments to more
function sortSolutions(states) {
return states.sort((a, b) => {
if (a.types.stars !== b.types.stars) {
return a.types.stars - b.types.stars;
}
if (a.types.stars) {
if (a.types.statics !== b.types.statics) {
return b.types.statics - a.types.statics;
}
if (a.types.dynamics !== b.types.dynamics) {
return b.types.dynamics - a.types.dynamics;
}
}
if (a.types.dynamics !== b.types.dynamics) {
return a.types.dynamics - b.types.dynamics;
}
if (a.types.statics !== b.types.statics) {
return b.types.statics - a.types.statics;
}
return 0;
});
}
function recognizeChar(states, ch) {
let nextStates = [];
for (let i = 0, l = states.length; i < l; i++) {
let state = states[i];
nextStates.push(...state.match(ch));
}
return nextStates;
}
function findHandler(state, path, queryParams) {
let handlers = state.handlers;
let regex = state.regex;
let captures = path.match(regex);
let currentCapture = 1;
let result = new RecognizeResults(queryParams);
for (let i = 0, l = handlers.length; i < l; i++) {
let handler = handlers[i];
let names = handler.names;
let params = {};
for (let j = 0, m = names.length; j < m; j++) {
params[names[j]] = captures[currentCapture++];
}
result.push({ handler: handler.handler, params: params, isDynamic: !!names.length });
}
return result;
}
function addSegment(currentState, segment) {
let firstState = currentState.put({ validChars: '/' });
let nextState = firstState;
segment.eachChar(ch => {
nextState = nextState.put(ch);
});
return [firstState, nextState];
}