Skip to content

Commit

Permalink
wip: disconnected streams on codegen download
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlugter committed Jul 13, 2022
1 parent 2be71f7 commit e8afdf0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 20 deletions.
38 changes: 24 additions & 14 deletions npm-js/kalix-scripts/bin/download-codegen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fetch = require('node-fetch');
const axios = require('axios').default;
const fs = require('fs');
const path = require('path');
const stream = require('stream');
const util = require('util');
const packageConfig = require('../package.json');

/**
Expand Down Expand Up @@ -44,20 +46,28 @@ if (localBinary) {
? `https://repo.lightbend.com/raw/kalix/versions/${kalixCodegenVersion}/${releases[release]}.exe`
: `https://repo.lightbend.com/raw/kalix/versions/${kalixCodegenVersion}/${releases[release]}`;
console.info(`Fetching kalix-codegen-js from ${url}`);
fetch(url).then((response) => {
if (!response.ok) {
throw new Error(
`Error fetching Kalix codegen tool from [${url}]: ${response.statusText}.`,
);
}
console.debug(`Saving to ${targetFile}`);
if (!fs.existsSync(binDir)) {
fs.mkdirSync(binDir);
}

const fileWriter = fs.createWriteStream(targetFile, { mode: 0o755 });
response.body.pipe(fileWriter);
});
axios
.get(url, { responseType: 'stream' })
.then((response) => {
console.debug(`Saving to ${targetFile}`);
if (!fs.existsSync(binDir)) {
fs.mkdirSync(binDir);
}

const streamPipeline = util.promisify(stream.pipeline);
const fileWriter = fs.createWriteStream(targetFile, { mode: 0o755 });
return streamPipeline(response.data, fileWriter);
})
.catch((failure) => {
const error = failure.isAxiosError
? failure.response.statusText
: failure;
console.error('Failed to download Kalix codegen binary:', error);
if (fs.existsSync(targetFile)) {
fs.rmSync(targetFile);
}
});
} else {
throw new Error(
'Unsupported platform. No prebuilt version of the Kalix codegen tool exists for this platform.',
Expand Down
58 changes: 54 additions & 4 deletions npm-js/kalix-scripts/package-lock.json

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

4 changes: 2 additions & 2 deletions npm-js/kalix-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"npm": ">=6.0.0"
},
"dependencies": {
"cross-spawn": "^7.0.3",
"node-fetch": "^2.6.1"
"axios": "^0.27.2",
"cross-spawn": "^7.0.3"
},
"bin": {
"kalix-scripts": "./bin/kalix-scripts.js"
Expand Down

0 comments on commit e8afdf0

Please sign in to comment.