Welcome to the Linux Foundation Forum!

Stream Readable Watermark

Hi,

In the explanation about createReadStream:

"The fs module here is used for demonstration purposes, readable stream interfaces are generic. The file system is covered in the next section, so we'll avoid in-depth explanation. But suffice to say the createReadStream method instantiates an instance of the Readable constructor and then causes it to emit data events for each chunk of the file that has been read. In this case the file would be the actual file executing this code, the implicitly available __filename refers to the file executing the code. Since it's so small only one data event would be emitted, but readable streams have a default highWaterMark option of 16kb. That means 16kb of data can be read before emitting a data event. So in the case of a file read stream, 64kb file would emit four data events. When there is no more data for a readable stream to read, an end event is emitted."

I dont understand how 64kb emit four data events.

In my test 64kb emit 1 event.

My bash log here can show this:
https://gist.github.com/AlexcastroDev/4719e61a5cf52f7d6ce0ffe641a29f93

The files i tested

Answers

  • My fault,

    I've read the FS interface and check the difference between Readable and fs.createReadStream

    "Unlike the 16 kb default highWaterMark for a stream.Readable, the stream
    returned by this method has a default highWaterMark of 64 kb."

    bests

  • xdxmxc
    xdxmxc Posts: 157

    that's some solid exploration of the area @alexcastro.dev

Categories

Upcoming Training