Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"@vitejs/plugin-react": "6.0.3",
"@zip.js/zip.js": "2.7.73",
"chokidar": "4.0.3",
"chromium-bidi": "12.1.0",
"chromium-bidi": "159.0.8132-0",
"colors": "1.4.0",
"commander": "15.0.0",
"cross-env": "10.1.0",
Expand Down
108 changes: 99 additions & 9 deletions packages/playwright-core/src/server/bidi/bidiOverCdp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,113 @@
import { debugLogger } from '@utils/debugLogger';

import type { ConnectionTransport, ProtocolRequest, ProtocolResponse } from '../transport';
import type * as bidiMapper from 'chromium-bidi/lib/cjs/bidiMapper/BidiMapper';
import type * as bidiCdpConnection from 'chromium-bidi/lib/cjs/cdp/CdpConnection';
import type { ChromiumBidi } from 'chromium-bidi/lib/cjs/protocol/protocol';
import type * as bidiTransport from 'chromium-bidi/lib/cjs/utils/transport';
import type * as bidiMapper from 'chromium-bidi/out/Default/gen/src/bidiMapper/BidiMapper.js';
import type * as bidiCdpConnection from 'chromium-bidi/out/Default/gen/src/cdp/CdpConnection.js';
import type { ChromiumBidi } from 'chromium-bidi/out/Default/gen/src/protocol/protocol.js';
import type * as bidiLog from 'chromium-bidi/out/Default/gen/src/utils/log.js';
import type * as bidiTransport from 'chromium-bidi/out/Default/gen/src/utils/transport.js';

const bidiServerLogger = (prefix: string, ...args: unknown[]): void => {
debugLogger.log(prefix as any, args);
const bidiServerLogger: bidiLog.LoggerFn = prefix => {
if (!debugLogger.isEnabled(prefix as any))
return undefined;
return (...args: unknown[]): void => {
debugLogger.log(prefix as any, args);
};
};

// Node < 24 does not expose DisposableStack without --harmony-explicit-resource-management.
function ensureDisposableStackPolyfill() {
if ('DisposableStack' in globalThis)
return;
const disposeSymbol: symbol = (Symbol as any).dispose ??= Symbol.for('Symbol.dispose');
class DisposableStackPolyfill {
private _disposed = false;
private _stack: (() => void)[] = [];

get disposed() {
return this._disposed;
}

defer(onDispose: () => void): void {
if (this._disposed)
throw new ReferenceError('DisposableStack is already disposed');
if (typeof onDispose !== 'function')
throw new TypeError('onDispose is not a function');
this._stack.push(onDispose);
}

use<T>(value: T): T {
if (this._disposed)
throw new ReferenceError('DisposableStack is already disposed');
if (value !== null && value !== undefined) {
const method = (value as any)[disposeSymbol];
if (typeof method !== 'function')
throw new TypeError('Value is not disposable');
this._stack.push(() => method.call(value));
}
return value;
}

adopt<T>(value: T, onDispose: (value: T) => void): T {
if (this._disposed)
throw new ReferenceError('DisposableStack is already disposed');
if (typeof onDispose !== 'function')
throw new TypeError('onDispose is not a function');
this._stack.push(() => onDispose(value));
return value;
}

move(): DisposableStackPolyfill {
if (this._disposed)
throw new ReferenceError('DisposableStack is already disposed');
const next = new DisposableStackPolyfill();
next._stack = this._stack;
this._stack = [];
this._disposed = true;
return next;
}

dispose(): void {
if (this._disposed)
return;
this._disposed = true;
const stack = this._stack;
this._stack = [];
let hasError = false;
let error: unknown;
for (let i = stack.length - 1; i >= 0; i--) {
try {
stack[i]();
} catch (e) {
if (hasError) {
const SuppressedErr = (globalThis as any).SuppressedError;
error = SuppressedErr ? new SuppressedErr(e, error, 'An error was suppressed during disposal') : e;
} else {
hasError = true;
error = e;
}
}
}
if (hasError)
throw error;
}

[disposeSymbol](): void {
this.dispose();
}
}
(globalThis as any).DisposableStack = DisposableStackPolyfill;
}

export async function connectBidiOverCdp(cdp: ConnectionTransport): Promise<ConnectionTransport> {
ensureDisposableStackPolyfill();
// chromium-bidi is not in our dependencies. Bundlers do not fail on a
// missing module if its require() is inside a try block.
// missing module if its import() is inside a try block.
let bidiMapperModule: typeof bidiMapper;
let bidiCdpConnectionModule: typeof bidiCdpConnection;
try {
bidiMapperModule = require('chromium-bidi/lib/cjs/bidiMapper/BidiMapper');
bidiCdpConnectionModule = require('chromium-bidi/lib/cjs/cdp/CdpConnection');
bidiMapperModule = await import('chromium-bidi/out/Default/gen/src/bidiMapper/BidiMapper.js');
bidiCdpConnectionModule = await import('chromium-bidi/out/Default/gen/src/cdp/CdpConnection.js');
} catch (error) {
throw error;
}
Expand Down