Improve clone stream handling by adding safety checks for writable and destroyed states

This commit is contained in:
Debadutta Panda 2025-02-08 11:13:04 +05:30
parent 7321cd20db
commit 9d811c7bce

View File

@ -119,8 +119,8 @@ module.exports = function(RED) {
// Function to handle 'data' event
function onData(chunk) {
// Continue pushing chunks into clone stream if it is writable.
if (!cloneStream.writable) {
// Safely call clone stream write
if (cloneStream.writable) {
cloneStream.write(chunk);
}
}
@ -128,12 +128,15 @@ module.exports = function(RED) {
// Function to handle 'end' or 'error' events
function onEnd(err) {
if (err) {
// If clone stream is already destroyed don't call destory function again
// Safely call clone stream destroy method
if (!cloneStream.destroyed) {
cloneStream.destroy(err);
}
} else {
cloneStream.end();
// Safely call clone stream end method
if(cloneStream.writable) {
cloneStream.end();
}
}
}