-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (29 loc) · 861 Bytes
/
index.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
const NO_BODY_METHODS = ['GET', 'HEAD', 'OPTIONS'];
const setLength = (headers, len) => {
delete headers['transfer-encoding'];
headers['content-length'] = len;
}
exports.server = (server) => {
server.on('request', (req) => {
const { method, headers, originalReq: { ruleValue } } = req;
if (NO_BODY_METHODS.includes(method) || headers['content-length']) {
return req.passThrough();
}
if (ruleValue >= 0) {
setLength(req.headers, ruleValue);
return req.passThrough();
}
let body;
req.on('data', (chunk) => {
body = body ? Buffer.concat([body, chunk]) : chunk;
});
req.on('end', () => {
if (body) {
setLength(req.headers, body.length);
} else if (ruleValue !== 'no-empty') {
setLength(req.headers, body.length);
}
req.request().end(body);
});
});
};