Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a Transform stream in Node.js?
A Transform stream is a type of stream that can read data, modify or process it, and then output the transformed data. It acts as both a readable and writable stream.
Click to reveal answer
intermediate
Which method must you implement when creating a custom Transform stream?
You must implement the _transform(chunk, encoding, callback) method. This method processes each chunk of data and calls the callback when done.
Click to reveal answer
intermediate
How do Transform streams differ from Duplex streams?
Transform streams are a special kind of Duplex stream where the output is computed from the input. Duplex streams can read and write independently without transforming data.
Click to reveal answer
beginner
What is the purpose of the callback function in the _transform method?
The callback signals that the current chunk has been processed. It can pass an error to indicate failure. The transformed data should be pushed using this.push() before calling the callback.
Click to reveal answer
beginner
Give a simple real-life analogy for a Transform stream.
Imagine a juice maker: it takes fruits (input), processes them (transforms), and outputs juice (transformed data). The juice maker is like a Transform stream.
Click to reveal answer
What does a Transform stream do in Node.js?
AReads data, processes it, and outputs transformed data
BOnly reads data without modifying it
COnly writes data without reading
DBuffers data without processing
✗ Incorrect
Transform streams both read and write data, modifying it as it passes through.
Which method is essential to override in a custom Transform stream?
A_write()
B_transform()
C_read()
D_flush()
✗ Incorrect
The _transform() method handles processing each chunk of data.
What arguments does the _transform method receive?
Ainput, output, next
Bdata, error, done
Cchunk, encoding, callback
Dbuffer, length, finish
✗ Incorrect
The _transform method receives the chunk of data, its encoding, and a callback to signal completion.
How is a Transform stream related to a Duplex stream?
AThey are unrelated stream types
BTransform streams only read data
CDuplex streams only write data
DTransform streams are a type of Duplex stream that modifies data
✗ Incorrect
Transform streams extend Duplex streams by transforming data as it passes through.
What happens if you don't call the callback in _transform?
AThe stream will pause and not continue processing
BThe stream will automatically continue
CThe data will be lost silently
DThe stream will throw an error immediately
✗ Incorrect
Not calling the callback blocks the stream from moving to the next chunk.
Explain how a Transform stream works in Node.js and why it is useful.
Think about how data flows through a machine that changes it.
You got /5 concepts.
Describe the role of the _transform method and the callback function inside it.
Focus on how each piece of data is handled step-by-step.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Transform stream in Node.js?
easy
A. To read data from a file without changing it
B. To write data to a file without reading
C. To modify or transform data chunks as they pass through the stream
D. To buffer all data before processing
Solution
Step 1: Understand the role of Transform streams
Transform streams allow you to change data while it flows, unlike plain readable or writable streams.
Step 2: Identify the correct purpose
Only To modify or transform data chunks as they pass through the stream describes modifying data chunks during streaming, which is the key feature of Transform streams.
Final Answer:
To modify or transform data chunks as they pass through the stream -> Option C
Quick Check:
Transform streams change data on the fly = B [OK]
Hint: Transform streams change data chunks during flow [OK]
Common Mistakes:
Confusing Transform streams with simple readable or writable streams
Thinking Transform streams buffer all data before processing
Assuming Transform streams only read or write without modification
2. Which of the following is the correct way to create a Transform stream using the stream module in Node.js?
easy
A. const { Transform } = require('stream'); const myTransform = new Transform({ transform(chunk, encoding, callback) { callback(null, chunk); } });
A. The callback should be called with null and the transformed chunk, so no error
B. The transform method is missing the encoding parameter
C. The chunk should not be converted to string before reversing
D. The transformed chunk should be pushed using this.push(), not passed as callback argument
Solution
Step 1: Review Transform stream callback usage
In Transform streams, the transformed data must be pushed using this.push(), not passed as the second argument to callback.
Step 2: Identify the mistake in the code
The code incorrectly calls callback(null, reversed) instead of pushing reversed data and then calling callback().
Final Answer:
The transformed chunk should be pushed using this.push(), not passed as callback argument -> Option D
Quick Check:
Use this.push() for output, callback(null) to signal done = C [OK]
Hint: Push transformed data, then call callback(null) [OK]
Common Mistakes:
Passing transformed data as callback second argument
Not calling callback at all
Ignoring this.push() method
5. You want to create a Transform stream that filters out all chunks containing the word "skip" (case insensitive) and passes through all other chunks unchanged. Which code snippet correctly implements this behavior?
hard
A. const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (chunk.toString().toLowerCase().includes('skip')) { callback(); } else { this.push(chunk); callback(); } } });
B. const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (chunk.includes('skip')) { this.push(chunk); } callback(); } });
C. const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (!chunk.toString().includes('skip')) { this.push(chunk); } callback(null); } });
D. const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (chunk.toString().toLowerCase().indexOf('skip') === -1) { this.push(chunk); callback(); } } });
Solution
Step 1: Understand filtering logic
Chunks containing "skip" (case insensitive) should be ignored (not pushed), others passed through.
Step 2: Check each option for correct logic and callback usage
const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (chunk.toString().toLowerCase().includes('skip')) { callback(); } else { this.push(chunk); callback(); } } }); correctly converts chunk to lowercase, checks for "skip", skips pushing if found, and calls callback in all cases. const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (chunk.includes('skip')) { this.push(chunk); } callback(); } }); pushes chunks containing "skip" (wrong). const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (!chunk.toString().includes('skip')) { this.push(chunk); } callback(null); } }); misses case insensitivity and calls callback with null (acceptable but less consistent). const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (chunk.toString().toLowerCase().indexOf('skip') === -1) { this.push(chunk); callback(); } } }); misses calling callback when chunk contains "skip" (callback not called).
Final Answer:
const filterSkip = new Transform({ transform(chunk, encoding, callback) { if (chunk.toString().toLowerCase().includes('skip')) { callback(); } else { this.push(chunk); callback(); } } }); -> Option A
Quick Check:
Skip chunks with 'skip' word, push others, always call callback = A [OK]
Hint: Call callback always; push only if chunk lacks 'skip' (case insensitive) [OK]