Add Brave CDP automation, replace Oracle browser mode

Connects to user's running Brave via Chrome DevTools Protocol
to automate ChatGPT interaction. Uses puppeteer-core to open a
tab, send the prompt, wait for response, and extract the result.

No cookies, no separate profiles, no copy/paste. Just connects
to the browser where the user is already logged in.

One-time setup: relaunch Brave with --remote-debugging-port=9222

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Taylor Eernisse
2026-02-07 16:16:41 -05:00
parent d776a266a8
commit e7882b917b
4163 changed files with 782828 additions and 148 deletions

View File

@@ -0,0 +1,23 @@
import { type Process } from '@puppeteer/browsers';
import { MapperServerCdpConnection } from './MapperCdpConnection.js';
import type { SimpleTransport } from './SimpleTransport.js';
export interface ChromeOptions {
chromeArgs: string[];
chromeBinary?: string;
}
/**
* BrowserProcess is responsible for running the browser and BiDi Mapper within
* it.
* 1. Launch Chromium (using Puppeteer for now).
* 2. Get `BiDi-CDP` mapper JS binaries using `MapperReader`.
* 3. Run `BiDi-CDP` mapper in launched browser using `MapperRunner`.
* 4. Bind `BiDi-CDP` mapper to the `BiDi server` to forward messages from BiDi
* Mapper to the client.
*/
export declare class BrowserInstance {
#private;
static run(chromeOptions: ChromeOptions, verbose: boolean): Promise<BrowserInstance>;
constructor(mapperCdpConnection: MapperServerCdpConnection, browserProcess: Process);
close(): Promise<void>;
bidiSession(): SimpleTransport;
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { mkdtemp } from 'fs/promises';
import os from 'os';
import path from 'path';
import { launch } from '@puppeteer/browsers';
import debug from 'debug';
import { MapperCdpConnection } from '../cdp/CdpConnection.js';
import { MapperServerCdpConnection } from './MapperCdpConnection.js';
import { PipeTransport } from './PipeTransport.js';
import { getMapperTabSource } from './reader.js';
const debugInternal = debug('bidi:mapper:internal');
/**
* BrowserProcess is responsible for running the browser and BiDi Mapper within
* it.
* 1. Launch Chromium (using Puppeteer for now).
* 2. Get `BiDi-CDP` mapper JS binaries using `MapperReader`.
* 3. Run `BiDi-CDP` mapper in launched browser using `MapperRunner`.
* 4. Bind `BiDi-CDP` mapper to the `BiDi server` to forward messages from BiDi
* Mapper to the client.
*/
export class BrowserInstance {
#mapperCdpConnection;
#browserProcess;
static async run(chromeOptions, verbose) {
const profileDir = await mkdtemp(path.join(os.tmpdir(), 'web-driver-bidi-server-'));
// See https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md
const chromeArguments = [
// keep-sorted start
'--allow-browser-signin=false',
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-component-update',
'--disable-default-apps',
'--disable-notifications',
'--disable-popup-blocking',
'--disable-search-engine-choice-screen',
'--enable-automation',
'--no-default-browser-check',
'--no-first-run',
'--password-store=basic',
'--remote-debugging-pipe',
'--use-mock-keychain',
`--user-data-dir=${profileDir}`,
// keep-sorted end
...chromeOptions.chromeArgs,
'about:blank',
];
const executablePath = chromeOptions.chromeBinary ?? process.env['BROWSER_BIN'];
if (!executablePath) {
throw new Error('Could not find Chrome binary');
}
const launchArguments = {
executablePath,
args: chromeArguments,
env: process.env,
pipe: true,
};
debugInternal(`Launching browser`, {
executablePath,
args: chromeArguments,
});
const browserProcess = launch(launchArguments);
const cdpConnection = this.#establishPipeConnection(browserProcess);
// 2. Get `BiDi-CDP` mapper JS binaries.
const mapperTabSource = await getMapperTabSource();
// 3. Run `BiDi-CDP` mapper in launched browser using `MapperRunner`.
const mapperCdpConnection = await MapperServerCdpConnection.create(cdpConnection, mapperTabSource, verbose);
return new BrowserInstance(mapperCdpConnection, browserProcess);
}
constructor(mapperCdpConnection, browserProcess) {
this.#mapperCdpConnection = mapperCdpConnection;
this.#browserProcess = browserProcess;
}
async close() {
// Close the mapper tab.
this.#mapperCdpConnection.close();
// Close browser.
await this.#browserProcess.close();
}
bidiSession() {
return this.#mapperCdpConnection.bidiSession();
}
static #establishPipeConnection(browserProcess) {
debugInternal('Establishing pipe connection to browser process with pid: ', browserProcess.nodeProcess.pid);
const { 3: pipeWrite, 4: pipeRead } = browserProcess.nodeProcess.stdio;
const transport = new PipeTransport(pipeWrite, pipeRead);
return new MapperCdpConnection(transport);
}
}
//# sourceMappingURL=BrowserInstance.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"BrowserInstance.js","sourceRoot":"","sources":["../../../src/bidiServer/BrowserInstance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAC,OAAO,EAAC,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAC,MAAM,EAAe,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAC,mBAAmB,EAAC,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EAAC,yBAAyB,EAAC,MAAM,0BAA0B,CAAC;AACnE,OAAO,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAC,kBAAkB,EAAC,MAAM,aAAa,CAAC;AAG/C,MAAM,aAAa,GAAG,KAAK,CAAC,sBAAsB,CAAC,CAAC;AAOpD;;;;;;;;GAQG;AACH,MAAM,OAAO,eAAe;IAC1B,oBAAoB,CAA4B;IAChD,eAAe,CAAU;IAEzB,MAAM,CAAC,KAAK,CAAC,GAAG,CACd,aAA4B,EAC5B,OAAgB;QAEhB,MAAM,UAAU,GAAG,MAAM,OAAO,CAC9B,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,M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EvB,IAAI,CAAC,oBAAoB,GAAG,mBAAmB,CAAC;QAChD,IAAI,CAAC,eAAe,GAAG,cAAc,CAAC;IACxC,CAAC;IAED,KAAK,CAAC,KAAK;QACT,wBAAwB;QACxB,IAAI,CAAC,oBAAoB,CAAC,KAAK,EAAE,CAAC;QAElC,iBAAiB;QACjB,MAAM,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;IACrC,CAAC;IAED,WAAW;QACT,OAAO,IAAI,CAAC,oBAAoB,CAAC,WAAW,EAAE,CAAC;IACjD,CAAC;IAED,MAAM,CAAC,wBAAwB,CAC7B,cAAuB;QAEvB,aAAa,CACX,4DAA4D,EAC5D,cAAc,CAAC,WAAW,CAAC,GAAG,CAC/B,CAAC;QACF,MAAM,EAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAC,GAAG,cAAc,CAAC,WAAW,CAAC,KAAK,CAAC;QACrE,MAAM,SAAS,GAAG,IAAI,aAAa,CACjC,SAAkC,EAClC,QAAiC,CAClC,CAAC;QACF,OAAO,IAAI,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAC5C,CAAC;CACF"}

View File

@@ -0,0 +1,25 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { MapperCdpConnection } from '../cdp/CdpConnection.js';
import { SimpleTransport } from './SimpleTransport.js';
export declare class MapperServerCdpConnection {
#private;
static create(cdpConnection: MapperCdpConnection, mapperTabSource: string, verbose: boolean): Promise<MapperServerCdpConnection>;
private constructor();
close(): void;
bidiSession(): SimpleTransport;
}

View File

@@ -0,0 +1,138 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import debug from 'debug';
import { SimpleTransport } from './SimpleTransport.js';
const debugInternal = debug('bidi:mapper:internal');
const debugInfo = debug('bidi:mapper:info');
const debugOthers = debug('bidi:mapper:debug:others');
// Memorizes a debug creation
const loggers = new Map();
const getLogger = (type) => {
const prefix = `bidi:mapper:${type}`;
let logger = loggers.get(prefix);
if (!logger) {
logger = debug(prefix);
loggers.set(prefix, logger);
}
return logger;
};
export class MapperServerCdpConnection {
#cdpConnection;
#bidiSession;
static async create(cdpConnection, mapperTabSource, verbose) {
try {
const bidiSession = await this.#initMapper(cdpConnection, mapperTabSource, verbose);
return new MapperServerCdpConnection(cdpConnection, bidiSession);
}
catch (e) {
cdpConnection.close();
throw e;
}
}
constructor(cdpConnection, bidiSession) {
this.#cdpConnection = cdpConnection;
this.#bidiSession = bidiSession;
}
static async #sendMessage(mapperCdpClient, message) {
try {
await mapperCdpClient.sendCommand('Runtime.evaluate', {
expression: `onBidiMessage(${JSON.stringify(message)})`,
});
}
catch (error) {
debugInternal('Call to onBidiMessage failed', error);
}
}
close() {
this.#cdpConnection.close();
}
bidiSession() {
return this.#bidiSession;
}
static #onBindingCalled = (params, bidiSession) => {
if (params.name === 'sendBidiResponse') {
bidiSession.emit('message', params.payload);
}
else if (params.name === 'sendDebugMessage') {
this.#onDebugMessage(params.payload);
}
};
static #onDebugMessage = (json) => {
try {
const log = JSON.parse(json);
if (log.logType !== undefined && log.messages !== undefined) {
const logger = getLogger(log.logType);
logger(log.messages);
}
}
catch {
// Fall back to raw log in case of unknown
debugOthers(json);
}
};
static #onConsoleAPICalled = (params) => {
debugInfo('consoleAPICalled: %s %O', params.type, params.args.map((arg) => arg.value));
};
static #onRuntimeExceptionThrown = (params) => {
debugInfo('exceptionThrown:', params);
};
static async #initMapper(cdpConnection, mapperTabSource, verbose) {
debugInternal('Initializing Mapper.');
const browserClient = await cdpConnection.createBrowserSession();
const { targetId: mapperTargetId } = await browserClient.sendCommand('Target.createTarget', {
url: 'about:blank#MAPPER_TARGET',
hidden: true,
background: true,
});
const { sessionId: mapperSessionId } = await browserClient.sendCommand('Target.attachToTarget', { targetId: mapperTargetId, flatten: true });
const mapperCdpClient = cdpConnection.getCdpClient(mapperSessionId);
const bidiSession = new SimpleTransport(async (message) => await this.#sendMessage(mapperCdpClient, message));
// Process responses from the mapper tab.
mapperCdpClient.on('Runtime.bindingCalled', (params) => this.#onBindingCalled(params, bidiSession));
// Forward console messages from the mapper tab.
mapperCdpClient.on('Runtime.consoleAPICalled', this.#onConsoleAPICalled);
// Catch unhandled exceptions in the mapper.
mapperCdpClient.on('Runtime.exceptionThrown', this.#onRuntimeExceptionThrown);
await mapperCdpClient.sendCommand('Runtime.enable');
await browserClient.sendCommand('Target.exposeDevToolsProtocol', {
bindingName: 'cdp',
targetId: mapperTargetId,
inheritPermissions: true,
});
await mapperCdpClient.sendCommand('Runtime.addBinding', {
name: 'sendBidiResponse',
});
if (verbose) {
// Needed to request verbose logs from Mapper.
await mapperCdpClient.sendCommand('Runtime.addBinding', {
name: 'sendDebugMessage',
});
}
// Evaluate Mapper Tab sources in the tab.
await mapperCdpClient.sendCommand('Runtime.evaluate', {
expression: mapperTabSource,
});
// TODO: handle errors in all these evaluate calls!
await mapperCdpClient.sendCommand('Runtime.evaluate', {
expression: `window.runMapperInstance('${mapperTargetId}')`,
awaitPromise: true,
});
debugInternal('Mapper is launched!');
return bidiSession;
}
}
//# sourceMappingURL=MapperCdpConnection.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"MapperCdpConnection.js","sourceRoot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CAAC,CACrE,CAAC;QAEF,yCAAyC;QACzC,eAAe,CAAC,EAAE,CAAC,uBAAuB,EAAE,CAAC,MAAM,EAAE,EAAE,CACrD,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,WAAW,CAAC,CAC3C,CAAC;QACF,gDAAgD;QAChD,eAAe,CAAC,EAAE,CAAC,0BAA0B,EAAE,IAAI,CAAC,mBAAmB,CAAC,CAAC;QACzE,4CAA4C;QAC5C,eAAe,CAAC,EAAE,CAChB,yBAAyB,EACzB,IAAI,CAAC,yBAAyB,CAC/B,CAAC;QAEF,MAAM,eAAe,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC;QAEpD,MAAM,aAAa,CAAC,WAAW,CAAC,+BAA+B,EAAE;YAC/D,WAAW,EAAE,KAAK;YAClB,QAAQ,EAAE,cAAc;YACxB,kBAAkB,EAAE,IAAI;SACzB,CAAC,CAAC;QAEH,MAAM,eAAe,CAAC,WAAW,CAAC,oBAAoB,EAAE;YACtD,IAAI,EAAE,kBAAkB;SACzB,CAAC,CAAC;QAEH,IAAI,OAAO,EAAE,CAAC;YACZ,8CAA8C;YAC9C,MAAM,eAAe,CAAC,WAAW,CAAC,oBAAoB,EAAE;gBACtD,IAAI,EAAE,kBAAkB;aACzB,CAAC,CAAC;QACL,CAAC;QAED,0CAA0C;QAC1C,MAAM,eAAe,CAAC,WAAW,CAAC,kBAAkB,EAAE;YACpD,UAAU,EAAE,eAAe;SAC5B,CAAC,CAAC;QAEH,mDAAmD;QACnD,MAAM,eAAe,CAAC,WAAW,CAAC,kBAAkB,EAAE;YACpD,UAAU,EAAE,6BAA6B,cAAc,IAAI;YAC3D,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;QAEH,aAAa,CAAC,qBAAqB,CAAC,CAAC;QACrC,OAAO,WAAW,CAAC;IACrB,CAAC"}

View File

@@ -0,0 +1,8 @@
import type { Transport } from '../utils/transport.js';
export declare class PipeTransport implements Transport {
#private;
constructor(pipeWrite: NodeJS.WritableStream, pipeRead: NodeJS.ReadableStream);
setOnMessage(onMessage: (message: string) => void): void;
sendMessage(message: string): void;
close(): void;
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2025 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import debug from 'debug';
const debugInternal = debug('bidi:server:pipeTranspot');
export class PipeTransport {
#pipeWrite;
#onMessage = null;
#pendingMessage = '';
constructor(pipeWrite, pipeRead) {
this.#pipeWrite = pipeWrite;
pipeRead.on('data', (chunk) => {
return this.#dispatch(chunk);
});
pipeRead.on('close', () => {
this.close();
});
pipeRead.on('error', (error) => {
debugInternal('Pipe read error: ', error);
this.close();
});
pipeWrite.on('error', (error) => {
debugInternal('Pipe read error: ', error);
this.close();
});
}
setOnMessage(onMessage) {
this.#onMessage = onMessage;
}
sendMessage(message) {
// TODO: WebSocketServer keeps sending messages after closing the transport.
// TODO: we should assert that the pipe was not closed.
this.#pipeWrite.write(message);
this.#pipeWrite.write('\0');
}
#dispatch(buffer) {
// TODO: WebSocketServer keeps sending messages after closing the transport.
// TODO: we should assert that the pipe was not closed.
let end = buffer.indexOf('\0');
if (end === -1) {
this.#pendingMessage += buffer.toString();
return;
}
const message = this.#pendingMessage + buffer.toString(undefined, 0, end);
if (this.#onMessage) {
this.#onMessage.call(null, message);
}
let start = end + 1;
end = buffer.indexOf('\0', start);
while (end !== -1) {
if (this.#onMessage) {
this.#onMessage.call(null, buffer.toString(undefined, start, end));
}
start = end + 1;
end = buffer.indexOf('\0', start);
}
this.#pendingMessage = buffer.toString(undefined, start);
}
close() {
debugInternal('Closing pipe');
}
}
//# sourceMappingURL=PipeTransport.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"PipeTransport.js","sourceRoot":"","sources":["../../../src/bidiServer/PipeTransport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,MAAM,aAAa,GAAG,KAAK,CAAC,0BAA0B,CAAC,CAAC;AAExD,MAAM,OAAO,aAAa;IACxB,UAAU,CAAwB;IAClC,UAAU,GAAuC,IAAI,CAAC;IAEtD,eAAe,GAAG,EAAE,CAAC;IAErB,YACE,SAAgC,EAChC,QAA+B;QAE/B,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAE5B,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YAC5B,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QACH,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;YACxB,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;QACH,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YAC7B,aAAa,CAAC,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAC1C,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;QACH,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YAC9B,aAAa,CAAC,mBAAmB,EAAE,KAAK,CAAC,CAAC;YAC1C,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC,CAAC,CAAC;IACL,CAAC;IAED,YAAY,CAAC,SAAoC;QAC/C,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IACD,WAAW,CAAC,OAAe;QACzB,4EAA4E;QAC5E,uDAAuD;QAEvD,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,C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+1,13 @@
import { EventEmitter } from '../utils/EventEmitter.js';
/**
* Implements simple transport that allows sending string messages via
* `sendCommand` and receiving them via `on('message')`.
*/
export declare class SimpleTransport extends EventEmitter<Record<'message', string>> {
#private;
/**
* @param sendCommandDelegate delegate to be called in `sendCommand`.
*/
constructor(sendCommandDelegate: (plainCommand: string) => Promise<void>);
sendCommand(plainCommand: string): Promise<void>;
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EventEmitter } from '../utils/EventEmitter.js';
/**
* Implements simple transport that allows sending string messages via
* `sendCommand` and receiving them via `on('message')`.
*/
export class SimpleTransport extends EventEmitter {
#sendCommandDelegate;
/**
* @param sendCommandDelegate delegate to be called in `sendCommand`.
*/
constructor(sendCommandDelegate) {
super();
this.#sendCommandDelegate = sendCommandDelegate;
}
async sendCommand(plainCommand) {
await this.#sendCommandDelegate(plainCommand);
}
}
//# sourceMappingURL=SimpleTransport.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"SimpleTransport.js","sourceRoot":"","sources":["../../../src/bidiServer/SimpleTransport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAC,YAAY,EAAC,MAAM,0BAA0B,CAAC;AAEtD;;;GAGG;AACH,MAAM,OAAO,eAAgB,SAAQ,YAAuC;IACjE,oBAAoB,CAA0C;IAEvE;;OAEG;IACH,YAAY,mBAA4D;QACtE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,oBAAoB,GAAG,mBAAmB,CAAC;IAClD,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,YAAoB;QACpC,MAAM,IAAI,CAAC,oBAAoB,CAAC,YAAY,CAAC,CAAC;IAChD,CAAC;CACF"}

View File

@@ -0,0 +1,6 @@
import debug from 'debug';
export declare const debugInfo: debug.Debugger;
export declare class WebSocketServer {
#private;
constructor(port: number, verbose: boolean);
}

View File

@@ -0,0 +1,374 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import http from 'http';
import debug from 'debug';
import * as websocket from 'websocket';
import { Deferred } from '../utils/Deferred.js';
import { uuidv4 } from '../utils/uuid.js';
import { BrowserInstance } from './BrowserInstance.js';
export const debugInfo = debug('bidi:server:info');
const debugInternal = debug('bidi:server:internal');
const debugSend = debug('bidi:server:SEND ▸');
const debugRecv = debug('bidi:server:RECV ◂');
export class WebSocketServer {
#sessions = new Map();
#port;
#verbose;
#server;
#wsServer;
constructor(port, verbose) {
this.#port = port;
this.#verbose = verbose;
this.#server = http.createServer((request, response) => {
return this.#onRequest(request, response).catch((e) => {
debugInfo('Error while processing request', e);
response.writeHead(500, String(e));
});
});
this.#wsServer = new websocket.server({
httpServer: this.#server,
autoAcceptConnections: false,
});
this.#wsServer.on('request', this.#onWsRequest.bind(this));
void this.#listen();
}
#logServerStarted() {
debugInfo('BiDi server is listening on port', this.#port);
debugInfo('BiDi server was started successfully.');
}
async #listen() {
try {
this.#server.listen(this.#port, () => {
this.#logServerStarted();
});
}
catch (error) {
if (error &&
typeof error === 'object' &&
'code' in error &&
error.code === 'EADDRINUSE') {
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
debugInfo('Retrying to run BiDi server');
this.#server.listen(this.#port, () => {
this.#logServerStarted();
});
}
throw error;
}
}
async #onRequest(request, response) {
debugInternal(`Received HTTP ${JSON.stringify(request.method)} request for ${JSON.stringify(request.url)}`);
if (!request.url) {
throw new Error('Request URL is empty.');
}
// https://w3c.github.io/webdriver-bidi/#transport, step 2.
if (request.url === '/session') {
const body = await new Promise((resolve, reject) => {
const bodyArray = [];
request.on('data', (chunk) => {
bodyArray.push(chunk);
});
request.on('error', reject);
request.on('end', () => {
resolve(Buffer.concat(bodyArray));
});
});
debugInternal(`Creating session by HTTP request ${body.toString()}`);
// https://w3c.github.io/webdriver-bidi/#transport, step 3.
const jsonBody = JSON.parse(body.toString());
response.writeHead(200, {
'Content-Type': 'application/json;charset=utf-8',
'Cache-Control': 'no-cache',
});
const sessionId = uuidv4();
const session = {
sessionId,
// TODO: launch browser instance and set it to the session after WPT
// tests clean up is switched to pure BiDi.
browserInstancePromise: undefined,
sessionOptions: {
chromeOptions: this.#getChromeOptions(jsonBody.capabilities),
verbose: this.#verbose,
sessionNewBody: `{"id":0,"method":"session.new","params":${body.toString()}}`,
},
};
this.#sessions.set(sessionId, session);
const webSocketUrl = `ws://localhost:${this.#port}/session/${sessionId}`;
debugInternal(`Session created. WebSocket URL: ${JSON.stringify(webSocketUrl)}.`);
response.write(JSON.stringify({
value: {
sessionId,
capabilities: {
webSocketUrl,
},
},
}));
return response.end();
}
else if (request.url.startsWith('/session')) {
debugInternal(`Unknown session command ${request.method ?? 'UNKNOWN METHOD'} request for ${request.url} with payload ${await this.#getHttpRequestPayload(request)}. 200 returned.`);
response.writeHead(200, {
'Content-Type': 'application/json;charset=utf-8',
'Cache-Control': 'no-cache',
});
response.write(JSON.stringify({
value: {},
}));
return response.end();
}
throw new Error(`Unknown "${request.method}" request for "${JSON.stringify(request.url)}" with payload "${await this.#getHttpRequestPayload(request)}".`);
}
#onWsRequest(request) {
// Session is set either by Classic or BiDi commands.
let session;
// Request to `/session` should be treated as a new session request.
let requestSessionId = '';
if ((request.resource ?? '').startsWith(`/session/`)) {
requestSessionId = (request.resource ?? '').split('/').pop() ?? '';
}
debugInternal(`new WS request received. Path: ${JSON.stringify(request.resourceURL.path)}, sessionId: ${JSON.stringify(requestSessionId)}`);
if (requestSessionId !== '' &&
requestSessionId !== undefined &&
!this.#sessions.has(requestSessionId)) {
debugInternal('Unknown session id:', requestSessionId);
request.reject();
return;
}
const connection = request.accept();
session = this.#sessions.get(requestSessionId ?? '');
if (session !== undefined) {
// BrowserInstance is created for each new WS connection, even for the
// same SessionId. This is because WPT uses a single session for all the
// tests, but cleans up tests using WebDriver Classic commands, which is
// not implemented in this Mapper runner.
// TODO: connect to an existing BrowserInstance instead.
const sessionOptions = session.sessionOptions;
session.browserInstancePromise = this.#closeBrowserInstanceIfLaunched(session)
.then(async () => await this.#launchBrowserInstance(connection, sessionOptions))
.catch((e) => {
debugInfo('Error while creating session', e);
connection.close(500, 'cannot create browser instance');
throw e;
});
}
connection.on('message', async (message) => {
// If type is not text, return error.
if (message.type !== 'utf8') {
this.#respondWithError(connection, {}, "invalid argument" /* ErrorCode.InvalidArgument */, `not supported type (${message.type})`);
return;
}
const plainCommandData = message.utf8Data;
if (debugRecv.enabled) {
try {
debugRecv(JSON.parse(plainCommandData));
}
catch {
debugRecv(plainCommandData);
}
}
// Try to parse the message to handle some of BiDi commands.
let parsedCommandData;
try {
parsedCommandData = JSON.parse(plainCommandData);
}
catch (error) {
this.#respondWithError(connection, {}, "invalid argument" /* ErrorCode.InvalidArgument */, `unable to parse BiDi command: ${error}`);
return;
}
// Handle creating new session.
if (parsedCommandData.method === 'session.new') {
if (session !== undefined) {
debugInfo('WS connection already have an associated session.');
this.#respondWithError(connection, plainCommandData, "session not created" /* ErrorCode.SessionNotCreated */, 'WS connection already have an associated session.');
return;
}
try {
const sessionOptions = {
chromeOptions: this.#getChromeOptions(parsedCommandData.params?.capabilities),
verbose: this.#verbose,
sessionNewBody: plainCommandData,
};
const browserInstance = await this.#launchBrowserInstance(connection, sessionOptions, true);
const sessionId = uuidv4();
session = {
sessionId,
browserInstancePromise: Promise.resolve(browserInstance),
sessionOptions,
};
this.#sessions.set(sessionId, session);
}
catch (e) {
debugInfo('Error while creating session', e);
this.#respondWithError(connection, plainCommandData, "session not created" /* ErrorCode.SessionNotCreated */, e?.message ?? 'Unknown error');
return;
}
return;
}
// Handle ending session. Close browser if open, remove session.
if (parsedCommandData.method === 'session.end') {
if (session === undefined) {
debugInfo('WS connection does not have an associated session.');
this.#respondWithError(connection, plainCommandData, "session not created" /* ErrorCode.SessionNotCreated */, 'WS connection does not have an associated session.');
return;
}
try {
await this.#closeBrowserInstanceIfLaunched(session);
this.#sessions.delete(session.sessionId);
}
catch (e) {
debugInfo('Error while closing session', e);
this.#respondWithError(connection, plainCommandData, "unknown error" /* ErrorCode.UnknownError */, `Session cannot be closed. Error: ${e?.message}`);
return;
}
this.#sendClientMessage({
id: parsedCommandData.id,
type: 'success',
result: {},
}, connection);
return;
}
if (session === undefined) {
debugInfo('Session is not yet initialized.');
this.#respondWithError(connection, plainCommandData, "invalid session id" /* ErrorCode.InvalidSessionId */, 'Session is not yet initialized.');
return;
}
if (session.browserInstancePromise === undefined) {
debugInfo('Browser instance is not launched.');
this.#respondWithError(connection, plainCommandData, "invalid session id" /* ErrorCode.InvalidSessionId */, 'Browser instance is not launched.');
return;
}
const browserInstance = await session.browserInstancePromise;
// Handle `browser.close` command.
if (parsedCommandData.method === 'browser.close') {
await browserInstance.close();
this.#sendClientMessage({
id: parsedCommandData.id,
type: 'success',
result: {},
}, connection);
return;
}
// Forward all other commands to BiDi Mapper.
await browserInstance.bidiSession().sendCommand(plainCommandData);
});
connection.on('close', async () => {
debugInternal(`Peer ${connection.remoteAddress} disconnected.`);
// TODO: don't close Browser instance to allow re-connecting to the session.
await this.#closeBrowserInstanceIfLaunched(session);
});
}
async #closeBrowserInstanceIfLaunched(session) {
if (session === undefined || session.browserInstancePromise === undefined) {
return;
}
const browserInstance = await session.browserInstancePromise;
session.browserInstancePromise = undefined;
void browserInstance.close();
}
#getChromeOptions(capabilities) {
const chromeCapabilities = capabilities?.alwaysMatch?.['goog:chromeOptions'];
return {
chromeArgs: chromeCapabilities?.args ?? [],
chromeBinary: chromeCapabilities?.binary ?? undefined,
};
}
async #launchBrowserInstance(connection, sessionOptions, passSessionNewThrough = false) {
debugInfo('Scheduling browser launch...');
const browserInstance = await BrowserInstance.run(sessionOptions.chromeOptions, sessionOptions.verbose);
const body = JSON.parse(sessionOptions.sessionNewBody);
const id = body.id;
const sessionCreated = new Deferred();
const sessionResponseListener = (message) => {
const jsonMessage = JSON.parse(message);
if (jsonMessage['id'] === id) {
debugInfo('Receiving session.new response from mapper', message);
sessionCreated.resolve();
if (passSessionNewThrough) {
this.#sendClientMessageString(message, connection);
}
}
};
browserInstance.bidiSession().on('message', sessionResponseListener);
debugInfo('Sending session.new to mapper', sessionOptions.sessionNewBody);
await browserInstance
.bidiSession()
.sendCommand(sessionOptions.sessionNewBody);
await sessionCreated;
browserInstance.bidiSession().off('message', sessionResponseListener);
// Forward messages from BiDi Mapper to the client unconditionally.
browserInstance.bidiSession().on('message', (message) => {
this.#sendClientMessageString(message, connection);
});
debugInfo('Browser is launched!');
return browserInstance;
}
#sendClientMessageString(message, connection) {
if (debugSend.enabled) {
try {
debugSend(JSON.parse(message));
}
catch {
debugSend(message);
}
}
connection.sendUTF(message);
}
#sendClientMessage(object, connection) {
const json = JSON.stringify(object);
return this.#sendClientMessageString(json, connection);
}
#respondWithError(connection, plainCommandData, errorCode, errorMessage) {
const errorResponse = this.#getErrorResponse(plainCommandData, errorCode, errorMessage);
void this.#sendClientMessage(errorResponse, connection);
}
#getErrorResponse(plainCommandData, errorCode, errorMessage) {
// XXX: this is bizarre per spec. We reparse the payload and
// extract the ID, regardless of what kind of value it was.
let commandId;
try {
const commandData = JSON.parse(plainCommandData);
if ('id' in commandData) {
commandId = commandData.id;
}
}
catch { }
return {
type: 'error',
id: commandId,
error: errorCode,
message: errorMessage,
// XXX: optional stacktrace field.
};
}
#getHttpRequestPayload(request) {
return new Promise((resolve, reject) => {
let data = '';
request.on('data', (chunk) => {
data += chunk;
});
request.on('end', () => {
resolve(data);
});
request.on('error', (error) => {
reject(error);
});
});
}
}
//# sourceMappingURL=WebSocketServer.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,23 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export declare function parseCommandLineArgs(): {
[x: string]: unknown;
port: number;
verbose: boolean;
_: (string | number)[];
$0: string;
};

49
node_modules/chromium-bidi/lib/esm/bidiServer/index.js generated vendored Normal file
View File

@@ -0,0 +1,49 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { debugInfo, WebSocketServer } from './WebSocketServer.js';
export function parseCommandLineArgs() {
return yargs(hideBin(process.argv))
.usage(`$0`, `[PORT=8080] [VERBOSE=8080]`)
.option('port', {
alias: 'p',
describe: 'Port that BiDi server should listen to. Default is 8080.',
type: 'number',
default: process.env['PORT'] ? Number(process.env['PORT']) : 8080,
})
.option('verbose', {
alias: 'v',
describe: 'If present, the Mapper debug log, including CDP commands and events will be logged into the server output.',
type: 'boolean',
default: process.env['VERBOSE'] === 'true' || false,
})
.parseSync();
}
(() => {
try {
const argv = parseCommandLineArgs();
const { port, verbose } = argv;
debugInfo('Launching BiDi server...');
new WebSocketServer(port, verbose);
debugInfo('BiDi server launched');
}
catch (e) {
debugInfo('Error launching BiDi server', e);
}
})();
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/bidiServer/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAC,SAAS,EAAE,eAAe,EAAC,MAAM,sBAAsB,CAAC;AAEhE,MAAM,UAAU,oBAAoB;IAClC,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SAChC,KAAK,CAAC,IAAI,EAAE,4BAA4B,CAAC;SACzC,MAAM,CAAC,MAAM,EAAE;QACd,KAAK,EAAE,GAAG;QACV,QAAQ,EAAE,0DAA0D;QACpE,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI;KAClE,CAAC;SACD,MAAM,CAAC,SAAS,EAAE;QACjB,KAAK,EAAE,GAAG;QACV,QAAQ,EACN,4GAA4G;QAC9G,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,MAAM,IAAI,KAAK;KACpD,CAAC;SACD,SAAS,EAAE,CAAC;AACjB,CAAC;AAED,CAAC,GAAG,EAAE;IACJ,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,oBAAoB,EAAE,CAAC;QAEpC,MAAM,EAAC,IAAI,EAAE,OAAO,EAAC,GAAG,IAAI,CAAC;QAE7B,SAAS,CAAC,0BAA0B,CAAC,CAAC;QAEtC,IAAI,eAAe,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QACnC,SAAS,CAAC,sBAAsB,CAAC,CAAC;IACpC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,SAAS,CAAC,6BAA6B,EAAE,CAAC,CAAC,CAAC;IAC9C,CAAC;AACH,CAAC,CAAC,EAAE,CAAC"}

View File

@@ -0,0 +1,17 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export declare function getMapperTabSource(): Promise<string>;

View File

@@ -0,0 +1,25 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs/promises';
import path from 'path';
import url from 'url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
export async function getMapperTabSource() {
return await fs.readFile(path.join(__dirname, '../../iife/mapperTab.js'), 'utf8');
}
//
//# sourceMappingURL=reader.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"reader.js","sourceRoot":"","sources":["../../../src/bidiServer/reader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,SAAS,GAAG,GAAG,CAAC,aAAa,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAEnE,MAAM,CAAC,KAAK,UAAU,kBAAkB;IACtC,OAAO,MAAM,EAAE,CAAC,QAAQ,CACtB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,yBAAyB,CAAC,EAC/C,MAAM,CACP,CAAC;AACJ,CAAC;AACD,EAAE"}