bidi2pdf 0.1.13 → 0.1.15

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 (121) 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 +93 -6
  8. data/README.md +80 -5
  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 +97 -4
  13. data/lib/bidi2pdf/bidi/buffered_web_socket_client.rb +178 -0
  14. data/lib/bidi2pdf/bidi/client.rb +12 -5
  15. data/lib/bidi2pdf/bidi/commands/base.rb +2 -0
  16. data/lib/bidi2pdf/bidi/commands/capture_screenshot.rb +33 -0
  17. data/lib/bidi2pdf/bidi/commands/set_viewport.rb +32 -0
  18. data/lib/bidi2pdf/bidi/commands.rb +2 -0
  19. data/lib/bidi2pdf/bidi/network_event.rb +12 -3
  20. data/lib/bidi2pdf/bidi/network_events.rb +9 -1
  21. data/lib/bidi2pdf/bidi/session.rb +1 -1
  22. data/lib/bidi2pdf/chromedriver_manager.rb +10 -6
  23. data/lib/bidi2pdf/cli.rb +11 -1
  24. data/lib/bidi2pdf/launcher.rb +5 -3
  25. data/lib/bidi2pdf/notifications/logging_subscriber.rb +2 -0
  26. data/lib/bidi2pdf/session_warmer.rb +377 -0
  27. data/lib/bidi2pdf/test_helpers/images.rb +8 -0
  28. data/lib/bidi2pdf/test_helpers/pdf_reader_utils.rb +1 -2
  29. data/lib/bidi2pdf/test_helpers/pdf_text_sanitizer.rb +1 -2
  30. data/lib/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rb +1 -1
  31. data/lib/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rb +22 -2
  32. data/lib/bidi2pdf/test_helpers/testcontainers/container_endpoint.rb +30 -0
  33. data/lib/bidi2pdf/test_helpers/testcontainers/testcontainers_refinement.rb +33 -0
  34. data/lib/bidi2pdf/test_helpers/testcontainers.rb +1 -0
  35. data/lib/bidi2pdf/version.rb +1 -1
  36. data/lib/bidi2pdf.rb +26 -1
  37. data/sig/bidi2pdf/bidi/add_headers_interceptor.rbs +16 -9
  38. data/sig/bidi2pdf/bidi/auth_interceptor.rbs +32 -7
  39. data/sig/bidi2pdf/bidi/browser.rbs +4 -31
  40. data/sig/bidi2pdf/bidi/browser_console_logger.rbs +59 -0
  41. data/sig/bidi2pdf/bidi/browser_tab.rbs +269 -25
  42. data/sig/bidi2pdf/bidi/buffered_web_socket_client.rbs +81 -0
  43. data/sig/bidi2pdf/bidi/client.rbs +103 -43
  44. data/sig/bidi2pdf/bidi/command_manager.rbs +5 -17
  45. data/sig/bidi2pdf/bidi/commands/add_intercept.rbs +16 -8
  46. data/sig/bidi2pdf/bidi/commands/base.rbs +49 -9
  47. data/sig/bidi2pdf/bidi/commands/browser_close.rbs +3 -1
  48. data/sig/bidi2pdf/bidi/commands/browser_create_user_context.rbs +2 -0
  49. data/sig/bidi2pdf/bidi/commands/browser_remove_user_context.rbs +19 -0
  50. data/sig/bidi2pdf/bidi/commands/browsing_context_close.rbs +8 -2
  51. data/sig/bidi2pdf/bidi/commands/browsing_context_navigate.rbs +12 -6
  52. data/sig/bidi2pdf/bidi/commands/browsing_context_print.rbs +10 -5
  53. data/sig/bidi2pdf/bidi/commands/cancel_auth.rbs +8 -2
  54. data/sig/bidi2pdf/bidi/commands/capture_screenshot.rbs +31 -0
  55. data/sig/bidi2pdf/bidi/commands/cdp_get_session.rbs +17 -0
  56. data/sig/bidi2pdf/bidi/commands/create_window.rbs +4 -0
  57. data/sig/bidi2pdf/bidi/commands/get_user_contexts.rbs +2 -0
  58. data/sig/bidi2pdf/bidi/commands/network_continue.rbs +4 -0
  59. data/sig/bidi2pdf/bidi/commands/page_print.rbs +28 -0
  60. data/sig/bidi2pdf/bidi/commands/print_parameters_validator.rbs +3 -0
  61. data/sig/bidi2pdf/bidi/commands/provide_credentials.rbs +12 -6
  62. data/sig/bidi2pdf/bidi/commands/script_evaluate.rbs +4 -0
  63. data/sig/bidi2pdf/bidi/commands/session_end.rbs +2 -0
  64. data/sig/bidi2pdf/bidi/commands/session_status.rbs +3 -1
  65. data/sig/bidi2pdf/bidi/commands/session_subscribe.rbs +4 -0
  66. data/sig/bidi2pdf/bidi/commands/set_tab_cookie.rbs +51 -23
  67. data/sig/bidi2pdf/bidi/commands/set_usercontext_cookie.rbs +17 -19
  68. data/sig/bidi2pdf/bidi/commands/set_viewport.rbs +31 -0
  69. data/sig/bidi2pdf/bidi/connection_manager.rbs +2 -1
  70. data/sig/bidi2pdf/bidi/event_manager.rbs +14 -19
  71. data/sig/bidi2pdf/bidi/interceptor.rbs +5 -0
  72. data/sig/bidi2pdf/bidi/js_logger_helper.rbs +9 -0
  73. data/sig/bidi2pdf/bidi/logger_events.rbs +21 -0
  74. data/sig/bidi2pdf/bidi/navigation_failed_events.rbs +17 -0
  75. data/sig/bidi2pdf/bidi/network_event.rbs +64 -46
  76. data/sig/bidi2pdf/bidi/network_event_formatters/network_event_console_formatter.rbs +63 -0
  77. data/sig/bidi2pdf/bidi/network_event_formatters/network_event_formatter_utils.rbs +15 -0
  78. data/sig/bidi2pdf/bidi/network_event_formatters/network_event_html_formatter.rbs +18 -0
  79. data/sig/bidi2pdf/bidi/network_event_formatters.rbs +6 -0
  80. data/sig/bidi2pdf/bidi/network_events.rbs +14 -39
  81. data/sig/bidi2pdf/bidi/session.rbs +144 -31
  82. data/sig/bidi2pdf/bidi/user_context.rbs +64 -45
  83. data/sig/bidi2pdf/bidi/web_socket_dispatcher.rbs +22 -30
  84. data/sig/bidi2pdf/chromedriver_manager.rbs +61 -22
  85. data/sig/bidi2pdf/cli.rbs +22 -2
  86. data/sig/bidi2pdf/dsl.rbs +34 -0
  87. data/sig/bidi2pdf/launcher.rbs +39 -1
  88. data/sig/bidi2pdf/notifications/event.rbs +40 -0
  89. data/sig/bidi2pdf/notifications/instrumenter.rbs +21 -0
  90. data/sig/bidi2pdf/notifications/logging_subscriber.rbs +41 -0
  91. data/sig/bidi2pdf/notifications.rbs +24 -0
  92. data/sig/bidi2pdf/process_tree.rbs +2 -2
  93. data/sig/bidi2pdf/session_runner.rbs +41 -1
  94. data/sig/bidi2pdf/session_warmer.rbs +221 -0
  95. data/sig/bidi2pdf/test_helpers/configuration.rbs +51 -0
  96. data/sig/bidi2pdf/test_helpers/images/extractor.rbs +43 -0
  97. data/sig/bidi2pdf/test_helpers/images/image_similarity_checker.rbs +35 -0
  98. data/sig/bidi2pdf/test_helpers/images/tiff_helper.rbs +70 -0
  99. data/sig/bidi2pdf/test_helpers/images.rbs +0 -0
  100. data/sig/bidi2pdf/test_helpers/matchers/contains_pdf_image.rbs +0 -0
  101. data/sig/bidi2pdf/test_helpers/matchers/contains_pdf_text.rbs +0 -0
  102. data/sig/bidi2pdf/test_helpers/matchers/have_pdf_page_count.rbs +0 -0
  103. data/sig/bidi2pdf/test_helpers/matchers/match_pdf_text.rbs +0 -0
  104. data/sig/bidi2pdf/test_helpers/pdf_file_helper.rbs +23 -0
  105. data/sig/bidi2pdf/test_helpers/pdf_reader_utils.rbs +49 -0
  106. data/sig/bidi2pdf/test_helpers/pdf_text_sanitizer.rbs +38 -0
  107. data/sig/bidi2pdf/test_helpers/spec_paths_helper.rbs +35 -0
  108. data/sig/bidi2pdf/test_helpers/testcontainers/chromedriver_container.rbs +43 -0
  109. data/sig/bidi2pdf/test_helpers/testcontainers/chromedriver_test_helper.rbs +45 -0
  110. data/sig/bidi2pdf/test_helpers/testcontainers/container_endpoint.rbs +25 -0
  111. data/sig/bidi2pdf/test_helpers/testcontainers/shared_docker_network.rbs +3 -0
  112. data/sig/bidi2pdf/test_helpers/testcontainers/testcontainers_refinement.rbs +47 -0
  113. data/sig/bidi2pdf/test_helpers/testcontainers.rbs +6 -0
  114. data/sig/bidi2pdf/test_helpers.rbs +0 -0
  115. data/sig/bidi2pdf/verbose_logger.rbs +37 -0
  116. data/sig/bidi2pdf/version.rbs +3 -0
  117. data/sig/bidi2pdf.rbs +130 -2
  118. data/tasks/generate_rbs.rake +11 -11
  119. data/tasks/release_credentials_check.rake +97 -0
  120. metadata +67 -10
  121. data/sig/bidi2pdf/utils.rbs +0 -5
@@ -1,17 +1,42 @@
1
1
  module Bidi2pdf
2
2
  module Bidi
3
3
  class AuthInterceptor
4
- @credentials: Hash[Symbol, String]?
4
+ @url_patterns: untyped
5
5
 
6
- def initialize: (?credentials: Hash[Symbol, String]?) -> void
6
+ @context: untyped
7
7
 
8
- def intercept: (untyped request) -> untyped
8
+ @username: untyped
9
9
 
10
- private
10
+ @password: untyped
11
+
12
+ @network_ids: untyped
13
+
14
+ include Interceptor
15
+
16
+ def self.phases: () -> ::Array[untyped]
17
+
18
+ def self.events: () -> ::Array["network.authRequired"]
19
+
20
+ attr_reader headers: untyped
21
+
22
+ attr_reader url_patterns: untyped
11
23
 
12
- def add_auth_header: (untyped request) -> void
24
+ attr_reader context: untyped
25
+
26
+ attr_reader username: untyped
27
+
28
+ attr_reader password: untyped
29
+
30
+ attr_reader network_ids: untyped
31
+
32
+ def initialize: (username: untyped, password: untyped, url_patterns: untyped, context: untyped) -> void
33
+
34
+ def process_interception: (untyped _event_response, untyped navigation_id, untyped network_id, untyped url) -> untyped
35
+
36
+ private
13
37
 
14
- def auth_header_value: () -> String?
38
+ # rubocop:disable Naming/PredicateMethod
39
+ def handled_bad_credentials: (untyped navigation_id, untyped network_id, untyped url) -> (false | true)
15
40
  end
16
41
  end
17
- end
42
+ end
@@ -1,38 +1,11 @@
1
1
  module Bidi2pdf
2
2
  module Bidi
3
3
  class Browser
4
- @url: String
5
- @chromedriver_manager: Bidi2pdf::ChromedriverManager
6
- @browser: untyped
7
- @timeout: Integer
8
- @debug: bool
4
+ @client: untyped
9
5
 
10
- attr_reader browser: untyped
11
- attr_reader timeout: Integer
6
+ def initialize: (untyped client) -> void
12
7
 
13
- def initialize: (
14
- url: String,
15
- chromedriver_manager: Bidi2pdf::ChromedriverManager,
16
- ?timeout: Integer,
17
- ?debug: bool
18
- ) -> void
19
-
20
- def navigate: (?reload: bool) -> void
21
-
22
- def current_url: -> String
23
-
24
- def wait_until: [T] (
25
- ?timeout: Integer,
26
- ?message: String
27
- ) { () -> T? } -> T
28
-
29
- def close: () -> void
30
-
31
- private
32
-
33
- def connect: () -> untyped
34
-
35
- def setup_browser: () -> untyped
8
+ def create_user_context: () -> untyped
36
9
  end
37
10
  end
38
- end
11
+ end
@@ -0,0 +1,59 @@
1
+ module Bidi2pdf
2
+ module Bidi
3
+ class BrowserConsoleLoggerSuggar
4
+ @browser_console_logger: untyped
5
+
6
+ @level: untyped
7
+
8
+ @prefix: untyped
9
+
10
+ @timestamp: untyped
11
+
12
+ @text: untyped
13
+
14
+ @args: untyped
15
+
16
+ @stack_trace: untyped
17
+
18
+ attr_reader browser_console_logger: untyped
19
+
20
+ def initialize: (untyped browser_console_logger) -> void
21
+
22
+ def with_level: (untyped level) -> self
23
+
24
+ def with_prefix: (untyped prefix) -> self
25
+
26
+ def with_timestamp: (untyped timestamp) -> self
27
+
28
+ def with_text: (untyped text) -> self
29
+
30
+ def with_args: (untyped args) -> self
31
+
32
+ def with_stack_trace: (untyped stack_trace) -> self
33
+
34
+ def log_event: () -> untyped
35
+
36
+ def prefix: () -> untyped
37
+ end
38
+
39
+ class BrowserConsoleLogger
40
+ @logger: untyped
41
+
42
+ include JsLoggerHelper
43
+
44
+ attr_accessor logger: untyped
45
+
46
+ def initialize: (untyped logger) -> void
47
+
48
+ def builder: () -> untyped
49
+
50
+ def log_message: (untyped level, untyped prefix, untyped text) -> (nil | untyped)
51
+
52
+ def log_args: (untyped prefix, untyped args) -> (nil | untyped)
53
+
54
+ def log_stack_trace: (untyped prefix, untyped trace) -> untyped
55
+
56
+ def self.format_timestamp: (untyped timestamp) -> ("N/A" | untyped)
57
+ end
58
+ end
59
+ end
@@ -1,42 +1,286 @@
1
+ # Represents a browser tab for managing interactions and communication
2
+ # using the Bidi2pdf library. This class provides methods for creating
3
+ # browser tabs, managing cookies, navigating to URLs, executing scripts,
4
+ # and handling network events.
5
+ #
6
+ # @example Creating a browser tab
7
+ # browser_tab = Bidi2pdf::Bidi::BrowserTab.new(client, browsing_context_id, user_context_id)
8
+ # browser_tab.create_browser_tab
9
+ #
10
+ # @example Navigating to a URL
11
+ # browser_tab.navigate_to("http://example.com")
12
+ #
13
+ # @example Setting a cookie
14
+ # browser_tab.set_cookie(
15
+ # name: "session",
16
+ # value: "abc123",
17
+ # domain: "example.com"
18
+ # )
19
+ #
20
+ # @param [Object] client The WebSocket client for communication.
21
+ # @param [String] browsing_context_id The ID of the browsing context.
22
+ # @param [String] user_context_id The ID of the user context.
1
23
  module Bidi2pdf
2
24
  module Bidi
25
+ # Represents a browser tab for managing interactions and communication
26
+ # using the Bidi2pdf library. This class provides methods for creating
27
+ # browser tabs, managing cookies, navigating to URLs, executing scripts,
28
+ # handling network events, and general tab lifecycle management.
29
+ #
3
30
  class BrowserTab
4
- @browser: Bidi2pdf::Bidi::Browser
5
- @tab_id: String?
6
- @session_id: String?
7
- @timeout: Integer
31
+ @client: untyped
8
32
 
9
- attr_reader browser: Bidi2pdf::Bidi::Browser
10
- attr_reader tab_id: String?
11
- attr_reader session_id: String?
33
+ @browsing_context_id: untyped
12
34
 
13
- def initialize: (
14
- browser: Bidi2pdf::Bidi::Browser,
15
- ?tab_id: String?,
16
- ?timeout: Integer
17
- ) -> void
35
+ @user_context_id: untyped
18
36
 
19
- def navigate_to: (String url) -> void
37
+ @tabs: untyped
20
38
 
21
- def execute_script: [T] (String script, *untyped args) -> T
39
+ @network_events: untyped
22
40
 
23
- def wait_for_navigation: (?timeout: Integer) -> void
41
+ @logger_events: untyped
24
42
 
25
- def wait_for_element: (
26
- selector: String,
27
- ?visible: bool,
28
- ?timeout: Integer
29
- ) -> untyped
43
+ @navigation_failed_events: untyped
30
44
 
31
- def capture_screenshot: (?path: String?) -> String
45
+ @open: untyped
32
46
 
33
- def close: () -> void
47
+ @header_interceptor: untyped
48
+
49
+ @basic_auth_interceptor: untyped
50
+
51
+ @cdp_session: untyped
52
+
53
+ @event_handlers_registered: untyped
54
+
55
+ @listener_refs: untyped
56
+
57
+ include JsLoggerHelper
58
+
59
+ # @return [Object] The WebSocket client.
60
+ attr_reader client: untyped
61
+
62
+ # @return [String] The browsing context ID.
63
+ attr_reader browsing_context_id: untyped
64
+
65
+ # @return [String] The user context ID.
66
+ attr_reader user_context_id: untyped
67
+
68
+ # @return [Array<BrowserTab>] The list of tabs.
69
+ attr_reader tabs: untyped
70
+
71
+ # @return [NetworkEvents] The network events handler.
72
+ attr_reader network_events: untyped
73
+
74
+ # @return [Boolean] Whether the tab is open.
75
+ attr_reader open: untyped
76
+
77
+ # @return [LoggerEvents] The logger events handler.
78
+ attr_reader logger_events: untyped
79
+
80
+ # @return [NavigationFailedEvents] The navigation failed events handler.
81
+ attr_reader navigation_failed_events: untyped
82
+
83
+ # Initializes a new browser tab.
84
+ #
85
+ # @param [Object] client The WebSocket client for communication.
86
+ # @param [String] browsing_context_id The ID of the browsing context.
87
+ # @param [String] user_context_id The ID of the user context.
88
+ def initialize: (untyped client, untyped browsing_context_id, untyped user_context_id) -> void
89
+
90
+ # Creates a new browser tab.
91
+ #
92
+ # @return [BrowserTab] The newly created browser tab.
93
+ def create_browser_tab: () -> untyped
94
+
95
+ # Sets a cookie in the browser tab.
96
+ #
97
+ # @param [String] name The name of the cookie.
98
+ # @param [String] value The value of the cookie.
99
+ # @param [String] domain The domain for the cookie.
100
+ # @param [String] path The path for the cookie. Defaults to "/".
101
+ # @param [Boolean] secure Whether the cookie is secure. Defaults to true.
102
+ # @param [Boolean] http_only Whether the cookie is HTTP-only. Defaults to false.
103
+ # @param [String] same_site The SameSite attribute for the cookie. Defaults to "strict".
104
+ # @param [Integer] ttl The time-to-live for the cookie in seconds. Defaults to 30.
105
+ def set_cookie: (name: untyped, value: untyped, domain: untyped, ?path: ::String, ?secure: bool, ?http_only: bool, ?same_site: ::String, ?ttl: ::Integer) -> untyped
106
+
107
+ # Adds headers to requests in the browser tab.
108
+ #
109
+ # @param [Hash] headers The headers to add.
110
+ # @param [Array<String>] url_patterns The URL patterns to match.
111
+ # @return [AddHeadersInterceptor] The interceptor instance.
112
+ def add_headers: (headers: untyped, url_patterns: untyped) -> untyped
113
+
114
+ # Configures basic authentication for requests in the browser tab.
115
+ #
116
+ # @param [String] username The username for authentication.
117
+ # @param [String] password The password for authentication.
118
+ # @param [Array<String>] url_patterns The URL patterns to match.
119
+ # @return [AuthInterceptor] The interceptor instance.
120
+ def basic_auth: (username: untyped, password: untyped, url_patterns: untyped) -> untyped
121
+
122
+ # Navigates the browser tab to a specified URL.
123
+ #
124
+ # This method registers necessary event listeners and sends a navigation
125
+ # command to the browser tab, instructing it to load the specified URL.
126
+ # It validates that the URL is properly formatted before attempting navigation.
127
+ #
128
+ # @param [String] url The URL to navigate to.
129
+ # @raise [NavigationError] If the URL is invalid or improperly formatted.
130
+ # @example
131
+ # browser_tab.navigate_to("https://example.com")
132
+ def navigate_to: (untyped url, ?wait: ::String) -> untyped
133
+
134
+ # Renders HTML content in the browser tab.
135
+ #
136
+ # @param [String] html_content The HTML content to render.
137
+ # rubocop:disable Metrics/BlockLength
138
+ def render_html_content: (untyped html_content, ?wait: ::String) -> untyped
139
+
140
+ # Executes a script in the browser tab.
141
+ #
142
+ # This method allows you to execute JavaScript code within the context of the browser tab.
143
+ # Optionally, the script can be wrapped in a JavaScript Promise to handle asynchronous operations.
144
+ #
145
+ # @param [String] script The JavaScript code to execute.
146
+ # - This can be any valid JavaScript code that you want to run in the browser tab.
147
+ # @param [Boolean] wrap_in_promise Whether to wrap the script in a Promise. Defaults to false.
148
+ # - If true, the script will be wrapped in a Promise to handle asynchronous execution.
149
+ # - Use this option when the script involves asynchronous operations like network requests.
150
+ # You can use the predefined variable result to store the result of the script.
151
+ # @return [Object] The result of the script execution.
152
+ # - If the script executes successfully, the result of the last evaluated expression is returned.
153
+ # - If the script fails, an error or exception details may be returned.
154
+ def execute_script: (untyped script, ?wrap_in_promise: bool) -> untyped
155
+
156
+ # Injects a JavaScript script element into the page, either from a URL or with inline content.
157
+ #
158
+ # @param [String, nil] url The URL of the script to load (optional).
159
+ # @param [String, nil] content The JavaScript content to inject (optional).
160
+ # @param [String, nil] id The ID attribute for the script element (optional).
161
+ # @return [Object] The result from the script creation promise.
162
+ def inject_script: (?url: untyped?, ?content: untyped?, ?id: untyped?) -> untyped
163
+
164
+ # Injects a CSS style element into the page, either from a URL or with inline content.
165
+ #
166
+ # @param [String, nil] url The URL of the stylesheet to load (optional).
167
+ # @param [String, nil] content The CSS content to inject (optional).
168
+ # @param [String, nil] id The ID attribute for the style element (optional).
169
+ # @return [Object] The result from the style creation promise.
170
+ def inject_style: (?url: untyped?, ?content: untyped?, ?id: untyped?) -> untyped
171
+
172
+ # Waits until the network is idle in the browser tab.
173
+ #
174
+ # @param [Integer] timeout The timeout duration in seconds. Defaults to 10.
175
+ # @param [Float] poll_interval The polling interval in seconds. Defaults to 0.1.
176
+ def wait_until_network_idle: (?timeout: ::Integer, ?poll_interval: ::Float) -> untyped
177
+
178
+ # Waits until the page is fully loaded in the browser tab.
179
+ #
180
+ # This method executes a JavaScript script that checks if the page
181
+ # has finished loading.
182
+ #
183
+ # @param [String] check_script The JavaScript code to check if the page is loaded.
184
+ # - Defaults to a script that polls the `window.loaded` property.
185
+ # @return [Object] The result of the script execution.
186
+ # - If the page is loaded successfully, the Promise resolves with the value `'done'`.
187
+ # - If the script fails, an error or exception details may be returned.
188
+ def wait_until_page_loaded: (?check_script: untyped?) -> untyped
189
+
190
+ # Logs network traffic in the browser tab.
191
+ #
192
+ # @param [Symbol] format The format for logging (:console or :pdf). Defaults to :console.
193
+ # @param [String, nil] output The output file for PDF logging. Defaults to nil.
194
+ # @param [Hash] print_options Options for printing. Defaults to { background: true }.
195
+ # @yield [pdf_base64] A block to handle the PDF content.
196
+ def log_network_traffic: (?format: ::Symbol, ?output: untyped?, ?print_options: ::Hash[untyped, untyped]) { (?) -> untyped } -> (nil | untyped)
197
+
198
+ # Closes the browser tab and its associated resources.
199
+ def close: () -> (nil | untyped)
200
+
201
+ # Prints the content of the browser tab.
202
+ #
203
+ # @param [String, nil] outputfile The output file for the PDF. Defaults to nil.
204
+ # @param [Hash] print_options Options for printing. Defaults to { background: true }.
205
+ # @yield [pdf_base64] A block to handle the PDF content.
206
+ # @return [String, nil] The base64-encoded PDF content, or nil if outputfile or block is provided.
207
+ # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
208
+ def print: (?untyped? outputfile, ?print_options: ::Hash[untyped, untyped]) ?{ (?) -> untyped } -> untyped
209
+
210
+ # Captures a screenshot of the browser tab, mirroring #print's own
211
+ # file/block/base64-return trichotomy.
212
+ #
213
+ # @param [String, nil] outputfile The output file for the PNG. Defaults to nil.
214
+ # @param [String] origin "document" (default) or "viewport" - see browsingContext.captureScreenshot.
215
+ # @param [String, nil] format Image format override, e.g. "image/png". Defaults to the browser's own default.
216
+ # @param [Hash, nil] clip A clip rectangle/element, passed through verbatim. Defaults to nil (full capture).
217
+ # @yield [png_base64] A block to handle the PNG content.
218
+ # @return [String, nil] The base64-encoded PNG content, or nil if outputfile or block is provided.
219
+ # rubocop:disable Metrics/AbcSize
220
+ def screenshot: (?untyped? outputfile, ?origin: ::String, ?format: untyped?, ?clip: untyped?) ?{ (?) -> untyped } -> untyped
221
+
222
+ # Sets the browsing context's viewport - makes a headless render deterministic across
223
+ # machines, unlike leaving it at the browser's own default.
224
+ #
225
+ # @param [Integer] width
226
+ # @param [Integer] height
227
+ # @param [Float, nil] device_pixel_ratio Defaults to nil (browser default).
228
+ def set_viewport: (width: untyped, height: untyped, ?device_pixel_ratio: untyped?) -> untyped
34
229
 
35
230
  private
36
231
 
37
- def ensure_tab_active: () -> void
232
+ def build_command_and_extractor: (untyped print_options) -> ::Array[untyped]
233
+
234
+ def cdp_session: () -> untyped
235
+
236
+ def navigate_with_listeners: (untyped url, ?wait: ::String) -> untyped
237
+
238
+ def raise_navigation_error_for: (untyped url, untyped error) -> untyped
239
+
240
+ # browsingContext.navigate's own response never carries an HTTP status - only a
241
+ # network.responseCompleted event does, correlated back to this specific navigation via its
242
+ # "navigation" field (a Chrome/redirect-chain-wide ID, not the network request's own id).
243
+ # register_event_listeners already ran above, so network_events has been tracking since
244
+ # before the navigate command was even sent. Deliberately conservative: with no navigation
245
+ # ID, or no correlated *completed* request found, this stays silent rather than guessing -
246
+ # only a positively confirmed >= 400 status raises. max_by(&:start_timestamp) picks the last
247
+ # hop of a redirect chain (every hop shares the same navigation ID), matching the page the
248
+ # browser actually ended up on.
249
+ def check_navigation_http_status: (untyped url, untyped navigation_id) -> (nil | untyped)
250
+
251
+ # The last hop of a redirect chain (every hop shares the same navigation ID) - the page the
252
+ # browser actually ended up on, not wherever the chain started.
253
+ def correlated_navigation_response: (untyped navigation_id) -> untyped
254
+
255
+ def register_event_listeners: () -> (nil | untyped)
256
+
257
+ def handle_injection_exception: (untyped response, untyped url, untyped exception_class) -> untyped
258
+
259
+ # Generates JavaScript code for creating a script element with given parameters.
260
+ #
261
+ # @param [String, nil] url The URL of the script to load (optional).
262
+ # @param [String, nil] content The JavaScript content for the script (optional).
263
+ # @param [String, nil] id The ID attribute for the script element (optional).
264
+ # @return [String] JavaScript code that creates a script element.
265
+ def generate_script_element_code: (?url: untyped?, ?content: untyped?, ?id: untyped?) -> ::String
266
+
267
+ # Generates JavaScript code for creating a style element with given parameters.
268
+ #
269
+ # @param [String, nil] url The URL of the stylesheet to load (optional).
270
+ # @param [String, nil] content The CSS content for the style (optional).
271
+ # @param [String, nil] id The ID attribute for the style element (optional).
272
+ # @return [String] JavaScript code that creates a style element.
273
+ def generate_style_element_code: (?url: untyped?, ?content: untyped?, ?id: untyped?) -> ::String
274
+
275
+ # Closes the browsing context.
276
+ def close_context: () -> untyped
277
+
278
+ # Removes event listeners for the browser tab.
279
+ # rubocop:disable Metrics/AbcSize
280
+ def remove_event_listeners: () -> (nil | untyped)
38
281
 
39
- def connect_to_tab: () -> String?
282
+ # Closes all tabs associated with the browser tab.
283
+ def close_tabs: () -> untyped
40
284
  end
41
285
  end
42
- end
286
+ end
@@ -0,0 +1,81 @@
1
+ module Bidi2pdf
2
+ module Bidi
3
+ # A threaded WebSocket client on the `websocket` gem's framing - the same shape Selenium's Ruby
4
+ # BiDi client uses. It replaces websocket-client-simple, whose reader pulled one byte at a time
5
+ # (`getc`, then a frame-parse attempt per byte): a printed PDF comes back as one base64 message
6
+ # and every network event for a `data:` navigation echoes the whole URL, so that loop ran
7
+ # millions of times per render - ~730 ms to decode a 700 KB frame, against ~0.5 ms in 16 KB
8
+ # chunks.
9
+ #
10
+ # Emits :open, :message (a WebSocket frame, payload in #data), :error and :close.
11
+ class BufferedWebSocketClient
12
+ @listeners: untyped
13
+
14
+ @listeners_mutex: untyped
15
+
16
+ @write_mutex: untyped
17
+
18
+ # Re-entrant: a failed write inside #close closes again.
19
+ @close_monitor: untyped
20
+
21
+ @handshaked: untyped
22
+
23
+ @closed: untyped
24
+
25
+ @url: untyped
26
+
27
+ @socket: untyped
28
+
29
+ @handshake: untyped
30
+
31
+ @thread: untyped
32
+
33
+ READ_CHUNK_BYTES: 16384
34
+
35
+ attr_reader url: untyped
36
+
37
+ def self.connect: (untyped url, ?::Hash[untyped, untyped] options) ?{ (untyped) -> untyped } -> untyped
38
+
39
+ def initialize: () -> void
40
+
41
+ def on: (untyped event) { (?) -> untyped } -> untyped
42
+
43
+ def connect: (untyped url, ?::Hash[untyped, untyped] options) -> (nil | untyped)
44
+
45
+ # Commands are sent from whichever thread issues them. On a plain TCP socket one IO#write is
46
+ # already atomic, but OpenSSL::SSL::SSLSocket#write is not, so writes share a lock for wss://.
47
+ def send: (untyped data, ?type: ::Symbol) -> untyped
48
+
49
+ # Safe from any thread, including the reader's own: the reader sees the peer hang up and
50
+ # closes from inside its loop, so it must neither race a caller closing at the same moment
51
+ # nor be killed before :close has been emitted.
52
+ def close: (?untyped? error) -> untyped
53
+
54
+ def open?: () -> untyped
55
+
56
+ def closed?: () -> untyped
57
+
58
+ private
59
+
60
+ def emit: (untyped event, *untyped) -> untyped
61
+
62
+ def write: (untyped bytes) -> untyped
63
+
64
+ def say_goodbye: () -> untyped
65
+
66
+ def open_socket: (untyped uri, untyped options) -> untyped
67
+
68
+ def ssl_context: (untyped options) -> untyped
69
+
70
+ def read_loop: (untyped socket, untyped frame) -> untyped
71
+
72
+ def consume: (untyped chunk, untyped frame) -> (nil | untyped)
73
+
74
+ # WebSocket control frames (ping/pong/close) must never reach a JSON-parsing consumer -
75
+ # WebSocketDispatcher tries to parse every :message payload as JSON, so a raw close frame
76
+ # would raise JSON::ParserError, and an unanswered ping can make chromedriver or an
77
+ # intermediate proxy tear down an otherwise healthy connection on its own keepalive timeout.
78
+ def dispatch_frame: (untyped message) -> untyped
79
+ end
80
+ end
81
+ end