js-smp-peer
Version:
<p align="center"> <a href="https://github.com/mhchia/js-smp-peer/actions?workflow=nodejs-test"><img alt="GitHub Actions status" src="https://github.com/mhchia/js-smp-peer/workflows/nodejs-test/badge.svg"></a> </p>
81 lines (80 loc) • 3.32 kB
TypeScript
import { TPeerServerConfig } from './config';
declare const eventServerConnected = "connected";
declare const eventServerDisconnected = "disconnected";
declare const eventError = "error";
declare const eventIncomingSMP = "incoming";
declare type TCBServerConnected = () => void;
declare type TCBServerDisconnected = () => void;
declare type TCBError = (error: string) => void;
declare type TCBIncomingSMP = (remotePeerID: string, result: boolean) => void;
declare class SMPPeer {
readonly localPeerID?: string | undefined;
readonly peerServerConfig: TPeerServerConfig;
readonly timeout: number;
secret: string;
cbServerConnected?: TCBServerConnected;
cbServerDisconnected?: TCBServerDisconnected;
cbError?: TCBError;
cbIncomingSMP?: TCBIncomingSMP;
private peer?;
/**
* @param secret - The secret which will be used to run SMP protocol with the remote peer
* @param localPeerID - Our peer id. We will "register" our peer id on the peer server later
* when calling `connectToPeerServer`.
* @param peerServerConfig - The information of the peer server. `defaultPeerServerConfig` is
* used if this parameter is not supplied.
*/
constructor(secret: string, localPeerID?: string | undefined, peerServerConfig?: TPeerServerConfig, timeout?: number);
/**
* @returns Our peer id.
* @throws `ServerUnconnected` if `id` is called when `SMPPeer` is not connected to the peer
* server.
*/
get id(): string;
/**
* Connect to the peer server with the infromation in `this.peerServerConfig`. A peer server
* allows us to discover peers and also others to find us. `connectToPeerServer` asynchronously
* waits until the connection to the peer server is established.
* @throws `ServerFault` when the peer id we sent mismatches the one returned from the peer
* server.
*/
connectToPeerServer(): Promise<void>;
/**
* Run SMP protocol with a peer. Connecting with a peer server is required before calling
* `runSMP`.
* @param remotePeerID - The id of the peer.
* @throws `ServerUnconnected` when `runSMP` is called without connecting to a peer server.
* @returns The result of SMP protocol, i.e. our secret is the same as the secret of the
* remote peer.
*/
runSMP(remotePeerID: string): Promise<boolean>;
/**
* Disconnect from the peer server.
*/
disconnect(): void;
/**
* Emitted when connected to the peer server.
* @param event - Event name
* @param cb - Callback function
*/
on(event: typeof eventServerConnected, cb: TCBServerConnected): void;
/**
* Emitted when the connection to the peer server is closed.
* @param event - Event name
* @param cb - Callback function
*/
on(event: typeof eventServerDisconnected, cb: TCBServerDisconnected): void;
/**
* Emitted when an error occurs in networking.
* @param event - Event name
* @param cb - Callback function
*/
on(event: typeof eventError, cb: TCBError): void;
/**
* Emitted when an incoming SMP request is finished.
* @param event - Event name
* @param cb - Callback function
*/
on(event: typeof eventIncomingSMP, cb: TCBIncomingSMP): void;
}
export default SMPPeer;