Skip to content

Commit

Permalink
fix: allow opt out from IndexedDB in storagestate (#34650)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt authored Feb 6, 2025
1 parent 8d751cf commit 902e83f
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 28 deletions.
6 changes: 6 additions & 0 deletions docs/src/api/class-apirequestcontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,9 @@ Returns storage state for this request context, contains current cookies and loc

### option: APIRequestContext.storageState.path = %%-storagestate-option-path-%%
* since: v1.16

### option: APIRequestContext.storageState.indexedDB
* since: v1.51
- `indexedDB` ?<boolean>

Defaults to `true`. Set to `false` to omit IndexedDB from snapshot.
6 changes: 6 additions & 0 deletions docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,12 @@ IndexedDBs with typed arrays are currently not supported.
### option: BrowserContext.storageState.path = %%-storagestate-option-path-%%
* since: v1.8

### option: BrowserContext.storageState.indexedDB
* since: v1.51
- `indexedDB` ?<boolean>

Defaults to `true`. Set to `false` to omit IndexedDB from snapshot.

## property: BrowserContext.tracing
* since: v1.12
- type: <[Tracing]>
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
});
}

async storageState(options: { path?: string } = {}): Promise<StorageState> {
const state = await this._channel.storageState();
async storageState(options: { path?: string, indexedDB?: boolean } = {}): Promise<StorageState> {
const state = await this._channel.storageState({ indexedDB: options.indexedDB });
if (options.path) {
await mkdirIfNeeded(options.path);
await fs.promises.writeFile(options.path, JSON.stringify(state, undefined, 2), 'utf8');
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/client/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ export class APIRequestContext extends ChannelOwner<channels.APIRequestContextCh
});
}

async storageState(options: { path?: string } = {}): Promise<StorageState> {
const state = await this._channel.storageState();
async storageState(options: { path?: string, indexedDB?: boolean } = {}): Promise<StorageState> {
const state = await this._channel.storageState({ indexedDB: options.indexedDB });
if (options.path) {
await mkdirIfNeeded(options.path);
await fs.promises.writeFile(options.path, JSON.stringify(state, undefined, 2), 'utf8');
Expand Down
8 changes: 6 additions & 2 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ scheme.APIRequestContextFetchLogParams = tObject({
scheme.APIRequestContextFetchLogResult = tObject({
log: tArray(tString),
});
scheme.APIRequestContextStorageStateParams = tOptional(tObject({}));
scheme.APIRequestContextStorageStateParams = tObject({
indexedDB: tOptional(tBoolean),
});
scheme.APIRequestContextStorageStateResult = tObject({
cookies: tArray(tType('NetworkCookie')),
origins: tArray(tType('OriginStorage')),
Expand Down Expand Up @@ -992,7 +994,9 @@ scheme.BrowserContextSetOfflineParams = tObject({
offline: tBoolean,
});
scheme.BrowserContextSetOfflineResult = tOptional(tObject({}));
scheme.BrowserContextStorageStateParams = tOptional(tObject({}));
scheme.BrowserContextStorageStateParams = tObject({
indexedDB: tOptional(tBoolean),
});
scheme.BrowserContextStorageStateResult = tObject({
cookies: tArray(tType('NetworkCookie')),
origins: tArray(tType('OriginStorage')),
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,14 @@ export abstract class BrowserContext extends SdkObject {
this._origins.add(origin);
}

async storageState(): Promise<channels.BrowserContextStorageStateResult> {
async storageState(indexedDB = true): Promise<channels.BrowserContextStorageStateResult> {
const result: channels.BrowserContextStorageStateResult = {
cookies: await this.cookies(),
origins: []
};
const originsToSave = new Set(this._origins);

const collectScript = `(${storageScript.collect})((${utilityScriptSerializers.source})(), ${this._browser.options.name === 'firefox'})`;
const collectScript = `(${storageScript.collect})((${utilityScriptSerializers.source})(), ${this._browser.options.name === 'firefox'}, ${indexedDB})`;

// First try collecting storage stage from existing pages.
for (const page of this.pages()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
}

async storageState(params: channels.BrowserContextStorageStateParams, metadata: CallMetadata): Promise<channels.BrowserContextStorageStateResult> {
return await this._context.storageState();
return await this._context.storageState(params.indexedDB);
}

async close(params: channels.BrowserContextCloseParams, metadata: CallMetadata): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export class APIRequestContextDispatcher extends Dispatcher<APIRequestContext, c
this.adopt(tracing);
}

async storageState(): Promise<channels.APIRequestContextStorageStateResult> {
return this._object.storageState();
async storageState(params: channels.APIRequestContextStorageStateParams): Promise<channels.APIRequestContextStorageStateResult> {
return this._object.storageState(params.indexedDB);
}

async dispose(params: channels.APIRequestContextDisposeParams, metadata: CallMetadata): Promise<void> {
Expand Down
10 changes: 5 additions & 5 deletions packages/playwright-core/src/server/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export abstract class APIRequestContext extends SdkObject {
abstract _defaultOptions(): FetchRequestOptions;
abstract _addCookies(cookies: channels.NetworkCookie[]): Promise<void>;
abstract _cookies(url: URL): Promise<channels.NetworkCookie[]>;
abstract storageState(): Promise<channels.APIRequestContextStorageStateResult>;
abstract storageState(indexedDB?: boolean): Promise<channels.APIRequestContextStorageStateResult>;

private _storeResponseBody(body: Buffer): string {
const uid = createGuid();
Expand Down Expand Up @@ -618,8 +618,8 @@ export class BrowserContextAPIRequestContext extends APIRequestContext {
return await this._context.cookies(url.toString());
}

override async storageState(): Promise<channels.APIRequestContextStorageStateResult> {
return this._context.storageState();
override async storageState(indexedDB?: boolean): Promise<channels.APIRequestContextStorageStateResult> {
return this._context.storageState(indexedDB);
}
}

Expand Down Expand Up @@ -684,10 +684,10 @@ export class GlobalAPIRequestContext extends APIRequestContext {
return this._cookieStore.cookies(url);
}

override async storageState(): Promise<channels.APIRequestContextStorageStateResult> {
override async storageState(indexedDB = true): Promise<channels.APIRequestContextStorageStateResult> {
return {
cookies: this._cookieStore.allCookies(),
origins: this._origins || []
origins: (this._origins || []).map(origin => ({ ...origin, indexedDB: indexedDB ? origin.indexedDB : [] })),
};
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/playwright-core/src/server/storageScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import type { source } from './isomorphic/utilityScriptSerializers';

export type Storage = Omit<channels.OriginStorage, 'origin'>;

export async function collect(serializers: ReturnType<typeof source>, isFirefox: boolean): Promise<Storage> {
const idbResult = await Promise.all((await indexedDB.databases()).map(async dbInfo => {
export async function collect(serializers: ReturnType<typeof source>, isFirefox: boolean, recordIndexedDB: boolean): Promise<Storage> {
async function collectDB(dbInfo: IDBDatabaseInfo) {
if (!dbInfo.name)
throw new Error('Database name is empty');
if (!dbInfo.version)
Expand Down Expand Up @@ -119,13 +119,13 @@ export async function collect(serializers: ReturnType<typeof source>, isFirefox:
version: dbInfo.version,
stores,
};
})).catch(e => {
throw new Error('Unable to serialize IndexedDB: ' + e.message);
});
}

return {
localStorage: Object.keys(localStorage).map(name => ({ name, value: localStorage.getItem(name)! })),
indexedDB: idbResult,
indexedDB: recordIndexedDB ? await Promise.all((await indexedDB.databases()).map(collectDB)).catch(e => {
throw new Error('Unable to serialize IndexedDB: ' + e.message);
}) : [],
};
}

Expand Down
10 changes: 10 additions & 0 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9274,6 +9274,11 @@ export interface BrowserContext {
* @param options
*/
storageState(options?: {
/**
* Defaults to `true`. Set to `false` to omit IndexedDB from snapshot.
*/
indexedDB?: boolean;

/**
* The file path to save the storage state to. If
* [`path`](https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state-option-path) is a
Expand Down Expand Up @@ -18534,6 +18539,11 @@ export interface APIRequestContext {
* @param options
*/
storageState(options?: {
/**
* Defaults to `true`. Set to `false` to omit IndexedDB from snapshot.
*/
indexedDB?: boolean;

/**
* The file path to save the storage state to. If
* [`path`](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-storage-state-option-path) is
Expand Down
20 changes: 14 additions & 6 deletions packages/protocol/src/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export interface APIRequestContextChannel extends APIRequestContextEventTarget,
fetch(params: APIRequestContextFetchParams, metadata?: CallMetadata): Promise<APIRequestContextFetchResult>;
fetchResponseBody(params: APIRequestContextFetchResponseBodyParams, metadata?: CallMetadata): Promise<APIRequestContextFetchResponseBodyResult>;
fetchLog(params: APIRequestContextFetchLogParams, metadata?: CallMetadata): Promise<APIRequestContextFetchLogResult>;
storageState(params?: APIRequestContextStorageStateParams, metadata?: CallMetadata): Promise<APIRequestContextStorageStateResult>;
storageState(params: APIRequestContextStorageStateParams, metadata?: CallMetadata): Promise<APIRequestContextStorageStateResult>;
disposeAPIResponse(params: APIRequestContextDisposeAPIResponseParams, metadata?: CallMetadata): Promise<APIRequestContextDisposeAPIResponseResult>;
dispose(params: APIRequestContextDisposeParams, metadata?: CallMetadata): Promise<APIRequestContextDisposeResult>;
}
Expand Down Expand Up @@ -402,8 +402,12 @@ export type APIRequestContextFetchLogOptions = {
export type APIRequestContextFetchLogResult = {
log: string[],
};
export type APIRequestContextStorageStateParams = {};
export type APIRequestContextStorageStateOptions = {};
export type APIRequestContextStorageStateParams = {
indexedDB?: boolean,
};
export type APIRequestContextStorageStateOptions = {
indexedDB?: boolean,
};
export type APIRequestContextStorageStateResult = {
cookies: NetworkCookie[],
origins: OriginStorage[],
Expand Down Expand Up @@ -1564,7 +1568,7 @@ export interface BrowserContextChannel extends BrowserContextEventTarget, EventT
setNetworkInterceptionPatterns(params: BrowserContextSetNetworkInterceptionPatternsParams, metadata?: CallMetadata): Promise<BrowserContextSetNetworkInterceptionPatternsResult>;
setWebSocketInterceptionPatterns(params: BrowserContextSetWebSocketInterceptionPatternsParams, metadata?: CallMetadata): Promise<BrowserContextSetWebSocketInterceptionPatternsResult>;
setOffline(params: BrowserContextSetOfflineParams, metadata?: CallMetadata): Promise<BrowserContextSetOfflineResult>;
storageState(params?: BrowserContextStorageStateParams, metadata?: CallMetadata): Promise<BrowserContextStorageStateResult>;
storageState(params: BrowserContextStorageStateParams, metadata?: CallMetadata): Promise<BrowserContextStorageStateResult>;
pause(params?: BrowserContextPauseParams, metadata?: CallMetadata): Promise<BrowserContextPauseResult>;
enableRecorder(params: BrowserContextEnableRecorderParams, metadata?: CallMetadata): Promise<BrowserContextEnableRecorderResult>;
newCDPSession(params: BrowserContextNewCDPSessionParams, metadata?: CallMetadata): Promise<BrowserContextNewCDPSessionResult>;
Expand Down Expand Up @@ -1797,8 +1801,12 @@ export type BrowserContextSetOfflineOptions = {

};
export type BrowserContextSetOfflineResult = void;
export type BrowserContextStorageStateParams = {};
export type BrowserContextStorageStateOptions = {};
export type BrowserContextStorageStateParams = {
indexedDB?: boolean,
};
export type BrowserContextStorageStateOptions = {
indexedDB?: boolean,
};
export type BrowserContextStorageStateResult = {
cookies: NetworkCookie[],
origins: OriginStorage[],
Expand Down
4 changes: 4 additions & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ APIRequestContext:
items: string

storageState:
parameters:
indexedDB: boolean?
returns:
cookies:
type: array
Expand Down Expand Up @@ -1234,6 +1236,8 @@ BrowserContext:
offline: boolean

storageState:
parameters:
indexedDB: boolean?
returns:
cookies:
type: array
Expand Down
2 changes: 2 additions & 0 deletions tests/library/browsercontext-storage-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,6 @@ it('should support IndexedDB', async ({ page, server, contextFactory }) => {
- listitem:
- text: /Pet the cat/
`);

expect(await context.storageState({ indexedDB: false })).toEqual({ cookies: [], origins: [] });
});

0 comments on commit 902e83f

Please sign in to comment.