v132 · network / connectivity

Fetch: Request.bytes() and Response.bytes()

Add a bytes() method to the Request and Response interfaces, which returns a promise that resolves with a Uint8Array. While Request and Response have an arrayBuffer() method, we can't read directly from a buffer. We have to create a view such as a Uint8Array to read it. The bytes() method improves the ergonomics of getting the body of Request and Response.

concepts

  1. Request/Response.bytes()

    Request.bytes() and Response.bytes() — direct Uint8Array reads. Saves the double-allocation of arrayBuffer() → typed array.

  2. request.bytes() upload side

    The upload side: construct a Request with a text or file body, read it back as Uint8Array, surface length and hex.

  3. side-by-side bytes()

    Fetch the same URL through arrayBuffer() + Uint8Array wrap and through bytes(). Compare byteLength, hex dump, and elapsed time.

  4. Body API comparator

    Six Body methods, four recipes (upload, image header, stream, service worker rewrite). Cheat sheet for when to pick which.

why it shipped

Add a bytes() method to the Request and Response interfaces, It improves the ergonomics of getting the body of Request and Response.

references