forked from Rob--W/cors-anywhere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
child.js
64 lines (57 loc) · 1.91 KB
/
child.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
// When this module is loaded, CORS Anywhere is started.
// Then, a request is generated to warm up the server (just in case).
// Then the base URL of CORS Anywhere is sent to the parent process.
// ...
// When the parent process is done, it sends an empty message to this child
// process, which in turn records the change in used heap space.
// The difference in heap space is finally sent back to the parent process.
// ...
// The parent process should then kill this child.
process.on('uncaughtException', function(e) {
console.error('Uncaught exception in child process: ' + e);
console.error(e.stack);
process.exit(-1);
});
// Invoke memoryUsage() without using its result to make sure that any internal
// datastructures that supports memoryUsage() is initialized and won't pollute
// the memory usage measurement later on.
process.memoryUsage();
var heapUsedStart = 0;
function getMemoryUsage(callback) {
// Note: Requires --expose-gc
// 6 is the minimum amount of gc() calls before calling gc() again does not
// reduce memory any more.
for (var i = 0; i < 6; ++i) {
global.gc();
}
callback(process.memoryUsage().heapUsed);
}
var server;
if (process.argv.indexOf('use-http-instead-of-cors-anywhere') >= 0) {
server = require('http').createServer(function(req, res) { res.end(); });
} else {
server = require('../').createServer();
}
server.listen(0, function() {
// Perform 1 request to warm up.
require('http').get({
hostname: '127.0.0.1',
port: server.address().port,
path: '/http://invalid:99999',
agent: false,
}, function() {
notifyParent();
});
function notifyParent() {
getMemoryUsage(function(usage) {
heapUsedStart = usage;
process.send('http://127.0.0.1:' + server.address().port + '/');
});
}
});
process.once('message', function() {
getMemoryUsage(function(heapUsedEnd) {
var delta = heapUsedEnd - heapUsedStart;
process.send(delta);
});
});