From e0ea2537bccf422c6ffbb5930e5f2aae3090ea72 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Thu, 10 Sep 2026 17:29:58 +0800 Subject: [PATCH] fix(fetch): preserve and clone Request stream bodies Retain ReadableStream BodyInit values instead of extracting empty bytes. Validate stream construction options, tee cloned bodies with independent chunks, and proxy inherited stream-only bodies while immediately marking the original disturbed. Use native stream operations for cancellation and error propagation even when public stream methods are overridden. Cover consumption, clone isolation, validation, errors, and cancellation in window, child-frame, and worker realms. The 37-case Fetch WPT comparison adds four passing cases and six passing subtests without regressions; public stream tee still passes all 52 subtests. Validation: cargo fmt --all; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo nextest run --no-fail-fast (17,943 passed, 13 skipped); 9/9 CLI regression probes. --- .../wpt-cross-current/passed-cases.txt | 4 + .../fixtures/runtime/request_stream_body.js | 139 ++++++++++++++++++ moli-core/tests/web_apis.rs | 2 + moli-core/tests/web_apis/request_stream.rs | 42 ++++++ moli-renderer-v8/src/context_bootstrap.rs | 3 +- .../src/context_bootstrap/stream_adapter.rs | 4 +- .../context_bootstrap/stream_adapter/tee.rs | 91 +++++++++++- .../src/context_bootstrap/streams.rs | 29 +++- .../src/network_host/fetch_surface.rs | 69 ++++++++- .../src/network_host/request/bindings.rs | 28 +++- .../src/network_host/request/bindings/init.rs | 68 +++++++-- 11 files changed, 449 insertions(+), 30 deletions(-) create mode 100644 moli-core/tests/fixtures/runtime/request_stream_body.js create mode 100644 moli-core/tests/web_apis/request_stream.rs diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt index 880185d1a4c..d002b5be6a9 100644 --- a/moli-benchmark/wpt-cross-current/passed-cases.txt +++ b/moli-benchmark/wpt-cross-current/passed-cases.txt @@ -6133,8 +6133,12 @@ fetch/api/policies/referrer-origin.html fetch/api/policies/referrer-unsafe-url-worker.html fetch/api/policies/referrer-unsafe-url.html fetch/api/request/multi-globals/url-parsing.html +fetch/api/request/request-constructor-init-body-override.any.js?moli-wpt-any=dedicatedworker +fetch/api/request/request-constructor-init-body-override.any.js?moli-wpt-any=window fetch/api/request/request-error.any.js?moli-wpt-any=dedicatedworker fetch/api/request/request-error.any.js?moli-wpt-any=window +fetch/api/request/request-init-stream.any.js?moli-wpt-any=dedicatedworker +fetch/api/request/request-init-stream.any.js?moli-wpt-any=window fetch/api/request/url-encoding.html fetch/api/response/many-empty-chunks-crash.html fetch/api/response/multi-globals/url-parsing.html diff --git a/moli-core/tests/fixtures/runtime/request_stream_body.js b/moli-core/tests/fixtures/runtime/request_stream_body.js new file mode 100644 index 00000000000..5018ec13e8e --- /dev/null +++ b/moli-core/tests/fixtures/runtime/request_stream_body.js @@ -0,0 +1,139 @@ +async function runRequestStreamBodyProbe(scenario, url) { + const errors = []; + const check = (value, label) => { if (!value) errors.push(label); }; + const throwsTypeError = (callback, label) => { + try { callback(); errors.push(label + ': accepted'); } + catch (error) { check(error instanceof TypeError, label + ': ' + error); } + }; + const makeStream = text => { + const bytes = new TextEncoder().encode(text); + let offset = 0; + return new ReadableStream({async pull(controller) { + await Promise.resolve(); + if (offset === bytes.length) controller.close(); + else { controller.enqueue(bytes.slice(offset, offset + 1)); ++offset; } + }}); + }; + const makeRequest = (stream, init = {}) => new Request(url, + {method: 'POST', body: stream, duplex: 'half', ...init}); + + if (scenario === 'input') { + for (const method of ['text', 'json', 'bytes', 'arrayBuffer', 'blob', 'formData']) { + const text = method === 'formData' ? 'name=value' : '"delayed bytes"'; + const stream = makeStream(text); + const headers = method === 'formData' ? {'Content-Type': 'application/x-www-form-urlencoded'} : {}; + const request = makeRequest(stream, {headers}); + check(request.body === stream && !stream.locked && !request.bodyUsed, method + ': retains input stream'); + if (method !== 'formData') check(!request.headers.has('Content-Type'), method + ': no inferred MIME'); + const value = await request[method](); + if (method === 'text') check(value === text, method + ': bytes'); + else if (method === 'json') check(value === 'delayed bytes', method + ': bytes'); + else if (method === 'formData') check(value.get('name') === 'value', method + ': bytes'); + else { + const bytes = method === 'bytes' ? value : new Uint8Array(method === 'blob' ? await value.arrayBuffer() : value); + check(new TextDecoder().decode(bytes) === text, method + ': bytes'); + } + check(request.bodyUsed && stream.locked, method + ': consumed state'); + throwsTypeError(() => request.clone(), method + ': consumed clone'); + } + const stream = makeStream('validation'); + throwsTypeError(() => new Request(url, {method: 'POST', body: stream}), 'missing duplex'); + throwsTypeError(() => makeRequest(stream, {mode: 'no-cors'}), 'no-cors stream'); + throwsTypeError(() => makeRequest(stream, {keepalive: true}), 'keepalive stream'); + for (const method of ['GET', 'HEAD']) { + throwsTypeError(() => makeRequest(stream, {method}), method + ': stream body'); + throwsTypeError(() => new Request(url, {method, body: ''}), method + ': empty body'); + } + check(!stream.locked, 'failed construction keeps source unlocked'); + check(await makeRequest(stream).text() === 'validation', 'failed construction preserves bytes'); + const locked = makeStream('locked'); + const reader = locked.getReader(); + throwsTypeError(() => makeRequest(locked), 'locked source'); + await reader.read(); + reader.releaseLock(); + throwsTypeError(() => makeRequest(locked), 'disturbed source with released lock'); + } else if (scenario === 'clone') { + const controller = new AbortController(); + const stream = makeStream('{"value":"AB"}'); + const request = makeRequest(stream, {signal: controller.signal, headers: {'X-Value': 'original'}}); + stream.tee = () => { throw new Error('public tee called'); }; + const clone = request.clone(); + const second = clone.clone(); + check(request.body !== stream && stream.locked, 'clone tees original stream'); + check(request.body !== clone.body && !request.bodyUsed && !clone.bodyUsed, 'clone has independent unused stream'); + clone.headers.set('X-Value', 'changed'); + check(request.headers.get('X-Value') === 'original', 'clone has independent headers'); + const reason = {abort: 'request signal'}; + controller.abort(reason); + check(clone.signal !== request.signal && clone.signal.aborted && clone.signal.reason === reason, + 'clone follows signal without sharing signal object'); + check(await request.text() === '{"value":"AB"}', 'original consumes late bytes'); + check(!clone.bodyUsed && !second.bodyUsed, 'reading original leaves clones unused'); + check((await clone.json()).value === 'AB' && await second.text() === '{"value":"AB"}', 'clones consume late bytes'); + + let cancelled; + const source = new ReadableStream({cancel(reason) { cancelled = reason; }}); + const first = makeRequest(source); + const other = first.clone(); + const cancellation = first.body.cancel('first'); + check(cancelled === undefined, 'one branch cancellation preserves source'); + await Promise.all([cancellation, other.body.cancel('second')]); + check(Array.isArray(cancelled) && cancelled[0] === 'first' && cancelled[1] === 'second', 'both cancellations reach source'); + const error = {stream: 'failed'}; + const failed = makeRequest(new ReadableStream({start(c) { c.error(error); }})); + const failedClone = failed.clone(); + for (const body of [failed, failedClone]) { + try { await body.text(); errors.push('errored clone fulfilled'); } + catch (reason) { check(reason === error, 'errored clone preserves reason'); } + } + const chunk = new Uint8Array([0, 65, 66, 0]).subarray(1, 3); + const isolated = makeRequest(new ReadableStream({start(c) { c.enqueue(chunk); c.close(); }})); + const isolatedClone = isolated.clone(); + const originalChunk = (await isolated.body.getReader().read()).value; + originalChunk[0] = 90; + check(await isolatedClone.text() === 'AB', 'clone copies chunks before original can mutate them'); + const shared = new ReadableStream({start(c) { c.enqueue(chunk); c.close(); }}).tee(); + check((await shared[0].getReader().read()).value === (await shared[1].getReader().read()).value, + 'public tee keeps sharing default stream chunks'); + let cloneFailure; + const uncloneable = makeRequest(new ReadableStream({ + start(c) { c.enqueue(() => {}); }, cancel(error) { cloneFailure = error; } + })); + const uncloneableClone = uncloneable.clone(); + const outcomes = await Promise.allSettled([ + uncloneable.body.getReader().read(), uncloneableClone.body.getReader().read() + ]); + check(outcomes.every(result => result.status === 'rejected' && result.reason.name === 'DataCloneError'), + 'uncloneable chunks error both branches'); + check(cloneFailure === outcomes[0].reason && cloneFailure === outcomes[1].reason, + 'clone failure cancels source with the same error'); + } else if (scenario === 'inherit') { + const source = makeStream('proxy bytes'); + const original = makeRequest(source); + source.pipeThrough = source.pipeTo = () => { throw new Error('public pipe called'); }; + const proxy = new Request(original); + check(proxy.body !== source && original.bodyUsed && source.locked, 'inherited stream becomes a proxy'); + check(!proxy.bodyUsed && await proxy.text() === 'proxy bytes', 'proxy preserves late bytes'); + throwsTypeError(() => original.clone(), 'transferred original is unusable'); + const closed = makeRequest(new ReadableStream({start(c) { c.close(); }})); + const closedProxy = new Request(closed); + check(closed.bodyUsed && !closedProxy.bodyUsed && await closedProxy.text() === '', + 'proxy immediately disturbs an already closed source'); + const retainedSource = makeStream('retained'); + const retained = makeRequest(retainedSource); + const replacement = makeStream('replacement'); + const replaced = new Request(retained, {body: replacement, duplex: 'half'}); + check(replaced.body === replacement && !retained.bodyUsed && !retainedSource.locked, 'override preserves original'); + check(await retained.text() === 'retained' && await replaced.text() === 'replacement', 'override bytes'); + const keepalive = new Request(makeRequest(makeStream('inherited')), {keepalive: true}); + check(keepalive.keepalive && await keepalive.text() === 'inherited', 'keepalive validation applies to extraction'); + const error = {cancel: 'proxy'}; + let complete; + const cancelled = new Promise(resolve => { complete = resolve; }); + const pending = makeRequest(new ReadableStream({cancel(reason) { complete(reason); }})); + const pendingProxy = new Request(pending); + await pendingProxy.body.cancel(error); + check(await cancelled === error, 'proxy cancellation reaches input'); + } + return {errors}; +} diff --git a/moli-core/tests/web_apis.rs b/moli-core/tests/web_apis.rs index 40cd1cec9be..36295e70244 100644 --- a/moli-core/tests/web_apis.rs +++ b/moli-core/tests/web_apis.rs @@ -38,6 +38,8 @@ mod callback_cleanup; mod event_dispatch; #[path = "web_apis/request_init.rs"] mod request_init; +#[path = "web_apis/request_stream.rs"] +mod request_stream; fn diagnostic_global<'a>( page: &'a moli_core::page::Page, diff --git a/moli-core/tests/web_apis/request_stream.rs b/moli-core/tests/web_apis/request_stream.rs new file mode 100644 index 00000000000..0d7b626d931 --- /dev/null +++ b/moli-core/tests/web_apis/request_stream.rs @@ -0,0 +1,42 @@ +use super::*; + +async fn assert_request_stream_body(scenario: &str) -> Result<()> { + let server = FixtureServer::spawn().await?; + let browser = Browser::new(AppConfig::default())?; + let source = format!( + "{}\nrunRequestStreamBodyProbe({}, {}).then(finish, error => finish({{error: String(error)}}));", + include_str!("../fixtures/runtime/request_stream_body.js"), + serde_json::to_string(scenario)?, + serde_json::to_string(&server.url("/net/echo"))?, + ); + for target in ["window", "child", "worker"] { + let observed = tokio::time::timeout( + Duration::from_secs(20), + super::event_dispatch::run_probe(&browser, &server, target, &source), + ) + .await??; + assert_eq!( + observed, + serde_json::json!({"errors": []}), + "{scenario}/{target}" + ); + } + server.shutdown().await; + Ok(()) +} + +#[tokio::test(flavor = "multi_thread")] +async fn request_stream_inputs_preserve_identity_bytes_and_consumption_state() -> Result<()> { + assert_request_stream_body("input").await +} + +#[tokio::test(flavor = "multi_thread")] +async fn request_stream_clones_tee_bytes_and_forward_cancellation_and_errors() -> Result<()> { + assert_request_stream_body("clone").await +} + +#[tokio::test(flavor = "multi_thread")] +async fn request_stream_construction_proxies_inherited_bodies_and_respects_overrides() -> Result<()> +{ + assert_request_stream_body("inherit").await +} diff --git a/moli-renderer-v8/src/context_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap.rs index 4427257c217..8963852f77e 100644 --- a/moli-renderer-v8/src/context_bootstrap.rs +++ b/moli-renderer-v8/src/context_bootstrap.rs @@ -527,7 +527,8 @@ pub(crate) use self::streams::{ initialize_readable_stream_clone_shell, initialize_transform_stream_clone_shell, initialize_writable_stream_clone_shell, new_readable_stream_from_array_buffer, new_readable_stream_from_source, prepare_readable_stream_transfer, - prepare_transform_stream_transfer, prepare_writable_stream_transfer, + prepare_transform_stream_transfer, prepare_writable_stream_transfer, proxy_fetch_body_stream, + tee_fetch_body_stream, }; pub(crate) use self::time_ranges::new_time_ranges_value; #[cfg(test)] diff --git a/moli-renderer-v8/src/context_bootstrap/stream_adapter.rs b/moli-renderer-v8/src/context_bootstrap/stream_adapter.rs index 8787c812344..676b07d9950 100644 --- a/moli-renderer-v8/src/context_bootstrap/stream_adapter.rs +++ b/moli-renderer-v8/src/context_bootstrap/stream_adapter.rs @@ -146,7 +146,9 @@ pub(in crate::context_bootstrap::stream_adapter) use readable_state::{ readable_stream_controller_algorithm_object, readable_stream_controller_algorithm_value, readable_stream_pull_state_has, readable_stream_snapshot, set_readable_stream_pull_state_bit, }; -pub(in crate::context_bootstrap) use tee::{TeeStartError, tee_readable_stream}; +pub(in crate::context_bootstrap) use tee::{ + TeeStartError, tee_readable_stream, tee_readable_stream_with_cloned_branch, +}; pub(crate) use utils::require_internal_stream_value; pub(in crate::context_bootstrap) use utils::{ StreamOwnerPublication, build_required_stream_callback, call_function_result, diff --git a/moli-renderer-v8/src/context_bootstrap/stream_adapter/tee.rs b/moli-renderer-v8/src/context_bootstrap/stream_adapter/tee.rs index 5025f6cc136..1ca6ae844b2 100644 --- a/moli-renderer-v8/src/context_bootstrap/stream_adapter/tee.rs +++ b/moli-renderer-v8/src/context_bootstrap/stream_adapter/tee.rs @@ -28,6 +28,7 @@ const STREAM_TEE_READING_INDEX: u32 = 11; const STREAM_TEE_BYOB_BRANCH_INDEX: u32 = 12; const STREAM_TEE_READ_AGAIN1_INDEX: u32 = 13; const STREAM_TEE_READ_AGAIN2_INDEX: u32 = 14; +const STREAM_TEE_CLONE_BRANCH2_INDEX: u32 = 15; const TEE_READ_MICROTASK_STATE_INDEX: u32 = 0; const TEE_READ_MICROTASK_CHUNK_INDEX: u32 = 1; @@ -40,18 +41,36 @@ pub(in crate::context_bootstrap) enum TeeStartError { pub(in crate::context_bootstrap) fn tee_readable_stream<'s>( scope: &mut v8::PinScope<'s, '_>, stream: v8::Local<'s, v8::Object>, +) -> Result, TeeStartError> { + tee_readable_stream_with_options(scope, stream, false) +} + +/// Specification consumers such as Fetch clone the second branch's chunks. +/// The public ReadableStream.tee() method instead shares default-stream chunks. +pub(in crate::context_bootstrap) fn tee_readable_stream_with_cloned_branch<'s>( + scope: &mut v8::PinScope<'s, '_>, + stream: v8::Local<'s, v8::Object>, +) -> Result, TeeStartError> { + tee_readable_stream_with_options(scope, stream, true) +} + +fn tee_readable_stream_with_options<'s>( + scope: &mut v8::PinScope<'s, '_>, + stream: v8::Local<'s, v8::Object>, + clone_branch2: bool, ) -> Result, TeeStartError> { let relevant_context = stream .get_creation_context(scope) .ok_or(TeeStartError::Unavailable)?; let [branch1, branch2] = if relevant_context == scope.get_current_context() { - tee_readable_stream_in_current_realm(scope, stream)? + tee_readable_stream_in_current_realm(scope, stream, clone_branch2)? } else { let stream = v8::Global::new(scope, stream); let (branch1, branch2) = { let target_scope = &mut v8::ContextScope::new(scope, relevant_context); let stream = v8::Local::new(target_scope, &stream); - let [branch1, branch2] = tee_readable_stream_in_current_realm(target_scope, stream)?; + let [branch1, branch2] = + tee_readable_stream_in_current_realm(target_scope, stream, clone_branch2)?; ( v8::Global::new(target_scope, branch1), v8::Global::new(target_scope, branch2), @@ -71,6 +90,7 @@ pub(in crate::context_bootstrap) fn tee_readable_stream<'s>( fn tee_readable_stream_in_current_realm<'s>( scope: &mut v8::PinScope<'s, '_>, stream: v8::Local<'s, v8::Object>, + clone_branch2: bool, ) -> Result<[v8::Local<'s, v8::Object>; 2], TeeStartError> { match TeeEntrySnapshot::new(readable_stream_locked(scope, stream)).plan() { TeeEntryPlan::RejectLocked => return Err(TeeStartError::Locked), @@ -90,7 +110,12 @@ fn tee_readable_stream_in_current_realm<'s>( }; let (cancel_promise, cancel_pending) = new_pending_read_promise(scope).ok_or(TeeStartError::Unavailable)?; - let tee_state = v8::Array::new(scope, 15); + let tee_state = v8::Array::new(scope, 16); + let _ = tee_state.set_index( + scope, + STREAM_TEE_CLONE_BRANCH2_INDEX, + v8::Boolean::new(scope, clone_branch2).into(), + ); let _ = tee_state.set_index(scope, STREAM_TEE_BRANCH1_INDEX, branch1.into()); let _ = tee_state.set_index(scope, STREAM_TEE_BRANCH2_INDEX, branch2.into()); let _ = tee_state.set_index(scope, STREAM_TEE_ORIGINAL_INDEX, stream.into()); @@ -689,6 +714,30 @@ fn apply_default_tee_chunk_actions<'s>( actions: BranchPair, value: v8::Local<'s, v8::Value>, ) -> bool { + let clone_branch2 = matches!(actions.get(TeeBranch::Second), DefaultChunkAction::Enqueue) + && tee_state + .get_index(scope, STREAM_TEE_CLONE_BRANCH2_INDEX) + .is_some_and(|value| value.boolean_value(scope)); + let second_value = if clone_branch2 { + let cloned = { + let try_catch = std::pin::pin!(v8::TryCatch::new(scope)); + let mut scope = try_catch.init(); + structured_clone_value(&mut scope, value).ok_or_else(|| { + scope + .exception() + .unwrap_or_else(|| v8::undefined(&scope).into()) + }) + }; + match cloned { + Ok(cloned) => cloned, + Err(error) => { + error_default_tee_clone(scope, tee_state, error); + return false; + } + } + } else { + value + }; for branch in [TeeBranch::First, TeeBranch::Second] { if !matches!(actions.get(branch), DefaultChunkAction::Enqueue) { continue; @@ -696,7 +745,12 @@ fn apply_default_tee_chunk_actions<'s>( let Some(stream) = readable_stream_tee_branch(scope, tee_state, branch) else { continue; }; - match enqueue_chunk(scope, stream, value) { + let chunk = if branch == TeeBranch::Second { + second_value + } else { + value + }; + match enqueue_chunk(scope, stream, chunk) { Ok(()) => maybe_pull_stream(scope, stream), Err(EnqueueChunkError::ClosedOrErrored) => {} Err(EnqueueChunkError::Strategy(error)) => { @@ -708,6 +762,35 @@ fn apply_default_tee_chunk_actions<'s>( true } +fn error_default_tee_clone<'s>( + scope: &mut v8::PinScope<'s, '_>, + tee_state: v8::Local<'s, v8::Array>, + error: v8::Local<'s, v8::Value>, +) { + let moli_streams::tee::SourceErrorPlan::ErrorBranches { branches, .. } = + readable_stream_tee_snapshot(scope, tee_state).plan_source_error(); + apply_terminal_branch_actions(scope, tee_state, branches, Some(error)); + let Some(original) = readable_stream_tee_original(scope, tee_state) else { + return; + }; + let Some(pending) = tee_state + .get_index(scope, STREAM_TEE_CANCEL_PENDING_INDEX) + .and_then(|value| v8::Local::::try_from(value).ok()) + else { + return; + }; + // Source cancellation can synchronously close the stream. Reserve the + // shared promise before that callback so it adopts the cancel result. + let _ = tee_state.set_index( + scope, + STREAM_TEE_CANCEL_SETTLED_INDEX, + v8::Boolean::new(scope, true).into(), + ); + let result = cancel_readable_stream(scope, original, error) + .unwrap_or_else(|| v8::undefined(scope).into()); + resolve_pending_promise(scope, pending, result); +} + fn readable_stream_tee_error_steps<'s>( scope: &mut v8::PinScope<'s, '_>, args: v8::FunctionCallbackArguments<'s>, diff --git a/moli-renderer-v8/src/context_bootstrap/streams.rs b/moli-renderer-v8/src/context_bootstrap/streams.rs index 07d86fd31cc..9c76d50acfe 100644 --- a/moli-renderer-v8/src/context_bootstrap/streams.rs +++ b/moli-renderer-v8/src/context_bootstrap/streams.rs @@ -4,12 +4,14 @@ use super::stream_adapter::{ close_stream, enqueue_chunk, initialize_transform_stream_object, initialize_webidl_readable_stream_object, initialize_webidl_transform_stream_object, initialize_webidl_writable_stream_object, new_readable_stream_object, - parse_readable_stream_source_object, parse_transform_stream_transformer_object, + new_transform_stream_shell_object, parse_readable_stream_source_object, + parse_transform_stream_transformer_object, parse_writable_stream_sink_object, readable_stream_byob_request_respond_callback, readable_stream_byob_request_respond_with_new_view_callback, readable_stream_byob_request_view_getter, readable_stream_is_byte_stream, readable_stream_locked, rejected_promise_value, set_resolved_promise, stream_slot_object, - writable_stream_close_internal, writable_stream_locked, writable_stream_snapshot, + suppress_promise_unhandled_rejection, writable_stream_close_internal, writable_stream_locked, + writable_stream_snapshot, }; use super::stream_objects::{ new_readable_stream_byob_reader_object, new_readable_stream_reader_object, @@ -43,6 +45,29 @@ pub(crate) fn body_stream_reader_operations<'s>( ]) } +pub(crate) fn tee_fetch_body_stream<'s>( + scope: &mut v8::PinScope<'s, '_>, + stream: v8::Local<'s, v8::Object>, +) -> Option> { + super::stream_adapter::tee_readable_stream_with_cloned_branch(scope, stream).ok() +} + +pub(crate) fn proxy_fetch_body_stream<'s>( + scope: &mut v8::PinScope<'s, '_>, + stream: v8::Local<'s, v8::Object>, +) -> Option> { + let transform = new_transform_stream_shell_object(scope); + initialize_transform_stream_object(scope, transform, None, None, 1.0, None, 0.0, None); + let readable = stream_slot_object(scope, transform, TRANSFORM_STREAM_READABLE_SLOT)?; + let writable = stream_slot_object(scope, transform, TRANSFORM_STREAM_WRITABLE_SLOT)?; + // A proxy disturbs its input immediately, even when backpressure delays + // the first source read. + super::stream_adapter::disturb_readable_stream(scope, stream); + let pipe = readable::start_internal_readable_stream_pipe_to(scope, stream, writable)?; + suppress_promise_unhandled_rejection(scope, pipe); + Some(readable) +} + #[derive(Clone, Copy)] enum StreamPrototypeInstaller { ReadableStream, diff --git a/moli-renderer-v8/src/network_host/fetch_surface.rs b/moli-renderer-v8/src/network_host/fetch_surface.rs index 58c0b508506..a304303c9f7 100644 --- a/moli-renderer-v8/src/network_host/fetch_surface.rs +++ b/moli-renderer-v8/src/network_host/fetch_surface.rs @@ -47,7 +47,14 @@ pub(crate) const RESPONSE_BODY_SLOT: &str = "__lmResponseBody"; pub(crate) const RESPONSE_BODY_USED_SLOT: &str = "__lmResponseBodyUsed"; #[derive(WebApiObject)] -#[webapi(interface = web_api_interfaces::Response, prototype = "Response")] +#[webapi(interface = web_api_interfaces::Request)] +struct RequestCloneShellDeclaration { + #[webapi(slot = REQUEST_BODY_USED_SLOT, init = false)] + body_used: (), +} + +#[derive(WebApiObject)] +#[webapi(interface = web_api_interfaces::Response)] struct ResponseCloneShellDeclaration<'scope> { #[webapi(slot = RESPONSE_BODY_USED_SLOT, init = false)] body_used: (), @@ -794,6 +801,66 @@ fn request_clone_callback<'s>( ); return; } + if network_body_source_from_object(scope, this).is_none() + && let Some(stream) = body_stream_object(scope, this) + { + let clone = RequestCloneShellDeclaration::new() + .bind(scope) + .expect("Request clone shell declaration should bind"); + for slot in [ + REQUEST_METHOD_SLOT, + REQUEST_URL_SLOT, + REQUEST_DESTINATION_SLOT, + REQUEST_REFERRER_SLOT, + REQUEST_REFERRER_POLICY_SLOT, + REQUEST_MODE_SLOT, + REQUEST_CREDENTIALS_SLOT, + REQUEST_CACHE_SLOT, + REQUEST_REDIRECT_SLOT, + REQUEST_INTEGRITY_SLOT, + REQUEST_KEEPALIVE_SLOT, + REQUEST_PRIORITY_SLOT, + REQUEST_DUPLEX_SLOT, + REQUEST_IS_HISTORY_NAVIGATION_SLOT, + REQUEST_IS_RELOAD_NAVIGATION_SLOT, + ] { + if let Some(value) = request_slot_value(scope, this, slot) { + set_request_slot_value(scope, clone, slot, value); + } + } + let entries = request_slot_object(scope, this, REQUEST_HEADERS_SLOT) + .map(|headers| headers_entries(scope, headers)) + .unwrap_or_default(); + let guard = + if request_slot_string(scope, this, REQUEST_MODE_SLOT).as_deref() == Some("no-cors") { + HeadersGuard::RequestNoCors + } else { + HeadersGuard::Request + }; + let headers = + super::headers::build_headers_object_with_state(scope, &entries, guard, false); + super::headers::install_headers_object_methods(scope, headers); + set_request_slot_value(scope, clone, REQUEST_HEADERS_SLOT, headers.into()); + let signal_source = request_slot_object(scope, this, REQUEST_SIGNAL_SLOT); + let Some(signal) = new_abort_signal_for_request_with_source(scope, signal_source) else { + return; + }; + set_request_slot_value(scope, clone, REQUEST_SIGNAL_SLOT, signal); + let Some(branches) = crate::context_bootstrap::tee_fetch_body_stream(scope, stream) else { + return; + }; + let Some(original_body) = branches.get_index(scope, 0) else { + return; + }; + let Some(clone_body) = branches.get_index(scope, 1) else { + return; + }; + set_request_slot_value(scope, this, REQUEST_BODY_SLOT, original_body); + set_request_slot_value(scope, clone, REQUEST_BODY_SLOT, clone_body); + mark_request_object(scope, clone); + rv.set(clone.into()); + return; + } let global = scope.get_current_context().global(scope); let Some(ctor) = global.get(scope, v8str(scope, "Request").into()) else { rv.set_undefined(); diff --git a/moli-renderer-v8/src/network_host/request/bindings.rs b/moli-renderer-v8/src/network_host/request/bindings.rs index ff75f44c34a..eb53c2fa53d 100644 --- a/moli-renderer-v8/src/network_host/request/bindings.rs +++ b/moli-renderer-v8/src/network_host/request/bindings.rs @@ -93,7 +93,7 @@ pub(crate) fn request_constructor_callback<'s>( webidl::throw_error(scope, &error); return; } - if let Err(error) = validate_request_body_is_usable(&state) { + if let Err(error) = validate_request_body_is_usable(scope, &state) { webidl::throw_error(scope, &error); return; } @@ -111,12 +111,6 @@ pub(crate) fn request_constructor_callback<'s>( false, ); - let body_value = body_buffer - .and_then(|buffer| { - new_readable_stream_from_array_buffer(scope, buffer, buffer.byte_length()) - }) - .map(|stream| stream.into()) - .unwrap_or_else(|| v8::null(scope).into()); let signal_source = state .signal .as_ref() @@ -126,6 +120,26 @@ pub(crate) fn request_constructor_callback<'s>( None if state.signal.is_some() => return, None => v8::undefined(scope).into(), }; + let body_value = if let Some(stream) = state.body_stream.as_ref() { + let stream = v8::Local::new(scope, stream); + let stream = if state.inherited_body_stream { + let Some(proxy) = crate::context_bootstrap::proxy_fetch_body_stream(scope, stream) + else { + return; + }; + proxy + } else { + stream + }; + stream.into() + } else { + body_buffer + .and_then(|buffer| { + new_readable_stream_from_array_buffer(scope, buffer, buffer.byte_length()) + }) + .map(|stream| stream.into()) + .unwrap_or_else(|| v8::null(scope).into()) + }; RequestInstanceDeclaration::new( state.method, state.url_resolved, diff --git a/moli-renderer-v8/src/network_host/request/bindings/init.rs b/moli-renderer-v8/src/network_host/request/bindings/init.rs index 768f5902726..bd37e6c2379 100644 --- a/moli-renderer-v8/src/network_host/request/bindings/init.rs +++ b/moli-renderer-v8/src/network_host/request/bindings/init.rs @@ -22,6 +22,8 @@ pub(super) struct RequestConstructionState { pub(super) priority: moli_fetch::FetchPriorityHint, pub(super) duplex: String, pub(super) body: Option, + pub(super) body_stream: Option>, + pub(super) inherited_body_stream: bool, pub(super) inherited_body_unusable: bool, pub(super) body_content_type: Option, pub(super) headers: Vec<(String, String)>, @@ -34,7 +36,7 @@ pub(super) fn request_initial_state<'s>( child_handle: Option, base_url: Option, ) -> Result { - let (url_str, inherited, _) = input.materialize(scope)?; + let (url_str, inherited, input_object) = input.materialize(scope)?; let url_resolved = try_resolve_request_constructor_url_for_scope(scope, &url_str, child_handle, base_url) .map_err(|_| { @@ -90,6 +92,15 @@ pub(super) fn request_initial_state<'s>( let body = inherited .as_ref() .and_then(|snapshot| snapshot.body.clone()); + let body_stream = body + .is_none() + .then(|| { + input_object.filter(|input| is_branded_request_object(scope, *input)) + .and_then(|input| body_stream_object(scope, input)) + .map(|stream| v8::Global::new(scope, stream)) + }) + .flatten(); + let inherited_body_stream = body_stream.is_some(); let inherited_body_unusable = inherited .as_ref() .is_some_and(|snapshot| snapshot.body_unusable); @@ -113,6 +124,8 @@ pub(super) fn request_initial_state<'s>( priority, duplex, body, + body_stream, + inherited_body_stream, inherited_body_unusable, body_content_type: None, headers, @@ -132,24 +145,35 @@ pub(super) fn apply_request_init_overrides<'s>( if let Some(method) = parsed.method { state.method = normalize_fetch_request_method(&method)?; } - let init_body_is_readable_stream = parsed - .body + let init_body_stream = parsed.body .and_then(|value| v8::Local::::try_from(value).ok()) - .is_some_and(|object| { - crate::web_api_interfaces::ReadableStream::is_instance(scope, object) - }); - let body_from_init = parsed - .body - .map(|body| body_init(scope, body, webidl::Context::member("RequestInit", "body"))) + .filter(|object| crate::web_api_interfaces::ReadableStream::is_instance(scope, *object)); + let body_from_init = parsed.body + .filter(|_| init_body_stream.is_none()) + .map(|value| body_init(scope, value, webidl::Context::member("RequestInit", "body"))) .transpose()? .flatten(); if let Some(init_body) = body_from_init.as_ref() { state.body_content_type = init_body.content_type.clone(); state.body = Some(init_body.bytes.clone()); + state.body_stream = None; + state.inherited_body_stream = false; state.inherited_body_unusable = false; } - if state.body.is_some() - && (body_from_init.is_some() || method_overridden) + if let Some(stream) = init_body_stream { + if readable_body_stream_unusable(scope, stream) { + return Err(webidl::WebIdlError::custom_message( + "BodyInit ReadableStream is locked or disturbed", + )); + } + state.body = None; + state.body_content_type = None; + state.body_stream = Some(v8::Global::new(scope, stream)); + state.inherited_body_stream = false; + state.inherited_body_unusable = false; + } + if (state.body.is_some() || state.body_stream.is_some()) + && (body_from_init.is_some() || init_body_stream.is_some() || method_overridden) && !request_method_allows_body(&state.method) { return Err(webidl::WebIdlError::custom_message( @@ -189,23 +213,39 @@ pub(super) fn apply_request_init_overrides<'s>( if parsed.duplex.is_some() { state.duplex = "half".to_owned(); } - if parsed.keepalive == Some(true) && init_body_is_readable_stream { + if init_body_stream.is_some() && parsed.duplex.is_none() { return Err(webidl::WebIdlError::custom_message( - "Request with keepalive cannot have a ReadableStream body", + "Request with a ReadableStream body requires duplex", )); } if let Some(keepalive) = parsed.keepalive { state.keepalive = keepalive; } + if state.keepalive && init_body_stream.is_some() { + return Err(webidl::WebIdlError::custom_message( + "Request with keepalive cannot have a ReadableStream body", + )); + } + if state.body_stream.is_some() && !matches!(state.mode.as_str(), "cors" | "same-origin") { + return Err(webidl::WebIdlError::custom_message( + "Request with a ReadableStream body requires cors or same-origin mode", + )); + } let guard = request_headers_guard_for_mode(&state.mode); state.headers = filter_headers_for_guard(&state.headers, guard); Ok(()) } pub(super) fn validate_request_body_is_usable( + scope: &mut v8::PinScope<'_, '_>, state: &RequestConstructionState, ) -> Result<(), webidl::WebIdlError> { - if state.inherited_body_unusable { + if state.inherited_body_unusable + || state.body_stream.as_ref().is_some_and(|stream| { + let stream = v8::Local::new(scope, stream); + readable_body_stream_unusable(scope, stream) + }) + { return Err(request_body_already_used_error()); } Ok(())