Binary Data
JSON IPC is text-only — base64 is fine for kilobytes but chokes on multi-megabyte files (encoding overhead, memory spikes, main-thread stalls). Tynd ships a second custom protocol dedicated to raw bytes.
The tynd-bin:// channel
Section titled “The tynd-bin:// channel”A native-host custom protocol tynd-bin://localhost/<api>/<method>?<query> carries raw bytes in the request body and response body:
- No base64.
- No JSON envelope.
ArrayBufferon arrival.
Current routes:
| Route | Method | In | Out |
|---|---|---|---|
fs/readBinary?path=... |
GET |
— | file bytes |
fs/writeBinary?path=...&createDirs=0|1 |
POST |
bytes | 204 |
compute/hash?algo=blake3|sha256|sha384|sha512&encoding=base64 |
POST |
bytes | UTF-8 digest |
Typical throughput is 5-10× faster than a base64-over-JSON round-trip on multi-MB payloads.
TS client wrappers
Section titled “TS client wrappers”You don’t hit the scheme directly — use the wrappers:
Install the required plugins:
tynd add fstynd add httpInstall the required plugin:
tynd add computeimport { os, path } from "@tynd/core/client";import { fs } from "@tynd/plugin-fs/client";import { compute } from "@tynd/plugin-compute/client";
// Paths inside the app's own data directory (the default fs scope).const dir = await os.appDataDir();
// Readconst bytes = await fs.readBinary(path.join(dir, "image.png")); // Uint8Array
// Writeawait fs.writeBinary(path.join(dir, "out", "copy.png"), bytes, { createDirs: true });
// Hashconst digest = await compute.hash(bytes, { algo: "sha256" }); // base64 stringSmall payloads stay on JSON IPC
Section titled “Small payloads stay on JSON IPC”Sub-MB or text-shaped calls stay on the JSON channel where it’s simpler:
fs.readText/fs.writeText— textcompute.randomBytes(32)— small, text protocol is fineterminal:dataevents — base64-encoded PTY chunks (small + streaming)
Backend RPC arguments and results also take Uint8Array / ArrayBuffer, encoded as base64 text (about 4/3 of the size, plus JSON escaping). Every JSON frame is capped at 8 MiB, which leaves about 5 MiB of binary data per call.
Use binary only when you have multi-MB payloads.
When to use fetch vs binary IPC for network I/O
Section titled “When to use fetch vs binary IPC for network I/O”For HTTP downloads, prefer http.download or fetch — they already stream and never round-trip through JSON:
import { os, path } from "@tynd/core/client";import { http } from "@tynd/plugin-http/client";
// Streams bytes straight to disk (inside the fs scope), emits progress eventsawait http.download("https://.../bigfile.zip", path.join(await os.appCacheDir(), "bigfile.zip"), { onProgress: ({ loaded, total }) => { console.log(total ? `${((loaded / total) * 100).toFixed(1)}%` : `${loaded}B`); },});
// Or read into memory as a Uint8Arrayconst { body: bytes } = await http.getBinary("https://.../image.png");http.getBinary / http.download use the Rust HTTP client (TLS, HTTP/1.1), bypassing the Bun fetch path.
Hashing large buffers
Section titled “Hashing large buffers”const bytes = await fs.readBinary(path.join(await os.appDataDir(), "video.mp4"));const digest = await compute.hash(bytes, { algo: "sha256" });For ~100 MB of video, compute.hash can outperform Bun’s crypto.createHash("sha256") because:
- The bytes travel via the zero-copy scheme, not base64.
- Rust computes the hash on a fresh thread, off the JS event loop.
Gotchas
Section titled “Gotchas”- Don’t pass large
ArrayBuffers across the RPC channel. A regular RPC call encodes bytes as text inside the JSON frame; a 50 MB buffer means a large encode, copy and decode on every hop. Usefs.readBinary/compute.hashor a handle-based pattern instead. - No transferable objects — the native host’s IPC bridge doesn’t have
Transferablesemantics. You always receive a fresh ArrayBuffer, the original is still yours. - If a fetch-based helper reads a body twice, use
Response.clone()or consume it once and stash the bytes.
Under the hood
Section titled “Under the hood”The native host implements the binary scheme. The TS client wraps it through the WebView’s native fetch, which handles tynd-bin:// transparently:
// Simplified version of what fs.readBinary doesconst res = await fetch(`tynd-bin://localhost/fs/readBinary?path=${encodeURIComponent(path)}`);const buf = await res.arrayBuffer();return new Uint8Array(buf);Custom APIs could add new routes in the Rust host, but the scheme is currently closed — only fs.readBinary, fs.writeBinary, compute.hash are registered.