Skip to lesson
Exit
Coding & Engineering Craft: TS, Rust, Python on the Hot Path1 / 3

1 min lesson

Worked example: a stream transformer

Imagine this comes up at work: "You're asked to parse newline-delimited events from an async byte stream. What is the single most important edge case to handle and how?" Start with the practical move.

Step 1 of 3

Worked example: a stream transformerpartial chunks are the trap

The classic version hands you a byte stream of newline-delimited JSON events and asks you to parse and re-emit them. The failure everyone forgets is that a chunk can split a line in half. Buffer the remainder and only emit on a complete delimiter.

Learn more

Full explanation

SSE/NDJSON parsing

SSE/NDJSON parsing: the leftover buffer is the part that separates pass from fail.ts
async function* parseLines(
  chunks: AsyncIterable<Uint8Array>,
): AsyncGenerator<string> {
  const decoder = new TextDecoder();
  let buf = "";
  for await (const chunk of chunks) {
    buf += decoder.decode(chunk, { stream: true });
    let nl: number;
    while ((nl = buf.indexOf("\n")) !== -1) {
      const line = buf.slice(0, nl);
      buf = buf.slice(nl + 1);
      if (line.trim()) yield line;
    }
  }
  // flush a trailing line with no terminating newline
  if (buf.trim()) yield buf;
}
Watch out

Do not assume one chunk equals one logical message. That single assumption is the most common silent failure in streaming questions and on the real path it corrupts a user's Tab completion mid-token. Call out the partial-chunk case in the first minute, even before you code it.

Interview move

For any of these, lead with correctness and failure handling, then complexity, then tradeoffs - in that order. Saying “let me get a correct version that buffers partial chunks, then we'll talk throughput” signals the production-owner instinct this team hires for.