Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve error message during weval pull failure #12

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
84 changes: 47 additions & 37 deletions npm/weval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ 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';

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

const TAG = "v0.3.2";
Expand All @@ -31,12 +33,18 @@ async function getWeval() {
}

async function getJSON(url) {
let response = await fetch(url);
if (!response.ok) {
console.error(`Bad response from ${url}`);
process.exit(1);
let resp;
try {
resp = await fetch(url);
if (!resp.ok) {
throw new Error("non 2xx response code");
}
return response.json();
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();
Expand All @@ -46,40 +54,42 @@ async function getWeval() {
const exeDir = join(__dirname, platformName);
const exe = join(exeDir, `weval${exeSuffix}`);

if (!existsSync(exe)) {
await mkdir(exeDir, { recursive: true });
// If we already have the executable installed, then return it
if (existsSync(exe)) {
return exe;
}

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 });
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();

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 (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}`,
});

return exe;
}
Expand Down
Loading