Skip to content

Commit

Permalink
fix(server): toString() method
Browse files Browse the repository at this point in the history
  • Loading branch information
hekike committed Nov 15, 2017
1 parent 3c08576 commit bd2c358
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions lib/chain.js
Expand Up @@ -79,13 +79,13 @@ Chain.call = function call(handle, err, req, res, _next) {
*/

/**
* Extract handlers from a middle instance
* Get handlers of a chain instance
*
* @memberof Chain
* @instance
* @returns {Function[]} handlers
*/
Chain.prototype.extractHandlers = function extractHandlers() {
Chain.prototype.getHandlers = function getHandlers() {
return this.stack.map(function map(stackItem) {
return stackItem.handle;
});
Expand Down
30 changes: 15 additions & 15 deletions lib/router.js
Expand Up @@ -37,13 +37,12 @@ function Router(options) {

this.strict = Boolean(options.strictRouting);
this.log = options.log;
this.mounts = {};
this.name = 'RestifyRouter';
this._anonymusHandlerCounter = 0;
this.mounts = {};

// Internals
this.mounts = {};
this.findMyWay = new FindMyWay({
this._anonymusHandlerCounter = 0;
this._findMyWay = new FindMyWay({
defaultRoute: this.defaultRoute.bind(this)
});
}
Expand Down Expand Up @@ -94,7 +93,7 @@ Router.prototype.defaultRoute = function defaultRoute(req, res, next) {

// Check for 405 instead of 404
var allowedMethods = http.METHODS.filter(function some(method) {
return self.findMyWay.find(method, pathname);
return self._findMyWay.find(method, pathname);
});

if (allowedMethods.length) {
Expand Down Expand Up @@ -132,7 +131,7 @@ Router.prototype.lookup = function lookup(req, res, next) {

// Find find-my-way (fmw) route
self._dtraceStart(req);
var fmwRoute = self.findMyWay.find(req.method, url);
var fmwRoute = self._findMyWay.find(req.method, url);
self._dtraceEnd(req, res);

// Not found
Expand Down Expand Up @@ -227,7 +226,7 @@ Router.prototype.mount = function mount(opts, handlers) {
chain.use(handler);
});

self.findMyWay.on(
self._findMyWay.on(
route.method,
route.path,
function onRoute(req, res, next) {
Expand Down Expand Up @@ -260,14 +259,15 @@ Router.prototype.unmount = function unmount(name) {

var route = this.mounts[name];

if (route) {
// TODO: revisit
throw new Error('Unmount is not implemented');
// this.findMyWay.off(route.method, route.path);
// delete this.mounts[name];
if (!route) {
return;
}

return name;
// TODO: revisit
throw new Error('Unmount is not implemented');
// this._findMyWay.off(route.method, route.path);
// delete this.mounts[name];
// return route;
};

/**
Expand All @@ -280,7 +280,7 @@ Router.prototype.unmount = function unmount(name) {
* @returns {String} stringified router
*/
Router.prototype.toString = function toString() {
return this.findMyWay.prettyPrint();
return this._findMyWay.prettyPrint();
};

/**
Expand All @@ -297,7 +297,7 @@ Router.prototype.getDebugInfo = function getDebugInfo() {
name: route.name,
method: route.method.toLowerCase(),
path: route.path,
handlers: route.chain.extractHandlers()
handlers: route.chain.getHandlers()
};
});
};
Expand Down
42 changes: 21 additions & 21 deletions lib/server.js
Expand Up @@ -85,8 +85,6 @@ var PROXY_EVENTS = [
* If provided the following restify server options will be ignored:
* spdy, ca, certificate, key, passphrase, rejectUnauthorized, requestCert and
* ciphers; however these can all be specified on httpsServerOptions.
* @param {Boolean} [options.strictRouting=false] - If set, Restify
* will treat "/foo" and "/foo/" as different paths.
* @example
* var restify = require('restify');
* var server = restify.createServer();
Expand All @@ -111,7 +109,6 @@ function Server(options) {
this.name = options.name;
this.handleUncaughtExceptions = options.handleUncaughtExceptions || false;
this.router = options.router;
this.routes = {};
this.secure = false;
this.socketio = options.socketio || false;
this.dtrace = options.dtrace || options.false;
Expand Down Expand Up @@ -533,17 +530,12 @@ Server.prototype.param = function param(name, fn) {
* @instance
* @function rm
* @throws {TypeError} on bad input.
* @param {String} route - the route name.
* @param {String} routeName - the route name.
* @returns {Boolean} true if route was removed, false if not.
*/
Server.prototype.rm = function rm(route) {
var r = this.router.unmount(route);

if (r && this.routes[r]) {
delete this.routes[r];
}

return r;
Server.prototype.rm = function rm(routeName) {
var route = this.router.unmount(routeName);
return !!route;
};

///--- Info and debug methods
Expand Down Expand Up @@ -639,8 +631,8 @@ Server.prototype.getDebugInfo = function getDebugInfo() {
// output the actual routes registered with restify
var routeInfo = self.router.getDebugInfo();

var preHandlers = self.preChain.extractHandlers().map(funcNameMapper);
var useHandlers = self.useChain.extractHandlers().map(funcNameMapper);
var preHandlers = self.preChain.getHandlers().map(funcNameMapper);
var useHandlers = self.useChain.getHandlers().map(funcNameMapper);

// get each route's handler chain
var routes = _.map(routeInfo, function mapValues(route) {
Expand Down Expand Up @@ -704,7 +696,6 @@ Server.prototype.getDebugInfo = function getDebugInfo() {
Server.prototype.toString = function toString() {
var LINE_FMT = '\t%s: %s\n';
var SUB_LINE_FMT = '\t\t%s: %s\n';
var self = this;
var str = '';

function handlersToString(arr) {
Expand All @@ -722,13 +713,22 @@ Server.prototype.toString = function toString() {

str += sprintf(LINE_FMT, 'Accepts', this.acceptable.join(', '));
str += sprintf(LINE_FMT, 'Name', this.name);
// TODO: revisit
// str += sprintf(LINE_FMT, 'Pre', handlersToString(this.preChain));
str += sprintf(LINE_FMT, 'Router', this.router.toString());
str += sprintf(
LINE_FMT,
'Pre',
handlersToString(this.preChain.getHandlers())
);
str += sprintf(LINE_FMT, 'Router', '');
this.router
.toString()
.split('\n')
.forEach(function forEach(line) {
str += sprintf('\t\t%s\n', line);
});
str += sprintf(LINE_FMT, 'Routes', '');
Object.keys(this.routes).forEach(function forEach(k) {
var handlers = handlersToString(self.routes[k]);
str += sprintf(SUB_LINE_FMT, k, handlers);
_.forEach(this.router.mounts, function forEach(route, routeName) {
var handlers = handlersToString(route.chain.getHandlers());
str += sprintf(SUB_LINE_FMT, routeName, handlers);
});
str += sprintf(LINE_FMT, 'Secure', this.secure);
str += sprintf(LINE_FMT, 'Url', this.url);
Expand Down
4 changes: 2 additions & 2 deletions test/chain.test.js
Expand Up @@ -138,11 +138,11 @@ test('count returns with the number of registered handlers', function(t) {
t.end();
});

test('extractHandlers returns with the array of handlers', function(t) {
test('getHandlers returns with the array of handlers', function(t) {
var chain = new Chain();
var handlers = [function(req, res, next) {}, function(req, res, next) {}];
chain.use(handlers[0]);
chain.use(handlers[1]);
t.deepEqual(chain.extractHandlers(), handlers);
t.deepEqual(chain.getHandlers(), handlers);
t.end();
});

0 comments on commit bd2c358

Please sign in to comment.