@earendil-works/gondolin 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,165 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SandboxController = void 0;
4
- const events_1 = require("events");
5
- const child_process_1 = require("child_process");
6
- class SandboxController extends events_1.EventEmitter {
7
- constructor(config) {
8
- super();
9
- this.config = config;
10
- this.child = null;
11
- this.state = "stopped";
12
- this.restartTimer = null;
13
- this.manualStop = false;
14
- }
15
- getState() {
16
- return this.state;
17
- }
18
- async start() {
19
- if (this.child)
20
- return;
21
- this.manualStop = false;
22
- this.setState("starting");
23
- const args = buildQemuArgs(this.config);
24
- this.child = (0, child_process_1.spawn)(this.config.qemuPath, args, {
25
- stdio: ["ignore", "pipe", "pipe"],
26
- });
27
- this.child.stdout?.on("data", (chunk) => {
28
- this.emit("log", chunk.toString());
29
- });
30
- this.child.stderr?.on("data", (chunk) => {
31
- this.emit("log", chunk.toString());
32
- });
33
- this.child.on("spawn", () => {
34
- this.setState("running");
35
- });
36
- this.child.on("exit", (code, signal) => {
37
- this.child = null;
38
- this.setState("stopped");
39
- this.emit("exit", { code, signal });
40
- if (this.manualStop) {
41
- this.manualStop = false;
42
- return;
43
- }
44
- if (this.config.autoRestart) {
45
- this.scheduleRestart();
46
- }
47
- });
48
- }
49
- async stop() {
50
- if (!this.child)
51
- return;
52
- const child = this.child;
53
- this.child = null;
54
- this.manualStop = true;
55
- if (this.restartTimer) {
56
- clearTimeout(this.restartTimer);
57
- this.restartTimer = null;
58
- }
59
- child.kill("SIGTERM");
60
- await new Promise((resolve) => {
61
- const timeout = setTimeout(() => {
62
- child.kill("SIGKILL");
63
- }, 3000);
64
- child.once("exit", () => {
65
- clearTimeout(timeout);
66
- resolve();
67
- });
68
- });
69
- this.setState("stopped");
70
- }
71
- async restart() {
72
- await this.stop();
73
- await this.start();
74
- }
75
- scheduleRestart() {
76
- if (this.restartTimer)
77
- return;
78
- this.restartTimer = setTimeout(() => {
79
- this.restartTimer = null;
80
- void this.start();
81
- }, 1000);
82
- }
83
- setState(state) {
84
- if (this.state === state)
85
- return;
86
- this.state = state;
87
- this.emit("state", state);
88
- }
89
- }
90
- exports.SandboxController = SandboxController;
91
- function buildQemuArgs(config) {
92
- const args = [
93
- "-nodefaults",
94
- "-no-reboot",
95
- "-m",
96
- config.memory,
97
- "-smp",
98
- String(config.cpus),
99
- "-kernel",
100
- config.kernelPath,
101
- "-initrd",
102
- config.initrdPath,
103
- "-append",
104
- config.append,
105
- "-nographic",
106
- ];
107
- const targetArch = detectTargetArch(config);
108
- const machineType = config.machineType ?? selectMachineType(targetArch);
109
- args.push("-machine", machineType);
110
- const accel = config.accel ?? selectAccel();
111
- if (accel)
112
- args.push("-accel", accel);
113
- const cpu = config.cpu ?? selectCpu();
114
- if (cpu)
115
- args.push("-cpu", cpu);
116
- if (config.console === "none") {
117
- args.push("-serial", "none");
118
- }
119
- else {
120
- args.push("-serial", "stdio");
121
- }
122
- args.push("-object", "rng-random,filename=/dev/urandom,id=rng0");
123
- args.push("-device", "virtio-rng-pci,rng=rng0");
124
- args.push("-chardev", `socket,id=virtiocon0,path=${config.virtioSocketPath},server=off`);
125
- args.push("-device", "virtio-serial-pci,id=virtio-serial0");
126
- args.push("-device", "virtserialport,chardev=virtiocon0,name=virtio-port,bus=virtio-serial0.0");
127
- if (config.netSocketPath) {
128
- args.push("-netdev", `stream,id=net0,server=off,addr.type=unix,addr.path=${config.netSocketPath}`);
129
- const mac = config.netMac ?? "02:00:00:00:00:01";
130
- args.push("-device", `virtio-net-pci,netdev=net0,mac=${mac}`);
131
- }
132
- return args;
133
- }
134
- function detectTargetArch(config) {
135
- const qemuPath = config.qemuPath.toLowerCase();
136
- if (qemuPath.includes("aarch64") || qemuPath.includes("arm64")) {
137
- return "arm64";
138
- }
139
- if (qemuPath.includes("x86_64") || qemuPath.includes("x64")) {
140
- return "x64";
141
- }
142
- return process.arch;
143
- }
144
- function selectMachineType(targetArch) {
145
- if (process.platform === "linux" && targetArch === "x64") {
146
- return "microvm";
147
- }
148
- if (targetArch === "arm64") {
149
- return "virt";
150
- }
151
- return "q35";
152
- }
153
- function selectAccel() {
154
- if (process.platform === "linux")
155
- return "kvm";
156
- if (process.platform === "darwin")
157
- return "hvf";
158
- return "tcg";
159
- }
160
- function selectCpu() {
161
- if (process.platform === "linux" || process.platform === "darwin") {
162
- return "host";
163
- }
164
- return "max";
165
- }