forked from antitoxic/node-reversable-router
-
Notifications
You must be signed in to change notification settings - Fork 12
/
route.js
327 lines (290 loc) · 10.3 KB
/
route.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
var XRegExp = require('xregexp');
exports = module.exports = Route;
function Route(path, options) {
this.options = {
recursiveWildcard:true,
caseSensitive: false,
wildcardInPairs: false
}
this.setOptions(options);
this.path = path;
this.regex = null;
}
/**
* Set route options
* @param options
*/
Route.prototype.setOptions = function (options) {
for (var prop in options) {
this.options[prop] = options[prop];
}
}
/**
* Creates a regular expression from the provided `path` to match the URL against
* @return XRegExp
* @todo add parameter-specific regexes like :id => [\d]+, clean the regex items like `\.` so `\` => `\\`
* @todo if a Regex object passed => extract pattern
*/
Route.prototype.compile = function () {
var path = this.path;
// ensure '*' is interpreted correctly
if(path === '*') path = '/*';
// force all inner `masked` parameters to be required
path = path.replace(/\/\*\*/g, '/*');
// store the cleaned path to be used for URL generation
this.cleanedPath = path;
// make bracketed groups in url path a optional regex group
path = path.replace(/\)/g, ')?');
//the last `masked` parameter is optional
if (path.slice(-2) == '/*') {
path = path.slice(0, -2) + "/**";
}
//escape full stops
path = path.replace(/\./g, '\\.');
// change wildcards , the `*` (calling them `masked` parameters) to masked0, masked1...
var maskedWildcardName = '';
var hasMaskedWildcard = false;
var masked = [];
var i = 0;
path = XRegExp.replace(path, XRegExp('/\\*\\*?', 'g'), function (match) {
var splatName = 'splat' + i;
i++;
masked.push(splatName);
// last `splat` is optional and allows having `/` inside of it
if (match == '/**') {
hasMaskedWildcard = true;
maskedWildcardName = splatName;
return '(/(?<' + splatName + '>.*))?';
}
// any other `masked` parameters are required and don't allow `/` inside of them
return '/(?<' + splatName + '>[^/]+)';
});
//the last `masked` parameter is optional
if (path.slice(-1) == '/') {
path = path.slice(0, -1);
}
this.hasMaskedWildcard = hasMaskedWildcard;
this.masked = masked;
this.maskedWildcardName = maskedWildcardName;
// change & count named parameters ( `:params`)
var params = [];
path = XRegExp.replace(path, XRegExp(':[\\p{L}0-9_]+', 'g'), function (match) {
match = match.replace(':', '');
if (params.indexOf(match) > -1) return ':' + match;
params.push(match);
return '(?<' + match + '>[^/]+)';
});
this.params = params;
// Check if there is optional parts in the path
var optionalParams = [];
var bracketGroups = [];
var self = this;
// Performance: only check if there's a bracket character in the path
if (self.cleanedPath.indexOf('(') > -1) {
var unclosed = [];
for (var i = 0; i <= self.cleanedPath.length - 1; i++) {
var currentChar = self.cleanedPath[i];
if (currentChar == '(') {
bracketGroups.push({
start: i,
end: null,
params:[]
});
unclosed.push(bracketGroups.length - 1);
continue;
}
if (currentChar == ')') {
bracketGroups[unclosed.pop()].end = i;
}
}
// Order by opening bracket position, descending
bracketGroups.sort(function (a, b) {
return a.start > b.start ? -1 : 1;
})
// Check for parameters in the optional parts, i.e. optional parameters
params.forEach(function (paramName) {
var index = self.cleanedPath.indexOf(':' + paramName);
bracketGroups.every(function (group) {
if (group.start < index && group.end > index && optionalParams.indexOf(paramName) == -1) {
optionalParams.push(paramName);
group.params.push(paramName);
return false;
}
return true;
})
})
}
this.optionalParams = optionalParams;
this.optionalParts = bracketGroups;
// Store compiled regex
this.regex = XRegExp('^' + path + '$', this.options.caseSensitive ? 'i' : undefined);
return this.regex;
}
/**
* Check if URL matches the route and if it does extract parameters
* @param url
* @return {*}
*/
Route.prototype.match = function (url) {
//make sure the last `/` is trimmed off the url, otherwise last splash can falsely be empty string, i.e. ''
if (url.slice(-1) == '/') {
url = url.slice(0, -1);
}
var path = this.path;
// Matches url?
// Performance: If there isn't any regex characters `*:()` then simply check for equality
if (path.indexOf('*') == -1 && path.indexOf(':') == -1 && path.indexOf('(') == -1 && path.indexOf(')') == -1 && path == url) {
return true;
}
var regex = this.regex ? this.regex : this.compile();
var matches = XRegExp.exec(url, regex);
if (matches == null) return false;
// Initialise the return value (found parameters)
var self = this;
var RequestParams = {_masked:[]};
// All named parameters
self.params.forEach(function (param) {
if (matches[param] == undefined) return;
RequestParams[param] = matches[param];
});
// All masked/unnamed parameters
var masked = self.masked;
masked.every(function (maskedName) {
if (matches[maskedName] == undefined) return true;
if (maskedName != self.maskedWildcardName) {
RequestParams._masked.push(matches[maskedName]);
return true;
}
// there is a anything-goes wildcard in the end of the url path, handle it:
if (self.options.recursiveWildcard) {
var maskedWildcardData = matches[maskedName].split('/');
// If we have `.../id/23` then:
// 1) parse is as 0=>id, 1=>23
if (!self.options.wildcardInPairs) {
RequestParams._masked = RequestParams._masked.concat(maskedWildcardData);
return false;
}
// 2) parse is as id => 23
var i = 0;
while (maskedWildcardData[i + 1] != undefined) {
if (i % 2 != 0) {
i++;
continue;
}
RequestParams[maskedWildcardData[i]] = maskedWildcardData[i + 1];
i++;
}
} else {
RequestParams._masked.push(matches[maskedName]);
}
return false;
});
return RequestParams;
}
/**
* Builds a URL using provided parameters
*
* @param {array} userParams The parameters, example: {id:4, _masked:[2, 1, 2]}
* @return {String}
*/
Route.prototype.generate = function (userParams) {
// Initialise return value
var self = this;
if (!this.regex) this.compile();
var url = this.cleanedPath;
var foundParameters = [];
var knownNamedParameters = this.params;
userParams = userParams == undefined ? {_masked:[]} : userParams;
userParams._masked = userParams._masked == undefined ? [] : userParams._masked;
// Check what parameters were provided by the user
knownNamedParameters.forEach(function (name) {
if (userParams[name] === undefined) return;
foundParameters.push(name);
})
// Has the user provided all parameters?
if (foundParameters.length != knownNamedParameters.length) {
// Don't match this route if there can't be any optional parameters
if (this.optionalParams.length == 0) throw new Error('not all url parameters are provided');
// Remove optional parts with missing parameters inside
url = this.removePartsWithMissingParams(url, userParams);
}
if (this.optionalParams.length) {
url = url.replace(/[\(\)]/g, '');
}
// Replace named parameters
foundParameters.forEach(function (name) {
var parameter = userParams[name];
if(parameter !== null){
// ignore everything beyond '#'
parameter = parameter.toString().split("#")[0];
url = url.replace(':' + name, parameter);
} else {
url = url.replace(new RegExp(':' + name + '/|/:' + name), '');
};
})
// Replace masked/unnamed parameters, account for masked parameters that could have been removed in the optional parts
var maskCount = url.match(/\*/g);
var requiredMaskedNum = maskCount == null ? 0 : maskCount.length;
requiredMaskedNum = this.hasMaskedWildcard ? requiredMaskedNum - 1 : requiredMaskedNum;
var usedMasked = 0;
// Handle masked parameters
userParams._masked.forEach(function (value, i) {
// fill in required masked parameters
if (usedMasked < requiredMaskedNum) {
url = url.replace('*', value);
usedMasked++;
return true;
}
if (url.indexOf('*') == -1) throw new Error('too much `_masked` values provided. if you think this is not the problem check if you forgot to pass a named parameter you wanted');
// if wildcard parameters are enabled and are not parsed in pairs join the remaining of userParams._masked
if (self.hasMaskedWildcard && !self.options.wildcardInPairs) {
url = url.replace('*', userParams._masked.splice(i).join('/'));
}
return false;
});
if (userParams._masked.length < requiredMaskedNum) throw new Error('not enough `_masked` values provided');
delete userParams._masked;
// Handle optional masked parameters
if (this.hasMaskedWildcard && url.indexOf('*') > -1) {
// Make sure we replace the last `*` with something (even if it is an empty string)
var lastMaskReplacement = '';
// Handle masked wildcard parameters parsed in pairs
if (this.options.wildcardInPairs) {
for (var name in userParams) {
var value = userParams[name];
if (foundParameters.indexOf(name) > -1) continue;
lastMaskReplacement += name + '/' + value + '/';
}
}
url = url.replace('*', lastMaskReplacement);
}
if (url === "") return "/";
if (url.length > 1 && url[url.length - 1] === "/") return url.slice(0,-1);
return url;
}
Route.prototype.removePartsWithMissingParams = function (url, userParams) {
var groupsToRemove = [];
this.optionalParts.forEach(function (part) {
part.params.every(function (optionalParam) {
if (userParams[optionalParam] == undefined) {
groupsToRemove.push(part);
return false;
}
return true;
});
});
var subtractPairs = [];
groupsToRemove.forEach(function (group) {
var from = group.start, to = group.end, accountedForRemoval = false;
subtractPairs.forEach(function (subtract, i) {
if (from < subtract.start && to > subtract.end) {
to = to - (subtract.end + 1 - subtract.start);
subtractPairs[i] = {start:from, end:to};
accountedForRemoval = true;
}
});
url = url.substr(0, from) + url.substr(to + 1);
if (!accountedForRemoval) subtractPairs.push({start:group.start, end:group.end});
});
return url;
}