From 8ddc75ba5fd5b2d4bc96f0d60bebd9dfe0b6c911 Mon Sep 17 00:00:00 2001 From: chaitanyapotti Date: Fri, 16 Aug 2019 11:12:38 +0800 Subject: [PATCH 1/3] Fix for when localStorage is available but access is restricted --- packages/web3-eth-accounts/src/index.js | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 066343a0ae5..315af1eac58 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -525,10 +525,35 @@ Wallet.prototype.load = function (password, keyName) { return this.decrypt(keystore || [], password); }; -if (typeof localStorage === 'undefined') { +if (!storageAvailable('localStorage')) { delete Wallet.prototype.save; delete Wallet.prototype.load; } +function storageAvailable(type) { + var storage; + try { + storage = window[type]; + var x = '__storage_test__'; + storage.setItem(x, x); + storage.removeItem(x); + return true; + } + catch(e) { + return e instanceof DOMException && ( + // everything except Firefox + e.code === 22 || + // Firefox + e.code === 1014 || + // test name field too, because code might not be present + // everything except Firefox + e.name === 'QuotaExceededError' || + // Firefox + e.name === 'NS_ERROR_DOM_QUOTA_REACHED') && + // acknowledge QuotaExceededError only if there's something already stored + (storage && storage.length !== 0); + } +} + module.exports = Accounts; From f1e51ffd1b211715a1d1c94a801f24ada42cb5c8 Mon Sep 17 00:00:00 2001 From: chaitanyapotti Date: Fri, 16 Aug 2019 11:21:55 +0800 Subject: [PATCH 2/3] DOMException not available in node --- packages/web3-eth-accounts/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 315af1eac58..43c454ae5a8 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -540,7 +540,7 @@ function storageAvailable(type) { return true; } catch(e) { - return e instanceof DOMException && ( + return e && ( // everything except Firefox e.code === 22 || // Firefox From dbac50f41769ece9ac342713b5fa80adccc555fb Mon Sep 17 00:00:00 2001 From: chaitanyapotti Date: Sun, 25 Aug 2019 10:25:10 +0800 Subject: [PATCH 3/3] added comments --- packages/web3-eth-accounts/src/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 43c454ae5a8..986e185918b 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -530,6 +530,15 @@ if (!storageAvailable('localStorage')) { delete Wallet.prototype.load; } +/** + * Checks whether a storage type is available or not + * For more info on how this works, please refer to MDN documentation + * https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Feature-detecting_localStorage + * + * @method storageAvailable + * @param {String} type the type of storage ('localStorage', 'sessionStorage') + * @returns {Boolean} a boolean indicating whether the specified storage is available or not + */ function storageAvailable(type) { var storage; try {