Node.js Stream Complete Reference
Node.js streams are a type of data-handling method and are used to read or write input into output sequentially. Streams are used to handle reading/writing files or exchanging information efficiently.
Example:
Javascript
// Node.js program to demonstrate the // writable.write() method // Including stream module const stream = require( 'stream' ); // Creating a stream and creating // a write function const writable = new stream.Writable({ // Write function with its // parameters write: function (chunk, encoding, next) { // Converting the chunk of // data to string console.log(chunk.toString()); next(); } }); // Calling write method with // all its parameter writable.write( "GfG" , "utf8" , () => { console.log( "CS-Portal!" ); }); |
Output:
GfG true CS-Portal!
The Complete List of Streams are listed below:
Writable Streams
Writable Streams Methods |
Descriprion |
---|---|
cork() | Write every data into the buffer memory. |
destroy() | Destroy the created stream and you cannot call the write() method |
end() | It is an inbuilt application programming interface of Stream module |
setDefaultEncoding() | Set the default encoding for a Writable stream. |
uncork() | Flush all the buffered data when stream.cork() method was called. |
_write() | It is affixed with an underscore as it is inside the class that defines it. |
write() | Write some data to the Writable stream. |
read() | Read the data out of the internal buffer. |
destroy() | Destroy the stream. |
pause() | Stop the flowing mode from emitting ‘data’ events. |
isPaused() | Check the current operating state of the Readable streams. |
resume() | Paused data that can be resumed again and data starts flowing again. |
Writable Streams Property |
Description |
---|---|
Length | check the number of bytes in the queue that is ready to be written. |
ObjectMode | Get the object mode value of the Writable stream. |
Finished | The writable.writableFinished property is set to true instantly before the emit of the ‘finish’ event. |
Corked | Check the number of times you need to call the uncork() function so that you can fully uncork the stream. |
destroyed | Check the writable.destroy() method is being called or not. |
writable | Check the writable.write() method is safe to call or not. |
writableEnded | Check the writable.end() method is being called or not. |
HighWaterMark | Check the highWaterMark value which was passed while creating the Writable. |
destroyed | Check the readable.destroy() function is being called or not. |
Writable Streams Event |
Description |
---|---|
close | Its hidden resources (for example, a file descriptor) is being closed. |
finish | The ‘finish’ event in a Writable Stream is emitted after the Calling of writable.end() method |
Stream pipe | When the stream.pipe() method is being called on a readable stream |
unpipe | When stream.unpipe() method is being called on a Readable stream |
Readable Streams
Readable Streams Methods |
Description |
---|---|
pipe() | Attach a Writable stream to the readable stream so that it consequently |
unpipe() | Detach a Writable stream which was previously attached while using the stream.pipe() method. |
unshift() | Readable Stream is utilized to push a chunk of data back into the internal buffer. |
Readable.from() | construct Readable Streams out of iterators. |
setEncoding() | Set the encoding of the data read. |
Readable Streams Property |
Description |
---|---|
readableLength | Check the number of bytes in the queue which is ready to be read. |
readableHighWaterMark | Check the value of highWaterMark used while constructing Readable streams. |
readableFlowing | Check if the streams are in flowing mode or not. |
readableEnded | Check if the end event is emitted or not. |
readableObjectMode | Check the objectMode of the stream |
readable | Check if it is safe to call readable.read() method. |
Readable Streams Events |
Description |
---|---|
pause | When stream.pause() is being called and readableFlowing property is not false. |
resume | When stream.resume() is being called and readableFlowing property is not true. |
error | The ‘error’ event in Readable stream can be emitted at any time. |
readable | When the data is available so that it can be read from the stream |
data | When readable.pipe() and readable.resume() method is called for switching the stream |
close | When the stream and any of its hidden resources are being closed |
end | When there is no available data to be consumed from the readable stream. |
Transform Streams
Transform Streams Methods |
Description |
---|---|
destroy() | Destroy the transform stream, and also emits an ‘error’ event optionally. |
pipeline() | That is used to the pipe by linking. |
finished() | It is utilized to receive an alert if a stream is not writable or readable anymore. |
Please Login to comment...