chore: add storage.getCookies and storage.setCookie to bidi/core (#11847)

Co-authored-by: Alex Rudenko <OrKoN@users.noreply.github.com>
This commit is contained in:
jrandolf 2024-02-06 17:34:41 +01:00 committed by GitHub
parent bf03e70e67
commit f62380d373
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 81 additions and 0 deletions

View File

@ -57,6 +57,14 @@ export type SetViewportOptions = Omit<
'context'
>;
/**
* @internal
*/
export type GetCookiesOptions = Omit<
Bidi.Storage.GetCookiesParameters,
'partition'
>;
/**
* @internal
*/
@ -464,6 +472,39 @@ export class BrowsingContext extends EventEmitter<{
await this.userContext.browser.removePreloadScript(script);
}
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async getCookies(
options: GetCookiesOptions = {}
): Promise<Bidi.Network.Cookie[]> {
const {
result: {cookies},
} = await this.#session.send('storage.getCookies', {
...options,
partition: {
type: 'context',
context: this.id,
},
});
return cookies;
}
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async setCookie(cookie: Bidi.Storage.PartialCookie): Promise<void> {
await this.#session.send('storage.setCookie', {
cookie,
partition: {
type: 'context',
context: this.id,
},
});
}
[disposeSymbol](): void {
this.#reason ??=
'Browsing context already closed, probably because the user context closed.';

View File

@ -12,6 +12,7 @@ import {inertIfDisposed, throwIfDisposed} from '../../util/decorators.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import type {Browser} from './Browser.js';
import type {GetCookiesOptions} from './BrowsingContext.js';
import {BrowsingContext} from './BrowsingContext.js';
/**
@ -174,6 +175,45 @@ export class UserContext extends EventEmitter<{
}
}
@throwIfDisposed<UserContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async getCookies(
options: GetCookiesOptions = {},
sourceOrigin: string | undefined = undefined
): Promise<Bidi.Network.Cookie[]> {
const {
result: {cookies},
} = await this.#session.send('storage.getCookies', {
...options,
partition: {
type: 'storageKey',
userContext: this.#id,
sourceOrigin,
},
});
return cookies;
}
@throwIfDisposed<UserContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async setCookie(
cookie: Bidi.Storage.PartialCookie,
sourceOrigin?: string
): Promise<void> {
await this.#session.send('storage.setCookie', {
cookie,
partition: {
type: 'storageKey',
sourceOrigin,
userContext: this.id,
},
});
}
[disposeSymbol](): void {
this.#reason ??=
'User context already closed, probably because the browser disconnected/closed.';