knaxuslearn · build · ship

Roadmaps / JavaScript / Phase 2

Phase 2 of 4 · Study weeks 5-8

Modules, streams, workers and correct data

This phase makes the second checkpoint possible: a service that reads untrusted bodies safely, keeps CPU work off the event loop and ships as a package other code can import.

Topics every week

  1. Week 58 h
    • ES modules and CommonJS interop
    • The exports map and conditions
    • Cycles and top-level await
    • The dual package hazard

    Hand inA package consumed from ESM, CommonJS and a bundler, with a three-way test matrix

  2. Week 68 h
    • Web Streams and Node streams
    • pipeline and error cleanup
    • Backpressure
    • Byte caps and decompression limits

    Hand inA streaming reader that cuts a 1 GB response at 2 MB with flat memory

  3. Week 78 h
    • Worker threads and message passing
    • Structured clone and transfer
    • SharedArrayBuffer and Atomics
    • When threads lose to the main thread

    Hand inA worker pool with a benchmark that shows where a worker starts to win

  4. Week 88 h
    • Floating point, BigInt and money
    • Unicode, normalisation and Intl
    • Temporal for dates and zones
    • JSON edge cases

    Hand inCheckpoint 2, plus a correctness audit of 10 date, money and text cases

Practice projects

  • Practice 1, the consumer matrix (Week 5): ship a small package and load it from an ES module, a CommonJS file and a bundler. Done when: all three pass in CI, and a test proves a deep import that is not in exports fails.
  • Practice 2, the byte cap (Week 6): stream a generated 1 GB body through your reader. Done when: the read stops at 2 MB, RSS grows by under 50 MB over 100 runs, and the fake server sees every socket close.
  • Practice 3, worker break-even (Week 7): parse HTML of growing size on the main thread and in a worker. Done when: a table shows the size where the worker wins, and says what copying and transfer cost.
  • Optional, a ring buffer on SharedArrayBuffer: pass numbers between threads with Atomics. Done when: a stress test shows no lost or duplicated values in 10 million messages.

GitHub repos to study

Existence, last commit and README notices were checked on 22 Nov 2026 by shallow clone. Every repository below had a commit in the last six months. Archive status comes from the README, not from the GitHub setting, so look again before you rely on one.

RepoWhat to readWeek
nodejs/undiciHow fetch, redirects and body streams are implemented6
whatwg/streamsThe standard and its reference implementation, for backpressure6
nodejs/nodelib/ streams and worker_threads code6
piscinajs/piscinaA worker pool: queueing, task transfer, cancellation7
js-temporal/temporal-polyfillA reference for correct date handling where Temporal is missing8

Milestone checkpoint

Extend the fetcher into Checkpoint 2. Read bodies as a stream with the 2 MB cap and the redirect limit, move HTML parsing behind a worker pool interface, and publish the package with an exports map. Add the redirect loop and the gzip bomb to the fake server. Hand in the byte-cap measurements, the matrix results and ADR-3 (stream cap and where it is enforced) and ADR-4 (worker pool or main thread, with the break-even number).

Check yourself

Answer each question in your head first, then open it to check.

  1. What does an exports map do, and what goes wrong without one?
    It lists the public entry points and the conditions (import, require) for each. Without it, deep imports become part of your API, and a mixed consumer can load two copies of the package.
  2. What is backpressure, and what happens to memory without it?
    The consumer tells the producer to pause. Without it, a fast producer queues data in memory until the process runs out.
  3. When is a worker thread slower than the main thread?
    For small tasks, where starting the worker and copying data costs more than the work. Transferring buffers or sharing memory removes the copy.
  4. Why does 0.1 + 0.2 differ from 0.3, and how do you store money?
    Numbers are binary floating point, and 0.1 has no exact binary form. Store money as integers in the smallest unit, or use a decimal library.
  5. What does a gzip bomb do, and why is the Content-Length header no defence?
    A small compressed body expands to gigabytes. The header describes the compressed size, so count the decompressed bytes as you read.