-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (93 loc) · 2.79 KB
/
index.js
File metadata and controls
116 lines (93 loc) · 2.79 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const child_process = require('child_process');
const fs = require('fs');
const http = require('http');
const path = require('path');
const baseModulePath = path.join(process.cwd(), process.argv[2]);
let port = parseInt(process.env.PORT) || 15000;
let processPort = port + 1;
const lavaResources = {};
const volcano = http.createServer((request, response) => {
const allocationMatch = request.url.match(/^\/\.lava\/resources(\/.*)$/);
if (allocationMatch) {
return allocateResource(allocationMatch[1], request, response);
}
const resourcesMatch = request.url.match(/^\/\.lava\/(\d{4,})(\/.*)$/);
if (resourcesMatch) {
return resourceFor(parseInt(resourcesMatch[1]), resourcesMatch[2], request, response);
}
if (request.url === '/.lava/lava.js') {
response.writeHead(200, {
'Content-Type': 'application/javascript'
});
response.end(fs.readFileSync(path.join(__dirname, 'lava', 'lava.js')).toString());
}
if (request.url === '/') {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.end(fs.readFileSync(path.join(baseModulePath, 'index.html')).toString());
}
response.writeHead(404);
response.end();
});
volcano.listen(port, function (error) {
if (error) {
console.error(error);
}
else {
console.log(`LAVA • Listening on http://localhost:${port}`);
}
});
function allocateResource(url, request, response) {
function done() {
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.end(
JSON.stringify(lavaResources[url])
);
}
if (url in lavaResources) {
return done();
}
let modulePath = path.join(baseModulePath, url);
console.log(`LAVA • Starting new process '${url}' on port ${processPort}`);
const child = child_process.fork(modulePath, [], {
env: {
PORT: processPort
}
});
lavaResources[url] = {
url,
port: processPort
};
processPort++;
child.on('message', function (message) {
if (message.type === 'ready') {
console.log(`LAVA • ... process '${url}' on port ${processPort} is ready!`);
done();
}
});
}
function resourceFor(port, path, request, response) {
const hostname = 'localhost';
const { headers, method } = request;
headers.host = `${hostname}:${port}`;
const options = {
hostname,
port,
path,
headers,
method
};
console.log('LAVA • Proxy request', options.method, options.port, options.path);
const resourceRequest = http.request(options, function(resourceResponse) {
response.writeHead(resourceResponse.statusCode, resourceResponse.headers);
return resourceResponse.pipe(response);
});
resourceRequest.on('error', function () {
response.writeHead(500);
response.end();
});
request.pipe(resourceRequest);
}