ferrum 0.19.1 → 0.20.0
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.
- checksums.yaml +4 -4
- data/README.md +60 -0
- data/lib/ferrum/browser/binary.rb +1 -2
- data/lib/ferrum/browser/options.rb +33 -12
- data/lib/ferrum/browser/process/output.rb +143 -0
- data/lib/ferrum/browser/process.rb +42 -69
- data/lib/ferrum/browser.rb +9 -23
- data/lib/ferrum/client/web_socket.rb +93 -73
- data/lib/ferrum/client.rb +9 -5
- data/lib/ferrum/console/location.rb +42 -0
- data/lib/ferrum/console/message.rb +230 -0
- data/lib/ferrum/console.rb +157 -0
- data/lib/ferrum/context.rb +23 -15
- data/lib/ferrum/contexts.rb +41 -18
- data/lib/ferrum/errors.rb +7 -5
- data/lib/ferrum/frame/dom.rb +50 -44
- data/lib/ferrum/frame/runtime/legacy.rb +336 -0
- data/lib/ferrum/frame/runtime.rb +416 -186
- data/lib/ferrum/frame/script.rb +289 -0
- data/lib/ferrum/frame.rb +33 -4
- data/lib/ferrum/interceptable.rb +19 -9
- data/lib/ferrum/isolated_world.rb +94 -0
- data/lib/ferrum/keyboard.rb +95 -8
- data/lib/ferrum/log/annotated.rb +77 -0
- data/lib/ferrum/log/default.rb +55 -0
- data/lib/ferrum/log/json.rb +32 -0
- data/lib/ferrum/log/paint.rb +28 -0
- data/lib/ferrum/log.rb +247 -0
- data/lib/ferrum/mouse.rb +16 -8
- data/lib/ferrum/node.rb +127 -52
- data/lib/ferrum/page/frames.rb +10 -3
- data/lib/ferrum/page/screenshot.rb +8 -7
- data/lib/ferrum/page.rb +106 -53
- data/lib/ferrum/proxy.rb +8 -7
- data/lib/ferrum/remote_object.rb +75 -0
- data/lib/ferrum/target.rb +7 -0
- data/lib/ferrum/utils/deprecate.rb +77 -0
- data/lib/ferrum/version.rb +1 -1
- data/lib/ferrum/worker.rb +8 -0
- data/lib/ferrum.rb +3 -0
- metadata +16 -2
|
@@ -12,19 +12,33 @@ module Ferrum
|
|
|
12
12
|
# framing, and exposes a queue of parsed incoming messages alongside
|
|
13
13
|
# methods to send commands and close the connection.
|
|
14
14
|
#
|
|
15
|
+
# The socket is only ever touched by two threads: the reader reads it and
|
|
16
|
+
# the writer writes it, both blocking. Callers and the driver only encode
|
|
17
|
+
# frames and queue them for the writer, so no lock is held while waiting
|
|
18
|
+
# on the socket and a browser that stops reading blocks nobody but the
|
|
19
|
+
# writer. A frame chunk the browser doesn't take within the write timeout
|
|
20
|
+
# closes the connection. JRuby can't mix a blocking read with
|
|
21
|
+
# `write_nonblock` or `IO.select` from another thread on the same socket,
|
|
22
|
+
# so neither is used.
|
|
23
|
+
#
|
|
15
24
|
class WebSocket
|
|
16
25
|
WEBSOCKET_BUG_SLEEP = 0.05
|
|
17
26
|
DEFAULT_PORTS = { "ws" => 80, "wss" => 443 }.freeze
|
|
18
|
-
|
|
27
|
+
WRITE_CHUNK_SIZE = 64 * 1024
|
|
19
28
|
ESCAPE_OR_LONE_SURROGATE = /\\u[dD][89abAB]\h{2}\\u[dD][c-fC-F]\h{2}|\\u([dD][89a-fA-F]\h{2})|\\./
|
|
20
29
|
|
|
21
30
|
attr_reader :url, :messages
|
|
22
31
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
# @return [String, nil]
|
|
33
|
+
# Why Ferrum closed the connection itself, `nil` if it didn't.
|
|
34
|
+
attr_reader :close_reason
|
|
35
|
+
|
|
36
|
+
def initialize(url, max_receive_size, log, write_timeout = nil)
|
|
37
|
+
@url = url
|
|
38
|
+
@log = log
|
|
39
|
+
@write_timeout = write_timeout
|
|
40
|
+
uri = URI.parse(@url)
|
|
41
|
+
port = uri.port || DEFAULT_PORTS[uri.scheme]
|
|
28
42
|
|
|
29
43
|
if port == 443 || url.scheme == "wss"
|
|
30
44
|
tcp = TCPSocket.new(uri.host, port)
|
|
@@ -40,18 +54,19 @@ module Ferrum
|
|
|
40
54
|
@driver = ::WebSocket::Driver.client(self, max_length: max_receive_size)
|
|
41
55
|
# websocket-driver holds no locks and is called from many threads: commands from
|
|
42
56
|
# callers, pong/close replies from the reader. One lock keeps frames from interleaving.
|
|
43
|
-
@
|
|
57
|
+
@mutex = Mutex.new
|
|
58
|
+
@encoded = []
|
|
44
59
|
@messages = Queue.new
|
|
45
|
-
|
|
46
|
-
@screenshot_commands = Concurrent::Hash.new if SKIP_LOGGING_SCREENSHOTS
|
|
60
|
+
@outgoing = Queue.new
|
|
47
61
|
|
|
48
62
|
@driver.on(:open, &method(:on_open))
|
|
49
63
|
@driver.on(:message, &method(:on_message))
|
|
50
64
|
@driver.on(:close, &method(:on_close))
|
|
51
65
|
|
|
52
66
|
start
|
|
67
|
+
start_writer
|
|
53
68
|
|
|
54
|
-
|
|
69
|
+
drive { @driver.start }
|
|
55
70
|
end
|
|
56
71
|
|
|
57
72
|
#
|
|
@@ -73,13 +88,7 @@ module Ferrum
|
|
|
73
88
|
#
|
|
74
89
|
def on_message(event)
|
|
75
90
|
data = safely_parse_json(event.data)
|
|
76
|
-
|
|
77
|
-
if SKIP_LOGGING_SCREENSHOTS && @screenshot_commands[data&.dig("id")]
|
|
78
|
-
@screenshot_commands.delete(data&.dig("id"))
|
|
79
|
-
output.sub!(/{"data":"[^"]*"}/, %("Set FERRUM_LOGGING_SCREENSHOTS=true to see screenshots in Base64"))
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
@logger&.puts(" ◀ #{Utils::ElapsedTime.elapsed_time} #{output}\n")
|
|
91
|
+
@log&.received(data, event.data, self)
|
|
83
92
|
|
|
84
93
|
# If we couldn't parse JSON data for some reason (parse error or deeply nested object) we
|
|
85
94
|
# don't push response to @messages. Worse that could happen we raise timeout error due to command didn't return
|
|
@@ -89,76 +98,52 @@ module Ferrum
|
|
|
89
98
|
end
|
|
90
99
|
|
|
91
100
|
#
|
|
92
|
-
# Handles the driver's `:close` event: closes the message queue
|
|
93
|
-
#
|
|
101
|
+
# Handles the driver's `:close` event: closes the message queue, lets the
|
|
102
|
+
# writer send what's queued and close the socket, then kills the reader
|
|
103
|
+
# thread.
|
|
94
104
|
#
|
|
95
105
|
# @return [void]
|
|
96
106
|
#
|
|
97
107
|
def on_close(_event)
|
|
98
108
|
@messages.close
|
|
99
|
-
|
|
109
|
+
queue_encoded
|
|
110
|
+
@outgoing.close
|
|
100
111
|
@thread.kill
|
|
101
112
|
end
|
|
102
113
|
|
|
103
114
|
#
|
|
104
|
-
# Serializes a CDP command to JSON and
|
|
105
|
-
# frame.
|
|
115
|
+
# Serializes a CDP command to JSON and queues it as a websocket text
|
|
116
|
+
# frame. Returns without waiting for the frame to be written.
|
|
106
117
|
#
|
|
107
118
|
# @param [Hash] data
|
|
108
119
|
# The message to send, must include an `:id` key.
|
|
109
120
|
#
|
|
110
121
|
# @param [Numeric, nil] timeout
|
|
111
|
-
# How long the
|
|
112
|
-
#
|
|
122
|
+
# How long the frame may wait in the queue. A frame still queued after
|
|
123
|
+
# that is dropped, nobody waits for its response anymore. `nil` keeps it
|
|
124
|
+
# until it's written.
|
|
113
125
|
#
|
|
114
126
|
# @return [void]
|
|
115
127
|
#
|
|
116
|
-
# @raise [Ferrum::DeadBrowserError]
|
|
117
|
-
# If the browser accepted no data for `timeout` seconds. The connection
|
|
118
|
-
# is closed, since the frame was cut off midway.
|
|
119
|
-
#
|
|
120
128
|
def send_message(data, timeout = nil)
|
|
121
|
-
@screenshot_commands[data[:id]] = true if SKIP_LOGGING_SCREENSHOTS
|
|
122
|
-
|
|
123
129
|
json = data.to_json
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
ensure
|
|
128
|
-
@write_timeout = nil
|
|
129
|
-
end
|
|
130
|
-
@logger&.puts("\n\n▶ #{Utils::ElapsedTime.elapsed_time} #{json}")
|
|
130
|
+
deadline = Utils::ElapsedTime.monotonic_time + timeout if timeout
|
|
131
|
+
drive(deadline) { @driver.text(json) }
|
|
132
|
+
@log&.sent(data, self)
|
|
131
133
|
end
|
|
132
134
|
|
|
133
135
|
#
|
|
134
|
-
#
|
|
135
|
-
#
|
|
136
|
-
#
|
|
137
|
-
#
|
|
138
|
-
# A browser that stops reading its socket (hung, or stopped) would block
|
|
139
|
-
# a plain write forever, holding the lock every other command needs to be
|
|
140
|
-
# sent. So the write gives up once the browser accepts no data for the
|
|
141
|
-
# timeout of the command being sent.
|
|
136
|
+
# Collects the raw bytes of a frame. Called by `websocket-driver` to
|
|
137
|
+
# emit frames, one call per frame, always from within the driver call
|
|
138
|
+
# that queues them for the writer thread.
|
|
142
139
|
#
|
|
143
140
|
# @param [String] data
|
|
144
|
-
# The raw bytes
|
|
141
|
+
# The raw bytes of one frame.
|
|
145
142
|
#
|
|
146
143
|
# @return [void]
|
|
147
144
|
#
|
|
148
|
-
# @raise [Ferrum::DeadBrowserError]
|
|
149
|
-
# If the browser accepted no data for the command's timeout.
|
|
150
|
-
#
|
|
151
145
|
def write(data)
|
|
152
|
-
|
|
153
|
-
written = @sock.write_nonblock(data, exception: false)
|
|
154
|
-
if written.is_a?(Integer)
|
|
155
|
-
data = data.byteslice(written..)
|
|
156
|
-
elsif !wait_for_socket(written)
|
|
157
|
-
abandon_stalled_write
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, IOError # rubocop:disable Lint/ShadowedException
|
|
161
|
-
@messages.close
|
|
146
|
+
@encoded << data
|
|
162
147
|
end
|
|
163
148
|
|
|
164
149
|
#
|
|
@@ -167,22 +152,23 @@ module Ferrum
|
|
|
167
152
|
# @return [void]
|
|
168
153
|
#
|
|
169
154
|
def close
|
|
170
|
-
|
|
155
|
+
drive { @driver.close }
|
|
171
156
|
end
|
|
172
157
|
|
|
173
158
|
#
|
|
174
|
-
#
|
|
175
|
-
# completed.
|
|
159
|
+
# Stops the writer and closes the underlying socket whether or not the
|
|
160
|
+
# close handshake ever completed.
|
|
176
161
|
#
|
|
177
|
-
# {#close} only
|
|
178
|
-
# {#on_close}
|
|
179
|
-
# its own. A browser that is already dead never answers, so without this
|
|
162
|
+
# {#close} only queues a close frame; the socket itself is closed by the
|
|
163
|
+
# writer once {#on_close} runs, when the browser answers with a close
|
|
164
|
+
# frame of its own. A browser that is already dead never answers, so without this
|
|
180
165
|
# the socket stays open until GC finalizes it -- long enough to exhaust
|
|
181
166
|
# the process's file descriptors when browsers are restarted in a loop.
|
|
182
167
|
#
|
|
183
168
|
# @return [void]
|
|
184
169
|
#
|
|
185
170
|
def force_close
|
|
171
|
+
@outgoing.close
|
|
186
172
|
@sock.close unless @sock.closed?
|
|
187
173
|
rescue IOError, SystemCallError
|
|
188
174
|
# nop
|
|
@@ -190,16 +176,50 @@ module Ferrum
|
|
|
190
176
|
|
|
191
177
|
private
|
|
192
178
|
|
|
193
|
-
def
|
|
194
|
-
|
|
195
|
-
|
|
179
|
+
def drive(deadline = nil)
|
|
180
|
+
@mutex.synchronize do
|
|
181
|
+
yield
|
|
182
|
+
queue_encoded(deadline)
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def queue_encoded(deadline = nil)
|
|
187
|
+
@encoded.each { |data| @outgoing << [data, deadline] }
|
|
188
|
+
rescue ClosedQueueError
|
|
189
|
+
@messages.close
|
|
190
|
+
ensure
|
|
191
|
+
@encoded.clear
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def start_writer
|
|
195
|
+
@writer = Utils::Thread.spawn do
|
|
196
|
+
while (frame = @outgoing.pop)
|
|
197
|
+
data, deadline = frame
|
|
198
|
+
write_frame(data) unless deadline && Utils::ElapsedTime.monotonic_time > deadline
|
|
199
|
+
end
|
|
200
|
+
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::EBADF, IOError # rubocop:disable Lint/ShadowedException
|
|
201
|
+
# The browser went away, nothing to report.
|
|
202
|
+
rescue StandardError => e
|
|
203
|
+
warn("Ferrum: websocket writer stopped, #{e.class}: #{e.message}\n #{e.backtrace&.first}")
|
|
204
|
+
ensure
|
|
205
|
+
@messages.close
|
|
206
|
+
force_close
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def write_frame(data)
|
|
211
|
+
0.step(data.bytesize - 1, WRITE_CHUNK_SIZE) do |offset|
|
|
212
|
+
watchdog = Concurrent::ScheduledTask.execute(@write_timeout) { abandon_stalled_write } if @write_timeout
|
|
213
|
+
@sock.write(data.byteslice(offset, WRITE_CHUNK_SIZE))
|
|
214
|
+
ensure
|
|
215
|
+
watchdog&.cancel
|
|
216
|
+
end
|
|
196
217
|
end
|
|
197
218
|
|
|
198
219
|
def abandon_stalled_write
|
|
199
|
-
|
|
220
|
+
@close_reason = "Browser accepted no data for #{@write_timeout}s and is likely stuck, connection closed"
|
|
200
221
|
@messages.close
|
|
201
|
-
|
|
202
|
-
raise DeadBrowserError, "Browser accepted no data for #{timeout}s and is likely stuck, connection closed"
|
|
222
|
+
force_close
|
|
203
223
|
end
|
|
204
224
|
|
|
205
225
|
def start
|
|
@@ -208,9 +228,9 @@ module Ferrum
|
|
|
208
228
|
data = @sock.readpartial(512)
|
|
209
229
|
break unless data
|
|
210
230
|
|
|
211
|
-
|
|
231
|
+
drive { @driver.parse(data) }
|
|
212
232
|
end
|
|
213
|
-
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, IOError # rubocop:disable Lint/ShadowedException
|
|
233
|
+
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::EBADF, IOError # rubocop:disable Lint/ShadowedException
|
|
214
234
|
# The browser went away, nothing to report.
|
|
215
235
|
rescue StandardError => e
|
|
216
236
|
warn("Ferrum: websocket reader stopped, #{e.class}: #{e.message}\n #{e.backtrace&.first}")
|
data/lib/ferrum/client.rb
CHANGED
|
@@ -160,7 +160,7 @@ module Ferrum
|
|
|
160
160
|
@ws_url = ws_url
|
|
161
161
|
@options = options
|
|
162
162
|
@pendings = Concurrent::Hash.new
|
|
163
|
-
@ws = WebSocket.new(ws_url, options.ws_max_receive_size, options.
|
|
163
|
+
@ws = WebSocket.new(ws_url, options.ws_max_receive_size, options.log, options.protocol_timeout)
|
|
164
164
|
@subscriber = Subscriber.new
|
|
165
165
|
|
|
166
166
|
start
|
|
@@ -208,8 +208,8 @@ module Ferrum
|
|
|
208
208
|
# Whether to return immediately instead of waiting for a response.
|
|
209
209
|
#
|
|
210
210
|
# @param [Numeric, nil] timeout
|
|
211
|
-
# How long to wait for the
|
|
212
|
-
#
|
|
211
|
+
# How long to wait for the response, counting the time the message waits
|
|
212
|
+
# to be sent. Defaults to `protocol_timeout`.
|
|
213
213
|
#
|
|
214
214
|
# @return [Boolean, Hash]
|
|
215
215
|
# `true` when sent asynchronously, otherwise the parsed `"result"`
|
|
@@ -222,7 +222,7 @@ module Ferrum
|
|
|
222
222
|
@ws.send_message(message, timeout)
|
|
223
223
|
true
|
|
224
224
|
else
|
|
225
|
-
raise
|
|
225
|
+
raise dead_browser_error if @ws.messages.closed?
|
|
226
226
|
|
|
227
227
|
pending = Concurrent::IVar.new
|
|
228
228
|
@pendings[message[:id]] = pending
|
|
@@ -233,7 +233,7 @@ module Ferrum
|
|
|
233
233
|
@pendings.delete(message[:id])
|
|
234
234
|
end
|
|
235
235
|
|
|
236
|
-
raise
|
|
236
|
+
raise dead_browser_error if data.nil? && @ws.messages.closed?
|
|
237
237
|
raise TimeoutError.new(message[:method], session_id: message[:sessionId]) unless data
|
|
238
238
|
|
|
239
239
|
error, response = data.values_at("error", "result")
|
|
@@ -365,6 +365,10 @@ module Ferrum
|
|
|
365
365
|
@pendings.each_value { |pending| pending.try_set(nil) }
|
|
366
366
|
end
|
|
367
367
|
|
|
368
|
+
def dead_browser_error
|
|
369
|
+
@ws.close_reason ? DeadBrowserError.new(@ws.close_reason) : DeadBrowserError.new
|
|
370
|
+
end
|
|
371
|
+
|
|
368
372
|
# Locked so two concurrent commands never share an id and read each
|
|
369
373
|
# other's responses from @pendings.
|
|
370
374
|
def next_command_id
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ferrum
|
|
4
|
+
class Console
|
|
5
|
+
#
|
|
6
|
+
# A place in the page's source a {Message} points at: where `console.*`
|
|
7
|
+
# was called, an exception was thrown or a resource failed to load.
|
|
8
|
+
# Line and column numbers are 1-based.
|
|
9
|
+
#
|
|
10
|
+
class Location
|
|
11
|
+
attr_reader :url, :line, :column, :function_name
|
|
12
|
+
|
|
13
|
+
# Builds a location from a CDP `Runtime.CallFrame`, or from the `url`
|
|
14
|
+
# and `lineNumber` of a `Log.LogEntry`. CDP numbers lines and columns
|
|
15
|
+
# from 0.
|
|
16
|
+
#
|
|
17
|
+
# @param [Hash{String => Object}] frame
|
|
18
|
+
#
|
|
19
|
+
# @return [Location]
|
|
20
|
+
def self.from_frame(frame)
|
|
21
|
+
new(url: frame["url"].to_s,
|
|
22
|
+
line: frame["lineNumber"]&.+(1),
|
|
23
|
+
column: frame["columnNumber"]&.+(1),
|
|
24
|
+
function_name: frame["functionName"].to_s)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def initialize(url:, line: nil, column: nil, function_name: "")
|
|
28
|
+
@url = url
|
|
29
|
+
@line = line
|
|
30
|
+
@column = column
|
|
31
|
+
@function_name = function_name
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The location as `url:line:column`, leaving out what's unknown.
|
|
35
|
+
#
|
|
36
|
+
# @return [String]
|
|
37
|
+
def to_s
|
|
38
|
+
[url, line, column].compact.join(":")
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "ferrum/console/location"
|
|
5
|
+
|
|
6
|
+
module Ferrum
|
|
7
|
+
class Console
|
|
8
|
+
#
|
|
9
|
+
# A message as the DevTools console would show it: a `console.*` call,
|
|
10
|
+
# a browser log entry (failed resource loads, CSP violations,
|
|
11
|
+
# deprecations, ...) or an uncaught exception. The text, arguments and
|
|
12
|
+
# stack trace are only converted from CDP when asked for.
|
|
13
|
+
#
|
|
14
|
+
class Message
|
|
15
|
+
SEVERITIES = { error: :error, assert: :error, warning: :warn,
|
|
16
|
+
debug: :debug, verbose: :debug, trace: :debug }.freeze
|
|
17
|
+
UNSERIALIZABLE = { "NaN" => Float::NAN, "Infinity" => Float::INFINITY,
|
|
18
|
+
"-Infinity" => -Float::INFINITY, "-0" => -0.0 }.freeze
|
|
19
|
+
|
|
20
|
+
# What logged the message: `:log`, `:info`, `:warning`, `:error`,
|
|
21
|
+
# `:debug`, `:table`, `:trace`, `:assert`, ... for `console.*` calls,
|
|
22
|
+
# `:verbose`, `:info`, `:warning` or `:error` for browser log entries
|
|
23
|
+
# and `:error` for uncaught exceptions.
|
|
24
|
+
#
|
|
25
|
+
# @return [Symbol]
|
|
26
|
+
attr_reader :type
|
|
27
|
+
|
|
28
|
+
# Where the message comes from: `:console_api` for `console.*` calls,
|
|
29
|
+
# `:exception` for uncaught exceptions, otherwise the browser log
|
|
30
|
+
# entry's source such as `:network`, `:security`, `:violation`,
|
|
31
|
+
# `:deprecation` or `:intervention`.
|
|
32
|
+
#
|
|
33
|
+
# @return [Symbol]
|
|
34
|
+
attr_reader :source
|
|
35
|
+
|
|
36
|
+
# When the message was logged.
|
|
37
|
+
#
|
|
38
|
+
# @return [Time]
|
|
39
|
+
attr_reader :timestamp
|
|
40
|
+
|
|
41
|
+
# Builds a message from `Runtime.consoleAPICalled` params.
|
|
42
|
+
#
|
|
43
|
+
# @param [Hash{String => Object}] params
|
|
44
|
+
#
|
|
45
|
+
# @return [Message]
|
|
46
|
+
def self.from_console_api(params)
|
|
47
|
+
new(type: params["type"], source: "console_api", timestamp: params["timestamp"],
|
|
48
|
+
raw_args: params["args"], frames: params.dig("stackTrace", "callFrames"))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Builds a message from a `Log.entryAdded` entry.
|
|
52
|
+
#
|
|
53
|
+
# @param [Hash{String => Object}] entry
|
|
54
|
+
#
|
|
55
|
+
# @return [Message]
|
|
56
|
+
def self.from_log_entry(entry)
|
|
57
|
+
frames = entry.dig("stackTrace", "callFrames")
|
|
58
|
+
frames ||= [entry.slice("url", "lineNumber")] if entry["url"]
|
|
59
|
+
new(type: entry["level"], source: entry["source"], timestamp: entry["timestamp"],
|
|
60
|
+
raw_text: entry["text"], frames: frames)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Builds a message from `Runtime.exceptionThrown` params.
|
|
64
|
+
#
|
|
65
|
+
# @param [Hash{String => Object}] params
|
|
66
|
+
#
|
|
67
|
+
# @return [Message]
|
|
68
|
+
def self.from_exception(params)
|
|
69
|
+
details = params["exceptionDetails"]
|
|
70
|
+
frames = details.dig("stackTrace", "callFrames")
|
|
71
|
+
frames ||= [details.slice("url", "lineNumber", "columnNumber")]
|
|
72
|
+
text = [details["text"], details.dig("exception", "description")].compact.uniq.join(" ")
|
|
73
|
+
new(type: "error", source: "exception", timestamp: params["timestamp"], raw_text: text, frames: frames)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def initialize(type:, source:, timestamp:, raw_args: nil, raw_text: nil, frames: nil)
|
|
77
|
+
@type = underscore(type)
|
|
78
|
+
@source = underscore(source)
|
|
79
|
+
@timestamp = Time.at(0, timestamp.to_f, :millisecond)
|
|
80
|
+
@raw_args = raw_args || []
|
|
81
|
+
@raw_text = raw_text
|
|
82
|
+
@frames = frames || []
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The message text. `console.*` arguments are joined by spaces, with
|
|
86
|
+
# objects and arrays rendered as JSON.
|
|
87
|
+
#
|
|
88
|
+
# @return [String]
|
|
89
|
+
def text
|
|
90
|
+
@text ||= @raw_text || @raw_args.map { |arg| arg_text(arg) }.join(" ")
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The `console.*` arguments as Ruby values. Strings, numbers,
|
|
94
|
+
# booleans and `null` come as they are, `undefined` is `nil`, plain
|
|
95
|
+
# objects and arrays are a `Hash` or an `Array` built from Chrome's
|
|
96
|
+
# shallow preview, anything else (functions, DOM nodes, errors, ...) is
|
|
97
|
+
# its description. Empty for log entries and exceptions.
|
|
98
|
+
#
|
|
99
|
+
# @return [Array<Object>]
|
|
100
|
+
def args
|
|
101
|
+
@args ||= @raw_args.map { |arg| arg_value(arg) }
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# The call stack at the moment the message was logged, innermost frame
|
|
105
|
+
# first. Can be empty.
|
|
106
|
+
#
|
|
107
|
+
# @return [Array<Location>]
|
|
108
|
+
def stack_trace
|
|
109
|
+
@stack_trace ||= @frames.map { |frame| Location.from_frame(frame) }
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Where the message was logged from.
|
|
113
|
+
#
|
|
114
|
+
# @return [Location, nil]
|
|
115
|
+
def location
|
|
116
|
+
stack_trace.first
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# The {#type} expressed as a `Logger` method name, so a message can be
|
|
120
|
+
# passed on with `logger.public_send(message.severity, message)`.
|
|
121
|
+
#
|
|
122
|
+
# @return [Symbol]
|
|
123
|
+
# `:debug`, `:info`, `:warn` or `:error`.
|
|
124
|
+
def severity
|
|
125
|
+
SEVERITIES.fetch(type, :info)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Whether the message is an error: `console.error`, a failed
|
|
129
|
+
# `console.assert`, an error log entry or an uncaught exception.
|
|
130
|
+
#
|
|
131
|
+
# @return [Boolean]
|
|
132
|
+
def error?
|
|
133
|
+
severity == :error
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Whether the message is a warning.
|
|
137
|
+
#
|
|
138
|
+
# @return [Boolean]
|
|
139
|
+
def warning?
|
|
140
|
+
severity == :warn
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Whether the message text matches the given pattern.
|
|
144
|
+
#
|
|
145
|
+
# @param [Regexp, String] pattern
|
|
146
|
+
# A regexp matched against the text, or a string checked for inclusion in it.
|
|
147
|
+
#
|
|
148
|
+
# @return [Boolean]
|
|
149
|
+
def match?(pattern)
|
|
150
|
+
return text.match?(pattern) if pattern.is_a?(Regexp)
|
|
151
|
+
|
|
152
|
+
text.include?(pattern.to_s)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# The message as the logger prints it: `[type] text`, followed by an
|
|
156
|
+
# `at function (url:line:column)` line per stack frame.
|
|
157
|
+
#
|
|
158
|
+
# @return [String]
|
|
159
|
+
def to_s
|
|
160
|
+
lines = stack_trace.map { |frame| " at #{frame.function_name} (#{frame})" }
|
|
161
|
+
["[#{type}] #{text}", *lines].join("\n")
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Debug representation of the message.
|
|
165
|
+
#
|
|
166
|
+
# @return [String]
|
|
167
|
+
def inspect
|
|
168
|
+
"#<#{self.class.name} type=#{type.inspect} source=#{source.inspect} text=#{text.inspect}>"
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
private
|
|
172
|
+
|
|
173
|
+
def underscore(name)
|
|
174
|
+
name.to_s.gsub(/(?<=[a-z])(?=[A-Z])|-/, "_").downcase.to_sym
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def arg_text(arg)
|
|
178
|
+
value = arg_value(arg)
|
|
179
|
+
|
|
180
|
+
case value
|
|
181
|
+
when String then value
|
|
182
|
+
when Hash, Array then JSON.generate(value)
|
|
183
|
+
when nil then arg["subtype"] == "null" ? "null" : "undefined"
|
|
184
|
+
else arg["description"] || value.to_s
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def arg_value(arg)
|
|
189
|
+
return arg["value"] if arg.key?("value")
|
|
190
|
+
return unserializable(arg["unserializableValue"]) if arg.key?("unserializableValue")
|
|
191
|
+
return if arg["type"] == "undefined"
|
|
192
|
+
|
|
193
|
+
preview_value(arg["preview"]) || arg["description"]
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def preview_value(preview)
|
|
197
|
+
return unless preview && preview["type"] == "object"
|
|
198
|
+
|
|
199
|
+
case preview["subtype"]
|
|
200
|
+
when nil then preview["properties"].to_h { |property| [property["name"], property_value(property)] }
|
|
201
|
+
when "array" then preview["properties"].map { |property| property_value(property) }
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def property_value(property)
|
|
206
|
+
return preview_value(property["valuePreview"]) || property["value"] if property["valuePreview"]
|
|
207
|
+
|
|
208
|
+
value = property["value"]
|
|
209
|
+
case property["type"]
|
|
210
|
+
when "number" then parse_number(value)
|
|
211
|
+
when "bigint" then unserializable(value)
|
|
212
|
+
when "boolean" then value == "true"
|
|
213
|
+
when "undefined" then nil
|
|
214
|
+
when "object" then property["subtype"] == "null" ? nil : value
|
|
215
|
+
else value
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def unserializable(value)
|
|
220
|
+
UNSERIALIZABLE.fetch(value) { value.end_with?("n") ? Integer(value.delete_suffix("n")) : value }
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def parse_number(value)
|
|
224
|
+
return UNSERIALIZABLE[value] if UNSERIALIZABLE.key?(value)
|
|
225
|
+
|
|
226
|
+
Integer(value, exception: false) || Float(value, exception: false) || value
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|