Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions moli-renderer-v8/src/network_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ pub(in crate::network_host) use self::body::{
pub(crate) use self::body_source::pending_network_body_source_buffered_len_for_test;
pub(in crate::network_host) use self::body_source::{
NetworkBodyConsumption, NetworkBodyConsumptionKind,
clone_filtered_response_internal_body_source, clone_pending_network_body_stream,
consume_fetch_body_value_from_object,
clone_filtered_response_internal_body_source, consume_fetch_body_value_from_object,
consume_filtered_response_internal_body_value_from_object,
consume_filtered_response_internal_body_value_from_object_with_chunk_callback,
consume_network_body_value_from_object,
Expand All @@ -62,7 +61,6 @@ pub(in crate::network_host) use self::body_source::{
set_filtered_response_internal_body_from_response_body,
set_filtered_response_internal_body_from_subresource_body, set_network_body_owned_bytes,
take_network_body_bytes_from_object, try_network_body_bytes_from_object,
try_network_body_value_from_object,
};
pub(crate) use self::body_source::{
PendingNetworkBodySourceState, close_pending_network_body_stream,
Expand Down
55 changes: 8 additions & 47 deletions moli-renderer-v8/src/network_host/body_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,14 +605,6 @@ fn network_body_source_uses_stream<'s>(
)
}

pub(in crate::network_host) fn body_uses_readable_stream<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
) -> bool {
network_body_source_from_object(scope, object)
.is_some_and(|source| network_body_source_uses_stream(scope, source))
}

fn set_network_body_response_body<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
Expand Down Expand Up @@ -1320,44 +1312,6 @@ pub(in crate::network_host) fn try_take_network_body_bytes_from_object<'s>(
try_network_body_bytes_from_storage(scope, object, true)
}

pub(in crate::network_host) fn try_network_body_value_from_object<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
) -> Result<Option<v8::Local<'s, v8::Value>>, String> {
let source = network_body_source_from_object(scope, object).unwrap_or(object);
if network_body_source_kind(scope, source)
.as_deref()
.is_some_and(|kind| {
matches!(
kind,
BODY_SOURCE_KIND_REGISTRY_BYTES | BODY_SOURCE_KIND_SUBRESOURCE_BODY
)
})
{
return try_network_body_bytes_from_storage(scope, source, false).map(|bytes| {
bytes
.and_then(|bytes| blob::array_buffer_from_bytes(scope, bytes))
.map(Into::into)
});
}
Ok(
network_body_source_slot_value(scope, source, NETWORK_BODY_BYTES_SLOT)
.or_else(|| source.get(scope, v8str(scope, NETWORK_BODY_SLOT).into()))
.filter(|value| !value.is_null_or_undefined()),
)
}

pub(in crate::network_host) fn clone_pending_network_body_stream<'s>(
scope: &mut v8::PinScope<'s, '_>,
source: v8::Local<'s, v8::Object>,
owner: v8::Local<'s, v8::Object>,
) -> Option<v8::Local<'s, v8::Object>> {
clone_pending_network_body_source_and_stream(scope, source, owner).map(|(source, stream)| {
set_network_body_source_object(scope, owner, source);
stream
})
}

fn clone_pending_network_body_source_and_stream<'s>(
scope: &mut v8::PinScope<'s, '_>,
source: v8::Local<'s, v8::Object>,
Expand Down Expand Up @@ -1628,8 +1582,10 @@ fn consume_network_body_value_from_source_inner<'s>(
let source = explicit_source.unwrap_or(object);
let pending_stream =
network_body_source_kind(scope, source).as_deref() == Some(BODY_SOURCE_KIND_PENDING_STREAM);
// Replay metadata survives cloning, but reads follow the selected stream.
// Native storage consumers retain their lock without acquiring a reader.
let uses_reader = explicit_source.is_none()
|| body_uses_readable_stream(scope, object)
|| network_body_source_uses_stream(scope, source)
|| pending_stream && chunk_callback.is_some();
if !uses_reader
&& let Some(stream) = body_stream_object(scope, object)
Expand Down Expand Up @@ -2458,6 +2414,11 @@ fn mark_registry_body_source_used<'s>(
if let Some(owner) = get_private_value(scope, source, NETWORK_BODY_SOURCE_OWNER_SLOT)
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
{
// Tee and transfer replace the public body while retaining this producer.
// Reading the old stream must not consume its former owner's new branch.
if network_body_source_from_object(scope, owner) != Some(source) {
return;
}
if is_branded_response_object(scope, owner) {
set_response_slot_bool(scope, owner, RESPONSE_BODY_USED_SLOT, true);
} else if is_branded_request_object(scope, owner) {
Expand Down
148 changes: 29 additions & 119 deletions moli-renderer-v8/src/network_host/fetch_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,11 @@ fn response_clone_callback<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'_, v8::Value>,
) {
let Some(this) = require_response_receiver(scope, args.this()) else {
let Some(response) = require_response_receiver(scope, args.this()) else {
return;
};
if body_already_used(scope, this, RESPONSE_BODY_USED_SLOT)
|| body_stream_object(scope, this)
if body_already_used(scope, response, RESPONSE_BODY_USED_SLOT)
|| body_stream_object(scope, response)
.is_some_and(|stream| readable_body_stream_unusable(scope, stream))
{
throw_type_error(
Expand All @@ -890,102 +890,30 @@ fn response_clone_callback<'s>(
);
return;
}
let Some(realm) = this.get_creation_context(scope) else {
return;
};
let global = realm.global(scope);
let Some(ctor) = global.get(scope, v8str(scope, "Response").into()) else {
rv.set_undefined();
let Some(realm) = response.get_creation_context(scope) else {
return;
};
let Ok(ctor) = v8::Local::<v8::Function>::try_from(ctor) else {
rv.set_undefined();
return;
};

let init = ResponseInitObjectDeclaration::new(
response_slot_number(scope, this, RESPONSE_STATUS_SLOT),
response_slot_string(scope, this, RESPONSE_STATUS_TEXT_SLOT),
response_slot_value(scope, this, RESPONSE_HEADERS_SLOT),
)
.bind(scope)
.expect("Response clone init declaration should bind");

if body_source::body_uses_readable_stream(scope, this)
&& let Some(stream) = body_stream_object(scope, this)
{
let clone = ResponseCloneShellDeclaration::new(None)
.bind(scope)
.expect("Response clone shell declaration should bind");
copy_response_surface_slots(scope, this, clone);
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_response_slot_value(scope, this, RESPONSE_BODY_SLOT, original_body);
set_response_slot_value(scope, clone, RESPONSE_BODY_SLOT, clone_body);
if let Some(source) = network_body_source_from_object(scope, this) {
body_source::set_network_body_source_object(scope, clone, source);
}
mark_response_object(scope, clone);
rv.set(clone.into());
return;
}
if let Some(source) = network_body_source_from_object(scope, this) {
let clone = ResponseCloneShellDeclaration::new(None)
.bind(scope)
.expect("Response clone shell declaration should bind");
copy_response_surface_slots(scope, this, clone);
if let Some(stream) = clone_pending_network_body_stream(scope, source, clone) {
set_response_slot_value(scope, clone, RESPONSE_BODY_SLOT, stream.into());
mark_response_object(scope, clone);
rv.set(clone.into());
return;
}
}

if response_slot_number(scope, this, RESPONSE_STATUS_SLOT) == Some(0.0) {
let clone = ResponseCloneShellDeclaration::new(Some(v8::null(scope).into()))
.bind(scope)
.expect("Response error clone shell declaration should bind");
copy_response_surface_slots(scope, this, clone);
clone_filtered_response_internal_body_source(scope, this, clone);
mark_response_object(scope, clone);
rv.set(clone.into());
return;
}

let body = match try_network_body_value_from_object(scope, this) {
Ok(Some(body)) => body,
Ok(None) => match clone_response_readable_stream_body(scope, this) {
Ok(Some(body)) => body,
Ok(None) => v8::undefined(scope).into(),
Err(()) => {
throw_type_error(
scope,
"Failed to execute 'clone' on 'Response': body stream already used",
);
return;
}
},
Err(_) => {
throw_type_error(scope, "Failed to materialize response body");
return;
let scope = &mut v8::ContextScope::new(scope, realm);
let null_body = v8::null(scope).into();
let clone = ResponseCloneShellDeclaration::new(Some(null_body))
.bind(scope)
.expect("Response clone shell declaration should bind");
copy_response_surface_slots(scope, response, clone);
if response_slot_number(scope, response, RESPONSE_STATUS_SLOT) == Some(0.0) {
clone_filtered_response_internal_body_source(scope, response, clone);
} else {
match clone_response_readable_stream_body(scope, response) {
Ok(Some(body)) => set_response_slot_value(scope, clone, RESPONSE_BODY_SLOT, body),
Ok(None) => {}
Err(()) => return,
}
};
if let Some(clone) = ctor.new_instance(scope, &[body, init.into()]) {
if let Some(url) = response_slot_value(scope, this, RESPONSE_URL_SLOT) {
set_response_slot_value(scope, clone, RESPONSE_URL_SLOT, url);
if let Some(source) = network_body_source_from_object(scope, response) {
body_source::set_network_body_stream_source(scope, response, source);
body_source::set_network_body_stream_source(scope, clone, source);
}
rv.set(clone.into());
} else {
rv.set_undefined();
}
mark_response_object(scope, clone);
rv.set(clone.into());
}

fn copy_response_surface_slots<'s>(
Expand Down Expand Up @@ -1035,32 +963,14 @@ fn clone_response_readable_stream_body<'s>(
scope: &mut v8::PinScope<'s, '_>,
response: v8::Local<'s, v8::Object>,
) -> Result<Option<v8::Local<'s, v8::Value>>, ()> {
let Some(value) = response_slot_value(scope, response, RESPONSE_BODY_SLOT) else {
return Ok(None);
};
if value.is_null_or_undefined() {
return Ok(None);
}
let Ok(stream) = v8::Local::<v8::Object>::try_from(value) else {
return Ok(None);
};
if !web_api_interfaces::ReadableStream::is_instance(scope, stream) {
let Some(stream) = body_stream_object(scope, response) else {
return Ok(None);
}
if readable_body_stream_unusable(scope, stream) {
return Err(());
}
let Some(branches) = crate::context_bootstrap::tee_fetch_body_stream(scope, stream) else {
return Err(());
};
let Some(original_branch) = branches.get_index(scope, 0) else {
return Err(());
};
let Some(clone_branch) = branches.get_index(scope, 1) else {
return Err(());
};
set_response_slot_value(scope, response, RESPONSE_BODY_SLOT, original_branch);
Ok(Some(clone_branch))
let branches = crate::context_bootstrap::tee_fetch_body_stream(scope, stream).ok_or(())?;
let original = branches.get_index(scope, 0).ok_or(())?;
let clone = branches.get_index(scope, 1).ok_or(())?;
set_response_slot_value(scope, response, RESPONSE_BODY_SLOT, original);
Ok(Some(clone))
}

pub(in crate::network_host) fn new_abort_signal_for_request<'s>(
Expand Down Expand Up @@ -1112,7 +1022,7 @@ pub(crate) fn install_response_bindings<'s>(
install_response_body_methods(scope, prototype);
}

fn body_already_used<'s>(
pub(in crate::network_host) fn body_already_used<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
slot: &str,
Expand Down
6 changes: 3 additions & 3 deletions moli-renderer-v8/src/network_host/response/materialize.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::super::fetch_surface::{
RESPONSE_BODY_USED_SLOT, RESPONSE_INTERNAL_HEADERS_SLOT, RESPONSE_INTERNAL_STATUS_SLOT,
RESPONSE_INTERNAL_STATUS_TEXT_SLOT, RESPONSE_INTERNAL_URL_SLOT, mark_response_object,
response_slot_number,
RESPONSE_INTERNAL_STATUS_TEXT_SLOT, RESPONSE_INTERNAL_URL_SLOT, body_already_used,
mark_response_object, response_slot_number,
};
use super::*;
use crate::network_host::headers::headers_list;
Expand Down Expand Up @@ -567,7 +567,7 @@ pub(crate) fn materialize_response_object_head<'s>(
if response_type == "error" {
return Err(format!("{context} rejected an error Response."));
}
if response_slot_bool(scope, response, RESPONSE_BODY_USED_SLOT) {
if body_already_used(scope, response, RESPONSE_BODY_USED_SLOT) {
return Err(format!(
"{context} rejected a Response whose body is already used."
));
Expand Down
2 changes: 2 additions & 0 deletions moli-renderer-v8/src/runtime/page_vm/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2272,3 +2272,5 @@ mod blob_reading;
mod blob_support;

mod request_body_transfer;

mod response_body_clone;
Loading
Loading