Skip to content

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.

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.
  • ArrayBuffer on 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.

You don’t hit the scheme directly — use the wrappers:

Install the required plugins:

Terminal window
tynd add fs
tynd add http

Install the required plugin:

Terminal window
tynd add compute
import { 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();
// Read
const bytes = await fs.readBinary(path.join(dir, "image.png")); // Uint8Array
// Write
await fs.writeBinary(path.join(dir, "out", "copy.png"), bytes, { createDirs: true });
// Hash
const digest = await compute.hash(bytes, { algo: "sha256" }); // base64 string

Sub-MB or text-shaped calls stay on the JSON channel where it’s simpler:

  • fs.readText / fs.writeText — text
  • compute.randomBytes(32) — small, text protocol is fine
  • terminal:data events — 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 events
await 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 Uint8Array
const { 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.

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.
  • 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. Use fs.readBinary / compute.hash or a handle-based pattern instead.
  • No transferable objects — the native host’s IPC bridge doesn’t have Transferable semantics. 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.

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 does
const 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.