refactor: disallow direct access to attributes in NetworkEventManager (#7826)

This commit is contained in:
Alex Rudenko 2021-12-09 11:02:24 +01:00 committed by GitHub
parent bbfad6802c
commit a604646f44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 134 additions and 56 deletions

View File

@ -1,21 +1,22 @@
import { Protocol } from 'devtools-protocol'; import { Protocol } from 'devtools-protocol';
import { HTTPRequest } from './HTTPRequest.js'; import { HTTPRequest } from './HTTPRequest.js';
export type QueuedEvents = { export type QueuedEventGroup = {
responseReceived: Protocol.Network.ResponseReceivedEvent; responseReceivedEvent: Protocol.Network.ResponseReceivedEvent;
promise: Promise<void>; promise: Promise<void>;
resolver: () => void; resolver: () => void;
loadingFinished?: Protocol.Network.LoadingFinishedEvent; loadingFinishedEvent?: Protocol.Network.LoadingFinishedEvent;
loadingFailed?: Protocol.Network.LoadingFailedEvent; loadingFailedEvent?: Protocol.Network.LoadingFailedEvent;
}; };
export type FetchRequestId = string; export type FetchRequestId = string;
export type NetworkRequestId = string; export type NetworkRequestId = string;
export type RedirectInfoMap = Array<{ type RedirectInfo = {
event: Protocol.Network.RequestWillBeSentEvent; event: Protocol.Network.RequestWillBeSentEvent;
fetchRequestId?: FetchRequestId; fetchRequestId?: FetchRequestId;
}>; };
export type RedirectInfoList = RedirectInfo[];
/** /**
* @internal * @internal
@ -55,15 +56,15 @@ export class NetworkEventManager {
* `_onRequestWillBeSent`, `_onRequestPaused`, `_onRequestPaused`, ... * `_onRequestWillBeSent`, `_onRequestPaused`, `_onRequestPaused`, ...
* (see crbug.com/1196004) * (see crbug.com/1196004)
*/ */
requestWillBeSent = new Map< private _requestWillBeSentMap = new Map<
NetworkRequestId, NetworkRequestId,
Protocol.Network.RequestWillBeSentEvent Protocol.Network.RequestWillBeSentEvent
>(); >();
requestPaused = new Map< private _requestPausedMap = new Map<
NetworkRequestId, NetworkRequestId,
Protocol.Fetch.RequestPausedEvent Protocol.Fetch.RequestPausedEvent
>(); >();
httpRequest = new Map<NetworkRequestId, HTTPRequest>(); private _httpRequestsMap = new Map<NetworkRequestId, HTTPRequest>();
/* /*
* The below maps are used to reconcile Network.responseReceivedExtraInfo * 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 * handle redirects, we have to make them Arrays to represent the chain of
* events. * events.
*/ */
private _responseReceivedExtraInfo = new Map< private _responseReceivedExtraInfoMap = new Map<
NetworkRequestId, NetworkRequestId,
Protocol.Network.ResponseReceivedExtraInfoEvent[] Protocol.Network.ResponseReceivedExtraInfoEvent[]
>(); >();
private _queuedRedirectInfoMap = new Map<NetworkRequestId, RedirectInfoMap>(); private _queuedRedirectInfoMap = new Map<
queuedEvents = new Map<NetworkRequestId, QueuedEvents>(); NetworkRequestId,
RedirectInfoList
>();
private _queuedEventGroupMap = new Map<NetworkRequestId, QueuedEventGroup>();
forget(networkRequestId: NetworkRequestId): void { forget(networkRequestId: NetworkRequestId): void {
this.requestWillBeSent.delete(networkRequestId); this._requestWillBeSentMap.delete(networkRequestId);
this.requestPaused.delete(networkRequestId); this._requestPausedMap.delete(networkRequestId);
this.queuedEvents.delete(networkRequestId); this._queuedEventGroupMap.delete(networkRequestId);
this._queuedRedirectInfoMap.delete(networkRequestId); this._queuedRedirectInfoMap.delete(networkRequestId);
this._responseReceivedExtraInfo.delete(networkRequestId); this._responseReceivedExtraInfoMap.delete(networkRequestId);
} }
responseExtraInfo( responseExtraInfo(
networkRequestId: NetworkRequestId networkRequestId: NetworkRequestId
): Protocol.Network.ResponseReceivedExtraInfoEvent[] { ): Protocol.Network.ResponseReceivedExtraInfoEvent[] {
if (!this._responseReceivedExtraInfo.has(networkRequestId)) { if (!this._responseReceivedExtraInfoMap.has(networkRequestId)) {
this._responseReceivedExtraInfo.set(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)) { if (!this._queuedRedirectInfoMap.has(fetchRequestId)) {
this._queuedRedirectInfoMap.set(fetchRequestId, []); this._queuedRedirectInfoMap.set(fetchRequestId, []);
} }
return this._queuedRedirectInfoMap.get(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);
}
} }

View File

@ -172,9 +172,7 @@ export class NetworkManager extends EventEmitter {
} }
numRequestsInProgress(): number { numRequestsInProgress(): number {
return [...this._networkEventManager.httpRequest].filter(([, request]) => { return this._networkEventManager.numRequestsInProgress();
return !request.response();
}).length;
} }
async setOfflineMode(value: boolean): Promise<void> { async setOfflineMode(value: boolean): Promise<void> {
@ -265,17 +263,17 @@ export class NetworkManager extends EventEmitter {
) { ) {
const { requestId: networkRequestId } = event; 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. * CDP may have sent a Fetch.requestPaused event already. Check for it.
*/ */
const requestPausedEvent = const requestPausedEvent =
this._networkEventManager.requestPaused.get(networkRequestId); this._networkEventManager.getRequestPaused(networkRequestId);
if (requestPausedEvent) { if (requestPausedEvent) {
const { requestId: fetchRequestId } = requestPausedEvent; const { requestId: fetchRequestId } = requestPausedEvent;
this._onRequest(event, fetchRequestId); this._onRequest(event, fetchRequestId);
this._networkEventManager.requestPaused.delete(networkRequestId); this._networkEventManager.forgetRequestPaused(networkRequestId);
} }
return; return;
@ -336,7 +334,7 @@ export class NetworkManager extends EventEmitter {
const requestWillBeSentEvent = (() => { const requestWillBeSentEvent = (() => {
const requestWillBeSentEvent = const requestWillBeSentEvent =
this._networkEventManager.requestWillBeSent.get(networkRequestId); this._networkEventManager.getRequestWillBeSent(networkRequestId);
// redirect requests have the same `requestId`, // redirect requests have the same `requestId`,
if ( if (
@ -344,7 +342,7 @@ export class NetworkManager extends EventEmitter {
(requestWillBeSentEvent.request.url !== event.request.url || (requestWillBeSentEvent.request.url !== event.request.url ||
requestWillBeSentEvent.request.method !== event.request.method) requestWillBeSentEvent.request.method !== event.request.method)
) { ) {
this._networkEventManager.requestWillBeSent.delete(networkRequestId); this._networkEventManager.forgetRequestWillBeSent(networkRequestId);
return; return;
} }
return requestWillBeSentEvent; return requestWillBeSentEvent;
@ -353,7 +351,7 @@ export class NetworkManager extends EventEmitter {
if (requestWillBeSentEvent) { if (requestWillBeSentEvent) {
this._onRequest(requestWillBeSentEvent, fetchRequestId); this._onRequest(requestWillBeSentEvent, fetchRequestId);
} else { } else {
this._networkEventManager.requestPaused.set(networkRequestId, event); this._networkEventManager.storeRequestPaused(networkRequestId, event);
} }
} }
@ -376,7 +374,7 @@ export class NetworkManager extends EventEmitter {
.responseExtraInfo(event.requestId) .responseExtraInfo(event.requestId)
.shift(); .shift();
if (!redirectResponseExtraInfo) { if (!redirectResponseExtraInfo) {
this._networkEventManager.queuedRedirectInfo(event.requestId).push({ this._networkEventManager.queueRedirectInfo(event.requestId, {
event, event,
fetchRequestId, fetchRequestId,
}); });
@ -384,9 +382,7 @@ export class NetworkManager extends EventEmitter {
} }
} }
const request = this._networkEventManager.httpRequest.get( const request = this._networkEventManager.getRequest(event.requestId);
event.requestId
);
// If we connect late to the target, we could have missed the // If we connect late to the target, we could have missed the
// requestWillBeSent event. // requestWillBeSent event.
if (request) { if (request) {
@ -409,7 +405,7 @@ export class NetworkManager extends EventEmitter {
event, event,
redirectChain redirectChain
); );
this._networkEventManager.httpRequest.set(event.requestId, request); this._networkEventManager.storeRequest(event.requestId, request);
this.emit(NetworkManagerEmittedEvents.Request, request); this.emit(NetworkManagerEmittedEvents.Request, request);
request.finalizeInterceptions(); request.finalizeInterceptions();
} }
@ -417,7 +413,7 @@ export class NetworkManager extends EventEmitter {
_onRequestServedFromCache( _onRequestServedFromCache(
event: Protocol.Network.RequestServedFromCacheEvent event: Protocol.Network.RequestServedFromCacheEvent
): void { ): void {
const request = this._networkEventManager.httpRequest.get(event.requestId); const request = this._networkEventManager.getRequest(event.requestId);
if (request) request._fromMemoryCache = true; if (request) request._fromMemoryCache = true;
this.emit(NetworkManagerEmittedEvents.RequestServedFromCache, request); this.emit(NetworkManagerEmittedEvents.RequestServedFromCache, request);
} }
@ -447,7 +443,7 @@ export class NetworkManager extends EventEmitter {
responseReceived: Protocol.Network.ResponseReceivedEvent, responseReceived: Protocol.Network.ResponseReceivedEvent,
extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent | null extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent | null
): void { ): void {
const request = this._networkEventManager.httpRequest.get( const request = this._networkEventManager.getRequest(
responseReceived.requestId responseReceived.requestId
); );
// FileUpload sends a response without a matching request. // FileUpload sends a response without a matching request.
@ -476,7 +472,7 @@ export class NetworkManager extends EventEmitter {
} }
_onResponseReceived(event: Protocol.Network.ResponseReceivedEvent): void { _onResponseReceived(event: Protocol.Network.ResponseReceivedEvent): void {
const request = this._networkEventManager.httpRequest.get(event.requestId); const request = this._networkEventManager.getRequest(event.requestId);
let extraInfo = null; let extraInfo = null;
if (request && !request._fromMemoryCache && event.hasExtraInfo) { if (request && !request._fromMemoryCache && event.hasExtraInfo) {
extraInfo = this._networkEventManager extraInfo = this._networkEventManager
@ -486,8 +482,8 @@ export class NetworkManager extends EventEmitter {
// Wait until we get the corresponding ExtraInfo event. // Wait until we get the corresponding ExtraInfo event.
let resolver = null; let resolver = null;
const promise = new Promise<void>((resolve) => (resolver = resolve)); const promise = new Promise<void>((resolve) => (resolver = resolve));
this._networkEventManager.queuedEvents.set(event.requestId, { this._networkEventManager.queueEventGroup(event.requestId, {
responseReceived: event, responseReceivedEvent: event,
promise, promise,
resolver, resolver,
}); });
@ -501,7 +497,7 @@ export class NetworkManager extends EventEmitter {
networkRequestId: NetworkRequestId networkRequestId: NetworkRequestId
): Promise<void> { ): Promise<void> {
const responseReceived = const responseReceived =
this._networkEventManager.queuedEvents.get(networkRequestId); this._networkEventManager.getQueuedEventGroup(networkRequestId);
if (!responseReceived) return Promise.resolve(); if (!responseReceived) return Promise.resolve();
return responseReceived.promise; 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 // 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 // this ExtraInfo event. If so, continue that work now that we have the
// request. // request.
const redirectInfo = this._networkEventManager const redirectInfo = this._networkEventManager.takeQueuedRedirectInfo(
.queuedRedirectInfo(event.requestId) event.requestId
.shift(); );
if (redirectInfo) { if (redirectInfo) {
this._networkEventManager.responseExtraInfo(event.requestId).push(event); this._networkEventManager.responseExtraInfo(event.requestId).push(event);
this._onRequest(redirectInfo.event, redirectInfo.fetchRequestId); 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 // We may have skipped response and loading events because we didn't have
// this ExtraInfo event yet. If so, emit those events now. // this ExtraInfo event yet. If so, emit those events now.
const queuedEvents = this._networkEventManager.queuedEvents.get( const queuedEvents = this._networkEventManager.getQueuedEventGroup(
event.requestId event.requestId
); );
if (queuedEvents) { if (queuedEvents) {
this._emitResponseEvent(queuedEvents.responseReceived, event); this._emitResponseEvent(queuedEvents.responseReceivedEvent, event);
if (queuedEvents.loadingFinished) { if (queuedEvents.loadingFinishedEvent) {
this._emitLoadingFinished(queuedEvents.loadingFinished); this._emitLoadingFinished(queuedEvents.loadingFinishedEvent);
} }
if (queuedEvents.loadingFailed) { if (queuedEvents.loadingFailedEvent) {
this._emitLoadingFailed(queuedEvents.loadingFailed); this._emitLoadingFailed(queuedEvents.loadingFailedEvent);
} }
queuedEvents.resolver(); queuedEvents.resolver();
return; return;
@ -546,7 +542,7 @@ export class NetworkManager extends EventEmitter {
const requestId = request._requestId; const requestId = request._requestId;
const interceptionId = request._interceptionId; const interceptionId = request._interceptionId;
this._networkEventManager.httpRequest.delete(requestId); this._networkEventManager.forgetRequest(requestId);
this._attemptedAuthentications.delete(interceptionId); this._attemptedAuthentications.delete(interceptionId);
if (events) { if (events) {
@ -557,18 +553,18 @@ export class NetworkManager extends EventEmitter {
_onLoadingFinished(event: Protocol.Network.LoadingFinishedEvent): void { _onLoadingFinished(event: Protocol.Network.LoadingFinishedEvent): void {
// If the response event for this request is still waiting on a // If the response event for this request is still waiting on a
// corresponding ExtraInfo event, then wait to emit this event too. // corresponding ExtraInfo event, then wait to emit this event too.
const queuedEvents = this._networkEventManager.queuedEvents.get( const queuedEvents = this._networkEventManager.getQueuedEventGroup(
event.requestId event.requestId
); );
if (queuedEvents) { if (queuedEvents) {
queuedEvents.loadingFinished = event; queuedEvents.loadingFinishedEvent = event;
} else { } else {
this._emitLoadingFinished(event); this._emitLoadingFinished(event);
} }
} }
_emitLoadingFinished(event: Protocol.Network.LoadingFinishedEvent): void { _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. // For certain requestIds we never receive requestWillBeSent event.
// @see https://crbug.com/750469 // @see https://crbug.com/750469
if (!request) return; if (!request) return;
@ -583,18 +579,18 @@ export class NetworkManager extends EventEmitter {
_onLoadingFailed(event: Protocol.Network.LoadingFailedEvent): void { _onLoadingFailed(event: Protocol.Network.LoadingFailedEvent): void {
// If the response event for this request is still waiting on a // If the response event for this request is still waiting on a
// corresponding ExtraInfo event, then wait to emit this event too. // corresponding ExtraInfo event, then wait to emit this event too.
const queuedEvents = this._networkEventManager.queuedEvents.get( const queuedEvents = this._networkEventManager.getQueuedEventGroup(
event.requestId event.requestId
); );
if (queuedEvents) { if (queuedEvents) {
queuedEvents.loadingFailed = event; queuedEvents.loadingFailedEvent = event;
} else { } else {
this._emitLoadingFailed(event); this._emitLoadingFailed(event);
} }
} }
_emitLoadingFailed(event: Protocol.Network.LoadingFailedEvent): void { _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. // For certain requestIds we never receive requestWillBeSent event.
// @see https://crbug.com/750469 // @see https://crbug.com/750469
if (!request) return; if (!request) return;