Initial Commit
This commit is contained in:
59
packages/devtools/tooling/javy-cli-linux-x64/postinstall.mjs
Normal file
59
packages/devtools/tooling/javy-cli-linux-x64/postinstall.mjs
Normal file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* postinstall — downloads Javy v8.1.1 prebuilt binary for linux-x64
|
||||
* 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 = "4bec224e1d51827808fd063541a2b4d94cf474744636b692be63e7a29bec83a6";
|
||||
const URL = "https://github.com/bytecodealliance/javy/releases/download/v8.1.1/javy-x86_64-linux-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-linux-x64] 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-linux-x64] downloading", URL);
|
||||
const res = await fetch(URL, { redirect: "follow" });
|
||||
if (!res.ok) {
|
||||
throw new Error("[javy-cli-linux-x64] 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-linux-x64] 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-linux-x64] installed", BIN_PATH, "(" + out.length + " bytes)");
|
||||
}
|
||||
|
||||
main().catch((err) => { console.error(err); process.exit(1); });
|
||||
Reference in New Issue
Block a user