mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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()
|
||||
|
||||
- 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()
|
||||
|
||||
|
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 _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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -209,7 +210,7 @@ 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;
|
||||
}
|
||||
@ -217,7 +218,7 @@ export class HTTPRequest {
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
@ -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);
|
||||
@ -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,
|
||||
@ -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,
|
||||
@ -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,
|
||||
})
|
||||
@ -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,
|
||||
@ -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);
|
||||
}
|
||||
@ -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',
|
||||
|
@ -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 {
|
||||
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user