Skip to content

Commit af58299

Browse files
authored
Add warning about using this package to send arbitrary data to worker threads (#200)
* Add warning about using this for worker threads * Referenceify links
1 parent bb0048c commit af58299

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ Serialize JavaScript to a _superset_ of JSON that includes regular expressions,
1111

1212
The code in this package began its life as an internal module to [express-state][]. To expand its usefulness, it now lives as `serialize-javascript` — an independent package on npm.
1313

14-
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps**, **dates**, **sets** or **maps**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client. But this module is also great for communicating between node processes.
14+
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps**, **dates**, **sets** or **maps**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client.
1515

1616
The string returned from this package's single export function is literal JavaScript which can be saved to a `.js` file, or be embedded into an HTML document by making the content of a `<script>` element.
1717

1818
> **HTML characters and JavaScript line terminators are escaped automatically.**
1919
2020
Please note that serialization for ES6 Sets & Maps requires support for `Array.from` (not available in IE or Node < 0.12), or an `Array.from` polyfill.
2121

22+
> [!WARNING]
23+
> It may be tempting to use this package as a way to pass arbitrary functions into [worker threads][], since you cannot pass them directly via `postMessage()`. However, passing functions between worker threads is not possible in the general case. This package lets you serialize *some* functions, but it has limitations.
24+
>
25+
> For instance, if a function references something from outside the function body, it will not run properly if serialized and deserialized. This could include [closed-over variables][] or imports from other packages. For a serialized function to run properly, it must be entirely self-contained.
26+
>
27+
> In general, it is not possible to send arbitrary JavaScript to a worker thread, and pretend it's running the same way it would run on the main thread. This package doesn't let you do that.
28+
2229
## Installation
2330

2431
Install using npm:
@@ -141,3 +148,5 @@ See the [LICENSE file][LICENSE] for license text and copyright information.
141148
[express-state]: https://github.com/yahoo/express-state
142149
[JSON.stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
143150
[LICENSE]: https://github.com/yahoo/serialize-javascript/blob/main/LICENSE
151+
[worker threads]: https://nodejs.org/api/worker_threads.html
152+
[closed-over variables]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures

0 commit comments

Comments
 (0)