Node.js traps · Chapter 5 of 16
A character split across chunks
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The event loop
- 3 tasks
- Node.js
- In the all labs plan
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: break a Marathi name on purpose
Write reproduce() in repro.js. Make a stand-in for the feed's socket: a Readable from node:stream whose read() does nothing, so you push the chunks yourself. Take the bytes of the feed 'नेहा बापट\nPranav Joshi\n' with Buffer.from, push the first 4 bytes as one chunk and the rest as a second chunk, then push null to end the stream. Hand the stream to readNames from box-office-feed.js and resolve to the names it gives.
The check runs your reproduce() twice: beside the original box-office-feed.js the names must have a � (U+FFFD) in them, and beside a fixed box-office-feed.js they must be exactly ['नेहा बापट', 'Pranav Joshi']. No real socket and no waiting: reproduce() must finish within 2 seconds.
- Task 2
box-office-feed.js: names that arrive whole, however the bytes are cut
Fix readNames(stream) in box-office-feed.js so every name comes out exactly as the old box office sent it, wherever the chunks are cut: a letter cut between two chunks, every byte in its own chunk, ₹ (3 bytes) and an emoji (4 bytes) all come out whole. It resolves to the names, one per line of the feed, in order, skipping empty lines and keeping a last name that has no newline after it, and it rejects with the stream's error when the stream fails.
Keep the export and the log line feed: <n> names read as they are. The check uses its own feed of Marathi and English names, ₹240 and an emoji, cut into two chunks after every byte, then one byte per chunk. Removing the � from the text is not a fix: the letter is lost, and the check notices.
- Task 3
box-office-feed.test.js: a test that would have caught it
Write a vitest test file, box-office-feed.test.js, that would have caught the broken names before the kiosks printed them. The check runs your file three times: beside the original box-office-feed.js, where at least one test must fail; beside the fixed box-office-feed.js, where every test must pass; and beside a wrong fix that removes the � from the text (so the cut letter is lost), where at least one test must fail.
So push a feed into a Readable in chunks cut inside a letter (a Marathi or Hindi name, or ₹), and expect the exact names back, letter for letter. A feed of English names only passes every version. Each test must finish within 3 seconds.
What you will learn
- repro.js: break a Marathi name on purpose
- box-office-feed.js: names that arrive whole, however the bytes are cut
- box-office-feed.test.js: a test that would have caught it
More in The event loop
Build "A character split across chunks" yourself
Parda's booking backend has run on Node for a year, and Nikhil keeps handing you incidents where Node did exactly what the team told it. Each is a runtime trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.