Node.js Buffers
A Buffer stores bytes. Text decoding gives those bytes a character interpretation, and one visible character can require several bytes.
How it works
Use Buffer.from for known content or initialized allocations rather than exposing uninitialized memory. A network or file chunk can split a multi-byte character, so decoding each chunk independently may produce replacement characters. StringDecoder preserves an incomplete UTF-8 sequence between writes. The fixture deliberately splits one character to demonstrate that boundary with no external file or network dependency.
Run the code
Run this example locally with Node.js. The exact sources are reviewed on Node 22.15.1 and Node 24.22.1 unless a specialised lab states otherwise. Save the complete source as buffers.mjs in a separate empty exercises folder and run node buffers.mjs. There is no Node.js browser runner on Bhylu; the plain JavaScript playground does not provide these filesystem, server or process APIs.
Download the complete source. Ordinary Node scripts need no Apache, PHP or MySQL service. File examples own their temporary folders; network fixtures use loopback and available temporary ports.
Example
import { StringDecoder } from 'node:string_decoder';
const bytes = Buffer.from('kit-é', 'utf8');
const decoder = new StringDecoder('utf8');
const text =
decoder.write(bytes.subarray(0, bytes.length - 1)) +
decoder.end(bytes.subarray(bytes.length - 1));
console.log(bytes.length);
console.log(text);
console.log(Buffer.from('kit').toString('hex'));
Expected result
6 kit-é 6b6974
The six-byte sequence decodes into five visible characters. The streaming decoder keeps the split accented character intact. Encoding a value as hexadecimal is a representation change, not encryption or a protection against disclosure.
Use the Node.js API reference for the precise version contract.