When network data arrives in chunks, a character’s UTF-8 bytes can be split between chunks. Decoding each chunk independently with `Buffer.toString("utf8")` can produce gibberish. `TextDecoder` avoids this when you call `decode(chunk, { stream: true })`: it retains incomplete character sequences internally for the next chunk. section 11.2.1
Listing 11.4 Decoding a text stream one chunk at a time
import assert from "node:assert";
import { once } from "node:events";
import { createServer } from "node:http";
import { setTimeout } from "node:timers/promises";
const words = [
"One ",
"word ",
"at ",
"a ",
"time.",
];
const server = createServer(async (req, res) => {
for (const word of words) { #A
res.write(word); #A
await setTimeout(100); #A
} #A
res.end();
});
server.listen(4567);
await once(server, "listening");
const res = await fetch("http://localhost:4567"); #B
assert( #C
res.body !== null, #C
"The response must have a body", #C
); #C
const resStrings: string[] = []; #D
const decoder = new TextDecoder(); #D
for await (const chunk of res.body) { #D
resStrings.push( #D
decoder.decode(chunk, { stream: true }), #D
); #D
process.stdout.write(text); #D
} #D
process.stdout.write("\n"); #D
console.assert(
resStrings.join("") === "One word at a time.",
"All chunks together form the complete message",
);
server.close();
See TextDecoder used as chunks arrive from an HTTP response body. section 11.2.1
