Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit b95b0e7

Browse files
committed
fix: handle disconnected streams on codegen download
1 parent 2be71f7 commit b95b0e7

File tree

3 files changed

+79
-20
lines changed

3 files changed

+79
-20
lines changed

npm-js/kalix-scripts/bin/download-codegen.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const fetch = require('node-fetch');
1+
const axios = require('axios').default;
22
const fs = require('fs');
33
const path = require('path');
4+
const stream = require('stream');
5+
const util = require('util');
46
const packageConfig = require('../package.json');
57

68
/**
@@ -44,20 +46,27 @@ if (localBinary) {
4446
? `https://repo.lightbend.com/raw/kalix/versions/${kalixCodegenVersion}/${releases[release]}.exe`
4547
: `https://repo.lightbend.com/raw/kalix/versions/${kalixCodegenVersion}/${releases[release]}`;
4648
console.info(`Fetching kalix-codegen-js from ${url}`);
47-
fetch(url).then((response) => {
48-
if (!response.ok) {
49-
throw new Error(
50-
`Error fetching Kalix codegen tool from [${url}]: ${response.statusText}.`,
51-
);
52-
}
53-
console.debug(`Saving to ${targetFile}`);
54-
if (!fs.existsSync(binDir)) {
55-
fs.mkdirSync(binDir);
56-
}
5749

58-
const fileWriter = fs.createWriteStream(targetFile, { mode: 0o755 });
59-
response.body.pipe(fileWriter);
60-
});
50+
axios
51+
.get(url, { responseType: 'stream' })
52+
.then((response) => {
53+
console.debug(`Saving to ${targetFile}`);
54+
if (!fs.existsSync(binDir)) {
55+
fs.mkdirSync(binDir);
56+
}
57+
58+
const streamPipeline = util.promisify(stream.pipeline);
59+
const fileWriter = fs.createWriteStream(targetFile, { mode: 0o755 });
60+
return streamPipeline(response.data, fileWriter);
61+
})
62+
.catch((error) => {
63+
const failure = error.isAxiosError ? error.response.statusText : error;
64+
console.error('Failed to download Kalix codegen binary:', failure);
65+
if (fs.existsSync(targetFile)) {
66+
fs.rmSync(targetFile);
67+
}
68+
process.exit(1);
69+
});
6170
} else {
6271
throw new Error(
6372
'Unsupported platform. No prebuilt version of the Kalix codegen tool exists for this platform.',

npm-js/kalix-scripts/package-lock.json

Lines changed: 54 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

npm-js/kalix-scripts/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"npm": ">=6.0.0"
1212
},
1313
"dependencies": {
14-
"cross-spawn": "^7.0.3",
15-
"node-fetch": "^2.6.1"
14+
"axios": "^0.27.2",
15+
"cross-spawn": "^7.0.3"
1616
},
1717
"bin": {
1818
"kalix-scripts": "./bin/kalix-scripts.js"

0 commit comments

Comments
 (0)