Skip to content

Commit

Permalink
Merge pull request #10 from sirensolutions/bump-angular-to-1.7.9-2
Browse files Browse the repository at this point in the history
mitigate federate bug - overhead in schema computation phase on /siren endpoint
  • Loading branch information
szydan authored Mar 19, 2020
2 parents 51d156c + 83e95a0 commit d0de8e3
Show file tree
Hide file tree
Showing 5 changed files with 1,934 additions and 23 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ server.route({
console.log('doing some aditional stuff before redirecting');
callback(null, 'https://some.upstream.service.com/');
},
onResponse: function (err, res, request, reply, settings, ttl, data) {
onResponse: function (err, res, request, reply, settings, ttl) {

console.log('receiving the response from the upstream.');
Wreck.read(res, { json: true }, function (err, payload) {
Expand All @@ -191,3 +191,11 @@ server.route({
});

```

### Tagging new release

Before tagging make sure the version in package.json was bumped and commited

```
git tag -a 5.4.0-kibi-5 -m 'version 5.4.0-kibi-5' && git push origin 5.4.0-kibi-5
```
103 changes: 90 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Hoek = require('hoek');
const Joi = require('joi');
const Wreck = require('wreck');
const Boom = require('boom');
const URL = require('url')
const URL = require('url');


// Declare internals
Expand Down Expand Up @@ -157,10 +157,11 @@ internals.handler = function (route, handlerOptions) {

// Encoding request path
const _encodeURL = (url) => {
const uri = URL.parse(url);
if (uri.pathname && decodeURI(uri.pathname) === uri.pathname) {
uri.pathname = encodeURI(uri.pathname);
return URL.format(uri);

const tempUri = URL.parse(url);
if (tempUri.pathname && decodeURI(tempUri.pathname) === tempUri.pathname) {
tempUri.pathname = encodeURI(tempUri.pathname);
return URL.format(tempUri);
}
return url;
};
Expand Down Expand Up @@ -205,16 +206,92 @@ internals.handler = function (route, handlerOptions) {
if (onBeforeSendRequest) {
onBeforeSendRequest(request)
.then((requestUpdates) => {
Hoek.merge(options, requestUpdates);
_sendRequest();

Hoek.merge(options, requestUpdates);

// Note:
// Below change is to mitigate federate bug - unnecessary overhead in schema computation phase
// on indices with many shards
//
// We do NOT send requests without joins to /siren endpoint
if (requestUpdates.payload) {
const payload = requestUpdates.payload.toString();
const myUri = URL.parse(uri);
let containJoin = false;
let isSearchOrMsearch = false;

const reviver = function (key, value) {

if (key === 'join' && value.on !== undefined) {
containJoin = true;
}
return value;
};

if (myUri.pathname && myUri.pathname.indexOf('/_msearch') !== -1) {
isSearchOrMsearch = true;
const lines = payload.split('\n');
for (let i = 1; i < lines.length - 1; i = i + 2 ) {
if (containJoin) {
continue;
}
if (lines[i] !== '') {
try {
JSON.parse(lines[i], reviver);
} catch (e) {
console.log('Error parsing _msearch request payload [' + lines[i] + ']', e);
}
}
}
} else if (myUri.pathname && myUri.pathname.indexOf('/_search') !== -1){
isSearchOrMsearch = true;
if (payload !== '') {
try {
JSON.parse(payload, reviver);
} catch (e) {
console.log('Error parsing _search request payload [' + payload + ']', e);
}
}
}

if (isSearchOrMsearch && !containJoin) {
const pathWithNoSiren = myUri.pathname.replace(/^\/siren\//, '/');
myUri.pathname = pathWithNoSiren;

let search = myUri.search;
if (search && search.indexOf('preference')) {
if (search.startsWith('?')) {
search = search.substring(1);
}
const pairs = search.split('&');
for (let i = pairs.length - 1; i >= 0; i = i - 1) {
if (pairs[i].startsWith('preference=')) {
pairs.splice(i, 1);
}
}
search = pairs.join('&');
myUri.search = search;
}

try {
uri = URL.format(myUri);
} catch (e) {
console.log('Error when formatting the URL', e);
}
}
}
// end of mitigation code

_sendRequest();
})
.catch((err) => {
let msg = 'Failed request';
if (err.message) {
msg = err.message;
}
return _onError(Boom.badRequest(msg, err));
});

let msg = 'Failed request';
if (err.message) {
msg = err.message;
}
return _onError(Boom.badRequest(msg, err));
});
} else {
_sendRequest();
}
Expand Down
Loading

0 comments on commit d0de8e3

Please sign in to comment.