Skip to content

Commit

Permalink
ref: rename Client
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Jan 5, 2024
1 parent 1294412 commit 6a73fcf
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 123 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ instead of passing browser and making cyclic dependency on the browser instance,
- Got rid of `Concurrent::Async` in `Ferrum::Browser::Subscriber` [#432]
- `Ferrum::Page#set_window_bounds` is renamed to `Ferrum::Page#window_bounds=`
- `Ferrum::Page` get right client from the Target and passes it down everywhere [#433]
- `Ferrum::Network::InterceptedRequest` accepts `Browser::Client` instead of `Ferrum::Page` [#433]
- `Ferrum::Network::InterceptedRequest` accepts `Ferrum::Browser::Client` instead of `Ferrum::Page` [#433]
- `Ferrum::Browser::Client` -> `Ferrum::Client` [#433]

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
require "forwardable"
require "ferrum/page"
require "ferrum/proxy"
require "ferrum/client"
require "ferrum/contexts"
require "ferrum/browser/xvfb"
require "ferrum/browser/options"
require "ferrum/browser/process"
require "ferrum/browser/client"
require "ferrum/browser/binary"
require "ferrum/browser/version_info"

Expand Down
115 changes: 0 additions & 115 deletions lib/ferrum/browser/client.rb

This file was deleted.

113 changes: 113 additions & 0 deletions lib/ferrum/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# frozen_string_literal: true

require "forwardable"
require "ferrum/client/subscriber"
require "ferrum/client/web_socket"

module Ferrum
class Client
extend Forwardable
delegate %i[timeout timeout=] => :options

attr_reader :options

def initialize(ws_url, options)
@command_id = 0
@options = options
@pendings = Concurrent::Hash.new
@ws = WebSocket.new(ws_url, options.ws_max_receive_size, options.logger)
@subscriber = Subscriber.new

start
end

def command(method, async: false, **params)
message = build_message(method, params)

if async
@ws.send_message(message)
true
else
pending = Concurrent::IVar.new
@pendings[message[:id]] = pending
@ws.send_message(message)
data = pending.value!(timeout)
@pendings.delete(message[:id])

raise DeadBrowserError if data.nil? && @ws.messages.closed?
raise TimeoutError unless data

error, response = data.values_at("error", "result")
raise_browser_error(error) if error
response
end
end

def on(event, &block)
@subscriber.on(event, &block)
end

def subscribed?(event)
@subscriber.subscribed?(event)
end

def close
@ws.close
# Give a thread some time to handle a tail of messages
@pendings.clear
@thread.kill unless @thread.join(1)
@subscriber.close
end

def inspect
"#<#{self.class} " \
"@command_id=#{@command_id.inspect} " \
"@pendings=#{@pendings.inspect} " \
"@ws=#{@ws.inspect}>"
end

private

def start
@thread = Utils::Thread.spawn do
loop do
message = @ws.messages.pop
break unless message

if message.key?("method")
@subscriber << message
else
@pendings[message["id"]]&.set(message)
end
end
end
end

def build_message(method, params)
{ method: method, params: params }.merge(id: next_command_id)
end

def next_command_id
@command_id += 1
end

def raise_browser_error(error)
case error["message"]
# Node has disappeared while we were trying to get it
when "No node with given id found",
"Could not find node with given id",
"Inspected target navigated or closed"
raise NodeNotFoundError, error
# Context is lost, page is reloading
when "Cannot find context with specified id"
raise NoExecutionContextError, error
when "No target with given id found"
raise NoSuchPageError
when /Could not compute content quads/
raise CoordinatesNotFoundError
else
raise BrowserError, error
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Ferrum
class Browser
class Client
class Subscriber
INTERRUPTIONS = %w[Fetch.requestPaused Fetch.authRequired].freeze

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require "websocket/driver"

module Ferrum
class Browser
class Client
class WebSocket
WEBSOCKET_BUG_SLEEP = 0.05
SKIP_LOGGING_SCREENSHOTS = !ENV["FERRUM_LOGGING_SCREENSHOTS"]
Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def maybe_sleep_if_new_window
def build_client
options = @client.options
ws_url = options.ws_url.merge(path: "/devtools/page/#{id}").to_s
Browser::Client.new(ws_url, options)
Client.new(ws_url, options)
end
end
end
2 changes: 1 addition & 1 deletion spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@
allow(Ferrum::Browser::Process).to receive(:new).and_return(process)

error = StandardError.new
allow(Ferrum::Browser::Client).to receive(:new).and_raise(error)
allow(Ferrum::Client).to receive(:new).and_raise(error)

expect { Ferrum::Browser.new }.to raise_error(error)
expect(process.pid).to be(nil)
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
it "forcibly kills the child if it does not respond to SIGTERM" do
allow(Process).to receive(:spawn).and_return(5678)
allow(Process).to receive(:wait).and_return(nil)
allow(Ferrum::Browser::Client).to receive(:new).and_return(double.as_null_object)
allow(Ferrum::Client).to receive(:new).and_return(double.as_null_object)

allow_any_instance_of(Ferrum::Browser::Process).to receive(:parse_ws_url)

Expand All @@ -25,7 +25,7 @@

it "passes through env" do
allow(Process).to receive(:wait).and_return(nil)
allow(Ferrum::Browser::Client).to receive(:new).and_return(double.as_null_object)
allow(Ferrum::Client).to receive(:new).and_return(double.as_null_object)

allow(Process).to receive(:spawn).with({ "LD_PRELOAD" => "some.so" }, any_args).and_return(123_456_789)

Expand Down

0 comments on commit 6a73fcf

Please sign in to comment.