Re: [whatwg/fetch] Fetch and SharedArrayBuffer (#897)

I haven't looked at what would be required in detail, but my first instinct is that allowing reading from a `SharedArrayBuffer` would be relatively safe, as we always end up copying the data without otherwise inspecting it anyway. However, if some hypothetical browser wanted to do a zero-copy optimisation of uploads then some mischief might be possible.

Reading into a `SharedArrayBuffer` with streams it would look something like (untested):
```js
async function toSharedArrayBuffer(response) {
  const parts = [];
  let totalLength = 0;
  for await (const buffer of response.body) {
    parts.push(buffer);
    totalLength += buffer.byteLength;
  }
  const sab = new SharedArrayBuffer(totalLength);
  const u8 = new Uint8Array(sab);
  let offset = 0;
  for (const buffer of parts) {
    u8.set(buffer, offset);
    offset += buffer.byteLength;
  }
  return sab;
}
```
This is certainly long and fiddly enough that it would be useful to have a built-in.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/897#issuecomment-770666078

Received on Monday, 1 February 2021 08:20:58 UTC