-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproxy.js
55 lines (47 loc) · 1.56 KB
/
proxy.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
const assert = require('assert');
const config = require('collections-online/lib/config');
const cip = require('./services/cip');
assert.ok(config.cip.proxy, 'Missing a config.cip.proxy object');
assert.ok(config.cip.proxy.maxSockets, 'Missing config.cip.proxy.maxSockets');
const Agent = require('agentkeepalive');
let agent;
// Choose a keep-alive agent based on the beginning of the baseUrl
if(config.cip.baseURL.indexOf('https://') === 0) {
agent = new Agent.HttpsAgent({
maxSockets: config.cip.proxy.maxSockets
});
} else {
agent = new Agent({
maxSockets: config.cip.proxy.maxSockets
});
}
var request = require('request').defaults({
agent,
timeout: config.cip.timeout || 55000 // 55 secs
});
function proxy(url, includeJSessionId = false) {
// Prefix the baseURL
url = config.cip.baseURL + url;
// Add any available jsessionid, just before any querystring.
if(url.indexOf('jsessionid') < 0 && cip.jsessionid && includeJSessionId) {
let jsessionidString = ';jsessionid=' + cip.jsessionid;
let queryStringStart = url.indexOf('?');
if(queryStringStart < 0) {
url += jsessionidString;
} else {
url = url.substring(0, queryStringStart) +
jsessionidString +
url.substring(queryStringStart);
}
}
console.log('CIP proxy is requesting', url);
return request
.get({
url,
rejectUnauthorized: config.cip.client.trustSelfSigned ? false : true
})
.on('response', function(response) {
response.headers['Cache-Control'] = 'max-age=2592000';
});
}
module.exports = proxy;