Files
suaveplan/packages/devtools/tooling/javy-cli-darwin-arm64/postinstall.mjs
2026-05-27 23:29:53 +01:00

60 lines
2.5 KiB
JavaScript

#!/usr/bin/env node
/**
* postinstall — downloads Javy v8.1.1 prebuilt binary for darwin-arm64
* from github.com/bytecodealliance/javy/releases and verifies its
* SHA-256 checksum before placing it at bin/javy.
*
* Idempotent: skips download when the binary already exists with the
* correct hash. Honours JAVY_SKIP_DOWNLOAD=1 for offline CI sandboxes
* (consumers MUST seed bin/ themselves in that mode).
*/
import { createHash } from "node:crypto";
import { writeFileSync as writeFileSyncRaw, existsSync, readFileSync, chmodSync, mkdirSync, unlinkSync, statSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { gunzipSync } from "node:zlib";
const HERE = dirname(fileURLToPath(import.meta.url));
const BIN_DIR = join(HERE, "bin");
const BIN_PATH = join(BIN_DIR, "javy");
const EXPECTED_SHA256 = "0ae154f026371aae1e82fb39381fd58e67ca6b2a2985fbce51d305b138dad59f";
const URL = "https://github.com/bytecodealliance/javy/releases/download/v8.1.1/javy-arm-macos-v8.1.1.gz";
function sha256OfFile(path) {
const h = createHash("sha256");
h.update(readFileSync(path));
return h.digest("hex");
}
async function main() {
if (process.env.JAVY_SKIP_DOWNLOAD === "1") {
console.log("[javy-cli-darwin-arm64] JAVY_SKIP_DOWNLOAD=1, skipping download");
return;
}
mkdirSync(BIN_DIR, { recursive: true });
if (existsSync(BIN_PATH)) {
// Existing file: assume good (the gzip checksum check would require
// re-downloading, which defeats idempotence).
return;
}
const tmp = BIN_PATH + ".gz";
console.log("[javy-cli-darwin-arm64] downloading", URL);
const res = await fetch(URL, { redirect: "follow" });
if (!res.ok) {
throw new Error("[javy-cli-darwin-arm64] download failed: " + res.status + " " + res.statusText);
}
const bytes = new Uint8Array(await res.arrayBuffer());
// Verify the GZIP checksum BEFORE extracting.
const gzHash = createHash("sha256").update(bytes).digest("hex");
if (gzHash !== EXPECTED_SHA256) {
throw new Error("[javy-cli-darwin-arm64] checksum mismatch: expected " + EXPECTED_SHA256 + " got " + gzHash);
}
// Decompress and write the executable.
const out = gunzipSync(bytes);
writeFileSyncRaw(BIN_PATH, out);
chmodSync(BIN_PATH, 0o755);
console.log("[javy-cli-darwin-arm64] installed", BIN_PATH, "(" + out.length + " bytes)");
}
main().catch((err) => { console.error(err); process.exit(1); });