Reference

Workers & sync access

Compressed high-throughput OPFS path. Pair with Lesson 3.

When which path

Need Path
Ordinary text/blob create-read-write on main or worker createWritable / getFile (async)
High-throughput random access, Wasm/DB engines Dedicated worker + createSyncAccessHandle()
User-visible open/save Pickers on main thread — not OPFS, not sync handles

Open (dedicated worker)

const root = await navigator.storage.getDirectory();
const file = await root.getFileHandle("fast.bin", { create: true });
const access = await file.createSyncAccessHandle();
// modes: "readwrite" (default, exclusive), "read-only", "readwrite-unsafe"

Sync methods

access.getSize()
access.read(buffer, { at: 0 })
access.write(buffer, { at: offset })  // check returned byte count
access.truncate(newSize)
access.flush()
access.close()  // always — releases lock

Constraints

Main ↔ worker shape

// main
const w = new Worker("io.js");
w.postMessage({ cmd: "write", bytes });
// worker: open → sync I/O → close → postMessage reply