From 2bf7d3990fa50a83366fa64558e6e15023d361b5 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 20 Nov 2024 09:56:24 -0800 Subject: [PATCH 1/3] fix: release download urls without ratelimiting --- npm/weval/index.js | 49 +++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/npm/weval/index.js b/npm/weval/index.js index 9a23eba..66c1982 100644 --- a/npm/weval/index.js +++ b/npm/weval/index.js @@ -60,36 +60,27 @@ async function getWeval() { } 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(); + 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; } From e20a9d53902ee21b9972c80d5b0986cb6e2fc943 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 20 Nov 2024 09:57:36 -0800 Subject: [PATCH 2/3] fixup formatting --- npm/weval/index.js | 125 +++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 62 deletions(-) diff --git a/npm/weval/index.js b/npm/weval/index.js index 66c1982..e685f02 100644 --- a/npm/weval/index.js +++ b/npm/weval/index.js @@ -1,88 +1,89 @@ 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); + 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 }); - 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(); + 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 (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}`, - }); + 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; From 5294ced4fbbf0562fe4bbc4ee27fab42cc02ed52 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 20 Nov 2024 09:58:52 -0800 Subject: [PATCH 3/3] remove unused function --- npm/weval/index.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/npm/weval/index.js b/npm/weval/index.js index e685f02..66f4bee 100644 --- a/npm/weval/index.js +++ b/npm/weval/index.js @@ -34,23 +34,6 @@ async function getWeval() { ); } - 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" : "";