This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
276 lines (221 loc) · 6.37 KB
/
index.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
;(function(exports) {
"use strict";
var url = require("url");
function Router(options) {
this._options = {};
this._routes = {
get: {
absolute: {},
regex: {},
relative: {},
},
post: {
absolute: {},
regex: {},
relative: {},
},
put: {
absolute: {},
regex: {},
relative: {},
},
delete: {
absolute: {},
regex: {},
relative: {},
},
options: {
absolute: {},
regex: {},
relative: {},
},
connect: {
absolute: {},
regex: {},
relative: {},
},
trace: {
absolute: {},
regex: {},
relative: {},
},
// HTTP HEAD omitted.
// One possible HEAD implementation would be to
// monkey patch anything that would end the
// request (request.end, "end" event, etc.)
// with NOPs, run the corresponding get handler
// for the route, strip the message body, and
// then return only the headers.
};
this._subRouters = {};
}
Router.prototype._addRoute = function addRoute(method, route, fn) {
if (route instanceof RegExp) {
this._routes[method].regex[route.source] = fn;
return this;
// If the route has no ":variable" path segments.
} else if (route.indexOf(':') == -1) {
this._routes[method].absolute[route] = fn;
return this;
}
// We start at the root of the route tree, and walk it for each
// path segment.
var pathTree = this._routes[method].relative;
route.split('/').forEach(function(pathSegment) {
// Skip empty path segments; if there was a prefixing
// forward-slash in the route, this is also skipped.
if (pathSegment.length === 0) {
return;
}
// If the path segment contains a ':', it is a
// ":variable" path segment.
if (pathSegment.indexOf(':') !== -1) {
pathSegment = ":variable";
}
if (!pathTree[pathSegment]) {
// Since we're creating a tree, some of the
// paths we're walking might exist already. If
// they don't yet, initialize a new 'leaf'.
pathTree[pathSegment] = {};
}
// Walk one branch forward.
pathTree = pathTree[pathSegment];
});
// Attach the function to this current leaf of the route tree.
pathTree.fn = fn;
return this;
};
Router.prototype._addSubRouter = function addSubRouter(route, router) {
this._subRouters[route] = router;
return this;
};
Router.prototype.connect = function connect(route, handler) {
this._addRoute("connect", route, handler);
return this;
};
Router.prototype.delete = function put(route, handler) {
this._addRoute("delete", route, handler);
return this;
};
Router.prototype.get = function get(route, handler) {
this._addRoute("get", route, handler);
return this;
};
Router.prototype.options = function put(route, handler) {
this._addRoute("options", route, handler);
return this;
};
Router.prototype.post = function post(route, handler) {
this._addRoute("post", route, handler);
return this;
};
Router.prototype.put = function put(route, handler) {
this._addRoute("put", route, handler);
return this;
};
Router.prototype.trace = function trace(route, handler) {
this._addRoute("trace", route, handler);
return this;
};
Router.prototype.addMethod = function addMethod(method) {
this._routes[method] = {
absolute: {},
regex: {},
relative: {},
};
this[method] = function method(route, handler) {
this._addRoute(method, route, handler);
return this;
};
return this;
};
Router.prototype.handle404 = function handle404(_, response) {
var notFound = "Not Found.";
response.writeHead(404, {
"Content-Length": notFound.length,
"Content-Type": "text/plain",
});
response.end(notFound);
return this;
};
Router.prototype.handle400 = function handle400(_, response) {
var badRequest = "Bad Request.";
response.writeHead(400, {
"Content-Length": badRequest.length,
"Content-Type": "text/plain",
});
response.end(badRequest);
return this;
};
Router.prototype.dispatch = function dispatch(request, response) {
var requestUrl = url.parse(request.url, true);
var method = request.method.toLowerCase();
if (!method in this._routes) {
this.handle400(request, response);
return this;
}
var absoluteMatch = this._routes[method]
.absolute[requestUrl.pathname];
if (absoluteMatch) {
absoluteMatch(request, response, requestUrl.query);
return this;
}
var regexMatch = null;
// Regexes matches are searched for in order of insertion: that
// is, a regex route inserted first will be searched before a
// regex route that was inserted second. Upon first regex route
// match, searching stops.
for (var regexp in this._routes[method].regex) {
regexMatch = new RegExp(regexp)
.exec(requestUrl.pathname);
if (regexMatch) {
var args = [request, response].concat(regexMatch.slice(1), requestUrl.query);
this._routes[method].regex[regexp]
.apply({}, args);
return this;
}
}
var requestUrlTree = requestUrl.pathname.split('/');
var pathTree = this._routes[method].relative;
var pathSegment = null;
var argList = [];
var relativeMatch = true;
// Walk the pathTree, using the path segments from the
// requestUrlTree. If every segment down to the leaf of the
// requestUrlTree matches every segment at the same level in
// the pathTree, there's a route match.
for (var i = 0, len = requestUrlTree.length; i < len; i++) {
pathSegment = requestUrlTree[i];
// Skips any duplicate '/'s that were in the url.
if (pathSegment.length === 0) {
continue;
}
// If there is a ":variable" in the current level of
// the pathTree, capture the corresponding path
// segment, add it to the argList, and drill deeper.
if (pathTree[":variable"]) {
argList.push(pathSegment);
pathTree = pathTree[":variable"]; // HEEERRROOOOO
// If the current level of the pathTree and the path
// segment match, drill deeper.
} else if (pathTree[pathSegment]) {
pathTree = pathTree[pathSegment];
} else {
// *All* pathSegments of a url must match.
relativeMatch = false;
break;
}
}
if (relativeMatch) {
var args = [request, response].concat(argList, requestUrl.query);
pathTree.fn.apply({}, args);
} else {
this.handle404(request, response);
}
return this;
}
function router(options) {
return new Router(options);
}
exports.router = router;
})(typeof module.exports === "undefined" ? this["lion"] = {} : module.exports);