|
| 1 | +import * as child_process from "node:child_process"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as http from "node:http"; |
| 4 | +import * as path from "node:path"; |
| 5 | + |
| 6 | +interface LocalStdServer { |
| 7 | + process: child_process.ChildProcess; |
| 8 | + port: number; |
| 9 | + baseUrl: string; |
| 10 | +} |
| 11 | + |
| 12 | +let activeServer: LocalStdServer | null = null; |
| 13 | + |
| 14 | +function findZigExecutable(): string { |
| 15 | + const zigPath = process.env.ZIG_PATH || "zig"; |
| 16 | + try { |
| 17 | + const result = child_process.execSync(`${zigPath} version`, { encoding: "utf8" }); |
| 18 | + if (result.includes("dev") || /\d+\.\d+\.\d+/.test(result)) { |
| 19 | + return zigPath; |
| 20 | + } |
| 21 | + } catch { |
| 22 | + // Continue to fallback |
| 23 | + } |
| 24 | + |
| 25 | + const commonPaths = [ |
| 26 | + "/usr/local/bin/zig", |
| 27 | + "/usr/bin/zig", |
| 28 | + "/opt/homebrew/bin/zig", |
| 29 | + "/opt/zig/zig", |
| 30 | + path.join(process.env.HOME || "", ".local/bin/zig"), |
| 31 | + ]; |
| 32 | + |
| 33 | + for (const p of commonPaths) { |
| 34 | + if (fs.existsSync(p)) { |
| 35 | + try { |
| 36 | + child_process.execSync(`${p} version`, { encoding: "utf8" }); |
| 37 | + return p; |
| 38 | + } catch { |
| 39 | + // Continue checking |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return "zig"; |
| 45 | +} |
| 46 | + |
| 47 | +export function getZigVersion(): string { |
| 48 | + const zigPath = findZigExecutable(); |
| 49 | + try { |
| 50 | + const result = child_process.execSync(`${zigPath} version`, { encoding: "utf8" }); |
| 51 | + return result.trim(); |
| 52 | + } catch (error) { |
| 53 | + throw new Error(`Failed to get Zig version: ${error}`); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +export async function startLocalStdServer(): Promise<LocalStdServer> { |
| 58 | + if (activeServer) { |
| 59 | + return activeServer; |
| 60 | + } |
| 61 | + |
| 62 | + const zigPath = findZigExecutable(); |
| 63 | + |
| 64 | + return new Promise((resolve, reject) => { |
| 65 | + const stdProcess = child_process.spawn(zigPath, ["std", "--no-open-browser"], { |
| 66 | + stdio: ["ignore", "pipe", "pipe"], |
| 67 | + }); |
| 68 | + |
| 69 | + let output = ""; |
| 70 | + let errorOutput = ""; |
| 71 | + let resolved = false; |
| 72 | + |
| 73 | + const timeout = setTimeout(() => { |
| 74 | + if (!resolved) { |
| 75 | + stdProcess.kill(); |
| 76 | + reject(new Error("Timeout waiting for Zig std server to start")); |
| 77 | + } |
| 78 | + }, 10000); |
| 79 | + |
| 80 | + stdProcess.stdout?.on("data", (data) => { |
| 81 | + output += data.toString(); |
| 82 | + |
| 83 | + // Match patterns like "http://127.0.0.1:43695/" |
| 84 | + const match = output.match(/(http:\/\/[0-9.]+:[0-9]+)/); |
| 85 | + if (match && !resolved) { |
| 86 | + resolved = true; |
| 87 | + clearTimeout(timeout); |
| 88 | + |
| 89 | + const baseUrl = match[1]; |
| 90 | + const port = parseInt(baseUrl.split(":").pop() || "43695"); |
| 91 | + |
| 92 | + activeServer = { |
| 93 | + process: stdProcess, |
| 94 | + port, |
| 95 | + baseUrl, |
| 96 | + }; |
| 97 | + |
| 98 | + resolve(activeServer); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + stdProcess.stderr?.on("data", (data) => { |
| 103 | + errorOutput += data.toString(); |
| 104 | + }); |
| 105 | + |
| 106 | + stdProcess.on("error", (error) => { |
| 107 | + clearTimeout(timeout); |
| 108 | + if (!resolved) { |
| 109 | + resolved = true; |
| 110 | + reject(new Error(`Failed to start Zig std server: ${error.message}`)); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + stdProcess.on("exit", (code) => { |
| 115 | + clearTimeout(timeout); |
| 116 | + if (!resolved) { |
| 117 | + resolved = true; |
| 118 | + reject(new Error(`Zig std server exited with code ${code}: ${errorOutput}`)); |
| 119 | + } |
| 120 | + activeServer = null; |
| 121 | + }); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +export function stopLocalStdServer(): void { |
| 126 | + if (activeServer) { |
| 127 | + activeServer.process.kill(); |
| 128 | + activeServer = null; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export async function fetchFromLocalServer(path: string): Promise<string> { |
| 133 | + const server = await startLocalStdServer(); |
| 134 | + const url = `${server.baseUrl}${path}`; |
| 135 | + |
| 136 | + return new Promise((resolve, reject) => { |
| 137 | + http.get(url, (res) => { |
| 138 | + let data = ""; |
| 139 | + |
| 140 | + res.on("data", (chunk) => { |
| 141 | + data += chunk; |
| 142 | + }); |
| 143 | + |
| 144 | + res.on("end", () => { |
| 145 | + if (res.statusCode === 200) { |
| 146 | + resolve(data); |
| 147 | + } else { |
| 148 | + reject(new Error(`HTTP ${res.statusCode}: ${data}`)); |
| 149 | + } |
| 150 | + }); |
| 151 | + }).on("error", (error) => { |
| 152 | + reject(error); |
| 153 | + }); |
| 154 | + }); |
| 155 | +} |
| 156 | + |
| 157 | +export async function getLocalStdSources(): Promise<Uint8Array<ArrayBuffer>> { |
| 158 | + const server = await startLocalStdServer(); |
| 159 | + const url = `${server.baseUrl}/sources.tar`; |
| 160 | + |
| 161 | + return new Promise((resolve, reject) => { |
| 162 | + http.get(url, (res) => { |
| 163 | + const chunks: Buffer[] = []; |
| 164 | + |
| 165 | + res.on("data", (chunk) => { |
| 166 | + chunks.push(chunk); |
| 167 | + }); |
| 168 | + |
| 169 | + res.on("end", () => { |
| 170 | + if (res.statusCode === 200) { |
| 171 | + const buffer = Buffer.concat(chunks); |
| 172 | + resolve(new Uint8Array(buffer)); |
| 173 | + } else { |
| 174 | + reject(new Error(`Failed to fetch sources.tar: HTTP ${res.statusCode}`)); |
| 175 | + } |
| 176 | + }); |
| 177 | + }).on("error", (error) => { |
| 178 | + reject(error); |
| 179 | + }); |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | + |
| 184 | +process.on("exit", stopLocalStdServer); |
| 185 | +process.on("SIGINT", stopLocalStdServer); |
| 186 | +process.on("SIGTERM", stopLocalStdServer); |
0 commit comments