-
Notifications
You must be signed in to change notification settings - Fork 74
feat: add bidi client #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KuznetsovRoman
wants to merge
1
commit into
master
Choose a base branch
from
TESTPLANE-1038.bidi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add bidi client #1275
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| import type { RawData } from "ws"; | ||
| import { inspect } from "node:util"; | ||
| import { debugBidi } from "./debug"; | ||
| import { | ||
| BIDIConnectionBreakError, | ||
| BIDIConnectionEstablishmentError, | ||
| BIDIConnectionTerminatedError, | ||
| BIDIConnectionTimeoutError, | ||
| BIDIRequestError, | ||
| BIDIRequestTimeoutError, | ||
| } from "./error"; | ||
| import { | ||
| BIDI_CONNECTION_RETRIES, | ||
| BIDI_CONNECTION_RETRY_BASE_DELAY, | ||
| BIDI_CONNECTION_TIMEOUT, | ||
| BIDI_REQUEST_RETRIES, | ||
| BIDI_REQUEST_RETRY_BASE_DELAY, | ||
| } from "./constants"; | ||
| import type { BiDiMessage, BiDiEvent, BiDiCommand } from "./types"; | ||
| import type { Browser } from "../types"; | ||
| import { WsConnection } from "../../ws-connection"; | ||
| import { WsError } from "../../ws-connection/error"; | ||
| import { exponentiallyWait } from "../../ws-connection/utils"; | ||
|
|
||
| type OnEventMessageFn = (bidiEventMessage: BiDiEvent) => unknown; | ||
|
|
||
| interface BiDiConnectionOptions { | ||
| sessionId: string; | ||
| headers?: Record<string, string>; | ||
| requestTimeout: number; | ||
| } | ||
|
|
||
| export class BIDIConnection { | ||
| public onEventMessage: OnEventMessageFn | null = null; | ||
| private readonly _sessionId: string; | ||
|
|
||
| private readonly _wsConnection: WsConnection<Record<string, unknown>, string>; | ||
|
|
||
| private constructor(bidiWsEndpoint: string, { headers, sessionId, requestTimeout }: BiDiConnectionOptions) { | ||
| this._sessionId = sessionId; | ||
|
|
||
| this._wsConnection = new WsConnection<Record<string, unknown>, string>(bidiWsEndpoint, { | ||
| headers, | ||
| debugFn: debugBidi, | ||
| retries: { | ||
| count: BIDI_CONNECTION_RETRIES, | ||
| baseDelay: BIDI_CONNECTION_RETRY_BASE_DELAY, | ||
| }, | ||
| timeouts: { | ||
| request: requestTimeout, | ||
| createSession: BIDI_CONNECTION_TIMEOUT, | ||
| }, | ||
| errors: { | ||
| ConnectionEstablishment: BIDIConnectionEstablishmentError, | ||
| ConnectionBreak: BIDIConnectionBreakError, | ||
| ConnectionTerminated: BIDIConnectionTerminatedError, | ||
| ConnectionTimeout: BIDIConnectionTimeoutError, | ||
| RequestTimeout: BIDIRequestError, | ||
| }, | ||
| onMessage: this._onMessage.bind(this), | ||
| }); | ||
| } | ||
|
|
||
| /** @description Creates BIDIConnection without establishing it */ | ||
| static create(browser: Browser): BIDIConnection | null { | ||
| const sessionId = browser.publicAPI.sessionId; | ||
|
|
||
| const bidiWsEndpoint = browser.publicAPI.capabilities.webSocketUrl as unknown as string; | ||
| const headers = browser.publicAPI.options?.headers ?? {}; | ||
|
|
||
| if (!bidiWsEndpoint) { | ||
| return null; | ||
| } | ||
|
|
||
| return new this(bidiWsEndpoint, { headers, sessionId, requestTimeout: browser.config.httpTimeout }); | ||
| } | ||
|
|
||
| close(): void { | ||
| this._wsConnection.close(); | ||
| } | ||
|
|
||
| private _onMessage(data: RawData): void { | ||
| const message = data.toString("utf8"); | ||
|
|
||
| try { | ||
| const jsonParsedMessage: BiDiMessage = JSON.parse(message); | ||
|
|
||
| if (debugBidi.enabled) { | ||
| debugBidi( | ||
| `< ${inspect( | ||
| { sessionId: this._sessionId, ...jsonParsedMessage }, | ||
| { | ||
| depth: 3, | ||
| maxStringLength: 150, | ||
| breakLength: Infinity, | ||
| compact: true, | ||
| }, | ||
| )}`, | ||
| ); | ||
| } | ||
|
|
||
| if (!("id" in jsonParsedMessage)) { | ||
| if (this.onEventMessage) { | ||
| this.onEventMessage(jsonParsedMessage); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (jsonParsedMessage.type === "error" && jsonParsedMessage.id === null) { | ||
| debugBidi( | ||
| `\u2718 Received error message without ID: ${jsonParsedMessage.message}\n${jsonParsedMessage.stacktrace}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (jsonParsedMessage.type === "success") { | ||
| this._wsConnection.provideResponseFor(jsonParsedMessage.id, jsonParsedMessage.result); | ||
| } else if (jsonParsedMessage.type === "error") { | ||
| this._wsConnection.provideResponseFor( | ||
| jsonParsedMessage.id as number, | ||
| new BIDIRequestError({ | ||
| message: jsonParsedMessage.message, | ||
| code: jsonParsedMessage.error, | ||
| requestId: jsonParsedMessage.id as number, | ||
| }), | ||
| ); | ||
| } | ||
| } catch (err) { | ||
| if (debugBidi.enabled) { | ||
| const data = [ | ||
| `\u2718 Couldn't process BIDI message`, | ||
| `\tSession ID: ${this._sessionId}`, | ||
| `\tError: ${inspect( | ||
| { sessionId: this._sessionId, ...(err || {}) }, | ||
| { | ||
| depth: 3, | ||
| maxStringLength: 150, | ||
| breakLength: Infinity, | ||
| compact: true, | ||
| }, | ||
| )}`, | ||
| `\tMessage: "${message}"`, | ||
| ]; | ||
| debugBidi(data.join("\n")); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** @description Performs high-level CDP request with retries and timeouts */ | ||
| async request<T = void>(method: BiDiCommand["method"], params: BiDiCommand["params"] = {}): Promise<T> { | ||
| let result!: T | WsError; | ||
|
|
||
| for (let retriesLeft = BIDI_REQUEST_RETRIES; retriesLeft >= 0; retriesLeft--) { | ||
| const id = this._wsConnection.getRequestId(); | ||
| const requestMessage = JSON.stringify({ id, method, params }); | ||
|
|
||
| if (debugBidi.enabled) { | ||
| debugBidi( | ||
| `> ${inspect( | ||
| { | ||
| sessionId: this._sessionId, | ||
| id, | ||
| method, | ||
| params, | ||
| }, | ||
| { | ||
| depth: 3, | ||
| maxStringLength: 150, | ||
| breakLength: Infinity, | ||
| compact: true, | ||
| }, | ||
| )}`, | ||
| ); | ||
| } | ||
|
|
||
| result = (await this._wsConnection.makeRequest(id, requestMessage)) as T | WsError; | ||
|
|
||
| if (!(result instanceof WsError) || !result.isRetryable() || retriesLeft <= 0) { | ||
| break; | ||
| } | ||
|
|
||
| if (debugBidi.enabled) { | ||
| debugBidi( | ||
| `⟳ ${inspect({ | ||
| sessionId: this._sessionId, | ||
| id, | ||
| method, | ||
| params, | ||
| errorMessage: result.message, | ||
| retriesLeft: retriesLeft, | ||
| })}`, | ||
| ); | ||
| } | ||
|
|
||
| // Intentionally avoiding wait after timeout | ||
| if (!(result instanceof BIDIRequestTimeoutError)) { | ||
| await exponentiallyWait({ | ||
| baseDelay: BIDI_REQUEST_RETRY_BASE_DELAY, | ||
| attempt: BIDI_REQUEST_RETRIES - retriesLeft, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (result instanceof WsError) { | ||
| throw result; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export const BIDI_CONNECTION_TIMEOUT = 15000; // 15 sec | ||
| export const BIDI_CONNECTION_RETRIES = 3; | ||
| export const BIDI_CONNECTION_RETRY_BASE_DELAY = 500; | ||
| export const BIDI_REQUEST_RETRIES = 3; | ||
| export const BIDI_REQUEST_RETRY_BASE_DELAY = 500; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import debugModule from "debug"; | ||
|
|
||
| export const debugBidi = debugModule("testplane:bidi"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import * as logger from "../../utils/logger"; | ||
| import { EventEmitter } from "events"; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type AnyFunc = (...args: any) => unknown; | ||
|
|
||
| export class BIDIEmitter<Events extends { [key in keyof Events]: unknown }> extends EventEmitter { | ||
| private _callbackMap: Map<AnyFunc, AnyFunc> = new Map(); | ||
|
|
||
| on<U extends string & keyof Events>(event: U, listener: (params: Events[U]) => void | Promise<void>): this { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const eventListenerWithErrorBoundary = (params: Events[U]): void | Promise<void> => { | ||
| const logError = (e: unknown): void => { | ||
| logger.error(`Catched unhandled error in BiDi "${event}" handler: ${(e && (e as Error).stack) || e}`); | ||
| }; | ||
|
|
||
| try { | ||
| const result = listener(params); | ||
|
|
||
| return result instanceof Promise ? result.catch(logError) : result; | ||
| } catch (e) { | ||
| logError(e); | ||
| } | ||
| }; | ||
|
|
||
| this._callbackMap.set(listener, eventListenerWithErrorBoundary); | ||
|
|
||
| return super.on(event, eventListenerWithErrorBoundary); | ||
| } | ||
|
|
||
| off<U extends string & keyof Events>(event: U, listener: (params: Events[U]) => void | Promise<void>): this { | ||
| const eventListenerWithErrorBoundary = this._callbackMap.get(listener); | ||
|
|
||
| if (eventListenerWithErrorBoundary) { | ||
| this._callbackMap.delete(listener); | ||
| super.off(event, eventListenerWithErrorBoundary); | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { | ||
| WsError, | ||
| WsConnectionEstablishmentError, | ||
| WsConnectionTerminatedError, | ||
| WsConnectionBreakError, | ||
| WsConnectionTimeoutError, | ||
| WsRequestTimeoutError, | ||
| } from "../../ws-connection/error"; | ||
|
|
||
| export class BIDIError extends WsError { | ||
| isRetryable(): boolean { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export class BIDIConnectionEstablishmentError extends WsConnectionEstablishmentError {} | ||
| export class BIDIConnectionBreakError extends WsConnectionBreakError {} | ||
| export class BIDIConnectionTerminatedError extends WsConnectionTerminatedError {} | ||
| export class BIDIConnectionTimeoutError extends WsConnectionTimeoutError {} | ||
| export class BIDIRequestTimeoutError extends WsRequestTimeoutError {} | ||
|
|
||
| export class BIDIRequestError extends WsError { | ||
| public stacktrace?: string; | ||
|
|
||
| constructor({ | ||
| message, | ||
| code, | ||
| requestId, | ||
| stacktrace, | ||
| }: { | ||
| message: string; | ||
| code?: number | string; | ||
| requestId?: number; | ||
| stacktrace?: string; | ||
| }) { | ||
| super({ message, requestId, code }); | ||
|
|
||
| this.stacktrace = stacktrace; | ||
|
|
||
| if (stacktrace) { | ||
| this.message += `\n\tStacktrace:\n${stacktrace}`; | ||
| } | ||
| } | ||
|
|
||
| isRetryable(): boolean { | ||
| return ( | ||
| this.code === "unknown error" || | ||
| this.code === "unable to capture screen" || | ||
| this.code === "unable to close browser" | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First file with some code (which is 80% similar to CDP one)