diff --git a/npm/javy-cli/CHANGELOG.md b/npm/javy-cli/CHANGELOG.md index a4af7e83..eeed983d 100644 --- a/npm/javy-cli/CHANGELOG.md +++ b/npm/javy-cli/CHANGELOG.md @@ -6,3 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased + +## [0.1.8] - 2023-07-28 + +### Fixed + +- HTTP response status codes other than 200 when downloading Javy binary now throws an error diff --git a/npm/javy-cli/index.js b/npm/javy-cli/index.js index 7591d54e..fd4f1916 100755 --- a/npm/javy-cli/index.js +++ b/npm/javy-cli/index.js @@ -12,20 +12,33 @@ const REPO = "bytecodealliance/javy"; const NAME = "javy"; async function main() { - const version = await getDesiredVersionNumber(); - if (!(await isBinaryDownloaded(version))) { - if (process.env.FORCE_FROM_SOURCE) { - await buildBinary(version); - } else { - await downloadBinary(version); + try { + const version = await getDesiredVersionNumber(); + if (!(await isBinaryDownloaded(version))) { + if (process.env.FORCE_FROM_SOURCE) { + await buildBinary(version); + } else { + await downloadBinary(version); + } } - } - const result = childProcess.spawnSync(binaryPath(version), getArgs(), { - stdio: "inherit", - }); - process.exitCode = result.status === null ? 1 : result.status; - if (result.error?.code === "ENOENT") { - console.error("Failed to start Javy. If on Linux, check if glibc is installed."); + const result = childProcess.spawnSync(binaryPath(version), getArgs(), { + stdio: "inherit", + }); + process.exitCode = result.status === null ? 1 : result.status; + if (result.error?.code === "ENOENT") { + console.error("Failed to start Javy. If on Linux, check if glibc is installed."); + } else if (result.error?.code === "EACCES") { + // This can happen if a previous version of javy-cli did not successfully download the binary. + // It would have created an empty file at `binaryPath(version)` without the execute bit set, + // which would result in an `EACCES` error code. + // We delete the cached binary because that cached binary will never run successfully and + // stops `javy-cli` from redownloading the binary. + console.error(`${NAME} was not downloaded correctly. Please retry.`); + fs.unlinkSync(binaryPath(version)); + } + } catch (e) { + console.error(e); + process.exitCode = 2; } } main(); @@ -49,10 +62,13 @@ async function isBinaryDownloaded(version) { async function downloadBinary(version) { const targetPath = binaryPath(version); - const compressedStream = await new Promise(async (resolve) => { + const compressedStream = await new Promise(async (resolve, reject) => { const url = binaryUrl(version); console.log(`Downloading ${NAME} ${version} to ${targetPath}`); const resp = await fetch(url); + if (resp.status !== 200) { + return reject(`Downloading ${NAME} failed with status code of ${resp.status}`); + } resolve(resp.body); }); const gunzip = gzip.createGunzip(); diff --git a/npm/javy-cli/package-lock.json b/npm/javy-cli/package-lock.json index 550ff68c..94dabfc4 100644 --- a/npm/javy-cli/package-lock.json +++ b/npm/javy-cli/package-lock.json @@ -1,12 +1,12 @@ { "name": "javy-cli", - "version": "0.1.7", + "version": "0.1.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "javy-cli", - "version": "0.1.7", + "version": "0.1.8", "license": "Apache-2.0", "dependencies": { "cachedir": "^2.3.0", diff --git a/npm/javy-cli/package.json b/npm/javy-cli/package.json index 4d12ac09..47dc5240 100644 --- a/npm/javy-cli/package.json +++ b/npm/javy-cli/package.json @@ -1,6 +1,6 @@ { "name": "javy-cli", - "version": "0.1.7", + "version": "0.1.8", "description": "", "main": "index.js", "bin": {