Skip to content

Conversation

@valadaptive
Copy link
Contributor

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.


I've discovered that many JavaScript packages use this package to try and send arbitrary data containing functions to worker threads, basically using it as a version of postMessage() that doesn't throw if there's a function in there somewhere. In particular, it seems to be used by developer tooling that wants to "transparently" start running things in parallel, passing a user-supplied configuration object into each worker thread. Here's @rollup/plugin-terser using it, and here it is in Mocha, a popular testing framework.

However, there's a very good reason for postMessage() to throw an error when encountering a function. Functions can, in general, reference data defined outside the function. Serializing that data would be quite difficult, and serialize-javascript just doesn't do it.

Using serialize-javascript to send arbitrary data to worker threads may work in simpler cases, but it fails as soon as the functions reference things like imported modules. I think we should make it clear that it's not magic, and doesn't let you treat worker threads the same way as the main thread. As such, this PR adds a disclaimer pretty high up in the readme.

@valadaptive valadaptive requested a review from okuryu November 26, 2025 16:21
@okuryu okuryu merged commit af58299 into yahoo:main Nov 27, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants