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.