Node.JS Flashcards
Refresh Node.JS knowledge
How many different stream classes are in Node.JS?
Readable, Writable, Transform and Duplex.
Which of the four stream types in Node have a “pipe” method?
Readable, Transform and Duplex (so all readable streams)
What does the “pipe” method in a stream return?
It returns the destination stream, to allow multiple calls to be chained together
What kind of mechanism does the “pipe” method support in a stream? What does it do?
It supports the backpressure. If the consumer is unable to consume data as fast as the producer, then the producer will be paused until the consumer catches up.
Are the errors forwarded upstream with the pipe mechanism in streams?
Not by default, because it would be difficult to know exactly where the error happened.
What kind of stream is the “request” object?
It is a readable stream.
Can you interrupt a “req” request?
Yes, by using req.on(“abort”,…);
What is the difference between flowing and non-flowing mode of a readable stream and which one of them is the default mode?
Non-flowing (paused) mode»_space;> data is explicitly pulled from the stream, on demand; when data finishes I wait for another “readable” event;
ex:
process.stdin(‘readable’, function( ) {
.on(‘readable’, function( ) {
console.log(‘new data available’);
while ( (chunk=process.stdin.read() ) != null {
console.log(‘chunk read’, chunk.toString( );
}
} )
.on (‘end’, function( ) {
process.stdout.write(‘End of stream’)}
} )
What kind of data is contained in a Buffer?
Uint8Array
What kind of data are strings in Node.JS operating with?
strings, Buffer and Uint8Array for streams operating in binary-mode and objects for streams operating in object-mode;
When can a stream switch between non-object to object Mode?
When the streams are created. Is not safe to switch an existing stream into object mode.
How can you retrieve the internal Buffer from a readable or a writable stream?
By using writable.writableBuffer or readable.readableBuffer methods
What kind of role is the highWaterMark option playing in streams?
It regulates the amount on data stored in a stream. It is passed into the Stream constructor, and is measured in “total number of bytes” for regular streams (non-object mode) and in “max stored objects” in object-mode streams.
Explain the mechanism of buffering data in Readable streams.
Data is buffered in a Readable stream when the implementation calls stream.push(chunk); if the consumer of the Stream does not call stream.read( ), the data will sit in the internal queue until consumed.
Explain how a Readable stream is actually working in non-flowing (default) mode.
- The data is pushed into the stream’s internal buffer by calling stream.push( );
ex: const {Readable} = require(‘readable-stream’);
const inStream = new Readable( { read( ) } );
inStream.push(‘bla, bla, bla’);
inStream.push(null) // no more data - Consume the data from the Readable stream by only using the ‘readable’ event and the read( ) function:
inStream.on(‘readable’, ( ) => {
while ( (chunk = inStream.read ( ) ) !== null ) {
console.log(inStream.read( ); // consume data
}
} - Detect the closing of the stream:
inStream.on(‘end’, ( ) => {
console.log(‘stream ended’); }
What is the interaction between the “readable” and the “data” events in a Readable stream?
The “readable” event takes precedence (that’s why the non-flowing mode is the default) in controlling the flow, so the “data” event will only be emitted when the stream.read( ) is called; in that case, the ‘readableFlowing’ property would become false.
On the other side, if there any “data” listeners when the “readable” event listener is removed, the stream will start flowing.
Explain how a Readable stream is actually working in flowing mode.
If the readable stream is in flowing mode (non-default) then:
1. The data is pushed into the stream’s internal buffer by calling stream.push( );
ex: const {Readable} = require(‘readable-stream’);
const inStream = new Readable( { read( ) } );
inStream.push(‘bla, bla, bla’);
inStream.push(null) // no more data
2. Consume the data from the Readable stream by attaching a listener to the “data” event:
inStream.on(‘data’, (chunk) => {
console.log(‘new data available’, chunk.toString( ));
} );
3. Detect the closing of the stream:
inStream.on(‘end’, ( ) => {
console.log(‘stream ended’); }
What is the recommended way to treat an error that occurs during the process of reading data from a readable stream in flowing mode? Is there any difference compared to the non-flowing mode?
It is recommended that the errors occurring during the processing of the read( ) method are emitted using the 'error' event rather than being thrown (otherwise, throwing an error will probably result in unexpected behaviour, depending whether the stream is in flowing or non-flowing mode); for ex: const {Readable} = require('readable-stream'); const myStream = new Readable( { read(size) { if (checkSomeErrorCondition ( ) ) { process.nextTick ( ( ) => this.emit('error', err) ); return; } // do the regular normal work in here; } } );
Is it possible for the “read’ event to be triggered several times when the data becomes available?
Yes
Is it possible to have multiple “data” events per chunk when using a Readable stream?
No, only 1 “data” event per chunk
How do you put a readable stream in flowing mode?
You enable flowing mode:
- by attaching a listener to the ‘data’ event;
- or by explicitly invoking the resume( ) method;
- or by piping into a writable stream by calling the stream.pipe( ) method;
How do you temporarily stop a readable stream in flowing mode?
By using the pause ( ) method;
What happens if you remove the “data” listener from a Readable stream?
The stream will go back to the default non-flowing mode, but that will not automatically pause the stream;
When is the “data” event emitted from a Readable stream?
Whenever the stream is relinquishing ownership of a chunk of data to a consumer. That means whenever:
- the stream is switched in flowing mode by calling readable.pipe(), readable.resume(), or by attaching a listener callback to the ‘data’ event;
- the readable.read() is called and a chunk of data is available to be returned