mafintosh / streamx

An iteration of the Node.js core streams with a series of improvements.
MIT License
226 stars 16 forks source link

Duplex state is inconsistent when receiving data from node core Readable #61

Closed phated closed 2 years ago

phated commented 2 years ago

This PR introduces 2 tests:

  1. Streamx's Readable.from is provided an empty array and the Duplex stream marks the writable side as ended
  2. Node core Readable.from is provided an empty array but the Duplex stream does not mark the writable side as ended

I'm not sure why this inconsistency exists, but I'm trying to update to-through to use a Duplex stream instead of a Transform stream (to better handle backpressure) and this is the failing case in my test suite.

phated commented 2 years ago

Talked with @mafintosh and it seems that this is the wrong way to get the finished state of the writable. It seems you should do something akin to:

let finished = false
const d = new Duplex({
  read (cb) {
    if (!finished) return cb()
    ... do normal push flow ...
    cb()
  },
  final (cb) {
    finished = true
    d.push(...) // starts the flow
    cb()
  }
})