Skip to content

Commit 3a5f5a0

Browse files
committed
remove/update dependencies, embed retry
1 parent 6f5e751 commit 3a5f5a0

File tree

3 files changed

+88
-131
lines changed

3 files changed

+88
-131
lines changed

installer.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const fs = require('fs-extra'),
66
path = require('path'),
77
spawn = require('cross-spawn'),
88
unCompress = require('all-unpacker'),
9-
retryPromise = require('retrying-promise'),
109
fetching = require('node-wget-fetch'),
1110
system_installer = require('system-installer'),
1211
macos_release = require('macos-release');
@@ -348,3 +347,79 @@ Promise.all(extractionPromises)
348347
});
349348
})
350349
.catch((err) => console.log(err));
350+
351+
/**
352+
* Returns a promise that conditionally tries to resolve multiple times, as specified by the retry
353+
* policy.
354+
* @param {retryPolicy} [options] - Either An object that specifies the retry policy.
355+
* @param {retryExecutor} executor - A function that is called for each attempt to resolve the promise.
356+
* @returns {Promise}
357+
*/
358+
function retryPromise(options, executor) {
359+
if (executor == undefined) {
360+
executor = options;
361+
options = {};
362+
}
363+
364+
var opts = prepOpts(options);
365+
var attempts = 1;
366+
367+
return new Promise((resolve, reject) => {
368+
let retrying = false;
369+
370+
function retry(err) {
371+
if (retrying) return;
372+
retrying = true;
373+
if (attempts < opts.retries) {
374+
setTimeout(() => {
375+
attempts++;
376+
retrying = false;
377+
executor(resolve, retry, reject, attempts);
378+
}, createTimeout(attempts, opts));
379+
} else {
380+
//console.log(attempts, opts.retries);
381+
reject(err);
382+
}
383+
}
384+
385+
executor(resolve, retry, reject, attempts);
386+
});
387+
}
388+
389+
/*
390+
* Preps the options object, initializing default values and checking constraints.
391+
* @param {Object} options - The options as provided to `retryingPromise`.
392+
*/
393+
function prepOpts(options) {
394+
var opts = {
395+
retries: 10,
396+
factor: 2,
397+
minTimeout: 1000,
398+
maxTimeout: Infinity,
399+
randomize: false
400+
};
401+
for (var key in options) {
402+
opts[key] = options[key];
403+
}
404+
405+
if (opts.minTimeout > opts.maxTimeout) {
406+
throw new Error('minTimeout is greater than maxTimeout');
407+
}
408+
409+
return opts;
410+
}
411+
412+
/**
413+
* Get a timeout value in milliseconds.
414+
* @param {number} attempt - The attempt count.
415+
* @param {Object} opts - The options.
416+
* @returns {number} The timeout value in milliseconds.
417+
*/
418+
function createTimeout(attempt, opts) {
419+
var random = opts.randomize ? Math.random() + 1 : 1;
420+
421+
var timeout = Math.round(random * opts.minTimeout * Math.pow(opts.factor, attempt));
422+
timeout = Math.min(timeout, opts.maxTimeout);
423+
424+
return timeout;
425+
}

package-lock.json

Lines changed: 9 additions & 125 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-7z-forall",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "A CommonJs and ESM frontend to 7-Zip, downloads binaries in for Linux, Windows, and Mac OSX, with methods to create SFX self extracting 7z archives targeting different platforms.",
55
"type": "commonjs",
66
"main": "lib/index.js",
@@ -10,7 +10,7 @@
1010
},
1111
"scripts": {
1212
"test": "mocha --recursive test/ --timeout=0 --extension mjs --extension js",
13-
"coverage": "npx nyc@latest --reporter json --reporter text mocha --recursive test/ --timeout=0 --extension mjs --extension js && codecov -f coverage/coverage-final.json",
13+
"coverage": "npx nyc@latest --reporter json --reporter text mocha --recursive test/ --timeout=0 --extension mjs --extension js && npx codecov -f coverage/coverage-final.json",
1414
"install": "node installer.js"
1515
},
1616
"repository": {
@@ -60,18 +60,16 @@
6060
"node": ">=10.0.0"
6161
},
6262
"dependencies": {
63-
"all-unpacker": "^0.1.12",
63+
"all-unpacker": "^0.1.13",
6464
"cross-spawn": "^7.0.3",
6565
"fs-extra": "^9.0.1",
6666
"macos-release": "^2.4.1",
6767
"node-wget-fetch": "^1.0.5",
68-
"retrying-promise": "^0.0.4",
6968
"system-installer": "^1.1.5",
7069
"when": "^3.7.8"
7170
},
7271
"devDependencies": {
7372
"chai": "^4.2.0",
74-
"codecov": "^3.8.0",
7573
"mocha": "^8.2.0"
7674
}
7775
}

0 commit comments

Comments
 (0)