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
*/
_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<string, string> = {};
private _frame: Frame;
private _frame: Frame | null;
private _continueRequestOverrides: ContinueRequestOverrides;
private _responseForRequest: Partial<ResponseForRequest> | 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<void> {
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,

View File

@ -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(

View File

@ -86,7 +86,7 @@ export class NetworkManager extends EventEmitter {
_networkEventManager = new NetworkEventManager();
_extraHTTPHeaders: Record<string, string> = {};
_credentials?: Credentials = null;
_credentials?: Credentials;
_attemptedAuthentications = new Set<string>();
_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);
}