-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (43 loc) · 1.25 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Fastify from 'fastify';
import proxy from '@fastify/http-proxy';
import scrambler from 'json-scrambler';
import 'dotenv/config';
const MAX_PAYLOAD_SIZE = 1048576 * 10;
// Proxy options
const PROXY_PORT = process.env.PROXY_PORT || 4000;
const PROXY_UPSTREAM = process.env.PROXY_UPSTREAM || 'https://jsonplaceholder.typicode.com/';
// Scrambler options
const SCRAMBLER_CHAOS = process.env.SCRAMBLER_CHAOS || 10;
const scramblerOptions = {
chaos: SCRAMBLER_CHAOS,
scrambleStructureOnly: false
};
const fastify = Fastify({
logger: true,
bodyLimit: MAX_PAYLOAD_SIZE
});
fastify.register(proxy, {
upstream: PROXY_UPSTREAM,
prefix: '/',
disableRequestLogging: true,
proxyPayloads: false
}).after(err => {
if (err) throw err;
});
fastify.addHook('onSend', (request, reply, payload, done) => {
const data = [];
payload.on('data', chunk => data.push(chunk));
payload.on('end', () => {
const body = Buffer.concat(data).toString('utf8');
const scrambledBody = scrambler(body, scramblerOptions);
done(null, scrambledBody);
// done(null, body);
})
});
fastify.listen({ port: PROXY_PORT }, (err, address) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
fastify.log.info(`Server listening on ${address}`);
});