22

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?

2
  • Very accurate question, I am also looking for the same solution.
    – MechaCode
    Jun 18, 2020 at 5:01
  • Just had the same issue today, and it can't be done. The docs are rubbish on this and doesn't say anything about what the workerData will accept.
    – volume one
    Jan 31 at 0:37

3 Answers 3

4

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.

3

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.

0

I have the same issue. I solved it by creating new class instance in each trad, so that I can pass only primitive arguments which require my constructor

1
  • that's more useful to put a piece of code in your answer. Feb 12 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.