@@ -214,12 +214,10 @@ export async function downloadTool(
214
214
215
215
// check if it's an absolute path already
216
216
var destPath : string ;
217
- if ( path . isAbsolute ( fileName ) )
218
- {
217
+ if ( path . isAbsolute ( fileName ) ) {
219
218
destPath = fileName ;
220
219
}
221
- else
222
- {
220
+ else {
223
221
destPath = path . join ( _getAgentTemp ( ) , fileName ) ;
224
222
}
225
223
@@ -235,21 +233,49 @@ export async function downloadTool(
235
233
236
234
tl . debug ( 'downloading' ) ;
237
235
let response : httpm . HttpClientResponse = await http . get ( url , additionalHeaders ) ;
238
-
236
+
239
237
if ( response . message . statusCode != 200 ) {
240
238
let err : Error = new Error ( 'Unexpected HTTP response: ' + response . message . statusCode ) ;
241
239
err [ 'httpStatusCode' ] = response . message . statusCode ;
242
240
tl . debug ( `Failed to download "${ fileName } " from "${ url } ". Code(${ response . message . statusCode } ) Message(${ response . message . statusMessage } )` ) ;
243
241
throw err ;
244
242
}
245
243
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
+
246
251
tl . debug ( 'creating stream' ) ;
247
252
let file : NodeJS . WritableStream = fs . createWriteStream ( destPath ) ;
248
253
file . on ( 'open' , async ( fd ) => {
249
254
try {
250
255
let stream = response . message . pipe ( file ) ;
251
256
stream . on ( 'close' , ( ) => {
252
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
+
253
279
resolve ( destPath ) ;
254
280
} ) ;
255
281
}
@@ -260,14 +286,42 @@ export async function downloadTool(
260
286
file . on ( 'error' , ( err ) => {
261
287
file . end ( ) ;
262
288
reject ( err ) ;
263
- } )
289
+ } ) ;
264
290
}
265
291
catch ( error ) {
266
292
reject ( error ) ;
267
293
}
268
294
} ) ;
269
295
}
270
296
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
+
271
325
//---------------------
272
326
// Install Functions
273
327
//---------------------
@@ -508,7 +562,7 @@ function _createExtractFolder(dest?: string): string {
508
562
}
509
563
510
564
tl . mkdirP ( dest ) ;
511
-
565
+
512
566
return dest ;
513
567
}
514
568
@@ -565,4 +619,4 @@ function _getAgentTemp(): string {
565
619
}
566
620
567
621
return tempDirectory ;
568
- }
622
+ }
0 commit comments