This commit is contained in:
velzie 2024-07-19 13:53:01 -04:00
commit f70e2a6ff4
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F

View file

@ -8,64 +8,112 @@ const store = new IDBMapSync(locationProxy.host, {
await store.sync();
function storageProxy(scope: Storage): Storage {
return new Proxy(scope, {
get(target, prop) {
switch (prop) {
case "getItem":
return (key: string) => {
return store.get(key);
};
const localStorageProxy = new Proxy(window.localStorage, {
get(target, prop) {
switch (prop) {
case "getItem":
return (key: string) => {
return store.get(key);
};
case "setItem":
return (key: string, value: string) => {
store.set(key, value);
store.sync();
};
case "setItem":
return (key: string, value: string) => {
store.set(key, value);
store.sync();
};
case "removeItem":
return (key: string) => {
store.delete(key);
store.sync();
};
case "removeItem":
return (key: string) => {
store.delete(key);
store.sync();
};
case "clear":
return () => {
store.clear();
store.sync();
};
case "clear":
return () => {
store.clear();
store.sync();
};
case "key":
return (index: number) => {
return [...store.keys()][index];
};
case "length":
return store.size;
default:
if (prop in Object.prototype) {
return Reflect.get(target, prop);
case "key":
return (index: number) => {
return [...store.keys()][index];
};
case "length":
return store.size;
default:
if (prop in Object.prototype) {
return Reflect.get(target, prop);
}
return store.get(prop);
}
},
//@ts-ignore
set(target, prop, value) {
store.set(prop, value);
store.sync();
},
defineProperty(target, property, attributes) {
store.set(property as string, attributes.value);
return true;
},
});
const sessionStorageProxy = new Proxy(window.sessionStorage, {
get(target, prop) {
switch (prop) {
case "getItem":
return (key: string) => {
return target.getItem(locationProxy.host + "@" + key);
}
case "setItem":
return (key: string, value: string) => {
target.setItem(locationProxy.host + "@" + key, value);
}
case "removeItem":
return (key: string) => {
target.removeItem(locationProxy.host + "@" + key);
}
case "clear":
return () => {
for (const key in Object.keys(target)) {
if (key.startsWith(locationProxy.host)) {
target.removeItem(key);
}
}
}
return store.get(prop);
}
},
case "key":
return (index: number) => {
const keys = Object.keys(target).filter((key) => key.startsWith(locationProxy.host));
//@ts-ignore
set(target, prop, value) {
store.set(prop, value);
store.sync();
},
return target.getItem(keys[index]);
}
defineProperty(target, property, attributes) {
store.set(property as string, attributes.value);
case "length":
return target.length;
return true;
},
});
}
default:
if (prop in Object.prototype) {
return Reflect.get(target, prop);
}
const localStorageProxy = storageProxy(window.localStorage);
const sessionStorageProxy = storageProxy(window.sessionStorage);
return target.getItem(locationProxy.host + "@" + (prop as string));
}
},
defineProperty(target, property, attributes) {
target.setItem(locationProxy.host + "@" + (property as string), attributes.value);
return true;
},
});
delete window.localStorage;
delete window.sessionStorage;