Skip to content

Commit 66d18fe

Browse files
authored
Merge pull request #18 from sonyseng/develop
develop
2 parents efbbd3b + 7e120ce commit 66d18fe

9 files changed

+635
-727
lines changed

README.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,31 @@ $ npm install -D json-caching-proxy
3535
-I, --header [header] change the response header property for identifying cached responses
3636
-l, --log print log output to console
3737
-t, --timeout change the timeout for proxy server
38-
-d, --deleteCookieDomain Remove the Domain portion of all cookies
38+
-d, --deleteCookieDomain remove the Domain portion of all cookies
39+
-o, --overrideCors [url] override Access-Control-Allow-Origin
40+
-z, --useCorsCredentials set Access-Control-Allow-Credentials to true
41+
3942
```
4043

4144
#### Example - basic JSON caching with output
4245
```
4346
$ json-caching-proxy -u http://remote:8080 -l
4447
```
4548

49+
#### Example - Bypassing CORS when proxying to a 3rd party api server
50+
```
51+
$ json-caching-proxy -u http://cors-api.example.com -o localhost:9000 -z
52+
```
53+
This use case occurs when developing a browser application against an api server on a different host with CORS restrictions.
54+
In this example we might be running a dev server that's hosting a frontend application on http://localhost:9000 and there is
55+
browser javascript that needs to fetch from http://cors-api.example.com. The `-z` option tells the proxy to set up auth headers
56+
in case the code uses cookies or tokens (e.g. Bearer tokens)
57+
4658
#### Example - hydrating the cache
47-
You may have a HAR file that was generated elsewhere (e.g. Chrome Developer tools). You can load this file and initialize the cache
4859
```
4960
$ json-caching-proxy -u http://remote:8080 -p 3001 -H chromeDevTools.har -l
5061
```
62+
You may have a HAR file that was generated elsewhere (e.g. Chrome Developer tools). You can load this file and initialize the cache
5163

5264
#### Example - advanced arguments
5365
```
@@ -79,7 +91,10 @@ $ json-caching-proxy -u http://remote:8080 -p 3001 -b time:dc -e '/keepalive' -H
7991
"dataRecord": true,
8092
"commandPrefix": "proxy",
8193
"proxyHeaderIdentifier": "proxy-cache-playback",
82-
"proxyTimeout": 500000
94+
"proxyTimeout": 500000,
95+
"deleteCookieDomain": true,
96+
"overrideCors": "localhost:8080",
97+
"useCorsCredentials": true
8398
}
8499
```
85100
```
@@ -108,7 +123,9 @@ let jsonCachingProxy = new JsonCachingProxy({
108123
dataRecord: true,
109124
showConsoleOutput: false,
110125
proxyTimeout: 500000,
111-
deleteCookieDomain: true
126+
deleteCookieDomain: true,
127+
overrideCors: "localhost:8080",
128+
useCorsCredentials: true
112129
});
113130

114131
jsonCachingProxy.start();

bin.js

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

4042
let configOptions = {};
@@ -67,6 +69,12 @@ let dataRecord = isDef(configOptions.dataRecord) ? configOptions.dataRecord : is
6769
let showConsoleOutput = isDef(configOptions.showConsoleOutput) ? configOptions.showConsoleOutput : isDef(program.log) ? program.log : false;
6870
let proxyTimeout = configOptions.proxyTimeout ? parseInt(configOptions.proxyTimeout, 10) : program.timeout;
6971
let deleteCookieDomain = isDef(configOptions.deleteCookieDomain) ? configOptions.deleteCookieDomain : isDef(program.deleteCookieDomain) ? program.deleteCookieDomain : false;
72+
let overrideCors = isDef(configOptions.overrideCors) ? configOptions.overrideCors : isDef(program.overrideCors) ? program.overrideCors : false;
73+
let useCorsCredentials = isDef(configOptions.useCorsCredentials) ? configOptions.useCorsCredentials : isDef(program.useCorsCredentials) ? program.useCorsCredentials : false;
74+
75+
if (overrideCors === true) {
76+
overrideCors = '*';
77+
}
7078

7179
let excludedRouteMatchers;
7280
if (configOptions.excludedRouteMatchers && configOptions.excludedRouteMatchers.length > 0) {
@@ -98,7 +106,9 @@ let jsonCachingProxy = new JsonCachingProxy({
98106
proxyHeaderIdentifier,
99107
showConsoleOutput,
100108
proxyTimeout,
101-
deleteCookieDomain
109+
deleteCookieDomain,
110+
overrideCors,
111+
useCorsCredentials
102112
});
103113

104114
jsonCachingProxy.start();

index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const npmPackage = require('./package.json');
22
const crypto = require('crypto');
33
const express = require('express');
44
const proxy = require('express-http-proxy');
5+
const cors = require('cors');
56
const bodyParser = require('body-parser');
67
const urlUtil = require('url');
78
const chalk = require('chalk');
@@ -27,6 +28,8 @@ class JsonCachingProxy {
2728
showConsoleOutput: false,
2829
proxyTimeout: 3600000, // one hour
2930
deleteCookieDomain: false, // Removes the domain portion from all cookies
31+
overrideCors: false,
32+
useCorsCredentials: false
3033
};
3134

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

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

51+
if (this.options.overrideCors) {
52+
this.app.use(cors({credentials: this.options.useCorsCredentials, origin: this.options.overrideCors}));
53+
this.app.options('*', cors({credentials: this.options.useCorsCredentials, origin: this.options.overrideCors}));
54+
}
55+
4856
if (this.options.showConsoleOutput) {
4957
this.log = console.log;
5058
this.err = console.error;
@@ -362,6 +370,10 @@ class JsonCachingProxy {
362370
res.location(urlUtil.parse(location).path);
363371
}
364372

373+
if (this.options.overrideCors && this.options.overrideCors !== '*') {
374+
res.header('access-control-allow-origin', this.options.overrideCors);
375+
}
376+
365377
if (this.options.deleteCookieDomain && res._headers['set-cookie']) {
366378
res.header('set-cookie', this.removeCookiesDomain(res._headers['set-cookie'] || []));
367379
}
@@ -398,7 +410,7 @@ class JsonCachingProxy {
398410
this.server.setTimeout(this.options.proxyTimeout);
399411

400412
this.log(chalk.bold(`\JSON Caching Proxy Started:`));
401-
this.log(chalk.gray(`==============\n`));
413+
this.log(chalk.gray(`===========================\n`));
402414
this.log(`Remote server url: \t${chalk.bold(this.options.remoteServerUrl)}`);
403415
this.log(`Proxy running on port: \t${chalk.bold(this.options.proxyPort)}`);
404416
this.log(`Proxy Timeout: \t\t${chalk.bold(this.options.proxyTimeout)}`);
@@ -408,6 +420,8 @@ class JsonCachingProxy {
408420
this.log(`Proxy response header: \t${chalk.bold(this.options.proxyHeaderIdentifier)}`);
409421
this.log(`Cache all: \t\t${chalk.bold(this.options.cacheEverything)}`);
410422
this.log(`Delete cookies domain: \t${chalk.bold(this.options.deleteCookieDomain)}`);
423+
this.log(`Override CORS origin: \t${chalk.bold(this.options.overrideCors)}`);
424+
this.log(`Use CORS Credentials: \t${chalk.bold(this.options.useCorsCredentials)}`);
411425
this.log(`Cache busting params: \t${chalk.bold(this.options.cacheBustingParams)}`);
412426
this.log(`Excluded routes: `);
413427
this.options.excludedRouteMatchers.forEach((regExp) => {

jsdoc/JsonCachingProxy.html

+26-26
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ <h5>Parameters:</h5>
142142

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

148148

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

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

257257

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

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

363363

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

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

469469

@@ -618,7 +618,7 @@ <h5>Parameters:</h5>
618618

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

624624

@@ -773,7 +773,7 @@ <h5>Parameters:</h5>
773773

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

779779

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

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

886886

@@ -1035,7 +1035,7 @@ <h5>Parameters:</h5>
10351035

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

10411041

@@ -1259,7 +1259,7 @@ <h5>Parameters:</h5>
12591259

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

12651265

@@ -1418,7 +1418,7 @@ <h5>Parameters:</h5>
14181418

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

14241424

@@ -1577,7 +1577,7 @@ <h5>Parameters:</h5>
15771577

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

15831583

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

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

16931693

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

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

17991799

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

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

19051905

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

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

20112011

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

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

21172117

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

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

22232223

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

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

23292329

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

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

24352435

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

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

25412541

@@ -2714,7 +2714,7 @@ <h5>Parameters:</h5>
27142714

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

27202720

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

27812781

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

27862786

@@ -2873,7 +2873,7 @@ <h5>Parameters:</h5>
28732873

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

28792879

@@ -3032,7 +3032,7 @@ <h5>Parameters:</h5>
30323032

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

30383038

@@ -3187,7 +3187,7 @@ <h5>Parameters:</h5>
31873187

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

31933193

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

32593259
<footer>
3260-
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)
3260+
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)
32613261
</footer>
32623262

32633263
<script> prettyPrint(); </script>

0 commit comments

Comments
 (0)