-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.js
62 lines (50 loc) · 1.94 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
56
57
58
59
60
61
62
'use strict';
const Wreck = require('wreck');
const Hoek = require('hoek');
module.exports = function createProxy(config) {
config = typeof config === 'object' && config || {};
return function proxy(req, res) {
const headers = config.headers;
const requestOptions = Object.assign({}, config);
if (config.connectTimeout) {
requestOptions.timeout = config.connectTimeout;
}
// merge request headers with proxy headers
requestOptions.headers = Object.assign({}, req.headers, headers, {
host: req.headers.host
});
requestOptions.payload = req;
var proxyRequest = Wreck.request(req.method, req.query.origin, requestOptions);
if (config.socketTimeout) {
proxyRequest.setTimeout(config.socketTimeout, function timedout() {
proxyRequest.abort();
if (res.headersSent) {
res.socket.destroy();
}
});
}
proxyRequest.once('response', function (proxyResponse) {
res.writeHead(proxyResponse.statusCode, proxyResponse.headers);
proxyResponse.pipe(res);
proxyRequest.once('abort', () => {
proxyResponse.unpipe();
proxyResponse.destroy();
});
});
const handleErr = Hoek.once(err => {
if (err.code === 'ETIMEDOUT' || err.code === 'ECONNRESET') {
res.writeHead(504, `Gateway timeout`);
delete requestOptions.payload;
console.log(`[ERROR] Host info ${JSON.stringify(requestOptions, null, ' ')}`);
}
else {
res.writeHead(500, 'Unknown error');
console.log('[ERROR]', err.stack);
}
res.end();
});
proxyRequest.on('error', handleErr);
req.on('end', () => proxyRequest.end());
return proxyRequest;
};
};