Skip to content

fix(fetch): support BufferSource bodies in Response() #5216

@iammdzaidalam

Description

@iammdzaidalam

While looking at core/runtime/src/fetch/response.rs, I noticed Response() body extraction still falls back to stringification for non-string inputs.

That means BufferSource-style inputs like ArrayBuffer, typed arrays, and DataView don’t get treated as raw bytes when constructing a Response.

For example, this currently goes through string coercion instead of byte extraction:

new Response(new Uint8Array([104, 101, 108, 108, 111]))

A narrow fix here would be to teach extract_body(...) in response.rs to handle:

  • ArrayBuffer
  • typed array views
  • DataView

using the visible byte slice (byteOffset / byteLength) before falling back to the existing string path.

This would keep the current string behavior intact, avoid injecting the default text content-type for binary inputs, and leave other BodyInit cases like Blob, FormData, and URLSearchParams for follow-up.

Repro

let u8a = new Uint8Array([104, 101, 108, 108, 111]);
let r = new Response(u8a);

// expected: "hello"
// current: "[object Uint8Array]"
r.text().then(console.log);

// expected: false
// current: true
console.log(r.headers.has("content-type"));

Happy to open a small PR scoped just to Response() if this direction makes sense.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions