Skip to content

Commit

Permalink
Fixes #74: ERR_CONTENT_DECODING_FAILED
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob McGuinness committed May 31, 2017
1 parent 21fbca9 commit acc69af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/availity-workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"lodash.merge": "^4.6.0",
"lodash.once": "^4.1.1",
"moment": "^2.18.1",
"node-http-proxy-json": "^0.1.3",
"opn": "5.0.0",
"ora": "^1.2.0",
"pretty-ms": "^2.1.0",
Expand Down
36 changes: 13 additions & 23 deletions packages/availity-workflow/scripts/proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const chalk = require('chalk');
const merge = require('lodash.merge');
const proxyJson = require('node-http-proxy-json');
const typeIs = require('type-is');
const urlJoin = require('url-join');
const get = require('lodash.get');
Expand All @@ -22,7 +23,6 @@ function proxyLogRewrite(daArgs) {

}


function onRequest(proxyConfig, proxyObject) {

if (!get(proxyConfig, 'contextRewrite', true)) {
Expand Down Expand Up @@ -59,7 +59,6 @@ function onResponse(proxyConfig, proxyObject, req, res) {
const port = settings.port();
const host = settings.host();


// http://localhost:3000
const hostUrl = `http://${host}:${port}`;
// http://localhost:3000/api
Expand All @@ -85,32 +84,23 @@ function onResponse(proxyConfig, proxyObject, req, res) {

});

const isJson = typeIs.is(proxyObject.headers['content-type'], ['json']) === 'json';
if (isJson) {

delete proxyObject.headers['content-length'];
const buffer = [];

const _write = res.write;
const _end = res.end;

res.write = (chunk) => {
buffer.push(chunk);
};

res.end = () => {
const isJson = typeIs.is(proxyObject.headers['content-type'], ['json']) === 'json';
if (isJson && !proxyObject.statusCode === 304) {

const json = buffer.toString('utf8');
const replacedUrl = regexerContext.test(json) ? hostUrl : hostUrlContext;
const replacedJson = json.replace(regexer, replacedUrl);
Logger.info(`Rewriting response body urls to ${chalk.blue(replacedUrl)} for request ${chalk.blue(req.url)}`);
proxyJson(res, proxyObject.headers['content-encoding'], body => {

const body = new Buffer(replacedJson);
if (body) {
const json = JSON.stringify(body);
const replacedUrl = regexerContext.test(json) ? hostUrl : hostUrlContext;
const replacedJson = json.replace(regexer, replacedUrl);
Logger.info(`Rewriting response body urls to ${chalk.blue(replacedUrl)} for request ${chalk.blue(req.url)}`);
body = JSON.parse(replacedJson);
}

_write.call(res, body);
_end.call(res);
return body;

};
});

}

Expand Down

0 comments on commit acc69af

Please sign in to comment.