diff options
author | 2023-07-16 23:16:54 -0700 | |
---|---|---|
committer | 2023-07-16 23:16:54 -0700 | |
commit | 75213aad37b4d5989be9bf06208573e1fe0c186a (patch) | |
tree | 390a837c5cd2b128fefa92be39b2061143041f71 | |
parent | 58566398333da7679574edff5c0fbf682134c6d8 (diff) | |
download | bun-75213aad37b4d5989be9bf06208573e1fe0c186a.tar.gz bun-75213aad37b4d5989be9bf06208573e1fe0c186a.tar.zst bun-75213aad37b4d5989be9bf06208573e1fe0c186a.zip |
Document serialize/deserialize
-rw-r--r-- | docs/api/utils.md | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/docs/api/utils.md b/docs/api/utils.md index cf76769eb..24cb94ed2 100644 --- a/docs/api/utils.md +++ b/docs/api/utils.md @@ -486,3 +486,17 @@ To resolve relative to the directory containing the current file, pass `import.m ```ts Bun.resolveSync("./foo.ts", import.meta.dir); ``` + +## `serialize` & `deserialize` in `bun:jsc` + +To save a JavaScript value into an ArrayBuffer & back, use `serialize` and `deserialize` from the `"bun:jsc"` module. + +```js +import { serialize, deserialize } from "bun:jsc"; + +const buf = serialize({ foo: "bar" }); +const obj = deserialize(buf); +console.log(obj); // => { foo: "bar" } +``` + +Internally, [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) and [`postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) serialize and deserialize the same way. This exposes the underlying [HTML Structured Clone Algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) to JavaScript as an ArrayBuffer. |