|
| 1 | +#!/usr/bin/env node |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +const https = require("https"); |
| 5 | +const http = require("http"); |
| 6 | +const fs = require("fs"); |
| 7 | +const path = require("path"); |
| 8 | +const { execSync } = require("child_process"); |
| 9 | +const PLATFORM_MAP = { |
| 10 | + linux: "linux", |
| 11 | + darwin: "darwin", |
| 12 | + win32: "windows", |
| 13 | +}; |
| 14 | + |
| 15 | +const ARCH_MAP = { |
| 16 | + x64: "amd64", |
| 17 | + arm64: "arm64", |
| 18 | +}; |
| 19 | + |
| 20 | +function getPackageVersion() { |
| 21 | + const pkg = JSON.parse( |
| 22 | + fs.readFileSync(path.join(__dirname, "package.json"), "utf8") |
| 23 | + ); |
| 24 | + return pkg.version; |
| 25 | +} |
| 26 | + |
| 27 | +function getDownloadUrl(version, os, arch) { |
| 28 | + const ext = os === "windows" ? "zip" : "tar.gz"; |
| 29 | + return `https://github.com/policylayer/intercept/releases/download/v${version}/intercept-${os}-${arch}.${ext}`; |
| 30 | +} |
| 31 | + |
| 32 | +function fetch(url, redirects = 0) { |
| 33 | + return new Promise((resolve, reject) => { |
| 34 | + if (redirects > 10) { |
| 35 | + return reject(new Error("Too many redirects")); |
| 36 | + } |
| 37 | + const client = url.startsWith("https") ? https : http; |
| 38 | + client |
| 39 | + .get(url, { headers: { "User-Agent": "policylayer-intercept-npm" } }, (res) => { |
| 40 | + if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { |
| 41 | + return fetch(res.headers.location, redirects + 1).then(resolve, reject); |
| 42 | + } |
| 43 | + if (res.statusCode !== 200) { |
| 44 | + return reject(new Error(`Download failed: HTTP ${res.statusCode} from ${url}`)); |
| 45 | + } |
| 46 | + const chunks = []; |
| 47 | + res.on("data", (chunk) => chunks.push(chunk)); |
| 48 | + res.on("end", () => resolve(Buffer.concat(chunks))); |
| 49 | + res.on("error", reject); |
| 50 | + }) |
| 51 | + .on("error", reject); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +function extractTarGz(buffer, destDir) { |
| 56 | + const tmpFile = path.join(destDir, "_tmp.tar.gz"); |
| 57 | + fs.writeFileSync(tmpFile, buffer); |
| 58 | + try { |
| 59 | + execSync(`tar -xzf "${tmpFile}" -C "${destDir}"`, { stdio: "ignore" }); |
| 60 | + } finally { |
| 61 | + fs.unlinkSync(tmpFile); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function extractZip(buffer, destDir) { |
| 66 | + const tmpFile = path.join(destDir, "_tmp.zip"); |
| 67 | + fs.writeFileSync(tmpFile, buffer); |
| 68 | + try { |
| 69 | + // PowerShell is available on all supported Windows versions |
| 70 | + execSync( |
| 71 | + `powershell -NoProfile -Command "Expand-Archive -Force '${tmpFile}' '${destDir}'"`, |
| 72 | + { stdio: "ignore" } |
| 73 | + ); |
| 74 | + } finally { |
| 75 | + fs.unlinkSync(tmpFile); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +async function main() { |
| 80 | + const platform = PLATFORM_MAP[process.platform]; |
| 81 | + const arch = ARCH_MAP[process.arch]; |
| 82 | + |
| 83 | + if (!platform || !arch) { |
| 84 | + console.error( |
| 85 | + `Unsupported platform: ${process.platform}-${process.arch}` |
| 86 | + ); |
| 87 | + process.exit(1); |
| 88 | + } |
| 89 | + |
| 90 | + const version = getPackageVersion(); |
| 91 | + if (version === "0.0.0") { |
| 92 | + console.error( |
| 93 | + "Package version is 0.0.0. Binary download is only available for published releases." |
| 94 | + ); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | + |
| 98 | + const url = getDownloadUrl(version, platform, arch); |
| 99 | + const binDir = path.join(__dirname, "bin"); |
| 100 | + const binaryName = platform === "windows" ? "intercept.exe" : "intercept"; |
| 101 | + const binaryPath = path.join(binDir, binaryName); |
| 102 | + |
| 103 | + console.log(`Downloading intercept v${version} for ${platform}/${arch}...`); |
| 104 | + |
| 105 | + try { |
| 106 | + const buffer = await fetch(url); |
| 107 | + |
| 108 | + fs.mkdirSync(binDir, { recursive: true }); |
| 109 | + |
| 110 | + if (platform === "windows") { |
| 111 | + extractZip(buffer, binDir); |
| 112 | + } else { |
| 113 | + extractTarGz(buffer, binDir); |
| 114 | + } |
| 115 | + |
| 116 | + fs.chmodSync(binaryPath, 0o755); |
| 117 | + console.log(`Installed intercept to ${binaryPath}`); |
| 118 | + } catch (err) { |
| 119 | + console.error(`Failed to install intercept: ${err.message}`); |
| 120 | + process.exit(1); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +main(); |
0 commit comments