chore: use abstract for HTTP classes (#11300)

This commit is contained in:
Nikolay Vitkov 2023-11-06 10:15:09 +01:00 committed by GitHub
parent 76434112ac
commit 47e708b979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 144 additions and 161 deletions

View File

@ -10,7 +10,7 @@ Aborts a request.
```typescript
class HTTPRequest {
abort(errorCode?: ErrorCode, priority?: number): Promise<void>;
abstract abort(errorCode?: ErrorCode, priority?: number): Promise<void>;
}
```

View File

@ -10,7 +10,7 @@ The most recent reason for aborting the request
```typescript
class HTTPRequest {
abortErrorReason(): Protocol.Network.ErrorReason | null;
abstract abortErrorReason(): Protocol.Network.ErrorReason | null;
}
```

View File

@ -10,7 +10,7 @@ Continues request with optional request overrides.
```typescript
class HTTPRequest {
continue(
abstract continue(
overrides?: ContinueRequestOverrides,
priority?: number
): Promise<void>;

View File

@ -10,7 +10,7 @@ The `ContinueRequestOverrides` that will be used if the interception is allowed
```typescript
class HTTPRequest {
continueRequestOverrides(): ContinueRequestOverrides;
abstract continueRequestOverrides(): ContinueRequestOverrides;
}
```

View File

@ -10,7 +10,7 @@ Adds an async request handler to the processing queue. Deferred handlers are not
```typescript
class HTTPRequest {
enqueueInterceptAction(
abstract enqueueInterceptAction(
pendingHandler: () => void | PromiseLike<unknown>
): void;
}

View File

@ -10,7 +10,7 @@ Access information about the request's failure.
```typescript
class HTTPRequest {
failure(): {
abstract failure(): {
errorText: string;
} | null;
}

View File

@ -10,7 +10,7 @@ Awaits pending interception handlers and then decides how to fulfill the request
```typescript
class HTTPRequest {
finalizeInterceptions(): Promise<void>;
abstract finalizeInterceptions(): Promise<void>;
}
```

View File

@ -10,7 +10,7 @@ The frame that initiated the request, or null if navigating to error pages.
```typescript
class HTTPRequest {
frame(): Frame | null;
abstract frame(): Frame | null;
}
```

View File

@ -10,7 +10,7 @@ An object with HTTP headers associated with the request. All header names are lo
```typescript
class HTTPRequest {
headers(): Record<string, string>;
abstract headers(): Record<string, string>;
}
```

View File

@ -10,7 +10,7 @@ The initiator of the request.
```typescript
class HTTPRequest {
initiator(): Protocol.Network.Initiator | undefined;
abstract initiator(): Protocol.Network.Initiator | undefined;
}
```

View File

@ -14,7 +14,7 @@ InterceptResolutionAction is one of: `abort`, `respond`, `continue`, `disabled`,
```typescript
class HTTPRequest {
interceptResolutionState(): InterceptResolutionState;
abstract interceptResolutionState(): InterceptResolutionState;
}
```

View File

@ -10,7 +10,7 @@ Is `true` if the intercept resolution has already been handled, `false` otherwis
```typescript
class HTTPRequest {
isInterceptResolutionHandled(): boolean;
abstract isInterceptResolutionHandled(): boolean;
}
```

View File

@ -10,7 +10,7 @@ True if the request is the driver of the current frame's navigation.
```typescript
class HTTPRequest {
isNavigationRequest(): boolean;
abstract isNavigationRequest(): boolean;
}
```

View File

@ -9,7 +9,7 @@ Represents an HTTP request sent by a page.
#### Signature:
```typescript
export declare class HTTPRequest
export declare abstract class HTTPRequest
```
## Remarks

View File

@ -10,7 +10,7 @@ The method used (`GET`, `POST`, etc.)
```typescript
class HTTPRequest {
method(): string;
abstract method(): string;
}
```

View File

@ -10,7 +10,7 @@ The request's post body, if any.
```typescript
class HTTPRequest {
postData(): string | undefined;
abstract postData(): string | undefined;
}
```

View File

@ -10,7 +10,7 @@ A `redirectChain` is a chain of requests initiated to fetch a resource.
```typescript
class HTTPRequest {
redirectChain(): HTTPRequest[];
abstract redirectChain(): HTTPRequest[];
}
```

View File

@ -10,7 +10,7 @@ Contains the request's resource type as it was perceived by the rendering engine
```typescript
class HTTPRequest {
resourceType(): ResourceType;
abstract resourceType(): ResourceType;
}
```

View File

@ -10,7 +10,7 @@ Fulfills a request with the given response.
```typescript
class HTTPRequest {
respond(
abstract respond(
response: Partial<ResponseForRequest>,
priority?: number
): Promise<void>;

View File

@ -10,7 +10,7 @@ A matching `HTTPResponse` object, or null if the response has not been received
```typescript
class HTTPRequest {
response(): HTTPResponse | null;
abstract response(): HTTPResponse | null;
}
```

View File

@ -10,7 +10,7 @@ The `ResponseForRequest` that gets used if the interception is allowed to respon
```typescript
class HTTPRequest {
responseForRequest(): Partial<ResponseForRequest> | null;
abstract responseForRequest(): Partial<ResponseForRequest> | null;
}
```

View File

@ -10,7 +10,7 @@ The URL of the request
```typescript
class HTTPRequest {
url(): string;
abstract url(): string;
}
```

View File

@ -10,7 +10,7 @@ Promise which resolves to a buffer with response body.
```typescript
class HTTPResponse {
buffer(): Promise<Buffer>;
abstract buffer(): Promise<Buffer>;
}
```

View File

@ -10,7 +10,7 @@ A [Frame](./puppeteer.frame.md) that initiated this response, or `null` if navig
```typescript
class HTTPResponse {
frame(): Frame | null;
abstract frame(): Frame | null;
}
```

View File

@ -10,7 +10,7 @@ True if the response was served from either the browser's disk cache or memory c
```typescript
class HTTPResponse {
fromCache(): boolean;
abstract fromCache(): boolean;
}
```

View File

@ -10,7 +10,7 @@ True if the response was served by a service worker.
```typescript
class HTTPResponse {
fromServiceWorker(): boolean;
abstract fromServiceWorker(): boolean;
}
```

View File

@ -10,7 +10,7 @@ An object with HTTP headers associated with the response. All header names are l
```typescript
class HTTPResponse {
headers(): Record<string, string>;
abstract headers(): Record<string, string>;
}
```

View File

@ -9,7 +9,7 @@ The HTTPResponse class represents responses which are received by the [Page](./p
#### Signature:
```typescript
export declare class HTTPResponse
export declare abstract class HTTPResponse
```
## Remarks

View File

@ -10,7 +10,7 @@ The IP address and port number used to connect to the remote server.
```typescript
class HTTPResponse {
remoteAddress(): RemoteAddress;
abstract remoteAddress(): RemoteAddress;
}
```

View File

@ -10,7 +10,7 @@ A matching [HTTPRequest](./puppeteer.httprequest.md) object.
```typescript
class HTTPResponse {
request(): HTTPRequest;
abstract request(): HTTPRequest;
}
```

View File

@ -10,7 +10,7 @@ sidebar_label: HTTPResponse.securityDetails
```typescript
class HTTPResponse {
securityDetails(): SecurityDetails | null;
abstract securityDetails(): SecurityDetails | null;
}
```

View File

@ -10,7 +10,7 @@ The status code of the response (e.g., 200 for a success).
```typescript
class HTTPResponse {
status(): number;
abstract status(): number;
}
```

View File

@ -10,7 +10,7 @@ The status text of the response (e.g. usually an "OK" for a success).
```typescript
class HTTPResponse {
statusText(): string;
abstract statusText(): string;
}
```

View File

@ -10,7 +10,7 @@ Timing information related to the response.
```typescript
class HTTPResponse {
timing(): Protocol.Network.ResourceTiming | null;
abstract timing(): Protocol.Network.ResourceTiming | null;
}
```

View File

@ -10,7 +10,7 @@ The URL of the response.
```typescript
class HTTPResponse {
url(): string;
abstract url(): string;
}
```

View File

@ -100,7 +100,7 @@ export const DEFAULT_INTERCEPT_RESOLUTION_PRIORITY = 0;
*
* @public
*/
export class HTTPRequest {
export abstract class HTTPRequest {
/**
* @internal
*/
@ -131,9 +131,7 @@ export class HTTPRequest {
*
* @experimental
*/
get client(): CDPSession {
throw new Error('Not implemented');
}
abstract get client(): CDPSession;
/**
* @internal
@ -143,33 +141,25 @@ export class HTTPRequest {
/**
* The URL of the request
*/
url(): string {
throw new Error('Not implemented');
}
abstract url(): string;
/**
* The `ContinueRequestOverrides` that will be used
* if the interception is allowed to continue (ie, `abort()` and
* `respond()` aren't called).
*/
continueRequestOverrides(): ContinueRequestOverrides {
throw new Error('Not implemented');
}
abstract continueRequestOverrides(): ContinueRequestOverrides;
/**
* The `ResponseForRequest` that gets used if the
* interception is allowed to respond (ie, `abort()` is not called).
*/
responseForRequest(): Partial<ResponseForRequest> | null {
throw new Error('Not implemented');
}
abstract responseForRequest(): Partial<ResponseForRequest> | null;
/**
* The most recent reason for aborting the request
*/
abortErrorReason(): Protocol.Network.ErrorReason | null {
throw new Error('Not implemented');
}
abstract abortErrorReason(): Protocol.Network.ErrorReason | null;
/**
* An InterceptResolutionState object describing the current resolution
@ -182,17 +172,13 @@ export class HTTPRequest {
* InterceptResolutionAction is one of: `abort`, `respond`, `continue`,
* `disabled`, `none`, or `already-handled`.
*/
interceptResolutionState(): InterceptResolutionState {
throw new Error('Not implemented');
}
abstract interceptResolutionState(): InterceptResolutionState;
/**
* Is `true` if the intercept resolution has already been handled,
* `false` otherwise.
*/
isInterceptResolutionHandled(): boolean {
throw new Error('Not implemented');
}
abstract isInterceptResolutionHandled(): boolean;
/**
* Adds an async request handler to the processing queue.
@ -200,80 +186,59 @@ export class HTTPRequest {
* but they are guaranteed to resolve before the request interception
* is finalized.
*/
enqueueInterceptAction(
abstract enqueueInterceptAction(
pendingHandler: () => void | PromiseLike<unknown>
): void;
enqueueInterceptAction(): void {
throw new Error('Not implemented');
}
/**
* Awaits pending interception handlers and then decides how to fulfill
* the request interception.
*/
async finalizeInterceptions(): Promise<void> {
throw new Error('Not implemented');
}
abstract finalizeInterceptions(): Promise<void>;
/**
* Contains the request's resource type as it was perceived by the rendering
* engine.
*/
resourceType(): ResourceType {
throw new Error('Not implemented');
}
abstract resourceType(): ResourceType;
/**
* The method used (`GET`, `POST`, etc.)
*/
method(): string {
throw new Error('Not implemented');
}
abstract method(): string;
/**
* The request's post body, if any.
*/
postData(): string | undefined {
throw new Error('Not implemented');
}
abstract postData(): string | undefined;
/**
* An object with HTTP headers associated with the request. All
* header names are lower-case.
*/
headers(): Record<string, string> {
throw new Error('Not implemented');
}
abstract headers(): Record<string, string>;
/**
* A matching `HTTPResponse` object, or null if the response has not
* been received yet.
*/
response(): HTTPResponse | null {
throw new Error('Not implemented');
}
abstract response(): HTTPResponse | null;
/**
* The frame that initiated the request, or null if navigating to
* error pages.
*/
frame(): Frame | null {
throw new Error('Not implemented');
}
abstract frame(): Frame | null;
/**
* True if the request is the driver of the current frame's navigation.
*/
isNavigationRequest(): boolean {
throw new Error('Not implemented');
}
abstract isNavigationRequest(): boolean;
/**
* The initiator of the request.
*/
initiator(): Protocol.Network.Initiator | undefined {
throw new Error('Not implemented');
}
abstract initiator(): Protocol.Network.Initiator | undefined;
/**
* A `redirectChain` is a chain of requests initiated to fetch a resource.
@ -302,9 +267,7 @@ export class HTTPRequest {
* @returns the chain of requests - if a server responds with at least a
* single redirect, this chain will contain all requests that were redirected.
*/
redirectChain(): HTTPRequest[] {
throw new Error('Not implemented');
}
abstract redirectChain(): HTTPRequest[];
/**
* Access information about the request's failure.
@ -326,9 +289,7 @@ export class HTTPRequest {
* message, e.g. `net::ERR_FAILED`. It is not guaranteed that there will be
* failure text if the request fails.
*/
failure(): {errorText: string} | null {
throw new Error('Not implemented');
}
abstract failure(): {errorText: string} | null;
/**
* Continues request with optional request overrides.
@ -359,13 +320,10 @@ export class HTTPRequest {
* cooperative handling rules. Otherwise, intercept is resolved
* immediately.
*/
async continue(
abstract continue(
overrides?: ContinueRequestOverrides,
priority?: number
): Promise<void>;
async continue(): Promise<void> {
throw new Error('Not implemented');
}
/**
* Fulfills a request with the given response.
@ -399,13 +357,10 @@ export class HTTPRequest {
* cooperative handling rules. Otherwise, intercept is resolved
* immediately.
*/
async respond(
abstract respond(
response: Partial<ResponseForRequest>,
priority?: number
): Promise<void>;
async respond(): Promise<void> {
throw new Error('Not implemented');
}
/**
* Aborts a request.
@ -420,10 +375,7 @@ export class HTTPRequest {
* cooperative handling rules. Otherwise, intercept is resolved
* immediately.
*/
async abort(errorCode?: ErrorCode, priority?: number): Promise<void>;
async abort(): Promise<void> {
throw new Error('Not implemented');
}
abstract abort(errorCode?: ErrorCode, priority?: number): Promise<void>;
}
/**
@ -499,7 +451,7 @@ export function headersArray(
* List taken from {@link https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml}
* with extra 306 and 418 codes.
*/
export const STATUS_TEXTS: Record<string, string | undefined> = {
export const STATUS_TEXTS: Record<string, string> = {
'100': 'Continue',
'101': 'Switching Protocols',
'102': 'Processing',

View File

@ -35,33 +35,22 @@ export interface RemoteAddress {
*
* @public
*/
export class HTTPResponse {
export abstract class HTTPResponse {
/**
* @internal
*/
constructor() {}
/**
* @internal
*/
_resolveBody(_err: Error | null): void {
throw new Error('Not implemented');
}
/**
* The IP address and port number used to connect to the remote
* server.
*/
remoteAddress(): RemoteAddress {
throw new Error('Not implemented');
}
abstract remoteAddress(): RemoteAddress;
/**
* The URL of the response.
*/
url(): string {
throw new Error('Not implemented');
}
abstract url(): string;
/**
* True if the response was successful (status in the range 200-299).
@ -75,47 +64,35 @@ export class HTTPResponse {
/**
* The status code of the response (e.g., 200 for a success).
*/
status(): number {
throw new Error('Not implemented');
}
abstract status(): number;
/**
* The status text of the response (e.g. usually an "OK" for a
* success).
*/
statusText(): string {
throw new Error('Not implemented');
}
abstract statusText(): string;
/**
* An object with HTTP headers associated with the response. All
* header names are lower-case.
*/
headers(): Record<string, string> {
throw new Error('Not implemented');
}
abstract headers(): Record<string, string>;
/**
* {@link SecurityDetails} if the response was received over the
* secure connection, or `null` otherwise.
*/
securityDetails(): SecurityDetails | null {
throw new Error('Not implemented');
}
abstract securityDetails(): SecurityDetails | null;
/**
* Timing information related to the response.
*/
timing(): Protocol.Network.ResourceTiming | null {
throw new Error('Not implemented');
}
abstract timing(): Protocol.Network.ResourceTiming | null;
/**
* Promise which resolves to a buffer with response body.
*/
buffer(): Promise<Buffer> {
throw new Error('Not implemented');
}
abstract buffer(): Promise<Buffer>;
/**
* Promise which resolves to a text representation of response body.
@ -141,30 +118,22 @@ export class HTTPResponse {
/**
* A matching {@link HTTPRequest} object.
*/
request(): HTTPRequest {
throw new Error('Not implemented');
}
abstract request(): HTTPRequest;
/**
* True if the response was served from either the browser's disk
* cache or memory cache.
*/
fromCache(): boolean {
throw new Error('Not implemented');
}
abstract fromCache(): boolean;
/**
* True if the response was served by a service worker.
*/
fromServiceWorker(): boolean {
throw new Error('Not implemented');
}
abstract fromServiceWorker(): boolean;
/**
* A {@link Frame} that initiated this response, or `null` if
* navigating to error pages.
*/
frame(): Frame | null {
throw new Error('Not implemented');
}
abstract frame(): Frame | null;
}

View File

@ -14,9 +14,16 @@
* limitations under the License.
*/
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import type {Protocol} from 'devtools-protocol';
import type {Frame} from '../api/Frame.js';
import type {
ContinueRequestOverrides,
InterceptResolutionState,
ResponseForRequest,
} from '../api/HTTPRequest.js';
import {HTTPRequest, type ResourceType} from '../api/HTTPRequest.js';
import type {CDPSession} from '../puppeteer-core.js';
import type {BidiHTTPResponse} from './HTTPResponse.js';
@ -64,6 +71,10 @@ export class BidiHTTPRequest extends HTTPRequest {
}
}
override get client(): CDPSession {
throw new Error('Not implemented');
}
override url(): string {
return this.#url;
}
@ -110,4 +121,49 @@ export class BidiHTTPRequest extends HTTPRequest {
override frame(): Frame | null {
return this.#frame;
}
override continueRequestOverrides(): ContinueRequestOverrides {
throw new Error('Not implemented');
}
override async continue(
_overrides: ContinueRequestOverrides = {}
): Promise<void> {
throw new Error('Not implemented');
}
override responseForRequest(): Partial<ResponseForRequest> {
throw new Error('Not implemented');
}
override abortErrorReason(): Protocol.Network.ErrorReason | null {
throw new Error('Not implemented');
}
override interceptResolutionState(): InterceptResolutionState {
throw new Error('Not implemented');
}
override isInterceptResolutionHandled(): boolean {
throw new Error('Not implemented');
}
override async finalizeInterceptions(): Promise<void> {
throw new Error('Not implemented');
}
override abort(): Promise<void> {
throw new Error('Not implemented');
}
override respond(
_response: Partial<ResponseForRequest>,
_priority?: number
): Promise<void> {
throw new Error('Not implemented');
}
override failure(): {errorText: string} | null {
throw new Error('Not implemented');
}
}

View File

@ -21,6 +21,7 @@ import {
HTTPResponse as HTTPResponse,
type RemoteAddress,
} from '../api/HTTPResponse.js';
import type {SecurityDetails} from '../common/SecurityDetails.js';
import type {BidiHTTPRequest} from './HTTPRequest.js';
@ -105,4 +106,12 @@ export class BidiHTTPResponse extends HTTPResponse {
override fromServiceWorker(): boolean {
return false;
}
override securityDetails(): SecurityDetails | null {
throw new Error('Not implemented');
}
override buffer(): Promise<Buffer> {
throw new Error('Not implemented');
}
}

View File

@ -153,7 +153,7 @@ export class CdpHTTPRequest extends HTTPRequest {
return this.#interceptionHandled;
}
override enqueueInterceptAction(
enqueueInterceptAction(
pendingHandler: () => void | PromiseLike<unknown>
): void {
this.#interceptHandlers.push(pendingHandler);

View File

@ -31,7 +31,7 @@ export class CdpHTTPResponse extends HTTPResponse {
#client: CDPSession;
#request: CdpHTTPRequest;
#contentPromise: Promise<Buffer> | null = null;
#bodyLoadedDeferred = Deferred.create<Error | void>();
#bodyLoadedDeferred = Deferred.create<void, Error>();
#remoteAddress: RemoteAddress;
#status: number;
#statusText: string;
@ -96,9 +96,9 @@ export class CdpHTTPResponse extends HTTPResponse {
return statusText;
}
override _resolveBody(err: Error | null): void {
_resolveBody(err?: Error): void {
if (err) {
return this.#bodyLoadedDeferred.resolve(err);
return this.#bodyLoadedDeferred.reject(err);
}
return this.#bodyLoadedDeferred.resolve();
}
@ -135,10 +135,7 @@ export class CdpHTTPResponse extends HTTPResponse {
if (!this.#contentPromise) {
this.#contentPromise = this.#bodyLoadedDeferred
.valueOrThrow()
.then(async error => {
if (error) {
throw error;
}
.then(async () => {
try {
const response = await this.#client.send(
'Network.getResponseBody',

View File

@ -680,7 +680,7 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
// Under certain conditions we never get the Network.responseReceived
// event from protocol. @see https://crbug.com/883475
if (request.response()) {
request.response()?._resolveBody(null);
request.response()?._resolveBody();
}
this.#forgetRequest(request, true);
this.emit(NetworkManagerEvent.RequestFinished, request);
@ -712,7 +712,7 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
request._failureText = event.errorText;
const response = request.response();
if (response) {
response._resolveBody(null);
response._resolveBody();
}
this.#forgetRequest(request, true);
this.emit(NetworkManagerEvent.RequestFailed, request);