Skip to content

Commit

Permalink
Simplify CDP client (#322)
Browse files Browse the repository at this point in the history
Better type-safety, less memory used and more aligned
with the CDP client in Puppeteer.
  • Loading branch information
OrKoN authored Nov 24, 2022
1 parent 67a33f4 commit bebb0a8
Show file tree
Hide file tree
Showing 13 changed files with 4,958 additions and 268 deletions.
4,729 changes: 4,729 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"main": "index.ts",
"scripts": {
"build": "npm run prettier && npm run clean && npm run build-mapper && npm run build-server",
"prebuild-mapper": "utils/filter_protocol_commands.js",
"build-mapper": "rollup --config src/bidiMapper/rollup.config.js",
"build-server": "tsc -b src/tsconfig.json",
"clean": "rimraf src/.build",
Expand Down
49 changes: 27 additions & 22 deletions src/bidiMapper/domains/context/browsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,18 @@ export class BrowsingContextImpl {

async #unblockAttachedTarget() {
LogManager.create(this.#cdpClient, this.#cdpSessionId, this.#eventManager);
await this.#cdpClient.Runtime.enable();
await this.#cdpClient.Page.enable();
await this.#cdpClient.Page.setLifecycleEventsEnabled({enabled: true});
await this.#cdpClient.Target.setAutoAttach({
await this.#cdpClient.sendCommand('Runtime.enable');
await this.#cdpClient.sendCommand('Page.enable');
await this.#cdpClient.sendCommand('Page.setLifecycleEventsEnabled', {
enabled: true,
});
await this.#cdpClient.sendCommand('Target.setAutoAttach', {
autoAttach: true,
waitForDebuggerOnStart: true,
flatten: true,
});

await this.#cdpClient.Runtime.runIfWaitingForDebugger();
await this.#cdpClient.sendCommand('Runtime.runIfWaitingForDebugger');
this.#targetDefers.targetUnblocked.resolve();
}

Expand Down Expand Up @@ -243,8 +245,8 @@ export class BrowsingContextImpl {
}

#initListeners() {
this.#cdpClient.Target.on(
'targetInfoChanged',
this.#cdpClient.on(
'Target.targetInfoChanged',
(params: Protocol.Target.TargetInfoChangedEvent) => {
if (this.contextId !== params.targetInfo.targetId) {
return;
Expand All @@ -253,8 +255,8 @@ export class BrowsingContextImpl {
}
);

this.#cdpClient.Page.on(
'frameNavigated',
this.#cdpClient.on(
'Page.frameNavigated',
async (params: Protocol.Page.FrameNavigatedEvent) => {
if (this.contextId !== params.frame.id) {
return;
Expand All @@ -271,8 +273,8 @@ export class BrowsingContextImpl {
}
);

this.#cdpClient.Page.on(
'navigatedWithinDocument',
this.#cdpClient.on(
'Page.navigatedWithinDocument',
(params: Protocol.Page.NavigatedWithinDocumentEvent) => {
if (this.contextId !== params.frameId) {
return;
Expand All @@ -283,8 +285,8 @@ export class BrowsingContextImpl {
}
);

this.#cdpClient.Page.on(
'lifecycleEvent',
this.#cdpClient.on(
'Page.lifecycleEvent',
async (params: Protocol.Page.LifecycleEventEvent) => {
if (this.contextId !== params.frameId) {
return;
Expand Down Expand Up @@ -334,8 +336,8 @@ export class BrowsingContextImpl {
}
);

this.#cdpClient.Runtime.on(
'executionContextCreated',
this.#cdpClient.on(
'Runtime.executionContextCreated',
(params: Protocol.Runtime.ExecutionContextCreatedEvent) => {
if (params.context.auxData.frameId !== this.contextId) {
return;
Expand Down Expand Up @@ -365,8 +367,8 @@ export class BrowsingContextImpl {
}
);

this.#cdpClient.Runtime.on(
'executionContextDestroyed',
this.#cdpClient.on(
'Runtime.executionContextDestroyed',
(params: Protocol.Runtime.ExecutionContextDestroyedEvent) => {
Realm.findRealms({
cdpSessionId: this.#cdpSessionId,
Expand Down Expand Up @@ -430,10 +432,13 @@ export class BrowsingContextImpl {
await this.#targetDefers.targetUnblocked;

// TODO: handle loading errors.
const cdpNavigateResult = await this.#cdpClient.Page.navigate({
url,
frameId: this.contextId,
});
const cdpNavigateResult = await this.#cdpClient.sendCommand(
'Page.navigate',
{
url,
frameId: this.contextId,
}
);

if (cdpNavigateResult.errorText) {
throw new UnknownException(cdpNavigateResult.errorText);
Expand Down Expand Up @@ -520,7 +525,7 @@ export class BrowsingContextImpl {
});

if (maybeSandboxes.length == 0) {
await this.#cdpClient.Page.createIsolatedWorld({
await this.#cdpClient.sendCommand('Page.createIsolatedWorld', {
frameId: this.contextId,
worldName: sandbox,
});
Expand Down
33 changes: 20 additions & 13 deletions src/bidiMapper/domains/context/browsingContextProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export class BrowsingContextProcessor {
}

#setTargetEventListeners(cdpClient: CdpClient) {
cdpClient.Target.on('attachedToTarget', async (params) => {
cdpClient.on('Target.attachedToTarget', async (params) => {
await this.#handleAttachedToTargetEvent(params, cdpClient);
});
cdpClient.Target.on('detachedFromTarget', async (params) => {
cdpClient.on('Target.detachedFromTarget', async (params) => {
await BrowsingContextProcessor.#handleDetachedFromTargetEvent(params);
});
}
Expand Down Expand Up @@ -82,8 +82,8 @@ export class BrowsingContextProcessor {
);
});

sessionCdpClient.Page.on(
'frameAttached',
sessionCdpClient.on(
'Page.frameAttached',
async (params: Protocol.Page.FrameAttachedEvent) => {
await BrowsingContextImpl.createFrameContext(
params.frameId,
Expand All @@ -106,8 +106,13 @@ export class BrowsingContextProcessor {

if (!this.#isValidTarget(targetInfo)) {
// DevTools or some other not supported by BiDi target.
await targetSessionCdpClient.Runtime.runIfWaitingForDebugger();
await parentSessionCdpClient.Target.detachFromTarget(params);
await targetSessionCdpClient.sendCommand(
'Runtime.runIfWaitingForDebugger'
);
await parentSessionCdpClient.sendCommand(
'Target.detachFromTarget',
params
);
return;
}

Expand Down Expand Up @@ -179,7 +184,7 @@ export class BrowsingContextProcessor {
}
}

const result = await browserCdpClient.Target.createTarget({
const result = await browserCdpClient.sendCommand('Target.createTarget', {
url: 'about:blank',
newWindow: params.type === 'window',
...(referenceContext?.cdpBrowserContextId
Expand Down Expand Up @@ -294,19 +299,21 @@ export class BrowsingContextProcessor {
eventParams: Protocol.Target.DetachedFromTargetEvent
) => {
if (eventParams.targetId === commandParams.context) {
browserCdpClient.Target.removeListener(
'detachedFromTarget',
browserCdpClient.removeListener(
'Target.detachedFromTarget',
onContextDestroyed
);
resolve();
}
};
browserCdpClient.Target.on('detachedFromTarget', onContextDestroyed);
browserCdpClient.on('Target.detachedFromTarget', onContextDestroyed);
});

await this.#cdpConnection.browserClient().Target.closeTarget({
targetId: commandParams.context,
});
await this.#cdpConnection
.browserClient()
.sendCommand('Target.closeTarget', {
targetId: commandParams.context,
});

// Sometimes CDP command finishes before `detachedFromTarget` event,
// sometimes after. Wait for the CDP command to be finished, and then wait
Expand Down
8 changes: 4 additions & 4 deletions src/bidiMapper/domains/log/logManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export class LogManager {
}

#initializeLogEntryAddedEventListener() {
this.#cdpClient.Runtime.on(
'consoleAPICalled',
this.#cdpClient.on(
'Runtime.consoleAPICalled',
(params: Protocol.Runtime.ConsoleAPICalledEvent) => {
// Try to find realm by `cdpSessionId` and `executionContextId`,
// if provided.
Expand Down Expand Up @@ -102,8 +102,8 @@ export class LogManager {
}
);

this.#cdpClient.Runtime.on(
'exceptionThrown',
this.#cdpClient.on(
'Runtime.exceptionThrown',
(params: Protocol.Runtime.ExceptionThrownEvent) => {
// Try to find realm by `cdpSessionId` and `executionContextId`,
// if provided.
Expand Down
Loading

0 comments on commit bebb0a8

Please sign in to comment.