Skip to content

Commit c729a59

Browse files
author
Sergey Koryshev
authored
Merge pull request #184 from microsoft/users/sergey.koryshev/4113-refactor-nodejs-downloading
Add logic to handle HTTP-errors during file downloading
2 parents 2de34c5 + b5652ea commit c729a59

File tree

3 files changed

+47
-40
lines changed

3 files changed

+47
-40
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
### 2.0.0
2525

2626
- Bump `azure-pipelines-task-lib` to `4.1.0`
27+
28+
### 2.0.1
29+
30+
- <https://github.com/microsoft/azure-pipelines-tool-lib/pull/184>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-tool-lib",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Azure Pipelines Tool Installer Lib for CI/CD Tasks",
55
"main": "tool.js",
66
"scripts": {

tool.ts

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -249,46 +249,49 @@ export async function downloadTool(
249249
}
250250

251251
tl.debug('creating stream');
252-
let file: NodeJS.WritableStream = fs.createWriteStream(destPath);
253-
file.on('open', async (fd) => {
254-
try {
255-
let stream = response.message.pipe(file);
256-
stream.on('close', () => {
257-
tl.debug('download complete');
258-
let fileSizeInBytes: number;
259-
try {
260-
fileSizeInBytes = _getFileSizeOnDisk(destPath);
261-
}
262-
catch (err) {
263-
fileSizeInBytes = NaN;
264-
tl.warning(`Unable to check file size of ${destPath} due to error: ${err.Message}`);
265-
}
266-
267-
if (!isNaN(fileSizeInBytes)) {
268-
tl.debug(`Downloaded file size: ${fileSizeInBytes} bytes`);
269-
} else {
270-
tl.debug(`File size on disk was not found`);
271-
}
272-
273-
if (!isNaN(downloadedContentLength) &&
274-
!isNaN(fileSizeInBytes) &&
275-
fileSizeInBytes !== downloadedContentLength) {
276-
tl.warning(`Content-Length (${downloadedContentLength} bytes) did not match downloaded file size (${fileSizeInBytes} bytes).`);
277-
}
278-
279-
resolve(destPath);
280-
});
281-
}
282-
catch (err) {
252+
const file: NodeJS.WritableStream = fs.createWriteStream(destPath);
253+
file
254+
.on('open', async (fd) => {
255+
try {
256+
response.message
257+
.on('error', (err) => {
258+
file.end();
259+
reject(err);
260+
})
261+
.pipe(file);
262+
} catch (err) {
263+
reject(err);
264+
}
265+
})
266+
.on('close', () => {
267+
tl.debug('download complete');
268+
let fileSizeInBytes: number;
269+
try {
270+
fileSizeInBytes = _getFileSizeOnDisk(destPath);
271+
} catch (err) {
272+
fileSizeInBytes = NaN;
273+
tl.warning(`Unable to check file size of ${destPath} due to error: ${err.Message}`);
274+
}
275+
276+
if (!isNaN(fileSizeInBytes)) {
277+
tl.debug(`Downloaded file size: ${fileSizeInBytes} bytes`);
278+
} else {
279+
tl.debug(`File size on disk was not found`);
280+
}
281+
282+
if (!isNaN(downloadedContentLength) &&
283+
!isNaN(fileSizeInBytes) &&
284+
fileSizeInBytes !== downloadedContentLength) {
285+
tl.warning(`Content-Length (${downloadedContentLength} bytes) did not match downloaded file size (${fileSizeInBytes} bytes).`);
286+
}
287+
288+
resolve(destPath);
289+
})
290+
.on('error', (err) => {
291+
file.end();
283292
reject(err);
284-
}
285-
});
286-
file.on('error', (err) => {
287-
file.end();
288-
reject(err);
289-
});
290-
}
291-
catch (error) {
293+
});
294+
} catch (error) {
292295
reject(error);
293296
}
294297
});

0 commit comments

Comments
 (0)