locize-cli 7.15.0 → 7.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/package.json +1 -1
  3. package/request.js +32 -25
package/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  Project versioning adheres to [Semantic Versioning](http://semver.org/).
6
6
  Change log format is based on [Keep a Changelog](http://keepachangelog.com/).
7
7
 
8
+ ## [7.15.1](https://github.com/locize/locize-cli/compare/v7.15.0...v7.15.1) - 2023-12-19
9
+
10
+ - refactor retriable requests handling
11
+
8
12
  ## [7.15.0](https://github.com/locize/locize-cli/compare/v7.14.15...v7.15.0) - 2023-12-18
9
13
 
10
14
  - locize sync: show publish mode advice if necessary
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "locize-cli",
3
- "version": "7.15.0",
3
+ "version": "7.15.1",
4
4
  "description": "locize cli to import locales",
5
5
  "main": "index.js",
6
6
  "bin": {
package/request.js CHANGED
@@ -8,6 +8,28 @@ cacheable.install(https.globalAgent);
8
8
 
9
9
  const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY || process.env.https_proxy || process.env.HTTPS_PROXY;
10
10
 
11
+ const isRetriableError = (err) => {
12
+ return err && err.message && (
13
+ err.message.indexOf('ETIMEDOUT') > -1 || // on timeout retry
14
+ // on dns errors
15
+ err.message.indexOf('ENOTFOUND') > -1 ||
16
+ err.message.indexOf('ENODATA') > -1 ||
17
+ err.message.indexOf('ENOENT') > -1 // Windows: name exists, but not this record type
18
+ );
19
+ };
20
+
21
+ const isJSONResponse = (res) => res.headers.get('content-type') && res.headers.get('content-type').indexOf('json') > 0;
22
+
23
+ const handleResponse = (res) => {
24
+ if (isJSONResponse(res)) {
25
+ return new Promise((resolve, reject) => res.json().then((obj) => resolve({ res, obj })).catch(reject));
26
+ } else {
27
+ return { res };
28
+ }
29
+ };
30
+
31
+ const handleSuccessful = (callback) => (ret) => callback(null, ret.res, ret.obj);
32
+
11
33
  module.exports = (url, options, callback) => {
12
34
  if (httpProxy) {
13
35
  const httpsProxyAgent = new HttpsProxyAgent(httpProxy);
@@ -25,31 +47,16 @@ module.exports = (url, options, callback) => {
25
47
  }
26
48
  }
27
49
  if (options.headers['Authorization'] === undefined) delete options.headers['Authorization'];
28
- fetch(url, options).then((res) => {
29
- if (res.headers.get('content-type') && res.headers.get('content-type').indexOf('json') > 0) {
30
- return new Promise((resolve, reject) => res.json().then((obj) => resolve({ res, obj })).catch(reject));
31
- } else {
32
- return { res };
33
- }
34
- }).then((ret) => callback(null, ret.res, ret.obj)).catch((err) => {
35
- if (err && err.message && (
36
- err.message.indexOf('ETIMEDOUT') > -1 || // on timeout retry
37
- // on dns errors
38
- err.message.indexOf('ENOTFOUND') > -1 ||
39
- err.message.indexOf('ENODATA') > -1 ||
40
- err.message.indexOf('ENOENT') > -1 // Windows: name exists, but not this record type
41
- )) {
50
+
51
+ function retriableFetch(maxRetries) {
52
+ fetch(url, options).then(handleResponse).then(handleSuccessful(callback)).catch((err) => {
53
+ if (maxRetries < 1) return callback(err);
54
+ if (!isRetriableError(err)) return callback(err);
42
55
  setTimeout(() => {
43
- fetch(url, options).then((res) => {
44
- if (res.headers.get('content-type') && res.headers.get('content-type').indexOf('json') > 0) {
45
- return new Promise((resolve, reject) => res.json().then((obj) => resolve({ res, obj })).catch(reject));
46
- } else {
47
- return { res };
48
- }
49
- }).then((ret) => callback(null, ret.res, ret.obj)).catch(callback);
56
+ retriableFetch(--maxRetries);
50
57
  }, 5000);
51
- return;
52
- }
53
- callback(err);
54
- });
58
+ });
59
+ }
60
+
61
+ retriableFetch(3);
55
62
  };