Add warning about using this package to send arbitrary data to worker threads #200
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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-terserusing 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, andserialize-javascriptjust doesn't do it.Using
serialize-javascriptto 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.