Skip to content

Commit

Permalink
Merge pull request #154 from microsoft/users/tatyana-kostromskaya/dow…
Browse files Browse the repository at this point in the history
…nloadtool-logging

Add more debug logs for downloadTool function
  • Loading branch information
Tatyana Kostromskaya authored Mar 31, 2022
2 parents 426ba81 + d9efc95 commit 437d0e5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

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": "1.3.0",
"version": "1.3.1",
"description": "Azure Pipelines Tool Installer Lib for CI/CD Tasks",
"main": "tool.js",
"scripts": {
Expand Down
70 changes: 62 additions & 8 deletions tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,10 @@ export async function downloadTool(

// check if it's an absolute path already
var destPath: string;
if(path.isAbsolute(fileName))
{
if (path.isAbsolute(fileName)) {
destPath = fileName;
}
else
{
else {
destPath = path.join(_getAgentTemp(), fileName);
}

Expand All @@ -235,21 +233,49 @@ export async function downloadTool(

tl.debug('downloading');
let response: httpm.HttpClientResponse = await http.get(url, additionalHeaders);

if (response.message.statusCode != 200) {
let err: Error = new Error('Unexpected HTTP response: ' + response.message.statusCode);
err['httpStatusCode'] = response.message.statusCode;
tl.debug(`Failed to download "${fileName}" from "${url}". Code(${response.message.statusCode}) Message(${response.message.statusMessage})`);
throw err;
}

let downloadedContentLength = _getContentLengthOfDownloadedFile(response);
if (!isNaN(downloadedContentLength)) {
tl.debug(`Content-Length of downloaded file: ${downloadedContentLength}`);
} else {
tl.debug(`Content-Length header missing`);
}

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);
});
}
Expand All @@ -260,14 +286,42 @@ export async function downloadTool(
file.on('error', (err) => {
file.end();
reject(err);
})
});
}
catch (error) {
reject(error);
}
});
}

//---------------------
// Size functions
//---------------------

/**
* Gets size of downloaded file from "Content-Length" header
*
* @param response response for request to get the file
* @returns number if the 'content-length' is not empty, otherwise NaN
*/
function _getContentLengthOfDownloadedFile(response: httpm.HttpClientResponse): number {
let contentLengthHeader = response.message.headers['content-length']
let parsedContentLength = parseInt(contentLengthHeader);
return parsedContentLength;
}

/**
* Gets size of file saved to disk
*
* @param filePath the path to the file, saved to the disk
* @returns size of file saved to disk
*/
function _getFileSizeOnDisk(filePath: string): number {
let fileStats = fs.statSync(filePath);
let fileSizeInBytes = fileStats.size;
return fileSizeInBytes;
}

//---------------------
// Install Functions
//---------------------
Expand Down Expand Up @@ -508,7 +562,7 @@ function _createExtractFolder(dest?: string): string {
}

tl.mkdirP(dest);

return dest;
}

Expand Down Expand Up @@ -565,4 +619,4 @@ function _getAgentTemp(): string {
}

return tempDirectory;
}
}

0 comments on commit 437d0e5

Please sign in to comment.