Skip to content

Commit

Permalink
Merge pull request #4 from killmenot/patch-2
Browse files Browse the repository at this point in the history
Patch 2
  • Loading branch information
killmenot committed Dec 9, 2018
2 parents 7c19cb7 + 2812bb4 commit 7aeb3eb
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
npm-debug.log
node_modules/
package-lock.json
*.lock

# Sublime Text
*.sublime-*
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: node_js
node_js:
- '11'
- '10'
- '9'
- '8'
- '7'
- '6'
- '5'
- '4'
after_success:
- npm run coverage
- npm run coveralls
79 changes: 50 additions & 29 deletions lib/microServiceRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ var debug = require('debug')('qewd-microservice-router');

var requestId = 0; // unique request counter (to link requests and responses)

function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}

function logRoutes(routes) {
var isArray = Array.isArray(routes);

if (!isArray) {
routes = [routes];
}

routes = routes.map(function (route) {
var obj = clone(route);

if (obj.route && obj.route.ast) {
delete obj.route.ast;
}

return obj;
});

return isArray ? routes : routes[0];
}

function countDestinations(destination) {
var response = {
count: 0,
Expand All @@ -51,8 +75,8 @@ function countDestinations(destination) {

function getDestinations(destination) {
var destObj = destinations[destination];
debug('destination: %s', JSON.stringify(destination));
debug('destObj: %s', JSON.stringify(destObj));
debug('destination: %j', destination);
debug('destObj: %j', destObj);

if (!destObj || !destObj.destinations) {
return;
Expand All @@ -72,7 +96,7 @@ function countDestinations(destination) {

debug('count destinations');
getDestinations(destination);
debug('count destinations response: %s', JSON.stringify(response));
debug('count destinations response: %j', response);

return response;
}
Expand All @@ -86,7 +110,7 @@ function sendToMicroService(jwt, message, microService, route, handleResponse) {
}
};

debug('updating jwt expiry: %s', JSON.stringify(msg));
debug('updating jwt expiry: %j', msg);

this.handleMessage(msg, function(responseObj) {

Expand All @@ -107,10 +131,12 @@ function sendToMicroService(jwt, message, microService, route, handleResponse) {
token: responseObj.message.jwt,
args: route.args,
jwt: true,
/*jshint camelcase: false */
ms_requestId: requestId
/*jshint camelcase: true */
};

debug('sending micro-service request over websocket to remote system: %s', JSON.stringify(messageObj));
debug('sending micro-service request over websocket to remote system: %j', messageObj);

microService.client.send(messageObj, handleResponse);
});
Expand All @@ -121,7 +147,7 @@ function handleMicroService(message, route, destination, handleResponse) {
var microService = this.u_services.byDestination[destination];
/*jshint camelcase: true */

debug('handling micro-service %s for %s destination', JSON.stringify(microService), destination);
debug('handling micro-service %j for %s destination', microService, destination);

if (!microService || !microService.client || !microService.client.send) {
return handleResponse({
Expand All @@ -141,7 +167,7 @@ function handleMicroService(message, route, destination, handleResponse) {

var token = this.jwt.handlers.getRestJWT(message);
debug('jwt: %s', token);
debug('route = %s', JSON.stringify(route));
debug('route = %j', logRoutes(route));

if (token === '' || token === 'undefined') {
debug('using updated registration JWT');
Expand Down Expand Up @@ -191,11 +217,11 @@ function handleMicroService(message, route, destination, handleResponse) {
}

function microServiceRouter(message, handleResponse) {
debug('testing route for message: %s', JSON.stringify(message));
debug('testing route for message: %j', message);

/*jshint camelcase: false */
debug('restRoutes: %s', JSON.stringify(this.u_services.restRoutes));
debug('destinations: %s', JSON.stringify(this.u_services.byDestination));
debug('restRoutes: %j', logRoutes(this.u_services.restRoutes));
debug('destinations: %j', this.u_services.byDestination);

var self = this;

Expand All @@ -209,7 +235,7 @@ function microServiceRouter(message, handleResponse) {
var route = this.router.hasRoute(message.path, message.method, this.u_services.restRoutes);
/*jshint camelcase: true */

debug('route: %s', JSON.stringify(route));
debug('route: %j', logRoutes(route));

if (route.matched) {
// route.args - variables found in route, which may include destination;
Expand All @@ -234,11 +260,9 @@ function microServiceRouter(message, handleResponse) {
args.req.pathTemplate = route.pathTemplate;
args.jwt = this.jwt.handlers.getRestJWT(message);

debug('calling route.onRequest with args: %s', JSON.stringify(args));

//var send = microServiceRouter.bind(this);
debug('calling route.onRequest with args: %j', args);

var send = function(message, args, handleResponse) {
var send = function (message, args, handleResponse) {
if (handleResponse) {
message.headers = {
authorization: 'Bearer ' + args.jwt
Expand All @@ -251,13 +275,13 @@ function microServiceRouter(message, handleResponse) {
};

var status = route.onRequest.call(this, args, send, handleResponse);
if (status !== false) return true;
debug('status: %s', status);
if (status !== false) {
return true;
}
}

//console.log('2 route = ' + JSON.stringify(route, null, 2));

var destination = route.args.destination || route.destination;
//console.log('destination: ' + destination);
if (destination) {
/*jshint camelcase: false */
var destObj = this.u_services.byDestination[destination];
Expand All @@ -273,17 +297,16 @@ function microServiceRouter(message, handleResponse) {
return true;
}

debug('destObj: %s', JSON.stringify(destObj));
debug('destObj: %j', destObj);

if (!destObj.destinations) {
// single destination
debug('single destination');
//console.log('*** handleMicroService ***');
handleMicroService.call(this, message, route, destination, function(response) {
var handled = false;

if (route.onResponse && typeof route.onResponse === 'function') {
debug('calling route.onResponse with responseObj: %s', response);
debug('calling route.onResponse with responseObj: %j', response);
handled = route.onResponse.call(self, {
message: message,
destination: destination,
Expand All @@ -293,7 +316,7 @@ function microServiceRouter(message, handleResponse) {
});
}

debug('handled: %s', JSON.stringify(handled));
debug('handled: %j', handled);

if (!handled) {
if (!response.message && response.error) {
Expand All @@ -303,7 +326,7 @@ function microServiceRouter(message, handleResponse) {
delete response.error;
}

debug('handleResponse: %s', JSON.stringify(response));
debug('handleResponse: %j', response);
handleResponse(response);
}
});
Expand All @@ -312,8 +335,6 @@ function microServiceRouter(message, handleResponse) {
// mutiple destinations
debug('mutiple destinations');

//console.log('** multiple destinations **');

var destinationGroup = destination;
var results = countDestinations.call(this, destination);
var count = 0;
Expand Down Expand Up @@ -398,7 +419,7 @@ function microServiceRouter(message, handleResponse) {
var handled = false;

if (route.onResponse && typeof route.onResponse === 'function') {
debug('calling route.onResponse with responseObj: %s', response);
debug('calling route.onResponse with responseObj: %j', response);
handled = route.onResponse.call(self, {
message: message,
destination: destinationGroup,
Expand All @@ -408,10 +429,10 @@ function microServiceRouter(message, handleResponse) {
});
}

debug('handled: %s', JSON.stringify(handled));
debug('handled: %j', handled);

if (!handled) {
debug('handleResponse: %s', JSON.stringify(responseObj));
debug('handleResponse: %j', responseObj);
handleResponse(responseObj);
}
}
Expand Down
18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,19 @@
"all": true,
"include": [
"lib/**/*.js"
],
"exclude": [
"spec/**/*.js"
]
},
"dependencies": {
"debug": "^3.1.0"
"debug": "^4.0.1"
},
"devDependencies": {
"coveralls": "^3.0.0",
"jasmine": "^2.9.0",
"coveralls": "^3.0.2",
"jasmine": "^3.3.1",
"jasmine-spec-reporter": "^4.1.1",
"jasmine-spy-matchers": "^1.2.0",
"jshint": "^2.9.5",
"nyc": "^11.4.1",
"pre-commit": "^1.2.2"
"jasmine-spy-matchers": "^2.1.0",
"jshint": "^2.9.7",
"nyc": "^13.1.0",
"pre-commit": "^1.2.2",
"rewire": "^4.0.1"
}
}
Loading

0 comments on commit 7aeb3eb

Please sign in to comment.