fix!: strict mode fixes for HTTPRequest/Response classes (#8297)
Solves type issues in HTTPRequest/Response with some slight changes in the behaviour and API. Issues #6769
This commit is contained in:
parent
af0163cca3
commit
2804ae8cdb
@ -5208,8 +5208,8 @@ Contains a boolean stating whether the response was successful (status in the ra
|
|||||||
#### httpResponse.remoteAddress()
|
#### httpResponse.remoteAddress()
|
||||||
|
|
||||||
- returns: <[Object]>
|
- returns: <[Object]>
|
||||||
- `ip` <[string]> the IP address of 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
|
- `port` <[number]> the port used to connect to the remote server if known and `undefined` otherwise.
|
||||||
|
|
||||||
#### httpResponse.request()
|
#### httpResponse.request()
|
||||||
|
|
||||||
|
BIN
puppeteer-13.7.0-post.tgz
Normal file
BIN
puppeteer-13.7.0-post.tgz
Normal file
Binary file not shown.
@ -150,9 +150,11 @@ export class HTTPRequest {
|
|||||||
private _headers: Record<string, string> = {};
|
private _headers: Record<string, string> = {};
|
||||||
private _frame: Frame;
|
private _frame: Frame;
|
||||||
private _continueRequestOverrides: ContinueRequestOverrides;
|
private _continueRequestOverrides: ContinueRequestOverrides;
|
||||||
private _responseForRequest: Partial<ResponseForRequest>;
|
private _responseForRequest: Partial<ResponseForRequest> | null = null;
|
||||||
private _abortErrorReason: Protocol.Network.ErrorReason;
|
private _abortErrorReason: Protocol.Network.ErrorReason | null = null;
|
||||||
private _interceptResolutionState: InterceptResolutionState;
|
private _interceptResolutionState: InterceptResolutionState = {
|
||||||
|
action: InterceptResolutionAction.None,
|
||||||
|
};
|
||||||
private _interceptHandlers: Array<() => void | PromiseLike<any>>;
|
private _interceptHandlers: Array<() => void | PromiseLike<any>>;
|
||||||
private _initiator: Protocol.Network.Initiator;
|
private _initiator: Protocol.Network.Initiator;
|
||||||
|
|
||||||
@ -174,13 +176,12 @@ export class HTTPRequest {
|
|||||||
this._interceptionId = interceptionId;
|
this._interceptionId = interceptionId;
|
||||||
this._allowInterception = allowInterception;
|
this._allowInterception = allowInterception;
|
||||||
this._url = event.request.url;
|
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._method = event.request.method;
|
||||||
this._postData = event.request.postData;
|
this._postData = event.request.postData;
|
||||||
this._frame = frame;
|
this._frame = frame;
|
||||||
this._redirectChain = redirectChain;
|
this._redirectChain = redirectChain;
|
||||||
this._continueRequestOverrides = {};
|
this._continueRequestOverrides = {};
|
||||||
this._interceptResolutionState = { action: InterceptResolutionAction.None };
|
|
||||||
this._interceptHandlers = [];
|
this._interceptHandlers = [];
|
||||||
this._initiator = event.initiator;
|
this._initiator = event.initiator;
|
||||||
|
|
||||||
@ -209,7 +210,7 @@ export class HTTPRequest {
|
|||||||
* @returns The `ResponseForRequest` that gets used if the
|
* @returns The `ResponseForRequest` that gets used if the
|
||||||
* interception is allowed to respond (ie, `abort()` is not called).
|
* 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!');
|
assert(this._allowInterception, 'Request Interception is not enabled!');
|
||||||
return this._responseForRequest;
|
return this._responseForRequest;
|
||||||
}
|
}
|
||||||
@ -217,7 +218,7 @@ export class HTTPRequest {
|
|||||||
/**
|
/**
|
||||||
* @returns the most recent reason for aborting the request
|
* @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!');
|
assert(this._allowInterception, 'Request Interception is not enabled!');
|
||||||
return this._abortErrorReason;
|
return this._abortErrorReason;
|
||||||
}
|
}
|
||||||
@ -275,6 +276,9 @@ export class HTTPRequest {
|
|||||||
case 'abort':
|
case 'abort':
|
||||||
return this._abort(this._abortErrorReason);
|
return this._abort(this._abortErrorReason);
|
||||||
case 'respond':
|
case 'respond':
|
||||||
|
if (this._responseForRequest === null) {
|
||||||
|
throw new Error('Response is missing for the interception');
|
||||||
|
}
|
||||||
return this._respond(this._responseForRequest);
|
return this._respond(this._responseForRequest);
|
||||||
case 'continue':
|
case 'continue':
|
||||||
return this._continue(this._continueRequestOverrides);
|
return this._continue(this._continueRequestOverrides);
|
||||||
@ -440,8 +444,8 @@ export class HTTPRequest {
|
|||||||
}
|
}
|
||||||
this._continueRequestOverrides = overrides;
|
this._continueRequestOverrides = overrides;
|
||||||
if (
|
if (
|
||||||
priority > this._interceptResolutionState.priority ||
|
this._interceptResolutionState.priority === undefined ||
|
||||||
this._interceptResolutionState.priority === undefined
|
priority > this._interceptResolutionState.priority
|
||||||
) {
|
) {
|
||||||
this._interceptResolutionState = {
|
this._interceptResolutionState = {
|
||||||
action: InterceptResolutionAction.Continue,
|
action: InterceptResolutionAction.Continue,
|
||||||
@ -530,8 +534,8 @@ export class HTTPRequest {
|
|||||||
}
|
}
|
||||||
this._responseForRequest = response;
|
this._responseForRequest = response;
|
||||||
if (
|
if (
|
||||||
priority > this._interceptResolutionState.priority ||
|
this._interceptResolutionState.priority === undefined ||
|
||||||
this._interceptResolutionState.priority === undefined
|
priority > this._interceptResolutionState.priority
|
||||||
) {
|
) {
|
||||||
this._interceptResolutionState = {
|
this._interceptResolutionState = {
|
||||||
action: InterceptResolutionAction.Respond,
|
action: InterceptResolutionAction.Respond,
|
||||||
@ -572,11 +576,12 @@ export class HTTPRequest {
|
|||||||
Buffer.byteLength(responseBody)
|
Buffer.byteLength(responseBody)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const status = response.status || 200;
|
||||||
await this._client
|
await this._client
|
||||||
.send('Fetch.fulfillRequest', {
|
.send('Fetch.fulfillRequest', {
|
||||||
requestId: this._interceptionId,
|
requestId: this._interceptionId,
|
||||||
responseCode: response.status || 200,
|
responseCode: status,
|
||||||
responsePhrase: STATUS_TEXTS[response.status || 200],
|
responsePhrase: STATUS_TEXTS[status],
|
||||||
responseHeaders: headersArray(responseHeaders),
|
responseHeaders: headersArray(responseHeaders),
|
||||||
body: responseBody ? responseBody.toString('base64') : undefined,
|
body: responseBody ? responseBody.toString('base64') : undefined,
|
||||||
})
|
})
|
||||||
@ -614,8 +619,8 @@ export class HTTPRequest {
|
|||||||
}
|
}
|
||||||
this._abortErrorReason = errorReason;
|
this._abortErrorReason = errorReason;
|
||||||
if (
|
if (
|
||||||
priority >= this._interceptResolutionState.priority ||
|
this._interceptResolutionState.priority === undefined ||
|
||||||
this._interceptResolutionState.priority === undefined
|
priority >= this._interceptResolutionState.priority
|
||||||
) {
|
) {
|
||||||
this._interceptResolutionState = {
|
this._interceptResolutionState = {
|
||||||
action: InterceptResolutionAction.Abort,
|
action: InterceptResolutionAction.Abort,
|
||||||
@ -626,13 +631,13 @@ export class HTTPRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async _abort(
|
private async _abort(
|
||||||
errorReason: Protocol.Network.ErrorReason
|
errorReason: Protocol.Network.ErrorReason | null
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
this._interceptionHandled = true;
|
this._interceptionHandled = true;
|
||||||
await this._client
|
await this._client
|
||||||
.send('Fetch.failRequest', {
|
.send('Fetch.failRequest', {
|
||||||
requestId: this._interceptionId,
|
requestId: this._interceptionId,
|
||||||
errorReason,
|
errorReason: errorReason || 'Failed',
|
||||||
})
|
})
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
}
|
}
|
||||||
@ -727,7 +732,7 @@ async function handleError(error: ProtocolError) {
|
|||||||
// List taken from
|
// List taken from
|
||||||
// https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
|
// https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
|
||||||
// with extra 306 and 418 codes.
|
// with extra 306 and 418 codes.
|
||||||
const STATUS_TEXTS = {
|
const STATUS_TEXTS: { [key: string]: string | undefined } = {
|
||||||
'100': 'Continue',
|
'100': 'Continue',
|
||||||
'101': 'Switching Protocols',
|
'101': 'Switching Protocols',
|
||||||
'102': 'Processing',
|
'102': 'Processing',
|
||||||
|
@ -26,8 +26,8 @@ import { ProtocolError } from './Errors.js';
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export interface RemoteAddress {
|
export interface RemoteAddress {
|
||||||
ip: string;
|
ip?: string;
|
||||||
port: number;
|
port?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CDPSession extends EventEmitter {
|
interface CDPSession extends EventEmitter {
|
||||||
@ -48,7 +48,7 @@ export class HTTPResponse {
|
|||||||
private _request: HTTPRequest;
|
private _request: HTTPRequest;
|
||||||
private _contentPromise: Promise<Buffer> | null = null;
|
private _contentPromise: Promise<Buffer> | null = null;
|
||||||
private _bodyLoadedPromise: Promise<Error | void>;
|
private _bodyLoadedPromise: Promise<Error | void>;
|
||||||
private _bodyLoadedPromiseFulfill: (err: Error | void) => void;
|
private _bodyLoadedPromiseFulfill: (err: Error | void) => void = () => {};
|
||||||
private _remoteAddress: RemoteAddress;
|
private _remoteAddress: RemoteAddress;
|
||||||
private _status: number;
|
private _status: number;
|
||||||
private _statusText: string;
|
private _statusText: string;
|
||||||
@ -94,7 +94,7 @@ export class HTTPResponse {
|
|||||||
this._securityDetails = responsePayload.securityDetails
|
this._securityDetails = responsePayload.securityDetails
|
||||||
? new SecurityDetails(responsePayload.securityDetails)
|
? new SecurityDetails(responsePayload.securityDetails)
|
||||||
: null;
|
: null;
|
||||||
this._timing = responsePayload.timing;
|
this._timing = responsePayload.timing || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -117,7 +117,10 @@ export class HTTPResponse {
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
_resolveBody(err: Error | null): void {
|
_resolveBody(err: Error | null): void {
|
||||||
return this._bodyLoadedPromiseFulfill(err);
|
if (err) {
|
||||||
|
return this._bodyLoadedPromiseFulfill(err);
|
||||||
|
}
|
||||||
|
return this._bodyLoadedPromiseFulfill();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user