YACS

@yacs/webshare

Resumable peer-to-peer file transfer over any reliable binary data channel.

Unlike the rest of YACS (Apache-2.0), this package is published under the MIT License.

The package never creates a peer: the host app owns one PeerJS peer and hands connections in, so transfers share it with messaging and calls. Sender and Receiver are EventTargets, so they work the same in browsers and Node.

Usage

import { Sender, Receiver, TransferRejected } from "@yacs/webshare";

const sender = new Sender(file, transferId, {
  authorize: async (metadata) => isValid(metadata),
});
peer.on("connection", (connection) => sender.accept(connection));

const receiver = new Receiver(async () => {
  const target = await lookupSender(transferId);
  if (!target) throw new TransferRejected("Invalid transfer");
  return openConnection(target);
}, transferId);

receiver.addEventListener("completed", ({ detail: file }) => receiver.download(file));

Protocol

Frames are yacs.v1.TransferFrame protobuf messages (packages/dto/proto/v1/transfer.proto).

  • Files are split into 16 kB chunks, indexed.
  • The receiver requests blocks of 64 chunks at a time and ignores anything out of order.
  • A dropped or stalled connection resumes from the next missing chunk instead of restarting.

Reconnection

The receiver reconnects with backoff (retryDelays) when:

  • the connection drops,
  • connect() throws, or
  • no frame arrives within stallTimeout.

It gives up after giveUpAfter without progress. Throwing TransferRejected from connect() cancels immediately without retrying.

Events

EventSender detailReceiver detail
connectedconnectionconnection
incoming—IFileMeta
progress{ file: File, bytes }{ file: IFileMeta, bytes }
transferratebytes per secondbytes per second
completedFilereceived File
errorErrorError
disconnectedno detailno detail
reconnect—no detail
cancelledno detailno detail

Development

yarn workspace @yacs/webshare test        # vitest over an in-memory connection pair
yarn workspace @yacs/webshare typecheck   # source and tests