From 474d73fe1b651d0e7bc141af9983dad50269ac8f Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Tue, 14 Nov 2023 15:21:24 +0100 Subject: [PATCH] refactor: extract Connect interfaces into common (#11392) --- .../src/cdp/BrowserConnector.ts | 56 ++----------- .../puppeteer-core/src/cdp/ConnectOptions.ts | 34 -------- packages/puppeteer-core/src/cdp/cdp.ts | 1 - .../src/common/ConnectOptions.ts | 79 +++++++++++++++++++ .../puppeteer-core/src/common/Puppeteer.ts | 2 +- packages/puppeteer-core/src/common/common.ts | 2 +- .../puppeteer-core/src/node/LaunchOptions.ts | 2 +- .../puppeteer-core/src/node/PuppeteerNode.ts | 6 +- 8 files changed, 94 insertions(+), 88 deletions(-) delete mode 100644 packages/puppeteer-core/src/cdp/ConnectOptions.ts create mode 100644 packages/puppeteer-core/src/common/ConnectOptions.ts diff --git a/packages/puppeteer-core/src/cdp/BrowserConnector.ts b/packages/puppeteer-core/src/cdp/BrowserConnector.ts index 9178b553..611a333e 100644 --- a/packages/puppeteer-core/src/cdp/BrowserConnector.ts +++ b/packages/puppeteer-core/src/cdp/BrowserConnector.ts @@ -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. diff --git a/packages/puppeteer-core/src/cdp/ConnectOptions.ts b/packages/puppeteer-core/src/cdp/ConnectOptions.ts deleted file mode 100644 index 29f9c0f4..00000000 --- a/packages/puppeteer-core/src/cdp/ConnectOptions.ts +++ /dev/null @@ -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; -} diff --git a/packages/puppeteer-core/src/cdp/cdp.ts b/packages/puppeteer-core/src/cdp/cdp.ts index c87a5162..aba3e98f 100644 --- a/packages/puppeteer-core/src/cdp/cdp.ts +++ b/packages/puppeteer-core/src/cdp/cdp.ts @@ -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'; diff --git a/packages/puppeteer-core/src/common/ConnectOptions.ts b/packages/puppeteer-core/src/common/ConnectOptions.ts new file mode 100644 index 00000000..c7463c0e --- /dev/null +++ b/packages/puppeteer-core/src/common/ConnectOptions.ts @@ -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; +} diff --git a/packages/puppeteer-core/src/common/Puppeteer.ts b/packages/puppeteer-core/src/common/Puppeteer.ts index e8c81b17..264bddba 100644 --- a/packages/puppeteer-core/src/common/Puppeteer.ts +++ b/packages/puppeteer-core/src/common/Puppeteer.ts @@ -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, diff --git a/packages/puppeteer-core/src/common/common.ts b/packages/puppeteer-core/src/common/common.ts index f9b43ee8..43fb7a72 100644 --- a/packages/puppeteer-core/src/common/common.ts +++ b/packages/puppeteer-core/src/common/common.ts @@ -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'; diff --git a/packages/puppeteer-core/src/node/LaunchOptions.ts b/packages/puppeteer-core/src/node/LaunchOptions.ts index d7015d45..826b293c 100644 --- a/packages/puppeteer-core/src/node/LaunchOptions.ts +++ b/packages/puppeteer-core/src/node/LaunchOptions.ts @@ -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'; /** diff --git a/packages/puppeteer-core/src/node/PuppeteerNode.ts b/packages/puppeteer-core/src/node/PuppeteerNode.ts index 23d682d4..b03fc517 100644 --- a/packages/puppeteer-core/src/node/PuppeteerNode.ts +++ b/packages/puppeteer-core/src/node/PuppeteerNode.ts @@ -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';