From 7f4c3450fdc6895bf36eeba9c3184a0814275a21 Mon Sep 17 00:00:00 2001 From: hackademix Date: Wed, 23 Oct 2019 00:08:05 +0200 Subject: Fixed bug in chunked storage causing shrunk items not to be retrieved correctly. --- src/common/Storage.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src/common') diff --git a/src/common/Storage.js b/src/common/Storage.js index c534bfb..78c481b 100644 --- a/src/common/Storage.js +++ b/src/common/Storage.js @@ -71,12 +71,14 @@ var Storage = (() => { // Firefox Sync's max object BYTEs size is 16384, Chrome's 8192. // Rather than mesuring actual bytes, we play it safe by halving then // lowest to cope with escapes / multibyte characters. + let removeKeys = []; for (let k of Object.keys(keys)) { let s = JSON.stringify(keys[k]); + let chunksCountKey = chunksKey(k); + let oldCount = await browser.storage.sync.get(chunksCountKey)[chunksCountKey] || 0; + let count; if (s.length > MAX_ITEM_SIZE) { - let count = Math.ceil(s.length / MAX_ITEM_SIZE); - let chunksCountKey = chunksKey(k); - let oldCount = await browser.storage.sync.get(chunksCountKey); + count = Math.ceil(s.length / MAX_ITEM_SIZE); let chunks = { [chunksCountKey]: count }; @@ -85,15 +87,17 @@ var Storage = (() => { } await browser.storage.sync.set(chunks); keys[k] = "[CHUNKED]"; - if (oldCount-- > count) { - let oldChunks = []; - do { - oldChunks.push(`${k}${oldCount}`); - } while(oldCount-- > count); - await browser.storage.sync.remove(oldChunks); - } + } else { + count = 0; + removeKeys.push(chunksCountKey); + } + if (oldCount-- > count) { + do { + removeKeys.push(`${k}${oldCount}`); + } while(oldCount-- > count); } } + await browser.storage.sync.remove(removeKeys); } } -- cgit v1.2.3