Global shared data between different instances of a Node.js native addon?

158 views Asked by At

I'm looking to create a native addon with Node-API (more specifically with the C++ wrapper module node-addon-api).

I need to have global read-only data that will be loaded once and will be shared between different instances of the addon (e.g. when the addon is loaded by different node.js workers).

Is there a recommended way to do this? Would it be safe to just store the data in a static variable?

1

There are 1 answers

0
Criminal_Affair_At_SO On BEST ANSWER

It depends on the type of your data. If your data does not involve any V8 data types (or Napi:: types), then, yes, it is generally safe.

Just be careful to not shared a variable that has been initialized with Node::Env - this is the rule to follow - if it needs Node::Env then it can't be shared.