// Copyright 2025 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import type {ProtocolMapping} from '../../generated/protocol-mapping.js'; export type Command = keyof ProtocolMapping.Commands; export type CommandParams = ProtocolMapping.Commands[T]['paramsType'][0]; export type CommandResult = Omit; export type Event = keyof ProtocolMapping.Events; export type EventParams = ProtocolMapping.Events[T][0]; export interface CDPBaseMessage { sessionId?: string; } export interface CDPCommandRequest extends CDPBaseMessage { id: number; method: T; params: CommandParams; } export interface CDPCommandResponse extends CDPBaseMessage { id: number; result: CommandResult; } export interface CDPEvent extends CDPBaseMessage { method: T; params: EventParams; } /** * Keep this in sync with https://source.chromium.org/chromium/chromium/src/+/main:third_party/inspector_protocol/crdtp/dispatch.h. */ export enum CDPErrorStatus { PARSE_ERROR = -32700, INVALID_REQUEST = -32600, METHOD_NOT_FOUND = -32601, INVALID_PARAMS = -32602, INTERNAL_ERROR = -32603, SERVER_ERROR = -32000, SESSION_NOT_FOUND = SERVER_ERROR - 1, DEVTOOLS_STUB_ERROR = -32015, DEVTOOLS_REHYDRATION_ERROR = -32016, } export interface CDPError { code: CDPErrorStatus; message: string; data?: string; } export interface CDPErrorMessage extends CDPBaseMessage { id?: number; error: CDPError; } export type CDPReceivableMessage = CDPCommandResponse|CDPEvent|CDPErrorMessage; /** * Allows the sending and receiving of CDP commands and the notification of CDP events to observers. * * An instance of a CDPConnection "owns" the full transport channel and no other CDP traffic must * be proxied over it. This is because each implementation needs to manage "message IDs", which * would conflict with any other shared traffic. */ export interface CDPConnection { send(method: T, params: CommandParams, sessionId: string|undefined): Promise<{result: CommandResult}|{error: CDPError}>; observe(observer: CDPConnectionObserver): void; unobserve(observer: CDPConnectionObserver): void; } export interface CDPConnectionObserver { onEvent(event: CDPEvent): void; onDisconnect(reason: string): void; } /** * The protocol monitor and test harness require inspection of raw CDP message traffic. */ export interface DebuggableCDPConnection extends CDPConnection { observeMessages(observer: RawMessageObserver): void; unobserveMessages(observer: RawMessageObserver): void; } export interface RawMessageObserver { onMessageReceived(message: CDPReceivableMessage): void; onMessageSent(message: CDPCommandRequest): void; }