CdpClient.d.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Copyright 2021 Google LLC.
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import type { Protocol } from 'devtools-protocol';
  18. import type { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping.js';
  19. import { EventEmitter } from '../utils/EventEmitter.js';
  20. import type { MapperCdpConnection } from './CdpConnection.js';
  21. export type CdpEvents = {
  22. [Property in keyof ProtocolMapping.Events]: ProtocolMapping.Events[Property][0];
  23. };
  24. /** A error that will be thrown if/when the connection is closed. */
  25. export declare class CloseError extends Error {
  26. }
  27. export interface CdpClient extends EventEmitter<CdpEvents> {
  28. /** Unique session identifier. */
  29. sessionId: Protocol.Target.SessionID | undefined;
  30. /**
  31. * Provides an unique way to detect if an error was caused by the closure of a
  32. * Target or Session.
  33. *
  34. * @example During the creation of a subframe we navigate the main frame.
  35. * The subframe Target is closed while initialized commands are in-flight.
  36. * In this case we want to swallow the thrown error.
  37. */
  38. isCloseError(error: unknown): boolean;
  39. /**
  40. * Returns a command promise, which will be resolved with the command result
  41. * after receiving the result from CDP.
  42. * @param method Name of the CDP command to call.
  43. * @param params Parameters to pass to the CDP command.
  44. */
  45. sendCommand<CdpMethod extends keyof ProtocolMapping.Commands>(method: CdpMethod, params?: ProtocolMapping.Commands[CdpMethod]['paramsType'][0]): Promise<ProtocolMapping.Commands[CdpMethod]['returnType']>;
  46. }
  47. /** Represents a high-level CDP connection to the browser. */
  48. export declare class MapperCdpClient extends EventEmitter<CdpEvents> implements CdpClient {
  49. #private;
  50. constructor(cdpConnection: MapperCdpConnection, sessionId?: Protocol.Target.SessionID);
  51. get sessionId(): Protocol.Target.SessionID | undefined;
  52. sendCommand<CdpMethod extends keyof ProtocolMapping.Commands>(method: CdpMethod, ...params: ProtocolMapping.Commands[CdpMethod]['paramsType']): Promise<ProtocolMapping.Commands[CdpMethod]['returnType']>;
  53. isCloseError(error: unknown): boolean;
  54. }