From 67e2a78ced37e52dcc628e90d3187d60262b4382 Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Mon, 19 Aug 2024 10:59:16 +0200 Subject: [PATCH] optimize "Failed to fetch" retry case --- CHANGELOG.md | 4 ++++ i18nextLocizeBackend.js | 16 +++++++--------- i18nextLocizeBackend.min.js | 2 +- lib/index.js | 2 +- package.json | 18 +++++++++--------- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 668176a..075ade5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 6.5.0 + +- optimize "Failed to fetch" retry case + ### 6.4.3 - dereference timers in node.js so that the process may exit when finished diff --git a/i18nextLocizeBackend.js b/i18nextLocizeBackend.js index adfaaff..8622a5e 100644 --- a/i18nextLocizeBackend.js +++ b/i18nextLocizeBackend.js @@ -28,11 +28,11 @@ Object.defineProperty(exports, "__esModule", { exports.default = void 0; var _utils = require("./utils.js"); var _request = _interopRequireDefault(require("./request.js")); -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } +function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } -function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } +function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } +function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } +function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var getDefaults = function getDefaults() { @@ -450,11 +450,9 @@ var I18NextLocizeBackend = function () { resourceNotExisting: resourceNotExisting }); } - if (!res && err && err.message && err.message.indexOf('Failed to fetch') > -1) { - return callback('failed loading ' + url, true, { - resourceNotExisting: resourceNotExisting - }); - } + if (!res && err && err.message && (err.message.indexOf('Failed to fetch') > -1 || err.message.indexOf('failed') > -1 && err.message.indexOf('fetch') > -1)) return callback('failed loading ' + url + ': ' + err.message, true, { + resourceNotExisting: resourceNotExisting + }); if (err) return callback(err, false); var ret, parseErr; try { diff --git a/i18nextLocizeBackend.min.js b/i18nextLocizeBackend.min.js index cbf0bb5..a73a882 100644 --- a/i18nextLocizeBackend.min.js +++ b/i18nextLocizeBackend.min.js @@ -1 +1 @@ -!function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).i18nextLocizeBackend=e()}(function(){return function n(i,r,s){function a(t,e){if(!r[t]){if(!i[t]){var o="function"==typeof require&&require;if(!e&&o)return o(t,!0);if(c)return c(t,!0);throw(e=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",e}o=r[t]={exports:{}},i[t][0].call(o.exports,function(e){return a(i[t][1][e]||e)},o,o.exports,n,i,r,s)}return r[t].exports}for(var c="function"==typeof require&&require,e=0;eo&&(window.localStorage.removeItem("notExistingLocizeProject_".concat(e)),1))}):"undefined"!=typeof document&&(a=function(e){var t=new Date,t=(t.setTime(t.getTime()+o),"; expires=".concat(t.toGMTString())),e="notExistingLocizeProject_".concat(e);try{document.cookie="".concat(e,"=").concat(Date.now()).concat(t,";path=/")}catch(e){}},s=function(e){var e="notExistingLocizeProject_".concat(e),t="".concat(e,"=");try{for(var o=document.cookie.split(";"),n=0;n=i.options.translatedPercentageThreshold&&e.push(t),e},[]),e=e.reduce(function(e,t){return-1(e=((e,t)=>{if("object"!=c(e)||!e)return e;var o=e[Symbol.toPrimitive];if(void 0===o)return("string"===t?String:Number)(e);if("object"!=c(o=o.call(e,t||"default")))return o;throw new TypeError("@@toPrimitive must return a primitive value.")})(e,"string"),"symbol"==c(e)?e:e+""))(n.key),n)}}try{var u="undefined"!=typeof window&&null!==window.localStorage,i="notExistingLocizeProject";window.localStorage.setItem(i,"foo"),window.localStorage.removeItem(i)}catch(e){u=!1}function p(e,t,o){var n={};return t.authorize&&t.apiKey&&(n.Authorization=t.apiKey),(o||t.setContentTypeJSON)&&(n["Content-Type"]="application/json"),{method:o?"POST":"GET",url:e,headers:n,body:o}}function h(e,t,o){if(1===e.request.length)try{var n=e.request(t);n&&"function"==typeof n.then?n.then(function(e){return o(null,e)}).catch(o):o(null,n)}catch(e){o(e)}else e.request(t,o)}e=function e(t){var o=1o&&(window.localStorage.removeItem("notExistingLocizeProject_".concat(e)),1))}):"undefined"!=typeof document&&(a=function(e){var t=new Date,t=(t.setTime(t.getTime()+o),"; expires=".concat(t.toGMTString())),e="notExistingLocizeProject_".concat(e);try{document.cookie="".concat(e,"=").concat(Date.now()).concat(t,";path=/")}catch(e){}},r=function(e){var e="notExistingLocizeProject_".concat(e),t="".concat(e,"=");try{for(var o=document.cookie.split(";"),n=0;n=i.options.translatedPercentageThreshold&&e.push(t),e},[]),e=e.reduce(function(e,t){return-1{if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=l(e)&&"function"!=typeof e)return{default:e};if((t=u(t))&&t.has(e))return t.get(e);var o,n,i={__proto__:null},s=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(o in e)"default"!==o&&{}.hasOwnProperty.call(e,o)&&((n=s?Object.getOwnPropertyDescriptor(e,o):null)&&(n.get||n.set)?Object.defineProperty(i,o,n):i[o]=e[o]);return i.default=e,t&&t.set(e,i),i})(t("./getFetch.js"));function u(e){var t,o;return"function"!=typeof WeakMap?null:(t=new WeakMap,o=new WeakMap,(u=function(e){return e?o:t})(e))}function l(e){return(l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}"function"==typeof fetch&&(r=void 0!==d&&d.fetch?d.fetch:"undefined"!=typeof window&&window.fetch?window.fetch:fetch),"function"!=typeof XMLHttpRequest&&"object"!==("undefined"==typeof XMLHttpRequest?"undefined":l(XMLHttpRequest))||(void 0!==d&&d.XMLHttpRequest?a=d.XMLHttpRequest:"undefined"!=typeof window&&window.XMLHttpRequest&&(a=window.XMLHttpRequest)),"function"==typeof ActiveXObject&&(void 0!==d&&d.ActiveXObject?c=d.ActiveXObject:"undefined"!=typeof window&&window.ActiveXObject&&(c=window.ActiveXObject)),"function"!=typeof(r=r||!e||a||c?r:e.default||e)&&(r=void 0);function f(e,t,o,n){function i(t){var o=t.headers&&"Error from cloudfront"===t.headers.get("x-cache");if(!t.ok)return n(t.statusText||"Error",{status:t.status,resourceNotExisting:o});t.text().then(function(e){n(null,{status:t.status,data:e,resourceNotExisting:o})}).catch(n)}var s={};"undefined"==typeof window&&void 0!==d&&void 0!==d.process&&d.process.versions&&d.process.versions.node&&(s["User-Agent"]="i18next-locize-backend (node/".concat(d.process.version,"; ").concat(d.process.platform," ").concat(d.process.arch,")")),e.authorize&&e.apiKey&&(s.Authorization=e.apiKey),(o||e.setContentTypeJSON)&&(s["Content-Type"]="application/json"),("function"==typeof fetch?fetch:r)(t,{method:o?"POST":"GET",body:o?JSON.stringify(o):void 0,headers:s}).then(i).catch(n)}n.default=function(e,t,o,n){if("function"==typeof o&&(n=o,o=void 0),n=n||function(){},r)return f(e,t,o,n);if("function"==typeof XMLHttpRequest||"object"===("undefined"==typeof XMLHttpRequest?"undefined":l(XMLHttpRequest))||"function"==typeof ActiveXObject){var i=n;try{var s=a?new a:new c("MSXML2.XMLHTTP.3.0");s.open(o?"POST":"GET",t,1),e.crossDomain||s.setRequestHeader("X-Requested-With","XMLHttpRequest"),e.authorize&&e.apiKey&&s.setRequestHeader("Authorization",e.apiKey),(o||e.setContentTypeJSON)&&s.setRequestHeader("Content-Type","application/json"),s.onreadystatechange=function(){var e="Error from cloudfront"===s.getResponseHeader("x-cache");3null==e?"":""+e)(i):i)||"").replace(/\$/g,"$$$$"),e=e.replace(n[0],t[i]||i),r.lastIndex=0;return e},o.isMissingOption=a,o.optionExist=function(e,t){return!a(e,t)},o.pushPath=function(e,t,o,n){e=s(e,t,Object),t=e.obj,e=e.k;t[e]=t[e]||[],n&&(t[e]=t[e].concat(o));n||t[e].push(o)},o.setPath=function(e,t,o){e=s(e,t,Object);e.obj[e.k]=o};var o=[],n=o.forEach,i=o.slice;function s(e,t,o){function n(e){return e&&-1= 500 && res.status < 600) || !res.status)) { return callback('failed loading ' + url, true /* retry */, { resourceNotExisting }) } if (res && res.status >= 400 && res.status < 500) { return callback('failed loading ' + url, false /* no retry */, { resourceNotExisting }) } - if (!res && err && err.message && err.message.indexOf('Failed to fetch') > -1) { return callback('failed loading ' + url, true /* retry */, { resourceNotExisting }) } + if (!res && err && err.message && (err.message.indexOf('Failed to fetch') > -1 || (err.message.indexOf('failed') > -1 && err.message.indexOf('fetch') > -1))) return callback('failed loading ' + url + ': ' + err.message, true /* retry */, { resourceNotExisting }) if (err) return callback(err, false) let ret, parseErr diff --git a/package.json b/package.json index 5521e9c..d1803a1 100644 --- a/package.json +++ b/package.json @@ -42,27 +42,27 @@ }, "types": "./index.d.mts", "devDependencies": { - "@babel/cli": "7.24.5", - "@babel/core": "7.24.5", - "@babel/preset-env": "7.24.5", + "@babel/cli": "7.24.8", + "@babel/core": "7.25.2", + "@babel/preset-env": "7.25.3", "babel-plugin-add-module-exports": "1.0.4", "browserify": "17.0.0", "dtslint": "4.2.1", "eslint": "8.53.0", "eslint-config-standard": "17.1.0", "eslint-plugin-import": "2.29.1", - "eslint-plugin-n": "16.3.1", + "eslint-plugin-n": "16.6.2", "eslint-plugin-promise": "6.1.1", "eslint-plugin-require-path-exists": "1.1.9", "eslint-plugin-standard": "5.0.0", "expect.js": "0.3.1", - "i18next": "23.11.4", + "i18next": "23.14.0", "json-server": "0.17.4", - "mocha": "10.4.0", + "mocha": "10.7.3", "tslint": "5.20.1", - "tsd": "0.31.0", - "typescript": "5.4.5", - "uglify-js": "3.17.4", + "tsd": "0.31.1", + "typescript": "5.5.4", + "uglify-js": "3.19.2", "xmlhttprequest": "1.8.0" }, "scripts": {