Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 49 additions & 74 deletions npm/weval/index.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,72 @@
import { endianness } from "node:os";
import { fileURLToPath } from 'node:url';
import { dirname, join, parse } from 'node:path';
import { fileURLToPath } from "node:url";
import { dirname, join, parse } from "node:path";
import { platform, arch } from "node:process";
import { mkdir } from "node:fs/promises";
import { existsSync } from "node:fs";

import decompress from 'decompress';
import decompressUnzip from 'decompress-unzip';
import decompressTar from 'decompress-tar';
import xz from '@napi-rs/lzma/xz';
import decompress from "decompress";
import decompressUnzip from "decompress-unzip";
import decompressTar from "decompress-tar";
import xz from "@napi-rs/lzma/xz";

const __dirname = dirname(fileURLToPath(import.meta.url));

const TAG = "v0.3.2";

async function getWeval() {
const knownPlatforms = {
"win32 x64 LE": "x86_64-windows",
"darwin arm64 LE": "aarch64-macos",
"darwin x64 LE": "x86_64-macos",
"linux x64 LE": "x86_64-linux",
"linux arm64 LE": "aarch64-linux",
};
const knownPlatforms = {
"win32 x64 LE": "x86_64-windows",
"darwin arm64 LE": "aarch64-macos",
"darwin x64 LE": "x86_64-macos",
"linux x64 LE": "x86_64-linux",
"linux arm64 LE": "aarch64-linux",
};

function getPlatformName() {
let platformKey = `${platform} ${arch} ${endianness()}`;
function getPlatformName() {
let platformKey = `${platform} ${arch} ${endianness()}`;

if (platformKey in knownPlatforms) {
return knownPlatforms[platformKey];
}
throw new Error(`Unsupported platform: "${platformKey}". "weval does not have a precompiled binary for the platform/architecture you are using. You can open an issue on https://github.com/bytecodealliance/weval/issues to request for your platform/architecture to be included."`);
if (platformKey in knownPlatforms) {
return knownPlatforms[platformKey];
}
throw new Error(
`Unsupported platform: "${platformKey}". "weval does not have a precompiled binary for the platform/architecture you are using. You can open an issue on https://github.com/bytecodealliance/weval/issues to request for your platform/architecture to be included."`
);
}

async function getJSON(url) {
let resp;
try {
resp = await fetch(url);
if (!resp.ok) {
throw new Error("non 2xx response code");
}
return resp.json();
} catch (err) {
const errMsg = err?.toString() ?? 'unknown error';
console.error(`failed to fetch JSON from URL [${url}] (status ${resp?.status}): ${errMsg}`);
process.exit(1);
}
}

const platformName = getPlatformName();
const assetSuffix = (platform == 'win32') ? 'zip' : 'tar.xz';
const exeSuffix = (platform == 'win32') ? '.exe' : '';
const platformName = getPlatformName();
const assetSuffix = platform == "win32" ? "zip" : "tar.xz";
const exeSuffix = platform == "win32" ? ".exe" : "";

const exeDir = join(__dirname, platformName);
const exe = join(exeDir, `weval${exeSuffix}`);
const exeDir = join(__dirname, platformName);
const exe = join(exeDir, `weval${exeSuffix}`);

// If we already have the executable installed, then return it
if (existsSync(exe)) {
return exe;
}
// If we already have the executable installed, then return it
if (existsSync(exe)) {
return exe;
}

await mkdir(exeDir, { recursive: true });
let repoBaseURL = `https://api.github.com/repos/bytecodealliance/weval`;
let response = await getJSON(`${repoBaseURL}/releases/tags/${TAG}`);
let id = response.id;
let assets = await getJSON(`${repoBaseURL}/releases/${id}/assets`);
let releaseAsset = `weval-${TAG}-${platformName}.${assetSuffix}`;
let asset = assets.find(asset => asset.name === releaseAsset);
if (!asset) {
console.error(`Can't find an asset named ${releaseAsset}`);
process.exit(1);
}
let data = await fetch(asset.browser_download_url);
if (!data.ok) {
console.error(`Error downloading ${asset.browser_download_url}`);
process.exit(1);
}
let buf = await data.arrayBuffer();
await mkdir(exeDir, { recursive: true });
const downloadUrl = `https://github.com/bytecodealliance/weval/releases/download/${TAG}/weval-${TAG}-${platformName}.${assetSuffix}`;
let data = await fetch(downloadUrl);
if (!data.ok) {
console.error(`Error downloading ${downloadUrl}`);
process.exit(1);
}
let buf = await data.arrayBuffer();

if (releaseAsset.endsWith('.xz')) {
buf = await xz.decompress(new Uint8Array(buf));
}
await decompress(Buffer.from(buf), exeDir, {
// Remove the leading directory from the extracted file.
strip: 1,
plugins: [
decompressUnzip(),
decompressTar()
],
// Only extract the binary file and nothing else
filter: file => parse(file.path).base === `weval${exeSuffix}`,
});
if (downloadUrl.endsWith(".xz")) {
buf = await xz.decompress(new Uint8Array(buf));
}
await decompress(Buffer.from(buf), exeDir, {
// Remove the leading directory from the extracted file.
strip: 1,
plugins: [decompressUnzip(), decompressTar()],
// Only extract the binary file and nothing else
filter: (file) => parse(file.path).base === `weval${exeSuffix}`,
});

return exe;
return exe;
}

export default getWeval;
Loading