Skip to content

Commit

Permalink
fix: strict mode fixes for HTTPRequest/Response classes
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed May 2, 2022
1 parent 1934e44 commit de44d9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
35 changes: 19 additions & 16 deletions src/common/HTTPRequest.ts
Expand Up @@ -150,9 +150,11 @@ export class HTTPRequest {
private _headers: Record<string, string> = {};
private _frame: Frame;
private _continueRequestOverrides: ContinueRequestOverrides;
private _responseForRequest: Partial<ResponseForRequest>;
private _abortErrorReason: Protocol.Network.ErrorReason;
private _interceptResolutionState: InterceptResolutionState;
private _responseForRequest: Partial<ResponseForRequest> = {};
private _abortErrorReason: Protocol.Network.ErrorReason | undefined;
private _interceptResolutionState: InterceptResolutionState = {
action: InterceptResolutionAction.None,
};
private _interceptHandlers: Array<() => void | PromiseLike<any>>;
private _initiator: Protocol.Network.Initiator;

Expand All @@ -174,13 +176,12 @@ export class HTTPRequest {
this._interceptionId = interceptionId;
this._allowInterception = allowInterception;
this._url = event.request.url;
this._resourceType = event.type.toLowerCase() as ResourceType;
this._resourceType = event.type?.toLowerCase() as ResourceType;
this._method = event.request.method;
this._postData = event.request.postData;
this._frame = frame;
this._redirectChain = redirectChain;
this._continueRequestOverrides = {};
this._interceptResolutionState = { action: InterceptResolutionAction.None };
this._interceptHandlers = [];
this._initiator = event.initiator;

Expand Down Expand Up @@ -217,7 +218,7 @@ export class HTTPRequest {
/**
* @returns the most recent reason for aborting the request
*/
abortErrorReason(): Protocol.Network.ErrorReason {
abortErrorReason(): Protocol.Network.ErrorReason | undefined {
assert(this._allowInterception, 'Request Interception is not enabled!');
return this._abortErrorReason;
}
Expand Down Expand Up @@ -440,8 +441,8 @@ export class HTTPRequest {
}
this._continueRequestOverrides = overrides;
if (
priority > this._interceptResolutionState.priority ||
this._interceptResolutionState.priority === undefined
this._interceptResolutionState.priority === undefined ||
priority > this._interceptResolutionState.priority
) {
this._interceptResolutionState = {
action: InterceptResolutionAction.Continue,
Expand Down Expand Up @@ -530,8 +531,8 @@ export class HTTPRequest {
}
this._responseForRequest = response;
if (
priority > this._interceptResolutionState.priority ||
this._interceptResolutionState.priority === undefined
this._interceptResolutionState.priority === undefined ||
priority > this._interceptResolutionState.priority
) {
this._interceptResolutionState = {
action: InterceptResolutionAction.Respond,
Expand Down Expand Up @@ -572,11 +573,13 @@ export class HTTPRequest {
Buffer.byteLength(responseBody)
);

const status = response.status || 200;
await this._client
.send('Fetch.fulfillRequest', {
requestId: this._interceptionId,
responseCode: response.status || 200,
responsePhrase: STATUS_TEXTS[response.status || 200],
responseCode: status,
responsePhrase:
STATUS_TEXTS[String(status) as keyof typeof STATUS_TEXTS],
responseHeaders: headersArray(responseHeaders),
body: responseBody ? responseBody.toString('base64') : undefined,
})
Expand Down Expand Up @@ -614,8 +617,8 @@ export class HTTPRequest {
}
this._abortErrorReason = errorReason;
if (
priority >= this._interceptResolutionState.priority ||
this._interceptResolutionState.priority === undefined
this._interceptResolutionState.priority === undefined ||
priority >= this._interceptResolutionState.priority
) {
this._interceptResolutionState = {
action: InterceptResolutionAction.Abort,
Expand All @@ -626,13 +629,13 @@ export class HTTPRequest {
}

private async _abort(
errorReason: Protocol.Network.ErrorReason
errorReason: Protocol.Network.ErrorReason | undefined
): Promise<void> {
this._interceptionHandled = true;
await this._client
.send('Fetch.failRequest', {
requestId: this._interceptionId,
errorReason,
errorReason: errorReason || 'Failed',
})
.catch(handleError);
}
Expand Down
13 changes: 8 additions & 5 deletions src/common/HTTPResponse.ts
Expand Up @@ -48,7 +48,7 @@ export class HTTPResponse {
private _request: HTTPRequest;
private _contentPromise: Promise<Buffer> | null = null;
private _bodyLoadedPromise: Promise<Error | void>;
private _bodyLoadedPromiseFulfill: (err: Error | void) => void;
private _bodyLoadedPromiseFulfill: (err: Error | void) => void = () => {};
private _remoteAddress: RemoteAddress;
private _status: number;
private _statusText: string;
Expand Down Expand Up @@ -76,8 +76,8 @@ export class HTTPResponse {
});

this._remoteAddress = {
ip: responsePayload.remoteIPAddress,
port: responsePayload.remotePort,
ip: responsePayload.remoteIPAddress || '',
port: responsePayload.remotePort || 0,
};
this._statusText =
this._parseStatusTextFromExtrInfo(extraInfo) ||
Expand All @@ -94,7 +94,7 @@ export class HTTPResponse {
this._securityDetails = responsePayload.securityDetails
? new SecurityDetails(responsePayload.securityDetails)
: null;
this._timing = responsePayload.timing;
this._timing = responsePayload.timing || null;
}

/**
Expand All @@ -117,7 +117,10 @@ export class HTTPResponse {
* @internal
*/
_resolveBody(err: Error | null): void {
return this._bodyLoadedPromiseFulfill(err);
if (err) {
return this._bodyLoadedPromiseFulfill(err);
}
return this._bodyLoadedPromiseFulfill();
}

/**
Expand Down

0 comments on commit de44d9c

Please sign in to comment.