liveBook · Manning

Buffer

As explained in JavaScript in Depth

A Node.js type for holding raw binary data and manipulating sequences of bytes without treating them as text.

A file can exist on disk yet appear to be missing to a Node.js application. If its filename contains bytes that are not valid UTF-8, decoding those bytes into a string changes the name the application later passes back to the file system. Keeping the filename as raw bytes avoids that mistaken interpretation. section 2.1.4

A Node.js Buffer holds a sequence of zero or more bytes. It predates JavaScript Typed Arrays and is now a subclass of Uint8Array. Unlike a string, it can carry binary data without first assigning the bytes a text encoding; conversion to or from text is a separate step. section 2.1.2 section 2.1.4

Inspect a UTF-8 encoding

Buffer.from('⇇') produces <Buffer e2 87 87>. Those three bytes encode codepoint U+0021C7 in UTF-8; the Buffer lets you inspect the encoded bytes rather than the displayed character. section 2.1.2

Keep directory-entry names as bytes

Use readdirSync('./tmp', { encoding: 'buffer', withFileTypes: true }) when directory-entry names must remain uninterpreted bytes. The returned names can then be passed to file system APIs as Buffers instead of being decoded as strings first. section 2.1.4

Do not confuse a Buffer object with a stream’s internal buffer. A stream buffer is a place where chunks wait; its growth and backpressure are separate concerns from how a Buffer represents bytes. section 10.3.3 section 2.1.2

How JavaScript in Depth covers it

How deep it goes
Defines Buffer as a byte sequence, identifies its Uint8Array relationship, and works through encoding and filename failures without detailing its memory layout.
Examples
Uses a three-byte UTF-8 encoding and a directory-reading example that returns filenames as Buffers.
What it assumes
Builds the byte-sequence intuition alongside a UTF-8 explanation, while assuming familiarity with JavaScript and basic Node.js API calls.
What you can do after
Apply it via Node.js Buffer and file system APIs to inspect encoded bytes and preserve filenames without unintended decoding.
How it connects
Connects Buffer to UTF-8, JavaScript strings, Uint8Array, and file system APIs while distinguishing it from stream buffering.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.