Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior with readable stream event "readable" in different versions of Node #31464

Closed
acdibble opened this issue Jan 22, 2020 · 2 comments
Labels
stream Issues and PRs related to the stream subsystem.

Comments

@acdibble
Copy link

  • Version:
    12.14.1, 10.18.1
  • Platform:
    Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64
  • Subsystem:
    stream

Minimal example:

// index.js

const { Transform } = require("stream");

const stream = new Transform({
  transform(chunk, encoding, cb) {
    this.push(JSON.parse(chunk.toString()));
    cb();
  },
  writableObjectMode: false,
  readableObjectMode: true
});

stream.on("readable", () => {
  const data = stream.read();
  console.log("data", data);
});

stream.end(Buffer.from(JSON.stringify({ foo: "bar" })));
$ nvm use 12.14.1
Now using node v12.14.1 (npm v6.13.4)
$ node index
data { foo: 'bar' }
data null
$ nvm use 10.18.1
Now using node v10.18.1 (npm v6.13.4)
$ node index
data { foo: 'bar' }
$ nvm use 8
Now using node v8.11.3 (npm v5.6.0)
$ node index
data { foo: 'bar' } object
data null

It looks like in Node 10, there is only one readable event emitted when the stream is ended with data, whereas in Node versions 8 and 12, there is one readable emitted for the new data, and one with null to mark the end of the stream.

Is Node 10 also supposed to be emitting null as well when the stream closes?

@ronag
Copy link
Member

ronag commented Jan 23, 2020

As far as I can see readable should always be emitted when a Readable is ended (push(null)). The code is pretty explicit about it. I'm not exactly sure why this is the case but I don't see any harm in it.

Related:

@targos
Copy link
Member

targos commented Dec 26, 2020

Closing because there was no follow-up.

@targos targos closed this as completed Dec 26, 2020
@targos targos added the stream Issues and PRs related to the stream subsystem. label Dec 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants