node.js Readable: can push(null) be called even if the last push returned false?

14 views
Skip to first unread message

Benjamin Pasero

unread,
Jan 22, 2018, 5:09:39 AM1/22/18
to nodejs
Hi,

I am looking into implementing my own Readable (by following https://nodejs.org/docs/v7.9.0/api/stream.html#stream_new_stream_readable_options). The documentation is a bit unclear to me in one point: if the EOS (end of stream) is reached, I should call this.push(null), however do I also have to check if the last call to push returned false and then not signal EOS?

My stream basically looks like this:

const readable = new Readable({
    read: function () {
        try {
            let chunk: string;
            let canPush = true;

            // Push all chunks as long as we can push and as long as
            // the underlying reader returns strings to us
            while (canPush && typeof (chunk = reader.read()) === 'string') {
                canPush = this.push(chunk);
            }

            // Signal EOS by pushing NULL
            if (typeof chunk !== 'string') {
                this.push(null);
            }
        } catch (error) {
            this.emit('error', error);
        }
    }
});

In this case I am happily calling this.push(null) even though maybe the last push signald I should wait?

Thanks for feedback,
Ben
Reply all
Reply to author
Forward
0 new messages