let worker = cluster.fork();
let myMap = new Map();
myMap.set('s', {foo: 'bar'});
worker.send({
myMap: myMap
});
When the worker gets the message myMap becomes {}
When sending an array or object it works as expected
This is because send() calls JSON.stringify() on the Map. As a workaround, you can define a toJSON() method for your Map.
Example solution that should work:
class SerializableMap extends Map {
toJSON() {
const obj = Object.create(null);
for (const [key, value] of this) {
obj[key] = value;
}
return obj;
}
}
let worker = cluster.fork();
let myMap = new SerializableMap();
myMap.set('s', {foo: 'bar'});
worker.send({
myMap
});
Other than that @cjihrig covered everything already, so closing.
@TimothyGu are you okay with me reopening this? I鈥檇 like to see how far we can get with the ValueSerializer stuff once https://github.com/nodejs/node/pull/11048 lands. (I鈥檒l assign this issue to myself.)
@addaleax no problem at all. Just trying to cleaning up some old and inactive issues.
@addaleax Is this still something you intend to look at? Or should this be closed?
Yes, but it鈥檚 not something we can realistically do until V8 moves its API out of experimental status, I think (because this is about a non-experimental API of ours).
Is this something we still want to pursue? In that case I guess it makes sense to add the help wanted label (I am not sure if the API is still experimental though but I hope not)?
@addaleax @TimothyGu Could you give a quick status update on this? I'm not familiar enough with V8 to know where this is at. Thank you!
Still experimental. There's also the issue that JSON is the documented serialization protocol so there might be applications or libraries out there relying on quirks of JSON.parse() and JSON.stringify().
Might someone supply another status update on this one for those of us who don't follow V8 and wouldn't know where to look?
@nodejs/v8 ^^^
(tl;dr: When does ValueSerializer/ValueDeserializer become no longer experimental?)
ping @addaleax
Most helpful comment
@addaleax no problem at all. Just trying to cleaning up some old and inactive issues.