2
2
const fs = require ( 'fs-extra' ) ;
3
3
const tl = require ( 'azure-pipelines-task-lib/task' ) ;
4
4
const path = require ( 'path' ) ;
5
- const httpm = require ( " typed-rest-client/HttpClient" ) ;
5
+ const httpm = require ( ' typed-rest-client/HttpClient' ) ;
6
6
const uuidV4 = require ( 'uuid/v4' ) ;
7
7
8
8
module . exports = {
9
9
downloadTool : downloadTool
10
10
} ;
11
11
12
- var __awaiter = ( this && this . __awaiter ) || function ( thisArg , _arguments , P , generator ) {
13
- return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
14
- function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
15
- function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
16
- function step ( result ) { result . done ? resolve ( result . value ) : new P ( function ( resolve ) { resolve ( result . value ) ; } ) . then ( fulfilled , rejected ) ; }
17
- step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
18
- } ) ;
19
- } ;
12
+ var __awaiter =
13
+ ( this && this . __awaiter ) ||
14
+ function ( thisArg , _arguments , P , generator ) {
15
+ return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
16
+ function fulfilled ( value ) {
17
+ try {
18
+ step ( generator . next ( value ) ) ;
19
+ } catch ( e ) {
20
+ reject ( e ) ;
21
+ }
22
+ }
23
+ function rejected ( value ) {
24
+ try {
25
+ step ( generator [ 'throw' ] ( value ) ) ;
26
+ } catch ( e ) {
27
+ reject ( e ) ;
28
+ }
29
+ }
30
+ function step ( result ) {
31
+ result . done
32
+ ? resolve ( result . value )
33
+ : new P ( function ( resolve ) {
34
+ resolve ( result . value ) ;
35
+ } ) . then ( fulfilled , rejected ) ;
36
+ }
37
+ step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
38
+ } ) ;
39
+ } ;
20
40
21
41
function _getAgentTemp ( ) {
22
42
tl . assertAgent ( '2.115.0' ) ;
@@ -32,74 +52,77 @@ function delay(ms) {
32
52
}
33
53
34
54
function downloadTool ( url , fileName , handlers ) {
35
- return __awaiter ( this , void 0 , void 0 , function * ( ) {
36
- return new Promise ( ( resolve , reject ) => __awaiter ( this , void 0 , void 0 , function * ( ) {
37
- try {
38
- let pkg = require ( path . join ( __dirname , 'package.json' ) ) ;
39
- let userAgent = 'vsts-task-installer/' + pkg . version ;
40
- let requestOptions = {
41
- // ignoreSslError: true,
42
- proxy : tl . getHttpProxyConfiguration ( ) ,
43
- cert : tl . getHttpCertConfiguration ( )
44
- } ;
45
- let http = new httpm . HttpClient ( userAgent , handlers , requestOptions ) ;
46
- tl . debug ( fileName ) ;
47
- fileName = fileName || uuidV4 ( ) ;
48
- // check if it's an absolute path already
49
- var destPath ;
50
- if ( path . isAbsolute ( fileName ) ) {
51
- destPath = fileName ;
52
- }
53
- else {
54
- destPath = path . join ( _getAgentTemp ( ) , fileName ) ;
55
- }
56
- // make sure that the folder exists
57
- tl . mkdirP ( path . dirname ( destPath ) ) ;
58
- console . log ( tl . loc ( 'TOOL_LIB_Downloading' , url ) ) ;
59
- tl . debug ( 'destination ' + destPath ) ;
60
- if ( fs . existsSync ( destPath ) ) {
61
- throw new Error ( "Destination file path already exists" ) ;
62
- }
63
- tl . debug ( 'downloading' ) ;
64
- const statusCodesToRetry = [ httpm . HttpCodes . BadGateway , httpm . HttpCodes . ServiceUnavailable , httpm . HttpCodes . GatewayTimeout ] ;
65
- let retryCount = 1 ;
66
- const maxRetries = 3 ;
67
- let response = yield http . get ( url ) ;
68
- while ( retryCount < maxRetries && statusCodesToRetry . indexOf ( response . message . statusCode ) > - 1 ) {
69
- tl . debug ( `Download attempt "${ retryCount } " of "${ maxRetries } " failed with status code "${ response . message . statusCode } ".` ) ;
70
- retryCount += 1 ;
71
- yield delay ( 1000 ) ;
72
- tl . debug ( `Downloading attempt "${ retryCount } " of "${ maxRetries } "` ) ;
73
- response = yield http . get ( url ) ;
74
- }
75
- if ( response . message . statusCode !== 200 ) {
76
- let err = new Error ( 'Unexpected HTTP response: ' + response . message . statusCode ) ;
77
- err [ 'httpStatusCode' ] = response . message . statusCode ;
78
- tl . debug ( `Failed to download "${ fileName } " from "${ url } ". Code(${ response . message . statusCode } ) Message(${ response . message . statusMessage } )` ) ;
79
- throw err ;
80
- }
81
- tl . debug ( 'creating stream' ) ;
82
- let file = fs . createWriteStream ( destPath ) ;
83
- file . on ( 'open' , ( fd ) => __awaiter ( this , void 0 , void 0 , function * ( ) {
84
- try {
85
- let stream = response . message . pipe ( file ) ;
86
- stream . on ( 'close' , ( ) => {
87
- tl . debug ( 'download complete' ) ;
88
- resolve ( destPath ) ;
89
- } ) ;
55
+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
56
+ return new Promise ( ( resolve , reject ) =>
57
+ __awaiter ( this , void 0 , void 0 , function * ( ) {
58
+ try {
59
+ let pkg = require ( path . join ( __dirname , 'package.json' ) ) ;
60
+ let userAgent = 'vsts-task-installer/' + pkg . version ;
61
+ let requestOptions = {
62
+ // ignoreSslError: true,
63
+ proxy : tl . getHttpProxyConfiguration ( ) ,
64
+ cert : tl . getHttpCertConfiguration ( )
65
+ } ;
66
+ let http = new httpm . HttpClient ( userAgent , handlers , requestOptions ) ;
67
+ tl . debug ( fileName ) ;
68
+ fileName = fileName || uuidV4 ( ) ;
69
+ // check if it's an absolute path already
70
+ var destPath ;
71
+ if ( path . isAbsolute ( fileName ) ) {
72
+ destPath = fileName ;
73
+ } else {
74
+ destPath = path . join ( _getAgentTemp ( ) , fileName ) ;
90
75
}
91
- catch ( err ) {
92
- reject ( err ) ;
76
+ // make sure that the folder exists
77
+ tl . mkdirP ( path . dirname ( destPath ) ) ;
78
+ console . log ( tl . loc ( 'TOOL_LIB_Downloading' , url ) ) ;
79
+ tl . debug ( 'destination ' + destPath ) ;
80
+ if ( fs . existsSync ( destPath ) ) {
81
+ throw new Error ( 'Destination file path already exists' ) ;
93
82
}
94
- } ) ) ;
95
- file . on ( 'error' , ( err ) => {
96
- file . end ( ) ;
97
- reject ( err ) ;
98
- } ) ;
99
- }
100
- catch ( error ) {
101
- reject ( error ) ;
102
- }
103
- } ) ) ;
83
+ tl . debug ( 'downloading' ) ;
84
+ const statusCodesToRetry = [ httpm . HttpCodes . BadGateway , httpm . HttpCodes . ServiceUnavailable , httpm . HttpCodes . GatewayTimeout ] ;
85
+ let retryCount = 1 ;
86
+ const maxRetries = 3 ;
87
+ let response = yield http . get ( url ) ;
88
+ while ( retryCount < maxRetries && statusCodesToRetry . indexOf ( response . message . statusCode ) > - 1 ) {
89
+ tl . debug ( `Download attempt "${ retryCount } " of "${ maxRetries } " failed with status code "${ response . message . statusCode } ".` ) ;
90
+ retryCount += 1 ;
91
+ yield delay ( 1000 ) ;
92
+ tl . debug ( `Downloading attempt "${ retryCount } " of "${ maxRetries } "` ) ;
93
+ response = yield http . get ( url ) ;
94
+ }
95
+ if ( response . message . statusCode !== 200 ) {
96
+ let err = new Error ( 'Unexpected HTTP response: ' + response . message . statusCode ) ;
97
+ err [ 'httpStatusCode' ] = response . message . statusCode ;
98
+ tl . debug (
99
+ `Failed to download "${ fileName } " from "${ url } ". Code(${ response . message . statusCode } ) Message(${ response . message . statusMessage } )`
100
+ ) ;
101
+ throw err ;
102
+ }
103
+ tl . debug ( 'creating stream' ) ;
104
+ let file = fs . createWriteStream ( destPath ) ;
105
+ file . on ( 'open' , fd =>
106
+ __awaiter ( this , void 0 , void 0 , function * ( ) {
107
+ try {
108
+ let stream = response . message . pipe ( file ) ;
109
+ stream . on ( 'close' , ( ) => {
110
+ tl . debug ( 'download complete' ) ;
111
+ resolve ( destPath ) ;
112
+ } ) ;
113
+ } catch ( err ) {
114
+ reject ( err ) ;
115
+ }
116
+ } )
117
+ ) ;
118
+ file . on ( 'error' , err => {
119
+ file . end ( ) ;
120
+ reject ( err ) ;
121
+ } ) ;
122
+ } catch ( error ) {
123
+ reject ( error ) ;
124
+ }
125
+ } )
126
+ ) ;
104
127
} ) ;
105
- }
128
+ }
0 commit comments