Skip to content

Commit 4c3059f

Browse files
authored
Merge pull request #14 from bytecodealliance/fixup-release-downloads
fix: release download urls without ratelimiting
2 parents 0287122 + 5294ced commit 4c3059f

File tree

1 file changed

+49
-74
lines changed

1 file changed

+49
-74
lines changed

npm/weval/index.js

Lines changed: 49 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,72 @@
11
import { endianness } from "node:os";
2-
import { fileURLToPath } from 'node:url';
3-
import { dirname, join, parse } from 'node:path';
2+
import { fileURLToPath } from "node:url";
3+
import { dirname, join, parse } from "node:path";
44
import { platform, arch } from "node:process";
55
import { mkdir } from "node:fs/promises";
66
import { existsSync } from "node:fs";
77

8-
import decompress from 'decompress';
9-
import decompressUnzip from 'decompress-unzip';
10-
import decompressTar from 'decompress-tar';
11-
import xz from '@napi-rs/lzma/xz';
8+
import decompress from "decompress";
9+
import decompressUnzip from "decompress-unzip";
10+
import decompressTar from "decompress-tar";
11+
import xz from "@napi-rs/lzma/xz";
1212

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

1515
const TAG = "v0.3.2";
1616

1717
async function getWeval() {
18-
const knownPlatforms = {
19-
"win32 x64 LE": "x86_64-windows",
20-
"darwin arm64 LE": "aarch64-macos",
21-
"darwin x64 LE": "x86_64-macos",
22-
"linux x64 LE": "x86_64-linux",
23-
"linux arm64 LE": "aarch64-linux",
24-
};
18+
const knownPlatforms = {
19+
"win32 x64 LE": "x86_64-windows",
20+
"darwin arm64 LE": "aarch64-macos",
21+
"darwin x64 LE": "x86_64-macos",
22+
"linux x64 LE": "x86_64-linux",
23+
"linux arm64 LE": "aarch64-linux",
24+
};
2525

26-
function getPlatformName() {
27-
let platformKey = `${platform} ${arch} ${endianness()}`;
26+
function getPlatformName() {
27+
let platformKey = `${platform} ${arch} ${endianness()}`;
2828

29-
if (platformKey in knownPlatforms) {
30-
return knownPlatforms[platformKey];
31-
}
32-
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."`);
29+
if (platformKey in knownPlatforms) {
30+
return knownPlatforms[platformKey];
3331
}
32+
throw new Error(
33+
`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."`
34+
);
35+
}
3436

35-
async function getJSON(url) {
36-
let resp;
37-
try {
38-
resp = await fetch(url);
39-
if (!resp.ok) {
40-
throw new Error("non 2xx response code");
41-
}
42-
return resp.json();
43-
} catch (err) {
44-
const errMsg = err?.toString() ?? 'unknown error';
45-
console.error(`failed to fetch JSON from URL [${url}] (status ${resp?.status}): ${errMsg}`);
46-
process.exit(1);
47-
}
48-
}
49-
50-
const platformName = getPlatformName();
51-
const assetSuffix = (platform == 'win32') ? 'zip' : 'tar.xz';
52-
const exeSuffix = (platform == 'win32') ? '.exe' : '';
37+
const platformName = getPlatformName();
38+
const assetSuffix = platform == "win32" ? "zip" : "tar.xz";
39+
const exeSuffix = platform == "win32" ? ".exe" : "";
5340

54-
const exeDir = join(__dirname, platformName);
55-
const exe = join(exeDir, `weval${exeSuffix}`);
41+
const exeDir = join(__dirname, platformName);
42+
const exe = join(exeDir, `weval${exeSuffix}`);
5643

57-
// If we already have the executable installed, then return it
58-
if (existsSync(exe)) {
59-
return exe;
60-
}
44+
// If we already have the executable installed, then return it
45+
if (existsSync(exe)) {
46+
return exe;
47+
}
6148

62-
await mkdir(exeDir, { recursive: true });
63-
let repoBaseURL = `https://api.github.com/repos/bytecodealliance/weval`;
64-
let response = await getJSON(`${repoBaseURL}/releases/tags/${TAG}`);
65-
let id = response.id;
66-
let assets = await getJSON(`${repoBaseURL}/releases/${id}/assets`);
67-
let releaseAsset = `weval-${TAG}-${platformName}.${assetSuffix}`;
68-
let asset = assets.find(asset => asset.name === releaseAsset);
69-
if (!asset) {
70-
console.error(`Can't find an asset named ${releaseAsset}`);
71-
process.exit(1);
72-
}
73-
let data = await fetch(asset.browser_download_url);
74-
if (!data.ok) {
75-
console.error(`Error downloading ${asset.browser_download_url}`);
76-
process.exit(1);
77-
}
78-
let buf = await data.arrayBuffer();
49+
await mkdir(exeDir, { recursive: true });
50+
const downloadUrl = `https://github.com/bytecodealliance/weval/releases/download/${TAG}/weval-${TAG}-${platformName}.${assetSuffix}`;
51+
let data = await fetch(downloadUrl);
52+
if (!data.ok) {
53+
console.error(`Error downloading ${downloadUrl}`);
54+
process.exit(1);
55+
}
56+
let buf = await data.arrayBuffer();
7957

80-
if (releaseAsset.endsWith('.xz')) {
81-
buf = await xz.decompress(new Uint8Array(buf));
82-
}
83-
await decompress(Buffer.from(buf), exeDir, {
84-
// Remove the leading directory from the extracted file.
85-
strip: 1,
86-
plugins: [
87-
decompressUnzip(),
88-
decompressTar()
89-
],
90-
// Only extract the binary file and nothing else
91-
filter: file => parse(file.path).base === `weval${exeSuffix}`,
92-
});
58+
if (downloadUrl.endsWith(".xz")) {
59+
buf = await xz.decompress(new Uint8Array(buf));
60+
}
61+
await decompress(Buffer.from(buf), exeDir, {
62+
// Remove the leading directory from the extracted file.
63+
strip: 1,
64+
plugins: [decompressUnzip(), decompressTar()],
65+
// Only extract the binary file and nothing else
66+
filter: (file) => parse(file.path).base === `weval${exeSuffix}`,
67+
});
9368

94-
return exe;
69+
return exe;
9570
}
9671

9772
export default getWeval;

0 commit comments

Comments
 (0)