-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathclient.rb
More file actions
179 lines (145 loc) · 4.22 KB
/
client.rb
File metadata and controls
179 lines (145 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true
require "concurrent-ruby"
require "forwardable"
require "ferrum/client/subscriber"
require "ferrum/client/web_socket"
require "ferrum/utils/thread"
module Ferrum
class SessionClient
attr_reader :client, :session_id
def self.event_name(event, session_id)
[event, session_id].compact.join("_")
end
def initialize(client, session_id)
@client = client
@session_id = session_id
end
def command(method, async: false, **params)
message = build_message(method, params)
@client.send_message(message, async: async)
end
def on(event, &block)
@client.on(event_name(event), &block)
end
def off(event, id)
@client.off(event_name(event), id)
end
def subscribed?(event)
@client.subscribed?(event_name(event))
end
def respond_to_missing?(name, include_private)
@client.respond_to?(name, include_private)
end
def method_missing(name, *args, **opts, &block)
@client.send(name, *args, **opts, &block)
end
def close
@client.subscriber.clear(session_id: session_id)
end
private
def build_message(method, params)
@client.build_message(method, params).merge(sessionId: session_id)
end
def event_name(event)
self.class.event_name(event, session_id)
end
end
class Client
extend Forwardable
delegate %i[timeout timeout=] => :options
attr_reader :ws_url, :options, :subscriber
def initialize(ws_url, options)
@command_id = 0
@ws_url = ws_url
@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)
send_message(message, async: async)
end
def send_message(message, async:)
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 off(event, id)
@subscriber.off(event, id)
end
def subscribed?(event)
@subscriber.subscribed?(event)
end
def session(session_id)
SessionClient.new(self, session_id)
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
def build_message(method, params)
{ method: method, params: params }.merge(id: next_command_id)
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 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", /Failed to find context with 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