mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: disallow direct access to attributes in NetworkEventManager (#7826)
This commit is contained in:
parent
bbfad6802c
commit
a604646f44
@ -1,21 +1,22 @@
|
||||
import { Protocol } from 'devtools-protocol';
|
||||
import { HTTPRequest } from './HTTPRequest.js';
|
||||
|
||||
export type QueuedEvents = {
|
||||
responseReceived: Protocol.Network.ResponseReceivedEvent;
|
||||
export type QueuedEventGroup = {
|
||||
responseReceivedEvent: Protocol.Network.ResponseReceivedEvent;
|
||||
promise: Promise<void>;
|
||||
resolver: () => void;
|
||||
loadingFinished?: Protocol.Network.LoadingFinishedEvent;
|
||||
loadingFailed?: Protocol.Network.LoadingFailedEvent;
|
||||
loadingFinishedEvent?: Protocol.Network.LoadingFinishedEvent;
|
||||
loadingFailedEvent?: Protocol.Network.LoadingFailedEvent;
|
||||
};
|
||||
|
||||
export type FetchRequestId = string;
|
||||
export type NetworkRequestId = string;
|
||||
|
||||
export type RedirectInfoMap = Array<{
|
||||
type RedirectInfo = {
|
||||
event: Protocol.Network.RequestWillBeSentEvent;
|
||||
fetchRequestId?: FetchRequestId;
|
||||
}>;
|
||||
};
|
||||
export type RedirectInfoList = RedirectInfo[];
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -55,15 +56,15 @@ export class NetworkEventManager {
|
||||
* `_onRequestWillBeSent`, `_onRequestPaused`, `_onRequestPaused`, ...
|
||||
* (see crbug.com/1196004)
|
||||
*/
|
||||
requestWillBeSent = new Map<
|
||||
private _requestWillBeSentMap = new Map<
|
||||
NetworkRequestId,
|
||||
Protocol.Network.RequestWillBeSentEvent
|
||||
>();
|
||||
requestPaused = new Map<
|
||||
private _requestPausedMap = new Map<
|
||||
NetworkRequestId,
|
||||
Protocol.Fetch.RequestPausedEvent
|
||||
>();
|
||||
httpRequest = new Map<NetworkRequestId, HTTPRequest>();
|
||||
private _httpRequestsMap = new Map<NetworkRequestId, HTTPRequest>();
|
||||
|
||||
/*
|
||||
* The below maps are used to reconcile Network.responseReceivedExtraInfo
|
||||
@ -74,34 +75,115 @@ export class NetworkEventManager {
|
||||
* handle redirects, we have to make them Arrays to represent the chain of
|
||||
* events.
|
||||
*/
|
||||
private _responseReceivedExtraInfo = new Map<
|
||||
private _responseReceivedExtraInfoMap = new Map<
|
||||
NetworkRequestId,
|
||||
Protocol.Network.ResponseReceivedExtraInfoEvent[]
|
||||
>();
|
||||
private _queuedRedirectInfoMap = new Map<NetworkRequestId, RedirectInfoMap>();
|
||||
queuedEvents = new Map<NetworkRequestId, QueuedEvents>();
|
||||
private _queuedRedirectInfoMap = new Map<
|
||||
NetworkRequestId,
|
||||
RedirectInfoList
|
||||
>();
|
||||
private _queuedEventGroupMap = new Map<NetworkRequestId, QueuedEventGroup>();
|
||||
|
||||
forget(networkRequestId: NetworkRequestId): void {
|
||||
this.requestWillBeSent.delete(networkRequestId);
|
||||
this.requestPaused.delete(networkRequestId);
|
||||
this.queuedEvents.delete(networkRequestId);
|
||||
this._requestWillBeSentMap.delete(networkRequestId);
|
||||
this._requestPausedMap.delete(networkRequestId);
|
||||
this._queuedEventGroupMap.delete(networkRequestId);
|
||||
this._queuedRedirectInfoMap.delete(networkRequestId);
|
||||
this._responseReceivedExtraInfo.delete(networkRequestId);
|
||||
this._responseReceivedExtraInfoMap.delete(networkRequestId);
|
||||
}
|
||||
|
||||
responseExtraInfo(
|
||||
networkRequestId: NetworkRequestId
|
||||
): Protocol.Network.ResponseReceivedExtraInfoEvent[] {
|
||||
if (!this._responseReceivedExtraInfo.has(networkRequestId)) {
|
||||
this._responseReceivedExtraInfo.set(networkRequestId, []);
|
||||
if (!this._responseReceivedExtraInfoMap.has(networkRequestId)) {
|
||||
this._responseReceivedExtraInfoMap.set(networkRequestId, []);
|
||||
}
|
||||
return this._responseReceivedExtraInfo.get(networkRequestId);
|
||||
return this._responseReceivedExtraInfoMap.get(networkRequestId);
|
||||
}
|
||||
|
||||
queuedRedirectInfo(fetchRequestId: FetchRequestId): RedirectInfoMap {
|
||||
private queuedRedirectInfo(fetchRequestId: FetchRequestId): RedirectInfoList {
|
||||
if (!this._queuedRedirectInfoMap.has(fetchRequestId)) {
|
||||
this._queuedRedirectInfoMap.set(fetchRequestId, []);
|
||||
}
|
||||
return this._queuedRedirectInfoMap.get(fetchRequestId);
|
||||
}
|
||||
|
||||
queueRedirectInfo(
|
||||
fetchRequestId: FetchRequestId,
|
||||
redirectInfo: RedirectInfo
|
||||
): void {
|
||||
this.queuedRedirectInfo(fetchRequestId).push(redirectInfo);
|
||||
}
|
||||
|
||||
takeQueuedRedirectInfo(
|
||||
fetchRequestId: FetchRequestId
|
||||
): RedirectInfo | undefined {
|
||||
return this.queuedRedirectInfo(fetchRequestId).shift();
|
||||
}
|
||||
|
||||
numRequestsInProgress(): number {
|
||||
return [...this._httpRequestsMap].filter(([, request]) => {
|
||||
return !request.response();
|
||||
}).length;
|
||||
}
|
||||
|
||||
storeRequestWillBeSent(
|
||||
networkRequestId: NetworkRequestId,
|
||||
event: Protocol.Network.RequestWillBeSentEvent
|
||||
): void {
|
||||
this._requestWillBeSentMap.set(networkRequestId, event);
|
||||
}
|
||||
|
||||
getRequestWillBeSent(
|
||||
networkRequestId: NetworkRequestId
|
||||
): Protocol.Network.RequestWillBeSentEvent | undefined {
|
||||
return this._requestWillBeSentMap.get(networkRequestId);
|
||||
}
|
||||
|
||||
forgetRequestWillBeSent(networkRequestId: NetworkRequestId): void {
|
||||
this._requestPausedMap.delete(networkRequestId);
|
||||
}
|
||||
|
||||
getRequestPaused(
|
||||
networkRequestId: NetworkRequestId
|
||||
): Protocol.Fetch.RequestPausedEvent | undefined {
|
||||
return this._requestPausedMap.get(networkRequestId);
|
||||
}
|
||||
|
||||
forgetRequestPaused(networkRequestId: NetworkRequestId): void {
|
||||
this._requestPausedMap.delete(networkRequestId);
|
||||
}
|
||||
|
||||
storeRequestPaused(
|
||||
networkRequestId: NetworkRequestId,
|
||||
event: Protocol.Fetch.RequestPausedEvent
|
||||
): void {
|
||||
this._requestPausedMap.set(networkRequestId, event);
|
||||
}
|
||||
|
||||
getRequest(networkRequestId: NetworkRequestId): HTTPRequest | undefined {
|
||||
return this._httpRequestsMap.get(networkRequestId);
|
||||
}
|
||||
|
||||
storeRequest(networkRequestId: NetworkRequestId, request: HTTPRequest): void {
|
||||
this._httpRequestsMap.set(networkRequestId, request);
|
||||
}
|
||||
|
||||
forgetRequest(networkRequestId: NetworkRequestId): void {
|
||||
this._httpRequestsMap.delete(networkRequestId);
|
||||
}
|
||||
|
||||
getQueuedEventGroup(
|
||||
networkRequestId: NetworkRequestId
|
||||
): QueuedEventGroup | undefined {
|
||||
return this._queuedEventGroupMap.get(networkRequestId);
|
||||
}
|
||||
|
||||
queueEventGroup(
|
||||
networkRequestId: NetworkRequestId,
|
||||
event: QueuedEventGroup
|
||||
): void {
|
||||
this._queuedEventGroupMap.set(networkRequestId, event);
|
||||
}
|
||||
}
|
||||
|
@ -172,9 +172,7 @@ export class NetworkManager extends EventEmitter {
|
||||
}
|
||||
|
||||
numRequestsInProgress(): number {
|
||||
return [...this._networkEventManager.httpRequest].filter(([, request]) => {
|
||||
return !request.response();
|
||||
}).length;
|
||||
return this._networkEventManager.numRequestsInProgress();
|
||||
}
|
||||
|
||||
async setOfflineMode(value: boolean): Promise<void> {
|
||||
@ -265,17 +263,17 @@ export class NetworkManager extends EventEmitter {
|
||||
) {
|
||||
const { requestId: networkRequestId } = event;
|
||||
|
||||
this._networkEventManager.requestWillBeSent.set(networkRequestId, event);
|
||||
this._networkEventManager.storeRequestWillBeSent(networkRequestId, event);
|
||||
|
||||
/**
|
||||
* CDP may have sent a Fetch.requestPaused event already. Check for it.
|
||||
*/
|
||||
const requestPausedEvent =
|
||||
this._networkEventManager.requestPaused.get(networkRequestId);
|
||||
this._networkEventManager.getRequestPaused(networkRequestId);
|
||||
if (requestPausedEvent) {
|
||||
const { requestId: fetchRequestId } = requestPausedEvent;
|
||||
this._onRequest(event, fetchRequestId);
|
||||
this._networkEventManager.requestPaused.delete(networkRequestId);
|
||||
this._networkEventManager.forgetRequestPaused(networkRequestId);
|
||||
}
|
||||
|
||||
return;
|
||||
@ -336,7 +334,7 @@ export class NetworkManager extends EventEmitter {
|
||||
|
||||
const requestWillBeSentEvent = (() => {
|
||||
const requestWillBeSentEvent =
|
||||
this._networkEventManager.requestWillBeSent.get(networkRequestId);
|
||||
this._networkEventManager.getRequestWillBeSent(networkRequestId);
|
||||
|
||||
// redirect requests have the same `requestId`,
|
||||
if (
|
||||
@ -344,7 +342,7 @@ export class NetworkManager extends EventEmitter {
|
||||
(requestWillBeSentEvent.request.url !== event.request.url ||
|
||||
requestWillBeSentEvent.request.method !== event.request.method)
|
||||
) {
|
||||
this._networkEventManager.requestWillBeSent.delete(networkRequestId);
|
||||
this._networkEventManager.forgetRequestWillBeSent(networkRequestId);
|
||||
return;
|
||||
}
|
||||
return requestWillBeSentEvent;
|
||||
@ -353,7 +351,7 @@ export class NetworkManager extends EventEmitter {
|
||||
if (requestWillBeSentEvent) {
|
||||
this._onRequest(requestWillBeSentEvent, fetchRequestId);
|
||||
} else {
|
||||
this._networkEventManager.requestPaused.set(networkRequestId, event);
|
||||
this._networkEventManager.storeRequestPaused(networkRequestId, event);
|
||||
}
|
||||
}
|
||||
|
||||
@ -376,7 +374,7 @@ export class NetworkManager extends EventEmitter {
|
||||
.responseExtraInfo(event.requestId)
|
||||
.shift();
|
||||
if (!redirectResponseExtraInfo) {
|
||||
this._networkEventManager.queuedRedirectInfo(event.requestId).push({
|
||||
this._networkEventManager.queueRedirectInfo(event.requestId, {
|
||||
event,
|
||||
fetchRequestId,
|
||||
});
|
||||
@ -384,9 +382,7 @@ export class NetworkManager extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
const request = this._networkEventManager.httpRequest.get(
|
||||
event.requestId
|
||||
);
|
||||
const request = this._networkEventManager.getRequest(event.requestId);
|
||||
// If we connect late to the target, we could have missed the
|
||||
// requestWillBeSent event.
|
||||
if (request) {
|
||||
@ -409,7 +405,7 @@ export class NetworkManager extends EventEmitter {
|
||||
event,
|
||||
redirectChain
|
||||
);
|
||||
this._networkEventManager.httpRequest.set(event.requestId, request);
|
||||
this._networkEventManager.storeRequest(event.requestId, request);
|
||||
this.emit(NetworkManagerEmittedEvents.Request, request);
|
||||
request.finalizeInterceptions();
|
||||
}
|
||||
@ -417,7 +413,7 @@ export class NetworkManager extends EventEmitter {
|
||||
_onRequestServedFromCache(
|
||||
event: Protocol.Network.RequestServedFromCacheEvent
|
||||
): void {
|
||||
const request = this._networkEventManager.httpRequest.get(event.requestId);
|
||||
const request = this._networkEventManager.getRequest(event.requestId);
|
||||
if (request) request._fromMemoryCache = true;
|
||||
this.emit(NetworkManagerEmittedEvents.RequestServedFromCache, request);
|
||||
}
|
||||
@ -447,7 +443,7 @@ export class NetworkManager extends EventEmitter {
|
||||
responseReceived: Protocol.Network.ResponseReceivedEvent,
|
||||
extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent | null
|
||||
): void {
|
||||
const request = this._networkEventManager.httpRequest.get(
|
||||
const request = this._networkEventManager.getRequest(
|
||||
responseReceived.requestId
|
||||
);
|
||||
// FileUpload sends a response without a matching request.
|
||||
@ -476,7 +472,7 @@ export class NetworkManager extends EventEmitter {
|
||||
}
|
||||
|
||||
_onResponseReceived(event: Protocol.Network.ResponseReceivedEvent): void {
|
||||
const request = this._networkEventManager.httpRequest.get(event.requestId);
|
||||
const request = this._networkEventManager.getRequest(event.requestId);
|
||||
let extraInfo = null;
|
||||
if (request && !request._fromMemoryCache && event.hasExtraInfo) {
|
||||
extraInfo = this._networkEventManager
|
||||
@ -486,8 +482,8 @@ export class NetworkManager extends EventEmitter {
|
||||
// Wait until we get the corresponding ExtraInfo event.
|
||||
let resolver = null;
|
||||
const promise = new Promise<void>((resolve) => (resolver = resolve));
|
||||
this._networkEventManager.queuedEvents.set(event.requestId, {
|
||||
responseReceived: event,
|
||||
this._networkEventManager.queueEventGroup(event.requestId, {
|
||||
responseReceivedEvent: event,
|
||||
promise,
|
||||
resolver,
|
||||
});
|
||||
@ -501,7 +497,7 @@ export class NetworkManager extends EventEmitter {
|
||||
networkRequestId: NetworkRequestId
|
||||
): Promise<void> {
|
||||
const responseReceived =
|
||||
this._networkEventManager.queuedEvents.get(networkRequestId);
|
||||
this._networkEventManager.getQueuedEventGroup(networkRequestId);
|
||||
if (!responseReceived) return Promise.resolve();
|
||||
return responseReceived.promise;
|
||||
}
|
||||
@ -512,9 +508,9 @@ export class NetworkManager extends EventEmitter {
|
||||
// We may have skipped a redirect response/request pair due to waiting for
|
||||
// this ExtraInfo event. If so, continue that work now that we have the
|
||||
// request.
|
||||
const redirectInfo = this._networkEventManager
|
||||
.queuedRedirectInfo(event.requestId)
|
||||
.shift();
|
||||
const redirectInfo = this._networkEventManager.takeQueuedRedirectInfo(
|
||||
event.requestId
|
||||
);
|
||||
if (redirectInfo) {
|
||||
this._networkEventManager.responseExtraInfo(event.requestId).push(event);
|
||||
this._onRequest(redirectInfo.event, redirectInfo.fetchRequestId);
|
||||
@ -523,16 +519,16 @@ export class NetworkManager extends EventEmitter {
|
||||
|
||||
// We may have skipped response and loading events because we didn't have
|
||||
// this ExtraInfo event yet. If so, emit those events now.
|
||||
const queuedEvents = this._networkEventManager.queuedEvents.get(
|
||||
const queuedEvents = this._networkEventManager.getQueuedEventGroup(
|
||||
event.requestId
|
||||
);
|
||||
if (queuedEvents) {
|
||||
this._emitResponseEvent(queuedEvents.responseReceived, event);
|
||||
if (queuedEvents.loadingFinished) {
|
||||
this._emitLoadingFinished(queuedEvents.loadingFinished);
|
||||
this._emitResponseEvent(queuedEvents.responseReceivedEvent, event);
|
||||
if (queuedEvents.loadingFinishedEvent) {
|
||||
this._emitLoadingFinished(queuedEvents.loadingFinishedEvent);
|
||||
}
|
||||
if (queuedEvents.loadingFailed) {
|
||||
this._emitLoadingFailed(queuedEvents.loadingFailed);
|
||||
if (queuedEvents.loadingFailedEvent) {
|
||||
this._emitLoadingFailed(queuedEvents.loadingFailedEvent);
|
||||
}
|
||||
queuedEvents.resolver();
|
||||
return;
|
||||
@ -546,7 +542,7 @@ export class NetworkManager extends EventEmitter {
|
||||
const requestId = request._requestId;
|
||||
const interceptionId = request._interceptionId;
|
||||
|
||||
this._networkEventManager.httpRequest.delete(requestId);
|
||||
this._networkEventManager.forgetRequest(requestId);
|
||||
this._attemptedAuthentications.delete(interceptionId);
|
||||
|
||||
if (events) {
|
||||
@ -557,18 +553,18 @@ export class NetworkManager extends EventEmitter {
|
||||
_onLoadingFinished(event: Protocol.Network.LoadingFinishedEvent): void {
|
||||
// If the response event for this request is still waiting on a
|
||||
// corresponding ExtraInfo event, then wait to emit this event too.
|
||||
const queuedEvents = this._networkEventManager.queuedEvents.get(
|
||||
const queuedEvents = this._networkEventManager.getQueuedEventGroup(
|
||||
event.requestId
|
||||
);
|
||||
if (queuedEvents) {
|
||||
queuedEvents.loadingFinished = event;
|
||||
queuedEvents.loadingFinishedEvent = event;
|
||||
} else {
|
||||
this._emitLoadingFinished(event);
|
||||
}
|
||||
}
|
||||
|
||||
_emitLoadingFinished(event: Protocol.Network.LoadingFinishedEvent): void {
|
||||
const request = this._networkEventManager.httpRequest.get(event.requestId);
|
||||
const request = this._networkEventManager.getRequest(event.requestId);
|
||||
// For certain requestIds we never receive requestWillBeSent event.
|
||||
// @see https://crbug.com/750469
|
||||
if (!request) return;
|
||||
@ -583,18 +579,18 @@ export class NetworkManager extends EventEmitter {
|
||||
_onLoadingFailed(event: Protocol.Network.LoadingFailedEvent): void {
|
||||
// If the response event for this request is still waiting on a
|
||||
// corresponding ExtraInfo event, then wait to emit this event too.
|
||||
const queuedEvents = this._networkEventManager.queuedEvents.get(
|
||||
const queuedEvents = this._networkEventManager.getQueuedEventGroup(
|
||||
event.requestId
|
||||
);
|
||||
if (queuedEvents) {
|
||||
queuedEvents.loadingFailed = event;
|
||||
queuedEvents.loadingFailedEvent = event;
|
||||
} else {
|
||||
this._emitLoadingFailed(event);
|
||||
}
|
||||
}
|
||||
|
||||
_emitLoadingFailed(event: Protocol.Network.LoadingFailedEvent): void {
|
||||
const request = this._networkEventManager.httpRequest.get(event.requestId);
|
||||
const request = this._networkEventManager.getRequest(event.requestId);
|
||||
// For certain requestIds we never receive requestWillBeSent event.
|
||||
// @see https://crbug.com/750469
|
||||
if (!request) return;
|
||||
|
Loading…
Reference in New Issue
Block a user