Skip to content

Commit

Permalink
Merge pull request #9 from microservice-framework/2.x-issue-5
Browse files Browse the repository at this point in the history
  • Loading branch information
Gormartsen authored Mar 11, 2019
2 parents 9bdb0c5 + c5157b5 commit 010b704
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"@microservice-framework/microservice": "^1.3.5",
"@microservice-framework/microservice-client": "^1.3.0",
"@microservice-framework/microservice-cluster": "^1.2.2",
"@microservice-framework/microservice-cluster": "github:microservice-framework/microservice-cluster#2.x",
"@microservice-framework/microservice-router-register": "^1.3.1",
"debug": "^2.2.0",
"dot": "^1.1.1",
Expand Down
99 changes: 56 additions & 43 deletions router-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function applyAccessToken(requestDetails) {
/**
* Proxy GET requests.
*/
function ProxyRequestGet(jsonData, requestDetails, callback) {
function ProxyRequestGet(url, requestDetails, callback) {
applyAccessToken(requestDetails);
if (requestDetails.url == '') {
var Explorer = new ExplorerClass(requestDetails, callback);
Expand All @@ -94,7 +94,7 @@ function ProxyRequestGet(jsonData, requestDetails, callback) {
let cutPosition = requestDetails.url.lastIndexOf('/');
let route = requestDetails.url.substring(0, cutPosition);
let path = requestDetails.url.substring(cutPosition + 1);
proxyRequest(route, path, 'GET', jsonData, requestDetails, callback);
proxyRequest(route, path, 'GET', url, requestDetails, callback);
}

/**
Expand Down Expand Up @@ -124,12 +124,12 @@ function ProxyRequestPUT(jsonData, requestDetails, callback) {
/**
* Proxy DELETE requests.
*/
function ProxyRequestDELETE(jsonData, requestDetails, callback) {
function ProxyRequestDELETE(url, requestDetails, callback) {
applyAccessToken(requestDetails);
let cutPosition = requestDetails.url.lastIndexOf('/');
let route = requestDetails.url.substring(0, cutPosition);
let path = requestDetails.url.substring(cutPosition + 1);
proxyRequest(route, path, 'DELETE', jsonData, requestDetails, callback);
proxyRequest(route, path, 'DELETE', url, requestDetails, callback);
}


Expand Down Expand Up @@ -193,7 +193,7 @@ function getProperty( propertyName, object ) {
let parts = propertyName.split( "." ),
length = parts.length,
i,
property = object || this;
property = object;
for ( i = 0; i < length; i++ ) {
if (!property[parts[i]]) {
return new Error('Property Does not exists')
Expand Down Expand Up @@ -235,6 +235,9 @@ function checkConditions(conditions, requestDetails, jsonData) {
// check payload
if (conditions.payload && conditions.payload.length
&& jsonData) {
if (typeof jsonData != "object") {
return false
}
for (let payload of conditions.payload ) {
debug.debug('Checking for condition %O', payload)
let receivedPayloadValue = getProperty(payload.name, jsonData)
Expand Down Expand Up @@ -265,7 +268,7 @@ function checkConditions(conditions, requestDetails, jsonData) {
function matchRoute(targetRequest, routeItem) {
let routeItems = targetRequest.route.split('/');

if(routeItem.type == "metric") {
if (routeItem.type == "metric") {
if (routeItem.conditions) {
if (!checkConditions(routeItem.conditions,
targetRequest.requestDetails, targetRequest.jsonData)) {
Expand Down Expand Up @@ -339,12 +342,9 @@ function hookCall(targetRequest, phase, callback) {
let headers = {};
// TODO verify date,content-type, transfer-encoding headers
let skipHeaders = [
'host',
'content-type',
'date',
'connection',
'content-length',
'transfer-encoding'
'host', // issue to properly connect
'connection', // if it is closed, behavior is unexpected
'content-length', //issue with recounting length of the package
]
for (var i in targetRequest.requestDetails.headers) {
if (skipHeaders.indexOf(i) != -1) {
Expand Down Expand Up @@ -547,17 +547,7 @@ function hookCall(targetRequest, phase, callback) {

} else {
debug.log('adapter processed');
let bodyJSON = false
try {
bodyJSON = JSON.parse(body);
} catch (e) {
debug.debug('JSON.parse(body) Error received: %O', e);
debug.log('Notify before reseived not json')
}
if (bodyJSON) {
// need to replace body data
targetRequest.requestDetails._buffer = body
}
targetRequest.requestDetails._buffer = body
// need to set headers x-set-XXXXX
debug.debug('Adapter Headers received: %O code: %s', response.headers, response.statusCode);
for (var i in response.headers) {
Expand All @@ -566,8 +556,8 @@ function hookCall(targetRequest, phase, callback) {
targetRequest.requestDetails.headers[headerName] = response.headers[i];
}
}
delete targetRequest.requestDetails.headers['content-length']
if (phase == 'before') {
delete targetRequest.requestDetails.headers['content-length']
// resign it
if (targetRequest.requestDetails.headers.signature) {
targetRequest.requestDetails.headers.signature = 'sha256='
Expand All @@ -590,6 +580,25 @@ function hookCall(targetRequest, phase, callback) {

}

/**
* decode buffer to specidied by content-type format.
*/
function decodeData(contentType, buffer){
let data = false
switch (contentType) {
case undefined: // version 1.x compatibility. If no content-type provided, assume json.
case 'application/json': {
data = JSON.parse(buffer);
break;
}
// Todo support more decoders here?
default: {
data = buffer
}
}
return data
}

/**
* Find all hook routes by stage.
*/
Expand Down Expand Up @@ -774,10 +783,10 @@ function _request(getRequest, callback, targetRequest, noMetric) {
debug.log('Metric route %s result %O', targetRequest.route, router);

let statusCode = 0
if(error) {
if (error) {
statusCode = error.code
} else {
if(response.statusCode) {
if (response.statusCode) {
statusCode = response.statusCode
}
}
Expand Down Expand Up @@ -886,6 +895,7 @@ function proxyRequest(route, path, method, jsonData, requestDetails, callback) {
let i;
let skipHeaders = [
'host',
'connection',
'content-length'
]
for (i in requestDetails.headers) {
Expand Down Expand Up @@ -915,15 +925,15 @@ function proxyRequest(route, path, method, jsonData, requestDetails, callback) {
// TODO call after hooks
return callback(new Error('Endpoint not found'), null)
}
let bodyJSON = false
let bodyJSON = ""
try {
bodyJSON = JSON.parse(body);
bodyJSON = decodeData(body, response.headers['content-type'])
} catch (e) {
debug.debug('JSON.parse(body) Error received: %O', e);
return callback(new Error('Service respond is not JSON.'));
debug.debug('decodeData Error received: %O', e);
return callback(e);
}
debug.debug('%s body: %O', route, bodyJSON);

debug.debug('%s body: %O', route, body);
// process after hooks
// hookCall requestDetails.headers and _buffer should contain response data.
let answerDetails = {
Expand All @@ -940,20 +950,23 @@ function proxyRequest(route, path, method, jsonData, requestDetails, callback) {
endpoint: targetRequest.endpoint
}
hookCall(targetAnswer, 'after', function(){
let body = false

// Double check updated _buffer after proxy.
let body = false
try {
body = JSON.parse(answerDetails._buffer);
body = decodeData(answerDetails._buffer, answerDetails.headers['content-type'])
} catch (e) {
debug.debug('JSON.parse(body) Error received: %O', e);
return callback(new Error('Service respond is not JSON.'));
debug.debug('decodeData Error received: %O', e);
return callback(e);
}
// prefix with base_URL all urls
if (method != 'OPTIONS') {
if (body.url) {
body.url = process.env.BASE_URL + body.url;
} else if (body.id) {
body.url = process.env.BASE_URL + route + '/' + body.id;
if(typeof body == "object") {
// prefix with base_URL all urls
if (method != 'OPTIONS') {
if (body.url) {
body.url = process.env.BASE_URL + body.url;
} else if (body.id) {
body.url = process.env.BASE_URL + route + '/' + body.id;
}
}
}
if (body instanceof Array) {
Expand All @@ -978,7 +991,7 @@ function proxyRequest(route, path, method, jsonData, requestDetails, callback) {
responseHeaders[i] = answerDetails.headers[i];
}
}

// deprecated. websoket need to be rewriten as a hook broadcast
if (response.statusCode == 200) {
sendBroadcastMessage(router, method, requestDetails.url, body);
}
Expand Down

0 comments on commit 010b704

Please sign in to comment.