Skip to content

Commit fd75728

Browse files
committed
Support BasicObject in Chrome debugging
1 parent 7f18c82 commit fd75728

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

lib/debug/server.rb

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require 'etc'
55
require_relative 'config'
66
require_relative 'version'
7+
require 'json'
8+
require 'securerandom'
79

810
module DEBUGGER__
911
class UI_ServerBase < UI_Base
@@ -22,6 +24,7 @@ def initialize
2224

2325
class Terminate < StandardError; end
2426
class GreetingError < StandardError; end
27+
class RetryConnection < StandardError; end
2528

2629
def deactivate
2730
@reader_thread.raise Terminate
@@ -78,6 +81,8 @@ def activate session, on_fork: false
7881
next
7982
rescue Terminate
8083
raise # should catch at outer scope
84+
rescue RetryConnection
85+
next
8186
rescue => e
8287
DEBUGGER__.warn "ReaderThreadError: #{e}"
8388
pp e.backtrace
@@ -157,7 +162,30 @@ def greeting
157162
@need_pause_at_first = false
158163
dap_setup @sock.read($1.to_i)
159164

160-
when /^GET \/.* HTTP\/1.1/
165+
when /^GET \/json\/version HTTP\/1.1/
166+
167+
body = {
168+
Browser: "ruby/v#{RUBY_VERSION}",
169+
'Protocol-Version': "1.1"
170+
}
171+
send_http_res body
172+
raise RetryConnection
173+
174+
when /^GET \/json HTTP\/1.1/
175+
addr = @local_addr.inspect_sockaddr
176+
body = [{
177+
description: "ruby instance",
178+
devtoolsFrontendUrl: "devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=#{addr}/#{@uuid}",
179+
id: @uuid,
180+
title: $0,
181+
type: "json",
182+
url: "file://#{File.absolute_path($0)}",
183+
webSocketDebuggerUrl: "ws://#{addr}/#{@uuid}"
184+
}]
185+
send_http_res body
186+
raise RetryConnection
187+
188+
when /^GET \/#{@uuid} HTTP\/1.1/
161189
require_relative 'server_cdp'
162190

163191
self.extend(UI_CDP)
@@ -172,6 +200,12 @@ def greeting
172200
end
173201
end
174202

203+
def send_http_res body
204+
json = JSON.generate body
205+
header = "HTTP/1.0 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\nCache-Control: no-cache\r\nContent-Length: #{json.bytesize}\r\n\r\n"
206+
@sock.puts "#{header}#{json}"
207+
end
208+
175209
def process
176210
while true
177211
DEBUGGER__.info "sleep IO.select"
@@ -395,18 +429,19 @@ def initialize host: nil, port: nil
395429
raise "Specify digits for port number"
396430
end
397431
end
432+
@uuid = SecureRandom.uuid # for CDP
398433

399434
super()
400435
end
401436

402437
def chrome_setup
403438
require_relative 'server_cdp'
404439

405-
unless @chrome_pid = UI_CDP.setup_chrome(@local_addr.inspect_sockaddr)
440+
unless @chrome_pid = UI_CDP.setup_chrome(@local_addr.inspect_sockaddr, @uuid)
406441
DEBUGGER__.warn <<~EOS
407442
With Chrome browser, type the following URL in the address-bar:
408443
409-
devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&ws=#{@local_addr.inspect_sockaddr}/#{SecureRandom.uuid}
444+
devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&ws=#{@local_addr.inspect_sockaddr}/#{@uuid}
410445
411446
EOS
412447
end

lib/debug/server_cdp.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module UI_CDP
1313
SHOW_PROTOCOL = ENV['RUBY_DEBUG_CDP_SHOW_PROTOCOL'] == '1'
1414

1515
class << self
16-
def setup_chrome addr
16+
def setup_chrome addr, uuid
1717
return if CONFIG[:chrome_path] == ''
1818

1919
port, path, pid = run_new_chrome
@@ -51,7 +51,7 @@ def setup_chrome addr
5151
ws_client.send sessionId: s_id, id: 5,
5252
method: 'Page.navigate',
5353
params: {
54-
url: "devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&ws=#{addr}/#{SecureRandom.uuid}",
54+
url: "devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&ws=#{addr}/#{uuid}",
5555
frameId: f_id
5656
}
5757
when res['method'] == 'Page.loadEventFired'

0 commit comments

Comments
 (0)