1 | 'use strict';
|
2 |
|
3 | var EventEmitter = require('events').EventEmitter;
|
4 | var util = require('util');
|
5 | var http = require('http');
|
6 |
|
7 | var _ = require('lodash');
|
8 | var assert = require('assert-plus');
|
9 | var errors = require('restify-errors');
|
10 | var uuid = require('uuid');
|
11 |
|
12 | var Chain = require('./chain');
|
13 | var RouterRegistryRadix = require('./routerRegistryRadix');
|
14 |
|
15 |
|
16 |
|
17 | var MethodNotAllowedError = errors.MethodNotAllowedError;
|
18 | var ResourceNotFoundError = errors.ResourceNotFoundError;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function Router(options) {
|
39 | assert.object(options, 'options');
|
40 | assert.object(options.log, 'options.log');
|
41 | assert.optionalBool(options.onceNext, 'options.onceNext');
|
42 | assert.optionalBool(options.strictNext, 'options.strictNext');
|
43 | assert.optionalBool(
|
44 | options.ignoreTrailingSlash,
|
45 | 'options.ignoreTrailingSlash'
|
46 | );
|
47 |
|
48 | EventEmitter.call(this);
|
49 |
|
50 | this.log = options.log;
|
51 | this.onceNext = !!options.onceNext;
|
52 | this.strictNext = !!options.strictNext;
|
53 | this.name = 'RestifyRouter';
|
54 |
|
55 |
|
56 | this._anonymousHandlerCounter = 0;
|
57 | this._registry = options.registry || new RouterRegistryRadix(options);
|
58 | }
|
59 | util.inherits(Router, EventEmitter);
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | Router.prototype.lookup = function lookup(req, res) {
|
73 | var pathname = req.getUrl().pathname;
|
74 |
|
75 |
|
76 | var registryRoute = this._registry.lookup(req.method, pathname);
|
77 |
|
78 |
|
79 | if (!registryRoute) {
|
80 | return undefined;
|
81 | }
|
82 |
|
83 |
|
84 | req.params = Object.assign(req.params, registryRoute.params);
|
85 | req.route = registryRoute.route;
|
86 |
|
87 |
|
88 | return registryRoute.handler;
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | Router.prototype.lookupByName = function lookupByName(name, req, res) {
|
104 | var self = this;
|
105 | var route = self._registry.get()[name];
|
106 |
|
107 | if (!route) {
|
108 | return undefined;
|
109 | }
|
110 |
|
111 |
|
112 | req.route = route;
|
113 |
|
114 | return route.chain.run.bind(route.chain);
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | Router.prototype.render = function render(routeName, params, query) {
|
138 | var self = this;
|
139 |
|
140 | function pathItem(match, key) {
|
141 | if (params.hasOwnProperty(key) === false) {
|
142 | throw new Error(
|
143 | 'Route <' + routeName + '> is missing parameter <' + key + '>'
|
144 | );
|
145 | }
|
146 | return '/' + encodeURIComponent(params[key]);
|
147 | }
|
148 |
|
149 | function queryItem(key) {
|
150 | return encodeURIComponent(key) + '=' + encodeURIComponent(query[key]);
|
151 | }
|
152 |
|
153 | var route = self._registry.get()[routeName];
|
154 |
|
155 | if (!route) {
|
156 | return null;
|
157 | }
|
158 |
|
159 | var _path = route.spec.path;
|
160 | var _url = _path.replace(/\/:([A-Za-z0-9_]+)(\([^\\]+?\))?/g, pathItem);
|
161 | var items = Object.keys(query || {}).map(queryItem);
|
162 | var queryString = items.length > 0 ? '?' + items.join('&') : '';
|
163 | return _url + queryString;
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | Router.prototype.mount = function mount(opts, handlers) {
|
182 | var self = this;
|
183 |
|
184 | assert.object(opts, 'opts');
|
185 | assert.string(opts.method, 'opts.method');
|
186 | assert.arrayOfFunc(handlers, 'handlers');
|
187 | assert.optionalString(opts.name, 'opts.name');
|
188 |
|
189 | var chain = new Chain({
|
190 | onceNext: self.onceNext,
|
191 | strictNext: self.strictNext
|
192 | });
|
193 |
|
194 |
|
195 | var route = {
|
196 | name: self._getRouteName(opts.name, opts.method, opts.path),
|
197 | method: opts.method,
|
198 | path: opts.path,
|
199 | spec: opts,
|
200 | chain: chain
|
201 | };
|
202 |
|
203 | handlers.forEach(function forEach(handler) {
|
204 |
|
205 | handler._name =
|
206 | handler.name || 'handler-' + self._anonymousHandlerCounter++;
|
207 |
|
208 | handler._identifier = `${handler._name} on ${opts.method} ${opts.path}`;
|
209 |
|
210 |
|
211 | chain.add(handler);
|
212 | });
|
213 |
|
214 | self._registry.add(route);
|
215 | self.emit('mount', route.method, route.path);
|
216 |
|
217 | return route;
|
218 | };
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | Router.prototype.unmount = function unmount(name) {
|
231 | assert.string(name, 'name');
|
232 |
|
233 | var route = this._registry.remove(name);
|
234 | return route;
|
235 | };
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | Router.prototype.toString = function toString() {
|
247 | return this._registry.toString();
|
248 | };
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | Router.prototype.getDebugInfo = function getDebugInfo() {
|
259 | var routes = this._registry.get();
|
260 | return _.mapValues(routes, function mapValues(route, routeName) {
|
261 | return {
|
262 | name: route.name,
|
263 | method: route.method.toLowerCase(),
|
264 | path: route.path,
|
265 | handlers: route.chain.getHandlers()
|
266 | };
|
267 | });
|
268 | };
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 | Router.prototype.getRoutes = function getRoutes() {
|
279 | return this._registry.get();
|
280 | };
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | Router._optionsError = function _optionsError(req, res) {
|
297 | var pathname = req.getUrl().pathname;
|
298 | return req.method === 'OPTIONS' && pathname === '*';
|
299 | };
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | Router.prototype.defaultRoute = function defaultRoute(req, res, next) {
|
315 | var self = this;
|
316 | var pathname = req.getUrl().pathname;
|
317 |
|
318 |
|
319 | if (Router._optionsError(req, res, pathname)) {
|
320 | res.send(200);
|
321 | next(null, req, res);
|
322 | return;
|
323 | }
|
324 |
|
325 |
|
326 | var allowedMethods = http.METHODS.filter(function some(method) {
|
327 | return method !== req.method && self._registry.lookup(method, pathname);
|
328 | });
|
329 |
|
330 | if (allowedMethods.length) {
|
331 | res.methods = allowedMethods;
|
332 | res.setHeader('Allow', allowedMethods.join(', '));
|
333 | var methodErr = new MethodNotAllowedError(
|
334 | '%s is not allowed',
|
335 | req.method
|
336 | );
|
337 | next(methodErr, req, res);
|
338 | return;
|
339 | }
|
340 |
|
341 |
|
342 |
|
343 | var err = new ResourceNotFoundError('%s does not exist', pathname);
|
344 | next(err, req, res);
|
345 | };
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | Router.prototype._getRouteName = function _getRouteName(name, method, path) {
|
360 |
|
361 | if (!name) {
|
362 | name = method + '-' + path;
|
363 | name = name.replace(/\W/g, '').toLowerCase();
|
364 | }
|
365 |
|
366 |
|
367 | if (this._registry.get()[name]) {
|
368 | name += uuid.v4().substr(0, 7);
|
369 | }
|
370 |
|
371 | return name;
|
372 | };
|
373 |
|
374 | module.exports = Router;
|