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
71 changes: 63 additions & 8 deletions moli-renderer-v8/src/network_host/body_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt};
const BODY_SOURCE_KIND_BYTES: &str = "bytes";
const BODY_SOURCE_KIND_BLOB: &str = "blob";
const BODY_SOURCE_BLOB_SLOT: &str = "__lmNetworkBodySourceBlob";
const BODY_SOURCE_KIND_STREAM: &str = "stream";
const BODY_SOURCE_ORIGINAL_SLOT: &str = "__lmNetworkBodySourceOriginal";
const BODY_SOURCE_KIND_REGISTRY_BYTES: &str = "registry-bytes";
const BODY_SOURCE_KIND_SUBRESOURCE_BODY: &str = "subresource-body";
const BODY_SOURCE_KIND_PENDING_STREAM: &str = "pending-stream";
Expand Down Expand Up @@ -69,6 +71,15 @@ struct BlobNetworkBodySourceDeclaration<'scope> {
blob: v8::Local<'scope, v8::Object>,
}

#[derive(WebApiObject)]
#[webapi(plain)]
struct StreamNetworkBodySourceDeclaration<'scope> {
#[webapi(slot = NETWORK_BODY_SOURCE_KIND_SLOT)]
kind: &'static str,
#[webapi(slot = BODY_SOURCE_ORIGINAL_SLOT)]
original: v8::Local<'scope, v8::Object>,
}

#[derive(WebApiObject)]
#[webapi(plain)]
struct RegisteredNetworkBodySourceDeclaration<'scope> {
Expand Down Expand Up @@ -553,13 +564,53 @@ pub(in crate::network_host) fn set_network_body_blob_source<'s>(
set_network_body_source_object(scope, object, source);
}

pub(in crate::network_host) fn body_uses_blob_stream<'s>(
/// Keep immutable replay data when a body starts using a transferred stream.
/// Reading this body must follow that stream, including its cancellation and
/// errors, even when the original source bytes are already available.
pub(in crate::network_host) fn set_network_body_stream_source<'s>(
scope: &mut v8::PinScope<'s, '_>,
object: v8::Local<'s, v8::Object>,
original: v8::Local<'s, v8::Object>,
) {
// Each wrapper points directly to the replay source. Repeated Request
// transfers must not grow a recursive chain of metadata carriers.
let original = if network_body_source_kind(scope, original).as_deref()
== Some(BODY_SOURCE_KIND_STREAM)
{
stream_body_original_source(scope, original).expect("stream body replay source must exist")
} else {
original
};
let source = StreamNetworkBodySourceDeclaration::new(BODY_SOURCE_KIND_STREAM, original)
.bind(scope)
.expect("stream body source declaration should bind");
set_network_body_source_object(scope, object, source);
}

fn stream_body_original_source<'s>(
scope: &mut v8::PinScope<'s, '_>,
source: v8::Local<'s, v8::Object>,
) -> Option<v8::Local<'s, v8::Object>> {
network_body_source_slot_value(scope, source, BODY_SOURCE_ORIGINAL_SLOT)
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
}

fn network_body_source_uses_stream<'s>(
scope: &mut v8::PinScope<'s, '_>,
source: v8::Local<'s, v8::Object>,
) -> bool {
network_body_source_from_object(scope, object).is_some_and(|source| {
network_body_source_kind(scope, source).as_deref() == Some(BODY_SOURCE_KIND_BLOB)
})
matches!(
network_body_source_kind(scope, source).as_deref(),
Some(BODY_SOURCE_KIND_BLOB | BODY_SOURCE_KIND_STREAM)
)
}

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>(
Expand Down Expand Up @@ -1185,6 +1236,12 @@ fn try_network_body_bytes_from_storage<'s>(
) -> Result<Option<Vec<u8>>, String> {
let source_kind = network_body_source_kind(scope, object);
match source_kind.as_deref() {
Some(BODY_SOURCE_KIND_STREAM) => {
let Some(original) = stream_body_original_source(scope, object) else {
return Ok(None);
};
return try_network_body_bytes_from_storage(scope, original, consume_registry_body);
}
Some(BODY_SOURCE_KIND_BLOB) => {
let blob = network_body_source_slot_value(scope, object, BODY_SOURCE_BLOB_SLOT)
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
Expand Down Expand Up @@ -1572,7 +1629,7 @@ fn consume_network_body_value_from_source_inner<'s>(
let pending_stream =
network_body_source_kind(scope, source).as_deref() == Some(BODY_SOURCE_KIND_PENDING_STREAM);
let uses_reader = explicit_source.is_none()
|| body_uses_blob_stream(scope, object)
|| body_uses_readable_stream(scope, object)
|| pending_stream && chunk_callback.is_some();
if !uses_reader
&& let Some(stream) = body_stream_object(scope, object)
Expand Down Expand Up @@ -1668,9 +1725,7 @@ fn consume_network_body_value_from_source_inner<'s>(
return (NetworkBodyConsumption::Pending(promise), None);
}

if (explicit_source.is_none() || body_uses_blob_stream(scope, object))
&& let Some(stream) = readable_body_stream_from_object(scope, object)
{
if uses_reader && let Some(stream) = readable_body_stream_from_object(scope, object) {
return consume_readable_body_stream(
scope,
object,
Expand Down
114 changes: 51 additions & 63 deletions moli-renderer-v8/src/network_host/fetch_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,51 +805,53 @@ fn request_clone_callback<'s>(
);
return;
}
if (network_body_source_from_object(scope, this).is_none()
|| body_source::body_uses_blob_stream(scope, this))
&& 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 Some(realm) = this.get_creation_context(scope) else {
return;
};
let scope = &mut v8::ContextScope::new(scope, realm);
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);
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 entries = request_slot_object(scope, this, REQUEST_HEADERS_SLOT)
.map(|headers| headers_list(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);
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 null_body = v8::null(scope).into();
set_request_slot_value(scope, clone, REQUEST_BODY_SLOT, null_body);
if let Some(stream) = body_stream_object(scope, this) {
let Some(branches) = crate::context_bootstrap::tee_fetch_body_stream(scope, stream) else {
return;
};
Expand All @@ -862,26 +864,12 @@ fn request_clone_callback<'s>(
set_request_slot_value(scope, this, REQUEST_BODY_SLOT, original_body);
set_request_slot_value(scope, clone, REQUEST_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);
body_source::set_network_body_stream_source(scope, this, source);
body_source::set_network_body_stream_source(scope, clone, source);
}
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();
return;
};
let Ok(ctor) = v8::Local::<v8::Function>::try_from(ctor) else {
rv.set_undefined();
return;
};
if let Some(clone) = ctor.new_instance(scope, &[this.into()]) {
rv.set(clone.into());
} else {
rv.set_undefined();
}
mark_request_object(scope, clone);
rv.set(clone.into());
}

fn response_clone_callback<'s>(
Expand Down Expand Up @@ -923,7 +911,7 @@ fn response_clone_callback<'s>(
.bind(scope)
.expect("Response clone init declaration should bind");

if body_source::body_uses_blob_stream(scope, this)
if body_source::body_uses_readable_stream(scope, this)
&& let Some(stream) = body_stream_object(scope, this)
{
let clone = ResponseCloneShellDeclaration::new(None)
Expand Down
2 changes: 1 addition & 1 deletion moli-renderer-v8/src/network_host/request/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub(crate) fn request_constructor_callback<'s>(
};
if let Some(source) = state.inherited_body_source {
let source = v8::Local::new(scope, source);
body_source::set_network_body_source_object(scope, obj, source);
body_source::set_network_body_stream_source(scope, obj, source);
}
stream.into()
} else {
Expand Down
6 changes: 2 additions & 4 deletions moli-renderer-v8/src/network_host/request/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,9 @@ fn request_input_snapshot_from_private_slots<'s>(
if body_unusable && !for_constructor {
return Err(request_body_already_used_error());
}
// Constructing from a Blob-backed Request transfers its current stream;
// Constructing from a Request transfers its current stream;
// copying its immutable source bytes would bypass pending reads and tee.
let body = if body_unusable
|| for_constructor && body_source::body_uses_blob_stream(scope, object)
{
let body = if body_unusable || for_constructor {
None
} else {
try_network_body_bytes_from_object(scope, object)
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 @@ -2270,3 +2270,5 @@ mod blob_text;
mod blob_reading;

mod blob_support;

mod request_body_transfer;
Loading
Loading