mafintosh / streamx

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

AsyncIterator in a Duplex stream never ends #62

Closed tinchoz49 closed 2 years ago

tinchoz49 commented 2 years ago

I was trying to iterate over the data in a Duplex stream since extends from Readable but it never ends properly.

I think the problem is that in a Duplex stream the close event for the Readable side never happen. Is that the correct behavior in this case?

import { Duplex } from 'streamx'

;(async () => {
  const stream = new Duplex({
    read (cb) {
      this.push('test')
      this.push(null)
      cb()
    }
  })

  for await (const chunk of stream) {
    console.log(chunk)
  }

  console.log('done') // never get this
})()
mafintosh commented 2 years ago

I'd say, that's a bug on your end since you are not ending the writable side of the stream.

mafintosh commented 2 years ago

ie, if you add stream.end() that works yea?

tinchoz49 commented 2 years ago

ah :sweat_smile: is true in my head I was thinking it like a half open duplex. Thank you Mathias!

mafintosh commented 2 years ago

It's tricky. Cause we could end the iterator on end, but then you cannot catch close errors. We should prob make this clear in the docs tho!