From 75213aad37b4d5989be9bf06208573e1fe0c186a Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sun, 16 Jul 2023 23:16:54 -0700 Subject: Document serialize/deserialize --- docs/api/utils.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'docs/api/utils.md') 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. -- cgit v1.2.3