Skip to content

Commit

Permalink
Merge pull request #184 from microsoft/users/sergey.koryshev/4113-ref…
Browse files Browse the repository at this point in the history
…actor-nodejs-downloading

Add logic to handle HTTP-errors during file downloading
  • Loading branch information
sergey-koryshev authored Mar 9, 2023
2 parents 2de34c5 + b5652ea commit c729a59
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
### 2.0.0

- Bump `azure-pipelines-task-lib` to `4.1.0`

### 2.0.1

- <https://github.com/microsoft/azure-pipelines-tool-lib/pull/184>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-tool-lib",
"version": "2.0.0",
"version": "2.0.1",
"description": "Azure Pipelines Tool Installer Lib for CI/CD Tasks",
"main": "tool.js",
"scripts": {
Expand Down
81 changes: 42 additions & 39 deletions tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,46 +249,49 @@ export async function downloadTool(
}

tl.debug('creating stream');
let file: NodeJS.WritableStream = fs.createWriteStream(destPath);
file.on('open', async (fd) => {
try {
let stream = response.message.pipe(file);
stream.on('close', () => {
tl.debug('download complete');
let fileSizeInBytes: number;
try {
fileSizeInBytes = _getFileSizeOnDisk(destPath);
}
catch (err) {
fileSizeInBytes = NaN;
tl.warning(`Unable to check file size of ${destPath} due to error: ${err.Message}`);
}

if (!isNaN(fileSizeInBytes)) {
tl.debug(`Downloaded file size: ${fileSizeInBytes} bytes`);
} else {
tl.debug(`File size on disk was not found`);
}

if (!isNaN(downloadedContentLength) &&
!isNaN(fileSizeInBytes) &&
fileSizeInBytes !== downloadedContentLength) {
tl.warning(`Content-Length (${downloadedContentLength} bytes) did not match downloaded file size (${fileSizeInBytes} bytes).`);
}

resolve(destPath);
});
}
catch (err) {
const file: NodeJS.WritableStream = fs.createWriteStream(destPath);
file
.on('open', async (fd) => {
try {
response.message
.on('error', (err) => {
file.end();
reject(err);
})
.pipe(file);
} catch (err) {
reject(err);
}
})
.on('close', () => {
tl.debug('download complete');
let fileSizeInBytes: number;
try {
fileSizeInBytes = _getFileSizeOnDisk(destPath);
} catch (err) {
fileSizeInBytes = NaN;
tl.warning(`Unable to check file size of ${destPath} due to error: ${err.Message}`);
}

if (!isNaN(fileSizeInBytes)) {
tl.debug(`Downloaded file size: ${fileSizeInBytes} bytes`);
} else {
tl.debug(`File size on disk was not found`);
}

if (!isNaN(downloadedContentLength) &&
!isNaN(fileSizeInBytes) &&
fileSizeInBytes !== downloadedContentLength) {
tl.warning(`Content-Length (${downloadedContentLength} bytes) did not match downloaded file size (${fileSizeInBytes} bytes).`);
}

resolve(destPath);
})
.on('error', (err) => {
file.end();
reject(err);
}
});
file.on('error', (err) => {
file.end();
reject(err);
});
}
catch (error) {
});
} catch (error) {
reject(error);
}
});
Expand Down

0 comments on commit c729a59

Please sign in to comment.