@@ -13,7 +13,7 @@ import {
13
13
} from 'path' ;
14
14
import spawn from 'cross-spawn' ;
15
15
import unCompress from 'all-unpacker' ;
16
- import { wget , isString } from 'node-wget-fetch' ;
16
+ import fetching from 'node-wget-fetch' ;
17
17
18
18
const __filename = fileURLToPath (
19
19
import . meta. url ) ;
@@ -92,43 +92,43 @@ function retrieve(path = {
92
92
dest : ''
93
93
} ) {
94
94
console . log ( 'Downloading ' + path . url ) ;
95
- return wget ( path . url , path . dest , { retry : { retries : 5 } } )
96
- . then ( ( info ) => {
97
- return info ;
98
- } )
99
- . catch ( ( err ) => {
100
- throw ( 'Error downloading file: ' + err ) ;
101
- } ) ;
95
+ return new Promise ( ( resolve , reject ) => {
96
+ fetching . wget ( path . url , path . dest )
97
+ . then ( ( info ) => resolve ( info ) )
98
+ . catch ( ( err ) => reject ( 'Error downloading file: ' + err ) ) ;
99
+ } ) ;
102
100
}
103
101
104
102
function platformUnpacker ( platformData = windowsPlatform ) {
105
- return new Promise ( ( resolve , reject ) => {
106
- retrieve ( {
103
+ return new retryPromise ( {
104
+ retries : 5
105
+ } , ( resolve , retry ) => {
106
+ return retrieve ( {
107
107
url : platformData . url + platformData . filename ,
108
108
dest : platformData . source
109
109
} ) . then ( ( ) => {
110
110
console . log ( 'Extracting: ' + platformData . filename ) ;
111
- if ( isString ( platformData . platform ) ) {
111
+ if ( fetching . isString ( platformData . platform ) ) {
112
112
unpack ( platformData . source , platformData . destination )
113
113
. then ( ( ) => {
114
114
return resolve ( platformData . platform ) ;
115
115
} )
116
- . catch ( ( err ) => reject ( err ) ) ;
116
+ . catch ( ( err ) => retry ( err ) ) ;
117
117
}
118
- } ) . catch ( ( err ) => reject ( err ) ) ;
118
+ } ) . catch ( ( err ) => retry ( err ) ) ;
119
119
} ) . catch ( ( err ) => console . error ( err ) ) ;
120
120
}
121
121
122
122
function unpack ( source , destination , toCopy ) {
123
123
return new Promise ( ( resolve , reject ) => {
124
124
return unCompress . unpack (
125
125
source , {
126
- files : ( toCopy == null ? '' : toCopy ) ,
127
- targetDir : destination ,
128
- forceOverwrite : true ,
129
- noDirectory : true ,
130
- quiet : true ,
131
- } ,
126
+ files : ( toCopy == null ? '' : toCopy ) ,
127
+ targetDir : destination ,
128
+ forceOverwrite : true ,
129
+ noDirectory : true ,
130
+ quiet : true ,
131
+ } ,
132
132
( err , files , text ) => {
133
133
if ( err )
134
134
return reject ( err ) ;
@@ -170,19 +170,97 @@ function makeExecutable(binary = [], binaryFolder = '') {
170
170
} ) ;
171
171
}
172
172
173
+ /**
174
+ * Returns a promise that conditionally tries to resolve multiple times, as specified by the retry
175
+ * policy.
176
+ * @param {retryPolicy } [options] - Either An object that specifies the retry policy.
177
+ * @param {retryExecutor } executor - A function that is called for each attempt to resolve the promise.
178
+ * @returns {Promise }
179
+ *
180
+ * @see https://github.com/wouter-vdb/retrying-promise
181
+ */
182
+ function retryPromise ( options , executor ) {
183
+ if ( executor == undefined ) {
184
+ executor = options ;
185
+ options = { } ;
186
+ }
187
+
188
+ var opts = prepOpts ( options ) ;
189
+ var attempts = 1 ;
190
+
191
+ return new Promise ( ( resolve , reject ) => {
192
+ let retrying = false ;
193
+
194
+ function retry ( err ) {
195
+ if ( retrying ) return ;
196
+ retrying = true ;
197
+ if ( attempts < opts . retries ) {
198
+ setTimeout ( ( ) => {
199
+ attempts ++ ;
200
+ retrying = false ;
201
+ executor ( resolve , retry , reject , attempts ) ;
202
+ } , createTimeout ( attempts , opts ) ) ;
203
+ } else {
204
+ //console.log(attempts, opts.retries);
205
+ reject ( err ) ;
206
+ }
207
+ }
208
+
209
+ executor ( resolve , retry , reject , attempts ) ;
210
+ } ) ;
211
+ }
212
+
213
+ /*
214
+ * Preps the options object, initializing default values and checking constraints.
215
+ * @param {Object } options - The options as provided to `retryingPromise`.
216
+ */
217
+ function prepOpts ( options ) {
218
+ var opts = {
219
+ retries : 10 ,
220
+ factor : 2 ,
221
+ minTimeout : 1000 ,
222
+ maxTimeout : Infinity ,
223
+ randomize : false
224
+ } ;
225
+ for ( var key in options ) {
226
+ opts [ key ] = options [ key ] ;
227
+ }
228
+
229
+ if ( opts . minTimeout > opts . maxTimeout ) {
230
+ throw new Error ( 'minTimeout is greater than maxTimeout' ) ;
231
+ }
232
+
233
+ return opts ;
234
+ }
235
+
236
+ /**
237
+ * Get a timeout value in milliseconds.
238
+ * @param {number } attempt - The attempt count.
239
+ * @param {Object } opts - The options.
240
+ * @returns {number } The timeout value in milliseconds.
241
+ */
242
+ function createTimeout ( attempt , opts ) {
243
+ var random = opts . randomize ? Math . random ( ) + 1 : 1 ;
244
+
245
+ var timeout = Math . round ( random * opts . minTimeout * Math . pow ( opts . factor , attempt ) ) ;
246
+ timeout = Math . min ( timeout , opts . maxTimeout ) ;
247
+
248
+ return timeout ;
249
+ }
250
+
173
251
let extractionPromises = [ ] ;
174
252
let platforms = [ linuxPlatform , appleMacPlatform , windowsOtherPlatform ] ;
175
253
if ( process . platform == 'win32' )
176
254
platforms = [ linuxPlatform , appleMacPlatform , windowsPlatform , windowsOtherPlatform ] ;
177
255
178
256
platforms . forEach ( ( dataFor ) => {
179
257
fs . mkdir ( dataFor . destination , ( err ) => {
180
- if ( err ) { }
258
+ if ( err ) { }
181
259
} ) ;
182
260
const extracted = retrieve ( {
183
- url : _7zAppUrl + dataFor . extraName ,
184
- dest : dataFor . extraSourceFile
185
- } )
261
+ url : _7zAppUrl + dataFor . extraName ,
262
+ dest : dataFor . extraSourceFile
263
+ } )
186
264
. then ( ( ) => {
187
265
return platformUnpacker ( dataFor )
188
266
. then ( ( ) => {
0 commit comments