1+ import { log } from "./log" ;
2+
13const maxExpiryDays = 365 ;
24
35const isPersistingSessionLocalStoreName = "gist.web.isPersistingSession" ;
@@ -52,25 +54,33 @@ function getStorage() {
5254function checkKeyForExpiry ( key ) {
5355 if ( ! key ) return null ;
5456
55- const itemStr = getStorage ( ) . getItem ( key ) ;
56- if ( ! itemStr ) return null ;
57+ try {
58+ const itemStr = getStorage ( ) . getItem ( key ) ;
59+ if ( ! itemStr ) return null ;
5760
58- const item = JSON . parse ( itemStr ) ;
59- const now = new Date ( ) ;
60- const expiryTime = new Date ( item . expiry ) ;
61-
62- // Retroactive bugfix: remove old cache entries with long expiry times
63- const isBroadcastOrUserKey = ( key . startsWith ( "gist.web.message.broadcasts" ) && ! key . endsWith ( "shouldShow" ) && ! key . endsWith ( "numberOfTimesShown" ) ) || ( key . startsWith ( "gist.web.message.user" ) && ! key . endsWith ( "seen" ) ) ;
64- const sixtyMinutesFromNow = new Date ( now . getTime ( ) + 61 * 60 * 1000 ) ;
65- if ( isBroadcastOrUserKey && expiryTime . getTime ( ) > sixtyMinutesFromNow . getTime ( ) ) {
66- clearKeyFromLocalStore ( key ) ;
67- return null ;
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 } ` ) ;
6883 }
6984
70- if ( now . getTime ( ) > expiryTime . getTime ( ) ) {
71- clearKeyFromLocalStore ( key ) ;
72- return null ;
73- }
74-
75- return item . value ;
85+ return null ;
7686}
0 commit comments