How Node really works · Chapter 7 of 34

Buffers

By the end of this chapter you can measure, encode, join and pack raw bytes, and read numbers back out of them. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Files and data
  • 6 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.

Why you need this

Tapri's kitchen printer is older than the app. It does not speak JSON: it takes a short run of bytes over a cable and prints a ticket. Last week it printed ₹40 on every masala chai, and Ravi had a queue of confused customers at the 4 pm rush. Anjali also needs the UPI payment note inside the QR code, written in base64.

Strings cannot do any of this. A string's length is not the number of bytes a printer line holds, text that arrives in pieces breaks if you decode each piece, and a string has no way to say "the order number is the two bytes at position 1". Node holds raw bytes in a Buffer.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    lineSize: a receipt line in characters and in bytes

    Write lineSize(line). It returns an object with two numbers: length, the string's JavaScript length (UTF-16 units, what .length says), and bytes, how many bytes the line takes in UTF-8.

    The two differ as soon as the line has a rupee sign or an emoji. An empty line gives { length: 0, bytes: 0 }.

  2. Task 2

    qrCodec: the UPI note in base64 and hex, and back

    Fill qrCodec with three methods. toBase64(text) returns the base64 of the text's UTF-8 bytes, with its = padding. toHex(text) returns the hex of those bytes, two lowercase digits per byte. fromBase64(encoded) turns base64 back into the UTF-8 text.

    The note can hold a rupee sign and emoji, so the round trip text to base64 to text must give back exactly the same string.

  3. Task 3

    joinChunks: rebuild the printer's text without breaking a character

    Write joinChunks(chunks). chunks is an array of Buffers that arrived one after another, and a chunk can end in the middle of a character (a rupee sign is split between two chunks in the check). It returns the whole text as a string, decoded as UTF-8, with every character whole.

    An empty array gives an empty string.

  4. Task 4

    buildTicket: the exact bytes the kitchen printer expects

    Write buildTicket({ type, orderId, amount, item }). It returns a Buffer: type as 1 byte at offset 0, orderId as 2 bytes big-endian at offset 1, amount in rupees as 4 bytes big-endian at offset 3, the item's length in bytes as 1 byte at offset 7, then the item's UTF-8 bytes from offset 8. The buffer is exactly 8 bytes plus the item's bytes long.

    The check also builds a ticket with orderId 65000, amount 70000 and an item with an emoji, so amount needs all 4 bytes and the length byte must count bytes, not characters.

  5. Task 5

    parseTicket: read a ticket back from the printer

    Write parseTicket(buf). It reads a ticket in the same layout as buildTicket and returns { type, orderId, amount, item }, the three numbers and the item as a UTF-8 string.

    The buffer can go on after the item with bytes of the next ticket: read only as many item bytes as the length byte at offset 7 says.

  6. Task 6 · interview question

    Interview: predict what subarray shares and what Buffer.from copies

    The starter shows a snippet that makes a buffer, a view of its first two bytes and a copy of its last two, writes one byte through the view and one through the copy, then pushes three strings to log. Set predictedViews to those three strings, in the order they are pushed.

    The check runs the same snippet and compares the two lists in order.

What you will learn

  • Buffer.from and byteLength: a string's bytes, and how many there are.
  • Encodings: the same bytes written as UTF-8 text, hex or base64.
  • Chunks: why text is joined as bytes before it is decoded.
  • Numbers at offsets: writing and reading fixed-size numbers, and views that share memory.

More in Files and data

Build "Buffers" yourself

Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.