diff --git a/src/common/Connection.ts b/src/common/Connection.ts index 1e4b810a022b0..84312fa2ef073 100644 --- a/src/common/Connection.ts +++ b/src/common/Connection.ts @@ -56,7 +56,7 @@ export class Connection extends EventEmitter { #transport: ConnectionTransport; #delay: number; #lastId = 0; - #sessions: Map = new Map(); + #sessions: Map = new Map(); #closed = false; #callbacks: Map = new Map(); #manuallyAttached = new Set(); @@ -147,7 +147,7 @@ export class Connection extends EventEmitter { const object = JSON.parse(message); if (object.method === 'Target.attachedToTarget') { const sessionId = object.params.sessionId; - const session = new CDPSession( + const session = new CDPSessionImpl( this, object.params.targetInfo.type, sessionId @@ -310,6 +310,47 @@ export const CDPSessionEmittedEvents = { * @public */ export class CDPSession extends EventEmitter { + /** + * @internal + */ + constructor() { + super(); + } + + connection(): Connection | undefined { + throw new Error('Not implemented'); + } + + send( + method: T, + ...paramArgs: ProtocolMapping.Commands[T]['paramsType'] + ): Promise; + send(): Promise< + ProtocolMapping.Commands[T]['returnType'] + > { + throw new Error('Not implemented'); + } + + /** + * Detaches the cdpSession from the target. Once detached, the cdpSession object + * won't emit any events and can't be used to send messages. + */ + async detach(): Promise { + throw new Error('Not implemented'); + } + + /** + * Returns the session's id. + */ + id(): string { + throw new Error('Not implemented'); + } +} + +/** + * @internal + */ +export class CDPSessionImpl extends CDPSession { #sessionId: string; #targetType: string; #callbacks: Map = new Map(); @@ -325,11 +366,11 @@ export class CDPSession extends EventEmitter { this.#sessionId = sessionId; } - connection(): Connection | undefined { + override connection(): Connection | undefined { return this.#connection; } - send( + override send( method: T, ...paramArgs: ProtocolMapping.Commands[T]['paramsType'] ): Promise { @@ -386,7 +427,7 @@ export class CDPSession extends EventEmitter { * Detaches the cdpSession from the target. Once detached, the cdpSession object * won't emit any events and can't be used to send messages. */ - async detach(): Promise { + override async detach(): Promise { if (!this.#connection) { throw new Error( `Session already detached. Most likely the ${ @@ -419,7 +460,7 @@ export class CDPSession extends EventEmitter { /** * Returns the session's id. */ - id(): string { + override id(): string { return this.#sessionId; } } diff --git a/src/common/NetworkManager.ts b/src/common/NetworkManager.ts index da1a0d19f18b3..92deee72ccc57 100644 --- a/src/common/NetworkManager.ts +++ b/src/common/NetworkManager.ts @@ -15,7 +15,6 @@ */ import {Protocol} from 'devtools-protocol'; -import {ProtocolMapping} from 'devtools-protocol/types/protocol-mapping.js'; import {assert} from '../util/assert.js'; import {EventEmitter} from './EventEmitter.js'; import {Frame} from './Frame.js'; @@ -25,6 +24,7 @@ import {FetchRequestId, NetworkEventManager} from './NetworkEventManager.js'; import {debugError, isString} from './util.js'; import {DeferredPromise} from '../util/DeferredPromise.js'; import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js'; +import {CDPSession} from './Connection.js'; /** * @public @@ -66,13 +66,6 @@ export const NetworkManagerEmittedEvents = { RequestFinished: Symbol('NetworkManager.RequestFinished'), } as const; -interface CDPSession extends EventEmitter { - send( - method: T, - ...paramArgs: ProtocolMapping.Commands[T]['paramsType'] - ): Promise; -} - interface FrameManager { frame(frameId: string): Frame | null; } diff --git a/test/src/NetworkManager.spec.ts b/test/src/NetworkManager.spec.ts index 5602a5de65b5f..96e3a5f3e9ac2 100644 --- a/test/src/NetworkManager.spec.ts +++ b/test/src/NetworkManager.spec.ts @@ -26,6 +26,13 @@ import {HTTPResponse} from '../../lib/cjs/puppeteer/common/HTTPResponse.js'; class MockCDPSession extends EventEmitter { async send(): Promise {} + connection() { + return undefined; + } + async detach() {} + id() { + return '1'; + } } describe('NetworkManager', () => {