Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clean NetworkManager Maps for BiDi #10468

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions packages/puppeteer-core/src/common/bidi/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {EventEmitter, Handler} from '../EventEmitter.js';
import {NetworkManagerEmittedEvents} from '../NetworkManager.js';

import {Connection} from './Connection.js';
import {Frame} from './Frame.js';
import {HTTPRequest} from './HTTPRequest.js';
import {HTTPResponse} from './HTTPResponse.js';
import {Page} from './Page.js';
Expand Down Expand Up @@ -74,18 +75,20 @@ export class NetworkManager extends EventEmitter {

#onResponseCompleted(event: Bidi.Network.ResponseCompletedParams): void {
const request = this.#requestMap.get(event.request.request);
if (request) {
const response = new HTTPResponse(request, event);
request._response = response;
if (event.navigation) {
this.#navigationMap.set(event.navigation, response);
}
if (response.fromCache()) {
this.emit(NetworkManagerEmittedEvents.RequestServedFromCache, request);
}
this.emit(NetworkManagerEmittedEvents.Response, response);
this.emit(NetworkManagerEmittedEvents.RequestFinished, request);
if (!request) {
return;
}
const response = new HTTPResponse(request, event);
request._response = response;
if (event.navigation) {
this.#navigationMap.set(event.navigation, response);
}
if (response.fromCache()) {
this.emit(NetworkManagerEmittedEvents.RequestServedFromCache, request);
}
this.emit(NetworkManagerEmittedEvents.Response, response);
this.emit(NetworkManagerEmittedEvents.RequestFinished, request);
this.#requestMap.delete(event.request.request);
}

#onFetchError(event: Bidi.Network.FetchErrorParams) {
Expand All @@ -95,10 +98,16 @@ export class NetworkManager extends EventEmitter {
}
request._failureText = event.errorText;
this.emit(NetworkManagerEmittedEvents.RequestFailed, request);
this.#requestMap.delete(event.request.request);
}

getNavigationResponse(navigationId: string | null): HTTPResponse | null {
return this.#navigationMap.get(navigationId ?? '') ?? null;
if (!navigationId) {
return null;
}
const response = this.#navigationMap.get(navigationId);

return response ?? null;
}

inFlightRequestsCount(): number {
Expand All @@ -112,6 +121,20 @@ export class NetworkManager extends EventEmitter {
return inFlightRequestCounter;
}

clearMapAfterFrameDispose(frame: Frame): void {
for (const [id, request] of this.#requestMap.entries()) {
if (request.frame() === frame) {
this.#requestMap.delete(id);
}
}

for (const [id, response] of this.#navigationMap.entries()) {
if (response.frame() === frame) {
this.#requestMap.delete(id);
}
}
}

dispose(): void {
this.removeAllListeners();
this.#requestMap.clear();
Expand Down
1 change: 1 addition & 0 deletions packages/puppeteer-core/src/common/bidi/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ export class Page extends PageBase {
this.#removeFramesRecursively(child);
}
frame.dispose();
this.#networkManager.clearMapAfterFrameDispose(frame);
this.#frameTree.removeFrame(frame);
this.emit(PageEmittedEvents.FrameDetached, frame);
}
Expand Down