From acc69af856ae5dea9a3f8c551e33c3b93466e65f Mon Sep 17 00:00:00 2001 From: Rob McGuinness Date: Wed, 31 May 2017 14:58:04 -0400 Subject: [PATCH] Fixes #74: ERR_CONTENT_DECODING_FAILED --- packages/availity-workflow/package.json | 1 + packages/availity-workflow/scripts/proxy.js | 36 ++++++++------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/packages/availity-workflow/package.json b/packages/availity-workflow/package.json index 0a8f9e9f..d528cfd8 100644 --- a/packages/availity-workflow/package.json +++ b/packages/availity-workflow/package.json @@ -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", diff --git a/packages/availity-workflow/scripts/proxy.js b/packages/availity-workflow/scripts/proxy.js index 00957763..0de41f88 100644 --- a/packages/availity-workflow/scripts/proxy.js +++ b/packages/availity-workflow/scripts/proxy.js @@ -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'); @@ -22,7 +23,6 @@ function proxyLogRewrite(daArgs) { } - function onRequest(proxyConfig, proxyObject) { if (!get(proxyConfig, 'contextRewrite', true)) { @@ -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 @@ -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; - }; + }); }