chore: tweaks after protocol separation (#10917)

This commit is contained in:
Nikolay Vitkov 2023-09-17 00:00:39 +02:00 committed by GitHub
parent 09207a71b8
commit 1283ca5bcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 96 additions and 80 deletions

View File

@ -44,7 +44,7 @@ import {debugError} from './util.js';
/**
* @internal
*/
interface Options {
export interface BidiBrowserOptions {
process?: ChildProcess;
closeCallback?: BrowserCloseCallback;
connection: BidiConnection;
@ -75,7 +75,7 @@ export class BidiBrowser extends Browser {
'cdp.Debugger.scriptParsed',
];
static async create(opts: Options): Promise<BidiBrowser> {
static async create(opts: BidiBrowserOptions): Promise<BidiBrowser> {
let browserName = '';
let browserVersion = '';
@ -135,7 +135,7 @@ export class BidiBrowser extends Browser {
]);
constructor(
opts: Options & {
opts: BidiBrowserOptions & {
browserName: string;
browserVersion: string;
}

View File

@ -25,7 +25,10 @@ import {type BidiBrowser} from './Browser.js';
import {type BidiConnection} from './Connection.js';
import {type BidiPage} from './Page.js';
interface BrowserContextOptions {
/**
* @internal
*/
export interface BidiBrowserContextOptions {
defaultViewport: Viewport | null;
isDefault: boolean;
}
@ -39,7 +42,7 @@ export class BidiBrowserContext extends BrowserContext {
#defaultViewport: Viewport | null;
#isDefault = false;
constructor(browser: BidiBrowser, options: BrowserContextOptions) {
constructor(browser: BidiBrowser, options: BidiBrowserContextOptions) {
super();
this.#browser = browser;
this.#connection = this.#browser.connection;

View File

@ -16,7 +16,7 @@ import {assert} from '../util/assert.js';
import {Deferred} from '../util/Deferred.js';
import {type BidiConnection} from './Connection.js';
import {Realm} from './Realm.js';
import {BidiRealm} from './Realm.js';
import {debugError} from './util.js';
/**
@ -150,7 +150,7 @@ export interface BrowsingContextEvents extends Record<EventType, unknown> {
/**
* @internal
*/
export class BrowsingContext extends Realm {
export class BrowsingContext extends BidiRealm {
#id: string;
#url: string;
#cdpSession: CDPSession;
@ -182,8 +182,8 @@ export class BrowsingContext extends Realm {
this.#url = info.url;
}
createRealmForSandbox(): Realm {
return new Realm(this.connection);
createRealmForSandbox(): BidiRealm {
return new BidiRealm(this.connection);
}
get url(): string {

View File

@ -30,7 +30,7 @@ const debugProtocolReceive = debug('puppeteer:webDriverBiDi:RECV ◀');
/**
* @internal
*/
interface Commands {
export interface Commands {
'script.evaluate': {
params: Bidi.Script.EvaluateParameters;
returnType: Bidi.Script.EvaluateResult;
@ -93,6 +93,10 @@ interface Commands {
params: Bidi.BrowsingContext.HandleUserPromptParameters;
returnType: Bidi.EmptyResult;
};
'browsingContext.setViewport': {
params: Bidi.BrowsingContext.SetViewportParameters;
returnType: Bidi.EmptyResult;
};
'input.performActions': {
params: Bidi.Input.PerformActionsParameters;

View File

@ -21,7 +21,7 @@ import {throwIfDisposed} from '../util/decorators.js';
import {type BidiFrame} from './Frame.js';
import {BidiJSHandle} from './JSHandle.js';
import {type Realm} from './Realm.js';
import {type BidiRealm} from './Realm.js';
import {type Sandbox} from './Sandbox.js';
/**
@ -44,7 +44,7 @@ export class BidiElementHandle<
return this.realm.environment;
}
context(): Realm {
context(): BidiRealm {
return this.handle.context();
}

View File

@ -13,14 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {type BrowsingContext} from './BrowsingContext.js';
interface Viewport {
width: number;
height: number;
}
/**
* @internal
*/
@ -31,13 +27,12 @@ export class EmulationManager {
this.#browsingContext = browsingContext;
}
async emulateViewport(viewport: Viewport): Promise<void> {
await this.#browsingContext.connection.send(
'browsingContext.setViewport' as any,
{
context: this.#browsingContext.id,
viewport,
}
);
async emulateViewport(
viewport: Bidi.BrowsingContext.Viewport
): Promise<void> {
await this.#browsingContext.connection.send('browsingContext.setViewport', {
context: this.#browsingContext.id,
viewport,
});
}
}

View File

@ -18,11 +18,10 @@ import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {type CDPSession} from '../api/CDPSession.js';
import {Frame, throwIfDetached} from '../api/Frame.js';
import {UTILITY_WORLD_NAME} from '../cdp/FrameManager.js';
import {type PuppeteerLifeCycleEvent} from '../cdp/LifecycleWatcher.js';
import {type TimeoutSettings} from '../common/TimeoutSettings.js';
import {type Awaitable} from '../common/types.js';
import {waitForEvent} from '../common/util.js';
import {UTILITY_WORLD_NAME, waitForEvent} from '../common/util.js';
import {Deferred} from '../util/Deferred.js';
import {

View File

@ -18,9 +18,9 @@ import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {type Point} from '../api/ElementHandle.js';
import {
Keyboard as BaseKeyboard,
Mouse as BaseMouse,
Touchscreen as BaseTouchscreen,
Keyboard,
Mouse,
Touchscreen,
type KeyDownOptions,
type KeyPressOptions,
type KeyboardTypeOptions,
@ -284,7 +284,7 @@ const getBidiKeyValue = (key: KeyInput) => {
/**
* @internal
*/
export class Keyboard extends BaseKeyboard {
export class BidiKeyboard extends Keyboard {
#context: BrowsingContext;
constructor(context: BrowsingContext) {
@ -420,21 +420,21 @@ export class Keyboard extends BaseKeyboard {
/**
* @internal
*/
interface BidiMouseClickOptions extends MouseClickOptions {
export interface BidiMouseClickOptions extends MouseClickOptions {
origin?: Bidi.Input.Origin;
}
/**
* @internal
*/
interface BidiMouseMoveOptions extends MouseMoveOptions {
export interface BidiMouseMoveOptions extends MouseMoveOptions {
origin?: Bidi.Input.Origin;
}
/**
* @internal
*/
interface BidiTouchMoveOptions {
export interface BidiTouchMoveOptions {
origin?: Bidi.Input.Origin;
}
@ -456,7 +456,7 @@ const getBidiButton = (button: MouseButton) => {
/**
* @internal
*/
export class Mouse extends BaseMouse {
export class BidiMouse extends Mouse {
#context: BrowsingContext;
#lastMovePoint?: Point;
@ -610,7 +610,7 @@ export class Mouse extends BaseMouse {
/**
* @internal
*/
export class Touchscreen extends BaseTouchscreen {
export class BidiTouchscreen extends Touchscreen {
#context: BrowsingContext;
constructor(context: BrowsingContext) {

View File

@ -15,12 +15,11 @@
*/
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import type Protocol from 'devtools-protocol';
import {type ElementHandle} from '../api/ElementHandle.js';
import {JSHandle} from '../api/JSHandle.js';
import {type Realm} from './Realm.js';
import {type BidiRealm} from './Realm.js';
import {type Sandbox} from './Sandbox.js';
import {BidiSerializer} from './Serializer.js';
import {releaseReference} from './util.js';
@ -39,7 +38,7 @@ export class BidiJSHandle<T = unknown> extends JSHandle<T> {
this.#remoteValue = remoteValue;
}
context(): Realm {
context(): BidiRealm {
return this.realm.environment.context();
}
@ -105,7 +104,7 @@ export class BidiJSHandle<T = unknown> extends JSHandle<T> {
return this.#remoteValue;
}
override remoteObject(): Protocol.Runtime.RemoteObject {
override remoteObject(): never {
throw new Error('Not available in WebDriver BiDi');
}
}

View File

@ -32,7 +32,7 @@ import {type BidiPage} from './Page.js';
/**
* @internal
*/
export interface NetworkManagerEvents extends Record<EventType, unknown> {
export interface BidiNetworkManagerEvents extends Record<EventType, unknown> {
[NetworkManagerEvent.Request]: BidiHTTPRequest;
[NetworkManagerEvent.RequestServedFromCache]: BidiHTTPRequest;
[NetworkManagerEvent.Response]: BidiHTTPResponse;
@ -43,7 +43,7 @@ export interface NetworkManagerEvents extends Record<EventType, unknown> {
/**
* @internal
*/
export class BidiNetworkManager extends EventEmitter<NetworkManagerEvents> {
export class BidiNetworkManager extends EventEmitter<BidiNetworkManagerEvents> {
#connection: BidiConnection;
#page: BidiPage;
#subscriptions = new DisposableStack();

View File

@ -69,7 +69,7 @@ import {EmulationManager} from './EmulationManager.js';
import {BidiFrame} from './Frame.js';
import {type BidiHTTPRequest} from './HTTPRequest.js';
import {type BidiHTTPResponse} from './HTTPResponse.js';
import {Keyboard, Mouse, Touchscreen} from './Input.js';
import {BidiKeyboard, BidiMouse, BidiTouchscreen} from './Input.js';
import {BidiNetworkManager} from './NetworkManager.js';
import {createBidiHandle} from './Realm.js';
import {BidiSerializer} from './Serializer.js';
@ -139,9 +139,9 @@ export class BidiPage extends Page {
#coverage: Coverage;
#cdpEmulationManager: CdpEmulationManager;
#emulationManager: EmulationManager;
#mouse: Mouse;
#touchscreen: Touchscreen;
#keyboard: Keyboard;
#mouse: BidiMouse;
#touchscreen: BidiTouchscreen;
#keyboard: BidiKeyboard;
#browsingContext: BrowsingContext;
#browserContext: BidiBrowserContext;
@ -192,9 +192,9 @@ export class BidiPage extends Page {
this.mainFrame().context().cdpSession
);
this.#emulationManager = new EmulationManager(browsingContext);
this.#mouse = new Mouse(this.mainFrame().context());
this.#touchscreen = new Touchscreen(this.mainFrame().context());
this.#keyboard = new Keyboard(this.mainFrame().context());
this.#mouse = new BidiMouse(this.mainFrame().context());
this.#touchscreen = new BidiTouchscreen(this.mainFrame().context());
this.#keyboard = new BidiKeyboard(this.mainFrame().context());
}
_setBrowserContext(browserContext: BidiBrowserContext): void {
@ -213,15 +213,15 @@ export class BidiPage extends Page {
return this.#coverage;
}
override get mouse(): Mouse {
override get mouse(): BidiMouse {
return this.#mouse;
}
override get touchscreen(): Touchscreen {
override get touchscreen(): BidiTouchscreen {
return this.#touchscreen;
}
override get keyboard(): Keyboard {
override get keyboard(): BidiKeyboard {
return this.#keyboard;
}

View File

@ -5,7 +5,9 @@ import {scriptInjector} from '../common/ScriptInjector.js';
import {type EvaluateFunc, type HandleFor} from '../common/types.js';
import {
PuppeteerURL,
SOURCE_URL_REGEX,
getSourcePuppeteerURLIfAvailable,
getSourceUrlComment,
isString,
} from '../common/util.js';
import type PuppeteerUtil from '../injected/injected.js';
@ -18,16 +20,10 @@ import {type Sandbox} from './Sandbox.js';
import {BidiSerializer} from './Serializer.js';
import {createEvaluationError} from './util.js';
export const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
export const getSourceUrlComment = (url: string): string => {
return `//# sourceURL=${url}`;
};
/**
* @internal
*/
export class Realm extends EventEmitter<Record<EventType, any>> {
export class BidiRealm extends EventEmitter<Record<EventType, any>> {
readonly connection: BidiConnection;
#id!: string;

View File

@ -22,7 +22,7 @@ import {withSourcePuppeteerURLIfNone} from '../common/util.js';
import {type BrowsingContext} from './BrowsingContext.js';
import {type BidiFrame} from './Frame.js';
import {type Realm as BidiRealm} from './Realm.js';
import {type BidiRealm as BidiRealm} from './Realm.js';
/**
* A unique key for {@link SandboxChart} to denote the default world.
* Realms are automatically created in the default sandbox.

View File

@ -16,7 +16,6 @@
import {type CDPSession} from '../api/CDPSession.js';
import {Target, TargetType} from '../api/Target.js';
import type {WebWorker} from '../cdp/WebWorker.js';
import {type BidiBrowser} from './Browser.js';
import {type BidiBrowserContext} from './BrowserContext.js';
@ -34,7 +33,7 @@ export class BidiTarget extends Target {
this._browserContext = browserContext;
}
override async worker(): Promise<WebWorker | null> {
override async worker(): Promise<null> {
return null;
}

View File

@ -17,6 +17,16 @@
export * from './BidiOverCdp.js';
export * from './Browser.js';
export * from './BrowserContext.js';
export * from './BrowsingContext.js';
export * from './Connection.js';
export * from './ElementHandle.js';
export * from './Frame.js';
export * from './HTTPRequest.js';
export * from './HTTPResponse.js';
export * from './Input.js';
export * from './JSHandle.js';
export * from './NetworkManager.js';
export * from './Page.js';
export * from './Realm.js';
export * from './Sandbox.js';
export * from './Target.js';

View File

@ -19,7 +19,7 @@ import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {debug} from '../common/Debug.js';
import {PuppeteerURL} from '../common/util.js';
import {type Realm} from './Realm.js';
import {type BidiRealm} from './Realm.js';
import {BidiSerializer} from './Serializer.js';
/**
@ -31,7 +31,7 @@ export const debugError = debug('puppeteer:error');
* @internal
*/
export async function releaseReference(
client: Realm,
client: BidiRealm,
remoteReference: Bidi.Script.RemoteReference
): Promise<void> {
if (!remoteReference.handle) {

View File

@ -24,9 +24,11 @@ import {scriptInjector} from '../common/ScriptInjector.js';
import {type EvaluateFunc, type HandleFor} from '../common/types.js';
import {
PuppeteerURL,
SOURCE_URL_REGEX,
createEvaluationError,
debugError,
getSourcePuppeteerURLIfAvailable,
getSourceUrlComment,
isString,
valueFromRemoteObject,
} from '../common/util.js';
@ -40,12 +42,6 @@ import {CdpElementHandle} from './ElementHandle.js';
import {type IsolatedWorld} from './IsolatedWorld.js';
import {CdpJSHandle} from './JSHandle.js';
const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
const getSourceUrlComment = (url: string) => {
return `//# sourceURL=${url}`;
};
/**
* @internal
*/

View File

@ -21,7 +21,7 @@ import {FrameEvent} from '../api/Frame.js';
import {type Page} from '../api/Page.js';
import {EventEmitter, type EventType} from '../common/EventEmitter.js';
import {type TimeoutSettings} from '../common/TimeoutSettings.js';
import {debugError, PuppeteerURL} from '../common/util.js';
import {debugError, PuppeteerURL, UTILITY_WORLD_NAME} from '../common/util.js';
import {assert} from '../util/assert.js';
import {Deferred} from '../util/Deferred.js';
import {isErrorLike} from '../util/ErrorLike.js';
@ -37,11 +37,6 @@ import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
import {NetworkManager} from './NetworkManager.js';
import {type CdpTarget} from './Target.js';
/**
* @internal
*/
export const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';
/**
* We use symbols to prevent external parties listening to these events.
* They are internal to Puppeteer.

View File

@ -80,7 +80,7 @@ export namespace NetworkManagerEvent {
/**
* @internal
*/
export interface NetworkManagerEvents extends Record<EventType, unknown> {
export interface CdpNetworkManagerEvents extends Record<EventType, unknown> {
[NetworkManagerEvent.Request]: CdpHTTPRequest;
[NetworkManagerEvent.RequestServedFromCache]: CdpHTTPRequest | undefined;
[NetworkManagerEvent.Response]: CdpHTTPResponse;
@ -98,7 +98,7 @@ export interface FrameProvider {
/**
* @internal
*/
export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
export class NetworkManager extends EventEmitter<CdpNetworkManagerEvents> {
#ignoreHTTPSErrors: boolean;
#frameManager: FrameProvider;
#networkEventManager = new NetworkEventManager();

View File

@ -1,6 +1,9 @@
import {source as injectedSource} from '../generated/injected.js';
class ScriptInjector {
/**
* @internal
*/
export class ScriptInjector {
#updated = false;
#amendments = new Set<string>();
@ -17,7 +20,7 @@ class ScriptInjector {
});
}
inject(inject: (script: string) => void, force = false) {
inject(inject: (script: string) => void, force = false): void {
if (this.#updated || force) {
inject(this.#get());
}

View File

@ -654,3 +654,19 @@ export function timeout(ms: number): Observable<never> {
})
);
}
/**
* @internal
*/
export const UTILITY_WORLD_NAME = '__puppeteer_utility_world__';
/**
* @internal
*/
export const SOURCE_URL_REGEX = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m;
/**
* @internal
*/
export function getSourceUrlComment(url: string): string {
return `//# sourceURL=${url}`;
}

View File

@ -17,6 +17,7 @@
export {Protocol} from 'devtools-protocol';
export * from './api/api.js';
export * from './bidi/bidi.js';
export * from './cdp/cdp.js';
export * from './common/common.js';
export * from './node/node.js';