[whatwg/streams] Stream Reader: event to detect first byte added to internal buffer (#1126)

After some hours of reading the specs it looks like there is no way to get an indication for the point in time when the first byte of a transferred chunk was added to internal buffer of a stream reader.

The use case of this is for example the measurement of throughput in bursty chunked-transfer with idle times between the HTTP chunks when using ```fetch``` api.

Example from the Spec how it IS:
```
function readAllChunks(readableStream) {
  const reader = readableStream.getReader();
  const chunks = [];

  return pump();

  function pump() {
    return reader.read().then(({ value, done }) => {
      if (done) {
        return chunks;
      }

      chunks.push(value);
      return pump();
    });
  }
}
```

Example how it could be (or in some other comparable way) to enable above use case:
```
function readAllChunks(readableStream) {
  const reader = readableStream.getReader();
  const chunks = [];

  // --- added from here ----
  const chunkStartTimes = [];
  const chunkEndTimes = [];
  const chunkBytes = [];
  reader.addEventListener('firstByteOfChunk', () => chunksStartTimes.push(Date.now()) );
  // --- added to here ----

  return pump();

  function pump() {
    return reader.read().then(({ value, done }) => {
      if (done) {
        // --- added from here ----
        // at this point throughput calculation for each chunk is possible
        // --- added to here ----
        return chunks;
      }
  // --- added from here ----
  chunksEndTimes.push(Date.now());
  chunkBytes.push(value.byteLength)
  // --- added to here ----
      chunks.push(value);
      return pump();
    });
  }
}
```

Or is there some other way (preferably supported by browsers already) to achieve this desired measurement with ```fetch```?

-- 
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/streams/issues/1126

Received on Tuesday, 11 May 2021 15:56:13 UTC