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

3

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?

Consternation answered 28/11, 2018 at 11:40 Comment(2)
Very accurate question, I am also looking for the same solution.Fichu
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.Anarchism
D
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.

Dnieper answered 27/12, 2018 at 18:11 Comment(0)
W
3

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.

Wintertime answered 31/5, 2019 at 5:38 Comment(0)
D
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

Dipnoan answered 11/2 at 21:44 Comment(1)
that's more useful to put a piece of code in your answer.Signalman

© 2022 - 2024 — McMap. All rights reserved.