Node.js Stream writable._write() Method
The writable._write() method is an inbuilt application programming interface of Stream module which is used to implement a writable stream. The writable._write() method is affixed with an underscore as it is inside the class that defines it. Moreover, the user program must not call it directly. This method implemented by using child classes and it is called by the internal Writable class methods only.
Syntax:
writable._write( chunk, encoding, callback )
Parameters: This method accept three parameters as mentioned above and described below:
- chunk: It is the data to be written which can be of type buffer, string or any.
- encoding: It is the type of encoding to be used if chunk is of type string.
- callback: It is a method which is called to check if the write completed or failed. The first argument passed to the callback must be “Error” object if the call didn’t succeeded and null if the write succeeded.
Below examples illustrate the use of writable._write() method Node.js:
Example 1:
const {Writable} = require( "stream" );
const charchecks = new Writable({
write(chunk, encoding, callback){
const string = chunk.toString();
if (string.includes( "\/" )){
callback(Error( "Forbidden character" ));
}
else
{
console.log(string);
callback();
}
}
});
process.stdin.pipe(charchecks).on('error', console.log);
console.log( "Enter the string: " );
|
Now, you need to run the code and enter the String in run time to get the output.
Enter the string:
GeeksforGeeks
GeeksforGeeks // Output
Enter the string:
GfG
GfG // Output
Enter the string:
Nidhi
Nidhi //Output
Now, to exit it you need to press control + C.
Example 2:
const {Writable} = require( "stream" );
const charchecks = new Writable({
write(chunk, encoding, callback){
const string = chunk.toString( 'hex' );
console.log(string);
if (string.includes( "c" )){
callback(Error( "This is an error." ));
}
else
{
console.log(string.length);
callback();
}
}
});
process.stdin.pipe(charchecks).on('error', console.log);
console.log( "Enter the string: " );
|
Now, you need to run the code and enter the String in run time to get the output.
Output:
Enter the string:
Geeks
4765656b730a // encoded string
12 // length of encoded string
Nidhi
4e696468690a
12
portal
706f7274616c0a // encoded string contains "c" so length of it
// is not returned and an error is thrown
Error: This is an error.
at Writable.write [as _write] (/home/runner/QuickwittedDistantCensorware/index.js:25:16)
at doWrite (_stream_writable.js:415:12)
at writeOrBuffer (_stream_writable.js:399:5)
at Writable.write (_stream_writable.js:299:11)
at ReadStream.ondata (_stream_readable.js:710:20)
at ReadStream.emit (events.js:198:13)
at ReadStream.EventEmitter.emit (domain.js:448:20)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at ReadStream.Readable.push (_stream_readable.js:224:10)
Now, to exit it you need to press control C.
So, here an error is thrown as the run time input includes the stated character which is forbidden character so, an error is thrown.
Reference: https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback_1
Last Updated :
11 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...