Skip to content

Commit 2e72bf5

Browse files
committed
Revert "corrections, remove unneeded code, retry now part of node-wget-fetch"
This reverts commit a9f37cc.
1 parent a9f37cc commit 2e72bf5

File tree

3 files changed

+189
-111
lines changed

3 files changed

+189
-111
lines changed

installer.mjs

Lines changed: 101 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from 'path';
1414
import spawn from 'cross-spawn';
1515
import unCompress from 'all-unpacker';
16-
import { wget, isString } from 'node-wget-fetch';
16+
import fetching from 'node-wget-fetch';
1717

1818
const __filename = fileURLToPath(
1919
import.meta.url);
@@ -92,43 +92,43 @@ function retrieve(path = {
9292
dest: ''
9393
}) {
9494
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+
});
102100
}
103101

104102
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({
107107
url: platformData.url + platformData.filename,
108108
dest: platformData.source
109109
}).then(() => {
110110
console.log('Extracting: ' + platformData.filename);
111-
if (isString(platformData.platform)) {
111+
if (fetching.isString(platformData.platform)) {
112112
unpack(platformData.source, platformData.destination)
113113
.then(() => {
114114
return resolve(platformData.platform);
115115
})
116-
.catch((err) => reject(err));
116+
.catch((err) => retry(err));
117117
}
118-
}).catch((err) => reject(err));
118+
}).catch((err) => retry(err));
119119
}).catch((err) => console.error(err));
120120
}
121121

122122
function unpack(source, destination, toCopy) {
123123
return new Promise((resolve, reject) => {
124124
return unCompress.unpack(
125125
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+
},
132132
(err, files, text) => {
133133
if (err)
134134
return reject(err);
@@ -170,19 +170,97 @@ function makeExecutable(binary = [], binaryFolder = '') {
170170
});
171171
}
172172

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+
173251
let extractionPromises = [];
174252
let platforms = [linuxPlatform, appleMacPlatform, windowsOtherPlatform];
175253
if (process.platform == 'win32')
176254
platforms = [linuxPlatform, appleMacPlatform, windowsPlatform, windowsOtherPlatform];
177255

178256
platforms.forEach((dataFor) => {
179257
fs.mkdir(dataFor.destination, (err) => {
180-
if (err) { }
258+
if (err) {}
181259
});
182260
const extracted = retrieve({
183-
url: _7zAppUrl + dataFor.extraName,
184-
dest: dataFor.extraSourceFile
185-
})
261+
url: _7zAppUrl + dataFor.extraName,
262+
dest: dataFor.extraSourceFile
263+
})
186264
.then(() => {
187265
return platformUnpacker(dataFor)
188266
.then(() => {

package-lock.json

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

package.json

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,86 @@
11
{
2-
"name": "node-7z-archive",
3-
"version": "0.9.4",
4-
"description": "ESM front-end to 7-Zip, featuring alternative full 7z CLI tools, binaries for Linux, Windows, Mac OSX, seamlessly create 7zip SFX self extracting archives targeting different platforms.",
5-
"type": "module",
6-
"main": "lib/index.mjs",
7-
"scripts": {
8-
"test": "mocha --recursive test/ --timeout=0",
9-
"report": "npx c8 npm test",
10-
"coverage": "npx c8 --reporter json npm test && npx codecov -f coverage/coverage-final.json",
11-
"install": "node installer.mjs"
12-
},
13-
"repository": {
14-
"type": "git",
15-
"url": "https://github.com/techno-express/node-7z-archive.git"
16-
},
17-
"keywords": [
18-
"node",
19-
"esm",
20-
"7z",
21-
"7za",
22-
"7zr",
23-
"p7zip",
24-
"7zip",
25-
"archive",
26-
"sfx",
27-
"binary",
28-
"wrapper",
29-
"Mac OS X",
30-
"Windows",
31-
"Linux",
32-
"Apple",
33-
"cross-platform"
34-
],
35-
"author": "l. stubbs <[email protected]>",
36-
"contributors": [
37-
"HelloGravity",
38-
"sketchpunk",
39-
"Dannii Willis <[email protected]>",
40-
"redx25 <[email protected]>",
41-
"Quentin Rossetti <[email protected]>",
42-
"František Gič <[email protected]>",
43-
"Oskar Larsson Högfeldt <[email protected]>"
44-
],
45-
"license": "MIT",
46-
"bugs": {
47-
"url": "https://github.com/techno-express/node-7z-archive/issues"
48-
},
49-
"homepage": "https://github.com/techno-express/node-7z-archive.git",
50-
"files": [
51-
"*.js",
52-
"*.mjs",
53-
"LICENSE",
54-
"README.md",
55-
"lib",
56-
"util",
57-
"cli"
58-
],
59-
"bin": {
60-
"7zip": "cli/7zip.js",
61-
"createArchive": "cli/create.js",
62-
"deleteArchive": "cli/delete.js",
63-
"extractArchive": "cli/extract.js",
64-
"fullArchive": "cli/full.js",
65-
"listArchive": "cli/list.js",
66-
"renameArchive": "cli/rename.js",
67-
"createSfx": "cli/sfx.js",
68-
"testArchive": "cli/test.js",
69-
"updateArchive": "cli/update.js"
70-
},
71-
"engines": {
72-
"node": ">=12.0.0"
73-
},
74-
"dependencies": {
75-
"all-unpacker": "^0.1.13",
76-
"cross-spawn": "^7.0.3",
77-
"fs-extra": "^9.0.1",
78-
"node-wget-fetch": "^1.1.0",
79-
"when": "^3.7.8",
80-
"minimist": "1.2.5"
81-
},
82-
"devDependencies": {
83-
"chai": "^4.2.0",
84-
"mocha": "^8.2.1"
85-
}
2+
"name": "node-7z-archive",
3+
"version": "0.9.3",
4+
"description": "ESM front-end to 7-Zip, featuring alternative full 7z CLI tools, binaries for Linux, Windows, Mac OSX, seamlessly create 7zip SFX self extracting archives targeting different platforms.",
5+
"type": "module",
6+
"main": "lib/index.mjs",
7+
"scripts": {
8+
"test": "mocha --recursive test/ --timeout=0",
9+
"report": "npx c8 npm test",
10+
"coverage": "npx c8 --reporter json npm test && npx codecov -f coverage/coverage-final.json",
11+
"install": "node installer.mjs"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/techno-express/node-7z-archive.git"
16+
},
17+
"keywords": [
18+
"node",
19+
"esm",
20+
"7z",
21+
"7za",
22+
"7zr",
23+
"p7zip",
24+
"7zip",
25+
"archive",
26+
"sfx",
27+
"binary",
28+
"wrapper",
29+
"Mac OS X",
30+
"Windows",
31+
"Linux",
32+
"Apple",
33+
"cross-platform"
34+
],
35+
"author": "l. stubbs <[email protected]>",
36+
"contributors": [
37+
"HelloGravity",
38+
"sketchpunk",
39+
"Dannii Willis <[email protected]>",
40+
"redx25 <[email protected]>",
41+
"Quentin Rossetti <[email protected]>",
42+
"František Gič <[email protected]>",
43+
"Oskar Larsson Högfeldt <[email protected]>"
44+
],
45+
"license": "MIT",
46+
"bugs": {
47+
"url": "https://github.com/techno-express/node-7z-archive/issues"
48+
},
49+
"homepage": "https://github.com/techno-express/node-7z-archive.git",
50+
"files": [
51+
"*.js",
52+
"*.mjs",
53+
"LICENSE",
54+
"README.md",
55+
"lib",
56+
"util",
57+
"cli"
58+
],
59+
"bin": {
60+
"7zip": "cli/7zip.js",
61+
"createArchive": "cli/create.js",
62+
"deleteArchive": "cli/delete.js",
63+
"extractArchive": "cli/extract.js",
64+
"fullArchive": "cli/full.js",
65+
"listArchive": "cli/list.js",
66+
"renameArchive": "cli/rename.js",
67+
"createSfx": "cli/sfx.js",
68+
"testArchive": "cli/test.js",
69+
"updateArchive": "cli/update.js"
70+
},
71+
"engines": {
72+
"node": ">=12.0.0"
73+
},
74+
"dependencies": {
75+
"all-unpacker": "^0.1.13",
76+
"cross-spawn": "^7.0.3",
77+
"fs-extra": "^9.0.1",
78+
"node-wget-fetch": "^1.0.5",
79+
"when": "^3.7.8",
80+
"minimist": "1.2.5"
81+
},
82+
"devDependencies": {
83+
"chai": "^4.2.0",
84+
"mocha": "^8.2.1"
85+
}
8686
}

0 commit comments

Comments
 (0)