Skip to content

feat: stream files direct-to-disk via OPFS Web Worker to eliminate memory limits (fixes #120, fixes #294) - #525

Open
BemreSTR wants to merge 1 commit into
schlagmichdoch:masterfrom
BemreSTR:feature/opfs-direct-to-disk-streaming
Open

BemreSTR wants to merge 1 commit into
schlagmichdoch:masterfrom
BemreSTR:feature/opfs-direct-to-disk-streaming

Conversation

@BemreSTR

Copy link
Copy Markdown

Summary

This PR resolves the long-standing memory limitation and tab crashes when receiving large files (especially on iOS / Safari), addressing #120 and #294.

Instead of storing incoming WebRTC chunks in browser RAM before assembling a Blob, this PR introduces direct-to-disk streaming via the Origin Private File System (OPFS) and FileSystemSyncAccessHandle in a dedicated Web Worker.

RAM usage remains practically zero regardless of file size (multi-gigabyte files can be received without memory pressure).


Why this approach over previous attempts?

A previous exploratory branch (sw_digester / PR #267) attempted a massive 30-file / 3,600+ line rewrite of the networking and UI stack, which stalled due to merge conflicts, global state bugs, and queue ordering issues.

In accordance with CONTRIBUTING.md ("Stability always comes first", "Radical simplicity"):

  • This PR touches only ~230 lines of code across 5 files.
  • Zero changes to the underlying WebRTC protocol or signalling server.
  • The entire existing user flow and UI remain completely unchanged.
  • Fully backward-compatible with 100% graceful fallback.

Key Changes

  1. Dedicated OPFS Worker (public/scripts/worker/file-digester-worker.js):

    • Uses navigator.storage.getDirectory() and createSyncAccessHandle() to write chunks directly to isolated OPFS storage on disk.
    • Incoming chunks from the WebRTC DataChannel are forwarded with zero-copy buffer transfer (postMessage(..., [chunk])).
    • Uses a sequential FIFO Promise chain to guarantee strict write ordering without race conditions.
  2. Modular File Digester with Graceful Fallback (public/scripts/network.js):

    • FileDigester detects OPFS support at startup via FileDigester.init().
    • When supported, it instantiates FileDigesterViaWorker.
    • If OPFS is unsupported (e.g. older browsers, non-secure contexts, or restricted private browsing modes), it automatically falls back to FileDigesterViaBuffer (the existing in-memory Blob mechanism).
  3. Lifting iOS Transfer Limits & Crash Protection (public/scripts/ui.js, network.js):

    • The artificial 200MB iOS limit (Peer._onFilesTransferRequest) is automatically bypassed when OPFS streaming is active.
    • iOS WebKit crashes when opening the native share sheet with files > 250MB. When window.iOS && totalSize > 250MB, PairDrop gracefully falls back to direct browser download instead of crashing navigator.share.
    • Preserves original filenames (originalName) when sharing or downloading.
  4. Lifecycle Management & Zero Disk Leaks:

    • OPFS temporary files are deleted immediately when:
      • The user closes/dismisses the ReceiveFileDialog.
      • The transfer is aborted or fails.
      • WebRTC disconnects or connection state fails.
      • The browser tab is closed/unloaded (pagehide).
  5. Service Worker Cache:

    • Added file-digester-worker.js to relativePathsToCache in public/service-worker.js for full offline PWA functionality.

Testing & Verification

Automated E2E tests were executed with Puppeteer across multiple headless browser instances:

  • Capability Detection: Verified FileDigester.isStreamingSupported correctly detects OPFS sync handle support.
  • Unit Integrity: Verified 2MB random binary chunks written through the worker match the original SHA-256 hash bit-for-bit.
  • Fallback Integrity: Verified fallback buffer mode produces exact SHA-256 match.
  • Full WebRTC P2P Transfer: Simulated end-to-end file transfer between two distinct browser peers over WebRTC; data chunks streamed directly to OPFS disk, producing a 100% bit-accurate SHA-256 match with 0 browser console errors.

Fixes #120, fixes #294.

- Implements FileDigesterViaWorker using Origin Private File System (OPFS) and FileSystemSyncAccessHandle in a dedicated Web Worker
- Chunks received over WebRTC data channels are streamed zero-copy to disk, keeping RAM consumption near zero regardless of file size
- Automatically falls back to FileDigesterViaBuffer (legacy in-memory Blob) when OPFS is unsupported (e.g. insecure contexts or older browsers)
- Lifts iOS 200MB transfer limit when OPFS streaming is active
- Falls back to standard browser downloads for files > 250MB on iOS to prevent WebKit share sheet jetsam crashes
- Handles complete file lifecycle: cleans up OPFS files on dialog dismissal, transfer cancellation, RTC disconnection, or tab close
- Fixes schlagmichdoch#120, fixes schlagmichdoch#294
Copilot AI balanced review requested due to automatic review settings October 16, 2026 12:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

dayowe added a commit to dayowe/PairDrop that referenced this pull request Nov 25, 2026
Protect each document's received files with a Web Lock. Reclaim abandoned transfer directories and discard incomplete batches without touching downloads in live tabs.

Check available storage at acceptance, acknowledge partitions after their writes finish, and report quota failures to both peers so they can retry. Cover cross-tab cleanup, low space, the memory fallback and a large download in browser tests.

Follow-up to the work by BemreSTR in schlagmichdoch#525.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants