Skip to content

Commit

Permalink
Merge pull request #15 from HuolalaTech/feat/storage
Browse files Browse the repository at this point in the history
fix storage stable
  • Loading branch information
wqcstrong authored Oct 16, 2023
2 parents ed20df1 + 0accc00 commit ff8c1bb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
59 changes: 49 additions & 10 deletions src/plugins/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,93 @@ export class StoragePlugin implements PageSpyPlugin {
if (StoragePlugin.hasInitd) return;
StoragePlugin.hasInitd = true;

const { sendStorageItem, initStorageProxy } = StoragePlugin;
StoragePlugin.takeLocalStorage();
StoragePlugin.takeSessionStorage();
StoragePlugin.takeCookie();

StoragePlugin.listenRefreshEvent();
StoragePlugin.initStorageProxy();
}

private static takeLocalStorage() {
const local = { ...localStorage };
Object.keys(local).forEach((name) => {
const value = local[name];
sendStorageItem({ type: 'local', action: 'get', name, value });
StoragePlugin.sendStorageItem({
type: 'localStorage',
action: 'get',
name,
value,
});
});
}

private static takeSessionStorage() {
const session = { ...sessionStorage };
Object.keys(session).forEach((name) => {
const value = session[name];
sendStorageItem({ type: 'session', action: 'get', name, value });
StoragePlugin.sendStorageItem({
type: 'sessionStorage',
action: 'get',
name,
value,
});
});
}

private static takeCookie() {
if (window.cookieStore) {
window.cookieStore.getAll().then((cookies) => {
cookies.forEach((cookie) => {
const data = StoragePlugin.formatCookieInfo(cookie);
sendStorageItem(data);
StoragePlugin.sendStorageItem(data);
});
});
window.cookieStore.addEventListener('change', (e) => {
const { changed, deleted } = e as CookieChangeEvent;
if (changed.length > 0) {
changed.forEach((cookie) => {
const data = StoragePlugin.formatCookieInfo(cookie, 'set');
sendStorageItem(data);
StoragePlugin.sendStorageItem(data);
});
}
if (deleted.length > 0) {
deleted.forEach((cookie) => {
const data = StoragePlugin.formatCookieInfo(cookie, 'remove');
sendStorageItem(data);
StoragePlugin.sendStorageItem(data);
});
}
});
} else {
document.cookie.split('; ').forEach((item) => {
const [name, value] = item.split('=');
sendStorageItem({
StoragePlugin.sendStorageItem({
type: 'cookie',
action: 'get',
name,
value,
});
});
}
}

initStorageProxy();
private static listenRefreshEvent() {
socketStore.addListener(DEBUG_MESSAGE_TYPE.REFRESH, ({ source }) => {
const { data } = source;
switch (data) {
case 'localStorage':
StoragePlugin.takeLocalStorage();
break;
case 'sessionStorage':
StoragePlugin.takeSessionStorage();
break;
case 'cookie':
StoragePlugin.takeCookie();
break;
default:
break;
}
});
}

private static formatCookieInfo(
Expand Down Expand Up @@ -113,8 +152,8 @@ export class StoragePlugin implements PageSpyPlugin {
}

private static getStorageType(ins: Storage): SpyStorage.DataType {
if (ins === localStorage) return 'local';
if (ins === sessionStorage) return 'session';
if (ins === localStorage) return 'localStorage';
if (ins === sessionStorage) return 'sessionStorage';
return ins.constructor.name as any;
}
}
2 changes: 1 addition & 1 deletion types/lib/storage.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type DataType = 'local' | 'session' | 'cookie';
export type DataType = 'localStorage' | 'sessionStorage' | 'cookie';
export type ActionType = 'clear' | 'remove' | 'get' | 'set';

type RestCookieInfo = {
Expand Down

0 comments on commit ff8c1bb

Please sign in to comment.