diff --git a/Cargo.lock b/Cargo.lock index d7a35676610..34835d8dafe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2963,9 +2963,9 @@ name = "moli-fetch" version = "0.1.0" dependencies = [ "anyhow", - "bytes", "base64 0.22.1", "brotlic-sys", + "bytes", "cidr", "crossbeam-channel", "curl", diff --git a/Cargo.toml b/Cargo.toml index f93a114cbaa..e20d2c625be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -124,7 +124,7 @@ url = { path = "vendor/url-2.5.8" } html5ever = { path = "vendor/html5ever-0.40.1" } xml5ever = { git = "https://github.com/lexmount/html5ever", rev = "4574311c63e79bba7d4ddb98cb0562d8dad499ee" } curl = { git = "https://github.com/lexmount/curl-rust", rev = "0392e12475d8d1160d4cae07c172cac02797cc25" } -curl-sys = { git = "https://github.com/lexmount/curl-rust", rev = "0392e12475d8d1160d4cae07c172cac02797cc25" } +curl-sys = { path = "vendor/curl-sys-0.4.91" } cookie = { git = "https://github.com/ldm0/cookie-rs", branch = "priority" } v8 = { path = "vendor/v8-152.2.0" } deno_v8 = { path = "vendor/deno_v8-0.3.0" } @@ -163,3 +163,6 @@ codegen-units = 1 [profile.test] debug = 1 + +[patch."https://github.com/lexmount/curl-rust"] +curl-sys = { path = "vendor/curl-sys-0.4.91" } diff --git a/moli-fetch/src/cancellation.rs b/moli-fetch/src/cancellation.rs index 630d10fb504..9b2459df34b 100644 --- a/moli-fetch/src/cancellation.rs +++ b/moli-fetch/src/cancellation.rs @@ -93,6 +93,13 @@ impl ResponseProgress { timing.body_bytes_received |= bytes > 0; } + pub(crate) fn record_encoded_response_body_chunk(&self, bytes: usize) { + let mut timing = self.inner.timing.lock(); + let sizes = timing.body_sizes.get_or_insert_with(Default::default); + sizes.encoded = sizes.encoded.saturating_add(bytes as u64); + timing.body_bytes_received |= bytes > 0; + } + pub(crate) fn record_decoded_response_body_size(&self, bytes: usize) { let mut timing = self.inner.timing.lock(); let sizes = timing.body_sizes.get_or_insert_with(Default::default); diff --git a/moli-fetch/src/compression_dictionary/mod.rs b/moli-fetch/src/compression_dictionary/mod.rs index 40944cce5f7..7ee4b0ceaef 100644 --- a/moli-fetch/src/compression_dictionary/mod.rs +++ b/moli-fetch/src/compression_dictionary/mod.rs @@ -2,7 +2,6 @@ //! requests which may use them. Bytes enter the store only after a readable //! response completes; a header alone cannot register a dictionary. -mod decoding; mod fields; use std::{borrow::Cow, collections::VecDeque, fmt, io::Read, sync::Arc}; @@ -17,7 +16,6 @@ use url::Url; use crate::{FetchConfig, Request, RequestMode}; -pub(crate) use decoding::DictionaryDecoding; pub(crate) use fields::RegistrationOptions; const MAX_DICTIONARY_BYTES: usize = 64 * 1024 * 1024; @@ -392,18 +390,6 @@ impl DictionaryResponse { } } - pub(crate) fn headers(&self) -> &[(String, Vec)] { - &self.headers - } - - pub(crate) fn headers_finalized(&self) -> bool { - self.headers_finalized - } - - pub(crate) fn bodyless(&self) -> bool { - self.request.head || matches!(self.status, 204 | 205 | 304) - } - pub(crate) fn from_cached( store: SharedDictionaryStore, request: &Request, diff --git a/moli-fetch/src/content_coding.rs b/moli-fetch/src/content_coding.rs index e8bbb2d5722..adf78bcdff8 100644 --- a/moli-fetch/src/content_coding.rs +++ b/moli-fetch/src/content_coding.rs @@ -1,6 +1,8 @@ use moli_header_field::{HeaderFieldTokenMode, HeaderFieldTokenizer}; fn supported_content_codings() -> impl Iterator { + // The bounded native decoder supplies the same coding set even when the + // linked libcurl lacks Zstandard or external-dictionary support. ["deflate", "gzip", "br", "zstd", "dcb", "dcz"].into_iter() } diff --git a/moli-fetch/src/compression_dictionary/decoding.rs b/moli-fetch/src/content_decoding.rs similarity index 86% rename from moli-fetch/src/compression_dictionary/decoding.rs rename to moli-fetch/src/content_decoding.rs index 87c0da92369..bee1dfdf9d8 100644 --- a/moli-fetch/src/compression_dictionary/decoding.rs +++ b/moli-fetch/src/content_decoding.rs @@ -1,6 +1,6 @@ -//! Decode response content with the linked Brotli/zlib/Zstandard libraries. -//! The same stack validates completion and counts wire bytes for ordinary and -//! dictionary-compressed responses, independently of libcurl's decoder build. +//! Bounded content decoding shared by ordinary and dictionary responses. +//! Linked zlib, Brotli and Zstandard decoders validate every negotiated response, +//! including completion, independently of the libcurl build configuration. use std::{ ffi::c_void, @@ -11,41 +11,68 @@ use std::{ use anyhow::{Result, anyhow, bail, ensure}; use flate2::{Decompress, FlushDecompress, Status}; -use super::Dictionary; +use crate::{Request, compression_dictionary::Dictionary}; const OUTPUT_SIZE: usize = 16 * 1024; type Output<'a> = dyn FnMut(&[u8]) -> Result<()> + 'a; -pub(crate) struct DictionaryDecoding { +pub(crate) struct ContentDecoding { dictionary: Option>, stages: Option>, - pub(crate) encoded_bytes: u64, + head: bool, + status: u16, + headers: Vec<(String, Vec)>, + headers_finalized: bool, } -impl DictionaryDecoding { - pub(crate) fn new(dictionary: Option>) -> Self { +impl ContentDecoding { + pub(crate) fn new(request: &Request) -> Self { Self { - dictionary, + dictionary: request.compression_dictionary.clone(), stages: None, - encoded_bytes: 0, + head: request.method == "HEAD", + status: 0, + headers: Vec::new(), + headers_finalized: false, } } - pub(crate) fn reset(&mut self) { - self.stages = None; - self.encoded_bytes = 0; + pub(crate) fn header(&mut self, data: &[u8]) -> Result<()> { + if let Some(line) = crate::response::parse_http_response_status_line(data) { + self.status = line.status; + self.headers.clear(); + self.stages = None; + self.headers_finalized = false; + return Ok(()); + } + if self.headers_finalized { + return Ok(()); + } + if let Some(header) = crate::headers::parse_http_response_header_line(data) { + self.headers.push(header); + return Ok(()); + } + if self.status >= 200 && data.iter().all(|byte| matches!(byte, b'\r' | b'\n')) { + self.headers_finalized = true; + } + Ok(()) } - pub(crate) fn uses_dictionary(&self) -> bool { - self.dictionary.is_some() + pub(crate) fn needs_readable_dictionary_response(&self) -> bool { + self.dictionary.is_some() && self.headers_finalized && !self.bodyless() } - pub(crate) fn initialize(&mut self, headers: &[(String, Vec)]) -> Result<()> { + fn bodyless(&self) -> bool { + self.head || matches!(self.status, 204 | 205 | 304) + } + + fn initialize(&mut self) -> Result<()> { if self.stages.is_some() { return Ok(()); } let mut names = Vec::new(); - for (_, value) in headers + for (_, value) in self + .headers .iter() .filter(|(name, _)| name.eq_ignore_ascii_case("content-encoding")) { @@ -67,9 +94,9 @@ impl DictionaryDecoding { "zstd" => Stage::Zstd(Zstd::new(None)?), "dcb" | "dcz" => Stage::Dictionary { brotli: name == "dcb", - dictionary: Arc::clone(self.dictionary.as_ref().ok_or_else(|| { - anyhow!("dictionary compression without a negotiated dictionary") - })?), + dictionary: self.dictionary.clone().ok_or_else(|| { + anyhow!("dictionary content encoding without a selected dictionary") + })?, prefix: Vec::new(), decoder: None, }, @@ -83,9 +110,10 @@ impl DictionaryDecoding { pub(crate) fn push(&mut self, input: &[u8], output: &mut Output<'_>) -> Result<()> { ensure!( - self.stages.is_some(), + self.headers_finalized, "compressed response body arrived before headers" ); + self.initialize()?; feed( self.stages.as_mut().expect("initialized decoding stack"), input, @@ -93,16 +121,12 @@ impl DictionaryDecoding { ) } - pub(crate) fn finish(&mut self, headers: &[(String, Vec)], bodyless: bool) -> Result<()> { - if bodyless { + pub(crate) fn finish(&mut self) -> Result<()> { + // An empty encoded body never invokes a decoder, including for an + // unknown coding. A decoder that received bytes must finish cleanly. + if self.bodyless() || self.stages.is_none() { return Ok(()); } - // An empty payload never enters a content decoder. HEAD and null-body - // responses likewise do not validate an unused coding declaration. - if self.encoded_bytes == 0 { - return Ok(()); - } - self.initialize(headers)?; for stage in self.stages.as_ref().expect("initialized decoding stack") { ensure!(stage.finished(), "truncated compressed response"); } diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rs index 4c39359aa83..ee56508300c 100644 --- a/moli-fetch/src/lib.rs +++ b/moli-fetch/src/lib.rs @@ -12,6 +12,7 @@ mod client_hints; mod compression_dictionary; mod config; mod content_coding; +mod content_decoding; mod cors; mod cross_origin_resource_policy; mod dns; diff --git a/moli-fetch/src/runtime.rs b/moli-fetch/src/runtime.rs index fde29fe7501..06ceb3e43e4 100644 --- a/moli-fetch/src/runtime.rs +++ b/moli-fetch/src/runtime.rs @@ -1559,19 +1559,12 @@ impl RuntimeOwner { state: &mut OwnerState, mut completion: RuntimeCurlCompletion, ) { - let cancelled = match &completion.context { - ActiveTransferContext::Buffered(context) => context.job.cancel_handle.is_cancelled(), - ActiveTransferContext::Streaming(context) => context.job.cancel_handle.is_cancelled(), - ActiveTransferContext::StreamingRaw(context) => { - context.job.cancel_handle.is_cancelled() - } - }; if let Some(easy) = completion.easy.as_mut() { - if let Some(error) = easy.get_mut().dictionary_error.take() { + if let Some(error) = easy.get_mut().content_decoding_error.take() { completion.result = Err(anyhow!(error)); } else if completion.result.is_ok() && !self.shutdown_requested.load(Ordering::SeqCst) - && !cancelled + && !completion.context.is_cancelled() { completion.result = easy.get_mut().finish_dictionary_response(); } @@ -2887,6 +2880,14 @@ enum ActiveTransferContext { } impl ActiveTransferContext { + fn is_cancelled(&self) -> bool { + match self { + Self::Buffered(context) => context.job.cancel_handle.is_cancelled(), + Self::Streaming(context) => context.job.cancel_handle.is_cancelled(), + Self::StreamingRaw(context) => context.job.cancel_handle.is_cancelled(), + } + } + fn into_buffered(self) -> Option { match self { Self::Buffered(context) => Some(*context), @@ -2941,8 +2942,8 @@ pub(crate) struct FetchTransferHandler { upload_observer: Option, proxy_connect_response_recorder: ProxyConnectResponseRecorder, dictionary_response: Option, - dictionary_decoding: Option, - dictionary_error: Option, + content_decoding: Option, + content_decoding_error: Option, } enum FetchResponseCollector { @@ -2989,8 +2990,8 @@ impl FetchTransferHandler { upload_observer: None, proxy_connect_response_recorder: ProxyConnectResponseRecorder::default(), dictionary_response: None, - dictionary_decoding: None, - dictionary_error: None, + content_decoding: None, + content_decoding_error: None, } } @@ -3057,20 +3058,17 @@ impl FetchTransferHandler { url.clone(), ) }); - self.dictionary_decoding = Some(crate::compression_dictionary::DictionaryDecoding::new( - request.compression_dictionary.clone(), - )); - self.dictionary_error = None; + self.content_decoding = Some(crate::content_decoding::ContentDecoding::new(request)); + self.content_decoding_error = None; } fn finish_dictionary_response(&mut self) -> Result<()> { - let Some(response) = self.dictionary_response.as_mut() else { - return Ok(()); - }; - if let Some(decoding) = self.dictionary_decoding.as_mut() { - decoding.finish(response.headers(), response.bodyless())?; + if let Some(decoding) = self.content_decoding.as_mut() { + decoding.finish()?; + } + if let Some(response) = self.dictionary_response.as_mut() { + response.finish(); } - response.finish(); Ok(()) } @@ -3108,7 +3106,7 @@ impl Handler for FetchTransferHandler { if !data.is_empty() { self.mark_response_body_received(); } - if self.dictionary_decoding.is_none() { + if self.content_decoding.is_none() { if let Some(timing) = self.response_progress() { timing.record_decoded_response_body_size(data.len()); } @@ -3126,24 +3124,13 @@ impl Handler for FetchTransferHandler { } return result; } - if self.dictionary_error.is_some() { - return Ok(0); - } let timing = self.response_progress().cloned(); - let decoding = self - .dictionary_decoding - .as_mut() - .expect("content decoding selected"); - decoding.encoded_bytes = decoding.encoded_bytes.saturating_add(data.len() as u64); + // A decoding failure can abort write before curl reports progress. + // Count the wire bytes at this boundary when we own content decoding. if let Some(timing) = &timing { - timing.record_encoded_response_body_size(decoding.encoded_bytes); + timing.record_encoded_response_body_chunk(data.len()); } - if let Err(error) = decoding.initialize( - self.dictionary_response - .as_ref() - .map_or(&[], |response| response.headers()), - ) { - self.dictionary_error = Some(error.to_string()); + if self.content_decoding_error.is_some() { return Ok(0); } let policy = &mut self.dictionary_response; @@ -3168,14 +3155,14 @@ impl Handler for FetchTransferHandler { Ok(()) }; let result = self - .dictionary_decoding + .content_decoding .as_mut() - .expect("dictionary decoding selected") + .expect("native content decoding selected") .push(data, &mut output); match result { Ok(()) => Ok(data.len()), Err(error) => { - self.dictionary_error = Some(error.to_string()); + self.content_decoding_error = Some(error.to_string()); Ok(0) } } @@ -3184,24 +3171,24 @@ impl Handler for FetchTransferHandler { fn header(&mut self, data: &[u8]) -> bool { if crate::response::parse_http_response_status_line(data) .is_some_and(|line| line.status >= 200) - && let Some(decoding) = self.dictionary_decoding.as_mut() { - decoding.reset(); - self.dictionary_error = None; + self.content_decoding_error = None; } if let Some(response) = self.dictionary_response.as_mut() { response.header(data); } - if data.iter().all(|byte| matches!(byte, b'\r' | b'\n')) - && let Some(response) = self.dictionary_response.as_ref() - && response.headers_finalized() - && !response.bodyless() - && let Some(decoding) = self.dictionary_decoding.as_mut() - && decoding.uses_dictionary() - && !response.readable() - { - self.dictionary_error = - Some("dictionary response failed its CORS readability check".to_owned()); + if let Some(decoding) = self.content_decoding.as_mut() { + if let Err(error) = decoding.header(data) { + self.content_decoding_error = Some(error.to_string()); + } else if decoding.needs_readable_dictionary_response() + && self + .dictionary_response + .as_ref() + .is_none_or(|response| !response.readable()) + { + self.content_decoding_error = + Some("dictionary response failed its CORS readability check".to_owned()); + } } if crate::response::parse_http_response_status_line(data) .is_some_and(|line| line.status >= 200) @@ -3223,7 +3210,8 @@ impl Handler for FetchTransferHandler { if dlnow > 0.0 { self.mark_response_body_received(); } - if dlnow > 0.0 + if self.content_decoding.is_none() + && dlnow > 0.0 && let Some(timing) = self.response_progress() { timing.record_encoded_response_body_size(dlnow as u64); diff --git a/moli-fetch/src/tests/cache_request_directives.rs b/moli-fetch/src/tests/cache_request_directives.rs index 83e97881700..3c6573b6870 100644 --- a/moli-fetch/src/tests/cache_request_directives.rs +++ b/moli-fetch/src/tests/cache_request_directives.rs @@ -14,13 +14,12 @@ async fn cache_only_requests_preserve_cancellation_before_local_completion() -> for follow_redirects in [true, false] { let cancel = FetchCancelHandle::new(); cancel.cancel(); - let request = controlled(&server.url(), "only-if-cached")?.with_redirect_handling( - if follow_redirects { - crate::RequestRedirectHandling::Follow - } else { - crate::RequestRedirectHandling::FollowByCaller - }, - ); + let request = controlled(&server.url(), "only-if-cached")?; + let request = if follow_redirects { + request + } else { + request.with_single_hop() + }; let error = client .fetch_with_cancel(request, cancel) .await @@ -56,11 +55,11 @@ async fn cache_only_misses_never_contact_origin_on_raw_text_html_or_buffered_pat response.head() } 1 | 2 => { - let request = request.with_redirect_handling(if path == 1 { - crate::RequestRedirectHandling::Follow + let request = if path == 1 { + request } else { - crate::RequestRedirectHandling::FollowByCaller - }); + request.with_single_hop() + }; let observed = client .fetch_with_cancel_and_network_metadata(request, cancel.clone()) .await?; @@ -249,11 +248,7 @@ async fn disk_cache_only_redirects_stop_at_uncached_hops_without_network() -> Re let mut config = FetchConfig::default(); config.set_http_cache_dir(Some(cache_dir.display().to_string())); let client = FetchClient::new(&config, new_shared_browser_cookie_store()); - let prime = Request::get(&server.url())?.with_redirect_handling(if false { - crate::RequestRedirectHandling::Follow - } else { - crate::RequestRedirectHandling::FollowByCaller - }); + let prime = Request::get(&server.url())?.with_single_hop(); assert_eq!(client.fetch_raw(prime).await?.status, 302); let request = controlled(&server.url(), "only-if-cached")?; let head = if html { diff --git a/moli-fetch/src/tests/upload.rs b/moli-fetch/src/tests/upload.rs index c8ada0139c5..19024f06602 100644 --- a/moli-fetch/src/tests/upload.rs +++ b/moli-fetch/src/tests/upload.rs @@ -3,15 +3,15 @@ use crate::{UploadEvent, UploadObserver}; async fn upload_response( client: &FetchClient, - mut request: Request, + request: Request, transport: &str, cancel: FetchCancelHandle, ) -> Result { match transport { - "buffered" => { - request = request.with_single_hop(); - Ok(client.fetch_with_cancel(request, cancel).await?.status) - } + "buffered" => Ok(client + .fetch_with_cancel(request.with_single_hop(), cancel) + .await? + .status), "html" => { let mut response = client.fetch_html_stream(request).await?; while response.next_chunk().await.is_some() {} diff --git a/moli-renderer-v8/src/network/backend/memory_cache.rs b/moli-renderer-v8/src/network/backend/memory_cache.rs index 1f90d7f2734..7ce107ebf87 100644 --- a/moli-renderer-v8/src/network/backend/memory_cache.rs +++ b/moli-renderer-v8/src/network/backend/memory_cache.rs @@ -1016,7 +1016,7 @@ fn raw_subresource_request_is_memory_cacheable( // A CORS-managed hop carries the logical request's earlier URLs. It // can cache this hop without treating the prefix as a followed // transport redirect; its origin/credential taint is part of the key. - && (request.redirect_chain().is_empty() || !request.follow_redirects) + && (request.redirect_chain().is_empty() || !request.transport_follows_redirects()) && raw_subresource_memory_cacheable_resource_type(request) && request.method.eq_ignore_ascii_case("GET") && request.body.is_none() @@ -1491,7 +1491,7 @@ mod tests { .with_request_origin((&initiator).into()) .with_initiator_url(&initiator); let per_hop = automatic.clone().with_single_hop(); - let key = raw_subresource_memory_cache_key(&per_hop, per_hop.headers()) + let key = raw_subresource_memory_cache_key(&per_hop, &per_hop.request_headers) .expect("cacheable first CORS hop"); let mut cache = SharedMemoryResourceCache::with_limits(usize::MAX, usize::MAX); cache.insert_raw_subresource( diff --git a/moli-renderer-v8/src/network/request_client.rs b/moli-renderer-v8/src/network/request_client.rs index c4980be4ed6..ab280f83c8e 100644 --- a/moli-renderer-v8/src/network/request_client.rs +++ b/moli-renderer-v8/src/network/request_client.rs @@ -1270,7 +1270,7 @@ impl ResourceRequestClient { )); } if response.status == 304 - && (!response.redirected || !request.follow_redirects) + && (!response.redirected || !request.transport_follows_redirects()) && response.final_url == request.url && let Some(cached) = cached { @@ -1358,7 +1358,7 @@ impl ResourceRequestClient { let (body_tx, body_rx) = tokio::sync::mpsc::unbounded_channel(); let (completion_tx, completion_rx) = tokio::sync::oneshot::channel(); let resource_runtime = self.resource_runtime.clone(); - let outward_cancel = FetchCancelHandle::new(); + let outward_cancel = response.cancellation_handle().child_for_subrequest(); response.share_response_progress(&outward_cancel); tokio::spawn(async move { let mut body = Vec::new(); @@ -1396,7 +1396,7 @@ impl ResourceRequestClient { if completion.as_ref().is_some_and(Result::is_ok) { let mut materialized = RawResponse::from_head_and_body(response.head(), body) .with_body_sizes(response.response_timing().body_sizes()); - if !request.follow_redirects { + if !request.transport_follows_redirects() { // The outer CORS loop owns the logical redirect history. // Retain only this hop's representation, then let each // consumer attach its own already-validated URL list. diff --git a/moli-renderer-v8/src/network/request_client/tests/raw_cache_revalidation/directives.rs b/moli-renderer-v8/src/network/request_client/tests/raw_cache_revalidation/directives.rs index f156a711714..bd508ce6341 100644 --- a/moli-renderer-v8/src/network/request_client/tests/raw_cache_revalidation/directives.rs +++ b/moli-renderer-v8/src/network/request_client/tests/raw_cache_revalidation/directives.rs @@ -231,7 +231,7 @@ async fn raw_memory_cache_only_redirects_stop_at_uncached_targets() -> Result<() // Prime the complete redirect representation independently. The browser // redirect loop may drop an unread body before its cache tee completes. assert_eq!( - fetch(&client, request().with_follow_redirects(false), false) + fetch(&client, request().with_single_hop(), false) .await? .0 .status, diff --git a/moli-renderer-v8/src/network_host/async_fetch.rs b/moli-renderer-v8/src/network_host/async_fetch.rs index 802e89d977b..91eeff5912e 100644 --- a/moli-renderer-v8/src/network_host/async_fetch.rs +++ b/moli-renderer-v8/src/network_host/async_fetch.rs @@ -1000,7 +1000,6 @@ async fn fetch_response_head_once( .map(|response| response.head()); } - let cancel_handle = cancel_handle.unwrap_or_default(); let response = loader .fetch_raw_stream_with_cancel(request, cancel_handle) .await @@ -1247,6 +1246,7 @@ mod tests { redirect_chain: Vec::new(), from_cache: false, cache_state: Default::default(), + preload_state: Default::default(), negotiated_http_version: None, }, None, diff --git a/moli-renderer-v8/src/script_vm/tests/fetch_resource_timing.rs b/moli-renderer-v8/src/script_vm/tests/fetch_resource_timing.rs index 70631df9dcc..2ad7a670d9f 100644 --- a/moli-renderer-v8/src/script_vm/tests/fetch_resource_timing.rs +++ b/moli-renderer-v8/src/script_vm/tests/fetch_resource_timing.rs @@ -60,7 +60,7 @@ async fn handle_timing_request( if url.path() == "/compressed-worker.js" { let body = r#"self.onmessage = async event => { const probe = (0, eval)('(' + event.data.source + ')'); - postMessage(await probe(event.data.origins, event.data.label)); + postMessage(await probe(event.data.origins, event.data.label, event.data.filter)); close(); };"#; let head = format!( @@ -399,14 +399,23 @@ fn xhr_resource_timing_reads_internal_headers_before_cors_exposure() { } async fn run_resource_timing_fixture(fixture: &str, expected_total: usize) { + run_resource_timing_fixture_scenario(fixture, expected_total, None).await; +} + +async fn run_resource_timing_fixture_scenario( + fixture: &str, + expected_total: usize, + scenario: Option<&serde_json::Value>, +) -> serde_json::Value { let first = timing_server().await; let second = timing_server().await; let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap(); let mut page = new_storage_page_task_executor_test_vm_with_loader(&format!("{}/", first.origin), &loader); page.eval(&format!( - "globalThis.__fetchOrigins = {}; globalThis.__fetchTimingResult = null;", + "globalThis.__fetchOrigins = {}; globalThis.__fetchTimingResult = null;globalThis.__compressedTimingScenario = {};", serde_json::to_string(&[&first.origin, &second.origin]).unwrap(), + serde_json::to_string(&scenario).unwrap(), )) .unwrap(); page.eval(&format!( @@ -426,13 +435,58 @@ async fn run_resource_timing_fixture(fixture: &str, expected_total: usize) { .unwrap_or_else(|error| panic!("fetch timing probe returned {result:?}: {error}")); assert_eq!(result["total"], expected_total); assert_eq!(result["failures"], serde_json::json!([])); + result } #[tokio::test(flavor = "current_thread")] async fn compressed_resource_timing_sizes_survive_window_child_worker_and_memory_cache() { - run_resource_timing_fixture( - include_str!("../../../tests/fixtures/compressed-request-resource-timing.js"), + let mut names = std::collections::BTreeSet::new(); + for realm in ["window", "child", "worker"] { + let apis = if realm == "worker" { + ["fetch", "xhr", "sync-xhr"].as_slice() + } else { + ["fetch", "xhr"].as_slice() + }; + for api in apis { + let modes = if *api == "fetch" { + [ + "same", + "cors", + "cors-tao", + "denied", + "opaque", + "opaque-tao", + "cache", + ] + .as_slice() + } else { + ["same", "cors", "cors-tao", "denied", "cache"].as_slice() + }; + for mode in modes { + let scenario = serde_json::json!({"realm": realm, "api": api, "mode": mode}); + let total = if *mode == "cache" { 8 } else { 15 }; + let result = run_resource_timing_fixture_scenario( + include_str!("../../../tests/fixtures/compressed-request-resource-timing.js"), + total, + Some(&scenario), + ) + .await; + let checks = result["names"].as_array().unwrap(); + assert_eq!(checks.len(), total, "{scenario}"); + for check in checks { + let name = check.as_str().unwrap(); + assert!( + name.starts_with(&format!("{realm}: {api}/")), + "{scenario}: {name}" + ); + assert!(names.insert(name.to_owned()), "duplicate check: {name}"); + } + } + } + } + assert_eq!( + names.len(), 566, - ) - .await; + "all Window, child, worker, coding, API and cache cases must run" + ); } diff --git a/moli-renderer-v8/tests/fixtures/compressed-request-resource-timing.js b/moli-renderer-v8/tests/fixtures/compressed-request-resource-timing.js index 8a8eed1c764..cd42b4e129d 100644 --- a/moli-renderer-v8/tests/fixtures/compressed-request-resource-timing.js +++ b/moli-renderer-v8/tests/fixtures/compressed-request-resource-timing.js @@ -1,5 +1,5 @@ (async () => { - const probe = async (origins, label) => { + const probe = async (origins, label, filter) => { const rows = []; globalThis.__compressedTimingProgress = rows; const checks = async (name, run) => { @@ -49,6 +49,7 @@ ]; let next = 0; for (const api of apis) for (const mode of ['same', 'cors', 'cors-tao', 'denied', ...(api === 'fetch' ? ['opaque', 'opaque-tao'] : [])]) for (const spec of specs) { + if (filter && (filter.api !== api || filter.mode !== mode)) continue; await checks(`${api}/${mode}/${spec.kind}/${spec.chunked ? 'chunked' : 'length'}/${spec.method || 'GET'}`, async () => { const url = new URL('/compressed', origins[mode === 'same' ? 0 : 1]); url.searchParams.set('key', `${label}-${++next}`); @@ -75,6 +76,7 @@ }); } for (const api of apis) for (const {kind, encoded} of [{kind: 'gzip', encoded: 23}, {kind: 'zstd', encoded: 12}]) { + if (filter && (filter.api !== api || filter.mode !== 'cache')) continue; const base = `${origins[0]}/compressed?kind=${kind}&cache=1&key=${label}-${api}-cache`; for (let phase = 0; phase < 4; ++phase) await checks(`${api}/${kind}/cache/${phase}`, async () => { const url = base + '#phase-' + phase; @@ -86,28 +88,36 @@ assert(performance.getEntriesByName(url, 'resource').length === 1, 'Duplicate cached timing'); }); } - return {total: rows.length, failures: rows.filter(row => !row.passed)}; + return {total: rows.length, failures: rows.filter(row => !row.passed), names: rows.map(row => row.name)}; }; - const results = [await probe(__fetchOrigins, 'window')]; - const iframe = document.createElement('iframe'); - const child = new Promise((resolve, reject) => { - const timer = setTimeout(() => reject(Error('Child timing probe did not finish')), 30000); - const receive = event => { - if (event.source !== iframe.contentWindow || event.data?.kind !== 'compressed-timing') return; - clearTimeout(timer); removeEventListener('message', receive); resolve(event.data.result); - }; - addEventListener('message', receive); - }); - iframe.srcdoc = '