Skip to main content
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Josh Knutson and Ryan Vikander
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Josh Knutson Ryan Vikander

Should The Relationship Between Object Mode And Read Size Be Symmetric In Node.js Streams?

By
Published in

After looking at how Object Mode affects string decoding in Node.js, it got me thinking about Object Mode at a more philosophical level. How should we, as Node.js developers, think about streams in Object Mode? And, how should the nature of the data reflect the configuration of streams? This is a question of intent, not necessarily of technical mechanics.

If you look at the Node.js Stream documentation, on "Object Mode," it states:

A Readable stream in object mode will always return a single item from a call to stream.read(size), regardless of what the size argument is.... No streams in Node core are object mode streams. This pattern is only used by userland streaming libraries.

This makes sense because Object Mode streams deal with distinct, finite values - not with "chunks" that are aggregated into a single, non-divisible value over time. But, does this relationship between Object Mode and read size constitue a symmetric relationship? Meaning, if I am building a Readable stream and each invocation of .push() deals with a distinct, finite value, should I configure the stream to run in "Object Mode"?


 
 
 

 
Should the relationship between Object Mode and .push() size be a symmetric relationship in Node.js?  
 
 
 

As someone who reads and writes a lot of code (JavaScript, Node.js, as well as others), I am concerned with both the technical merits of the code as well as with the way in which the code speaks to my intentions and my mindset. And, the more that I think about it, the more I want to use Object Mode if I am dealing with distinct values. To me, this says, "Not only is the data delimited by coincidence, the configuration of the stream also enforces this behavior."

To frame it another way, if I wrote a stream that only pushed distinct, finite values, I don't think I could offer up a good counter explanation. Meaning, I don't think I could come up with a compelling reason as to why the Stream was NOT running in Object Mode. Sure, I could lean on, "that's just how streams work;" but, to me, this only speaks to a lack of intent.

As always, I must caveat that my Node.js experience is quite limited. So, I would be very interested in hearing what more seasoned Node.js developers have to say on the matter.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel