nodejs - DataCloneError: function () { [native code] } could not be cloned

I'm trying to spawn threads (using the new Nodejs module 'worker_threads') and pass to each of them a complex object which is the 'page' object of a Puppeteer browser.newPage() instance. I tried both using workerData and MessageChannels - which, from docs:

port.postMessage: Sends a JavaScript value to the receiving side of this channel. value will be transferred in a way which is compatible with the HTML structured clone algorithm. In particular, it may contain circular references and objects like typed arrays that the JSON API is not able to stringify.

but I always get the error:

(node:7133) UnhandledPromiseRejectionWarning: DataCloneError: function () { [native code] } could not be cloned. at Worker.postMessage (internal/worker.js:378:23)

I also tried to stringify it and parse it but the page object also contains functions which I couldn't get to evaluate from the threads context (I also would like to avoid using eval()).

The question is: how do I pass a complex object like Puppeteer browser.newPage() instance to the threads spawned with worker_threads in Nodejs?


dethSwathch is right about that: You can't. Message passing is only allowed for some few native data types, including Object (and Date and of course TypedArrays). That is why you lose all type information and functions. However, you can certainly use the "naked" object to just fill a new Page instance (or any other custom object).

This DataCloneError seems to be a limitation of the past. At least I couldn't reproduce it. The object should be copied automatically without any serialization.


Believe the answer is basically 'nope'.

Can you change what you're passing such that you can 'reconstitute' it on the other side?

The model is akin to making a call to another server- you have to encode everything you need and then reform it on the other side.