From 4e2aaf804947033f00ab9c8614660f0b5ac55d38 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 4 Feb 2025 09:57:30 +0100 Subject: [PATCH] make it two properties --- .../playwright-core/src/protocol/validator.ts | 4 +- .../src/server/browserContext.ts | 10 +- packages/protocol/src/channels.d.ts | 4 +- packages/protocol/src/protocol.yml | 6 +- .../browsercontext-storage-state.spec.ts | 110 +++++++++--------- 5 files changed, 71 insertions(+), 63 deletions(-) diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts index e6d8ccd18e..e9f06d5126 100644 --- a/packages/playwright-core/src/protocol/validator.ts +++ b/packages/playwright-core/src/protocol/validator.ts @@ -149,13 +149,15 @@ scheme.IndexedDBDatabase = tObject({ name: tString, autoIncrement: tBoolean, keyPath: tOptional(tString), + keyPathArray: tOptional(tType('string[]')), records: tArray(tObject({ key: tOptional(tString), value: tString, })), indexes: tArray(tObject({ name: tString, - keyPath: tString, + keyPath: tOptional(tString), + keyPathArray: tOptional(tType('string[]')), multiEntry: tBoolean, unique: tBoolean, })), diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.ts index e376460df4..f8d5ed5431 100644 --- a/packages/playwright-core/src/server/browserContext.ts +++ b/packages/playwright-core/src/server/browserContext.ts @@ -558,7 +558,8 @@ export abstract class BrowserContext extends SdkObject { const index = objectStore.index(indexName); return { name: index.name, - keyPath: JSON.stringify(index.keyPath), + keyPath: typeof index.keyPath === 'string' ? index.keyPath : undefined, + keyPathArray: Array.isArray(index.keyPath) ? index.keyPath : undefined, multiEntry: index.multiEntry, unique: index.unique, }; @@ -569,7 +570,8 @@ export abstract class BrowserContext extends SdkObject { records: records.filter(Boolean), indexes, autoIncrement: objectStore.autoIncrement, - keyPath: objectStore.keyPath === null ? undefined : JSON.stringify(objectStore.keyPath), + keyPath: typeof objectStore.keyPath === 'string' ? objectStore.keyPath : undefined, + keyPathArray: Array.isArray(objectStore.keyPath) ? objectStore.keyPath : undefined, }; })); @@ -690,9 +692,9 @@ export abstract class BrowserContext extends SdkObject { openRequest.addEventListener('upgradeneeded', () => { const db = openRequest.result; for (const store of dbInfo.stores) { - const objectStore = db.createObjectStore(store.name, { autoIncrement: store.autoIncrement, keyPath: store.keyPath ? JSON.parse(store.keyPath) : undefined }); + const objectStore = db.createObjectStore(store.name, { autoIncrement: store.autoIncrement, keyPath: store.keyPathArray ?? store.keyPath }); for (const index of store.indexes) - objectStore.createIndex(index.name, JSON.parse(index.keyPath), { unique: index.unique, multiEntry: index.multiEntry }); + objectStore.createIndex(index.name, index.keyPathArray ?? index.keyPath!, { unique: index.unique, multiEntry: index.multiEntry }); } }); openRequest.addEventListener('success', async () => { diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.ts index 600e64cb97..f1cee7bcc1 100644 --- a/packages/protocol/src/channels.d.ts +++ b/packages/protocol/src/channels.d.ts @@ -278,13 +278,15 @@ export type IndexedDBDatabase = { name: string, autoIncrement: boolean, keyPath?: string, + keyPathArray?: string[], records: { key?: string, value: string, }[], indexes: { name: string, - keyPath: string, + keyPath?: string, + keyPathArray?: string[], multiEntry: boolean, unique: boolean, }[], diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml index 29ea1bb4c3..4a70663154 100644 --- a/packages/protocol/src/protocol.yml +++ b/packages/protocol/src/protocol.yml @@ -234,7 +234,8 @@ IndexedDBDatabase: properties: name: string autoIncrement: boolean - keyPath: string? # JSON of string | string[] + keyPath: string? + keyPathArray: string[]? records: type: array items: @@ -248,7 +249,8 @@ IndexedDBDatabase: type: object properties: name: string - keyPath: string # JSON of string | string[] + keyPath: string? + keyPathArray: string[]? multiEntry: boolean unique: boolean diff --git a/tests/library/browsercontext-storage-state.spec.ts b/tests/library/browsercontext-storage-state.spec.ts index eb3c9d9f7f..335c1fedbe 100644 --- a/tests/library/browsercontext-storage-state.spec.ts +++ b/tests/library/browsercontext-storage-state.spec.ts @@ -337,7 +337,7 @@ it('should support IndexedDB', async ({ page, contextFactory }) => { { name: 'toDoList', autoIncrement: false, - keyPath: '"taskTitle"', + keyPath: 'taskTitle', records: [ { value: JSON.stringify({ @@ -347,53 +347,53 @@ it('should support IndexedDB', async ({ page, contextFactory }) => { day: '01', month: 'January', year: '2025', - notified: 'no' - }) - } + notified: 'no', + }), + }, ], indexes: [ { - 'name': 'day', - 'keyPath': '"day"', - 'multiEntry': false, - 'unique': false, + name: 'day', + keyPath: 'day', + multiEntry: false, + unique: false, }, { - 'name': 'hours', - 'keyPath': '"hours"', - 'multiEntry': false, - 'unique': false, + name: 'hours', + keyPath: 'hours', + multiEntry: false, + unique: false, }, { - 'name': 'minutes', - 'keyPath': '"minutes"', - 'multiEntry': false, - 'unique': false, + name: 'minutes', + keyPath: 'minutes', + multiEntry: false, + unique: false, }, { - 'name': 'month', - 'keyPath': '"month"', - 'multiEntry': false, - 'unique': false, + name: 'month', + keyPath: 'month', + multiEntry: false, + unique: false, }, { - 'name': 'notified', - 'keyPath': '"notified"', - 'multiEntry': false, - 'unique': false, + name: 'notified', + keyPath: 'notified', + multiEntry: false, + unique: false, }, { - 'name': 'year', - 'keyPath': '"year"', - 'multiEntry': false, - 'unique': false, + name: 'year', + keyPath: 'year', + multiEntry: false, + unique: false, }, - ] - } - ] - } - ] - } + ], + }, + ], + }, + ], + }, ]); const context = await contextFactory({ storageState }); @@ -415,42 +415,42 @@ it('indexedDb firebase acceptance test', async ({ page, contextFactory }) => { await page.close(); const storageState = await page.context().storageState(); expect(storageState).toEqual({ - 'cookies': [], - 'origins': [ + cookies: [], + origins: [ { - 'indexedDB': [ + indexedDB: [ { - 'name': 'firebase-heartbeat-database', - 'stores': [ + name: 'firebase-heartbeat-database', + stores: [ { - 'autoIncrement': false, - 'indexes': [], - 'name': 'firebase-heartbeat-store', - 'records': [], + autoIncrement: false, + indexes: [], + name: 'firebase-heartbeat-store', + records: [], }, ], - 'version': 1, + version: 1, }, { - 'name': 'firebaseLocalStorageDb', - 'stores': [ + name: 'firebaseLocalStorageDb', + stores: [ { - 'autoIncrement': false, - 'indexes': [], - 'keyPath': '"fbase_key"', - 'name': 'firebaseLocalStorage', - 'records': [ + autoIncrement: false, + indexes: [], + keyPath: 'fbase_key', + name: 'firebaseLocalStorage', + records: [ { - 'value': expect.any(String), + value: expect.any(String), }, ], }, ], - 'version': 1, + version: 1, }, ], - 'localStorage': [], - 'origin': 'https://fir-ui-demo-84a6c.firebaseapp.com', + localStorage: [], + origin: 'https://fir-ui-demo-84a6c.firebaseapp.com', }, ], });