MCPcopy Create free account
hub / github.com/darkreader/darkreader / prepareSyncStorage

Function prepareSyncStorage

src/background/utils/extension-api.ts:100–121  ·  view source on GitHub ↗
(values: T)

Source from the content-addressed store, hash-verified

98}
99
100function prepareSyncStorage<T extends {[key: string]: any}>(values: T): {[key: string]: any} {
101 for (const key in values) {
102 const value = values[key];
103 const string = JSON.stringify(value);
104 // The maximum size of any one item that each extension is allowed to store in the sync storage area,
105 // as measured by the JSON stringification of the item's value plus the length of its key.
106 // Source: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/sync
107 const totalLength = string.length + key.length;
108 if (totalLength > chrome.storage.sync.QUOTA_BYTES_PER_ITEM) {
109 // This length limit permits us to store up to 1000 = (parseInt('rr', 36) + 1) records.
110 const maxLength = chrome.storage.sync.QUOTA_BYTES_PER_ITEM - key.length - 1 - 2;
111 const minimalKeysNeeded = Math.ceil(string.length / maxLength);
112 for (let i = 0; i < minimalKeysNeeded; i++) {
113 (values as any)[`${key}_${i.toString(36)}`] = string.substring(i * maxLength, (i + 1) * maxLength);
114 }
115 (values as any)[key] = {
116 __meta_split_count: minimalKeysNeeded,
117 };
118 }
119 }
120 return values;
121}
122
123export async function writeSyncStorage<T extends {[key: string]: any}>(values: T): Promise<void> {
124 return new Promise<void>((resolve, reject) => {

Callers 1

writeSyncStorageFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…