browser-transport.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import type { BrowserTransportCommandParams } from './browser-transport-types.js';
  2. export interface BrowserEvaluateOptions {
  3. awaitPromise?: boolean;
  4. returnByValue?: boolean;
  5. userGesture?: boolean;
  6. signal?: AbortSignal;
  7. timeoutMs?: number;
  8. }
  9. export interface BrowserTransportEvent {
  10. method: string;
  11. params?: unknown;
  12. sessionId?: string;
  13. }
  14. export type BrowserTransportEventListener = (event: BrowserTransportEvent) => void;
  15. export interface BrowserTransportCommandOptions {
  16. /** Abort the caller's wait and discard a late CDP response. */
  17. readonly signal?: AbortSignal;
  18. /** Per-command deadline. Providers may clamp this to a stricter limit. */
  19. readonly timeoutMs?: number;
  20. }
  21. export interface BrowserAttachedFrame {
  22. /** Flattened CDP session used to address the out-of-process frame. */
  23. readonly sessionId: string;
  24. /** Chromium frame/target identity, stable for the attached document. */
  25. readonly frameId: string;
  26. /** Session that owns the iframe element; omitted for a direct child of the main page. */
  27. readonly parentSessionId?: string;
  28. readonly url?: string;
  29. }
  30. export interface BrowserPageLifecycle {
  31. readonly sessionId: string;
  32. start(signal?: AbortSignal): Promise<void>;
  33. /**
  34. * Reset the provider connection/session without closing the page.
  35. *
  36. * Browser Core uses this after a command timeout or cancellation. Providers
  37. * that do not need a reconnect may implement it as a no-op. `close()` is
  38. * reserved for permanently destroying the page and its provider resources.
  39. */
  40. resetConnection?(): Promise<void>;
  41. stopLoading(): Promise<void>;
  42. close(): Promise<void>;
  43. }
  44. /**
  45. * Provider-neutral lower boundary for a browser page.
  46. *
  47. * The browser core only depends on this lifecycle/command contract. A
  48. * concrete provider owns the process and implements the transport (the
  49. * current provider is Headless Chrome over a DevTools WebSocket).
  50. */
  51. export interface BrowserTransport extends BrowserPageLifecycle {
  52. /** Provider-owned directory used for completed browser downloads. */
  53. readonly downloadDirectory?: string;
  54. /**
  55. * Open a sibling page within the same provider-owned browser/profile.
  56. * The returned page is started and ready for Browser Core actions.
  57. */
  58. openTab?(url: string): Promise<BrowserTransport>;
  59. /**
  60. * Send one protocol command.
  61. *
  62. * Providers must reject caller cancellation with `code: "ABORTED"` and a
  63. * command deadline with `code: "BROWSER_OPERATION_TIMEOUT"`. Browser Core
  64. * uses these structured codes to reset a connection whose late command may
  65. * still reach Chromium.
  66. */
  67. send<T = unknown>(
  68. method: string,
  69. params?: BrowserTransportCommandParams,
  70. sessionId?: string,
  71. options?: BrowserTransportCommandOptions,
  72. ): Promise<T>;
  73. evaluate<T = unknown>(expression: string, options?: BrowserEvaluateOptions): Promise<T>;
  74. /**
  75. * Evaluate in every currently attached frame execution context.
  76. *
  77. * Electron implements this through WebFrameMain while the native provider
  78. * routes through attached CDP target sessions. Browser Core uses it only for
  79. * probes whose result must include OOPIFs (for example drag-start state).
  80. */
  81. evaluateInAllFrames?<T = unknown>(
  82. expression: string,
  83. options?: BrowserEvaluateOptions,
  84. ): Promise<readonly T[]>;
  85. /** Current flattened OOPIF sessions attached below this page. */
  86. listAttachedFrames?(): readonly BrowserAttachedFrame[];
  87. onEvent(listener: BrowserTransportEventListener): () => void;
  88. waitForEvent(
  89. method: string,
  90. timeoutMs?: number,
  91. options?: Pick<BrowserTransportCommandOptions, 'signal'>,
  92. ): Promise<unknown>;
  93. }
  94. /** Backward-compatible name used by the native-headless test seam. */
  95. export type CdpTransport = BrowserTransport;