Skip to content

Commit

Permalink
fix(core): user profile not loading all frames
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Sep 3, 2022
1 parent 333a80c commit d4f6ccc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion core/injected-scripts/domStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function readStoreData(store: IDBObjectStore) {
if (cursor) {
const key = store.keyPath === null ? cursor.key : undefined;
let value = cursor.value;
if (typeof value === 'object') {
if (typeof value === 'object' && !('toJSON' in value && typeof value.toJSON === 'function')) {
value = { ...value };
}
data.push(TypeSerializer.stringify({ key, value }));
Expand Down
66 changes: 34 additions & 32 deletions core/injected-scripts/indexedDbRestore.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import type { IIndexedDB } from '@unblocked-web/specifications/agent/browser/IIndexedDB';

async function restoreIndexedDb(restoreDBs: IIndexedDB[]) {
async function restoreIndexedDb(restoreDBs: IIndexedDB[]): Promise<void> {
if (!restoreDBs || !restoreDBs.length) return;

for (const restoreDB of restoreDBs) {
await new Promise<void>((resolve, reject) => {
const openDBRequest = indexedDB.open(restoreDB.name, restoreDB.version);
// only run changes when the db doesn't already exist
openDBRequest.onupgradeneeded = event => {
const request = event.target as IDBRequest<IDBDatabase>;
const db = request.result;
for (const objectStoreToRestore of restoreDB.objectStores) {
const objectStore = db.createObjectStore(objectStoreToRestore.name, {
autoIncrement: objectStoreToRestore.autoIncrement,
keyPath: objectStoreToRestore.keyPath,
});

for (const restoreIndex of objectStoreToRestore.indexes) {
objectStore.createIndex(restoreIndex.name, restoreIndex.keyPath, {
multiEntry: restoreIndex.multiEntry,
unique: restoreIndex.unique,
await Promise.all(
restoreDBs.map(restoreDB => {
return new Promise<void>((resolve, reject) => {
const openDBRequest = indexedDB.open(restoreDB.name, restoreDB.version);
// only run changes when the db doesn't already exist
openDBRequest.onupgradeneeded = event => {
const request = event.target as IDBRequest<IDBDatabase>;
const db = request.result;
for (const objectStoreToRestore of restoreDB.objectStores) {
const objectStore = db.createObjectStore(objectStoreToRestore.name, {
autoIncrement: objectStoreToRestore.autoIncrement,
keyPath: objectStoreToRestore.keyPath,
});

for (const restoreIndex of objectStoreToRestore.indexes) {
objectStore.createIndex(restoreIndex.name, restoreIndex.keyPath, {
multiEntry: restoreIndex.multiEntry,
unique: restoreIndex.unique,
});
}
}
}
};
openDBRequest.onsuccess = async event => {
const request = event.target as IDBRequest<IDBDatabase>;
try {
await restoreData(request.result, restoreDB);
resolve();
} catch (err) {
reject(err);
}
};
openDBRequest.onerror = reject;
});
}
};
openDBRequest.onsuccess = async event => {
const request = event.target as IDBRequest<IDBDatabase>;
try {
await restoreData(request.result, restoreDB);
resolve();
} catch (err) {
reject(err);
}
};
openDBRequest.onerror = reject;
});
}),
);
}

async function restoreData(db: IDBDatabase, restoreDB: IIndexedDB) {
Expand Down
16 changes: 11 additions & 5 deletions core/lib/UserProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ export default class UserProfile {
responseCode: 200,
requestId,
responseHeaders: [{ name: 'Content-Type', value: 'text/html' }],
body: Buffer.from(`<html>
body: Buffer.from(
`<html>
<body>
<h1>Restoring Dom Storage</h1>
${origins.map(x => `<iframe src="${x}"></iframe>`).join('\n')}
</body>
</html>`).toString('base64'),
</html>`,
).toString('base64'),
};
}
let script = '';
Expand Down Expand Up @@ -160,18 +162,22 @@ for (const [key,value] of ${JSON.stringify(localStorage)}) {
return {
responseCode: 200,
requestId,
body: Buffer.from(`<html><body class="${readyClass}">
body: Buffer.from(
`<html><body class="${readyClass}">
<h5>${url.origin}</h5>
<script>
${script}
</script>
</body></html>`).toString('base64'),
</body></html>`,
).toString('base64'),
};
}, true);


// clear out frame state
await page.navigate(storageRestoreDomain);
while (page.frames.length <= origins.length) {
await new Promise(resolve => setTimeout(resolve, 50));
}

await Promise.all(
page.frames.map(async frame => {
Expand Down
1 change: 1 addition & 0 deletions core/test/user-profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ describe('UserProfile IndexedDb tests', () => {
expect(db.objectStores[1].keyPath).not.toBeTruthy();

expect(db.data.store1).toHaveLength(2);
expect(db.data.store2).toHaveLength(1);
}
{
const meta = await connection.createSession({
Expand Down

0 comments on commit d4f6ccc

Please sign in to comment.