From 3a11926bd84d113449054fe30d3e51df143230dc Mon Sep 17 00:00:00 2001 From: Maksim Sadym <69349599+sadym-chromium@users.noreply.github.com> Date: Thu, 9 Nov 2023 18:12:23 +0100 Subject: [PATCH] refactor: respect `protocol` option in `Puppeteer.connect` (#11338) Co-authored-by: Maksim Sadym --- .../src/cdp/BrowserConnector.ts | 2 +- .../puppeteer-core/src/cdp/ConnectOptions.ts | 34 +++++++++++++++++++ packages/puppeteer-core/src/cdp/cdp.ts | 2 +- .../src/{cdp => common}/Puppeteer.ts | 31 +++++------------ packages/puppeteer-core/src/common/common.ts | 1 + .../puppeteer-core/src/node/PuppeteerNode.ts | 7 ++-- 6 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 packages/puppeteer-core/src/cdp/ConnectOptions.ts rename packages/puppeteer-core/src/{cdp => common}/Puppeteer.ts (85%) diff --git a/packages/puppeteer-core/src/cdp/BrowserConnector.ts b/packages/puppeteer-core/src/cdp/BrowserConnector.ts index a11e37e61be..c5228b5fb3a 100644 --- a/packages/puppeteer-core/src/cdp/BrowserConnector.ts +++ b/packages/puppeteer-core/src/cdp/BrowserConnector.ts @@ -28,7 +28,7 @@ import {isErrorLike} from '../util/ErrorLike.js'; import {CdpBrowser} from './Browser.js'; import {Connection} from './Connection.js'; -import type {ConnectOptions} from './Puppeteer.js'; +import type {ConnectOptions} from './ConnectOptions.js'; /** * Generic browser options that can be passed when launching any browser or when * connecting to an existing browser instance. diff --git a/packages/puppeteer-core/src/cdp/ConnectOptions.ts b/packages/puppeteer-core/src/cdp/ConnectOptions.ts new file mode 100644 index 00000000000..29f9c0f4d2e --- /dev/null +++ b/packages/puppeteer-core/src/cdp/ConnectOptions.ts @@ -0,0 +1,34 @@ +/* + * 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 7b07690adb9..c87a51627cb 100644 --- a/packages/puppeteer-core/src/cdp/cdp.ts +++ b/packages/puppeteer-core/src/cdp/cdp.ts @@ -22,6 +22,7 @@ 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'; @@ -43,7 +44,6 @@ export * from './NetworkEventManager.js'; export * from './NetworkManager.js'; export * from './Page.js'; export * from './PredefinedNetworkConditions.js'; -export * from './Puppeteer.js'; export * from './Target.js'; export * from './TargetManager.js'; export * from './Tracing.js'; diff --git a/packages/puppeteer-core/src/cdp/Puppeteer.ts b/packages/puppeteer-core/src/common/Puppeteer.ts similarity index 85% rename from packages/puppeteer-core/src/cdp/Puppeteer.ts rename to packages/puppeteer-core/src/common/Puppeteer.ts index 8e2a9222b2a..60bb03add43 100644 --- a/packages/puppeteer-core/src/cdp/Puppeteer.ts +++ b/packages/puppeteer-core/src/common/Puppeteer.ts @@ -15,16 +15,13 @@ */ import type {Browser} from '../api/Browser.js'; -import type {ConnectionTransport} from '../common/ConnectionTransport.js'; +import {_connectToCdpBrowser} from '../cdp/BrowserConnector.js'; +import type {ConnectOptions} from '../cdp/ConnectOptions.js'; + import { type CustomQueryHandler, customQueryHandlers, -} from '../common/CustomQueryHandler.js'; - -import { - type BrowserConnectOptions, - _connectToCdpBrowser, -} from './BrowserConnector.js'; +} from './CustomQueryHandler.js'; /** * Settings that are common to the Puppeteer class, regardless of environment. @@ -34,20 +31,6 @@ import { export interface CommonPuppeteerSettings { isPuppeteerCore: boolean; } -/** - * @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; -} /** * The main Puppeteer class. @@ -145,6 +128,10 @@ export class Puppeteer { * @returns Promise which resolves to browser instance. */ connect(options: ConnectOptions): Promise { - return _connectToCdpBrowser(options); + if (options.protocol === 'webDriverBiDi') { + throw new Error('Not implemented'); + } else { + return _connectToCdpBrowser(options); + } } } diff --git a/packages/puppeteer-core/src/common/common.ts b/packages/puppeteer-core/src/common/common.ts index e6cbc85a370..f9b43ee8109 100644 --- a/packages/puppeteer-core/src/common/common.ts +++ b/packages/puppeteer-core/src/common/common.ts @@ -34,6 +34,7 @@ export * from './PDFOptions.js'; export * from './PierceQueryHandler.js'; export * from './PQueryHandler.js'; export * from './Product.js'; +export * from './Puppeteer.js'; export * from './QueryHandler.js'; export * from './ScriptInjector.js'; export * from './SecurityDetails.js'; diff --git a/packages/puppeteer-core/src/node/PuppeteerNode.ts b/packages/puppeteer-core/src/node/PuppeteerNode.ts index 0058556aa76..23d682d4509 100644 --- a/packages/puppeteer-core/src/node/PuppeteerNode.ts +++ b/packages/puppeteer-core/src/node/PuppeteerNode.ts @@ -24,13 +24,10 @@ import { import type {Browser} from '../api/Browser.js'; import type {BrowserConnectOptions} from '../cdp/BrowserConnector.js'; -import { - type CommonPuppeteerSettings, - type ConnectOptions, - Puppeteer, -} from '../cdp/Puppeteer.js'; +import type {ConnectOptions} from '../cdp/ConnectOptions.js'; import type {Configuration} from '../common/Configuration.js'; import type {Product} from '../common/Product.js'; +import {type CommonPuppeteerSettings, Puppeteer} from '../common/Puppeteer.js'; import {PUPPETEER_REVISIONS} from '../revisions.js'; import {ChromeLauncher} from './ChromeLauncher.js';