Skip to content

Commit

Permalink
Throw error when trying to download non existing Kaoto backend binary (
Browse files Browse the repository at this point in the history
…#308)

Signed-off-by: Dominik Jelinek <[email protected]>
  • Loading branch information
djelinek authored Aug 22, 2023
1 parent d2bfcb0 commit ff510a9
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@ const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');

class HTTPResponseError extends Error {
constructor(response) {
super(`${response.status} ${response.statusText} - ${response.url}`);
}
}

const checkStatus = (response) => {
if (response.ok) {
return response;
} else {
throw new HTTPResponseError(response);
}
}

const downloadFile = (async (url, filePath) => {
console.log(`Will fetch backend binary from ${url} into ${path.resolve(filePath)}`);
const res = await fetch(url);
const fileStream = fs.createWriteStream(filePath);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
res.body.on("error", reject);
fileStream.on("finish", resolve);
});
console.log(`Binary downloaded ${path.resolve(filePath)}`)
fs.chmodSync(filePath, "766");
if(checkStatus(res)) {
const fileStream = fs.createWriteStream(filePath);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
res.body.on("error", reject);
fileStream.on("finish", resolve);
});
console.log(`Binary downloaded ${path.resolve(filePath)}`)
fs.chmodSync(filePath, "766");
}
});

const downloadKaotoBackendNativeExecutable = (backendVersion, platform, extension) => {
Expand Down

0 comments on commit ff510a9

Please sign in to comment.