Reference
Compressed high-throughput OPFS path. Pair with Lesson 3.
| 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 |
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"
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
createWritable fails until close()
showSaveFilePicker + copy; cannot move OPFS → UVFS
// main
const w = new Worker("io.js");
w.postMessage({ cmd: "write", bytes });
// worker: open → sync I/O → close → postMessage reply