NodeJS Program to PassThrough Logger with Explanation
NodeJS
Medium
Streams & Buffers
31 views
1 min read
81 words
This problem helps you practice core NodeJS fundamentals in a practical way. It builds intuition around byte, passthrough, count. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Use PassThrough to tap into a stream and count bytes.
Input Format
stdin: any text.
Output Format
Print integer bytes.
Constraints
Count bytes flowing through.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
const { PassThrough } = require('stream');
let bytes = 0;
const tap = new PassThrough();
tap.on('data', (c) => { bytes += c.length; });
process.stdin.pipe(tap).resume();
process.stdin.on('end', () => console.log(bytes));
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Use PassThrough to tap into a stream and count bytes.
Input / Output
Output
Print integer bytes.
Constraints
Count bytes flowing through.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
const { PassThrough } = require('stream');
let bytes = 0;
const tap = new PassThrough();
tap.on('data', (c) => { bytes += c.length; });
process.stdin.pipe(tap).resume();
process.stdin.on('end', () => console.log(bytes));
Solutions (0)
No solutions submitted yet. Be the first!