Skip to content

Commit

Permalink
Fix missing HTTP status check in javy-cli (#442)
Browse files Browse the repository at this point in the history
* Fix missing HTTP status check in javy-cli

* Update CHANGELOG

* Fix CHANGELOG entry
  • Loading branch information
jeffcharles authored Jul 28, 2023
1 parent b5b01e8 commit f2c5e7e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
6 changes: 6 additions & 0 deletions npm/javy-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 30 additions & 14 deletions npm/javy-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions npm/javy-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion npm/javy-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javy-cli",
"version": "0.1.7",
"version": "0.1.8",
"description": "",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit f2c5e7e

Please sign in to comment.