chore: fix TS issues in NetworkManager (#8386)

This commit is contained in:
Alex Rudenko 2022-05-23 17:41:11 +02:00 committed by GitHub
parent 84fc4227a4
commit aa014d8f5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 13 deletions

View File

@ -120,11 +120,11 @@ export class HTTPRequest {
/** /**
* @internal * @internal
*/ */
_interceptionId: string; _interceptionId: string | undefined;
/** /**
* @internal * @internal
*/ */
_failureText = null; _failureText: string | null = null;
/** /**
* @internal * @internal
*/ */
@ -148,7 +148,7 @@ export class HTTPRequest {
private _method: string; private _method: string;
private _postData?: string; private _postData?: string;
private _headers: Record<string, string> = {}; private _headers: Record<string, string> = {};
private _frame: Frame; private _frame: Frame | null;
private _continueRequestOverrides: ContinueRequestOverrides; private _continueRequestOverrides: ContinueRequestOverrides;
private _responseForRequest: Partial<ResponseForRequest> | null = null; private _responseForRequest: Partial<ResponseForRequest> | null = null;
private _abortErrorReason: Protocol.Network.ErrorReason | null = null; private _abortErrorReason: Protocol.Network.ErrorReason | null = null;
@ -163,8 +163,8 @@ export class HTTPRequest {
*/ */
constructor( constructor(
client: CDPSession, client: CDPSession,
frame: Frame, frame: Frame | null,
interceptionId: string, interceptionId: string | undefined,
allowInterception: boolean, allowInterception: boolean,
event: Protocol.Network.RequestWillBeSentEvent, event: Protocol.Network.RequestWillBeSentEvent,
redirectChain: HTTPRequest[] redirectChain: HTTPRequest[]
@ -476,6 +476,10 @@ export class HTTPRequest {
? Buffer.from(postData).toString('base64') ? Buffer.from(postData).toString('base64')
: undefined; : undefined;
if (this._interceptionId === undefined)
throw new Error(
'HTTPRequest is missing _interceptionId needed for Fetch.continueRequest'
);
await this._client await this._client
.send('Fetch.continueRequest', { .send('Fetch.continueRequest', {
requestId: this._interceptionId, requestId: this._interceptionId,
@ -577,6 +581,10 @@ export class HTTPRequest {
); );
const status = response.status || 200; const status = response.status || 200;
if (this._interceptionId === undefined)
throw new Error(
'HTTPRequest is missing _interceptionId needed for Fetch.fulfillRequest'
);
await this._client await this._client
.send('Fetch.fulfillRequest', { .send('Fetch.fulfillRequest', {
requestId: this._interceptionId, requestId: this._interceptionId,
@ -634,6 +642,10 @@ export class HTTPRequest {
errorReason: Protocol.Network.ErrorReason | null errorReason: Protocol.Network.ErrorReason | null
): Promise<void> { ): Promise<void> {
this._interceptionHandled = true; this._interceptionHandled = true;
if (this._interceptionId === undefined)
throw new Error(
'HTTPRequest is missing _interceptionId needed for Fetch.failRequest'
);
await this._client await this._client
.send('Fetch.failRequest', { .send('Fetch.failRequest', {
requestId: this._interceptionId, requestId: this._interceptionId,

View File

@ -97,14 +97,16 @@ export class NetworkEventManager {
if (!this._responseReceivedExtraInfoMap.has(networkRequestId)) { if (!this._responseReceivedExtraInfoMap.has(networkRequestId)) {
this._responseReceivedExtraInfoMap.set(networkRequestId, []); this._responseReceivedExtraInfoMap.set(networkRequestId, []);
} }
return this._responseReceivedExtraInfoMap.get(networkRequestId); return this._responseReceivedExtraInfoMap.get(
networkRequestId
) as Protocol.Network.ResponseReceivedExtraInfoEvent[];
} }
private queuedRedirectInfo(fetchRequestId: FetchRequestId): RedirectInfoList { 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) as RedirectInfoList;
} }
queueRedirectInfo( queueRedirectInfo(

View File

@ -86,7 +86,7 @@ export class NetworkManager extends EventEmitter {
_networkEventManager = new NetworkEventManager(); _networkEventManager = new NetworkEventManager();
_extraHTTPHeaders: Record<string, string> = {}; _extraHTTPHeaders: Record<string, string> = {};
_credentials?: Credentials = null; _credentials?: Credentials;
_attemptedAuthentications = new Set<string>(); _attemptedAuthentications = new Set<string>();
_userRequestInterceptionEnabled = false; _userRequestInterceptionEnabled = false;
_protocolRequestInterceptionEnabled = false; _protocolRequestInterceptionEnabled = false;
@ -275,7 +275,7 @@ export class NetworkManager extends EventEmitter {
return; return;
} }
this._onRequest(event, null); this._onRequest(event, undefined);
} }
_onAuthRequired(event: Protocol.Fetch.AuthRequiredEvent): void { _onAuthRequired(event: Protocol.Fetch.AuthRequiredEvent): void {
@ -368,7 +368,7 @@ export class NetworkManager extends EventEmitter {
event: Protocol.Network.RequestWillBeSentEvent, event: Protocol.Network.RequestWillBeSentEvent,
fetchRequestId?: FetchRequestId fetchRequestId?: FetchRequestId
): void { ): void {
let redirectChain = []; let redirectChain: HTTPRequest[] = [];
if (event.redirectResponse) { if (event.redirectResponse) {
// We want to emit a response and requestfinished for the // We want to emit a response and requestfinished for the
// redirectResponse, but we can't do so unless we have a // redirectResponse, but we can't do so unless we have a
@ -406,6 +406,7 @@ export class NetworkManager extends EventEmitter {
const frame = event.frameId const frame = event.frameId
? this._frameManager.frame(event.frameId) ? this._frameManager.frame(event.frameId)
: null; : null;
const request = new HTTPRequest( const request = new HTTPRequest(
this._client, this._client,
frame, frame,
@ -430,7 +431,7 @@ export class NetworkManager extends EventEmitter {
_handleRequestRedirect( _handleRequestRedirect(
request: HTTPRequest, request: HTTPRequest,
responsePayload: Protocol.Network.Response, responsePayload: Protocol.Network.Response,
extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent | null
): void { ): void {
const response = new HTTPResponse( const response = new HTTPResponse(
this._client, this._client,
@ -539,7 +540,8 @@ export class NetworkManager extends EventEmitter {
const interceptionId = request._interceptionId; const interceptionId = request._interceptionId;
this._networkEventManager.forgetRequest(requestId); this._networkEventManager.forgetRequest(requestId);
this._attemptedAuthentications.delete(interceptionId); interceptionId !== undefined &&
this._attemptedAuthentications.delete(interceptionId);
if (events) { if (events) {
this._networkEventManager.forget(requestId); this._networkEventManager.forget(requestId);
@ -567,7 +569,7 @@ export class NetworkManager extends EventEmitter {
// Under certain conditions we never get the Network.responseReceived // Under certain conditions we never get the Network.responseReceived
// event from protocol. @see https://crbug.com/883475 // 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._forgetRequest(request, true);
this.emit(NetworkManagerEmittedEvents.RequestFinished, request); this.emit(NetworkManagerEmittedEvents.RequestFinished, request);
} }