Complete the code to create a readable stream from a string.
const { Readable } = require('stream');
const readable = [1] Readable({
read() {
this.push('Hello');
this.push(null);
}
});The new Readable() constructor creates a readable stream instance where you can define a custom read() method to push data like strings.
Complete the code to write data to a writable stream.
const { Writable } = require('stream');
const writable = new Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString());
[1]();
}
});The write method must call the callback function to signal that it finished processing the chunk.
Fix the error in the transform stream by completing the missing method call.
const { Transform } = require('stream');
const transform = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
[1]();
}
});In a transform stream, the transform method must call callback() to signal that the chunk has been processed.
Fill both blanks to create a duplex stream that echoes input data.
const { Duplex } = require('stream');
const duplex = new Duplex({
read(size) {
// No data to push here
},
write(chunk, encoding, [1]) {
this.push(chunk);
[2]();
}
});In the write method of a duplex stream, the third argument is the callback function that must be called to signal completion.
Fill all three blanks to create a transform stream that reverses input strings.
const { Transform } = require('stream');
const reverse = new Transform({
transform(chunk, encoding, [1]) {
const input = chunk.toString();
const reversed = input.split('').[2]().join('');
this.push(reversed);
[3]();
}
});The transform method uses the callback function to signal completion. The string is reversed by splitting, reversing, and joining. Both {{BLANK_1}} and {{BLANK_3}} are the callback function.