mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
maybe fix storage proxy
This commit is contained in:
parent
0a81ae042b
commit
b2afb9c2ca
1 changed files with 43 additions and 73 deletions
|
@ -1,89 +1,27 @@
|
||||||
import IDBMapSync from "idb-map-entries/sync";
|
import { client } from ".";
|
||||||
import { locationProxy } from "./location";
|
|
||||||
|
|
||||||
const store = new IDBMapSync(locationProxy.host, {
|
const handler: ProxyHandler<Storage> = {
|
||||||
prefix: "Storage",
|
|
||||||
durability: "relaxed",
|
|
||||||
});
|
|
||||||
|
|
||||||
await store.sync();
|
|
||||||
|
|
||||||
const localStorageProxy = new Proxy(window.localStorage, {
|
|
||||||
get(target, prop) {
|
get(target, prop) {
|
||||||
switch (prop) {
|
switch (prop) {
|
||||||
case "getItem":
|
case "getItem":
|
||||||
return (key: string) => {
|
return (key: string) => {
|
||||||
return store.get(key);
|
return target.getItem(client.url.host + "@" + key);
|
||||||
};
|
};
|
||||||
|
|
||||||
case "setItem":
|
case "setItem":
|
||||||
return (key: string, value: string) => {
|
return (key: string, value: string) => {
|
||||||
store.set(key, value);
|
return target.setItem(client.url.host + "@" + key, value);
|
||||||
store.sync();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
case "removeItem":
|
case "removeItem":
|
||||||
return (key: string) => {
|
return (key: string) => {
|
||||||
store.delete(key);
|
return target.removeItem(client.url.host + "@" + key);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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":
|
case "clear":
|
||||||
return () => {
|
return () => {
|
||||||
for (const key in Object.keys(target)) {
|
for (const key in Object.keys(target)) {
|
||||||
if (key.startsWith(locationProxy.host)) {
|
if (key.startsWith(client.url.host)) {
|
||||||
target.removeItem(key);
|
target.removeItem(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,33 +30,65 @@ const sessionStorageProxy = new Proxy(window.sessionStorage, {
|
||||||
case "key":
|
case "key":
|
||||||
return (index: number) => {
|
return (index: number) => {
|
||||||
const keys = Object.keys(target).filter((key) =>
|
const keys = Object.keys(target).filter((key) =>
|
||||||
key.startsWith(locationProxy.host)
|
key.startsWith(client.url.host)
|
||||||
);
|
);
|
||||||
|
|
||||||
return target.getItem(keys[index]);
|
return target.getItem(keys[index]);
|
||||||
};
|
};
|
||||||
|
|
||||||
case "length":
|
case "length":
|
||||||
return target.length;
|
return Object.keys(target).filter((key) =>
|
||||||
|
key.startsWith(client.url.host)
|
||||||
|
).length;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (prop in Object.prototype) {
|
if (prop in Object.prototype) {
|
||||||
return Reflect.get(target, prop);
|
return Reflect.get(target, prop);
|
||||||
}
|
}
|
||||||
|
console.log("GET", prop, target == realLocalStorage);
|
||||||
|
|
||||||
return target.getItem(locationProxy.host + "@" + (prop as string));
|
return target.getItem(client.url.host + "@" + (prop as string));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
set(target, prop, value) {
|
||||||
|
if (target == realLocalStorage)
|
||||||
|
console.log("SET", prop, value, target === realLocalStorage);
|
||||||
|
target.setItem(client.url.host + "@" + (prop as string), value);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
ownKeys(target) {
|
||||||
|
return Reflect.ownKeys(target)
|
||||||
|
.filter((f) => typeof f === "string" && f.startsWith(client.url.host))
|
||||||
|
.map((f) => f.substring(client.url.host.length + 1));
|
||||||
|
},
|
||||||
|
|
||||||
|
getOwnPropertyDescriptor(target, property) {
|
||||||
|
return {
|
||||||
|
value: target.getItem(client.url.host + "@" + (property as string)),
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true,
|
||||||
|
writable: true,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
defineProperty(target, property, attributes) {
|
defineProperty(target, property, attributes) {
|
||||||
target.setItem(
|
target.setItem(
|
||||||
locationProxy.host + "@" + (property as string),
|
client.url.host + "@" + (property as string),
|
||||||
attributes.value
|
attributes.value
|
||||||
);
|
);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
|
const realLocalStorage = window.localStorage;
|
||||||
|
const realSessionStorage = window.sessionStorage;
|
||||||
|
|
||||||
|
const localStorageProxy = new Proxy(window.localStorage, handler);
|
||||||
|
const sessionStorageProxy = new Proxy(window.sessionStorage, handler);
|
||||||
|
|
||||||
delete window.localStorage;
|
delete window.localStorage;
|
||||||
delete window.sessionStorage;
|
delete window.sessionStorage;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue