UNPKG

1.03 kBJavaScriptView Raw
1const getRequestMetadata = req => {
2 const config = {};
3
4 config.serverIP = req.connection.localAddress;
5 config.serverPort = req.connection.localPort.toString();
6 config.clientIP = req.headers['x-robbyson-client-address'];
7 config.clientPort = req.headers['x-robbyson-client-port'];
8
9 config.contractorId = req.contractor_id;
10 config.user = req.user;
11
12 return config;
13};
14
15const getResponseBody = (res, body) => {
16 const oldWrite = res.write;
17 const oldEnd = res.end;
18 const chunks = [];
19
20 return new Promise((resolve, reject) => {
21 res.write = function (chunk) {
22 chunks.push(new Buffer(chunk));
23 oldWrite.apply(res, arguments);
24 };
25
26 res.end = function (chunk) {
27 if (chunk) {
28 chunks.push(new Buffer(chunk));
29 }
30
31 body = Buffer.concat(chunks).toString('utf8');
32
33 resolve(body);
34 oldEnd.apply(res, arguments);
35 };
36 });
37};
38
39module.exports = {
40 getRequestMetadata,
41 getResponseBody
42};