Invental/Review lessons/Writes that span systems

Writes that span systems

Close the streams you hand to other code

Every time a pipeline passes a stream to code it doesn't control, it owns closing that stream when that code throws. A codemod should never remove the old thing before the new thing is in place.

01 · Before and afterThe pattern

Close what you handed out when a stage fails.

TypeScript
const handedOut = new AbortController()
try {
  for (const stage of pipeline) {
    const src = new Response(body.pipeThrough(new TransformStream(), { signal: handedOut.signal }))
    body = (await stage(src)).body
  }
} catch (err) {
  handedOut.abort(err)                 // closes bodies a stage locked, then threw
  if (!body?.locked) await body?.cancel().catch(() => {})
  throw err
}

02 · Run it on your appThe check

When a file-processing step throws halfway, does your code close the download or storage stream it opened?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.