StringDecoder and split characters
Local practice: Node.js 22.16.0 and npm 10.9.2 on Linux x64; proposed teaching baseline 26.13.1 LTS remains untested. Use a fresh disposable folder for each example, copy only the shown files and run the fixed command. No packages or external services required. Static examples are not ByteMind runner support. The archive is author-only; do not copy expected.json into the example directory.
Preserve a split accented character
import { Buffer } from "node:buffer";import { StringDecoder } from "node:string_decoder";const bytes = Buffer.from("café", "utf8");const decoder = new StringDecoder("utf8");const first = bytes.subarray(0, 4);const second = bytes.subarray(4);console.log(JSON.stringify([decoder.write(first), decoder.write(second), decoder.end()]));console.log(first.toString("utf8") + second.toString("utf8"));Run in this fixture directory:
node main.mjsExpected stdout:
["caf","é",""]caf��Expected exit status: 0. Expected stderr: empty. The first chunk ends with an incomplete é; the decoder combines its saved byte with the next chunk. Independent conversion yields two replacement characters.
How it works
Construct new StringDecoder("utf8") from node:string_decoder, call write for each byte chunk, and call end once the input ends. A write may produce an empty string while bytes are pending. The final call flushes buffered data; incomplete input can produce the replacement character U+FFFD. In contrast, Buffer.toString on each independent chunk has no decoder state shared with the next chunk. TextDecoder also supports incremental decoding with its stream option; its fatal option can reject malformed input. Choose a decoding policy that matches your protocol. StringDecoder is not a strict UTF-8 validator.
Flush an incomplete final sequence
import { Buffer } from "node:buffer";import { StringDecoder } from "node:string_decoder";const decoder = new StringDecoder("utf8");console.log(JSON.stringify(decoder.write(Buffer.from([0xe2, 0x82]))));console.log(decoder.end().codePointAt(0).toString(16));Run in this fixture directory:
node main.mjsExpected stdout:
""fffdExpected exit status: 0. Expected stderr: empty. Two bytes of a three-byte UTF-8 sequence remain pending until end flushes them as a replacement character.
Common mistake
Do not assume chunk boundaries coincide with character boundaries. Do not omit end when the source ends or mistake replacement characters for proof of valid input.
Try it yourself
Encode a short word containing an emoji, split its bytes in the middle of the emoji, and compare independent chunk decoding with a single StringDecoder instance. Test an incomplete final chunk.
Further reading: Official documentation 1, Official documentation 2. Related ByteMind reference candidate: node-stringdecoder-write (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
StringDecoder and split characters — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Why can StringDecoder.write return an empty string?
What should be called when all chunks are received?
Does StringDecoder guarantee malformed UTF-8 is rejected?