refactor: extract Connect interfaces into common (#11392)

This commit is contained in:
Nikolay Vitkov 2023-11-14 15:21:24 +01:00 committed by GitHub
parent a9e959e8fe
commit 474d73fe1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 94 additions and 88 deletions

View File

@ -14,66 +14,24 @@
* limitations under the License.
*/
import type {
IsPageTargetCallback,
TargetFilterCallback,
} from '../api/Browser.js';
import type {BidiBrowser} from '../bidi/Browser.js';
import type {ConnectionTransport} from '../common/ConnectionTransport.js';
import type {
BrowserConnectOptions,
ConnectOptions,
} from '../common/ConnectOptions.js';
import {UnsupportedOperation} from '../common/Errors.js';
import {getFetch} from '../common/fetch.js';
import {debugError} from '../common/util.js';
import type {Viewport} from '../common/Viewport.js';
import {isNode} from '../environment.js';
import {assert} from '../util/assert.js';
import {isErrorLike} from '../util/ErrorLike.js';
import {CdpBrowser} from './Browser.js';
import {Connection} from './Connection.js';
import type {ConnectOptions} from './ConnectOptions.js';
const DEFAULT_VIEWPORT = Object.freeze({width: 800, height: 600});
/**
* Generic browser options that can be passed when launching any browser or when
* connecting to an existing browser instance.
* @public
*/
export interface BrowserConnectOptions {
/**
* Whether to ignore HTTPS errors during navigation.
* @defaultValue `false`
*/
ignoreHTTPSErrors?: boolean;
/**
* Sets the viewport for each page.
*/
defaultViewport?: Viewport | null;
/**
* Slows down Puppeteer operations by the specified amount of milliseconds to
* aid debugging.
*/
slowMo?: number;
/**
* Callback to decide if Puppeteer should connect to a given target or not.
*/
targetFilter?: TargetFilterCallback;
/**
* @internal
*/
_isPageTarget?: IsPageTargetCallback;
/**
* @defaultValue 'cdp'
* @internal
*/
protocol?: 'cdp' | 'webDriverBiDi';
/**
* Timeout setting for individual protocol (CDP) calls.
*
* @defaultValue `180_000`
*/
protocolTimeout?: number;
}
const getWebSocketTransportClass = async () => {
return isNode
? (await import('../node/NodeWebSocketTransport.js')).NodeWebSocketTransport
@ -139,7 +97,9 @@ export async function _connectToBiDiOverCdpBrowser(
const version = await connection.send('Browser.getVersion');
if (version.product.toLowerCase().includes('firefox')) {
throw new Error('Firefox is not supported in BiDi over CDP mode.');
throw new UnsupportedOperation(
'Firefox is not supported in BiDi over CDP mode.'
);
}
// TODO: use other options too.

View File

@ -1,34 +0,0 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type {ConnectionTransport} from '../common/ConnectionTransport.js';
import type {BrowserConnectOptions} from './BrowserConnector.js';
/**
* @public
*/
export interface ConnectOptions extends BrowserConnectOptions {
browserWSEndpoint?: string;
browserURL?: string;
transport?: ConnectionTransport;
/**
* Headers to use for the web socket connection.
* @remarks
* Only works in the Node.js environment.
*/
headers?: Record<string, string>;
}

View File

@ -22,7 +22,6 @@ export * from './BrowserConnector.js';
export * from './CDPSession.js';
export * from './ChromeTargetManager.js';
export * from './Connection.js';
export * from './ConnectOptions.js';
export * from './Coverage.js';
export * from './DeviceRequestPrompt.js';
export * from './Dialog.js';

View File

@ -0,0 +1,79 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type {
IsPageTargetCallback,
TargetFilterCallback,
} from '../api/Browser.js';
import type {ConnectionTransport} from './ConnectionTransport.js';
import type {Viewport} from './Viewport.js';
/**
* Generic browser options that can be passed when launching any browser or when
* connecting to an existing browser instance.
* @public
*/
export interface BrowserConnectOptions {
/**
* Whether to ignore HTTPS errors during navigation.
* @defaultValue `false`
*/
ignoreHTTPSErrors?: boolean;
/**
* Sets the viewport for each page.
*/
defaultViewport?: Viewport | null;
/**
* Slows down Puppeteer operations by the specified amount of milliseconds to
* aid debugging.
*/
slowMo?: number;
/**
* Callback to decide if Puppeteer should connect to a given target or not.
*/
targetFilter?: TargetFilterCallback;
/**
* @internal
*/
_isPageTarget?: IsPageTargetCallback;
/**
* @defaultValue 'cdp'
* @internal
*/
protocol?: 'cdp' | 'webDriverBiDi';
/**
* Timeout setting for individual protocol (CDP) calls.
*
* @defaultValue `180_000`
*/
protocolTimeout?: number;
}
/**
* @public
*/
export interface ConnectOptions extends BrowserConnectOptions {
browserWSEndpoint?: string;
browserURL?: string;
transport?: ConnectionTransport;
/**
* Headers to use for the web socket connection.
* @remarks
* Only works in the Node.js environment.
*/
headers?: Record<string, string>;
}

View File

@ -19,8 +19,8 @@ import {
_connectToBiDiOverCdpBrowser,
_connectToCdpBrowser,
} from '../cdp/BrowserConnector.js';
import type {ConnectOptions} from '../cdp/ConnectOptions.js';
import type {ConnectOptions} from './ConnectOptions.js';
import {
type CustomQueryHandler,
customQueryHandlers,

View File

@ -15,9 +15,9 @@
*/
export * from './BrowserWebSocketTransport.js';
export * from './common.js';
export * from './Configuration.js';
export * from './ConnectionTransport.js';
export * from './ConnectOptions.js';
export * from './ConsoleMessage.js';
export * from './CustomQueryHandler.js';
export * from './Debug.js';

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
import type {BrowserConnectOptions} from '../cdp/BrowserConnector.js';
import type {BrowserConnectOptions} from '../common/ConnectOptions.js';
import type {Product} from '../common/Product.js';
/**

View File

@ -23,9 +23,11 @@ import {
} from '@puppeteer/browsers';
import type {Browser} from '../api/Browser.js';
import type {BrowserConnectOptions} from '../cdp/BrowserConnector.js';
import type {ConnectOptions} from '../cdp/ConnectOptions.js';
import type {Configuration} from '../common/Configuration.js';
import type {
ConnectOptions,
BrowserConnectOptions,
} from '../common/ConnectOptions.js';
import type {Product} from '../common/Product.js';
import {type CommonPuppeteerSettings, Puppeteer} from '../common/Puppeteer.js';
import {PUPPETEER_REVISIONS} from '../revisions.js';