Skip to content

Commit

Permalink
fix!: strict mode fixes for HTTPRequest/Response classes (#8297)
Browse files Browse the repository at this point in the history
Solves type issues in HTTPRequest/Response with some slight changes in the behaviour and API.

Issues #6769
  • Loading branch information
OrKoN committed May 5, 2022
1 parent af0163c commit 2804ae8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
4 changes: 2 additions & 2 deletions docs/api.md
Expand Up @@ -5208,8 +5208,8 @@ Contains a boolean stating whether the response was successful (status in the ra
#### httpResponse.remoteAddress()

- returns: <[Object]>
- `ip` <[string]> the IP address of the remote server
- `port` <[number]> the port used to connect to the remote server
- `ip` <[string]> the IP address of the remote server if known and `undefined` otherwise.
- `port` <[number]> the port used to connect to the remote server if known and `undefined` otherwise.

#### httpResponse.request()

Expand Down
Binary file added puppeteer-13.7.0-post.tgz
Binary file not shown.
41 changes: 23 additions & 18 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> | null = null;
private _abortErrorReason: Protocol.Network.ErrorReason | null = null;
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 || 'other').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 @@ -209,15 +210,15 @@ export class HTTPRequest {
* @returns The `ResponseForRequest` that gets used if the
* interception is allowed to respond (ie, `abort()` is not called).
*/
responseForRequest(): Partial<ResponseForRequest> {
responseForRequest(): Partial<ResponseForRequest> | null {
assert(this._allowInterception, 'Request Interception is not enabled!');
return this._responseForRequest;
}

/**
* @returns the most recent reason for aborting the request
*/
abortErrorReason(): Protocol.Network.ErrorReason {
abortErrorReason(): Protocol.Network.ErrorReason | null {
assert(this._allowInterception, 'Request Interception is not enabled!');
return this._abortErrorReason;
}
Expand Down Expand Up @@ -275,6 +276,9 @@ export class HTTPRequest {
case 'abort':
return this._abort(this._abortErrorReason);
case 'respond':
if (this._responseForRequest === null) {
throw new Error('Response is missing for the interception');
}
return this._respond(this._responseForRequest);
case 'continue':
return this._continue(this._continueRequestOverrides);
Expand Down Expand Up @@ -440,8 +444,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 +534,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 +576,12 @@ 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[status],
responseHeaders: headersArray(responseHeaders),
body: responseBody ? responseBody.toString('base64') : undefined,
})
Expand Down Expand Up @@ -614,8 +619,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 +631,13 @@ export class HTTPRequest {
}

private async _abort(
errorReason: Protocol.Network.ErrorReason
errorReason: Protocol.Network.ErrorReason | null
): Promise<void> {
this._interceptionHandled = true;
await this._client
.send('Fetch.failRequest', {
requestId: this._interceptionId,
errorReason,
errorReason: errorReason || 'Failed',
})
.catch(handleError);
}
Expand Down Expand Up @@ -727,7 +732,7 @@ async function handleError(error: ProtocolError) {
// List taken from
// https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
// with extra 306 and 418 codes.
const STATUS_TEXTS = {
const STATUS_TEXTS: { [key: string]: string | undefined } = {
'100': 'Continue',
'101': 'Switching Protocols',
'102': 'Processing',
Expand Down
13 changes: 8 additions & 5 deletions src/common/HTTPResponse.ts
Expand Up @@ -26,8 +26,8 @@ import { ProtocolError } from './Errors.js';
* @public
*/
export interface RemoteAddress {
ip: string;
port: number;
ip?: string;
port?: number;
}

interface CDPSession extends EventEmitter {
Expand All @@ -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 @@ -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 2804ae8

Please sign in to comment.