@@ -24,9 +24,10 @@ const reverseFilterObject = (object: Record<string, unknown>, regex: string | Re
24
24
/**
25
25
* This function is used to get the total size of the local storage.
26
26
* @param storage The storage area to get the size of.
27
+ * @param encoder The encoder to use to get the size of the keys and values.
27
28
*/
28
- const getLocalStorageSize = ( storage = window . localStorage ) => {
29
- return Object . entries ( storage ) . reduce ( ( acc , [ key , value ] ) => acc + key . length + value . length , 0 ) ;
29
+ const getLocalStorageSize = ( storage = window . localStorage , encoder = new TextEncoder ( ) ) => {
30
+ return Object . entries ( storage ) . reduce ( ( acc , [ key , value ] ) => acc + encoder . encode ( key ) . length + encoder . encode ( value ) . length , 0 ) ;
30
31
} ;
31
32
32
33
/**
@@ -107,8 +108,11 @@ export const defaultMaxLocalStorageSize = 10485760;
107
108
108
109
export const localCache : < T > ( key : string , value : T , regex ?: string | RegExp ) => Promise < void > = async ( key , value , regex ) => {
109
110
let inUse = await storage . local . getBytesInUse ( ) ;
111
+
110
112
const max = globalThis ?. chrome ?. storage ?. local . QUOTA_BYTES ?? defaultMaxLocalStorageSize ;
111
- const payload = JSON . stringify ( value ) . length ;
113
+
114
+ const encoder = new TextEncoder ( ) ;
115
+ const payload = encoder . encode ( JSON . stringify ( value ) ) . length ;
112
116
113
117
if ( payload > max ) {
114
118
console . warn ( 'Payload is too large to store in local storage.' , { payload, max, inUse } ) ;
0 commit comments