Skip to content

Commit

Permalink
chore: clean NetworkManager Maps for BiDi (#10468)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Jun 29, 2023
1 parent 1c80ebe commit 0a7bad6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
47 changes: 35 additions & 12 deletions packages/puppeteer-core/src/common/bidi/NetworkManager.ts
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
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

0 comments on commit 0a7bad6

Please sign in to comment.