demo · v131

stream reader: release the lock on scope exit

A common bug: const reader = stream.getReader() then forget to reader.releaseLock(). The stream is permanently locked, other consumers can't read it, and you don't notice until the next bug report. Wrap the reader as AsyncDisposable and the lock releases automatically when the scope ends — including on early returns and thrown errors.

locks acquired: 0 locks released: 0 currently leaked: 0

stream reader log

the api

function disposableReader(stream) {
  const reader = stream.getReader();
  reader[Symbol.asyncDispose] = async () => {
    reader.releaseLock();
  };
  return reader;
}

async function read(stream) {
  await using reader = disposableReader(stream);
  const { value } = await reader.read();
  return value;
}  // lock released here, even if read() threw

see also