Skip to content

Commit

Permalink
refactor: extract Connect interfaces into common (#11392)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Nov 14, 2023
1 parent a9e959e commit 474d73f
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 88 deletions.
56 changes: 8 additions & 48 deletions packages/puppeteer-core/src/cdp/BrowserConnector.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
34 changes: 0 additions & 34 deletions packages/puppeteer-core/src/cdp/ConnectOptions.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/puppeteer-core/src/cdp/cdp.ts
Expand Up @@ -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';
Expand Down
79 changes: 79 additions & 0 deletions 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<string, string>;
}
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/common/Puppeteer.ts
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/common/common.ts
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/node/LaunchOptions.ts
Expand Up @@ -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';

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/puppeteer-core/src/node/PuppeteerNode.ts
Expand Up @@ -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';
Expand Down

0 comments on commit 474d73f

Please sign in to comment.