@holochain/client
Version:
A JavaScript client for the Holochain Conductor API
82 lines (81 loc) • 2.8 kB
TypeScript
import Emittery from "emittery";
import IsoWebSocket from "isomorphic-ws";
import { WsClientOptions } from "./common.js";
import { AppAuthenticationToken } from "./admin/index.js";
/**
* @public
*/
export interface AppAuthenticationRequest {
token: AppAuthenticationToken;
}
/**
* A WebSocket client which can make requests and receive responses,
* as well as send and receive signals.
*
* Uses Holochain's WireMessage for communication.
*
* @public
*/
export declare class WsClient extends Emittery {
socket: IsoWebSocket;
url: URL | undefined;
options: WsClientOptions | undefined;
private pendingRequests;
private index;
private authenticationToken;
private reconnectPromise;
constructor(socket: IsoWebSocket, url?: URL, options?: WsClientOptions);
/**
* Instance factory for creating WsClients.
*
* @param url - The WebSocket URL to connect to.
* @param options - Options for the WsClient.
* @returns An new instance of the WsClient.
*/
static connect(url: URL, options?: WsClientOptions): Promise<WsClient>;
/**
* Sends data as a signal.
*
* @param data - Data to send.
*/
emitSignal(data: unknown): void;
/**
* Authenticate the client with the conductor.
*
* This is only relevant for app websockets.
*
* @param request - The authentication request, containing an app authentication token.
*/
authenticate(request: AppAuthenticationRequest): Promise<void>;
/**
* Close the websocket connection.
*/
close(code?: number): Promise<IsoWebSocket.CloseEvent>;
/**
* Send requests to the connected websocket.
*
* If the underlying socket is closed when this method is called, the
* client transparently reconnects and re-authenticates using the cached
* token. Transient reconnect errors are surfaced as a `ConnectionError`
* and the cached token is retained, so a subsequent call can retry the
* reconnect.
*
* If the conductor rejects the cached token during the reconnect
* (signalled by an immediate close after the `authenticate` handshake),
* the cached token is cleared and the call rejects with an
* `InvalidTokenError`. The consumer must rebuild the `AppWebsocket`
* with a fresh token; further calls on this client will reject with
* `WebsocketClosedError`.
*
* @param request - The request to send over the websocket.
* @returns The decoded response payload.
*/
request<Response>(request: unknown): Promise<Response>;
private exchange;
private sendMessage;
private registerMessageListener;
private registerCloseListener;
private reconnectWebsocket;
private handleResponse;
}
export { IsoWebSocket };