From aa014d8f5fb468da0beba62a497af17ed07fab33 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Mon, 23 May 2022 17:41:11 +0200 Subject: [PATCH] chore: fix TS issues in NetworkManager (#8386) --- src/common/HTTPRequest.ts | 22 +++++++++++++++++----- src/common/NetworkEventManager.ts | 6 ++++-- src/common/NetworkManager.ts | 14 ++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/common/HTTPRequest.ts b/src/common/HTTPRequest.ts index 70cd9650..cd57d3eb 100644 --- a/src/common/HTTPRequest.ts +++ b/src/common/HTTPRequest.ts @@ -120,11 +120,11 @@ export class HTTPRequest { /** * @internal */ - _interceptionId: string; + _interceptionId: string | undefined; /** * @internal */ - _failureText = null; + _failureText: string | null = null; /** * @internal */ @@ -148,7 +148,7 @@ export class HTTPRequest { private _method: string; private _postData?: string; private _headers: Record = {}; - private _frame: Frame; + private _frame: Frame | null; private _continueRequestOverrides: ContinueRequestOverrides; private _responseForRequest: Partial | null = null; private _abortErrorReason: Protocol.Network.ErrorReason | null = null; @@ -163,8 +163,8 @@ export class HTTPRequest { */ constructor( client: CDPSession, - frame: Frame, - interceptionId: string, + frame: Frame | null, + interceptionId: string | undefined, allowInterception: boolean, event: Protocol.Network.RequestWillBeSentEvent, redirectChain: HTTPRequest[] @@ -476,6 +476,10 @@ export class HTTPRequest { ? Buffer.from(postData).toString('base64') : undefined; + if (this._interceptionId === undefined) + throw new Error( + 'HTTPRequest is missing _interceptionId needed for Fetch.continueRequest' + ); await this._client .send('Fetch.continueRequest', { requestId: this._interceptionId, @@ -577,6 +581,10 @@ export class HTTPRequest { ); const status = response.status || 200; + if (this._interceptionId === undefined) + throw new Error( + 'HTTPRequest is missing _interceptionId needed for Fetch.fulfillRequest' + ); await this._client .send('Fetch.fulfillRequest', { requestId: this._interceptionId, @@ -634,6 +642,10 @@ export class HTTPRequest { errorReason: Protocol.Network.ErrorReason | null ): Promise { this._interceptionHandled = true; + if (this._interceptionId === undefined) + throw new Error( + 'HTTPRequest is missing _interceptionId needed for Fetch.failRequest' + ); await this._client .send('Fetch.failRequest', { requestId: this._interceptionId, diff --git a/src/common/NetworkEventManager.ts b/src/common/NetworkEventManager.ts index 97431094..632a8f5a 100644 --- a/src/common/NetworkEventManager.ts +++ b/src/common/NetworkEventManager.ts @@ -97,14 +97,16 @@ export class NetworkEventManager { if (!this._responseReceivedExtraInfoMap.has(networkRequestId)) { this._responseReceivedExtraInfoMap.set(networkRequestId, []); } - return this._responseReceivedExtraInfoMap.get(networkRequestId); + return this._responseReceivedExtraInfoMap.get( + networkRequestId + ) as Protocol.Network.ResponseReceivedExtraInfoEvent[]; } private queuedRedirectInfo(fetchRequestId: FetchRequestId): RedirectInfoList { if (!this._queuedRedirectInfoMap.has(fetchRequestId)) { this._queuedRedirectInfoMap.set(fetchRequestId, []); } - return this._queuedRedirectInfoMap.get(fetchRequestId); + return this._queuedRedirectInfoMap.get(fetchRequestId) as RedirectInfoList; } queueRedirectInfo( diff --git a/src/common/NetworkManager.ts b/src/common/NetworkManager.ts index 84efafa3..1f3c748e 100644 --- a/src/common/NetworkManager.ts +++ b/src/common/NetworkManager.ts @@ -86,7 +86,7 @@ export class NetworkManager extends EventEmitter { _networkEventManager = new NetworkEventManager(); _extraHTTPHeaders: Record = {}; - _credentials?: Credentials = null; + _credentials?: Credentials; _attemptedAuthentications = new Set(); _userRequestInterceptionEnabled = false; _protocolRequestInterceptionEnabled = false; @@ -275,7 +275,7 @@ export class NetworkManager extends EventEmitter { return; } - this._onRequest(event, null); + this._onRequest(event, undefined); } _onAuthRequired(event: Protocol.Fetch.AuthRequiredEvent): void { @@ -368,7 +368,7 @@ export class NetworkManager extends EventEmitter { event: Protocol.Network.RequestWillBeSentEvent, fetchRequestId?: FetchRequestId ): void { - let redirectChain = []; + let redirectChain: HTTPRequest[] = []; if (event.redirectResponse) { // We want to emit a response and requestfinished for the // redirectResponse, but we can't do so unless we have a @@ -406,6 +406,7 @@ export class NetworkManager extends EventEmitter { const frame = event.frameId ? this._frameManager.frame(event.frameId) : null; + const request = new HTTPRequest( this._client, frame, @@ -430,7 +431,7 @@ export class NetworkManager extends EventEmitter { _handleRequestRedirect( request: HTTPRequest, responsePayload: Protocol.Network.Response, - extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent + extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent | null ): void { const response = new HTTPResponse( this._client, @@ -539,7 +540,8 @@ export class NetworkManager extends EventEmitter { const interceptionId = request._interceptionId; this._networkEventManager.forgetRequest(requestId); - this._attemptedAuthentications.delete(interceptionId); + interceptionId !== undefined && + this._attemptedAuthentications.delete(interceptionId); if (events) { this._networkEventManager.forget(requestId); @@ -567,7 +569,7 @@ export class NetworkManager extends EventEmitter { // Under certain conditions we never get the Network.responseReceived // event from protocol. @see https://crbug.com/883475 - if (request.response()) request.response()._resolveBody(null); + if (request.response()) request.response()?._resolveBody(null); this._forgetRequest(request, true); this.emit(NetworkManagerEmittedEvents.RequestFinished, request); }