From 9ba2517dba2c4adfc3a52cf3a6d9595def0dbfa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Mon, 7 Nov 2022 17:10:48 +0100 Subject: [PATCH] fix: Handle closed connection --- scripts/install.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/scripts/install.js b/scripts/install.js index 8fd6aedbb7..b1afa34083 100755 --- a/scripts/install.js +++ b/scripts/install.js @@ -258,19 +258,29 @@ async function downloadBinary() { decompressor = new stream.PassThrough(); } const name = downloadUrl.match(/.*\/(.*?)$/)[1]; - const total = parseInt(response.headers.get('content-length'), 10); - const progressBar = createProgressBar(name, total); + let downloadedBytes = 0; + const totalBytes = parseInt(response.headers.get('content-length'), 10); + const progressBar = createProgressBar(name, totalBytes); const tempPath = getTempFile(cachedPath); fs.mkdirSync(path.dirname(tempPath), { recursive: true }); await new Promise((resolve, reject) => { response.body .on('error', (e) => reject(e)) - .on('data', (chunk) => progressBar.tick(chunk.length)) + .on('data', (chunk) => { + downloadedBytes += chunk.length; + progressBar.tick(chunk.length); + }) .pipe(decompressor) .pipe(fs.createWriteStream(tempPath, { mode: '0755' })) .on('error', (e) => reject(e)) - .on('close', () => resolve()); + .on('close', () => { + if (downloadedBytes >= totalBytes) { + resolve(); + } else { + reject(new Error('connection interrupted')); + } + }); }); if (process.env.SENTRYCLI_SKIP_CHECKSUM_VALIDATION !== '1') {