forked from snaplet/postgres-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
170 lines (149 loc) · 6.54 KB
/
index.html
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!doctype html>
<head>
<title>Postgres in browser</title>
<link rel="stylesheet" href="https://unpkg.com/xterm@4.19.0/css/xterm.css" />
<link rel="stylesheet" href="./styles.css" />
<script src="https://unpkg.com/xterm-addon-fit@0.5.0/lib/xterm-addon-fit.js"></script>
<script src="https://unpkg.com/xterm@4.19.0/lib/xterm.js"></script>
<script src="./lib/libv86.js"></script>
<script>
window.onload = () => {
const baseOptions = {
wasm_path: "./lib/v86.wasm",
memory_size: 128 * 1024 * 1024,
filesystem: {
basefs: "filesystem/filesystem.json",
baseurl: "filesystem/",
},
screen_container: document.getElementById("screen_container"),
serial_container_xtermjs: document.getElementById("terminal"),
network_relay_url: "wss://relay.widgetry.org/",
autostart: true,
disable_keyboard: true,
disable_mouse: true,
disable_speaker: true,
acpi: true,
};
const params = (new URL(document.location)).searchParams;
const boot = params.get("boot") === "true";
if (boot) {
document.getElementById("screen_container").style = 'display:block';
document.getElementById("terminal").style = '';
}
const bzimageUrl = "./filesystem/123ccf58.bin";
const options = {
...baseOptions,
...(boot ? {
bzimage: {
url: bzimageUrl,
},
cmdline: [
"rw",
"root=host9p rootfstype=9p rootflags=version=9p2000.L,trans=virtio,cache=loose quiet acpi=off console=ttyS0 tsc=reliable mitigations=off random.trust_cpu=on nowatchdog page_poison=on",
].join(" "),
bios: {
url: "./bios/seabios.bin",
},
vga_bios: {
url: "./bios/vgabios.bin",
},
} : { initial_state: { url: "./state/v86state.bin.zst" } })
};
var emulator = new V86Starter(options);
if (!boot) {
emulator.add_listener("emulator-ready", function () {
const fitAddon = new FitAddon.FitAddon();
emulator.serial_adapter.term.loadAddon(fitAddon);
fitAddon.fit();
window.addEventListener("resize", () => fitAddon.fit());
// emulator.serial_adapter.term.setOption("allowTransparency", true);
// emulator.serial_adapter.term.setOption("theme", { background: "transparent" });
setTimeout(() => {
emulator.serial0_send('/etc/init.d/S40network restart\n');
emulator.serial0_send('psql -U postgres\n');
emulator.serial0_send('\\! echo "boot_completed"; reset\n');
}, 0);
});
function handleBoot(line) {
if (line.startsWith("postgres=# boot_completed")) {
emulator.remove_listener(handleBoot);
setTimeout(() => {
emulator.serial_adapter.term.focus();
}, 300);
}
}
emulator.add_listener("serial0-output-line", handleBoot);
}
var state;
document.getElementById("save_restore").onclick = async function () {
var button = this;
if (state) {
button.value = "Save state";
await emulator.restore_state(state);
state = undefined;
}
else {
const new_state = await emulator.save_state();
console.log("Saved state of " + new_state.byteLength + " bytes");
button.value = "Restore state";
state = new_state;
}
button.blur();
};
document.getElementById("save_file").onclick = async function () {
const new_state = await emulator.save_state();
var a = document.createElement("a");
a.download = "v86state.bin";
a.href = window.URL.createObjectURL(new Blob([new_state]));
a.dataset.downloadurl = "application/octet-stream:" + a.download + ":" + a.href;
a.click();
this.blur();
};
document.getElementById("restore_file").onchange = function () {
if (this.files.length) {
var filereader = new FileReader();
emulator.stop();
filereader.onload = async function (e) {
await emulator.restore_state(e.target.result);
emulator.run();
};
filereader.readAsArrayBuffer(this.files[0]);
this.value = "";
}
this.blur();
};
document.getElementById("upload_files").onchange = function (e) {
var files = e.target.files;
for (var i = 0; i < files.length; i++) {
var reader = new FileReader();
reader.onload = function (file) {
return function (e) {
var data = (new TextEncoder('UTF-8')).encode(e.target.result);
emulator.create_file("/" + file.name, data);
console.log("uploaded " + file.name);
}
}(files[i]);
reader.readAsText(files[i]);
}
}
};
</script>
</head>
<body>
<div id="screen_container" style="display:none">
<div></div>
<canvas></canvas>
</div>
<div>
<input id="save_restore" type="button" value="Save state">
<input id="save_file" type="button" value="Save state to file">
Restore from file: <input id="restore_file" type="file">
Upload files to the emulator: <input id="upload_files" type="file" multiple>
</div>
<hr />
<div class="container">
<div class="xterminal">
<div id="terminal"></div>
</div>
</div>
</body>