import { Observable, Subscription } from 'rxjs'; import type { MaybePromise, ReplicationOptions, ReplicationPullOptions, ReplicationPushOptions, RxError, RxReplicationHandler, RxStorageDefaultCheckpoint, RxTypeError, StringKeys } from '../../types/index.js'; import { RxReplicationState } from '../replication/index.js'; import { WebsocketMessageResponseType, WebsocketMessageType } from '../replication-websocket/index.js'; 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, 'handler'> & {}; export type WebRTCSyncPullOptions = Omit, 'handler' | 'stream$'> & {}; export type SyncOptionsWebRTC = Omit, '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; replicationState?: RxWebRTCReplicationState; subs: Subscription[]; };