Skip to content

Commit 437d0e5

Browse files
author
Tatyana Kostromskaya
authored
Merge pull request #154 from microsoft/users/tatyana-kostromskaya/downloadtool-logging
Add more debug logs for downloadTool function
2 parents 426ba81 + d9efc95 commit 437d0e5

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Azure Pipelines Tool Installer Lib for CI/CD Tasks",
55
"main": "tool.js",
66
"scripts": {

tool.ts

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,10 @@ export async function downloadTool(
214214

215215
// check if it's an absolute path already
216216
var destPath: string;
217-
if(path.isAbsolute(fileName))
218-
{
217+
if (path.isAbsolute(fileName)) {
219218
destPath = fileName;
220219
}
221-
else
222-
{
220+
else {
223221
destPath = path.join(_getAgentTemp(), fileName);
224222
}
225223

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

236234
tl.debug('downloading');
237235
let response: httpm.HttpClientResponse = await http.get(url, additionalHeaders);
238-
236+
239237
if (response.message.statusCode != 200) {
240238
let err: Error = new Error('Unexpected HTTP response: ' + response.message.statusCode);
241239
err['httpStatusCode'] = response.message.statusCode;
242240
tl.debug(`Failed to download "${fileName}" from "${url}". Code(${response.message.statusCode}) Message(${response.message.statusMessage})`);
243241
throw err;
244242
}
245243

244+
let downloadedContentLength = _getContentLengthOfDownloadedFile(response);
245+
if (!isNaN(downloadedContentLength)) {
246+
tl.debug(`Content-Length of downloaded file: ${downloadedContentLength}`);
247+
} else {
248+
tl.debug(`Content-Length header missing`);
249+
}
250+
246251
tl.debug('creating stream');
247252
let file: NodeJS.WritableStream = fs.createWriteStream(destPath);
248253
file.on('open', async (fd) => {
249254
try {
250255
let stream = response.message.pipe(file);
251256
stream.on('close', () => {
252257
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+
253279
resolve(destPath);
254280
});
255281
}
@@ -260,14 +286,42 @@ export async function downloadTool(
260286
file.on('error', (err) => {
261287
file.end();
262288
reject(err);
263-
})
289+
});
264290
}
265291
catch (error) {
266292
reject(error);
267293
}
268294
});
269295
}
270296

297+
//---------------------
298+
// Size functions
299+
//---------------------
300+
301+
/**
302+
* Gets size of downloaded file from "Content-Length" header
303+
*
304+
* @param response response for request to get the file
305+
* @returns number if the 'content-length' is not empty, otherwise NaN
306+
*/
307+
function _getContentLengthOfDownloadedFile(response: httpm.HttpClientResponse): number {
308+
let contentLengthHeader = response.message.headers['content-length']
309+
let parsedContentLength = parseInt(contentLengthHeader);
310+
return parsedContentLength;
311+
}
312+
313+
/**
314+
* Gets size of file saved to disk
315+
*
316+
* @param filePath the path to the file, saved to the disk
317+
* @returns size of file saved to disk
318+
*/
319+
function _getFileSizeOnDisk(filePath: string): number {
320+
let fileStats = fs.statSync(filePath);
321+
let fileSizeInBytes = fileStats.size;
322+
return fileSizeInBytes;
323+
}
324+
271325
//---------------------
272326
// Install Functions
273327
//---------------------
@@ -508,7 +562,7 @@ function _createExtractFolder(dest?: string): string {
508562
}
509563

510564
tl.mkdirP(dest);
511-
565+
512566
return dest;
513567
}
514568

@@ -565,4 +619,4 @@ function _getAgentTemp(): string {
565619
}
566620

567621
return tempDirectory;
568-
}
622+
}

0 commit comments

Comments
 (0)