feat(fetch): Support ArrayBuffer and TypedArray request body in Fetch RequestInit#5043
feat(fetch): Support ArrayBuffer and TypedArray request body in Fetch RequestInit#5043RajdeepKushwaha5 wants to merge 3 commits intoboa-dev:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds binary request body support to Boa’s Fetch RequestInit.body, enabling ArrayBuffer, TypedArray, and DataView inputs (per #4957) by propagating &mut Context through the request-construction path.
Changes:
- Extend
RequestInit::into_request_builderto accept and extract bytes fromArrayBuffer/TypedArray/DataView. - Thread
&mut ContextthroughJsRequest::{constructor,create_from_js}andfetch_innerto support the new conversions. - Add a fetch
Requesttest coveringUint8ArrayandDataViewbodies.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| core/runtime/src/fetch/request.rs | Implements byte extraction for ArrayBuffer/TypedArray/DataView bodies; updates request construction signatures to accept Context. |
| core/runtime/src/fetch/mod.rs | Updates fetch option application to pass Context into into_request_builder. |
| core/runtime/src/fetch/tests/request.rs | Updates create_from_js callsite and adds a test for Uint8Array/DataView request bodies. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
core/runtime/src/fetch/request.rs
Outdated
| @@ -105,11 +107,59 @@ impl RequestInit { | |||
|
|
|||
| if let Some(body) = &self.body { | |||
| // TODO: add more support types. | |||
| const buf = new Uint8Array([104, 101, 108, 108, 111]); // "hello" | ||
| globalThis.req1 = new Request("http://unit.test", { | ||
| method: "POST", | ||
| body: buf, | ||
| }); | ||
| const dv = new DataView(buf.buffer); | ||
| globalThis.req2 = new Request("http://unit.test", { | ||
| method: "POST", | ||
| body: dv, | ||
| }); |
| } else if let Ok(ta) = JsTypedArray::from_object(object.clone()) { | ||
| let buffer = ta.buffer(context)?; | ||
| let array_buffer = JsArrayBuffer::from_object( | ||
| buffer | ||
| .as_object() | ||
| .expect("buffer must be an object") | ||
| .clone(), | ||
| )?; | ||
| if let Some(bytes) = array_buffer.to_vec() { | ||
| let offset = ta.byte_offset(context)?; | ||
| let length = ta.byte_length(context)?; | ||
| request_body = bytes[offset..offset + length].to_vec(); | ||
| } else { |
core/runtime/src/fetch/request.rs
Outdated
| let buffer = ta.buffer(context)?; | ||
| let array_buffer = JsArrayBuffer::from_object( | ||
| buffer | ||
| .as_object() | ||
| .expect("buffer must be an object") | ||
| .clone(), | ||
| )?; |
Test262 conformance changes
Tested main commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5043 +/- ##
===========================================
+ Coverage 47.24% 59.18% +11.93%
===========================================
Files 476 563 +87
Lines 46892 62577 +15685
===========================================
+ Hits 22154 37036 +14882
- Misses 24738 25541 +803 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Description
This PR implements support for injecting standard JavaScript Array buffers (
ArrayBuffer,Uint8Array,DataView, and generalTypedArrayvariants) into thebodyoption of theRequestInitdictionary conforming with the Fetch API, addressing Issue #4957.Previously, attempting to generate a Fetch Request from bytes threw a
TypeError("body is not a supported type").Changes Made:
into_request_builder,create_from_js, andrequest::constructorinternally to passcontext: &mut Contextthrough the calling tree.bodyfield locally against engine nativeJsArrayBuffer,JsTypedArray, andJsDataViewbuiltin wrappers.offsetand the datalength.fetch/tests/request.rsautomated execution block to securely validateUint8ArrayalongsideDataViewbindings duringcargo test.Related Issue:
Fixes #4957