|
|
|
@ -26,33 +26,47 @@ const server = http.createServer((req, res) => {
|
|
|
|
|
res.end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const jobId = generateId();
|
|
|
|
|
let body: Uint8Array[] = [];
|
|
|
|
|
|
|
|
|
|
switch (req.url) {
|
|
|
|
|
case '/run':
|
|
|
|
|
const jobId = generateId();
|
|
|
|
|
const code = 'echo a';
|
|
|
|
|
const image = 'ghcr.io/moshell-lang/moshell:master';
|
|
|
|
|
const buffer = Buffer.allocUnsafe(jobId.length + image.length + code.length + 8);
|
|
|
|
|
buffer.write(jobId, 0);
|
|
|
|
|
buffer.writeUInt32BE(image.length, jobId.length);
|
|
|
|
|
buffer.writeUInt32BE(code.length, jobId.length + 4);
|
|
|
|
|
buffer.write(image, jobId.length + 8);
|
|
|
|
|
buffer.write(code, jobId.length + 8 + image.length);
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
|
Connection: 'keep-alive',
|
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
...CORS,
|
|
|
|
|
});
|
|
|
|
|
sender.send(buffer).then(() => {
|
|
|
|
|
res.write('event: connected\n');
|
|
|
|
|
res.write(`data: ${jobId}\n`);
|
|
|
|
|
res.write('id: 0\n\n');
|
|
|
|
|
});
|
|
|
|
|
req.on('close', () => {
|
|
|
|
|
res.end('OK');
|
|
|
|
|
delete clients[jobId];
|
|
|
|
|
});
|
|
|
|
|
clients[jobId] = res;
|
|
|
|
|
req
|
|
|
|
|
.on('data', chunk => {
|
|
|
|
|
body.push(chunk);
|
|
|
|
|
})
|
|
|
|
|
.on('end', () => {
|
|
|
|
|
let params = JSON.parse(Buffer.concat(body).toString());
|
|
|
|
|
|
|
|
|
|
const code = params.code;
|
|
|
|
|
const image = 'ghcr.io/moshell-lang/moshell:master';
|
|
|
|
|
const buffer = Buffer.allocUnsafe(jobId.length + image.length + code.length + 8);
|
|
|
|
|
buffer.write(jobId, 0);
|
|
|
|
|
buffer.writeUInt32BE(image.length, jobId.length);
|
|
|
|
|
buffer.writeUInt32BE(code.length, jobId.length + 4);
|
|
|
|
|
buffer.write(image, jobId.length + 8);
|
|
|
|
|
buffer.write(code, jobId.length + 8 + image.length);
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
|
Connection: 'keep-alive',
|
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
...CORS,
|
|
|
|
|
});
|
|
|
|
|
sender.send(buffer).then(() => {
|
|
|
|
|
res.write('event: connected\n');
|
|
|
|
|
res.write(`data: ${jobId}\n`);
|
|
|
|
|
res.write('id: 0\n\n');
|
|
|
|
|
});
|
|
|
|
|
//res.end(body);
|
|
|
|
|
})
|
|
|
|
|
.on('close', () => {
|
|
|
|
|
res.end('OK');
|
|
|
|
|
delete clients[jobId];
|
|
|
|
|
});
|
|
|
|
|
clients[jobId] = res;
|
|
|
|
|
|
|
|
|
|
//res.end()
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
res.writeHead(404, CORS);
|
|
|
|
|