import { Observable, Subscription } from 'rxjs'; import type { MaybePromise, ReplicationOptions, ReplicationPullOptions, ReplicationPushOptions, RxError, RxReplicationHandler, RxStorageDefaultCheckpoint, RxTypeError, StringKeys } from '../../types/index.d.ts'; import { RxReplicationState } from '../replication/index.ts'; import { WebsocketMessageResponseType, WebsocketMessageType } from '../replication-websocket/index.ts'; export type WebRTCReplicationCheckpoint = RxStorageDefaultCheckpoint; export type WebRTCMessage = Omit & { method: StringKeys> | 'token'; }; export type WebRTCResponse = Omit; export type PeerWithMessage = { peer: PeerType; message: WebRTCMessage; }; export type PeerWithResponse = { peer: PeerType; response: WebRTCResponse; }; export type WebRTCConnectionHandler = { connect$: Observable; disconnect$: Observable; message$: Observable>; response$: Observable>; error$: Observable; send(peer: PeerType, message: WebRTCMessage | WebRTCResponse): Promise; close(): Promise; }; export type WebRTCConnectionHandlerCreator = ( opts: SyncOptionsWebRTC ) => Promise>; export type WebRTCSyncPushOptions = Omit< ReplicationPushOptions, 'handler' > & {}; export type WebRTCSyncPullOptions = Omit< ReplicationPullOptions, 'handler' | 'stream$' > & {}; export type SyncOptionsWebRTC = Omit< ReplicationOptions, 'pull' | 'push' | 'replicationIdentifier' | 'deletedField' | 'live' | 'autostart' | 'waitForLeadership' > & { /** * It will only replicate with other instances * that use the same topic. */ topic: string; connectionHandlerCreator: WebRTCConnectionHandlerCreator; /** * Run on new peers so that bad peers can be blocked. * If returns true, the peer is valid and it will replicate. * If returns false, it will drop the peer. */ isPeerValid?: (peer: PeerType) => MaybePromise; pull?: WebRTCSyncPullOptions; push?: WebRTCSyncPushOptions; }; export type RxWebRTCReplicationState = RxReplicationState; export type WebRTCPeerState = { peer: PeerType; // only exists when the peer was picked as master and the own client was picked as fork. replicationState?: RxWebRTCReplicationState; // clean this up when removing the peer subs: Subscription[]; };