feat(debugger): Network tab for a debugged page (#3315)

A page session gets a Network tab in the Debug tool window: the page's requests with status, method, type, size and time, filtered by URL, resource kind and My code only (a request sent through a library is attributed to the project code that called it). A request shows its headers, response body (text, cut at 1 MB) and initiator; Break on Next Request turns it into a request breakpoint, and Reload Page loads the page again. js-debug's enableNetworking is asked once the page's launch is answered; a js-debug that doesn't report requests says so in the tab.

Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com>
This commit is contained in:
ntbang0901
2026-11-14 16:37:23 +08:00
committed by GitHub
parent fecbf8ed14
commit f0f15366e4
24 changed files with 1564 additions and 6 deletions
+50
View File
@@ -291,6 +291,56 @@ pub enum DebugEvent {
JvmBackend {
state: JvmBackendState,
},
/// A step in the life of one request of a debugged page.
JsNetwork {
event: JsNetworkEvent,
},
/// The adapter does not report the requests of this page: there will be no `JsNetwork`.
JsNetworkUnavailable,
}
/// Which step of a request a [`JsNetworkEvent`] reports.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum JsNetworkKind {
Request,
Response,
Finished,
Failed,
}
/// The script frame that made a request, where the browser ran it (0-based line and column).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct JsInitiator {
pub function: String,
pub url: String,
pub line: u32,
pub column: u32,
}
/// What a debugged page's browser reports about a request, reduced to what the Network view
/// shows. Events of one request share `request_id`; each kind fills its own fields.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct JsNetworkEvent {
pub kind: JsNetworkKind,
pub request_id: String,
/// The browser's monotonic clock, comparable between the events of a page.
pub timestamp_ms: u64,
/// Request and response.
pub url: Option<String>,
pub method: Option<String>,
/// `Fetch`, `XHR`, `Script`, `Document`, …
pub resource_type: Option<String>,
pub status: Option<u16>,
pub mime_type: Option<String>,
/// The request's headers, or the response's.
pub headers: BTreeMap<String, String>,
/// Bytes received over the network, once finished.
pub encoded_length: Option<u64>,
pub error_text: Option<String>,
pub initiator: Option<JsInitiator>,
}
/// The backend that reads a debugged JVM's objects (instances, fields, referrers).
+225
View File
@@ -5,8 +5,12 @@
use crate::session::Session;
use crate::{DebugManager, DebugSessionId, REQUEST_TIMEOUT};
use ide_core::event::{JsInitiator, JsNetworkEvent, JsNetworkKind};
use ide_core::{IdeError, IdeResult};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use specta::Type;
use std::collections::BTreeMap;
pub(crate) const ADAPTER: &str = "js-debug";
@@ -56,3 +60,224 @@ impl DebugManager {
Ok(())
}
}
/// The browser events js-debug is asked to pass on (`enableNetworking`'s `mirrorEvents`).
pub(crate) const NETWORK_EVENTS: [&str; 4] =
["requestWillBeSent", "responseReceived", "loadingFinished", "loadingFailed"];
/// Response bodies shown are cut at this many bytes.
pub const RESPONSE_BODY_LIMIT: usize = 1_048_576;
/// The body of a response, as text or as the browser's base64 for binary content.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct JsResponseBody {
pub body: String,
pub base64_encoded: bool,
/// The body was longer than [`RESPONSE_BODY_LIMIT`] and is cut there.
pub truncated: bool,
}
fn text(v: &Value, key: &str) -> Option<String> {
v.get(key).and_then(Value::as_str).map(str::to_owned)
}
fn headers(v: &Value) -> BTreeMap<String, String> {
v.get("headers")
.and_then(Value::as_object)
.map(|h| h.iter().filter_map(|(k, v)| Some((k.clone(), v.as_str()?.to_owned()))).collect())
.unwrap_or_default()
}
/// The frame a request is attributed to: the innermost one outside `node_modules`, looked
/// for through the stacks that scheduled this one (an `await`, a timer), since a request sent
/// through a library has the library innermost. A stack of library frames only gives its first.
fn initiator(data: &Value) -> Option<JsInitiator> {
let stack = data.pointer("/initiator/stack")?;
let frames = |stack: &Value| stack.get("callFrames").and_then(Value::as_array).cloned().unwrap_or_default();
let stacks = std::iter::successors(Some(stack), |stack| stack.get("parent")).take(32);
let all: Vec<Value> = stacks.flat_map(frames).collect();
let own = |frame: &&Value| {
frame.get("url").and_then(Value::as_str).is_some_and(|url| !url.is_empty() && !url.contains("/node_modules/"))
};
let frame = all.iter().find(own).or(all.first())?;
let number = |key: &str| frame.get(key).and_then(Value::as_u64).and_then(|n| u32::try_from(n).ok()).unwrap_or(0);
Some(JsInitiator {
function: text(frame, "functionName").unwrap_or_default(),
url: text(frame, "url")?,
line: number("lineNumber"),
column: number("columnNumber"),
})
}
/// js-debug's `networkEvent` (`{event, data}`, `data` being the browser's own parameters of
/// that event) as a [`JsNetworkEvent`]; `None` for events the Network view has no use for.
pub(crate) fn network_event(body: &Value) -> Option<JsNetworkEvent> {
let data = body.get("data")?;
let kind = match body.get("event").and_then(Value::as_str)? {
"requestWillBeSent" => JsNetworkKind::Request,
"responseReceived" => JsNetworkKind::Response,
"loadingFinished" => JsNetworkKind::Finished,
"loadingFailed" => JsNetworkKind::Failed,
_ => return None,
};
let part = match kind {
JsNetworkKind::Request => data.get("request"),
JsNetworkKind::Response => data.get("response"),
_ => None,
};
let part = part.unwrap_or(&Value::Null);
Some(JsNetworkEvent {
kind,
request_id: text(data, "requestId")?,
// Seconds with a fraction.
timestamp_ms: (data.get("timestamp").and_then(Value::as_f64).unwrap_or(0.0).max(0.0) * 1000.0).round() as u64,
url: text(part, "url"),
method: text(part, "method"),
resource_type: text(data, "type"),
status: part.get("status").and_then(Value::as_u64).and_then(|s| u16::try_from(s).ok()),
mime_type: text(part, "mimeType"),
headers: headers(part),
encoded_length: data.get("encodedDataLength").and_then(Value::as_f64).map(|n| n.max(0.0) as u64),
error_text: text(data, "errorText"),
initiator: if kind == JsNetworkKind::Request { initiator(data) } else { None },
})
}
/// `networkCall getResponseBody`'s answer, cut at `limit` bytes on a character boundary.
fn response_body(answer: &Value, limit: usize) -> JsResponseBody {
let mut body = text(answer, "body").unwrap_or_default();
let truncated = body.len() > limit;
if truncated {
let mut end = limit;
while !body.is_char_boundary(end) {
end -= 1;
}
body.truncate(end);
}
JsResponseBody {
body,
base64_encoded: answer.get("base64Encoded").and_then(Value::as_bool).unwrap_or(false),
truncated,
}
}
/// The arguments of `enableNetworking`, which has a page report its requests
/// ([`ide_core::event::DebugEvent::JsNetwork`]). A page session sends it as it starts.
pub(crate) fn networking_arguments() -> Value {
json!({ "mirrorEvents": NETWORK_EVENTS })
}
impl DebugManager {
/// The body of the response to `request_id`, while the browser still has it.
pub async fn js_response_body(&self, s: DebugSessionId, request_id: String) -> IdeResult<JsResponseBody> {
let call = json!({ "method": "getResponseBody", "params": { "requestId": request_id } });
let answer = self.js_page_request(s, "networkCall", call).await?;
Ok(response_body(&answer, RESPONSE_BODY_LIMIT))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn event(kind: &str, data: Value) -> Option<ide_core::event::JsNetworkEvent> {
network_event(&json!({ "event": kind, "data": data }))
}
#[test]
fn a_request_carries_what_was_asked_and_who_asked() {
let e = event(
"requestWillBeSent",
json!({
"requestId": "76695.31", "timestamp": 611807.5, "type": "Fetch",
"request": { "method": "GET", "url": "http://127.0.0.1:5195/src/main.ts", "headers": { "Accept": "*/*", "X-N": 1 } },
"initiator": { "type": "script", "stack": { "callFrames": [
{ "functionName": "load", "url": "http://127.0.0.1:5195/src/App.vue", "lineNumber": 9, "columnNumber": 2 }
] } }
}),
)
.unwrap();
assert_eq!(e.kind, JsNetworkKind::Request);
assert_eq!((e.request_id.as_str(), e.timestamp_ms), ("76695.31", 611_807_500));
assert_eq!(e.url.as_deref(), Some("http://127.0.0.1:5195/src/main.ts"));
assert_eq!((e.method.as_deref(), e.resource_type.as_deref()), (Some("GET"), Some("Fetch")));
// Only text headers are kept.
assert_eq!(e.headers.len(), 1);
assert_eq!(e.headers["Accept"], "*/*");
let from = e.initiator.unwrap();
assert_eq!((from.function.as_str(), from.line, from.column), ("load", 9, 2));
assert_eq!(from.url, "http://127.0.0.1:5195/src/App.vue");
}
#[test]
fn a_request_made_through_a_library_is_asked_by_the_code_that_called_the_library() {
let frame = |function: &str, url: &str| json!({ "functionName": function, "url": url, "lineNumber": 1, "columnNumber": 0 });
let lib = "http://localhost:5173/node_modules/.vite/deps/http-client.js";
let asked_by = |stack: Value| {
let data = json!({ "requestId": "1", "timestamp": 1.0, "request": { "method": "GET", "url": "http://localhost/api" },
"initiator": { "type": "script", "stack": stack } });
event("requestWillBeSent", data).unwrap().initiator.unwrap().function
};
// The innermost frame is the library's; the project's caller is further out.
let direct = json!({ "callFrames": [frame("dispatch", lib), frame("request", lib), frame("loadItems", "http://localhost:5173/src/api.ts")] });
assert_eq!(asked_by(direct), "loadItems");
// Across an `await`, the caller is in the stack that scheduled this one.
let scheduled = json!({ "callFrames": [frame("dispatch", lib)],
"parent": { "callFrames": [frame("then", lib)], "parent": { "callFrames": [frame("onMounted", "http://localhost:5173/src/App.vue")] } } });
assert_eq!(asked_by(scheduled), "onMounted");
// A library on its own: the frame that sent the request.
assert_eq!(asked_by(json!({ "callFrames": [frame("dispatch", lib), frame("poll", lib)] })), "dispatch");
}
#[test]
fn a_response_a_finish_and_a_failure_carry_their_outcome() {
let response = event(
"responseReceived",
json!({ "requestId": "7", "timestamp": 2.0, "type": "Script", "response": {
"url": "http://localhost/a.js", "status": 200, "mimeType": "text/javascript",
"headers": { "Content-Type": "text/javascript" }
} }),
)
.unwrap();
assert_eq!(response.kind, JsNetworkKind::Response);
assert_eq!((response.status, response.mime_type.as_deref()), (Some(200), Some("text/javascript")));
assert_eq!(response.headers["Content-Type"], "text/javascript");
let finished =
event("loadingFinished", json!({ "requestId": "7", "timestamp": 2.5, "encodedDataLength": 1234 })).unwrap();
assert_eq!((finished.kind, finished.encoded_length), (JsNetworkKind::Finished, Some(1234)));
let failed =
event("loadingFailed", json!({ "requestId": "8", "timestamp": 3.0, "errorText": "net::ERR_FAILED" }))
.unwrap();
assert_eq!((failed.kind, failed.error_text.as_deref()), (JsNetworkKind::Failed, Some("net::ERR_FAILED")));
}
#[test]
fn events_that_name_no_request_or_are_not_mirrored_are_dropped() {
assert!(event("requestWillBeSent", json!({ "timestamp": 1.0 })).is_none());
assert!(event("dataReceived", json!({ "requestId": "7", "timestamp": 1.0 })).is_none());
assert!(network_event(&json!({ "data": { "requestId": "7" } })).is_none());
// A request made by the page's parser has no script behind it.
let parser = event(
"requestWillBeSent",
json!({ "requestId": "9", "timestamp": 1.0, "request": { "method": "GET", "url": "http://localhost/" },
"initiator": { "type": "parser" } }),
)
.unwrap();
assert!(parser.initiator.is_none());
}
#[test]
fn a_response_body_is_cut_at_the_limit_on_a_character() {
let whole = response_body(&json!({ "body": "héllo", "base64Encoded": false }), 100);
assert_eq!((whole.body.as_str(), whole.base64_encoded, whole.truncated), ("héllo", false, false));
// `é` is two bytes: a cut inside it backs off to the character before.
let cut = response_body(&json!({ "body": "héllo" }), 2);
assert_eq!((cut.body.as_str(), cut.truncated), ("h", true));
let binary = response_body(&json!({ "body": "AAEC", "base64Encoded": true }), 100);
assert!(binary.base64_encoded && !binary.truncated);
assert_eq!(response_body(&json!({}), 100).body, "");
}
}
+16
View File
@@ -24,6 +24,7 @@ use connection::{Connection, DapError};
use futures::future::join_all;
use ide_core::{DebugSessionId, IdeError, IdeResult, SharedSink};
use ide_language::LanguageRegistry;
pub use js_debug::JsResponseBody;
pub use jvm_memory::*;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
@@ -1181,6 +1182,21 @@ impl DebugManager {
}
}
s.launched.send_replace(true);
// A page reports its requests from here on. This is the earliest js-debug takes the
// request (before its answer to `launch` the page has no adapter and the request is
// never answered), and the page is already loading: its document and first scripts
// are not in the list.
if js_debug::is_page(s) {
let answer = s.conn.send_request("enableNetworking", js_debug::networking_arguments())?;
let session = s.clone();
// Not waited for here: the page runs on. A refusal is told to the Network view,
// which would otherwise wait for requests that never come.
tokio::spawn(async move {
if matches!(answer.await, Ok(Err(_))) {
session.emit(ide_core::event::DebugEvent::JsNetworkUnavailable);
}
});
}
if let Some(link) = s.jdwp_link.lock().as_ref() {
link.release();
}
+6
View File
@@ -839,6 +839,12 @@ async fn on_event(mgr: &Arc<DebugManager>, s: &Arc<Session>, event: &str, mut bo
.map(|a| a.iter().filter_map(Value::as_str).map(str::to_owned).collect())
.unwrap_or_default(),
}),
// js-debug, after `enableNetworking`.
"networkEvent" => {
if let Some(event) = crate::js_debug::network_event(&body) {
s.emit(DebugEvent::JsNetwork { event });
}
}
_ => tracing::trace!("dap: ignored event {event}"),
}
}
+87
View File
@@ -160,8 +160,13 @@ struct Script {
launch_after_configuration: bool,
/// Requests of the adapter's own (js-debug's `setCustomBreakpoints`…) answered with an empty body.
accept: &'static [&'static str],
/// Requests of the adapter's own answered with a body and followed by events.
custom: Option<fn(&Value) -> Option<CustomReply>>,
}
/// The answer to a request of the adapter's own: the response body, then events (name, body).
type CustomReply = (Value, Vec<(&'static str, Value)>);
impl Default for Script {
fn default() -> Self {
Self {
@@ -194,6 +199,7 @@ impl Default for Script {
gdb15_starts_at_launch: false,
launch_after_configuration: false,
accept: &[],
custom: None,
}
}
}
@@ -512,6 +518,16 @@ async fn fake_adapter<S: AsyncRead + AsyncWrite + Unpin + Send + 'static>(stream
}
ok!(msg, json!({}))
}
other if script.custom.and_then(|custom| custom(&msg)).is_some() => {
if starting.load(Ordering::Acquire) {
log.lock().push(json!({"type": "dropped", "command": other}));
}
let (body, events) = script.custom.and_then(|custom| custom(&msg)).unwrap();
ok!(msg, body);
for (event, body) in events {
send!(json!({"type": "event", "event": event, "body": body}));
}
}
other => send!(
json!({"type": "response", "request_seq": msg["seq"], "command": other, "success": false, "message": "unsupported"})
),
@@ -1017,6 +1033,77 @@ async fn a_page_gets_its_requests_once_its_session_has_started() {
assert_eq!(dropped, 0, "the request reached the page before it took requests");
}
/// js-debug's network requests: `enableNetworking` is followed by one mirrored browser event.
fn network_adapter(msg: &Value) -> Option<CustomReply> {
match msg["command"].as_str()? {
"enableNetworking" => Some((
json!({}),
vec![(
"networkEvent",
json!({ "event": "requestWillBeSent", "data": {
"requestId": "7", "timestamp": 1.5, "type": "Fetch",
"request": { "method": "GET", "url": "http://localhost:5173/api/items", "headers": {} }
} }),
)],
)),
"networkCall" => Some((json!({ "body": "é".repeat(600_000), "base64Encoded": false }), Vec::new())),
_ => None,
}
}
#[tokio::test]
async fn a_page_reports_its_requests_and_their_bodies() {
let h = harness(Script {
start_debugging: true,
custom: Some(network_adapter),
child_launch_delay: Duration::from_millis(100),
..Script::default()
});
let browser = h.start(js_config("pwa-chrome")).await.unwrap();
let page = child_of(&h, browser).await;
// Recording is asked for by the session itself, as soon as js-debug takes the request:
// once it has answered the page's `launch` (before that, the request is never answered).
let DebugEvent::JsNetwork { event } = h.wait_event(|e| matches!(e, DebugEvent::JsNetwork { .. })).await else {
unreachable!()
};
assert_eq!(
requests(&h, "enableNetworking"),
[json!({ "mirrorEvents": ["requestWillBeSent", "responseReceived", "loadingFinished", "loadingFailed"] })]
);
let dropped = h.log.lock().iter().filter(|m| m["type"] == "dropped").count();
assert_eq!(dropped, 0, "recording was asked for before the page took requests");
assert_eq!((event.request_id.as_str(), event.timestamp_ms), ("7", 1500));
assert_eq!(event.url.as_deref(), Some("http://localhost:5173/api/items"));
let body = h.manager.js_response_body(page, "7".into()).await.unwrap();
assert_eq!(requests(&h, "networkCall"), [json!({ "method": "getResponseBody", "params": { "requestId": "7" } })]);
// 1.2 MB of two-byte characters: cut at the limit, on a character.
assert!(body.truncated && !body.base64_encoded);
assert_eq!(body.body.len(), 1_048_576);
assert!(body.body.chars().all(|c| c == 'é'));
assert_eq!(
h.manager.js_response_body(browser, "7".into()).await.unwrap_err().kind,
ide_core::error::ErrorKind::Unavailable
);
// A Node.js program is not a page: nothing is asked of it.
let node = h.start(js_config("pwa-node")).await.unwrap();
child_of(&h, node).await;
h.wait_event(|e| matches!(e, DebugEvent::Stopped { .. })).await;
assert_eq!(requests(&h, "enableNetworking").len(), 1);
}
#[tokio::test]
async fn a_page_whose_requests_the_adapter_does_not_report_says_so() {
// A js-debug build without `enableNetworking`.
let h = harness(Script { start_debugging: true, ..Script::default() });
let browser = h.start(js_config("pwa-chrome")).await.unwrap();
child_of(&h, browser).await;
h.wait_event(|e| matches!(e, DebugEvent::JsNetworkUnavailable)).await;
}
#[test]
fn a_child_session_is_named_by_the_adapter_unless_it_is_a_blank_page() {
assert_eq!(child_name(&json!({ "name": "server.js [4120]" }), "Server"), "server.js [4120]");
+12
View File
@@ -382,6 +382,15 @@ pub async fn debug_js_custom_breakpoints(
host.debugger.js_set_custom_breakpoints(session, ids, xhr).await
}
pub async fn debug_js_response_body(
host: &Host,
_window: &str,
session: DebugSessionId,
request_id: String,
) -> IdeResult<ide_dap::JsResponseBody> {
host.debugger.js_response_body(session, request_id).await
}
pub fn debug_capabilities(host: &Host, _window: &str, session: DebugSessionId) -> IdeResult<DebugCapabilities> {
host.debugger.capabilities(session)
}
@@ -598,6 +607,9 @@ pub(crate) fn register(d: &mut Dispatcher) {
debug_restart_frame(&c.host, &c.window, c.arg("session")?, c.arg("frameId")?).await
});
d.add("debug_capabilities", |c| async move { debug_capabilities(&c.host, &c.window, c.arg("session")?) });
d.add("debug_js_response_body", |c| async move {
debug_js_response_body(&c.host, &c.window, c.arg("session")?, c.arg("requestId")?).await
});
d.add("debug_js_custom_breakpoints", |c| async move {
debug_js_custom_breakpoints(&c.host, &c.window, c.arg("session")?, c.arg("ids")?, c.arg("xhr")?).await
});
+11
View File
@@ -244,6 +244,17 @@ pub async fn debug_restart_frame(state: State<'_, AppState>, session: DebugSessi
ide_host::ops::debug::debug_restart_frame(&state.host, "", session, frame_id).await
}
/// The body of a response a debugged page received.
#[tauri::command]
#[specta::specta]
pub async fn debug_js_response_body(
state: State<'_, AppState>,
session: DebugSessionId,
request_id: String,
) -> IdeResult<ide_dap::JsResponseBody> {
ide_host::ops::debug::debug_js_response_body(&state.host, "", session, request_id).await
}
/// Breakpoints of a debugged page on DOM events and on requests (js-debug).
#[tauri::command]
#[specta::specta]
+1
View File
@@ -528,6 +528,7 @@ pub fn builder() -> tauri_specta::Builder<tauri::Wry> {
debug::debug_restart_frame,
debug::debug_capabilities,
debug::debug_js_custom_breakpoints,
debug::debug_js_response_body,
debug::debug_set_function_breakpoints,
debug::debug_set_data_breakpoints,
debug::debug_data_breakpoint_info,
+50 -1
View File
@@ -1216,6 +1216,8 @@ export const commands = {
debugCapabilities: (session: DebugSessionId) => typedError<DebugCapabilities, IdeError>(__TAURI_INVOKE("debug_capabilities", { session })),
/** Breakpoints of a debugged page on DOM events and on requests (js-debug). */
debugJsCustomBreakpoints: (session: DebugSessionId, ids: string[], xhr: string[]) => typedError<null, IdeError>(__TAURI_INVOKE("debug_js_custom_breakpoints", { session, ids, xhr })),
/** The body of a response a debugged page received. */
debugJsResponseBody: (session: DebugSessionId, requestId: string) => typedError<JsResponseBody, IdeError>(__TAURI_INVOKE("debug_js_response_body", { session, requestId })),
/** Method breakpoints (all files); states aligned with `breakpoints`. */
debugSetFunctionBreakpoints: (breakpoints: FunctionBreakpoint[]) => typedError<BreakpointState[], IdeError>(__TAURI_INVOKE("debug_set_function_breakpoints", { breakpoints })),
/** Field watchpoints; states aligned with `breakpoints`. */
@@ -4002,7 +4004,11 @@ export type DebugEvent =
* The adapter's `invalidated` event: what it shows for `areas` (`all`, `stacks`, `threads`,
* `variables`; empty means all) is stale and has to be fetched again.
*/
{ kind: "invalidated"; areas: string[] } | { kind: "jvmBackend"; state: JvmBackendState };
{ kind: "invalidated"; areas: string[] } | { kind: "jvmBackend"; state: JvmBackendState } |
/** A step in the life of one request of a debugged page. */
{ kind: "jsNetwork"; event: JsNetworkEvent } |
/** The adapter does not report the requests of this page: there will be no `JsNetwork`. */
{ kind: "jsNetworkUnavailable" };
/** A debug adapter session. */
export type DebugSessionId = number;
@@ -6423,6 +6429,49 @@ export type JetBrainsSettings = {
files: { [key in string]: string },
};
/** The script frame that made a request, where the browser ran it (0-based line and column). */
export type JsInitiator = {
function: string,
url: string,
line: number,
column: number,
};
/**
* What a debugged page's browser reports about a request, reduced to what the Network view
* shows. Events of one request share `request_id`; each kind fills its own fields.
*/
export type JsNetworkEvent = {
kind: JsNetworkKind,
requestId: string,
/** The browser's monotonic clock, comparable between the events of a page. */
timestampMs: number,
/** Request and response. */
url: string | null,
method: string | null,
/** `Fetch`, `XHR`, `Script`, `Document`, … */
resourceType: string | null,
status: number | null,
mimeType: string | null,
/** The request's headers, or the response's. */
headers: { [key in string]: string },
/** Bytes received over the network, once finished. */
encodedLength: number | null,
errorText: string | null,
initiator: JsInitiator | null,
};
/** Which step of a request a [`JsNetworkEvent`] reports. */
export type JsNetworkKind = "request" | "response" | "finished" | "failed";
/** The body of a response, as text or as the browser's base64 for binary content. */
export type JsResponseBody = {
body: string,
base64Encoded: boolean,
/** The body was longer than [`RESPONSE_BODY_LIMIT`] and is cut there. */
truncated: boolean,
};
/** The backend that reads a debugged JVM's objects (instances, fields, referrers). */
export type JvmBackendState = { kind: "ready" } |
/** Turned off for the rest of the session: the debug adapter lacks what it needs. */
@@ -13,7 +13,11 @@ describe("JavaScript events and requests section", () => {
expect(screen.getByRole("alert")).toHaveTextContent(
"The debugger of the running page does not take these breakpoints: unknown request",
);
act(() => setRefused(4, "eventBreakpoints", null));
// Another tool's refusal is not about breakpoints.
act(() => {
setRefused(4, "eventBreakpoints", null);
setRefused(4, "network", "");
});
expect(screen.queryByRole("alert")).toBeNull();
});
});
@@ -0,0 +1,200 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import type { JsNetworkEvent, JsResponseBody } from "@/platform/ipc";
const asked: Array<[number, string]> = [];
const evaluated: unknown[][] = [];
let answer: { status: "ok"; data: JsResponseBody } | { status: "error"; error: { kind: string; message: string } };
vi.mock("@/platform/ipc", async (orig) => {
const real = await orig<typeof import("@/platform/ipc")>();
return {
...real,
commands: {
...real.commands,
debugJsResponseBody: (session: number, requestId: string) => {
asked.push([session, requestId]);
return Promise.resolve(answer);
},
debugEvaluateIn: (...args: unknown[]) => {
evaluated.push(args);
return Promise.resolve({ status: "ok", data: { value: "", variablesReference: 0 } });
},
},
};
});
const { applyNetworkEvents, useJsNetwork } = await import("../js/network");
const { NetworkView } = await import("../js/NetworkView");
const { useJsBreakpoints } = await import("../js/eventBreakpoints");
const { setRefused } = await import("../js/refusals");
const { handleDebugEvent, useDebug } = await import("../state");
const event = (kind: JsNetworkEvent["kind"], requestId: string, p: Partial<JsNetworkEvent> = {}): JsNetworkEvent => ({
kind,
requestId,
timestampMs: 1000,
url: null,
method: null,
resourceType: null,
status: null,
mimeType: null,
headers: {},
encodedLength: null,
errorText: null,
initiator: null,
...p,
});
function load() {
applyNetworkEvents(7, [
event("request", "1", {
url: "http://h/api/items",
method: "GET",
resourceType: "Fetch",
headers: { Accept: "*/*" },
}),
event("response", "1", {
status: 200,
mimeType: "application/json",
headers: { "Content-Type": "application/json" },
}),
event("finished", "1", { timestampMs: 1250, encodedLength: 2048 }),
event("request", "2", { url: "http://h/logo.png", method: "GET", resourceType: "Image" }),
event("request", "3", { url: "http://h/gone", method: "POST", resourceType: "XHR" }),
event("failed", "3", { timestampMs: 1100, errorText: "net::ERR_FAILED" }),
]);
}
beforeEach(() => {
useJsNetwork.setState({ rows: {} });
asked.length = 0;
evaluated.length = 0;
answer = { status: "ok", data: { body: '{"items":[]}', base64Encoded: false, truncated: false } };
});
describe("Network view", () => {
it("lists a session's requests with status, type, size and time", () => {
load();
render(<NetworkView session={7} />);
const rows = screen.getAllByRole("row").slice(1);
expect(rows).toHaveLength(3);
expect(rows[0]).toHaveTextContent("items");
expect(rows[0]).toHaveTextContent("200");
expect(rows[0]).toHaveTextContent("Fetch");
expect(rows[0]).toHaveTextContent("2.0 kB");
expect(rows[0]).toHaveTextContent("250 ms");
expect(rows[2]).toHaveTextContent("net::ERR_FAILED");
});
it("filters by a part of the URL", () => {
load();
render(<NetworkView session={7} />);
fireEvent.change(screen.getByLabelText("Filter requests"), { target: { value: "logo" } });
expect(screen.getAllByRole("row").slice(1)).toHaveLength(1);
});
it("shows a selected request's headers and loads its body", async () => {
load();
render(<NetworkView session={7} />);
fireEvent.click(screen.getAllByRole("row")[1]!);
expect(screen.getByText("http://h/api/items")).toBeInTheDocument();
expect(screen.getByText("Accept")).toBeInTheDocument();
expect(screen.getByText("Content-Type")).toBeInTheDocument();
await waitFor(() => expect(screen.getByText('{"items":[]}')).toBeInTheDocument());
expect(asked).toEqual([[7, "1"]]);
});
it("says so when a body is binary, cut or gone", async () => {
load();
answer = { status: "ok", data: { body: "AAEC", base64Encoded: true, truncated: false } };
const { unmount } = render(<NetworkView session={7} />);
fireEvent.click(screen.getAllByRole("row")[1]!);
await waitFor(() => expect(screen.getByText("Binary content")).toBeInTheDocument());
expect(screen.queryByText("AAEC")).toBeNull();
unmount();
answer = { status: "ok", data: { body: "abc", base64Encoded: false, truncated: true } };
const second = render(<NetworkView session={7} />);
fireEvent.click(screen.getAllByRole("row")[1]!);
await waitFor(() => expect(screen.getByText("Showing the first 1 MB")).toBeInTheDocument());
second.unmount();
// The session ended, or the browser dropped the response.
answer = { status: "error", error: { kind: "unavailable", message: "gone" } };
render(<NetworkView session={7} />);
fireEvent.click(screen.getAllByRole("row")[1]!);
await waitFor(() => expect(screen.getByText("Response body is no longer available")).toBeInTheDocument());
});
it("clears the list", () => {
load();
render(<NetworkView session={7} />);
fireEvent.click(screen.getByRole("button", { name: "Clear" }));
expect(screen.queryAllByRole("row").slice(1)).toHaveLength(0);
expect(screen.getByText(/^No requests yet/)).toBeInTheDocument();
});
it("filters by the kind of resource, like a browser's network panel", () => {
load();
render(<NetworkView session={7} />);
fireEvent.click(screen.getByRole("button", { name: "Fetch/XHR" }));
expect(screen.getByRole("button", { name: "Fetch/XHR" })).toHaveAttribute("aria-pressed", "true");
const shown = screen.getAllByRole("row").slice(1);
expect(shown).toHaveLength(2);
expect(shown[0]).toHaveTextContent("items");
expect(shown[1]).toHaveTextContent("gone");
fireEvent.click(screen.getByRole("button", { name: "Img" }));
expect(screen.getAllByRole("row").slice(1)).toHaveLength(1);
fireEvent.click(screen.getByRole("button", { name: "All" }));
expect(screen.getAllByRole("row").slice(1)).toHaveLength(3);
});
it("can show only what the project's own code requested", () => {
applyNetworkEvents(7, [
event("request", "1", {
url: "http://h/api/items",
resourceType: "Fetch",
initiator: { function: "load", url: "http://h/src/App.vue", line: 9, column: 2 },
}),
event("request", "2", {
url: "http://h/api/track",
resourceType: "Fetch",
initiator: { function: "send", url: "http://h/node_modules/.vite/deps/analytics.js", line: 1, column: 1 },
}),
]);
render(<NetworkView session={7} />);
fireEvent.click(screen.getByLabelText("My code only"));
const shown = screen.getAllByRole("row").slice(1);
expect(shown).toHaveLength(1);
expect(shown[0]).toHaveTextContent("items");
});
it("turns a request into a breakpoint on the next one to that address", () => {
useJsBreakpoints.setState({ root: null, events: {}, requests: [] });
load();
render(<NetworkView session={7} />);
fireEvent.click(screen.getAllByRole("row")[1]!);
fireEvent.click(screen.getByRole("button", { name: "Break on Next Request" }));
expect(useJsBreakpoints.getState().requests).toEqual([{ pattern: "/api/items", enabled: true }]);
});
it("reloads the page, to list the requests of its load", () => {
render(<NetworkView session={7} />);
// Recording starts while the page is already loading.
expect(screen.getByText("No requests yet. Reload the page to see the requests of its load.")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Reload Page" }));
expect(evaluated).toEqual([[7, null, "location.reload()", "repl"]]);
});
it("says that requests are not reported instead of waiting for them", () => {
render(<NetworkView session={7} />);
expect(screen.getByText(/^No requests yet/)).toBeInTheDocument();
const page = { id: 7, name: "page", status: "running", caps: { jsPage: true } };
useDebug.setState({ sessions: { 7: page } as unknown as ReturnType<typeof useDebug.getState>["sessions"] });
act(() => handleDebugEvent(7, { kind: "jsNetworkUnavailable" }));
expect(
screen.getByText("This version of the JavaScript debugger does not report a page's requests."),
).toBeInTheDocument();
setRefused(7, "network", null);
});
});
@@ -0,0 +1,159 @@
import { beforeEach, describe, expect, it } from "vitest";
import type { JsNetworkEvent } from "@/platform/ipc";
import {
applyNetworkEvents,
clearNetwork,
filterRows,
forgetNetwork,
MAX_ROWS,
rowCategory,
rowName,
useJsNetwork,
} from "../js/network";
const base: JsNetworkEvent = {
kind: "request",
requestId: "1",
timestampMs: 1000,
url: null,
method: null,
resourceType: null,
status: null,
mimeType: null,
headers: {},
encodedLength: null,
errorText: null,
initiator: null,
};
const request = (requestId: string, url: string, timestampMs = 1000): JsNetworkEvent => ({
...base,
requestId,
url,
method: "GET",
resourceType: "Fetch",
timestampMs,
headers: { Accept: "*/*" },
});
const response = (requestId: string, status = 200): JsNetworkEvent => ({
...base,
kind: "response",
requestId,
status,
mimeType: "application/json",
resourceType: "Fetch",
timestampMs: 1040,
headers: { "Content-Type": "application/json" },
});
const finished = (requestId: string, timestampMs = 1250): JsNetworkEvent => ({
...base,
kind: "finished",
requestId,
timestampMs,
encodedLength: 1234,
});
const rows = (session = 1) => useJsNetwork.getState().rows[session] ?? [];
beforeEach(() => useJsNetwork.setState({ rows: {} }));
describe("network rows of a debugged page", () => {
it("folds the events of one request into a row", () => {
applyNetworkEvents(1, [request("1", "http://h/api/items"), response("1"), finished("1")]);
expect(rows()).toHaveLength(1);
expect(rows()[0]).toMatchObject({
requestId: "1",
url: "http://h/api/items",
name: "items",
method: "GET",
type: "Fetch",
status: 200,
mimeType: "application/json",
size: 1234,
durationMs: 250,
error: null,
requestHeaders: { Accept: "*/*" },
responseHeaders: { "Content-Type": "application/json" },
});
});
it("a failed request keeps its error", () => {
applyNetworkEvents(1, [
request("2", "http://h/gone"),
{ ...base, kind: "failed", requestId: "2", timestampMs: 1100, errorText: "net::ERR_FAILED" },
]);
expect(rows()[0]).toMatchObject({ status: null, error: "net::ERR_FAILED", durationMs: 100 });
});
it("a reused request id after completion starts a new row", () => {
applyNetworkEvents(1, [request("1", "http://h/a"), finished("1")]);
applyNetworkEvents(1, [request("1", "http://h/b", 5000), response("1", 404)]);
expect(rows().map((r) => [r.url, r.status, r.size])).toEqual([
["http://h/a", null, 1234],
["http://h/b", 404, null],
]);
expect(new Set(rows().map((r) => r.key)).size).toBe(2);
});
it("events without a request are dropped", () => {
applyNetworkEvents(1, [response("9"), finished("9")]);
expect(rows()).toEqual([]);
});
it("keeps the newest rows of each session apart, up to the limit", () => {
const many = Array.from({ length: MAX_ROWS + 1 }, (_, i) => request(`r${i}`, `http://h/${i}`));
applyNetworkEvents(1, many);
applyNetworkEvents(2, [request("x", "http://h/other")]);
expect(MAX_ROWS).toBe(2000);
expect(rows(1)).toHaveLength(MAX_ROWS);
expect(rows(1)[0]!.url).toBe("http://h/1");
expect(rows(2)).toHaveLength(1);
});
it("filters by a part of the URL, ignoring case", () => {
applyNetworkEvents(1, [request("1", "http://h/api/Items"), request("2", "http://h/logo.png")]);
expect(filterRows(rows(), "ITEMS").map((r) => r.requestId)).toEqual(["1"]);
expect(filterRows(rows(), " ")).toHaveLength(2);
});
it("sorts resource types into the browser's filter groups", () => {
expect(["Fetch", "XHR"].map(rowCategory)).toEqual(["fetch", "fetch"]);
expect(["Document", "Script", "Stylesheet", "Image", "Font"].map(rowCategory)).toEqual([
"doc",
"js",
"css",
"img",
"font",
]);
expect(["WebSocket", "Media", "Other", ""].map(rowCategory)).toEqual(["other", "other", "other", "other"]);
});
it("filters by group, and by who made the request", () => {
const from = (url: string) => ({ function: "load", url, line: 1, column: 1 });
applyNetworkEvents(1, [
{ ...request("1", "http://h/api/items"), initiator: from("http://h/src/App.vue") },
{ ...request("2", "http://h/api/track"), initiator: from("http://h/node_modules/.vite/deps/analytics.js") },
{ ...request("3", "http://h/src/main.ts"), resourceType: "Script" },
]);
const ids = (rows: ReturnType<typeof filterRows>) => rows.map((r) => r.requestId);
expect(ids(filterRows(rows(), "", "fetch"))).toEqual(["1", "2"]);
expect(ids(filterRows(rows(), "", "js"))).toEqual(["3"]);
expect(ids(filterRows(rows(), "track", "fetch"))).toEqual(["2"]);
// Only what the project's own code asked for: not a library's request, not a tag in the page.
expect(ids(filterRows(rows(), "", "all", true))).toEqual(["1"]);
expect(ids(filterRows(rows(), "", "js", true))).toEqual([]);
});
it("names a row after the end of its path", () => {
expect(rowName("http://h/a/b.js?v=1")).toBe("b.js?v=1");
expect(rowName("http://h:5173/")).toBe("h:5173");
expect(rowName("http://h/dir/")).toBe("dir/");
expect(rowName("data:text/plain,hi")).toBe("data:text/plain,hi");
});
it("clears and forgets a session's rows", () => {
applyNetworkEvents(1, [request("1", "http://h/a")]);
clearNetwork(1);
expect(useJsNetwork.getState().rows[1]).toEqual([]);
forgetNetwork(1);
expect(1 in useJsNetwork.getState().rows).toBe(false);
});
});
@@ -0,0 +1,52 @@
import { beforeEach, describe, expect, it } from "vitest";
import type { JsNetworkEvent } from "@/platform/ipc";
import { useDebug } from "../state";
import { applyNetworkEvents, useJsNetwork } from "../js/network";
import { watchNetworkSessions } from "../js/networkSessions";
type Sessions = ReturnType<typeof useDebug.getState>["sessions"];
function sessions(ids: number[]) {
const out: Record<number, unknown> = {};
for (const id of ids) out[id] = { id, name: `s${id}`, status: "running", caps: { jsPage: true } };
useDebug.setState({ sessions: out as Sessions });
}
const request: JsNetworkEvent = {
kind: "request",
requestId: "1",
timestampMs: 1,
url: "http://h/a",
method: "GET",
resourceType: "Fetch",
status: null,
mimeType: null,
headers: {},
encodedLength: null,
errorText: null,
initiator: null,
};
beforeEach(() => {
sessions([]);
useJsNetwork.setState({ rows: {} });
});
describe("the requests of page sessions", () => {
it("stay while the session is listed, finished or not", () => {
const stop = watchNetworkSessions();
sessions([1, 2]);
applyNetworkEvents(1, [request]);
sessions([1]);
expect(useJsNetwork.getState().rows[1]).toHaveLength(1);
stop();
});
it("are dropped when the session is removed", () => {
const stop = watchNetworkSessions();
sessions([1]);
applyNetworkEvents(1, [request]);
sessions([]);
expect(1 in useJsNetwork.getState().rows).toBe(false);
stop();
});
});
+2
View File
@@ -21,6 +21,7 @@ import { registerDebugActions } from "./actions";
import { loadBreakpoints } from "./breakpoints/store";
import { loadExceptionBreakpoints } from "./breakpoints/exceptions";
import { loadJsBreakpoints, watchPageSessions } from "./js/eventBreakpoints";
import { watchNetworkSessions } from "./js/networkSessions";
import { loadSpecialBreakpoints } from "./breakpoints/special";
import { breakpointGutter } from "./editor/breakpointGutter";
import { executionPoint } from "./editor/execution";
@@ -148,6 +149,7 @@ export function activate(): () => void {
}
}),
watchPageSessions(),
watchNetworkSessions(),
];
return () => disposers.reverse().forEach((d) => d());
}
+184
View File
@@ -0,0 +1,184 @@
.root {
display: flex;
flex-direction: column;
height: 100%;
min-height: 0;
font-size: var(--fs-ui);
}
.toolbar {
display: flex;
align-items: center;
gap: 6px;
padding: 4px 8px;
border-bottom: 1px solid var(--border);
}
.count {
color: var(--fg-subtle);
font-size: var(--fs-small);
white-space: nowrap;
}
.categories {
display: flex;
flex: none;
gap: 2px;
}
.category {
border: 0;
border-radius: 4px;
padding: 2px 6px;
background: transparent;
color: var(--fg-muted);
font: inherit;
font-size: var(--fs-small);
cursor: default;
}
.category:hover {
background: var(--bg-hover);
}
.categoryOn,
.categoryOn:hover {
background: var(--bg-selected);
color: inherit;
}
.actions {
padding-top: 6px;
}
.split {
display: flex;
flex: 1;
min-height: 0;
}
.table {
flex: 1 1 55%;
min-width: 0;
overflow: auto;
}
.head,
.row {
display: grid;
grid-template-columns: minmax(120px, 3fr) minmax(60px, 1.4fr) 64px 88px 76px 76px;
gap: 8px;
padding: 0 8px;
align-items: center;
min-height: 22px;
}
.head {
position: sticky;
top: 0;
color: var(--fg-muted);
font-size: var(--fs-small);
background: var(--bg-panel, var(--bg-hover));
border-bottom: 1px solid var(--border);
}
.row {
cursor: default;
/* Off-screen rows of a long list aren't laid out. */
content-visibility: auto;
contain-intrinsic-size: auto 22px;
}
.row > span,
.head > span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.row:hover {
background: var(--bg-hover);
}
.selected,
.selected:hover {
background: var(--bg-list-selected);
}
.failed {
color: var(--danger);
}
.name {
font-family: var(--font-mono);
}
.number {
text-align: right;
font-variant-numeric: tabular-nums;
}
.detail {
flex: 1 1 45%;
min-width: 0;
overflow: auto;
border-left: 1px solid var(--border);
padding: 6px 10px;
}
.url {
font-family: var(--font-mono);
word-break: break-all;
user-select: text;
}
.summary {
color: var(--fg-muted);
font-size: var(--fs-small);
padding-top: 2px;
word-break: break-all;
}
.sectionTitle {
font-size: var(--fs-small);
font-weight: 600;
color: var(--fg-muted);
padding: 8px 0 2px;
}
.header {
display: flex;
gap: 8px;
font-family: var(--font-mono);
font-size: var(--fs-small);
user-select: text;
}
.headerName {
flex: none;
color: var(--fg-muted);
}
.headerValue {
word-break: break-all;
}
.note {
padding: 4px 8px;
color: var(--fg-subtle);
}
.refused {
padding: 4px 8px;
color: var(--danger);
font-size: var(--fs-small);
}
.body {
margin: 0;
font-family: var(--font-mono);
font-size: var(--fs-small);
white-space: pre-wrap;
word-break: break-all;
user-select: text;
}
+235
View File
@@ -0,0 +1,235 @@
import { memo, useEffect, useMemo, useState } from "react";
import { useTx } from "@/platform/i18n";
import { call, commands, type JsResponseBody } from "@/platform/ipc";
import { Button, Checkbox, IconButton, Input, T } from "@/ui";
import { addRequestBreakpoint } from "./eventBreakpoints";
import {
clearNetwork,
filterRows,
NETWORK_CATEGORIES,
useJsNetwork,
type NetworkCategory,
type NetworkRow,
} from "./network";
import { useRefused } from "./refusals";
import styles from "./Network.module.css";
const NO_ROWS: NetworkRow[] = [];
/** What a request breakpoint looks for to stop at the next request like `url`: its path. */
export function requestPattern(url: string): string {
try {
return new URL(url).pathname;
} catch {
return url;
}
}
export function formatSize(bytes: number | null): string {
if (bytes == null) return "";
if (bytes < 1000) return `${bytes} B`;
return bytes < 1_000_000 ? `${(bytes / 1000).toFixed(1)} kB` : `${(bytes / 1_000_000).toFixed(1)} MB`;
}
export function formatDuration(ms: number | null): string {
if (ms == null) return "";
return ms < 1000 ? `${Math.round(ms)} ms` : `${(ms / 1000).toFixed(2)} s`;
}
const Row = memo(function Row({
row,
selected,
onSelect,
}: {
row: NetworkRow;
selected: boolean;
onSelect: (key: string) => void;
}) {
return (
<div
role="row"
aria-selected={selected}
className={`${styles.row} ${selected ? styles.selected : ""} ${row.error ? styles.failed : ""}`}
onClick={() => onSelect(row.key)}
title={row.url}
>
<span role="cell" className={styles.name}>
{row.name}
</span>
<span role="cell">{row.error ?? row.status ?? ""}</span>
<span role="cell">{row.method}</span>
<span role="cell">{row.type}</span>
<span role="cell" className={styles.number}>
{formatSize(row.size)}
</span>
<span role="cell" className={styles.number}>
{formatDuration(row.durationMs)}
</span>
</div>
);
});
function Headers({ title, headers }: { title: string; headers: Record<string, string> }) {
const names = Object.keys(headers).sort((a, b) => a.localeCompare(b));
if (!names.length) return null;
return (
<>
<div className={styles.sectionTitle}>{title}</div>
{names.map((name) => (
<div key={name} className={styles.header}>
<span className={styles.headerName}>{name}</span>
<span className={styles.headerValue}>{headers[name]}</span>
</div>
))}
</>
);
}
type Body = { state: "loading" } | { state: "gone" } | { state: "loaded"; body: JsResponseBody };
/** The body of one finished request; mounted per request (`key`), so it starts out loading. */
function ResponseBody({ session, requestId }: { session: number; requestId: string }) {
const [body, setBody] = useState<Body>({ state: "loading" });
useEffect(() => {
let current = true;
call(commands.debugJsResponseBody(session, requestId)).then(
(loaded) => current && setBody({ state: "loaded", body: loaded }),
// The session ended, or the browser no longer keeps the response.
() => current && setBody({ state: "gone" }),
);
return () => {
current = false;
};
}, [session, requestId]);
if (body.state === "loading") return null;
if (body.state === "gone")
return (
<div className={styles.note}>
<T>Response body is no longer available</T>
</div>
);
if (body.body.base64Encoded)
return (
<div className={styles.note}>
<T>Binary content</T>
</div>
);
return (
<>
{body.body.truncated && (
<div className={styles.note}>
<T>Showing the first 1 MB</T>
</div>
)}
<pre className={styles.body}>{body.body.body}</pre>
</>
);
}
function Detail({ session, row }: { session: number; row: NetworkRow }) {
const tx = useTx();
return (
<div className={styles.detail} aria-label={tx("Request details")}>
<div className={styles.url}>{row.url}</div>
<div className={styles.summary}>
{[row.method, row.error ?? row.status, row.mimeType].filter((part) => part != null && part !== "").join(" · ")}
</div>
{row.initiator && (
<div className={styles.summary}>
{tx("Requested by")} {row.initiator.function || tx("(anonymous)")} — {row.initiator.url}:
{row.initiator.line + 1}
</div>
)}
<div className={styles.actions}>
<Button size="small" variant="ghost" onClick={() => addRequestBreakpoint(requestPattern(row.url))}>
<T>Break on Next Request</T>
</Button>
</div>
<Headers title={tx("Request Headers")} headers={row.requestHeaders} />
<Headers title={tx("Response Headers")} headers={row.responseHeaders} />
{row.done && !row.error && (
<>
<div className={styles.sectionTitle}>{tx("Response")}</div>
<ResponseBody key={row.key} session={session} requestId={row.requestId} />
</>
)}
</div>
);
}
/**
* Loads the page again. Recording starts while the page is already loading, so this is how
* the document and its first scripts get into the list.
*/
function reloadPage(session: number) {
void call(commands.debugEvaluateIn(session, null, "location.reload()", "repl")).catch(() => undefined);
}
/** The requests of the page debugged by `session`. */
export function NetworkView({ session }: { session: number }) {
const tx = useTx();
const rows = useJsNetwork((s) => s.rows[session] ?? NO_ROWS);
const [query, setQuery] = useState("");
const [category, setCategory] = useState<NetworkCategory | "all">("all");
const [mine, setMine] = useState(false);
const [selectedKey, setSelectedKey] = useState<string | null>(null);
const shown = useMemo(() => filterRows(rows, query, category, mine), [rows, query, category, mine]);
const selected = rows.find((r) => r.key === selectedKey) ?? null;
const notReported = useRefused(session, "network") != null;
return (
<div className={styles.root}>
<div className={styles.toolbar}>
<Input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Filter by URL"
aria-label="Filter requests"
/>
<div className={styles.categories} role="group" aria-label={tx("Resource type")}>
{NETWORK_CATEGORIES.map((c) => (
<button
key={c.id}
type="button"
className={`${styles.category} ${category === c.id ? styles.categoryOn : ""}`}
aria-pressed={category === c.id}
onClick={() => setCategory(c.id)}
>
{tx(c.label)}
</button>
))}
</div>
<Checkbox checked={mine} onChange={setMine} label="My code only" />
<IconButton icon="refresh" title="Reload Page" size="small" onClick={() => reloadPage(session)} />
<IconButton icon="trash" title="Clear" size="small" onClick={() => clearNetwork(session)} />
<span className={styles.count}>
{shown.length === rows.length ? rows.length : `${shown.length} / ${rows.length}`}
</span>
</div>
<div className={styles.split}>
<div className={styles.table} role="table" aria-label={tx("Requests")}>
<div role="row" className={styles.head}>
<span role="columnheader">{tx("Name")}</span>
<span role="columnheader">{tx("Status")}</span>
<span role="columnheader">{tx("Method")}</span>
<span role="columnheader">{tx("Type")}</span>
<span role="columnheader">{tx("Size")}</span>
<span role="columnheader">{tx("Time")}</span>
</div>
{shown.length === 0 && (
<div className={styles.note}>
{rows.length
? tx("No request matches the filter")
: notReported
? tx("This version of the JavaScript debugger does not report a page's requests.")
: tx("No requests yet. Reload the page to see the requests of its load.")}
</div>
)}
{shown.map((row) => (
<Row key={row.key} row={row} selected={row.key === selectedKey} onSelect={setSelectedKey} />
))}
</div>
{selected && <Detail session={session} row={selected} />}
</div>
</div>
);
}
+194
View File
@@ -0,0 +1,194 @@
/**
* The requests of a debugged page, one row each, built from the browser's events
* (`DebugEvent::JsNetwork`). Kept per debug session.
*/
import { create } from "zustand";
import type { JsInitiator, JsNetworkEvent } from "@/platform/ipc";
export interface NetworkRow {
/** Unique among rows; a browser reuses request ids. */
key: string;
requestId: string;
url: string;
name: string;
method: string;
/** `Fetch`, `XHR`, `Script`, `Document`, … */
type: string;
status: number | null;
mimeType: string | null;
/** Bytes received over the network. */
size: number | null;
/** The browser's clock when the request was sent (ms). */
startedAt: number;
durationMs: number | null;
error: string | null;
/** Finished or failed: later events with this request id belong to another request. */
done: boolean;
requestHeaders: Record<string, string>;
responseHeaders: Record<string, string>;
initiator: JsInitiator | null;
}
/** Rows kept per session; the oldest go first. */
export const MAX_ROWS = 2000;
export const useJsNetwork = create<{ rows: Record<number, NetworkRow[]> }>(() => ({ rows: {} }));
let serial = 0;
/** The end of the URL's path with its query, or the host for the root path. */
export function rowName(url: string): string {
try {
const u = new URL(url);
if (!u.host) return url;
const last = u.pathname.match(/[^/]+\/?$/)?.[0];
return last ? last + u.search : u.host;
} catch {
return url;
}
}
function apply(rows: NetworkRow[], e: JsNetworkEvent) {
let at = -1;
for (let i = rows.length - 1; i >= 0; i--) {
if (rows[i]!.requestId === e.requestId) {
at = rows[i]!.done ? -1 : i;
break;
}
}
if (e.kind === "request") {
const url = e.url ?? "";
const started: NetworkRow = {
key: `${e.requestId}#${serial++}`,
requestId: e.requestId,
url,
name: rowName(url),
method: e.method ?? "",
type: e.resourceType ?? "",
status: null,
mimeType: null,
size: null,
startedAt: e.timestampMs,
durationMs: null,
error: null,
done: false,
requestHeaders: e.headers,
responseHeaders: {},
initiator: e.initiator,
};
// Sent again while open: the request was redirected, and goes on under its new URL.
if (at >= 0) rows[at] = { ...started, key: rows[at]!.key, startedAt: rows[at]!.startedAt };
else rows.push(started);
return;
}
if (at < 0) return;
const row = rows[at]!;
if (e.kind === "response") {
rows[at] = {
...row,
status: e.status,
mimeType: e.mimeType,
type: e.resourceType ?? row.type,
responseHeaders: e.headers,
};
} else {
rows[at] = {
...row,
done: true,
durationMs: Math.max(0, e.timestampMs - row.startedAt),
size: e.kind === "finished" ? e.encodedLength : row.size,
error: e.kind === "failed" ? (e.errorText ?? "Failed") : null,
};
}
}
export function applyNetworkEvents(session: number, events: readonly JsNetworkEvent[]) {
if (!events.length) return;
useJsNetwork.setState((s) => {
const rows = [...(s.rows[session] ?? [])];
for (const e of events) apply(rows, e);
return { rows: { ...s.rows, [session]: rows.length > MAX_ROWS ? rows.slice(-MAX_ROWS) : rows } };
});
}
const pending = new Map<number, JsNetworkEvent[]>();
let flushing: number | null = null;
/** Adds an event; a burst (a page load) reaches the store once per frame. */
export function queueNetworkEvent(session: number, event: JsNetworkEvent) {
const list = pending.get(session);
if (list) list.push(event);
else pending.set(session, [event]);
// A timer and not an animation frame: those stop while the window is in the background.
flushing ??= window.setTimeout(() => {
flushing = null;
const batches = [...pending];
pending.clear();
for (const [id, events] of batches) applyNetworkEvents(id, events);
}, 50);
}
/** Empties the list; requests still open are forgotten with it. */
export function clearNetwork(session: number) {
useJsNetwork.setState((s) => ({ rows: { ...s.rows, [session]: [] } }));
}
export function forgetNetwork(session: number) {
pending.delete(session);
useJsNetwork.setState((s) => {
if (!(session in s.rows)) return s;
const rows = { ...s.rows };
delete rows[session];
return { rows };
});
}
/** The groups a browser's network panel filters by. */
export type NetworkCategory = "fetch" | "doc" | "js" | "css" | "img" | "font" | "other";
export const NETWORK_CATEGORIES: ReadonlyArray<{ id: NetworkCategory | "all"; label: string }> = [
{ id: "all", label: "All" },
{ id: "fetch", label: "Fetch/XHR" },
{ id: "doc", label: "Doc" },
{ id: "js", label: "JS" },
{ id: "css", label: "CSS" },
{ id: "img", label: "Img" },
{ id: "font", label: "Font" },
{ id: "other", label: "Other" },
];
const CATEGORY_OF: Record<string, NetworkCategory> = {
Fetch: "fetch",
XHR: "fetch",
Document: "doc",
Script: "js",
Stylesheet: "css",
Image: "img",
Font: "font",
};
/** The group of a browser resource type (`Fetch`, `Script`, …). */
export function rowCategory(type: string): NetworkCategory {
return CATEGORY_OF[type] ?? "other";
}
/** Whether a script of the project made the request: not a library, and not a tag of the page. */
export function requestedByProject(row: Pick<NetworkRow, "initiator">): boolean {
return row.initiator != null && !/\/node_modules\//.test(row.initiator.url);
}
/** The rows whose URL contains `query`, of one `category`, and (`mine`) asked for by the project's code. */
export function filterRows(
rows: readonly NetworkRow[],
query: string,
category: NetworkCategory | "all" = "all",
mine = false,
): NetworkRow[] {
const q = query.trim().toLowerCase();
return rows.filter(
(r) =>
(!q || r.url.toLowerCase().includes(q)) &&
(category === "all" || rowCategory(r.type) === category) &&
(!mine || requestedByProject(r)),
);
}
@@ -0,0 +1,13 @@
import { useDebug } from "../state";
import { forgetNetwork, useJsNetwork } from "./network";
/**
* Drops a session's requests when the session is removed. (Recording itself starts in the
* backend with the page session, so that the page's load is in the list.) Returns the
* unsubscribe function.
*/
export function watchNetworkSessions(): () => void {
return useDebug.subscribe((s) => {
for (const id of Object.keys(useJsNetwork.getState().rows)) if (!s.sessions[+id]) forgetNetwork(+id);
});
}
+3 -1
View File
@@ -4,7 +4,7 @@
*/
import { create } from "zustand";
export type JsTool = "eventBreakpoints";
export type JsTool = "eventBreakpoints" | "network";
/** Per session, the reason each refused tool was refused for. */
export const useJsRefusals = create<{ refused: Record<number, Partial<Record<JsTool, string>>> }>(() => ({
@@ -41,6 +41,8 @@ export async function recordRefusal(
export const refusedIn = (session: number, tool: JsTool) => useJsRefusals.getState().refused[session]?.[tool];
export const useRefused = (session: number, tool: JsTool) => useJsRefusals((s) => s.refused[session]?.[tool]);
export function forgetRefusals(session: number) {
const { refused } = useJsRefusals.getState();
if (!(session in refused)) return;
+8
View File
@@ -40,6 +40,8 @@ import { refreshExceptionFilters } from "./breakpoints/exceptions";
import { flashTaskbarForBreakpoint, stopTaskbarFlash } from "./taskbarFlash";
import { useProject } from "@/platform/project";
import { isRemoteWindow } from "@/platform/remote";
import { queueNetworkEvent } from "./js/network";
import { setRefused } from "./js/refusals";
import {
browserLaunch,
installedDebugBrowser,
@@ -1313,6 +1315,12 @@ export function handleDebugEvent(id: DebugSessionId, event: DebugEvent) {
case "jvmBackend":
patch(id, { jvmBackend: event.state });
break;
case "jsNetwork":
queueNetworkEvent(id, event.event);
break;
case "jsNetworkUnavailable":
setRefused(id, "network", "");
break;
case "invalidated":
if (s.status !== "paused") break;
if (event.areas.length && !event.areas.some((a) => a === "variables" || a === "all")) break;
+9 -3
View File
@@ -41,6 +41,7 @@ import {
import { remoteWaitingNote } from "../remote";
import { sessionTabs } from "./sessionTabs";
import { stopNote } from "../js/eventBreakpoints";
import { NetworkView } from "../js/NetworkView";
import { liveWatchesApply, useLiveWatches } from "../js/liveWatches";
import { MemoryView } from "../memory/MemoryView";
import { DebugConsole } from "./DebugConsole";
@@ -103,7 +104,7 @@ function MoreActions() {
);
}
type Tab = "variables" | "console" | "memory";
type Tab = "variables" | "console" | "memory" | "network";
/** The view the user last picked; the tool window opens on it again (Console until one is picked). */
let chosenTab: Tab = "console";
@@ -169,8 +170,10 @@ export function DebugToolWindow() {
: view.tab,
});
const hasMemory = !!active && sessionSupports("jvmMemory", active);
// The selected session may be one without the Memory tab.
const tab: Tab = view.tab === "memory" && !hasMemory ? "variables" : view.tab;
const hasNetwork = !!active && sessionSupports("jsPage", active);
// The selected session may be one without the Memory or the Network tab.
const tab: Tab =
(view.tab === "memory" && !hasMemory) || (view.tab === "network" && !hasNetwork) ? "variables" : view.tab;
if (!active) {
return (
@@ -227,6 +230,7 @@ export function DebugToolWindow() {
["variables", "Threads & Variables"],
["console", "Console"],
...(hasMemory ? [["memory", "Memory"] as const] : []),
...(hasNetwork ? [["network", "Network"] as const] : []),
] as const
).map(([id, label]) => (
<button
@@ -252,6 +256,8 @@ export function DebugToolWindow() {
<ThreadsAndVariables session={active} />
) : tab === "memory" ? (
<MemoryView key={active.id} session={active} />
) : tab === "network" ? (
<NetworkView key={active.id} session={active.id} />
) : (
<DebugConsole key={active.id} session={active} />
)}
+19
View File
@@ -1647,6 +1647,25 @@
"Debug in Browser": "Debug in Browser",
"Opens a URL in Chrome or Edge with the debugger attached (arg: the URL).": "Opens a URL in Chrome or Edge with the debugger attached (arg: the URL).",
"Debugging a script that starts Vite, Next.js or another dev server also opens its page in Chrome or Edge with the debugger attached.": "Debugging a script that starts Vite, Next.js or another dev server also opens its page in Chrome or Edge with the debugger attached.",
"(anonymous)": "(anonymous)",
"Filter by URL": "Filter by URL",
"Filter requests": "Filter requests",
"No request matches the filter": "No request matches the filter",
"Request details": "Request details",
"Request Headers": "Request Headers",
"Requested by": "Requested by",
"Requests": "Requests",
"Response body is no longer available": "Response body is no longer available",
"Response Headers": "Response Headers",
"Showing the first 1 MB": "Showing the first 1 MB",
"Time": "Time",
"Break on Next Request": "Break on Next Request",
"My code only": "My code only",
"Resource type": "Resource type",
"Doc": "Doc",
"Img": "Img",
"This version of the JavaScript debugger does not report a page's requests.": "This version of the JavaScript debugger does not report a page's requests.",
"No requests yet. Reload the page to see the requests of its load.": "No requests yet. Reload the page to see the requests of its load.",
"Evaluate watches while the program runs": "Evaluate watches while the program runs",
"Where the debugger can do it without suspending (JavaScript). A watch with side effects runs again on each refresh.": "Where the debugger can do it without suspending (JavaScript). A watch with side effects runs again on each refresh.",
"Stash Selected Files…": "Stash Selected Files…",
+19
View File
@@ -8034,6 +8034,25 @@
"Debug in Browser": "在浏览器中调试",
"Opens a URL in Chrome or Edge with the debugger attached (arg: the URL).": "在 Chrome 或 Edge 中打开 URL 并附加调试器(参数:URL)。",
"Debugging a script that starts Vite, Next.js or another dev server also opens its page in Chrome or Edge with the debugger attached.": "调试启动 Vite、Next.js 等开发服务器的脚本时,同时在 Chrome 或 Edge 中打开其页面并附加调试器。",
"(anonymous)": "(匿名)",
"Filter by URL": "按 URL 筛选",
"Filter requests": "筛选请求",
"No request matches the filter": "没有符合筛选条件的请求",
"Request details": "请求详情",
"Request Headers": "请求标头",
"Requested by": "发起者",
"Requests": "请求",
"Response body is no longer available": "响应正文已不可用",
"Response Headers": "响应标头",
"Showing the first 1 MB": "仅显示前 1 MB",
"Time": "时间",
"Break on Next Request": "下次请求时中断",
"My code only": "仅我的代码",
"Resource type": "资源类型",
"Doc": "文档",
"Img": "图片",
"This version of the JavaScript debugger does not report a page's requests.": "此版本的 JavaScript 调试器不报告页面的请求。",
"No requests yet. Reload the page to see the requests of its load.": "还没有请求。重新加载页面即可看到加载时的请求。",
"Evaluate watches while the program runs": "程序运行时计算监视表达式",
"Where the debugger can do it without suspending (JavaScript). A watch with side effects runs again on each refresh.": "仅适用于无需暂停即可计算的调试器(JavaScript)。带副作用的监视表达式在每次刷新时都会再次执行。"
}
+4
View File
@@ -100,6 +100,10 @@ const UNTRANSLATED = new Set<string>([
"GitHub",
"PHPStan",
"Xdebug",
// Resource kinds of a browser's network panel
"Fetch/XHR",
"JS",
"CSS",
"Chrome",
"Edge",
"Safari",