UNPKG

@nktkas/hyperliquid

Version:

Hyperliquid API SDK for all major JS runtimes, written in TypeScript.

74 lines (73 loc) 2.67 kB
/** * WebSocket request dispatcher: sends `post`, `subscribe`, and `unsubscribe` * messages and matches responses by id. * * @module */ /// <amd-module name="file:///home/runner/work/hyperliquid/hyperliquid/src/transport/websocket/_dispatcher.ts" /> import { ReconnectingWebSocket } from "@nktkas/rews"; import { TransportError } from "../_base.js"; import type { HyperliquidEventTarget } from "./_events.js"; /** * Error thrown when a WebSocket request fails. * * @example * ```ts * import { WebSocketRequestError, WebSocketTransport } from "@nktkas/hyperliquid"; * * const transport = new WebSocketTransport(); * try { * // Throws on a server rejection, a timeout, an abort, or a lost connection. * await transport.request("info", { type: "allMids" }); * } catch (error) { * if (error instanceof WebSocketRequestError) { * console.error(error.message, error.request); * } * } * ``` */ export declare class WebSocketRequestError extends TransportError { /** The original request payload that triggered the error, if available. */ request?: unknown; /** * Creates a WebSocket request error. * * The failed request payload goes into `options.request`. */ constructor(message?: string, options?: ErrorOptions & { request?: unknown; }); } /** * Owns the WebSocket request queue and matches server responses back to * in-flight requests. */ export declare class WebSocketDispatcher { /** Timeout for requests in ms. Set to `null` to disable. */ timeout: number | null; private readonly _socket; private _lastId; private _queue; constructor(socket: ReconnectingWebSocket, hlEvents: HyperliquidEventTarget, timeout: number | null); /** * Sends a request and resolves with the matched server response. * * @param signal Cancels the request from the caller's side. */ request<T>(method: "post" | "subscribe" | "unsubscribe", payload: unknown, signal?: AbortSignal): Promise<T>; private _handleSubscriptionResponse; private _handlePostResponse; private _handleErrorEvent; /** Rejects `pending`, when found, with the server text as the message. */ private _reject; /** * Finds the pending request matching an echoed body. * * The server normalizes the echo — fields can be added to the payload and * unknown ones dropped — so the queue is searched by subset. Among several * subset matches the most specific pending wins: when one in-flight payload * is a subset of another, the looser one must not swallow the echo meant * for the stricter one. */ private _findByEcho; }