Skip to content

juliangruber/stream-write

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stream-write

Write to streams, respecting backpressure.

Example

Write random numbers to an http response until it ends:

import write from 'stream-write'
import http from 'node:http'

http.createServer(async (req, res) => {
  while (true) {
    const isOpen = await write(res, String(Math.random()))
    if (!isOpen) break
  }
}).listen(8000)

write(stream, chunk)

Write chunk to stream and returns a Promise that is resolved once stream is writable again, or rejects if an error happened.

The promise's resolved value is false when stream ended and you should stop writing to it.

HTTP special casing

When an HTTP request ended, the HTTP response stays writable, only it's underlying socket closes. To stay consistent with other streams' behavior, in that case write() will throw an error itself.

Installation

$ npm install stream-write

License

MIT