Module: Camoufox::Server

Defined in:
lib/camoufox/server.rb

Class Method Summary collapse

Class Method Details

.launch(**kwargs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/camoufox/server.rb', line 11

def launch(**kwargs)
  launch_config = Utils.launch_options(**kwargs).to_h
  payload = Base64.strict_encode64(JSON.generate(Utils.camelize_hash(launch_config)))

  driver_dir = Camoufox.configuration.playwright_driver_dir
  raise MissingPlaywrightDriver, missing_driver_message unless driver_dir && Dir.exist?(driver_dir)

  node_path = Camoufox.configuration.node_path || 'node'
  script_path = File.expand_path("launchServer.js", __dir__)

  run_node_script(node_path, script_path, driver_dir, payload)
end

.missing_driver_messageObject



24
25
26
# File 'lib/camoufox/server.rb', line 24

def missing_driver_message
  'Set CAMOUFOX_PLAYWRIGHT_DRIVER_DIR to the Playwright driver directory (contains lib/browserServerImpl.js)'
end

.run_node_script(node_path, script_path, working_dir, payload) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/camoufox/server.rb', line 28

def run_node_script(node_path, script_path, working_dir, payload)
  env = { 'CAMOUFOX_PLAYWRIGHT_DRIVER_DIR' => working_dir }
  Open3.popen2e(env, node_path, script_path, chdir: working_dir) do |stdin, stdout_err, wait_thr|
    stdin.write(payload)
    stdin.close

    stdout_err.each { |line| puts line }

    status = wait_thr.value
    return if status.success?

    raise NodeExecutionFailed.new("Playwright server exited with status #{status.exitstatus}", status)
  end
rescue Errno::ENOENT => e
  raise NodeExecutionFailed.new("Failed to execute #{node_path}: #{e.message}", nil)
end