-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
routing.js
108 lines (92 loc) · 3.32 KB
/
routing.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
const param_pattern = /^(\.\.\.)?(\w+)(?:=(\w+))?$/;
/** @param {string} id */
export function parse_route_id(id) {
/** @type {string[]} */
const names = [];
/** @type {string[]} */
const types = [];
// `/foo` should get an optional trailing slash, `/foo.json` should not
// const add_trailing_slash = !/\.[a-z]+$/.test(key);
let add_trailing_slash = true;
const pattern =
id === ''
? /^\/$/
: new RegExp(
`^${id
.split(/(?:@[a-zA-Z0-9_-]+)?(?:\/|$)/)
.map((segment, i, segments) => {
const decoded_segment = decodeURIComponent(segment);
// special case — /[...rest]/ could contain zero segments
const match = /^\[\.\.\.(\w+)(?:=(\w+))?\]$/.exec(decoded_segment);
if (match) {
names.push(match[1]);
types.push(match[2]);
return '(?:/(.*))?';
}
const is_last = i === segments.length - 1;
return (
decoded_segment &&
'/' +
decoded_segment
.split(/\[(.+?)\]/)
.map((content, i) => {
if (i % 2) {
const match = param_pattern.exec(content);
if (!match) {
throw new Error(
`Invalid param: ${content}. Params and matcher names can only have underscores and alphanumeric characters.`
);
}
const [, rest, name, type] = match;
names.push(name);
types.push(type);
return rest ? '(.*?)' : '([^/]+?)';
}
if (is_last && content.includes('.')) add_trailing_slash = false;
return (
content // allow users to specify characters on the file system in an encoded manner
.normalize()
// We use [ and ] to denote parameters, so users must encode these on the file
// system to match against them. We don't decode all characters since others
// can already be epressed and so that '%' can be easily used directly in filenames
.replace(/%5[Bb]/g, '[')
.replace(/%5[Dd]/g, ']')
// '#', '/', and '?' can only appear in URL path segments in an encoded manner.
// They will not be touched by decodeURI so need to be encoded here, so
// that we can match against them.
// We skip '/' since you can't create a file with it on any OS
.replace(/#/g, '%23')
.replace(/\?/g, '%3F')
// escape characters that have special meaning in regex
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
); // TODO handle encoding
})
.join('')
);
})
.join('')}${add_trailing_slash ? '/?' : ''}$`
);
return { pattern, names, types };
}
/**
* @param {RegExpMatchArray} match
* @param {string[]} names
* @param {string[]} types
* @param {Record<string, import('types').ParamMatcher>} matchers
*/
export function exec(match, names, types, matchers) {
/** @type {Record<string, string>} */
const params = {};
for (let i = 0; i < names.length; i += 1) {
const name = names[i];
const type = types[i];
const value = match[i + 1] || '';
if (type) {
const matcher = matchers[type];
if (!matcher) throw new Error(`Missing "${type}" param matcher`); // TODO do this ahead of time?
if (!matcher(value)) return;
}
params[name] = value;
}
return params;
}