mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: use a less chatty BiDi+ (#10453)
This commit is contained in:
@@ -141,7 +141,7 @@
|
||||
"author": "The Chromium Authors",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"chromium-bidi": "0.4.13",
|
||||
"chromium-bidi": "0.4.14",
|
||||
"cross-fetch": "3.1.6",
|
||||
"debug": "4.3.4",
|
||||
"devtools-protocol": "0.0.1135028",
|
||||
|
||||
@@ -36,11 +36,18 @@ import {debugError} from './utils.js';
|
||||
* @internal
|
||||
*/
|
||||
export class Browser extends BrowserBase {
|
||||
static readonly subscribeModules = [
|
||||
static readonly subscribeModules: Bidi.Session.SubscriptionRequestEvent[] = [
|
||||
'browsingContext',
|
||||
'network',
|
||||
'log',
|
||||
'cdp',
|
||||
];
|
||||
static readonly subscribeCdpEvents: Bidi.CDP.EventNames[] = [
|
||||
// Coverage
|
||||
'cdp.Debugger.scriptParsed',
|
||||
'cdp.CSS.styleSheetAdded',
|
||||
'cdp.Runtime.executionContextsCleared',
|
||||
// Tracing
|
||||
'cdp.Tracing.tracingComplete',
|
||||
];
|
||||
|
||||
#browserName = '';
|
||||
@@ -67,11 +74,9 @@ export class Browser extends BrowserBase {
|
||||
}
|
||||
|
||||
await opts.connection.send('session.subscribe', {
|
||||
events: (browserName.toLocaleLowerCase().includes('firefox')
|
||||
? Browser.subscribeModules.filter(module => {
|
||||
return !['cdp'].includes(module);
|
||||
})
|
||||
: Browser.subscribeModules) as Bidi.Message.EventNames[],
|
||||
events: browserName.toLocaleLowerCase().includes('firefox')
|
||||
? Browser.subscribeModules
|
||||
: [...Browser.subscribeModules, ...Browser.subscribeCdpEvents],
|
||||
});
|
||||
|
||||
return new Browser({
|
||||
|
||||
@@ -51,7 +51,7 @@ export class CDPSessionWrapper extends EventEmitter implements CDPSession {
|
||||
context: context.id,
|
||||
})
|
||||
.then(session => {
|
||||
this.#sessionId.resolve(session.result.cdpSession);
|
||||
this.#sessionId.resolve(session.result.session!);
|
||||
})
|
||||
.catch(err => {
|
||||
this.#sessionId.reject(err);
|
||||
@@ -65,11 +65,11 @@ export class CDPSessionWrapper extends EventEmitter implements CDPSession {
|
||||
method: T,
|
||||
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
|
||||
): Promise<ProtocolMapping.Commands[T]['returnType']> {
|
||||
const cdpSession = await this.#sessionId.valueOrThrow();
|
||||
const session = await this.#sessionId.valueOrThrow();
|
||||
const result = await this.#context.connection.send('cdp.sendCommand', {
|
||||
cdpMethod: method,
|
||||
cdpParams: paramArgs[0] || {},
|
||||
cdpSession,
|
||||
method: method,
|
||||
params: paramArgs[0],
|
||||
session,
|
||||
});
|
||||
return result.result;
|
||||
}
|
||||
|
||||
@@ -234,17 +234,14 @@ export class Connection extends EventEmitter {
|
||||
// `log.entryAdded` specific context
|
||||
} else if ('source' in event.params && event.params.source.context) {
|
||||
context = this.#browsingContexts.get(event.params.source.context);
|
||||
} else if (event.method === 'cdp.eventReceived') {
|
||||
} else if (isCDPEvent(event)) {
|
||||
// TODO: this is not a good solution and we need to find a better one.
|
||||
// Perhaps we need to have a dedicated CDP event emitter or emulate
|
||||
// the CDPSession interface with BiDi?.
|
||||
const cdpSessionId = event.params.cdpSession;
|
||||
const cdpSessionId = event.params.session;
|
||||
for (const context of this.#browsingContexts.values()) {
|
||||
if (context.cdpSession?.id() === cdpSessionId) {
|
||||
context.cdpSession!.emit(
|
||||
event.params.cdpMethod,
|
||||
event.params.cdpParams
|
||||
);
|
||||
context.cdpSession!.emit(event.params.event, event.params.params);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -285,3 +282,9 @@ function createProtocolError(object: Bidi.Message.ErrorResult): string {
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
function isCDPEvent(
|
||||
event: Bidi.Message.EventMessage
|
||||
): event is Bidi.CDP.EventReceivedEvent {
|
||||
return event.method.startsWith('cdp.');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user