Node: worker.send stripping Map and replacing with {}

Created on 23 Jan 2017  路  12Comments  路  Source: nodejs/node

  • Version:v6.9.4
  • Platform:Darwin Daves-MacBook-Air-2.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
  • Subsystem:
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

child_process question

Most helpful comment

@addaleax no problem at all. Just trying to cleaning up some old and inactive issues.

All 12 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fanjunzhi picture fanjunzhi  路  3Comments

stevenvachon picture stevenvachon  路  3Comments

sandeepks1 picture sandeepks1  路  3Comments

Brekmister picture Brekmister  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments