I have the (strong) impression there's an "issue" in RED.util.cloneMessage. I've intentionally put those brackets around the word issue as most users won't get in touch with it ever, yet ...
Observation:
RED.util.cloneMessage wrongly changes weak arrays when cloning a message. In detail, weak ( == empty) array elements receive the value undefined during clone operation.
Analysis:
RED.util.cloneMessage forwards the message object to _.cloneDeep (lodash) to perform the clone operation. Surprisingly, there's evidence that _.cloneDeep wrongly applies undefined to empty elements:
import * as _ from 'lodash';
// create a weak array, just fill index 3:
let x = new Array(5);
x[3] = 42;
console.log("Original array:")
x.forEach( (xx, i) => console.log(`${i}: ${xx}`));
console.log("");
console.log("Array after _.cloneDeep:")
let y = _.cloneDeep(x);
y.forEach( (xx, i) => console.log(`${i}: ${xx}`));
console.log("");
console.log("Array after structuredClone:")
let z = structuredClone(x);
z.forEach( (xx, i) => console.log(`${i}: ${xx}`));
output:
There was a pull request (in 2023) to address this issue - yet, despite merged, it looks like those changes got lost due to whatever reason...
Possible fix:
As you can see, structuredClone clones the array correctly. Most probably, it should be mature enough to act as drop-in replacement for _.cloneDeep. It's a one-liner fix...
Any comments?
