Skip to content

Commit

Permalink
Merge pull request #18 from sonyseng/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
sonyseng authored Nov 13, 2020
2 parents efbbd3b + 7e120ce commit 66d18fe
Show file tree
Hide file tree
Showing 9 changed files with 635 additions and 727 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,31 @@ $ npm install -D json-caching-proxy
-I, --header [header] change the response header property for identifying cached responses
-l, --log print log output to console
-t, --timeout change the timeout for proxy server
-d, --deleteCookieDomain Remove the Domain portion of all cookies
-d, --deleteCookieDomain remove the Domain portion of all cookies
-o, --overrideCors [url] override Access-Control-Allow-Origin
-z, --useCorsCredentials set Access-Control-Allow-Credentials to true
```

#### Example - basic JSON caching with output
```
$ json-caching-proxy -u http://remote:8080 -l
```

#### Example - Bypassing CORS when proxying to a 3rd party api server
```
$ json-caching-proxy -u http://cors-api.example.com -o localhost:9000 -z
```
This use case occurs when developing a browser application against an api server on a different host with CORS restrictions.
In this example we might be running a dev server that's hosting a frontend application on http://localhost:9000 and there is
browser javascript that needs to fetch from http://cors-api.example.com. The `-z` option tells the proxy to set up auth headers
in case the code uses cookies or tokens (e.g. Bearer tokens)

#### Example - hydrating the cache
You may have a HAR file that was generated elsewhere (e.g. Chrome Developer tools). You can load this file and initialize the cache
```
$ json-caching-proxy -u http://remote:8080 -p 3001 -H chromeDevTools.har -l
```
You may have a HAR file that was generated elsewhere (e.g. Chrome Developer tools). You can load this file and initialize the cache

#### Example - advanced arguments
```
Expand Down Expand Up @@ -79,7 +91,10 @@ $ json-caching-proxy -u http://remote:8080 -p 3001 -b time:dc -e '/keepalive' -H
"dataRecord": true,
"commandPrefix": "proxy",
"proxyHeaderIdentifier": "proxy-cache-playback",
"proxyTimeout": 500000
"proxyTimeout": 500000,
"deleteCookieDomain": true,
"overrideCors": "localhost:8080",
"useCorsCredentials": true
}
```
```
Expand Down Expand Up @@ -108,7 +123,9 @@ let jsonCachingProxy = new JsonCachingProxy({
dataRecord: true,
showConsoleOutput: false,
proxyTimeout: 500000,
deleteCookieDomain: true
deleteCookieDomain: true,
overrideCors: "localhost:8080",
useCorsCredentials: true
});

jsonCachingProxy.start();
Expand Down
20 changes: 15 additions & 5 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ function isDef (val) {
program
.version(version)
.option('-c, --config [path]', 'load a config file of options. Command line args will be overridden')
.option('-u, --url [url]', 'remote server (e.g. https://network:8080)')
.option('-p, --port [number]', 'port for the local proxy server', parseInt)
.option('-u, --url [url]', 'set target server (e.g. https://network:8080)')
.option('-p, --port [number]', 'set port for the local proxy server', parseInt)
.option('-H, --har [path]', 'load entries from a HAR file and hydrate the cache')
.option('-b, --bust [list]', 'a list of cache busting query params to ignore. (e.g. --bust _:cacheSlayer:time:dc)', list)
.option('-b, --bust [list]', 'set cache busting query params to ignore. (e.g. --bust _:cacheSlayer:time:dc)', list)
.option('-e, --exclude [regex]', 'exclude specific routes from cache, (e.g. --exclude "GET /api/keep-alive/.*")')
.option('-a, --all', 'cache everything from the remote server (Default is to cache just JSON responses)')
.option('-P, --disablePlayback', 'disables cache playback')
Expand All @@ -34,7 +34,9 @@ program
.option('-I, --header [header]', 'change the response header property for identifying cached responses')
.option('-l, --log', 'print log output to console')
.option('-t, --timeout [number]', 'proxy timeout in milliseconds', parseInt)
.option('-d, --deleteCookieDomain', 'Remove the Domain portion of all cookies')
.option('-d, --deleteCookieDomain', 'remove the Domain portion of all cookies')
.option('-o, --overrideCors [url]', 'override Access-Control-Allow-Origin')
.option('-z, --useCorsCredentials', 'set Access-Control-Allow-Credentials to true')
.parse(process.argv);

let configOptions = {};
Expand Down Expand Up @@ -67,6 +69,12 @@ let dataRecord = isDef(configOptions.dataRecord) ? configOptions.dataRecord : is
let showConsoleOutput = isDef(configOptions.showConsoleOutput) ? configOptions.showConsoleOutput : isDef(program.log) ? program.log : false;
let proxyTimeout = configOptions.proxyTimeout ? parseInt(configOptions.proxyTimeout, 10) : program.timeout;
let deleteCookieDomain = isDef(configOptions.deleteCookieDomain) ? configOptions.deleteCookieDomain : isDef(program.deleteCookieDomain) ? program.deleteCookieDomain : false;
let overrideCors = isDef(configOptions.overrideCors) ? configOptions.overrideCors : isDef(program.overrideCors) ? program.overrideCors : false;
let useCorsCredentials = isDef(configOptions.useCorsCredentials) ? configOptions.useCorsCredentials : isDef(program.useCorsCredentials) ? program.useCorsCredentials : false;

if (overrideCors === true) {
overrideCors = '*';
}

let excludedRouteMatchers;
if (configOptions.excludedRouteMatchers && configOptions.excludedRouteMatchers.length > 0) {
Expand Down Expand Up @@ -98,7 +106,9 @@ let jsonCachingProxy = new JsonCachingProxy({
proxyHeaderIdentifier,
showConsoleOutput,
proxyTimeout,
deleteCookieDomain
deleteCookieDomain,
overrideCors,
useCorsCredentials
});

jsonCachingProxy.start();
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const npmPackage = require('./package.json');
const crypto = require('crypto');
const express = require('express');
const proxy = require('express-http-proxy');
const cors = require('cors');
const bodyParser = require('body-parser');
const urlUtil = require('url');
const chalk = require('chalk');
Expand All @@ -27,6 +28,8 @@ class JsonCachingProxy {
showConsoleOutput: false,
proxyTimeout: 3600000, // one hour
deleteCookieDomain: false, // Removes the domain portion from all cookies
overrideCors: false,
useCorsCredentials: false
};

// Ignore undefined values and combine the options with defaults
Expand All @@ -45,6 +48,11 @@ class JsonCachingProxy {

this.excludedParamMap = this.options.cacheBustingParams.reduce((map, param) => { map[param] = true; return map }, {});

if (this.options.overrideCors) {
this.app.use(cors({credentials: this.options.useCorsCredentials, origin: this.options.overrideCors}));
this.app.options('*', cors({credentials: this.options.useCorsCredentials, origin: this.options.overrideCors}));
}

if (this.options.showConsoleOutput) {
this.log = console.log;
this.err = console.error;
Expand Down Expand Up @@ -362,6 +370,10 @@ class JsonCachingProxy {
res.location(urlUtil.parse(location).path);
}

if (this.options.overrideCors && this.options.overrideCors !== '*') {
res.header('access-control-allow-origin', this.options.overrideCors);
}

if (this.options.deleteCookieDomain && res._headers['set-cookie']) {
res.header('set-cookie', this.removeCookiesDomain(res._headers['set-cookie'] || []));
}
Expand Down Expand Up @@ -398,7 +410,7 @@ class JsonCachingProxy {
this.server.setTimeout(this.options.proxyTimeout);

this.log(chalk.bold(`\JSON Caching Proxy Started:`));
this.log(chalk.gray(`==============\n`));
this.log(chalk.gray(`===========================\n`));
this.log(`Remote server url: \t${chalk.bold(this.options.remoteServerUrl)}`);
this.log(`Proxy running on port: \t${chalk.bold(this.options.proxyPort)}`);
this.log(`Proxy Timeout: \t\t${chalk.bold(this.options.proxyTimeout)}`);
Expand All @@ -408,6 +420,8 @@ class JsonCachingProxy {
this.log(`Proxy response header: \t${chalk.bold(this.options.proxyHeaderIdentifier)}`);
this.log(`Cache all: \t\t${chalk.bold(this.options.cacheEverything)}`);
this.log(`Delete cookies domain: \t${chalk.bold(this.options.deleteCookieDomain)}`);
this.log(`Override CORS origin: \t${chalk.bold(this.options.overrideCors)}`);
this.log(`Use CORS Credentials: \t${chalk.bold(this.options.useCorsCredentials)}`);
this.log(`Cache busting params: \t${chalk.bold(this.options.cacheBustingParams)}`);
this.log(`Excluded routes: `);
this.options.excludedRouteMatchers.forEach((regExp) => {
Expand Down
52 changes: 26 additions & 26 deletions jsdoc/JsonCachingProxy.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line14">line 14</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line11">line 11</a>
</li></ul></dd>


Expand Down Expand Up @@ -251,7 +251,7 @@ <h4 class="name" id="addAdminRoutes"><span class="type-signature"></span>addAdmi

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line218">line 218</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line227">line 227</a>
</li></ul></dd>


Expand Down Expand Up @@ -357,7 +357,7 @@ <h4 class="name" id="addBodyParser"><span class="type-signature"></span>addBodyP

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line283">line 283</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line292">line 292</a>
</li></ul></dd>


Expand Down Expand Up @@ -463,7 +463,7 @@ <h4 class="name" id="addCachingRoute"><span class="type-signature"></span>addCac

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line302">line 302</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line311">line 311</a>
</li></ul></dd>


Expand Down Expand Up @@ -618,7 +618,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line185">line 185</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line194">line 194</a>
</li></ul></dd>


Expand Down Expand Up @@ -773,7 +773,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line267">line 267</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line276">line 276</a>
</li></ul></dd>


Expand Down Expand Up @@ -880,7 +880,7 @@ <h4 class="name" id="addProxyRoute"><span class="type-signature"></span>addProxy

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line355">line 355</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line364">line 364</a>
</li></ul></dd>


Expand Down Expand Up @@ -1035,7 +1035,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line81">line 81</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line90">line 90</a>
</li></ul></dd>


Expand Down Expand Up @@ -1259,7 +1259,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line126">line 126</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line135">line 135</a>
</li></ul></dd>


Expand Down Expand Up @@ -1418,7 +1418,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line109">line 109</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line118">line 118</a>
</li></ul></dd>


Expand Down Expand Up @@ -1577,7 +1577,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line90">line 90</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line99">line 99</a>
</li></ul></dd>


Expand Down Expand Up @@ -1687,7 +1687,7 @@ <h4 class="name" id="getApp"><span class="type-signature"></span>getApp<span cla

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line460">line 460</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line476">line 476</a>
</li></ul></dd>


Expand Down Expand Up @@ -1793,7 +1793,7 @@ <h4 class="name" id="getDefaultOptions"><span class="type-signature"></span>getD

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line454">line 454</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line470">line 470</a>
</li></ul></dd>


Expand Down Expand Up @@ -1899,7 +1899,7 @@ <h4 class="name" id="getExcludedParamMap"><span class="type-signature"></span>ge

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line472">line 472</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line488">line 488</a>
</li></ul></dd>


Expand Down Expand Up @@ -2005,7 +2005,7 @@ <h4 class="name" id="getOptions"><span class="type-signature"></span>getOptions<

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line448">line 448</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line464">line 464</a>
</li></ul></dd>


Expand Down Expand Up @@ -2111,7 +2111,7 @@ <h4 class="name" id="getServer"><span class="type-signature"></span>getServer<sp

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line466">line 466</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line482">line 482</a>
</li></ul></dd>


Expand Down Expand Up @@ -2217,7 +2217,7 @@ <h4 class="name" id="getTotalCachedRoutes"><span class="type-signature"></span>g

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line478">line 478</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line494">line 494</a>
</li></ul></dd>


Expand Down Expand Up @@ -2323,7 +2323,7 @@ <h4 class="name" id="isRecording"><span class="type-signature"></span>isRecordin

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line496">line 496</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line512">line 512</a>
</li></ul></dd>


Expand Down Expand Up @@ -2429,7 +2429,7 @@ <h4 class="name" id="isReplaying"><span class="type-signature"></span>isReplayin

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line490">line 490</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line506">line 506</a>
</li></ul></dd>


Expand Down Expand Up @@ -2535,7 +2535,7 @@ <h4 class="name" id="isRouteCacheEmpty"><span class="type-signature"></span>isRo

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line484">line 484</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line500">line 500</a>
</li></ul></dd>


Expand Down Expand Up @@ -2714,7 +2714,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line176">line 176</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line185">line 185</a>
</li></ul></dd>


Expand Down Expand Up @@ -2780,7 +2780,7 @@ <h4 class="name" id="removeCookiesDomain"><span class="type-signature"></span>re


<div class="description">
Remove the domain portion of any cookies from the object
Remove the domain portion of any cookies from the object. Remove the secure attribute so we can set cookies to https targets
</div>


Expand Down Expand Up @@ -2873,7 +2873,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line61">line 61</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line70">line 70</a>
</li></ul></dd>


Expand Down Expand Up @@ -3032,7 +3032,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line395">line 395</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line408">line 408</a>
</li></ul></dd>


Expand Down Expand Up @@ -3187,7 +3187,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line435">line 435</a>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line451">line 451</a>
</li></ul></dd>


Expand Down Expand Up @@ -3257,7 +3257,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="JsonCachi
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.3</a> on Mon Jul 15 2019 11:14:20 GMT-0500 (Central Daylight Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Fri Nov 13 2020 17:22:31 GMT-0600 (Central Standard Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
Loading

0 comments on commit 66d18fe

Please sign in to comment.