Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/earthstar/src/entries/npm.ts>Peer

Storage for private, distributed, offline-first applications.
Go to Latest
class Peer
implements IPeer
Re-export
import { Peer } from "https://deno.land/x/earthstar@v10.2.0/src/entries/npm.ts";

Holds many shares' replicas and manages their synchronisation with other peers. Recommended as the point of contact between your application and Earthstar shares.

const peer = new Peer();
peer.addReplica(myReplica);
peer.sync("https://my.server");

Constructors

new
Peer()

Properties

private
replicaEventBus: BlockingBus<Map<ShareAddress, Replica>>
private
syncerManager: SyncerManager
replicaMap: Map<ShareAddress, Replica>

A map of the replicas stored in this peer.

Methods

addReplica(replica: Replica): Promise<void>
addSyncPartner<I, F>(
partner: ISyncPartner<I>,
description: string,
formats?: FormatsArg<F>,
)

Begin syncing using an instance implementing ISyncPartner. Use this if you don't want to sync with a local peer or a server.

discover(service: DiscoveryService): AsyncIterable<DiscoveryEvent>

Discover other Earthstar peers using a discovery service, for example other peers on the local network.

Returns an asynchronous iterable of DiscoveryEvent, which describe finding a Peer, another peer initiating sync with you, etc. Some of these events have callbacks which let you initiate sync with the discovered peer.

getReplica(ws: ShareAddress): Replica | undefined

Get all syncers created by this peer. This includes running and cancelled syncers.

hasShare(share: ShareAddress): boolean
onReplicasChange(callback: (map: Map<ShareAddress, Replica>) => void | Promise<void>)

Fires a given callback whenever the Peer's store of replicas changes.

onSyncersChange(callback: (map: Map<string, { description: string; syncer: Syncer<unknown, unknown>; }>) => void | Promise<void>): () => void

Fires a given callback whenever a new syncer is added to the peer.

removeReplica(replica: Replica): Promise<void>
removeReplicaByShare(share: ShareAddress): Promise<void>

Remove a replica from the peer using its share address.

Returns an array of all the addresses of this peer's held replicas.

size(): number

The number of replicas held by this peer

sync<F>(
target: IPeer | string,
continuous?,
formats?: FormatsArg<F>,
): Syncer<undefined, F>

Begin synchronising with a remote or local peer.