-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.js
125 lines (108 loc) · 3.36 KB
/
html.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const http = require('node:http');
const { WebSocket, WebSocketServer } = require('ws');
const { promisify } = require('node:util');
const execAsync = promisify(require('node:child_process').exec);
const fs = require('node:fs/promises');
const path = require('node:path');
const { Channel } = require('queueable');
const HTTP_PORT = 8000;
const WEBSOCKET_PORT = 8001;
const openUrlInBrowser = url => execAsync(`open '${url}'`);
const openLocalfileInBrowser = file =>
openUrlInBrowser(`http://localhost:${HTTP_PORT}${path.resolve(file)}`);
const scriptToInject = `
<script>
var module = {};
</script>
<script src="${path.join(__dirname, 'evaluator.js')}"></script>
<script>
const evalFunction = module.exports.scopedEvaluator();
const socket = new WebSocket("ws://localhost:${WEBSOCKET_PORT}/ws-hakk");
socket.addEventListener("message", e => {
const data = JSON.parse(e.data);
if (data.command === "eval") {
const { code, id } = data;
try {
const result = evalFunction({code});
console.log({data, result})
socket.send(JSON.stringify({id, result}));
} catch (e) {
console.log({data, e})
socket.send(JSON.stringify({id, error: e.toString()}));
}
}
});
</script>
`;
let lastRequest;
const respond = async (req, res) => {
console.log(req.url, req.rawHeaders);
lastRequest = req;
const pathname = req.url;
let contentType = '';
if (pathname.endsWith('html') || pathname.endsWith('htm')) {
contentType = 'text/html';
} else if (pathname.endsWith('js') || pathname.endsWith('jsm')) {
contentType = 'application/js';
} else if (pathname.endsWith('json')) {
contentType = 'application/json';
}
try {
let fileContents = await fs.readFile(pathname);
if (contentType === 'text/html') {
fileContents = scriptToInject + fileContents;
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(fileContents);
} catch (e) {
res.writeHead(404);
res.end('');
}
};
const webEvaluator = () => {
const server = http.createServer((...args) => respond(...args));
server.listen(HTTP_PORT);
console.log('about to create new WebSocketServer');
const wss = new WebSocketServer({
port: WEBSOCKET_PORT
});
const broadcastMessage = (message) => {
wss.clients.forEach(client => {
if (client.readyState = WebSocket.OPEN) {
client.send(JSON.stringify(message));
}
});
};
const incomingMessages = new Channel();
const handleData = (data) => {
incomingMessages.push(JSON.parse(data));
};
const receiveMessage = async (id) => {
let data;
do {
const message = await incomingMessages.next();
data = message.value;
} while (data.id !== id);
return data;
};
wss.on('connection', (ws) => {
ws.on('error', console.error);
ws.on('message', (data) => handleData(data));
});
let counter = 0;
const evaluate = async ({ code, sourceURL }) => {
++counter;
const id = counter.toString();
const receiveMessagePromise = receiveMessage(id);
broadcastMessage({ command: 'eval', code, sourceURL, id });
const message = await receiveMessagePromise;
const { result, error } = message;
if (error) {
throw error;
} else {
return result;
}
};
return evaluate;
};
module.exports = { openLocalfileInBrowser, webEvaluator };