Backend

Pairing on a parser

We parsed query strings by hand for an afternoon, then deleted it and used URLSearchParams. Notes from both sides of the keyboard.

Lene and I paired on stummel’s query parsing for an afternoon. We wrote 60 lines, found four bugs in them, and replaced all of it with URLSearchParams. The afternoon was not wasted: we now know which of those bugs the platform handles.

What the 60 lines got wrong

  1. + is a space in a query string, not a plus.
  2. A key without = is a key with an empty value.
  3. The same key twice is a list, not the last value.
  4. Percent-encoded UTF-8 needs decoding as a whole, not byte by byte.
const params = new URL(req.url).searchParams;
params.getAll('tag'); // ['ts', 'node'] for ?tag=ts&tag=node
The fastest way to learn what a standard library does is to write it badly first, with someone watching.

Lene wrote that sentence on the whiteboard and we left it there.

Comments

Buy Logbuch