diff --git a/crates/ide-core/src/event.rs b/crates/ide-core/src/event.rs index 63a1df7a0..3cfd1acbb 100644 --- a/crates/ide-core/src/event.rs +++ b/crates/ide-core/src/event.rs @@ -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, + pub method: Option, + /// `Fetch`, `XHR`, `Script`, `Document`, … + pub resource_type: Option, + pub status: Option, + pub mime_type: Option, + /// The request's headers, or the response's. + pub headers: BTreeMap, + /// Bytes received over the network, once finished. + pub encoded_length: Option, + pub error_text: Option, + pub initiator: Option, } /// The backend that reads a debugged JVM's objects (instances, fields, referrers). diff --git a/crates/ide-dap/src/js_debug.rs b/crates/ide-dap/src/js_debug.rs index 4ba6f597d..aff3f4e02 100644 --- a/crates/ide-dap/src/js_debug.rs +++ b/crates/ide-dap/src/js_debug.rs @@ -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 { + v.get(key).and_then(Value::as_str).map(str::to_owned) +} + +fn headers(v: &Value) -> BTreeMap { + 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 { + 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 = 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 { + 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 { + 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 { + 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, ""); + } +} diff --git a/crates/ide-dap/src/lib.rs b/crates/ide-dap/src/lib.rs index 9fa8e8693..5deb6dc59 100644 --- a/crates/ide-dap/src/lib.rs +++ b/crates/ide-dap/src/lib.rs @@ -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(); } diff --git a/crates/ide-dap/src/session.rs b/crates/ide-dap/src/session.rs index 7e6126992..6bf2a2e40 100644 --- a/crates/ide-dap/src/session.rs +++ b/crates/ide-dap/src/session.rs @@ -839,6 +839,12 @@ async fn on_event(mgr: &Arc, s: &Arc, 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}"), } } diff --git a/crates/ide-dap/src/tests.rs b/crates/ide-dap/src/tests.rs index e02ba5c04..1c1544ffd 100644 --- a/crates/ide-dap/src/tests.rs +++ b/crates/ide-dap/src/tests.rs @@ -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 Option>, } +/// 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(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 { + 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]"); diff --git a/crates/ide-host/src/ops/debug.rs b/crates/ide-host/src/ops/debug.rs index c9f005d82..fb28e2a1a 100644 --- a/crates/ide-host/src/ops/debug.rs +++ b/crates/ide-host/src/ops/debug.rs @@ -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 { + host.debugger.js_response_body(session, request_id).await +} + pub fn debug_capabilities(host: &Host, _window: &str, session: DebugSessionId) -> IdeResult { 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 }); diff --git a/src-tauri/src/commands/debug.rs b/src-tauri/src/commands/debug.rs index 3001aa8af..bf7f02288 100644 --- a/src-tauri/src/commands/debug.rs +++ b/src-tauri/src/commands/debug.rs @@ -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_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] diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs index b3257c185..7cceb1f1a 100644 --- a/src-tauri/src/commands/mod.rs +++ b/src-tauri/src/commands/mod.rs @@ -528,6 +528,7 @@ pub fn builder() -> tauri_specta::Builder { 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, diff --git a/src/bindings.ts b/src/bindings.ts index 0773ef92e..7176eea07 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -1216,6 +1216,8 @@ export const commands = { debugCapabilities: (session: DebugSessionId) => typedError(__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(__TAURI_INVOKE("debug_js_custom_breakpoints", { session, ids, xhr })), + /** The body of a response a debugged page received. */ + debugJsResponseBody: (session: DebugSessionId, requestId: string) => typedError(__TAURI_INVOKE("debug_js_response_body", { session, requestId })), /** Method breakpoints (all files); states aligned with `breakpoints`. */ debugSetFunctionBreakpoints: (breakpoints: FunctionBreakpoint[]) => typedError(__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. */ diff --git a/src/features/debugger/__tests__/EventBreakpointsSection.test.tsx b/src/features/debugger/__tests__/EventBreakpointsSection.test.tsx index dd5af3ee8..f009b8ac7 100644 --- a/src/features/debugger/__tests__/EventBreakpointsSection.test.tsx +++ b/src/features/debugger/__tests__/EventBreakpointsSection.test.tsx @@ -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(); }); }); diff --git a/src/features/debugger/__tests__/NetworkView.test.tsx b/src/features/debugger/__tests__/NetworkView.test.tsx new file mode 100644 index 000000000..37a7c34c1 --- /dev/null +++ b/src/features/debugger/__tests__/NetworkView.test.tsx @@ -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(); + 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 => ({ + 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(); + 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(); + 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(); + 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(); + 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(); + 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(); + fireEvent.click(screen.getAllByRole("row")[1]!); + await waitFor(() => expect(screen.getByText("Response body is no longer available")).toBeInTheDocument()); + }); + + it("clears the list", () => { + load(); + render(); + 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(); + 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(); + 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(); + 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(); + // 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(); + 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["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); + }); +}); diff --git a/src/features/debugger/__tests__/network.test.ts b/src/features/debugger/__tests__/network.test.ts new file mode 100644 index 000000000..9d1d116af --- /dev/null +++ b/src/features/debugger/__tests__/network.test.ts @@ -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) => 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); + }); +}); diff --git a/src/features/debugger/__tests__/networkSessions.test.ts b/src/features/debugger/__tests__/networkSessions.test.ts new file mode 100644 index 000000000..5d86228ff --- /dev/null +++ b/src/features/debugger/__tests__/networkSessions.test.ts @@ -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["sessions"]; +function sessions(ids: number[]) { + const out: Record = {}; + 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(); + }); +}); diff --git a/src/features/debugger/index.ts b/src/features/debugger/index.ts index 8bcafdacb..cb3917fe7 100644 --- a/src/features/debugger/index.ts +++ b/src/features/debugger/index.ts @@ -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()); } diff --git a/src/features/debugger/js/Network.module.css b/src/features/debugger/js/Network.module.css new file mode 100644 index 000000000..0feb40833 --- /dev/null +++ b/src/features/debugger/js/Network.module.css @@ -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; +} diff --git a/src/features/debugger/js/NetworkView.tsx b/src/features/debugger/js/NetworkView.tsx new file mode 100644 index 000000000..a643c7278 --- /dev/null +++ b/src/features/debugger/js/NetworkView.tsx @@ -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 ( +
onSelect(row.key)} + title={row.url} + > + + {row.name} + + {row.error ?? row.status ?? ""} + {row.method} + {row.type} + + {formatSize(row.size)} + + + {formatDuration(row.durationMs)} + +
+ ); +}); + +function Headers({ title, headers }: { title: string; headers: Record }) { + const names = Object.keys(headers).sort((a, b) => a.localeCompare(b)); + if (!names.length) return null; + return ( + <> +
{title}
+ {names.map((name) => ( +
+ {name} + {headers[name]} +
+ ))} + + ); +} + +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({ 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 ( +
+ Response body is no longer available +
+ ); + if (body.body.base64Encoded) + return ( +
+ Binary content +
+ ); + return ( + <> + {body.body.truncated && ( +
+ Showing the first 1 MB +
+ )} +
{body.body.body}
+ + ); +} + +function Detail({ session, row }: { session: number; row: NetworkRow }) { + const tx = useTx(); + return ( +
+
{row.url}
+
+ {[row.method, row.error ?? row.status, row.mimeType].filter((part) => part != null && part !== "").join(" · ")} +
+ {row.initiator && ( +
+ {tx("Requested by")} {row.initiator.function || tx("(anonymous)")} — {row.initiator.url}: + {row.initiator.line + 1} +
+ )} +
+ +
+ + + {row.done && !row.error && ( + <> +
{tx("Response")}
+ + + )} +
+ ); +} + +/** + * 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("all"); + const [mine, setMine] = useState(false); + const [selectedKey, setSelectedKey] = useState(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 ( +
+
+ setQuery(e.target.value)} + placeholder="Filter by URL" + aria-label="Filter requests" + /> +
+ {NETWORK_CATEGORIES.map((c) => ( + + ))} +
+ + reloadPage(session)} /> + clearNetwork(session)} /> + + {shown.length === rows.length ? rows.length : `${shown.length} / ${rows.length}`} + +
+
+
+
+ {tx("Name")} + {tx("Status")} + {tx("Method")} + {tx("Type")} + {tx("Size")} + {tx("Time")} +
+ {shown.length === 0 && ( +
+ {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.")} +
+ )} + {shown.map((row) => ( + + ))} +
+ {selected && } +
+
+ ); +} diff --git a/src/features/debugger/js/network.ts b/src/features/debugger/js/network.ts new file mode 100644 index 000000000..14bbe9e14 --- /dev/null +++ b/src/features/debugger/js/network.ts @@ -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; + responseHeaders: Record; + initiator: JsInitiator | null; +} + +/** Rows kept per session; the oldest go first. */ +export const MAX_ROWS = 2000; + +export const useJsNetwork = create<{ rows: Record }>(() => ({ 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(); +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 = { + 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): 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)), + ); +} diff --git a/src/features/debugger/js/networkSessions.ts b/src/features/debugger/js/networkSessions.ts new file mode 100644 index 000000000..bb6f19760 --- /dev/null +++ b/src/features/debugger/js/networkSessions.ts @@ -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); + }); +} diff --git a/src/features/debugger/js/refusals.ts b/src/features/debugger/js/refusals.ts index d03baf140..f43ec1df5 100644 --- a/src/features/debugger/js/refusals.ts +++ b/src/features/debugger/js/refusals.ts @@ -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>> }>(() => ({ @@ -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; diff --git a/src/features/debugger/state.ts b/src/features/debugger/state.ts index cc9fc10ee..1997c001f 100644 --- a/src/features/debugger/state.ts +++ b/src/features/debugger/state.ts @@ -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; diff --git a/src/features/debugger/ui/DebugToolWindow.tsx b/src/features/debugger/ui/DebugToolWindow.tsx index e7c2ba4a3..0d2bbf311 100644 --- a/src/features/debugger/ui/DebugToolWindow.tsx +++ b/src/features/debugger/ui/DebugToolWindow.tsx @@ -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]) => (