-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.js
More file actions
118 lines (103 loc) · 3.27 KB
/
Copy pathhtml.js
File metadata and controls
118 lines (103 loc) · 3.27 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
117
118
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 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>
`;
const respond = async (req, res) => {
console.log(req.url, req.rawHeaders);
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 pendingResults = new Map();
const handleData = (data) => {
const message = JSON.parse(data);
const pending = pendingResults.get(message.id);
if (!pending) {
return;
}
pendingResults.delete(message.id);
if (message.error) {
pending.reject(message.error);
} else {
pending.resolve(message.result);
}
};
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 = new Promise((resolve, reject) => {
pendingResults.set(id, { resolve, reject });
});
broadcastMessage({ command: 'eval', code, sourceURL, id });
return receiveMessagePromise;
};
return evaluate;
};
module.exports = { openLocalfileInBrowser, webEvaluator };