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?
workerData
will accept.