From d639a624a5b536050caa51c3c6d712ba76c9a613 Mon Sep 17 00:00:00 2001 From: Dan Ho <70310805+djanhjo@users.noreply.github.com> Date: Wed, 15 Oct 2025 16:43:25 -0400 Subject: [PATCH 1/3] clearing expired local storage items on init --- src/gist.js | 2 ++ src/utilities/local-storage.js | 50 ++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/gist.js b/src/gist.js index e38dc45..56c720f 100644 --- a/src/gist.js +++ b/src/gist.js @@ -1,5 +1,6 @@ import EventEmitter from "./utilities/event-emitter"; import { log } from "./utilities/log"; +import { clearExpiredFromLocalStore } from "./utilities/local-storage"; import { startQueueListener, checkMessageQueue, stopSSEListener } from "./managers/queue-manager"; import { setUserToken, clearUserToken, useGuestSession } from "./managers/user-manager"; import { showMessage, embedMessage, hideMessage, removePersistentMessage, fetchMessageByInstanceId, logBroadcastDismissedLocally } from "./managers/message-manager"; @@ -23,6 +24,7 @@ export default class { this.currentRoute = null; this.isDocumentVisible = true; this.config.isPreviewSession = setupPreview(); + clearExpiredFromLocalStore(); log(`Setup complete on ${this.config.env} environment.`); diff --git a/src/utilities/local-storage.js b/src/utilities/local-storage.js index 6f44622..c92ba1d 100644 --- a/src/utilities/local-storage.js +++ b/src/utilities/local-storage.js @@ -21,6 +21,37 @@ export function setKeyToLocalStore(key, value, ttl = null) { } export function getKeyFromLocalStore(key) { + return checkKeyForExpiry(key); +} + +export function clearKeyFromLocalStore(key) { + getStorage().removeItem(key); +} + +export function clearExpiredFromLocalStore() { + const storage = getStorage(); + for (let i = 0; i < storage.length; i++) { + checkKeyForExpiry(storage.key(i)); + } +} + +export function isSessionBeingPersisted() { + const currentValue = sessionStorage.getItem(isPersistingSessionLocalStoreName); + if (currentValue === null) { + sessionStorage.setItem(isPersistingSessionLocalStoreName, "true"); + return true; + } + return currentValue === "true"; +} + +// Helper function to select the correct storage based on the session flag +function getStorage() { + return isSessionBeingPersisted() ? localStorage : sessionStorage; +} + +function checkKeyForExpiry(key) { + if (!key) return null; + const itemStr = getStorage().getItem(key); if (!itemStr) return null; @@ -40,23 +71,6 @@ export function getKeyFromLocalStore(key) { clearKeyFromLocalStore(key); return null; } - return item.value; -} -export function clearKeyFromLocalStore(key) { - getStorage().removeItem(key); -} - -export function isSessionBeingPersisted() { - const currentValue = sessionStorage.getItem(isPersistingSessionLocalStoreName); - if (currentValue === null) { - sessionStorage.setItem(isPersistingSessionLocalStoreName, "true"); - return true; - } - return currentValue === "true"; -} - -// Helper function to select the correct storage based on the session flag -function getStorage() { - return isSessionBeingPersisted() ? localStorage : sessionStorage; + return item.value; } \ No newline at end of file From cdd713a9e06a794bcdf64857444401c6c0f9f66a Mon Sep 17 00:00:00 2001 From: Dan Ho <70310805+djanhjo@users.noreply.github.com> Date: Wed, 15 Oct 2025 17:30:09 -0400 Subject: [PATCH 2/3] fix --- src/utilities/local-storage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities/local-storage.js b/src/utilities/local-storage.js index c92ba1d..c933794 100644 --- a/src/utilities/local-storage.js +++ b/src/utilities/local-storage.js @@ -30,7 +30,7 @@ export function clearKeyFromLocalStore(key) { export function clearExpiredFromLocalStore() { const storage = getStorage(); - for (let i = 0; i < storage.length; i++) { + for (let i = storage.length - 1; i >= 0; i--) { checkKeyForExpiry(storage.key(i)); } } From edd032c8af703081193ba96d54039545b12336a1 Mon Sep 17 00:00:00 2001 From: Dan Ho <70310805+djanhjo@users.noreply.github.com> Date: Thu, 16 Oct 2025 09:43:44 -0400 Subject: [PATCH 3/3] feedback --- src/utilities/local-storage.js | 46 +++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/utilities/local-storage.js b/src/utilities/local-storage.js index c933794..9aa4d83 100644 --- a/src/utilities/local-storage.js +++ b/src/utilities/local-storage.js @@ -1,3 +1,5 @@ +import { log } from "./log"; + const maxExpiryDays = 365; const isPersistingSessionLocalStoreName = "gist.web.isPersistingSession"; @@ -52,25 +54,33 @@ function getStorage() { function checkKeyForExpiry(key) { if (!key) return null; - const itemStr = getStorage().getItem(key); - if (!itemStr) return null; + try { + const itemStr = getStorage().getItem(key); + if (!itemStr) return null; - const item = JSON.parse(itemStr); - const now = new Date(); - const expiryTime = new Date(item.expiry); - - // Retroactive bugfix: remove old cache entries with long expiry times - const isBroadcastOrUserKey = (key.startsWith("gist.web.message.broadcasts") && !key.endsWith("shouldShow") && !key.endsWith("numberOfTimesShown")) || (key.startsWith("gist.web.message.user") && !key.endsWith("seen")); - const sixtyMinutesFromNow = new Date(now.getTime() + 61 * 60 * 1000); - if (isBroadcastOrUserKey && expiryTime.getTime() > sixtyMinutesFromNow.getTime()) { - clearKeyFromLocalStore(key); - return null; + const item = JSON.parse(itemStr); + if (!item.expiry) return item.value; + + const now = new Date(); + const expiryTime = new Date(item.expiry); + + // remove old cache entries with long expiry times + const isBroadcastOrUserKey = (key.startsWith("gist.web.message.broadcasts") && !key.endsWith("shouldShow") && !key.endsWith("numberOfTimesShown")) || (key.startsWith("gist.web.message.user") && !key.endsWith("seen")); + const sixtyMinutesFromNow = new Date(now.getTime() + 61 * 60 * 1000); + if (isBroadcastOrUserKey && expiryTime.getTime() > sixtyMinutesFromNow.getTime()) { + clearKeyFromLocalStore(key); + return null; + } + + if (now.getTime() > expiryTime.getTime()) { + clearKeyFromLocalStore(key); + return null; + } + + return item.value; + } catch (e) { + log(`Error checking key ${key} for expiry: ${e}`); } - if (now.getTime() > expiryTime.getTime()) { - clearKeyFromLocalStore(key); - return null; - } - - return item.value; + return null; } \ No newline at end of file