0

I'm writing a very basic webapp that displays data requested from a server. The server sends JSON "packets" of data as a chunked response.

I'm reading the data from the server using the Javascript fetch API in my webpage, via the ReadableStream API. As far as I can tell from experimenting, each chunk that I send from the server arrives at the client as a separate block of data. If I assume that, my client is straightforward:

      const response = await fetch("/server_api");
      const reader = response.body.getReader();

      while (true) {
        const {value, done} = await reader.read();
        if (done) break;
        // convert "value" from an array of bytes to a JS object
        // by parsing it as JSON
        obj = JSON.parse(new TextDecoder().decode(value))
        // process the object
      }

However, this will fail if a chunk from the server gets split between two reads, or if two chunks from the server get merged in a single read.

Is this a possibility that I need to worry about? If I do, my code (both server and client side) will need to get significantly more complex, so I'd prefer to avoid that if I can.

Note that I'm specifically talking here about HTTP chunked responses and how they interact with the Javascript fetch API, not about TCP or other levels of the network stack.

Paul Moore
  • 6,569
  • 6
  • 40
  • 47
  • Does this answer your question? [Is TCP Guaranteed to arrive in order?](https://stackoverflow.com/questions/1691179/is-tcp-guaranteed-to-arrive-in-order) – esqew Nov 04 '22 at 16:20
  • No, not really. This is about http chunked responses, which is at a higher level than TCP. And my question is not about ordering, but about merging/splitting chunks. – Paul Moore Nov 04 '22 at 17:44

1 Answers1

0

Answering my own question, I have triggered a case where two blocks of data sent by the server arrived at the client in a single read() result from the reader.

So yes, you do have to allow for the possibility that chunks sent by the server get merged. I haven't seen a case of a chunk getting split, so it's possible that might not happen, but the key point is that there is not a one to one correspondence between the chunks the server sends and the chunks the client receives.

Paul Moore
  • 6,569
  • 6
  • 40
  • 47