1 | ;
|
2 |
|
3 | var assert = require('assert-plus');
|
4 | var FindMyWay = require('find-my-way');
|
5 | var Chain = require('./chain');
|
6 |
|
7 | /**
|
8 | * Radix tree based router registry backed by `find-my-way`
|
9 | *
|
10 | * @class RouterRegistryRadix
|
11 | * @public
|
12 | * @param {Object} options - an options object
|
13 | * @param {Object} [options.ignoreTrailingSlash] - ignore trailing slash on
|
14 | * paths
|
15 | */
|
16 | function RouterRegistryRadix(options) {
|
17 | this._findMyWay = new FindMyWay(options);
|
18 | this._routes = {};
|
19 | }
|
20 |
|
21 | /**
|
22 | * Adds a route.
|
23 | *
|
24 | * @public
|
25 | * @memberof Router
|
26 | * @instance
|
27 | * @function add
|
28 | * @param {Object} route - an route object
|
29 | * @param {String} route.name - name of the route
|
30 | * @param {String} route.method - HTTP method
|
31 | * @param {String} route.path - any String accepted by
|
32 | * [find-my-way](https://github.com/delvedor/find-my-way)
|
33 | * @param {Chain} route.chain - Chain instance
|
34 | * @returns {Boolean} true
|
35 | */
|
36 | RouterRegistryRadix.prototype.add = function add(route) {
|
37 | assert.object(route, 'route');
|
38 | assert.string(route.method, 'route.method');
|
39 | assert.string(route.path, 'path');
|
40 | assert.ok(route.chain instanceof Chain, 'route.chain');
|
41 |
|
42 | this._findMyWay.on(
|
43 | route.method,
|
44 | route.path,
|
45 | function onRoute(req, res, next) {
|
46 | route.chain.run(req, res, next);
|
47 | },
|
48 | {
|
49 | route: route
|
50 | }
|
51 | );
|
52 |
|
53 | this._routes[route.name] = route;
|
54 |
|
55 | return route;
|
56 | };
|
57 |
|
58 | /**
|
59 | * Removes a route.
|
60 | *
|
61 | * @public
|
62 | * @memberof RouterRegistryRadix
|
63 | * @instance
|
64 | * @function remove
|
65 | * @param {String} name - the route name
|
66 | * @returns {Object|undefined} removed route if found
|
67 | */
|
68 | RouterRegistryRadix.prototype.remove = function remove(name) {
|
69 | assert.string(name, 'name');
|
70 |
|
71 | // check for route
|
72 | var route = this._routes[name];
|
73 | if (!route) {
|
74 | return undefined;
|
75 | }
|
76 |
|
77 | // remove from registry
|
78 | this._findMyWay.off(route.method, route.path);
|
79 | delete this._routes[name];
|
80 |
|
81 | return route;
|
82 | };
|
83 |
|
84 | /**
|
85 | * Registry for route
|
86 | *
|
87 | * @public
|
88 | * @memberof RouterRegistryRadix
|
89 | * @instance
|
90 | * @function Registry
|
91 | * @param {String} method - method
|
92 | * @param {String} pathname - pathname
|
93 | * @returns {Chain|undefined} handler or undefined
|
94 | */
|
95 | RouterRegistryRadix.prototype.lookup = function lookup(method, pathname) {
|
96 | assert.string(method, 'method');
|
97 | assert.string(pathname, 'pathname');
|
98 |
|
99 | var fmwRoute = this._findMyWay.find(method, pathname);
|
100 |
|
101 | // Not found
|
102 | if (!fmwRoute) {
|
103 | return undefined;
|
104 | }
|
105 |
|
106 | // Call handler chain
|
107 | return {
|
108 | route: fmwRoute.store.route,
|
109 | params: fmwRoute.params,
|
110 | handler: fmwRoute.handler
|
111 | };
|
112 | };
|
113 |
|
114 | /**
|
115 | * Get registry
|
116 | *
|
117 | * @public
|
118 | * @memberof RouterRegistryRadix
|
119 | * @instance
|
120 | * @function toString
|
121 | * @returns {String} stringified RouterRegistryRadix
|
122 | */
|
123 | RouterRegistryRadix.prototype.get = function get() {
|
124 | return this._routes;
|
125 | };
|
126 |
|
127 | /**
|
128 | * toString() serialization.
|
129 | *
|
130 | * @public
|
131 | * @memberof RouterRegistryRadix
|
132 | * @instance
|
133 | * @function toString
|
134 | * @returns {String} stringified RouterRegistryRadix
|
135 | */
|
136 | RouterRegistryRadix.prototype.toString = function toString() {
|
137 | return this._findMyWay.prettyPrint();
|
138 | };
|
139 |
|
140 | module.exports = RouterRegistryRadix;
|