forked from sonyseng/json-caching-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
executable file
·124 lines (108 loc) · 5.32 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
const version = require('./package.json').version;
const JsonCachingProxy = require('./');
const fs = require('fs');
const url = require('url');
const path = require('path');
const program = require('commander');
const stripJsonComments = require('strip-json-comments');
const cwd = process.cwd();
function list (val) {
return val.split(':').map(item => item.trim());
}
function isDef (val) {
return typeof val !== 'undefined';
}
program
.storeOptionsAsProperties()
.version(version)
.option('-c, --config [path]', 'load a config file of options. Command line args will be overridden')
.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]', '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('-S, --excludeStatus [regex]', 'exclude specific status from cache, (e.g. --excludeStatus "503|404")')
.option('-a, --all', 'cache everything from the remote server (Default is to cache just JSON responses)')
.option('-P, --disablePlayback', 'disables cache playback')
.option('-R, --disableRecord', 'disables recording to cache')
.option('-C, --cmdPrefix [prefix]', 'change the prefix for the proxy\'s web admin endpoints')
.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('-o, --overrideCors [url]', 'override Access-Control-Allow-Origin')
.option('-z, --useCorsCredentials', 'set Access-Control-Allow-Credentials to true')
.parse(process.argv);
let configOptions = {};
if (program.config) {
try {
let filePath = path.isAbsolute(program.config) ? program.config : path.join(cwd, program.config);
configOptions = JSON.parse(stripJsonComments(fs.readFileSync(filePath, "utf8")));
} catch (err) {
console.error('Could not read config file', err.path);
process.exit(1);
}
}
let remoteServerUrl = configOptions.remoteServerUrl || program.url;
// Required Remote URL
if (!remoteServerUrl) {
program.outputHelp();
process.exit(1);
}
let proxyPort = configOptions.proxyPort ? parseInt(configOptions.proxyPort, 10) : program.port;
let inputHarFile = configOptions.inputHarFile || program.har;
let cacheBustingParams = configOptions.cacheBustingParams ? configOptions.cacheBustingParams : program.bust;
let commandPrefix = configOptions.commandPrefix || program.cmdPrefix;
let proxyHeaderIdentifier = configOptions.proxyHeaderIdentifier || program.header;
let cacheEverything = isDef(configOptions.cacheEverything) ? configOptions.cacheEverything : isDef(program.all) ? program.all : false;
let dataPlayback = isDef(configOptions.dataPlayback) ? configOptions.dataPlayback : isDef(program.disablePlayback) ? !program.disablePlayback : true;
let dataRecord = isDef(configOptions.dataRecord) ? configOptions.dataRecord : isDef(program.disableRecord) ? !program.disableRecord : true;
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) {
excludedRouteMatchers = configOptions.excludedRouteMatchers.map(matcher => new RegExp(matcher));
} else {
excludedRouteMatchers = program.exclude ? [new RegExp(program.exclude)] : [];
}
let excludedStatusMatchers;
if (configOptions.excludedStatusMatchers && configOptions.excludedStatusMatchers.length > 0) {
excludedStatusMatchers = configOptions.excludedStatusMatchers.map(matcher => new RegExp(matcher));
} else {
excludedStatusMatchers = program.excludeStatus ? [new RegExp(program.excludeStatus)] : [];
}
let harObject;
if (inputHarFile) {
try {
let filePath = path.isAbsolute(inputHarFile) ? inputHarFile : path.join(cwd, inputHarFile);
harObject = JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch (err) {
console.error('Could not read har file', err.path);
}
}
let jsonCachingProxy = new JsonCachingProxy({
remoteServerUrl,
harObject,
proxyPort,
cacheEverything,
cacheBustingParams,
excludedRouteMatchers,
excludedStatusMatchers,
dataPlayback,
dataRecord,
commandPrefix,
proxyHeaderIdentifier,
showConsoleOutput,
proxyTimeout,
deleteCookieDomain,
overrideCors,
useCorsCredentials
});
jsonCachingProxy.start();