Skip to content

Commit

Permalink
feat: support network.setCacheBehavior (#2593)
Browse files Browse the repository at this point in the history
Closes #2385

---------

Co-authored-by: browser-automation-bot <133232582+browser-automation-bot@users.noreply.github.com>
  • Loading branch information
1 parent 36fb707 commit 75ba46c
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 99 deletions.
3 changes: 2 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ export default [
'mocha/no-exclusive-tests': 'error',
'mocha/no-mocha-arrows': 'off',
'mocha/no-setup-in-describe': 'off',
'no-console': 'warn',
'no-else-return': 'warn',

'no-empty': [
Expand Down Expand Up @@ -181,6 +180,8 @@ export default [
},

rules: {
'no-console': 'warn',

'no-restricted-syntax': [
'error',
{
Expand Down
24 changes: 21 additions & 3 deletions src/bidiMapper/modules/cdp/CdpTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class CdpTarget {
readonly #unhandledPromptBehavior?: Session.UserPromptHandler;
readonly #logger: LoggerFn | undefined;

#cacheDisableState = false;
#networkDomainEnabled = false;
#fetchDomainStages = {
request: false,
Expand Down Expand Up @@ -279,9 +280,9 @@ export class CdpTarget {
this.#networkDomainEnabled = enabled;
try {
await Promise.all([
this.#cdpClient.sendCommand(
enabled ? 'Network.enable' : 'Network.disable'
),
this.#cdpClient
.sendCommand(enabled ? 'Network.enable' : 'Network.disable')
.then(async () => await this.toggleSetCacheDisabled()),
this.toggleFetchIfNeeded(),
]);
} catch (err) {
Expand All @@ -290,6 +291,23 @@ export class CdpTarget {
}
}

async toggleSetCacheDisabled(disable?: boolean) {
const defaultCacheDisabled =
this.#networkStorage.defaultCacheBehavior === 'bypass';
const cacheDisabled = disable ?? defaultCacheDisabled;

if (
!this.#networkDomainEnabled ||
this.#cacheDisableState === cacheDisabled
) {
return;
}
this.#cacheDisableState = cacheDisabled;
await this.#cdpClient.sendCommand('Network.setCacheDisabled', {
cacheDisabled,
});
}

async toggleDeviceAccessIfNeeded(): Promise<void> {
const enabled = this.isSubscribedTo(BiDiModule.Bluetooth);
await this.#cdpClient.sendCommand(
Expand Down
31 changes: 26 additions & 5 deletions src/bidiMapper/modules/network/NetworkProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
type EmptyResult,
NoSuchRequestException,
InvalidArgumentException,
UnknownErrorException,
} from '../../../protocol/protocol.js';
import {URLPattern} from '../../../utils/UrlPattern.js';
import type {BrowsingContextStorage} from '../context/BrowsingContextStorage.js';
Expand Down Expand Up @@ -189,13 +188,35 @@ export class NetworkProcessor {
return {};
}

// eslint-disable-next-line @typescript-eslint/require-await
async setCacheBehavior(
_params: Network.SetCacheBehaviorParameters
params: Network.SetCacheBehaviorParameters
): Promise<EmptyResult> {
throw new UnknownErrorException(
"Method 'network.setCacheBehavior' is not implemented."
const contexts = this.#browsingContextStorage.verifyTopLevelContextsList(
params.contexts
);

// Change all targets
if (contexts.size === 0) {
this.#networkStorage.defaultCacheBehavior = params.cacheBehavior;

await Promise.all(
this.#browsingContextStorage.getAllContexts().map((context) => {
return context.cdpTarget.toggleSetCacheDisabled();
})
);

return {};
}

const cacheDisabled = params.cacheBehavior === 'bypass';

await Promise.all(
[...contexts.values()].map((context) => {
return context.cdpTarget.toggleSetCacheDisabled(cacheDisabled);
})
);

return {};
}

#getRequestOrFail(id: Network.Request): NetworkRequest {
Expand Down
13 changes: 13 additions & 0 deletions src/bidiMapper/modules/network/NetworkStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export class NetworkStorage {
/** A map from intercept ID to track active network intercepts. */
readonly #intercepts = new Map<Network.Intercept, NetworkInterception>();

#defaultCacheBehavior: Network.SetCacheBehaviorParameters['cacheBehavior'] =
'default';

constructor(
eventManager: EventManager,
browsingContextStorage: BrowsingContextStorage,
Expand Down Expand Up @@ -328,4 +331,14 @@ export class NetworkStorage {
this.#browsingContextStorage.findContext(contextId)?.navigationId ?? null
);
}

set defaultCacheBehavior(
behavior: Network.SetCacheBehaviorParameters['cacheBehavior']
) {
this.#defaultCacheBehavior = behavior;
}

get defaultCacheBehavior() {
return this.#defaultCacheBehavior;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 75ba46c

Please sign in to comment.