ferrum 0.18.0 → 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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +63 -1
  3. data/lib/ferrum/browser/binary.rb +1 -3
  4. data/lib/ferrum/browser/command.rb +0 -2
  5. data/lib/ferrum/browser/options/base.rb +5 -6
  6. data/lib/ferrum/browser/options/chrome.rb +0 -3
  7. data/lib/ferrum/browser/options.rb +33 -12
  8. data/lib/ferrum/browser/process/killer.rb +188 -0
  9. data/lib/ferrum/browser/process/output.rb +143 -0
  10. data/lib/ferrum/browser/process.rb +122 -144
  11. data/lib/ferrum/browser.rb +24 -30
  12. data/lib/ferrum/client/subscriber.rb +7 -2
  13. data/lib/ferrum/client/web_socket.rb +124 -43
  14. data/lib/ferrum/client.rb +36 -9
  15. data/lib/ferrum/console/location.rb +42 -0
  16. data/lib/ferrum/console/message.rb +230 -0
  17. data/lib/ferrum/console.rb +157 -0
  18. data/lib/ferrum/context.rb +41 -20
  19. data/lib/ferrum/contexts.rb +110 -28
  20. data/lib/ferrum/downloads.rb +2 -1
  21. data/lib/ferrum/errors.rb +50 -4
  22. data/lib/ferrum/frame/dom.rb +100 -45
  23. data/lib/ferrum/frame/runtime/legacy.rb +336 -0
  24. data/lib/ferrum/frame/runtime.rb +417 -186
  25. data/lib/ferrum/frame/script.rb +289 -0
  26. data/lib/ferrum/frame.rb +33 -4
  27. data/lib/ferrum/interceptable.rb +35 -4
  28. data/lib/ferrum/isolated_world.rb +94 -0
  29. data/lib/ferrum/keyboard.rb +95 -8
  30. data/lib/ferrum/log/annotated.rb +77 -0
  31. data/lib/ferrum/log/default.rb +55 -0
  32. data/lib/ferrum/log/json.rb +32 -0
  33. data/lib/ferrum/log/paint.rb +28 -0
  34. data/lib/ferrum/log.rb +247 -0
  35. data/lib/ferrum/mouse.rb +22 -8
  36. data/lib/ferrum/node.rb +127 -52
  37. data/lib/ferrum/page/frames.rb +10 -3
  38. data/lib/ferrum/page/screenshot.rb +11 -10
  39. data/lib/ferrum/page/stream.rb +16 -1
  40. data/lib/ferrum/page.rb +117 -58
  41. data/lib/ferrum/proxy.rb +8 -7
  42. data/lib/ferrum/remote_object.rb +75 -0
  43. data/lib/ferrum/target.rb +14 -3
  44. data/lib/ferrum/utils/deprecate.rb +77 -0
  45. data/lib/ferrum/utils/thread.rb +3 -1
  46. data/lib/ferrum/version.rb +1 -1
  47. data/lib/ferrum/worker.rb +8 -0
  48. data/lib/ferrum.rb +4 -1
  49. metadata +18 -4
  50. data/lib/ferrum/browser/options/firefox.rb +0 -70
@@ -12,18 +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
- SKIP_LOGGING_SCREENSHOTS = !ENV["FERRUM_LOGGING_SCREENSHOTS"]
27
+ WRITE_CHUNK_SIZE = 64 * 1024
28
+ ESCAPE_OR_LONE_SURROGATE = /\\u[dD][89abAB]\h{2}\\u[dD][c-fC-F]\h{2}|\\u([dD][89a-fA-F]\h{2})|\\./
19
29
 
20
30
  attr_reader :url, :messages
21
31
 
22
- def initialize(url, max_receive_size, logger)
23
- @url = url
24
- @logger = logger
25
- uri = URI.parse(@url)
26
- port = uri.port || DEFAULT_PORTS[uri.scheme]
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]
27
42
 
28
43
  if port == 443 || url.scheme == "wss"
29
44
  tcp = TCPSocket.new(uri.host, port)
@@ -39,18 +54,19 @@ module Ferrum
39
54
  @driver = ::WebSocket::Driver.client(self, max_length: max_receive_size)
40
55
  # websocket-driver holds no locks and is called from many threads: commands from
41
56
  # callers, pong/close replies from the reader. One lock keeps frames from interleaving.
42
- @driver_mutex = Mutex.new
57
+ @mutex = Mutex.new
58
+ @encoded = []
43
59
  @messages = Queue.new
44
-
45
- @screenshot_commands = Concurrent::Hash.new if SKIP_LOGGING_SCREENSHOTS
60
+ @outgoing = Queue.new
46
61
 
47
62
  @driver.on(:open, &method(:on_open))
48
63
  @driver.on(:message, &method(:on_message))
49
64
  @driver.on(:close, &method(:on_close))
50
65
 
51
66
  start
67
+ start_writer
52
68
 
53
- @driver_mutex.synchronize { @driver.start }
69
+ drive { @driver.start }
54
70
  end
55
71
 
56
72
  #
@@ -72,13 +88,7 @@ module Ferrum
72
88
  #
73
89
  def on_message(event)
74
90
  data = safely_parse_json(event.data)
75
- output = event.data
76
- if SKIP_LOGGING_SCREENSHOTS && @screenshot_commands[data&.dig("id")]
77
- @screenshot_commands.delete(data&.dig("id"))
78
- output.sub!(/{"data":"[^"]*"}/, %("Set FERRUM_LOGGING_SCREENSHOTS=true to see screenshots in Base64"))
79
- end
80
-
81
- @logger&.puts(" ◀ #{Utils::ElapsedTime.elapsed_time} #{output}\n")
91
+ @log&.received(data, event.data, self)
82
92
 
83
93
  # If we couldn't parse JSON data for some reason (parse error or deeply nested object) we
84
94
  # don't push response to @messages. Worse that could happen we raise timeout error due to command didn't return
@@ -88,48 +98,52 @@ module Ferrum
88
98
  end
89
99
 
90
100
  #
91
- # Handles the driver's `:close` event: closes the message queue and
92
- # underlying socket, then kills the reader thread.
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.
93
104
  #
94
105
  # @return [void]
95
106
  #
96
107
  def on_close(_event)
97
108
  @messages.close
98
- @sock.close
109
+ queue_encoded
110
+ @outgoing.close
99
111
  @thread.kill
100
112
  end
101
113
 
102
114
  #
103
- # Serializes a CDP command to JSON and sends it as a websocket text
104
- # 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.
105
117
  #
106
118
  # @param [Hash] data
107
119
  # The message to send, must include an `:id` key.
108
120
  #
121
+ # @param [Numeric, nil] timeout
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.
125
+ #
109
126
  # @return [void]
110
127
  #
111
- def send_message(data)
112
- @screenshot_commands[data[:id]] = true if SKIP_LOGGING_SCREENSHOTS
113
-
128
+ def send_message(data, timeout = nil)
114
129
  json = data.to_json
115
- @driver_mutex.synchronize { @driver.text(json) }
116
- @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)
117
133
  end
118
134
 
119
135
  #
120
- # Writes raw bytes to the underlying socket. Called by
121
- # `websocket-driver` to emit frames. Closes {#messages} instead of
122
- # raising if the connection has already been torn down.
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.
123
139
  #
124
140
  # @param [String] data
125
- # The raw bytes to write.
141
+ # The raw bytes of one frame.
126
142
  #
127
143
  # @return [void]
128
144
  #
129
145
  def write(data)
130
- @sock.write(data)
131
- rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, IOError # rubocop:disable Lint/ShadowedException
132
- @messages.close
146
+ @encoded << data
133
147
  end
134
148
 
135
149
  #
@@ -138,20 +152,89 @@ module Ferrum
138
152
  # @return [void]
139
153
  #
140
154
  def close
141
- @driver_mutex.synchronize { @driver.close }
155
+ drive { @driver.close }
156
+ end
157
+
158
+ #
159
+ # Stops the writer and closes the underlying socket whether or not the
160
+ # close handshake ever completed.
161
+ #
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
165
+ # the socket stays open until GC finalizes it -- long enough to exhaust
166
+ # the process's file descriptors when browsers are restarted in a loop.
167
+ #
168
+ # @return [void]
169
+ #
170
+ def force_close
171
+ @outgoing.close
172
+ @sock.close unless @sock.closed?
173
+ rescue IOError, SystemCallError
174
+ # nop
142
175
  end
143
176
 
144
177
  private
145
178
 
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
217
+ end
218
+
219
+ def abandon_stalled_write
220
+ @close_reason = "Browser accepted no data for #{@write_timeout}s and is likely stuck, connection closed"
221
+ @messages.close
222
+ force_close
223
+ end
224
+
146
225
  def start
147
226
  @thread = Utils::Thread.spawn do
148
227
  loop do
149
228
  data = @sock.readpartial(512)
150
229
  break unless data
151
230
 
152
- @driver_mutex.synchronize { @driver.parse(data) }
231
+ drive { @driver.parse(data) }
153
232
  end
154
- 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
234
+ # The browser went away, nothing to report.
235
+ rescue StandardError => e
236
+ warn("Ferrum: websocket reader stopped, #{e.class}: #{e.message}\n #{e.backtrace&.first}")
237
+ ensure
155
238
  @messages.close
156
239
  end
157
240
  end
@@ -161,14 +244,12 @@ module Ferrum
161
244
  rescue JSON::NestingError
162
245
  # nop
163
246
  rescue JSON::ParserError
164
- safely_parse_escaped_json(data)
247
+ safely_parse_lone_surrogates(data)
165
248
  end
166
249
 
167
- def safely_parse_escaped_json(data)
168
- unescaped_unicode =
169
- data.gsub(/\\u([\da-fA-F]{4})/) { |_| [::Regexp.last_match(1)].pack("H*").unpack("n*").pack("U*") }
170
- escaped_data = unescaped_unicode.encode("UTF-8", "UTF-8", undef: :replace, invalid: :replace, replace: "?")
171
- JSON.parse(escaped_data, max_nesting: false)
250
+ def safely_parse_lone_surrogates(data)
251
+ replaced = data.gsub(ESCAPE_OR_LONE_SURROGATE) { |escape| ::Regexp.last_match(1) ? "\\ufffd" : escape }
252
+ JSON.parse(replaced, max_nesting: false)
172
253
  rescue JSON::ParserError
173
254
  # nop
174
255
  end
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.logger)
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,25 +208,33 @@ 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 response. Defaults to `protocol_timeout`.
211
+ # How long to wait for the response, counting the time the message waits
212
+ # to be sent. Defaults to `protocol_timeout`.
212
213
  #
213
214
  # @return [Boolean, Hash]
214
215
  # `true` when sent asynchronously, otherwise the parsed `"result"`
215
216
  # from the response.
216
217
  #
217
218
  def send_message(message, async:, timeout: nil)
219
+ timeout ||= protocol_timeout
220
+
218
221
  if async
219
- @ws.send_message(message)
222
+ @ws.send_message(message, timeout)
220
223
  true
221
224
  else
225
+ raise dead_browser_error if @ws.messages.closed?
226
+
222
227
  pending = Concurrent::IVar.new
223
228
  @pendings[message[:id]] = pending
224
- @ws.send_message(message)
225
- data = pending.value!(timeout || protocol_timeout)
226
- @pendings.delete(message[:id])
229
+ begin
230
+ @ws.send_message(message, timeout)
231
+ data = pending.value!(timeout)
232
+ ensure
233
+ @pendings.delete(message[:id])
234
+ end
227
235
 
228
- raise DeadBrowserError if data.nil? && @ws.messages.closed?
229
- raise TimeoutError unless data
236
+ raise dead_browser_error if data.nil? && @ws.messages.closed?
237
+ raise TimeoutError.new(message[:method], session_id: message[:sessionId]) unless data
230
238
 
231
239
  error, response = data.values_at("error", "result")
232
240
  raise_browser_error(error) if error
@@ -297,6 +305,7 @@ module Ferrum
297
305
  @pendings.clear
298
306
  @thread.kill unless @thread.join(1)
299
307
  @subscriber.close
308
+ @ws.force_close
300
309
  end
301
310
 
302
311
  #
@@ -340,9 +349,26 @@ module Ferrum
340
349
  @pendings[message["id"]]&.set(message)
341
350
  end
342
351
  end
352
+ rescue StandardError => e
353
+ # Nothing delivers responses once this thread is gone, so the connection
354
+ # is done for. Close it rather than leave every command hanging.
355
+ warn("Ferrum: dispatch stopped, #{e.class}: #{e.message}\n #{e.backtrace&.first}")
356
+ @ws.messages.close
357
+ ensure
358
+ release_pendings
343
359
  end
344
360
  end
345
361
 
362
+ # Nothing is going to answer the commands still in flight once the socket
363
+ # is gone, release them instead of making each wait out its timeout.
364
+ def release_pendings
365
+ @pendings.each_value { |pending| pending.try_set(nil) }
366
+ end
367
+
368
+ def dead_browser_error
369
+ @ws.close_reason ? DeadBrowserError.new(@ws.close_reason) : DeadBrowserError.new
370
+ end
371
+
346
372
  # Locked so two concurrent commands never share an id and read each
347
373
  # other's responses from @pendings.
348
374
  def next_command_id
@@ -354,7 +380,8 @@ module Ferrum
354
380
  # Node has disappeared while we were trying to get it
355
381
  when "No node with given id found",
356
382
  "Could not find node with given id",
357
- "Inspected target navigated or closed"
383
+ "Inspected target navigated or closed",
384
+ "Argument should belong to the same JavaScript world as target object"
358
385
  raise NodeNotFoundError, error
359
386
  # Context is lost, page is reloading
360
387
  when "Cannot find context with specified 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