Skip to content

Response.arrayBuffer() may resolve to ArrayBufferView #801

@freund17

Description

@freund17
new Response(new Uint8Array([8])).arrayBuffer();

returns a Promise, which resolves to a Uint8Array.
It should resolve to an ArrayBuffer instead.

Steps to reproduce:
Load this polyfill and paste above code into the developer console.

My hacky workaround:

After loading the polyfill add the following:

const ori = window.Response.prototype.arrayBuffer;

window.Response.prototype.arrayBuffer = function (...props) {
  return (ori.apply(this, props) as Promise<
    ArrayBuffer | ArrayBufferView
  >).then((value) => {
    if (ArrayBuffer.isView(value)) {
      return value.buffer.slice(
        value.byteOffset,
        value.byteOffset + value.byteLength
      );
    } else {
      return value;
    }
  });
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions