bidi2pdf 0.1.13 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/Dockerfile +53 -0
  3. data/.devcontainer/devcontainer.json +51 -0
  4. data/.devcontainer/docker-compose.yml +33 -0
  5. data/.ruby-gemset +1 -1
  6. data/.ruby-version +1 -1
  7. data/CHANGELOG.md +46 -6
  8. data/README.md +12 -0
  9. data/docker/Dockerfile +1 -1
  10. data/docker/Dockerfile.slim +2 -2
  11. data/docker/chrome-driver/chrome.json +874 -0
  12. data/lib/bidi2pdf/bidi/browser_tab.rb +52 -0
  13. data/lib/bidi2pdf/bidi/commands/capture_screenshot.rb +33 -0
  14. data/lib/bidi2pdf/bidi/commands/set_viewport.rb +32 -0
  15. data/lib/bidi2pdf/bidi/commands.rb +2 -0
  16. data/lib/bidi2pdf/cli.rb +11 -1
  17. data/lib/bidi2pdf/launcher.rb +5 -3
  18. data/lib/bidi2pdf/test_helpers/images.rb +8 -0
  19. data/lib/bidi2pdf/test_helpers/pdf_reader_utils.rb +1 -2
  20. data/lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb +1 -2
  21. data/lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb +1 -1
  22. data/lib/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rb +18 -0
  23. data/lib/bidi2pdf/test_helpers/testcontainers/container_endpoint.rb +30 -0
  24. data/lib/bidi2pdf/test_helpers/testcontainers/testcontainers_refinement.rb +33 -0
  25. data/lib/bidi2pdf/test_helpers/testcontainers.rb +1 -0
  26. data/lib/bidi2pdf/version.rb +1 -1
  27. data/lib/bidi2pdf.rb +2 -0
  28. data/sig/bidi2pdf/bidi/add_headers_interceptor.rbs +16 -9
  29. data/sig/bidi2pdf/bidi/auth_interceptor.rbs +32 -7
  30. data/sig/bidi2pdf/bidi/browser.rbs +4 -31
  31. data/sig/bidi2pdf/bidi/browser_console_logger.rbs +59 -0
  32. data/sig/bidi2pdf/bidi/browser_tab.rbs +252 -25
  33. data/sig/bidi2pdf/bidi/client.rbs +95 -43
  34. data/sig/bidi2pdf/bidi/command_manager.rbs +5 -17
  35. data/sig/bidi2pdf/bidi/commands/add_intercept.rbs +16 -8
  36. data/sig/bidi2pdf/bidi/commands/base.rbs +49 -9
  37. data/sig/bidi2pdf/bidi/commands/browser_close.rbs +3 -1
  38. data/sig/bidi2pdf/bidi/commands/browser_create_user_context.rbs +2 -0
  39. data/sig/bidi2pdf/bidi/commands/browser_remove_user_context.rbs +19 -0
  40. data/sig/bidi2pdf/bidi/commands/browsing_context_close.rbs +8 -2
  41. data/sig/bidi2pdf/bidi/commands/browsing_context_navigate.rbs +12 -6
  42. data/sig/bidi2pdf/bidi/commands/browsing_context_print.rbs +10 -5
  43. data/sig/bidi2pdf/bidi/commands/cancel_auth.rbs +8 -2
  44. data/sig/bidi2pdf/bidi/commands/capture_screenshot.rbs +31 -0
  45. data/sig/bidi2pdf/bidi/commands/cdp_get_session.rbs +17 -0
  46. data/sig/bidi2pdf/bidi/commands/create_window.rbs +4 -0
  47. data/sig/bidi2pdf/bidi/commands/get_user_contexts.rbs +2 -0
  48. data/sig/bidi2pdf/bidi/commands/network_continue.rbs +4 -0
  49. data/sig/bidi2pdf/bidi/commands/page_print.rbs +28 -0
  50. data/sig/bidi2pdf/bidi/commands/print_parameters_validator.rbs +3 -0
  51. data/sig/bidi2pdf/bidi/commands/provide_credentials.rbs +12 -6
  52. data/sig/bidi2pdf/bidi/commands/script_evaluate.rbs +4 -0
  53. data/sig/bidi2pdf/bidi/commands/session_end.rbs +2 -0
  54. data/sig/bidi2pdf/bidi/commands/session_status.rbs +3 -1
  55. data/sig/bidi2pdf/bidi/commands/session_subscribe.rbs +4 -0
  56. data/sig/bidi2pdf/bidi/commands/set_tab_cookie.rbs +51 -23
  57. data/sig/bidi2pdf/bidi/commands/set_usercontext_cookie.rbs +17 -19
  58. data/sig/bidi2pdf/bidi/commands/set_viewport.rbs +31 -0
  59. data/sig/bidi2pdf/bidi/connection_manager.rbs +2 -1
  60. data/sig/bidi2pdf/bidi/event_manager.rbs +14 -19
  61. data/sig/bidi2pdf/bidi/interceptor.rbs +5 -0
  62. data/sig/bidi2pdf/bidi/js_logger_helper.rbs +9 -0
  63. data/sig/bidi2pdf/bidi/logger_events.rbs +21 -0
  64. data/sig/bidi2pdf/bidi/navigation_failed_events.rbs +17 -0
  65. data/sig/bidi2pdf/bidi/network_event.rbs +54 -46
  66. data/sig/bidi2pdf/bidi/network_event_formatters/network_event_console_formatter.rbs +63 -0
  67. data/sig/bidi2pdf/bidi/network_event_formatters/network_event_formatter_utils.rbs +15 -0
  68. data/sig/bidi2pdf/bidi/network_event_formatters/network_event_html_formatter.rbs +18 -0
  69. data/sig/bidi2pdf/bidi/network_event_formatters.rbs +6 -0
  70. data/sig/bidi2pdf/bidi/network_events.rbs +14 -39
  71. data/sig/bidi2pdf/bidi/session.rbs +144 -31
  72. data/sig/bidi2pdf/bidi/user_context.rbs +64 -45
  73. data/sig/bidi2pdf/bidi/web_socket_dispatcher.rbs +22 -30
  74. data/sig/bidi2pdf/chromedriver_manager.rbs +57 -22
  75. data/sig/bidi2pdf/cli.rbs +22 -2
  76. data/sig/bidi2pdf/dsl.rbs +34 -0
  77. data/sig/bidi2pdf/launcher.rbs +39 -1
  78. data/sig/bidi2pdf/notifications/event.rbs +40 -0
  79. data/sig/bidi2pdf/notifications/instrumenter.rbs +21 -0
  80. data/sig/bidi2pdf/notifications/logging_subscriber.rbs +41 -0
  81. data/sig/bidi2pdf/notifications.rbs +24 -0
  82. data/sig/bidi2pdf/process_tree.rbs +2 -2
  83. data/sig/bidi2pdf/session_runner.rbs +41 -1
  84. data/sig/bidi2pdf/test_helpers/configuration.rbs +51 -0
  85. data/sig/bidi2pdf/test_helpers/images/extractor.rbs +43 -0
  86. data/sig/bidi2pdf/test_helpers/images/image_similarity_checker.rbs +35 -0
  87. data/sig/bidi2pdf/test_helpers/images/tiff_helper.rbs +70 -0
  88. data/sig/bidi2pdf/test_helpers/images.rbs +0 -0
  89. data/sig/bidi2pdf/test_helpers/matchers/contains_pdf_image.rbs +0 -0
  90. data/sig/bidi2pdf/test_helpers/matchers/contains_pdf_text.rbs +0 -0
  91. data/sig/bidi2pdf/test_helpers/matchers/have_pdf_page_count.rbs +0 -0
  92. data/sig/bidi2pdf/test_helpers/matchers/match_pdf_text.rbs +0 -0
  93. data/sig/bidi2pdf/test_helpers/pdf_file_helper.rbs +23 -0
  94. data/sig/bidi2pdf/test_helpers/pdf_reader_utils.rbs +49 -0
  95. data/sig/bidi2pdf/test_helpers/pdf_text_sanitizer.rbs +38 -0
  96. data/sig/bidi2pdf/test_helpers/spec_paths_helper.rbs +35 -0
  97. data/sig/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rbs +43 -0
  98. data/sig/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rbs +45 -0
  99. data/sig/bidi2pdf/test_helpers/testcontainers/container_endpoint.rbs +25 -0
  100. data/sig/bidi2pdf/test_helpers/testcontainers/shared_docker_network.rbs +3 -0
  101. data/sig/bidi2pdf/test_helpers/testcontainers/testcontainers_refinement.rbs +47 -0
  102. data/sig/bidi2pdf/test_helpers/testcontainers.rbs +6 -0
  103. data/sig/bidi2pdf/test_helpers.rbs +0 -0
  104. data/sig/bidi2pdf/verbose_logger.rbs +37 -0
  105. data/sig/bidi2pdf/version.rbs +3 -0
  106. data/sig/bidi2pdf.rbs +112 -2
  107. data/tasks/generate_rbs.rake +11 -11
  108. metadata +59 -7
  109. data/sig/bidi2pdf/utils.rbs +0 -5
@@ -1,51 +1,59 @@
1
1
  module Bidi2pdf
2
2
  module Bidi
3
3
  class NetworkEvent
4
- # Request data
5
- @request_id: String
6
- @url: String
7
- @method: String
8
- @headers: Hash[String, String]
9
- @post_data: String?
10
-
11
- # Response data
12
- @response_status: Integer?
13
- @response_headers: Hash[String, String]?
14
- @response_body: String?
15
-
16
- # Timing information
17
- @timestamp: Float
18
- @timing: Hash[Symbol, Float]?
19
-
20
- attr_reader request_id: String
21
- attr_reader url: String
22
- attr_reader method: String
23
- attr_reader headers: Hash[String, String]
24
- attr_reader post_data: String?
25
- attr_reader response_status: Integer?
26
- attr_reader response_headers: Hash[String, String]?
27
- attr_reader response_body: String?
28
- attr_reader timestamp: Float
29
- attr_reader timing: Hash[Symbol, Float]?
30
-
31
- def initialize: (
32
- request_id: String,
33
- url: String,
34
- method: String,
35
- headers: Hash[String, String],
36
- ?post_data: String?,
37
- ?timestamp: Float?
38
- ) -> void
39
-
40
- def add_response: (
41
- status: Integer,
42
- headers: Hash[String, String],
43
- ?body: String?
44
- ) -> void
45
-
46
- def add_timing: (Hash[Symbol, Float] timing_data) -> void
47
-
48
- def completed?: () -> bool
4
+ @id: untyped
5
+
6
+ @url: untyped
7
+
8
+ @start_timestamp: untyped
9
+
10
+ @timing: untyped
11
+
12
+ @state: untyped
13
+
14
+ @http_status_code: untyped
15
+
16
+ @http_method: untyped
17
+
18
+ @end_timestamp: untyped
19
+
20
+ @bytes_received: untyped
21
+
22
+ attr_reader id: untyped
23
+
24
+ attr_reader url: untyped
25
+
26
+ attr_reader state: untyped
27
+
28
+ attr_reader start_timestamp: untyped
29
+
30
+ attr_reader end_timestamp: untyped
31
+
32
+ attr_reader timing: untyped
33
+
34
+ attr_reader http_status_code: untyped
35
+
36
+ attr_reader http_method: untyped
37
+
38
+ attr_reader bytes_received: untyped
39
+
40
+ STATE_MAP: { "network.responseStarted" => "started", "network.responseCompleted" => "completed", "network.fetchError" => "error" }
41
+
42
+ def initialize: (id: untyped, url: untyped, timestamp: untyped, timing: untyped, state: untyped, ?http_status_code: untyped?, ?http_method: untyped?) -> void
43
+
44
+ def update_state: (untyped new_state, ?timestamp: untyped?, ?timing: untyped?, ?http_status_code: untyped?, ?bytes_received: untyped?) -> untyped
45
+
46
+ def map_state: (untyped state) -> untyped
47
+
48
+ def format_timestamp: (untyped timestamp) -> ("N/A" | untyped)
49
+
50
+ def duration_seconds: () -> (nil | untyped)
51
+
52
+ def in_progress?: () -> untyped
53
+
54
+ def to_s: () -> ::String
55
+
56
+ def dup: () -> untyped
49
57
  end
50
58
  end
51
- end
59
+ end
@@ -0,0 +1,63 @@
1
+ module Bidi2pdf
2
+ module Bidi
3
+ module NetworkEventFormatters
4
+ # rubocop: disable Metrics/AbcSize
5
+ class NetworkEventConsoleFormatter
6
+ @color_enabled: untyped
7
+
8
+ @logger: untyped
9
+
10
+ include NetworkEventFormatterUtils
11
+
12
+ attr_reader color_enabled: untyped
13
+
14
+ attr_reader logger: untyped
15
+
16
+ # ANSI styles
17
+ RESET: "\e[0m"
18
+
19
+ BOLD: "\e[1m"
20
+
21
+ DIM: "\e[2m"
22
+
23
+ RED: "\e[31m"
24
+
25
+ GREEN: "\e[32m"
26
+
27
+ YELLOW: "\e[33m"
28
+
29
+ CYAN: "\e[36m"
30
+
31
+ GRAY: "\e[90m"
32
+
33
+ def initialize: (?color: bool, ?logger: untyped) -> void
34
+
35
+ def log: (untyped events) -> untyped
36
+
37
+ def pretty_log: (untyped event) -> untyped
38
+
39
+ private
40
+
41
+ def format_timing: (untyped event) -> ("" | untyped)
42
+
43
+ def color_for_status: (untyped code) -> untyped
44
+
45
+ def bold: (untyped str) -> (::String | untyped)
46
+
47
+ def dim: (untyped str) -> (::String | untyped)
48
+
49
+ def green: (?::String str) -> (::String | untyped)
50
+
51
+ def yellow: (?::String str) -> (::String | untyped)
52
+
53
+ def red: (?::String str) -> (::String | untyped)
54
+
55
+ def cyan: (?::String str) -> (::String | untyped)
56
+
57
+ def gray: (?::String str) -> (::String | untyped)
58
+
59
+ def reset: () -> (untyped | "")
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,15 @@
1
+ module Bidi2pdf
2
+ module Bidi
3
+ module NetworkEventFormatters
4
+ module NetworkEventFormatterUtils
5
+ def format_bytes: (untyped size) -> ("N/A" | untyped)
6
+
7
+ def parse_timing: (untyped event) -> (::Array[untyped] | untyped)
8
+
9
+ def format_timestamp: (untyped timestamp) -> ("N/A" | untyped)
10
+
11
+ def shorten_url: (untyped url) -> (untyped | "data:text/html,...")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module Bidi2pdf
2
+ module Bidi
3
+ module NetworkEventFormatters
4
+ class NetworkEventHtmlFormatter
5
+ include NetworkEventFormatterUtils
6
+
7
+ def render: (untyped events) -> (nil | ::String)
8
+
9
+ def toc_entry: (untyped event, untyped index) -> ::String
10
+
11
+ # rubocop: disable Metrics/AbcSize
12
+ def render_event: (untyped event, untyped index) -> ::String
13
+
14
+ def render_timing_bars: (untyped timing) -> ("" | ::String)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module Bidi2pdf
2
+ module Bidi
3
+ module NetworkEventFormatters
4
+ end
5
+ end
6
+ end
@@ -1,55 +1,30 @@
1
1
  module Bidi2pdf
2
2
  module Bidi
3
3
  class NetworkEvents
4
- include Enumerable[NetworkEvent]
4
+ @context_id: untyped
5
5
 
6
- @events: Hash[String, NetworkEvent]
7
- @browser: Bidi2pdf::Bidi::Browser?
8
- @event_manager: Bidi2pdf::Bidi::EventManager?
9
- @recording: bool
10
- @filters: Array[Proc]
6
+ @events: untyped
11
7
 
12
- attr_reader events: Hash[String, NetworkEvent]
13
- attr_reader recording: bool
8
+ @network_event_formatter: untyped
14
9
 
15
- def initialize: (?browser: Bidi2pdf::Bidi::Browser?) -> void
10
+ attr_reader context_id: untyped
16
11
 
17
- def start_recording: (?browser: Bidi2pdf::Bidi::Browser?) -> void
12
+ attr_reader events: untyped
18
13
 
19
- def stop_recording: () -> void
14
+ attr_reader network_event_formatter: untyped
20
15
 
21
- def add_filter: () { (NetworkEvent) -> bool } -> void
16
+ def initialize: (untyped context_id) -> void
22
17
 
23
- def clear_filters: () -> void
18
+ def handle_event: (untyped data) -> untyped
24
19
 
25
- def clear: () -> void
20
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
21
+ def handle_response: (untyped method, untyped event) -> (nil | untyped)
26
22
 
27
- def size: () -> Integer
23
+ def all_events: () -> untyped
28
24
 
29
- def []: (String request_id) -> NetworkEvent?
25
+ def log_network_traffic: (?format: ::Symbol) -> untyped
30
26
 
31
- def each: () { (NetworkEvent) -> void } -> self
32
- | () -> Enumerator[NetworkEvent, self]
33
-
34
- def find_by_url: (String url_pattern) -> Array[NetworkEvent]
35
-
36
- def find_by_method: (String method) -> Array[NetworkEvent]
37
-
38
- def completed_requests: () -> Array[NetworkEvent]
39
-
40
- def pending_requests: () -> Array[NetworkEvent]
41
-
42
- def to_a: () -> Array[NetworkEvent]
43
-
44
- private
45
-
46
- def setup_event_listeners: () -> void
47
-
48
- def handle_request_event: (Hash[String, untyped] params) -> void
49
-
50
- def handle_response_event: (Hash[String, untyped] params) -> void
51
-
52
- def handle_loading_finished_event: (Hash[String, untyped] params) -> void
27
+ def wait_until_network_idle: (?timeout: ::Integer, ?poll_interval: ::Float) -> untyped
53
28
  end
54
29
  end
55
- end
30
+ end
@@ -1,52 +1,165 @@
1
+ # Represents a session for managing browser interactions and communication
2
+ # using the Bidi2pdf library. This class handles the setup, configuration,
3
+ # and execution of browser-related workflows, including session creation,
4
+ # WebSocket communication, and browser management.
5
+ #
6
+ # @example Creating and starting a session
7
+ # session = Bidi2pdf::Bidi::Session.new(session_url: "http://example.com/session", headless: true)
8
+ # session.start
9
+ #
10
+ # @example Retrieving user contexts
11
+ # session.user_contexts
12
+ #
13
+ # @example Closing the session
14
+ # session.close
15
+ #
16
+ # @param [String] session_url The URL for the session.
17
+ # @param [Boolean] headless Whether to run the browser in headless mode. Defaults to true.
18
+ # @param [Array<String>] chrome_args Additional Chrome arguments. Defaults to predefined arguments.
1
19
  module Bidi2pdf
2
20
  module Bidi
3
21
  class Session
4
- @browser: Bidi2pdf::Bidi::Browser
5
- @session_id: String?
6
- @contexts: Array[String]
7
- @timeout: Integer
8
- @event_manager: Bidi2pdf::Bidi::EventManager?
9
- @network_events: Bidi2pdf::Bidi::NetworkEvents?
22
+ @session_uri: untyped
10
23
 
11
- attr_reader browser: Bidi2pdf::Bidi::Browser
12
- attr_reader session_id: String?
13
- attr_reader contexts: Array[String]
24
+ @headless: untyped
14
25
 
15
- def initialize: (
16
- browser: Bidi2pdf::Bidi::Browser,
17
- ?session_id: String?,
18
- ?timeout: Integer
19
- ) -> void
26
+ @started: untyped
20
27
 
21
- def create: () -> String
28
+ @chrome_args: untyped
22
29
 
23
- def attach: (session_id: String) -> String
30
+ @client: untyped
24
31
 
25
- def detach: () -> void
32
+ @browser: untyped
26
33
 
27
- def execute_command: [T] (String method, ?Hash[Symbol, untyped] params) -> T
34
+ @websocket_url: untyped
28
35
 
29
- def navigate_to: (String url) -> void
36
+ # Events to subscribe to during the session.
37
+ SUBSCRIBE_EVENTS: ::Array["script"]
30
38
 
31
- def evaluate: [T] (String script) -> T
39
+ # Default Chrome arguments for the session.
40
+ DEFAULT_CHROME_ARGS: ::Array["--allow-pre-commit-input" | "--disable-dev-shm-usage" | "--disable-gpu" | "--disable-popup-blocking" | "--disable-hang-monitor" | "--disable-background-networking" | "--disable-background-timer-throttling" | "--disable-client-side-phishing-detection" | "--disable-component-extensions-with-background-pages" | "--disable-crash-reporter" | "--disable-default-apps" | "--disable-infobars" | "--disable-ipc-flooding-protection" | "--disable-prompt-on-repost" | "--disable-renderer-backgrounding" | "--disable-search-engine-choice-screen" | "--disable-sync" | "--enable-automation" | "--export-tagged-pdf" | "--force-color-profile=srgb" | "--generate-pdf-document-outline" | "--metrics-recording-only" | "--no-first-run" | "--password-store=basic" | "--use-mock-keychain" | "--disable-backgrounding-occluded-windows" | "--disable-breakpad" | "--enable-features=PdfOopif" | "--disable-features=Translate,AcceptCHFrame,MediaRouter,OptimizationHints,ProcessPerSiteUpToMainFrameThreshold,IsolateSandboxedIframes" | "--disable-extensions about:blank"]
32
41
 
33
- def wait_for_load: (?timeout: Integer?) -> void
42
+ # @return [URI] The URI of the session.
43
+ attr_reader session_uri: untyped
34
44
 
35
- def network_events: () -> Bidi2pdf::Bidi::NetworkEvents
45
+ # @return [Boolean] Whether the session has started.
46
+ attr_reader started: untyped
36
47
 
37
- def capture_screenshot: (?path: String?) -> String
48
+ # @return [Array<String>] The Chrome arguments for the session.
49
+ attr_reader chrome_args: untyped
38
50
 
39
- def print_to_pdf: (
40
- ?Hash[Symbol, untyped] parameters
41
- ) -> String
51
+ # Initializes a new session.
52
+ #
53
+ # @param [String] session_url The URL for the session.
54
+ # @param [Boolean] headless Whether to run the browser in headless mode. Defaults to true.
55
+ # @param [Array<String>] chrome_args Additional Chrome arguments. Defaults to predefined arguments.
56
+ def initialize: (session_url: untyped, ?headless: bool, ?chrome_args: untyped) -> void
42
57
 
43
- def close: () -> void
58
+ # Starts the session and initializes the client.
59
+ #
60
+ # @raise [StandardError] If an error occurs during session start.
61
+ def start: () -> untyped
44
62
 
45
- private
63
+ # Returns the WebSocket client for the session.
64
+ #
65
+ # @return [Bidi2pdf::Bidi::Client, nil] The WebSocket client, or nil if the session is not started.
66
+ def client: () -> untyped
67
+
68
+ # Returns the browser instance for the session.
69
+ #
70
+ # @return [Bidi2pdf::Bidi::Browser] The browser instance.
71
+ def browser: () -> untyped
72
+
73
+ # Closes the session and cleans up resources.
74
+ # rubocop:disable Metrics/AbcSize
75
+ def close: () -> untyped
76
+
77
+ # Retrieves user contexts for the session.
78
+ def user_contexts: () -> untyped
46
79
 
47
- def ensure_session: () -> String
80
+ # Retrieves the status of the session.
81
+ def status: () -> untyped
82
+
83
+ # Checks if the session has started.
84
+ #
85
+ # @return [Boolean] True if the session has started, false otherwise.
86
+ def started?: () -> untyped
87
+
88
+ # Retrieves the WebSocket URL for the session.
89
+ #
90
+ # @return [String] The WebSocket URL.
91
+ def websocket_url: () -> untyped
92
+
93
+ private
48
94
 
49
- def setup_event_listeners: () -> void
95
+ # Sends a command to the WebSocket client.
96
+ #
97
+ # @param [Object] command The command to send.
98
+ # @yield [response] A block to handle the response.
99
+ def send_cmd: (untyped command) { (?) -> untyped } -> untyped
100
+
101
+ # Creates a new browser instance.
102
+ #
103
+ # @return [Bidi2pdf::Bidi::Browser] The browser instance.
104
+ # rubocop:disable Metrics/AbcSize
105
+ def create_browser: () -> untyped
106
+
107
+ # Creates a new WebSocket client.
108
+ #
109
+ # @return [Bidi2pdf::Bidi::Client] The WebSocket client.
110
+ def create_client: () -> untyped
111
+
112
+ # Creates a new session and retrieves the WebSocket URL.
113
+ #
114
+ # @return [String] The WebSocket URL.
115
+ def create_new_session: () -> untyped
116
+
117
+ # Builds the session request payload.
118
+ #
119
+ # @return [Hash] The session request payload.
120
+ def session_request: () -> { "capabilities" => { "alwaysMatch" => { "browserName" => "chrome", "goog:chromeOptions" => { "args" => untyped }, "goog:prerenderingDisabled" => true, "unhandledPromptBehavior" => { default: "ignore" }, "acceptInsecureCerts" => true, "webSocketUrl" => true } } }
121
+
122
+ # Executes an API call with the given payload.
123
+ #
124
+ # @param [Hash] payload The payload for the API call.
125
+ # @return [Hash] The parsed response data.
126
+ def exec_api_call: (untyped payload) -> untyped
127
+
128
+ # Logs an API error.
129
+ #
130
+ # @param [String] message The error message.
131
+ # @param [Integer] code The response code.
132
+ # @param [String] body The response body.
133
+ def log_api_error: (untyped message, untyped code, untyped body) -> untyped
134
+
135
+ # Determines the error category based on the exception.
136
+ #
137
+ # @param [Exception] exception The exception to categorize.
138
+ # @return [String] The error category.
139
+ def error_category: (untyped exception) -> untyped
140
+
141
+ # Retrieves the error description for a given error type.
142
+ #
143
+ # @param [String] type The error type.
144
+ # @return [String] The error description.
145
+ def error_description: (untyped type) -> untyped
146
+
147
+ # Builds an error response.
148
+ #
149
+ # @param [String] error The error type.
150
+ # @param [String] message The error message.
151
+ # @param [Array<String>, nil] backtrace The error backtrace.
152
+ # @return [Hash] The error response.
153
+ def build_error: (untyped error, untyped message, ?untyped? backtrace) -> { "value" => untyped }
154
+
155
+ # Handles an error response from the session.
156
+ #
157
+ # @param [Hash] value The error response value.
158
+ # @raise [SessionNotStartedError] If the session could not be started.
159
+ def handle_error: (untyped value) -> (nil | untyped)
160
+
161
+ # Cleans up resources associated with the session.
162
+ def cleanup: () -> untyped
50
163
  end
51
164
  end
52
- end
165
+ end
@@ -1,50 +1,69 @@
1
1
  module Bidi2pdf
2
2
  module Bidi
3
+ # Represents a user context for managing browser interactions and cookies
4
+ # using the Bidi2pdf library. This class provides methods for creating
5
+ # user contexts, setting cookies, and creating browser windows.
6
+ #
7
+ # @example Creating a user context
8
+ # user_context = Bidi2pdf::Bidi::UserContext.new(client)
9
+ #
10
+ # @example Setting a cookie
11
+ # user_context.set_cookie(
12
+ # name: "session",
13
+ # value: "abc123",
14
+ # domain: "example.com",
15
+ # source_origin: "http://example.com"
16
+ # )
17
+ #
18
+ # @example Creating a browser window
19
+ # browser_window = user_context.create_browser_window
20
+ #
21
+ # @param [Object] client The WebSocket client for communication.
3
22
  class UserContext
4
- @browser: Bidi2pdf::Bidi::Browser
5
- @context_id: String
6
- @session_id: String?
7
- @event_manager: Bidi2pdf::Bidi::EventManager?
8
- @network_events: Bidi2pdf::Bidi::NetworkEvents?
9
- @closed: bool
10
-
11
- attr_reader context_id: String
12
- attr_reader browser: Bidi2pdf::Bidi::Browser
13
- attr_reader session_id: String?
14
-
15
- def initialize: (
16
- browser: Bidi2pdf::Bidi::Browser,
17
- context_id: String,
18
- ?session_id: String?
19
- ) -> void
20
-
21
- def create_session: () -> String
22
-
23
- def attach_session: (session_id: String) -> String
24
-
25
- def execute_command: [T] (String method, ?Hash[Symbol, untyped] params) -> T
26
-
27
- def navigate_to: (String url) -> void
28
-
29
- def evaluate: [T] (String script) -> T
30
-
31
- def wait_for_load: (?timeout: Integer?) -> void
32
-
33
- def network_events: () -> Bidi2pdf::Bidi::NetworkEvents
34
-
35
- def capture_screenshot: (?path: String?) -> String
36
-
37
- def print_to_pdf: (?Hash[Symbol, untyped] parameters) -> String
38
-
39
- def close: () -> void
40
-
41
- def closed?: () -> bool
42
-
43
- private
44
-
45
- def ensure_open: () -> void
46
-
47
- def setup_event_listeners: () -> void
23
+ @client: untyped
24
+
25
+ @context_id: untyped
26
+
27
+ # @return [Object] The WebSocket client.
28
+ attr_reader client: untyped
29
+
30
+ # Initializes a new user context.
31
+ #
32
+ # @param [Object] client The WebSocket client for communication.
33
+ def initialize: (untyped client) -> void
34
+
35
+ # Retrieves the user context ID, creating it if it does not exist.
36
+ #
37
+ # @return [String] The user context ID.
38
+ # @raise [RuntimeError] If an error occurs while creating the user context.
39
+ def context_id: () -> untyped
40
+
41
+ # Sets a cookie in the user context.
42
+ #
43
+ # @param [String] name The name of the cookie.
44
+ # @param [String] value The value of the cookie.
45
+ # @param [String] domain The domain for the cookie.
46
+ # @param [String] source_origin The source origin for the cookie.
47
+ # @param [String] path The path for the cookie. Defaults to "/".
48
+ # @param [Boolean] secure Whether the cookie is secure. Defaults to true.
49
+ # @param [Boolean] http_only Whether the cookie is HTTP-only. Defaults to false.
50
+ # @param [String] same_site The SameSite attribute for the cookie. Defaults to "strict".
51
+ # @param [Integer] ttl The time-to-live for the cookie in seconds. Defaults to 30.
52
+ def set_cookie: (name: untyped, value: untyped, domain: untyped, source_origin: untyped, ?path: ::String, ?secure: bool, ?http_only: bool, ?same_site: ::String, ?ttl: ::Integer) -> untyped
53
+
54
+ # Creates a new browser window in the user context.
55
+ #
56
+ # @return [BrowserTab] The newly created browser tab.
57
+ def create_browser_window: () -> untyped
58
+
59
+ # Closes the user context.
60
+ #
61
+ # This method removes the user context from the browser, effectively cleaning up
62
+ # any associated resources. If the user context does not exist, the method does nothing.
63
+ #
64
+ # @return [nil]
65
+ # @raise [RuntimeError] If an error occurs while removing the user context.
66
+ def close: () -> (nil | untyped)
48
67
  end
49
68
  end
50
- end
69
+ end