1+ import  {  log  }  from  "./log" ; 
2+ 
13const  maxExpiryDays  =  365 ; 
24
35const  isPersistingSessionLocalStoreName  =  "gist.web.isPersistingSession" ; 
@@ -21,32 +23,20 @@ export function setKeyToLocalStore(key, value, ttl = null) {
2123} 
2224
2325export  function  getKeyFromLocalStore ( key )  { 
24-     const  itemStr  =  getStorage ( ) . getItem ( key ) ; 
25-     if  ( ! itemStr )  return  null ; 
26- 
27-     const  item  =  JSON . parse ( itemStr ) ; 
28-     const  now  =  new  Date ( ) ; 
29-     const  expiryTime  =  new  Date ( item . expiry ) ; 
30-     
31-     // Retroactive bugfix: remove old cache entries with long expiry times 
32-     const  isBroadcastOrUserKey  =  ( key . startsWith ( "gist.web.message.broadcasts" )  &&  ! key . endsWith ( "shouldShow" )  &&  ! key . endsWith ( "numberOfTimesShown" ) )  ||  ( key . startsWith ( "gist.web.message.user" )  &&  ! key . endsWith ( "seen" ) ) ; 
33-     const  sixtyMinutesFromNow  =  new  Date ( now . getTime ( )  +  61  *  60  *  1000 ) ; 
34-     if  ( isBroadcastOrUserKey  &&  expiryTime . getTime ( )  >  sixtyMinutesFromNow . getTime ( ) )  { 
35-         clearKeyFromLocalStore ( key ) ; 
36-         return  null ; 
37-     } 
38-     
39-     if  ( now . getTime ( )  >  expiryTime . getTime ( ) )  { 
40-         clearKeyFromLocalStore ( key ) ; 
41-         return  null ; 
42-     } 
43-     return  item . value ; 
26+     return  checkKeyForExpiry ( key ) ; 
4427} 
4528
4629export  function  clearKeyFromLocalStore ( key )  { 
4730    getStorage ( ) . removeItem ( key ) ; 
4831} 
4932
33+ export  function  clearExpiredFromLocalStore ( )  { 
34+     const  storage  =  getStorage ( ) ; 
35+     for  ( let  i  =  storage . length  -  1 ;  i  >=  0 ;  i -- )  { 
36+         checkKeyForExpiry ( storage . key ( i ) ) ; 
37+     } 
38+ } 
39+ 
5040export  function  isSessionBeingPersisted ( )  { 
5141    const  currentValue  =  sessionStorage . getItem ( isPersistingSessionLocalStoreName ) ; 
5242    if  ( currentValue  ===  null )  { 
@@ -59,4 +49,38 @@ export function isSessionBeingPersisted() {
5949// Helper function to select the correct storage based on the session flag 
6050function  getStorage ( )  { 
6151    return  isSessionBeingPersisted ( )  ? localStorage  : sessionStorage ; 
52+ } 
53+ 
54+ function  checkKeyForExpiry ( key )  { 
55+     if  ( ! key )  return  null ; 
56+ 
57+     try  { 
58+         const  itemStr  =  getStorage ( ) . getItem ( key ) ; 
59+         if  ( ! itemStr )  return  null ; 
60+ 
61+         const  item  =  JSON . parse ( itemStr ) ; 
62+         if  ( ! item . expiry )  return  item . value ; 
63+ 
64+         const  now  =  new  Date ( ) ; 
65+         const  expiryTime  =  new  Date ( item . expiry ) ; 
66+         
67+         // remove old cache entries with long expiry times 
68+         const  isBroadcastOrUserKey  =  ( key . startsWith ( "gist.web.message.broadcasts" )  &&  ! key . endsWith ( "shouldShow" )  &&  ! key . endsWith ( "numberOfTimesShown" ) )  ||  ( key . startsWith ( "gist.web.message.user" )  &&  ! key . endsWith ( "seen" ) ) ; 
69+         const  sixtyMinutesFromNow  =  new  Date ( now . getTime ( )  +  61  *  60  *  1000 ) ; 
70+         if  ( isBroadcastOrUserKey  &&  expiryTime . getTime ( )  >  sixtyMinutesFromNow . getTime ( ) )  { 
71+             clearKeyFromLocalStore ( key ) ; 
72+             return  null ; 
73+         } 
74+         
75+         if  ( now . getTime ( )  >  expiryTime . getTime ( ) )  { 
76+             clearKeyFromLocalStore ( key ) ; 
77+             return  null ; 
78+         } 
79+ 
80+         return  item . value ; 
81+     }  catch  ( e )  { 
82+         log ( `Error checking key ${ key }   for expiry: ${ e }  ` ) ; 
83+     } 
84+     
85+     return  null ; 
6286} 
0 commit comments