Skip to content

Commit

Permalink
fix(workflow): fixed bug with proxy middleware json not updating cont…
Browse files Browse the repository at this point in the history
…ent length
  • Loading branch information
GoPro16 committed Oct 31, 2019
1 parent 79de08e commit 281a2d7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 32 deletions.
1 change: 0 additions & 1 deletion packages/workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"lodash.trimstart": "^4.5.1",
"mini-css-extract-plugin": "^0.8.0",
"moment": "^2.21.0",
"node-http-proxy-json": "^0.1.3",
"node-sass": "^4.8.3",
"open": "^6.4.0",
"optimize-css-assets-webpack-plugin": "^5.0.1",
Expand Down
36 changes: 19 additions & 17 deletions packages/workflow/scripts/proxy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const chalk = require('chalk');
const debug = require('debug')('workflow:proxy');
const { get, merge } = require('lodash');
const proxyJson = require('node-http-proxy-json');
const typeIs = require('type-is');
const urlJoin = require('url-join');
const Logger = require('@availity/workflow-logger');
const escapeStringRegexp = require('escape-string-regexp');
Expand Down Expand Up @@ -44,7 +42,7 @@ function onRequest(proxyConfig, proxyObject) {
});
}

function onResponse(proxyConfig, proxyObject, req, res) {
function onResponse(proxyConfig, proxyObject) {
if (!get(proxyConfig, 'contextRewrite', true)) {
return;
}
Expand Down Expand Up @@ -76,20 +74,24 @@ function onResponse(proxyConfig, proxyObject, req, res) {
}
});

const isJson = typeIs.is(proxyObject.headers['content-type'], ['json']) === 'json';
if (isJson && proxyObject.statusCode !== 304) {
proxyJson(res, proxyObject.headers['content-encoding'], body => {
if (body) {
const json = JSON.stringify(body);
const replacedUrl = regexerContext.test(json) ? hostUrl : hostUrlContext;
const replacedJson = json.replace(regexer, replacedUrl);
debug(`Rewriting response body urls to ${chalk.blue(replacedUrl)} for request ${chalk.blue(req.url)}`);
body = JSON.parse(replacedJson);
}

return body;
});
}
// Below code never worked as the content length also had to be modified before gzipping back to UI.
// This middleware didn't update the content length. Leaving this out unless in the future we have
// urls we want to rewrite to our localhost that isn't using ekko

// const isJson = typeIs.is(proxyObject.headers['content-type'], ['json']) === 'json';
// if (isJson && proxyObject.statusCode !== 304) {
// proxyJson(res, proxyObject.headers['content-encoding'], body => {
// if (body) {
// const json = JSON.stringify(body);
// const replacedUrl = regexerContext.test(json) ? hostUrl : hostUrlContext;
// const replacedJson = json.replace(regexer, replacedUrl);
// debug(`Rewriting response body urls to ${chalk.blue(replacedUrl)} for request ${chalk.blue(req.url)}`);
// body = JSON.parse(replacedJson);
// }

// return body;
// });
// }
}

function onProxyError(proxyConfiguration, err, req, res) {
Expand Down
15 changes: 1 addition & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4477,11 +4477,6 @@ buffer@^4.3.0:
ieee754 "^1.1.4"
isarray "^1.0.0"

bufferhelper@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/bufferhelper/-/bufferhelper-0.2.1.tgz#fa74a385724a58e242f04ad6646c2366f83b913e"
integrity sha1-+nSjhXJKWOJC8ErWZGwjZvg7kT4=

builtin-modules@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.1.0.tgz#aad97c15131eb76b65b50ef208e7584cd76a7484"
Expand Down Expand Up @@ -5303,7 +5298,7 @@ concat-map@0.0.1:
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=

concat-stream@^1.5.0, concat-stream@^1.5.1:
concat-stream@^1.5.0:
version "1.6.2"
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34"
integrity sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==
Expand Down Expand Up @@ -12968,14 +12963,6 @@ node-gyp@^5.0.2:
tar "^4.4.12"
which "1"

node-http-proxy-json@^0.1.3:
version "0.1.9"
resolved "https://registry.yarnpkg.com/node-http-proxy-json/-/node-http-proxy-json-0.1.9.tgz#5e744138c189ebd7e0105fe92d035a5486478cd4"
integrity sha512-WrKAR/y09BWaz5WqgbxuE6D/XsdhQFkLkSdnRk0a5uBKSINtApMV085MN7JMh+stiyBBltvgSR9SYVIZIpKKKQ==
dependencies:
bufferhelper "^0.2.1"
concat-stream "^1.5.1"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down

0 comments on commit 281a2d7

Please sign in to comment.