From b51a7b288ab979f98cbc7ec70c7dad9dac6a2231 Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Sun, 29 May 2022 02:35:31 +0200 Subject: [PATCH] Fix MSP_BOARD_INFO && Storage Quota Cleanup LocalStorage --- src/js/ConfigStorage.js | 19 +++++++++----- src/js/FirmwareCache.js | 16 ++++++------ src/js/SessionStorage.js | 45 +++++++++++++++++++++++++++++++++ src/js/jenkins_loader.js | 9 +++---- src/js/main.js | 22 ++++++++++++++++ src/js/msp/MSPHelper.js | 29 +++++++++++---------- src/js/release_checker.js | 4 +-- src/js/tabs/firmware_flasher.js | 27 ++++++++++++-------- src/main.html | 1 + 9 files changed, 126 insertions(+), 46 deletions(-) create mode 100644 src/js/SessionStorage.js diff --git a/src/js/ConfigStorage.js b/src/js/ConfigStorage.js index 7708d3e4..7bf49368 100644 --- a/src/js/ConfigStorage.js +++ b/src/js/ConfigStorage.js @@ -9,18 +9,18 @@ const ConfigStorage = { if (Array.isArray(key)) { key.forEach(function (element) { try { - result = {...result, ...JSON.parse(window.localStorage.getItem(element))}; + result = {...result, ...JSON.parse(localStorage.getItem(element))}; } catch (e) { - // is okay + console.error(e); } }); } else { - const keyValue = window.localStorage.getItem(key); + const keyValue = localStorage.getItem(key); if (keyValue) { try { result = JSON.parse(keyValue); } catch (e) { - // It's fine if we fail that parse + console.error(e); } } } @@ -32,10 +32,17 @@ const ConfigStorage = { Object.keys(input).forEach(function (element) { const tmpObj = {}; tmpObj[element] = input[element]; - window.localStorage.setItem(element, JSON.stringify(tmpObj)); + try { + localStorage.setItem(element, JSON.stringify(tmpObj)); + } catch (e) { + console.error(e); + } }); }, remove: function(item) { - window.localStorage.removeItem(item); + localStorage.removeItem(item); + }, + clear: function() { + localStorage.clear(); }, }; diff --git a/src/js/FirmwareCache.js b/src/js/FirmwareCache.js index 0d472d8e..cff5c48a 100644 --- a/src/js/FirmwareCache.js +++ b/src/js/FirmwareCache.js @@ -44,14 +44,14 @@ let FirmwareCache = (function () { function persist(data) { let obj = {}; obj[CACHEKEY] = data; - ConfigStorage.set(obj); + SessionStorage.set(obj); } /** * @param {Function} callback */ function load(callback) { - const obj = ConfigStorage.get(CACHEKEY); + const obj = SessionStorage.get(CACHEKEY); let entries = typeof obj === "object" && obj.hasOwnProperty(CACHEKEY) ? obj[CACHEKEY] : []; @@ -75,13 +75,13 @@ let FirmwareCache = (function () { } let key = oldest[0]; let cacheKey = withCachePrefix(key); - const obj = ConfigStorage.get(cacheKey); + const obj = SessionStorage.get(cacheKey); /** @type {CacheItem} */ const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null; if (cached === null) { return undefined; } - ConfigStorage.remove(cacheKey); + SessionStorage.remove(cacheKey); onRemoveFromCache(cached.release); return oldest; }; @@ -138,7 +138,7 @@ let FirmwareCache = (function () { release: release, hexdata: hexdata, }; - ConfigStorage.set(obj); + SessionStorage.set(obj); onPutToCache(release); } @@ -157,7 +157,7 @@ let FirmwareCache = (function () { return; } let cacheKey = withCachePrefix(key); - const obj = ConfigStorage.get(cacheKey); + const obj = SessionStorage.get(cacheKey); const cached = typeof obj === "object" && obj.hasOwnProperty(cacheKey) ? obj[cacheKey] : null; callback(cached); } @@ -174,7 +174,7 @@ let FirmwareCache = (function () { for (let key of journal.keys()) { cacheKeys.push(withCachePrefix(key)); } - const obj = ConfigStorage.get(cacheKeys); + const obj = SessionStorage.get(cacheKeys); if (typeof obj !== "object") { return; } @@ -186,7 +186,7 @@ let FirmwareCache = (function () { onRemoveFromCache(item.release); } } - ConfigStorage.remove(cacheKeys); + SessionStorage.remove(cacheKeys); journal.clear(); JournalStorage.persist(journal.toJSON()); } diff --git a/src/js/SessionStorage.js b/src/js/SessionStorage.js new file mode 100644 index 00000000..b25f395e --- /dev/null +++ b/src/js/SessionStorage.js @@ -0,0 +1,45 @@ +'use strict'; + +const SessionStorage = { + // key can be one string, or array of strings + get: function(key) { + let result = {}; + if (Array.isArray(key)) { + key.forEach(function (element) { + try { + result = {...result, ...JSON.parse(sessionStorage.getItem(element))}; + } catch (e) { + console.error(e); + } + }); + } else { + const keyValue = sessionStorage.getItem(key); + if (keyValue) { + try { + result = JSON.parse(keyValue); + } catch (e) { + console.error(e); + } + } + } + + return result; + }, + set: function(input) { + Object.keys(input).forEach(function (element) { + const tmpObj = {}; + tmpObj[element] = input[element]; + try { + sessionStorage.setItem(element, JSON.stringify(tmpObj)); + } catch (e) { + console.error(e); + } + }); + }, + remove: function(item) { + sessionStorage.removeItem(item); + }, + clear: function() { + sessionStorage.clear(); + }, +}; diff --git a/src/js/jenkins_loader.js b/src/js/jenkins_loader.js index 1f2a9415..98b2688f 100644 --- a/src/js/jenkins_loader.js +++ b/src/js/jenkins_loader.js @@ -21,7 +21,7 @@ JenkinsLoader.prototype.loadJobs = function (viewName, callback) { callback(jobs); }; - const result = ConfigStorage.get([cacheLastUpdateTag, jobsDataTag]); + const result = SessionStorage.get([cacheLastUpdateTag, jobsDataTag]); const jobsDataTimestamp = $.now(); const cachedJobsData = result[jobsDataTag]; const cachedJobsLastUpdate = result[cacheLastUpdateTag]; @@ -49,7 +49,7 @@ JenkinsLoader.prototype.loadJobs = function (viewName, callback) { const object = {}; object[jobsDataTag] = jobs; object[cacheLastUpdateTag] = $.now(); - ConfigStorage.set(object); + SessionStorage.set(object); wrappedCallback(jobs); }).fail(xhr => { @@ -68,7 +68,7 @@ JenkinsLoader.prototype.loadBuilds = function (jobName, callback) { const buildsDataTag = `${jobUrl}BuildsData`; const cacheLastUpdateTag = `${jobUrl}BuildsLastUpdate`; - const result = ConfigStorage.get([cacheLastUpdateTag, buildsDataTag]); + const result = SessionStorage.get([cacheLastUpdateTag, buildsDataTag]); const buildsDataTimestamp = $.now(); const cachedBuildsData = result[buildsDataTag]; const cachedBuildsLastUpdate = result[cacheLastUpdateTag]; @@ -100,8 +100,7 @@ JenkinsLoader.prototype.loadBuilds = function (jobName, callback) { const object = {}; object[buildsDataTag] = builds; object[cacheLastUpdateTag] = $.now(); - ConfigStorage.set(object); - + SessionStorage.set(object); self._parseBuilds(jobUrl, jobName, builds, callback); }).fail(xhr => { GUI.log(i18n.getMessage('buildServerLoadFailed', [jobName, `HTTP ${xhr.status}`])); diff --git a/src/js/main.js b/src/js/main.js index ddda8a46..4926db60 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -28,9 +28,31 @@ function readConfiguratorVersionMetadata() { CONFIGURATOR.gitRevision = manifest.gitRevision; } +function cleanupLocalStorage() { + + const cleanupLocalStorageList = [ + 'cache', + 'firmware', + 'https', + 'selected_board', + 'unifiedConfigLast', + 'unifiedSourceCache', + ]; + + for (const key in localStorage) { + for (const item of cleanupLocalStorageList) { + if (key.includes(item)) { + localStorage.removeItem(key); + } + } + } +} + function appReady() { readConfiguratorVersionMetadata(); + cleanupLocalStorage(); + i18n.init(function() { startProcess(); diff --git a/src/js/msp/MSPHelper.js b/src/js/msp/MSPHelper.js index 89c3686c..f3707a89 100644 --- a/src/js/msp/MSPHelper.js +++ b/src/js/msp/MSPHelper.js @@ -843,38 +843,43 @@ MspHelper.prototype.process_data = function(dataHandler) { break; case MSPCodes.MSP_BOARD_INFO: - let boardIdentifier = ''; + FC.CONFIG.boardIdentifier = ''; + for (let i = 0; i < 4; i++) { - boardIdentifier += String.fromCharCode(data.readU8()); + FC.CONFIG.boardIdentifier += String.fromCharCode(data.readU8()); } - FC.CONFIG.boardIdentifier = boardIdentifier; + FC.CONFIG.boardVersion = data.readU16(); + FC.CONFIG.boardType = 0; if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_35)) { FC.CONFIG.boardType = data.readU8(); - } else { - FC.CONFIG.boardType = 0; } + FC.CONFIG.targetCapabilities = 0; + FC.CONFIG.targetName = ''; + if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_37)) { FC.CONFIG.targetCapabilities = data.readU8(); - - let length = data.readU8(); + const length = data.readU8(); for (let i = 0; i < length; i++) { FC.CONFIG.targetName += String.fromCharCode(data.readU8()); } - } else { - FC.CONFIG.targetCapabilities = 0; - FC.CONFIG.targetName = ""; } + FC.CONFIG.boardName = ''; + FC.CONFIG.manufacturerId = ''; + FC.CONFIG.signature = []; + if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_39)) { let length = data.readU8(); + for (let i = 0; i < length; i++) { FC.CONFIG.boardName += String.fromCharCode(data.readU8()); } length = data.readU8(); + for (let i = 0; i < length; i++) { FC.CONFIG.manufacturerId += String.fromCharCode(data.readU8()); } @@ -882,10 +887,6 @@ MspHelper.prototype.process_data = function(dataHandler) { for (let i = 0; i < self.SIGNATURE_LENGTH; i++) { FC.CONFIG.signature.push(data.readU8()); } - } else { - FC.CONFIG.boardName = ""; - FC.CONFIG.manufacturerId = ""; - FC.CONFIG.signature = []; } if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_41)) { diff --git a/src/js/release_checker.js b/src/js/release_checker.js index d96a67d5..73f08aef 100644 --- a/src/js/release_checker.js +++ b/src/js/release_checker.js @@ -11,7 +11,7 @@ const ReleaseChecker = function (releaseName, releaseUrl) { ReleaseChecker.prototype.loadReleaseData = function (processFunction) { const self = this; - const result = ConfigStorage.get([self._releaseLastUpdateTag, self._releaseDataTag]); + const result = SessionStorage.get([self._releaseLastUpdateTag, self._releaseDataTag]); const releaseDataTimestamp = $.now(); const cacheReleaseData = result[self._releaseDataTag]; const cachedReleaseLastUpdate = result[self._releaseLastUpdateTag]; @@ -23,7 +23,7 @@ ReleaseChecker.prototype.loadReleaseData = function (processFunction) { const data = {}; data[self._releaseDataTag] = releaseData; data[self._releaseLastUpdateTag] = releaseDataTimestamp; - ConfigStorage.set(data); + SessionStorage.set(data); self._processReleaseData(releaseData, processFunction); }).fail(function (data) { diff --git a/src/js/tabs/firmware_flasher.js b/src/js/tabs/firmware_flasher.js index 673ae667..5b1eae2d 100644 --- a/src/js/tabs/firmware_flasher.js +++ b/src/js/tabs/firmware_flasher.js @@ -158,7 +158,7 @@ firmware_flasher.initialize = function (callback) { TABS.firmware_flasher.releases = builds; - result = ConfigStorage.get('selected_board'); + result = SessionStorage.get('selected_board'); if (result.selected_board) { const boardBuilds = builds[result.selected_board]; $('select[name="board"]').val(boardBuilds ? result.selected_board : 0).trigger('change'); @@ -243,7 +243,7 @@ firmware_flasher.initialize = function (callback) { if (builds && hasUnifiedTargetBuild(builds)) { console.log('loaded some builds for later'); const storageTag = 'unifiedSourceCache'; - result = ConfigStorage.get(storageTag); + result = SessionStorage.get(storageTag); let storageObj = result[storageTag]; if (!storageObj || !storageObj.lastUpdate || checkTime - storageObj.lastUpdate > expirationPeriod) { console.log('go get', unifiedSource); @@ -254,7 +254,7 @@ firmware_flasher.initialize = function (callback) { newDataObj.lastUpdate = checkTime; newDataObj.data = data; newStorageObj[storageTag] = newDataObj; - ConfigStorage.set(newStorageObj); + SessionStorage.set(newStorageObj); parseUnifiedBuilds(data, builds); }).fail(xhr => { @@ -312,7 +312,7 @@ firmware_flasher.initialize = function (callback) { TABS.firmware_flasher.releases = releases; TABS.firmware_flasher.unifiedConfigs = unifiedConfigs; - result = ConfigStorage.get('selected_board'); + result = SessionStorage.get('selected_board'); if (result.selected_board) { const boardReleases = TABS.firmware_flasher.unifiedConfigs[result.selected_board] || TABS.firmware_flasher.releases[result.selected_board]; @@ -410,7 +410,12 @@ firmware_flasher.initialize = function (callback) { if (!GUI.connect_lock) { TABS.firmware_flasher.unifiedConfigs = {}; - buildTypesToShow[build_type].loader(); + + try { + buildTypesToShow[build_type].loader(); + } catch (err) { + console.error(err); + } } ConfigStorage.set({'selected_build_type': build_type}); @@ -554,7 +559,7 @@ firmware_flasher.initialize = function (callback) { } if (target !== '0') { - ConfigStorage.set({'selected_board': target}); + SessionStorage.set({'selected_board': target}); } TABS.firmware_flasher.selectedBoard = target; @@ -610,7 +615,7 @@ firmware_flasher.initialize = function (callback) { const storageTag = 'unifiedConfigLast'; const expirationPeriod = 3600; // One of your earth hours. const checkTime = Math.floor(Date.now() / 1000); // Lets deal in seconds. - result = ConfigStorage.get(storageTag); + result = SessionStorage.get(storageTag); let storageObj = result[storageTag]; const unifiedConfigList = TABS.firmware_flasher.unifiedConfigs[target]; const manufacturerIds = Object.keys(unifiedConfigList); @@ -653,7 +658,7 @@ firmware_flasher.initialize = function (callback) { targetId: targetId, lastUpdate: checkTime, }; - ConfigStorage.set(newStorageObj); + SessionStorage.set(newStorageObj); populateBuilds(builds, target, manufacturerId, duplicateName, TABS.firmware_flasher.releases[bareBoard], processNext); }); @@ -1170,10 +1175,10 @@ firmware_flasher.initialize = function (callback) { function setAcknowledgementTimestamp() { const storageObj = {}; storageObj[storageTag] = Date.now(); - ConfigStorage.set(storageObj); + SessionStorage.set(storageObj); } - result = ConfigStorage.get(storageTag); + result = SessionStorage.get(storageTag); if (!result[storageTag] || Date.now() - result[storageTag] > DAY_MS) { showAcknowledgementDialog(setAcknowledgementTimestamp); @@ -1392,7 +1397,7 @@ firmware_flasher.showDialogVerifyBoard = function (selected, verified, onAbort, if (!dialogVerifyBoard.hasAttribute('open')) { dialogVerifyBoard.showModal(); $('#dialog-verify-board-abort-confirmbtn').click(function() { - ConfigStorage.set({'selected_board': FC.CONFIG.boardName}); + SessionStorage.set({'selected_board': FC.CONFIG.boardName}); dialogVerifyBoard.close(); onAbort(); }); diff --git a/src/main.html b/src/main.html index 9efc0dd0..816532a3 100644 --- a/src/main.html +++ b/src/main.html @@ -88,6 +88,7 @@ +