-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (82 loc) · 2.29 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const http = require('http');
const zlib = require('zlib');
const cluster = require('cluster');
const { promisify } = require('util');
const numCPUs = require('os').cpus().length;
const DEFAULT_PORT = 80;
const { log } = console;
const brotliDecompress = promisify(zlib.brotliDecompress);
async function process(data) {
const { body, headers } = data;
log(`Processing data. Headers: ${JSON.stringify(headers)}`);
log(`Compressed body length: ${body.length}`);
try {
const decompressedBody = await brotliDecompress(body);
log(`Decompressed body length: ${decompressedBody.length}`);
} catch (err) {
log('Decompression failed');
}
}
function handle(req, res) {
const { host, ...headers } = req.headers;
const market = headers['x-point-of-sale'];
const version = headers['x-csv-version-number'];
const pcc = headers['x-customer-pcc'];
log(`Request received: ${JSON.stringify({
host, market, version, pcc,
})}`);
const chunks = [];
// Collect chunks
req.on('data', (chunk) => chunks.push(chunk));
// Send response on end BEFORE processing starts
req.on('end', () => {
// Providing response
res.setHeader('Content-Length', 0);
res.setHeader('transfer-encoding', '');
res.writeHead(200);
res.end();
// Starting processing
const body = Buffer.concat(chunks);
process({ body, headers });
});
}
if (cluster.isPrimary) {
// Starting cluster
log(`Stream consumer started with ${numCPUs} cpus`);
// Fork workers
Array.from(Array(numCPUs)).forEach(() => {
cluster.fork();
});
cluster.on('exit', (worker) => {
log(`worker ${worker.process.pid} died`);
cluster.fork();
});
} else {
// Processing data here
const server = http.createServer((req, res) => {
req.on('error', (err) => {
log('Request err', err);
res.end(err);
});
res.on('error', (err) => {
log('Response error', err);
});
switch (req.url) {
case '/':
if (req.method === 'POST') {
handle(req, res);
} else {
res.writeHead(405, 'Method Not Allowed');
res.end();
}
break;
default:
res.writeHead(404, 'Not Found');
res.end();
}
});
server.on('error', (err) => {
log('Server error', err);
});
server.listen(DEFAULT_PORT);
}