|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execFileSync } from "child_process"; |
| 3 | +import fs from "fs"; |
| 4 | +import https from "https"; |
| 5 | +import os from "os"; |
| 6 | +import path from "path"; |
| 7 | +import { fileURLToPath } from "url"; |
| 8 | + |
| 9 | +const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url)); |
| 10 | +const BUN_VERSION = "1.3.13"; |
| 11 | +const ENTRY_SCRIPT = "execution.js"; |
| 12 | +const REQUEST_TIMEOUT = 120_000; |
| 13 | + |
| 14 | +const isAlpineOrMusl = () => { |
| 15 | + try { |
| 16 | + const lddOut = execFileSync("ldd", ["--version"], { |
| 17 | + stdio: ["ignore", "pipe", "pipe"], |
| 18 | + }).toString(); |
| 19 | + if (lddOut.includes("musl")) return true; |
| 20 | + } catch {} |
| 21 | + try { |
| 22 | + return fs.readFileSync("/etc/os-release", "utf8").includes("Alpine"); |
| 23 | + } catch { |
| 24 | + return false; |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +const PLATFORM_MAP = { |
| 29 | + "linux-arm64": () => "bun-linux-aarch64", |
| 30 | + "linux-x64": () => |
| 31 | + isAlpineOrMusl() ? "bun-linux-x64-musl-baseline" : "bun-linux-x64-baseline", |
| 32 | + "darwin-arm64": () => "bun-darwin-aarch64", |
| 33 | + "darwin-x64": () => "bun-darwin-x64", |
| 34 | + "win32-arm64": () => "bun-windows-aarch64", |
| 35 | + "win32-x64": () => "bun-windows-x64-baseline", |
| 36 | +}; |
| 37 | + |
| 38 | +function resolveAsset() { |
| 39 | + const key = `${process.platform}-${process.arch}`; |
| 40 | + const resolver = PLATFORM_MAP[key]; |
| 41 | + if (!resolver) throw new Error(`Unsupported platform/arch: ${key}`); |
| 42 | + return resolver(); |
| 43 | +} |
| 44 | + |
| 45 | +function downloadToFile(url, dest, redirectsLeft = 5) { |
| 46 | + return new Promise((resolve, reject) => { |
| 47 | + const req = https.get( |
| 48 | + url, |
| 49 | + { headers: { "User-Agent": "node" }, timeout: REQUEST_TIMEOUT }, |
| 50 | + (res) => { |
| 51 | + const { statusCode, headers } = res; |
| 52 | + if ([301, 302, 307, 308].includes(statusCode)) { |
| 53 | + res.resume(); |
| 54 | + if (redirectsLeft <= 0) |
| 55 | + return reject(new Error("Too many redirects")); |
| 56 | + return downloadToFile(headers.location, dest, redirectsLeft - 1).then( |
| 57 | + resolve, |
| 58 | + reject, |
| 59 | + ); |
| 60 | + } |
| 61 | + if (statusCode !== 200) { |
| 62 | + res.resume(); |
| 63 | + return reject(new Error(`HTTP ${statusCode} for ${url}`)); |
| 64 | + } |
| 65 | + const file = fs.createWriteStream(dest); |
| 66 | + res.pipe(file); |
| 67 | + file.on("finish", () => file.close(resolve)); |
| 68 | + file.on("error", (err) => { |
| 69 | + fs.unlink(dest, () => reject(err)); |
| 70 | + }); |
| 71 | + }, |
| 72 | + ); |
| 73 | + req.on("error", reject); |
| 74 | + req.on("timeout", () => req.destroy(new Error("Request timed out"))); |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +function hasCommand(cmd, args = ["--version"]) { |
| 79 | + try { |
| 80 | + execFileSync(cmd, args, { stdio: "ignore" }); |
| 81 | + return true; |
| 82 | + } catch { |
| 83 | + return false; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function extractBun(zipPath, entry, outDir) { |
| 88 | + if (hasCommand("unzip", ["-v"])) { |
| 89 | + // -o overwrite, -j junk paths, -q quiet → places binary directly in outDir |
| 90 | + execFileSync("unzip", ["-ojq", zipPath, entry, "-d", outDir], { |
| 91 | + stdio: "inherit", |
| 92 | + }); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (process.platform === "win32" && hasCommand("powershell", ["-Help"])) { |
| 97 | + // Expand-Archive extracts the whole zip preserving structure. |
| 98 | + execFileSync( |
| 99 | + "powershell", |
| 100 | + [ |
| 101 | + "-NoProfile", |
| 102 | + "-NonInteractive", |
| 103 | + "-ExecutionPolicy", |
| 104 | + "Bypass", |
| 105 | + "-Command", |
| 106 | + `Expand-Archive -LiteralPath '${zipPath}' -DestinationPath '${outDir}' -Force`, |
| 107 | + ], |
| 108 | + { stdio: "inherit" }, |
| 109 | + ); |
| 110 | + // Move the binary out of its nested folder so callers find it at outDir/<binName>. |
| 111 | + const nestedPath = path.join(outDir, entry); |
| 112 | + const flatPath = path.join(outDir, path.basename(entry)); |
| 113 | + fs.renameSync(nestedPath, flatPath); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + throw new Error( |
| 118 | + "No supported extractor found. Install `unzip`, or on Windows ensure PowerShell is available.", |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +async function main() { |
| 123 | + if (hasCommand("bun")) return; |
| 124 | + |
| 125 | + const asset = resolveAsset(); |
| 126 | + const isWin = process.platform === "win32"; |
| 127 | + const binName = isWin ? "bun.exe" : "bun"; |
| 128 | + const url = `https://github.com/oven-sh/bun/releases/download/bun-v${BUN_VERSION}/${asset}.zip`; |
| 129 | + |
| 130 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "bun-dl-")); |
| 131 | + const zipPath = path.join(tmpDir, `${asset}.zip`); |
| 132 | + const binPath = path.join(tmpDir, binName); |
| 133 | + const entryScriptPath = path.join(SCRIPT_DIR, ENTRY_SCRIPT); |
| 134 | + |
| 135 | + try { |
| 136 | + await downloadToFile(url, zipPath); |
| 137 | + extractBun(zipPath, `${asset}/${binName}`, tmpDir); |
| 138 | + fs.unlinkSync(zipPath); |
| 139 | + |
| 140 | + if (!isWin) fs.chmodSync(binPath, 0o755); |
| 141 | + execFileSync(binPath, [entryScriptPath], { |
| 142 | + stdio: "inherit", |
| 143 | + cwd: SCRIPT_DIR, |
| 144 | + }); |
| 145 | + } finally { |
| 146 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +main().catch((e) => { |
| 151 | + console.error(e.message); |
| 152 | + process.exit(1); |
| 153 | +}); |
0 commit comments