Merge pull request #75 from redbaron2k7/main

add network request tracking for fetch requests in devtools
This commit is contained in:
Face
2025-09-11 18:20:18 +03:00
committed by GitHub
4 changed files with 50 additions and 4 deletions

View File

@@ -122,3 +122,20 @@ func get_request_stats() -> Dictionary:
"pending": pending_requests,
"total_size": total_size
}
func add_completed_request(url: String, method: String, is_from_lua: bool, status_code: int, status_text: String, response_headers: Dictionary, response_body: String, body_bytes: PackedByteArray = [], request_headers: Dictionary = {}, request_body: String = "", time_ms: float = 0.0):
var request = NetworkRequest.new(url, method)
request.is_from_lua = is_from_lua
request.request_headers = request_headers
request.request_body = request_body
if time_ms > 0.0:
request.start_time = Time.get_ticks_msec() - time_ms
request.set_response(status_code, status_text, response_headers, response_body, body_bytes)
all_requests.append(request)
if dev_tools_network_tab:
dev_tools_network_tab.add_network_request(request)

View File

@@ -27,12 +27,32 @@ func _ready():
func init(network_request: NetworkRequest, parent_tab: NetworkTab):
request = network_request
network_tab = parent_tab
update_display()
if is_node_ready():
update_display()
else:
call_deferred("update_display")
func update_display():
if not request:
return
if icon == null:
icon = get_node_or_null("HBoxContainer/IconContainer/Icon") as TextureRect
if name_label == null:
name_label = get_node_or_null("HBoxContainer/NameLabel") as Label
if status_label == null:
status_label = get_node_or_null("HBoxContainer/StatusLabel") as Label
if type_label == null:
type_label = get_node_or_null("HBoxContainer/TypeLabel") as Label
if size_label == null:
size_label = get_node_or_null("HBoxContainer/SizeLabel") as Label
if time_label == null:
time_label = get_node_or_null("HBoxContainer/TimeLabel") as Label
if icon == null or name_label == null or status_label == null or type_label == null or size_label == null or time_label == null:
call_deferred("update_display")
return
# Update icon
icon.texture = request.get_icon_texture()

View File

@@ -46,6 +46,8 @@ func _ready():
update_status_bar()
NetworkManager.register_dev_tools_network_tab(self)
for req in NetworkManager.get_all_requests():
add_network_request(req)
func add_network_request(request: NetworkRequest):
network_requests.append(request)

View File

@@ -45,17 +45,24 @@ static func _lua_fetch_handler(vm: LuauVM) -> int:
# Set request options
var headers_array: PackedStringArray = []
var headers_dict = {}
var has_user_agent = false
for header_name in headers:
if str(header_name).to_lower() == "user-agent":
has_user_agent = true
headers_array.append(str(header_name) + ": " + str(headers[header_name]))
headers_dict[str(header_name)] = str(headers[header_name])
if not has_user_agent:
headers_array.append("User-Agent: " + UserAgent.get_user_agent())
var ua_value = UserAgent.get_user_agent()
headers_array.append("User-Agent: " + ua_value)
headers_dict["User-Agent"] = ua_value
var response_data = make_http_request(url, method, headers_array, body)
var start_ms = Time.get_ticks_msec()
var response_data = make_http_request(url, method, headers_array, str(body))
var elapsed_ms = Time.get_ticks_msec() - start_ms
NetworkManager.call_deferred("add_completed_request", url, method, true, int(response_data.status), str(response_data.status_text), response_data.headers, str(response_data.body), [], headers_dict, str(body), float(elapsed_ms))
# Create response object with actual data
vm.lua_newtable()