-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqroute.js
174 lines (153 loc) · 5.56 KB
/
qroute.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
/**
* Quick REST route mapping and lookup
*
* Copyright (C) 2015,2017 Andras Radics
* Licensed under the Apache License, Version 2.0
*
* 2015-01-08 - AR.
*/
'use strict';
module.exports = QRoute;
function QRoute( ) {
this._literals = {};
this._patterns = new Array();
}
/**
* register the handlers associated with the route routeName
*/
QRoute.prototype.addRoute = function addRoute( routeName, handlers ) {
if (typeof routeName !== 'string') {
// reinsert removed route
var route = routeName.type === 'mappedRoute' && routeName._route || routeName;
if (route.type === 'lit') this._literals[info.name] = route;
else this._patterns.push(route);
}
else if (routeName.indexOf("/:") < 0) {
var info = {type: 'lit', name: routeName, method: null, handlers: handlers, steps: 0, stack: null};
this._literals[routeName] = info;
return info;
}
else {
var match = this._buildCapturingRegex(routeName);
var info = {type: 'patt', name: routeName, method: null, handlers: handlers, patt: match.patt, names: match.names, steps: 0, stack: null};
this._patterns.push(info);
return info;
}
};
/**
* remove a route
*/
QRoute.prototype.removeRoute = function removeRoute( info ) {
if (info.type === 'mappedRoute') info = info._route;
// TODO: maybe also match by routeName, so can remove /path/name like add /path/name
if (info.type === 'lit') {
// TODO: maybe support multiple routes for the same path?
delete this._literals[info.name];
}
else {
var idx = this._patterns.indexOf(info);
if (idx >= 0) this._patterns.splice(idx, 1);
}
}
/**
* clear all middleware stacks cached in the routes
*/
QRoute.prototype.clearMwStacks = function clearMwStacks( ) {
var i;
for (i in this._literals) this._literals[i].stack = null;
for (i=0; i<this._literals.length; i++) this._patterns[i].stack = null;
};
/**
* look up the added route matching querypath
*/
QRoute.prototype.mapRoute = function mapRoute( querypath ) {
var route;
// nodejs http req.url could contain ?a=1 but #tag is striped
var tail, qmark = querypath.indexOf('?');
if (qmark >= 0) { tail = querypath.slice(qmark+1); querypath = querypath.slice(0, qmark); }
if ((route = this._literals[querypath])) {
return {
type: 'mappedRoute',
path: querypath,
name: route.name,
tail: tail || "",
vars: {},
_route: route,
};
}
var patterns = this._patterns;
for (var i=0; i<patterns.length; i++) {
var j, match, vars = {};
if ((match = querypath.match(patterns[i].patt))) {
var patt = patterns[i];
var names = patt.names;
for (j=0; j<names.length; j++) vars[names[j]] = match[j+1];
return {
type: 'mappedRoute',
path: querypath,
name: patt.name,
tail: match[j+1],
vars: vars,
_route: patt,
};
}
}
return undefined;
};
/**
* build a regex to match the routeName and extract any /:param parameters
*/
QRoute.prototype._buildCapturingRegex = function _buildCapturingRegex( routeName ) {
var match, names = new Array();
var pattern = "^";
while ((match = routeName.match(/\/:[^/]*/))) {
if (match.index > 0) pattern += this._regexEscape(routeName.slice(0, match.index));
pattern += '\\/([^/]*)';
names.push(match[0].slice(2));
routeName = routeName.slice(match.index + match[0].length);
}
pattern += this._regexEscape(routeName);
// the route matches if the query string ends here or continues only past / or ?
pattern += "([/?].*)?$";
return {patt: new RegExp(pattern), names: names};
};
/**
* backslash-escape the chars that have special meaning in regex strings
*/
QRoute.prototype._regexEscape = function _regexEscape( str ) {
// For PCRE or POSIX, the regex metacharacters are:
// . [ ( - terms
// * + ? { - repetition specifiers
// | - alternation
// \ - escape char
// ^ $ - anchors
// ) - close paren (else invalid node regex)
// Matching close chars ] } are not special without the open char.
// / is not special in a regex, it matches a literal /.
// : and = are not special outside of [] ranges or (?) conditionals.
// ) has to be escaped always, else results in "invalid regex"
return str.replace(/([.[(*+?{|\\^$=)])/g, '\\$1');
};
QRoute.prototype = toStruct(QRoute.prototype);
function toStruct( x ) {
return toStruct.prototype = x;
}
// quickest:
/**
var timeit = require('qtimeit');
var f = new QRoute();
f.addRoute('GET::/foo/bar', 1);
f.addRoute('POST::/:kid/b]ar/:collection/:op', 2);
console.log(f.mapRoute('POST::/kid/b]ar/collection/op/zed?a=1'));
//timeit(100000, function(){ f.mapRoute('POST::/foo/bar') });
timeit(100000, function(){ f.mapRoute('GET::/foo/bar') });
// 3m/s (2.6m/s node-v0.11.13)
// (but only 1.4m/s if mapped w/ regex... => regex param capturing is free)
// 19.5m/s without the routeName string concat !!
timeit(100000, function(){ f.mapRoute('POST::/kid/bar/collection/op') });
// 1.47m/s (single regex) (1.53m/s node-v0.11.13)
// ...ie, 50 routes is at most 27k requests mapped / sec (so mapped routes *halve* the service rate)
// 4m/s without the routeName string concat !!
// to avoid concat: have caller create an array of QRoute mappers, one per GET,POST etc method
//console.log(f);
/**/