Added qhelp for UnhandledStreamPipe query

This commit is contained in:
Napalys Klicius
2025-05-23 13:35:36 +02:00
parent c6db32ed73
commit 248f83c4db
4 changed files with 83 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
const fs = require('fs');
const source = fs.createReadStream('source.txt');
const destination = fs.createWriteStream('destination.txt');
// Bad: No error handling
source.pipe(destination);

View File

@@ -0,0 +1,17 @@
const { pipeline } = require('stream');
const fs = require('fs');
const source = fs.createReadStream('source.txt');
const destination = fs.createWriteStream('destination.txt');
// Good: Using pipeline for automatic error handling
pipeline(
source,
destination,
(err) => {
if (err) {
console.error('Pipeline failed:', err);
} else {
console.log('Pipeline succeeded');
}
}
);

View File

@@ -0,0 +1,16 @@
const fs = require('fs');
const source = fs.createReadStream('source.txt');
const destination = fs.createWriteStream('destination.txt');
// Alternative Good: Manual error handling with pipe()
source.on('error', (err) => {
console.error('Source stream error:', err);
destination.destroy(err);
});
destination.on('error', (err) => {
console.error('Destination stream error:', err);
source.destroy(err);
});
source.pipe(destination);