forked from o1-labs/o1js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-in-browser.js
executable file
·87 lines (74 loc) · 2.02 KB
/
run-in-browser.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
#!/usr/bin/env node
import fs from 'node:fs/promises';
import path from 'node:path';
import http from 'node:http';
import minimist from 'minimist';
import { build } from './src/build/build-example.js';
let {
_: [filePath],
} = minimist(process.argv.slice(2));
if (!filePath) {
console.log(`Usage:
./run-in-browser [file]`);
process.exit(0);
}
// copy file into /dist/web
let targetDir = `./dist/web`;
let absPath = await build(filePath, true);
let fileName = path.basename(absPath);
let newPath = `${targetDir}/${fileName}`;
await fs.copyFile(absPath, newPath);
await fs.unlink(absPath);
console.log(`running in the browser: ${newPath}`);
const indexHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="icon" href="data:," />
<title>o1js</title>
<link rel="modulepreload" href="./index.js">
<script type="module" src="./${fileName}">
</script>
</head>
<body>
<div>Check out the console (F12)</div>
</body>
</html>
`;
const port = 8000;
const defaultHeaders = {
'content-type': 'text/html',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
};
const server = http.createServer(async (req, res) => {
let file = '.' + req.url;
if (file === './') file = './index.html';
// console.log('serving', file);
let content;
if (file === './index.html') content = indexHtml;
else {
try {
content = await fs.readFile(path.resolve('./dist/web', file), 'utf8');
} catch (err) {
res.writeHead(404, defaultHeaders);
res.write('<html><body>404</body><html>');
res.end();
return;
}
}
const extension = path.basename(file).split('.').pop();
const contentType = {
html: 'text/html',
js: 'application/javascript',
map: 'application/json',
}[extension];
const headers = { ...defaultHeaders, 'content-type': contentType };
res.writeHead(200, headers);
res.write(content);
res.end();
});
server.listen(port, () => {
console.log(`Server is running on: http://localhost:${port}`);
});