@nktkas/hyperliquid 0.13.1 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/README.md +334 -107
- package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.js +2 -0
- package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
- package/esm/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.js +1 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.js +2 -0
- package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
- package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.js +1 -1
- package/{script/deps/jsr.io/@std/encoding/1.0.6 → esm/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/hex.js +1 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +2 -0
- package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.js +2 -2
- package/esm/mod.d.ts +4 -12
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +2 -2
- package/{script/src/transports → esm/src}/base.d.ts +10 -13
- package/esm/src/base.d.ts.map +1 -0
- package/esm/src/base.js +14 -0
- package/esm/src/clients/event.d.ts +59 -18
- package/esm/src/clients/event.d.ts.map +1 -1
- package/esm/src/clients/event.js +74 -18
- package/esm/src/clients/public.d.ts +348 -53
- package/esm/src/clients/public.d.ts.map +1 -1
- package/esm/src/clients/public.js +349 -50
- package/esm/src/clients/wallet.d.ts +345 -73
- package/esm/src/clients/wallet.d.ts.map +1 -1
- package/esm/src/clients/wallet.js +979 -229
- package/esm/src/signing.d.ts +135 -0
- package/esm/src/signing.d.ts.map +1 -0
- package/esm/src/signing.js +188 -0
- package/esm/src/transports/http/http_transport.d.ts +1 -1
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +1 -1
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +64 -0
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
- package/esm/src/transports/websocket/_hyperliquid_event_target.js +52 -0
- package/esm/src/transports/websocket/{reconnecting_websocket.d.ts → _reconnecting_websocket.d.ts} +26 -26
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -0
- package/esm/src/transports/websocket/{reconnecting_websocket.js → _reconnecting_websocket.js} +91 -76
- package/{script/src/transports/websocket/websocket_request_dispatcher.d.ts → esm/src/transports/websocket/_websocket_request_dispatcher.d.ts} +3 -7
- package/esm/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +1 -0
- package/esm/src/transports/websocket/{websocket_request_dispatcher.js → _websocket_request_dispatcher.js} +6 -10
- package/esm/src/transports/websocket/websocket_transport.d.ts +15 -21
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +55 -52
- package/esm/src/types/exchange/requests.d.ts +298 -79
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/exchange/responses.d.ts +38 -38
- package/esm/src/types/exchange/responses.d.ts.map +1 -1
- package/esm/src/types/explorer/requests.d.ts +23 -10
- package/esm/src/types/explorer/requests.d.ts.map +1 -1
- package/esm/src/types/explorer/responses.d.ts +45 -3
- package/esm/src/types/explorer/responses.d.ts.map +1 -1
- package/esm/src/types/info/accounts.d.ts +279 -189
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +191 -185
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/delegations.d.ts +117 -0
- package/esm/src/types/info/delegations.d.ts.map +1 -0
- package/esm/src/types/info/orders.d.ts +94 -94
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +192 -64
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/info/vaults.d.ts +42 -85
- package/esm/src/types/info/vaults.d.ts.map +1 -1
- package/esm/src/types/mod.d.ts +14 -0
- package/esm/src/types/mod.d.ts.map +1 -0
- package/esm/src/types/subscriptions/requests.d.ts +11 -1
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/{script/src/types/subscriptions/common.d.ts → esm/src/types/subscriptions/responses.d.ts} +18 -3
- package/esm/src/types/subscriptions/responses.d.ts.map +1 -0
- package/package.json +20 -4
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
- package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
- package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
- package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
- package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
- package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
- package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
- package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
- package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
- package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
- package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
- package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
- package/script/mod.d.ts +4 -12
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +21 -13
- package/{esm/src/transports → script/src}/base.d.ts +10 -13
- package/script/src/base.d.ts.map +1 -0
- package/script/src/base.js +29 -0
- package/script/src/clients/event.d.ts +59 -18
- package/script/src/clients/event.d.ts.map +1 -1
- package/script/src/clients/event.js +551 -485
- package/script/src/clients/public.d.ts +348 -53
- package/script/src/clients/public.d.ts.map +1 -1
- package/script/src/clients/public.js +1017 -708
- package/script/src/clients/wallet.d.ts +345 -73
- package/script/src/clients/wallet.d.ts.map +1 -1
- package/script/src/clients/wallet.js +1737 -977
- package/script/src/signing.d.ts +135 -0
- package/script/src/signing.d.ts.map +1 -0
- package/script/src/signing.js +203 -0
- package/script/src/transports/http/http_transport.d.ts +1 -1
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +174 -164
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +64 -0
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
- package/script/src/transports/websocket/_hyperliquid_event_target.js +66 -0
- package/script/src/transports/websocket/{reconnecting_websocket.d.ts → _reconnecting_websocket.d.ts} +26 -26
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -0
- package/script/src/transports/websocket/_reconnecting_websocket.js +400 -0
- package/{esm/src/transports/websocket/websocket_request_dispatcher.d.ts → script/src/transports/websocket/_websocket_request_dispatcher.d.ts} +3 -7
- package/script/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +1 -0
- package/script/src/transports/websocket/_websocket_request_dispatcher.js +212 -0
- package/script/src/transports/websocket/websocket_transport.d.ts +15 -21
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +235 -222
- package/script/src/types/exchange/requests.d.ts +298 -79
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/exchange/requests.js +12 -2
- package/script/src/types/exchange/responses.d.ts +38 -38
- package/script/src/types/exchange/responses.d.ts.map +1 -1
- package/script/src/types/exchange/responses.js +12 -2
- package/script/src/types/explorer/requests.d.ts +23 -10
- package/script/src/types/explorer/requests.d.ts.map +1 -1
- package/script/src/types/explorer/requests.js +12 -2
- package/script/src/types/explorer/responses.d.ts +45 -3
- package/script/src/types/explorer/responses.d.ts.map +1 -1
- package/script/src/types/explorer/responses.js +12 -2
- package/script/src/types/info/accounts.d.ts +279 -189
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/accounts.js +12 -2
- package/script/src/types/info/assets.d.ts +191 -185
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/assets.js +12 -2
- package/script/src/types/info/delegations.d.ts +117 -0
- package/script/src/types/info/delegations.d.ts.map +1 -0
- package/script/src/types/info/delegations.js +12 -0
- package/script/src/types/info/orders.d.ts +94 -94
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/orders.js +12 -2
- package/script/src/types/info/requests.d.ts +192 -64
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/requests.js +12 -2
- package/script/src/types/info/vaults.d.ts +42 -85
- package/script/src/types/info/vaults.d.ts.map +1 -1
- package/script/src/types/info/vaults.js +12 -2
- package/script/src/types/mod.d.ts +14 -0
- package/script/src/types/mod.d.ts.map +1 -0
- package/script/src/types/mod.js +12 -0
- package/script/src/types/subscriptions/requests.d.ts +11 -1
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/script/src/types/subscriptions/requests.js +12 -2
- package/{esm/src/types/subscriptions/common.d.ts → script/src/types/subscriptions/responses.d.ts} +18 -3
- package/script/src/types/subscriptions/responses.d.ts.map +1 -0
- package/script/src/types/subscriptions/responses.js +12 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
- package/esm/src/transports/base.d.ts.map +0 -1
- package/esm/src/transports/base.js +0 -14
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +0 -66
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
- package/esm/src/transports/websocket/hyperliquid_event_target.js +0 -33
- package/esm/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
- package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
- package/esm/src/types/common.d.ts +0 -3
- package/esm/src/types/common.d.ts.map +0 -1
- package/esm/src/types/exchange/common.d.ts +0 -36
- package/esm/src/types/exchange/common.d.ts.map +0 -1
- package/esm/src/types/explorer/common.d.ts +0 -37
- package/esm/src/types/explorer/common.d.ts.map +0 -1
- package/esm/src/types/subscriptions/common.d.ts.map +0 -1
- package/esm/src/types/subscriptions/common.js +0 -1
- package/esm/src/utils/key_sort.d.ts +0 -21
- package/esm/src/utils/key_sort.d.ts.map +0 -1
- package/esm/src/utils/key_sort.js +0 -124
- package/esm/src/utils/signing.d.ts +0 -109
- package/esm/src/utils/signing.d.ts.map +0 -1
- package/esm/src/utils/signing.js +0 -164
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
- package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
- package/script/src/transports/base.d.ts.map +0 -1
- package/script/src/transports/base.js +0 -18
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts +0 -66
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
- package/script/src/transports/websocket/hyperliquid_event_target.js +0 -37
- package/script/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
- package/script/src/transports/websocket/reconnecting_websocket.js +0 -374
- package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
- package/script/src/transports/websocket/websocket_request_dispatcher.js +0 -206
- package/script/src/types/common.d.ts +0 -3
- package/script/src/types/common.d.ts.map +0 -1
- package/script/src/types/common.js +0 -2
- package/script/src/types/exchange/common.d.ts +0 -36
- package/script/src/types/exchange/common.d.ts.map +0 -1
- package/script/src/types/exchange/common.js +0 -2
- package/script/src/types/explorer/common.d.ts +0 -37
- package/script/src/types/explorer/common.d.ts.map +0 -1
- package/script/src/types/explorer/common.js +0 -2
- package/script/src/types/subscriptions/common.d.ts.map +0 -1
- package/script/src/types/subscriptions/common.js +0 -2
- package/script/src/utils/key_sort.d.ts +0 -21
- package/script/src/utils/key_sort.d.ts.map +0 -1
- package/script/src/utils/key_sort.js +0 -127
- package/script/src/utils/signing.d.ts +0 -109
- package/script/src/utils/signing.d.ts.map +0 -1
- package/script/src/utils/signing.js +0 -172
- /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
- /package/esm/src/types/{common.js → info/delegations.js} +0 -0
- /package/esm/src/types/{exchange/common.js → mod.js} +0 -0
- /package/esm/src/types/{explorer/common.js → subscriptions/responses.js} +0 -0
- /package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { ReconnectingWebSocket, type ReconnectingWebSocketOptions } from "./
|
|
2
|
-
import { HyperliquidEventTarget } from "./
|
|
3
|
-
import { WebSocketRequestDispatcher } from "./
|
|
4
|
-
import type { IRequestTransport, ISubscriptionTransport, Subscription } from "
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*/
|
|
1
|
+
import { ReconnectingWebSocket, type ReconnectingWebSocketOptions } from "./_reconnecting_websocket.js";
|
|
2
|
+
import { HyperliquidEventTarget } from "./_hyperliquid_event_target.js";
|
|
3
|
+
import { WebSocketRequestDispatcher, WebSocketRequestError } from "./_websocket_request_dispatcher.js";
|
|
4
|
+
import type { IRequestTransport, ISubscriptionTransport, Subscription } from "../../base.js";
|
|
5
|
+
export { WebSocketRequestError };
|
|
6
|
+
export type { MessageBufferStrategy, ReconnectingWebSocketOptions } from "./_reconnecting_websocket.js";
|
|
7
|
+
/** Configuration options for the WebSocket transport layer. */
|
|
8
8
|
export interface WebSocketTransportOptions {
|
|
9
9
|
/**
|
|
10
10
|
* The WebSocket URL.
|
|
@@ -36,24 +36,22 @@ export interface WebSocketTransportOptions {
|
|
|
36
36
|
*/
|
|
37
37
|
reconnect?: ReconnectingWebSocketOptions;
|
|
38
38
|
}
|
|
39
|
-
/**
|
|
40
|
-
* WebSocket implementation of the REST and Subscription transport interfaces.
|
|
41
|
-
*/
|
|
39
|
+
/** WebSocket implementation of the REST and Subscription transport interfaces. */
|
|
42
40
|
export declare class WebSocketTransport implements IRequestTransport, ISubscriptionTransport {
|
|
43
41
|
/** The interval timer ID for keep-alive messages. */
|
|
44
|
-
protected _keepAliveTimer: number |
|
|
42
|
+
protected _keepAliveTimer: number | null;
|
|
45
43
|
/** The WebSocket request dispatcher instance. */
|
|
46
44
|
protected _wsRequester: WebSocketRequestDispatcher;
|
|
47
45
|
/** The Hyperliquid event target instance. */
|
|
48
46
|
protected _hlEvents: HyperliquidEventTarget;
|
|
49
47
|
/**
|
|
50
48
|
* Map of active subscriptions.
|
|
51
|
-
* - Key: Unique subscription identifier
|
|
52
|
-
* - Value: Subscription info containing
|
|
49
|
+
* - Key: Unique subscription identifier based on payload
|
|
50
|
+
* - Value: Subscription info containing the subscription request promise
|
|
51
|
+
* and a map of listeners to their metadata (channel + unsubscribe function).
|
|
53
52
|
*/
|
|
54
53
|
protected _subscriptions: Map<string, {
|
|
55
|
-
|
|
56
|
-
listeners: Map<(data: CustomEvent) => void, () => Promise<void>>;
|
|
54
|
+
listeners: Map<(data: CustomEvent) => void, (signal?: AbortSignal) => Promise<void>>;
|
|
57
55
|
requestPromise: Promise<unknown>;
|
|
58
56
|
}>;
|
|
59
57
|
/**
|
|
@@ -107,11 +105,7 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
|
|
|
107
105
|
* @returns A promise that resolves when the connection is fully closed.
|
|
108
106
|
*/
|
|
109
107
|
close(signal?: AbortSignal): Promise<void>;
|
|
110
|
-
/**
|
|
111
|
-
|
|
112
|
-
* @param signal - A user-provided signal.
|
|
113
|
-
* @returns An AbortSignal that triggers when either the timeout or user-provided signal aborts.
|
|
114
|
-
*/
|
|
115
|
-
protected combineSignals(signal?: AbortSignal): AbortSignal;
|
|
108
|
+
/** Combines a timeout with an optional abort signal. */
|
|
109
|
+
protected _combineTimeoutWithSignal(timeout?: number | null, signal?: AbortSignal): AbortSignal | undefined;
|
|
116
110
|
}
|
|
117
111
|
//# sourceMappingURL=websocket_transport.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/websocket_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,KAAK,4BAA4B,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"websocket_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/websocket_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,KAAK,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AACxG,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,0BAA0B,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AACvG,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7F,OAAO,EAAE,qBAAqB,EAAE,CAAC;AACjC,YAAY,EAAE,qBAAqB,EAAE,4BAA4B,EAAE,MAAM,8BAA8B,CAAC;AAExG,+DAA+D;AAC/D,MAAM,WAAW,yBAAyB;IACtC;;;;;OAKG;IACH,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IAEnB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAExB;;;OAGG;IACH,SAAS,CAAC,EAAE;QACR;;;;WAIG;QACH,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KAC5B,CAAC;IAEF;;OAEG;IACH,SAAS,CAAC,EAAE,4BAA4B,CAAC;CAC5C;AAED,kFAAkF;AAClF,qBAAa,kBAAmB,YAAW,iBAAiB,EAAE,sBAAsB;IAChF,qDAAqD;IACrD,SAAS,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAQ;IAEhD,iDAAiD;IACjD,SAAS,CAAC,YAAY,EAAE,0BAA0B,CAAC;IAEnD,6CAA6C;IAC7C,SAAS,CAAC,SAAS,EAAE,sBAAsB,CAAC;IAE5C;;;;;OAKG;IACH,SAAS,CAAC,cAAc,EAAE,GAAG,CACzB,MAAM,EACN;QACI,SAAS,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,WAAW,KAAK,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QACrF,cAAc,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;KACpC,CACJ,CAAa;IAEd;;;OAGG;IACH,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAEvB,yCAAyC;IACzC,QAAQ,CAAC,SAAS,EAAE;QAChB;;;WAGG;QACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;KACpC,CAAC;IAEF,oDAAoD;IACpD,QAAQ,CAAC,MAAM,EAAE,qBAAqB,CAAC;IAEvC;;;OAGG;gBACS,OAAO,CAAC,EAAE,yBAAyB;IAuC/C;;;;;;;;OAQG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC;IAavG;;;;;;;OAOG;IACG,SAAS,CACX,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,CAAC,IAAI,EAAE,WAAW,KAAK,IAAI,EACrC,MAAM,CAAC,EAAE,WAAW,GACrB,OAAO,CAAC,YAAY,CAAC;IAgExB;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAuB1C;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAqB1C,wDAAwD;IACxD,SAAS,CAAC,yBAAyB,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,WAAW,GAAG,SAAS;CAK9G"}
|
|
@@ -1,237 +1,250 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true,
|
|
21
|
-
value: void 0
|
|
22
|
-
});
|
|
23
|
-
/** The WebSocket request dispatcher instance. */
|
|
24
|
-
Object.defineProperty(this, "_wsRequester", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
configurable: true,
|
|
27
|
-
writable: true,
|
|
28
|
-
value: void 0
|
|
29
|
-
});
|
|
30
|
-
/** The Hyperliquid event target instance. */
|
|
31
|
-
Object.defineProperty(this, "_hlEvents", {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
configurable: true,
|
|
34
|
-
writable: true,
|
|
35
|
-
value: void 0
|
|
36
|
-
});
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "./_reconnecting_websocket.js", "./_hyperliquid_event_target.js", "./_websocket_request_dispatcher.js"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.WebSocketTransport = exports.WebSocketRequestError = void 0;
|
|
13
|
+
const _reconnecting_websocket_js_1 = require("./_reconnecting_websocket.js");
|
|
14
|
+
const _hyperliquid_event_target_js_1 = require("./_hyperliquid_event_target.js");
|
|
15
|
+
const _websocket_request_dispatcher_js_1 = require("./_websocket_request_dispatcher.js");
|
|
16
|
+
Object.defineProperty(exports, "WebSocketRequestError", { enumerable: true, get: function () { return _websocket_request_dispatcher_js_1.WebSocketRequestError; } });
|
|
17
|
+
/** WebSocket implementation of the REST and Subscription transport interfaces. */
|
|
18
|
+
class WebSocketTransport {
|
|
37
19
|
/**
|
|
38
|
-
*
|
|
39
|
-
* -
|
|
40
|
-
* - Value: Subscription info containing payload and listener mappings
|
|
20
|
+
* Creates a new WebSocket transport instance.
|
|
21
|
+
* @param options - Configuration options for the WebSocket transport layer.
|
|
41
22
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
23
|
+
constructor(options) {
|
|
24
|
+
/** The interval timer ID for keep-alive messages. */
|
|
25
|
+
Object.defineProperty(this, "_keepAliveTimer", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: null
|
|
30
|
+
});
|
|
31
|
+
/** The WebSocket request dispatcher instance. */
|
|
32
|
+
Object.defineProperty(this, "_wsRequester", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
38
|
+
/** The Hyperliquid event target instance. */
|
|
39
|
+
Object.defineProperty(this, "_hlEvents", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
/**
|
|
46
|
+
* Map of active subscriptions.
|
|
47
|
+
* - Key: Unique subscription identifier based on payload
|
|
48
|
+
* - Value: Subscription info containing the subscription request promise
|
|
49
|
+
* and a map of listeners to their metadata (channel + unsubscribe function).
|
|
50
|
+
*/
|
|
51
|
+
Object.defineProperty(this, "_subscriptions", {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
configurable: true,
|
|
54
|
+
writable: true,
|
|
55
|
+
value: new Map()
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Request timeout in ms.
|
|
59
|
+
* Set to `null` to disable.
|
|
60
|
+
*/
|
|
61
|
+
Object.defineProperty(this, "timeout", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: void 0
|
|
66
|
+
});
|
|
67
|
+
/** Keep-alive configuration settings. */
|
|
68
|
+
Object.defineProperty(this, "keepAlive", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
/** The WebSocket that is used for communication. */
|
|
75
|
+
Object.defineProperty(this, "socket", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true,
|
|
78
|
+
writable: true,
|
|
79
|
+
value: void 0
|
|
80
|
+
});
|
|
81
|
+
this.socket = new _reconnecting_websocket_js_1.ReconnectingWebSocket(options?.url ?? "wss://api.hyperliquid.xyz/ws", undefined, options?.reconnect);
|
|
82
|
+
this._hlEvents = new _hyperliquid_event_target_js_1.HyperliquidEventTarget(this.socket);
|
|
83
|
+
this._wsRequester = new _websocket_request_dispatcher_js_1.WebSocketRequestDispatcher(this.socket, this._hlEvents);
|
|
84
|
+
this.timeout = options?.timeout === undefined ? 10_000 : options.timeout;
|
|
85
|
+
this.keepAlive = {
|
|
86
|
+
interval: options?.keepAlive?.interval === undefined ? 20_000 : options.keepAlive.interval,
|
|
87
|
+
};
|
|
88
|
+
// Initialize listeners
|
|
89
|
+
this.socket.addEventListener("open", () => {
|
|
90
|
+
// Start keep-alive timer
|
|
91
|
+
if (this.keepAlive.interval && this._keepAliveTimer === null) {
|
|
92
|
+
this._keepAliveTimer = setInterval(() => {
|
|
93
|
+
this.socket.send(JSON.stringify({ method: "ping" }));
|
|
94
|
+
}, this.keepAlive.interval);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
this.socket.addEventListener("close", () => {
|
|
98
|
+
// Clear keep-alive timer
|
|
99
|
+
if (this._keepAliveTimer !== null) {
|
|
100
|
+
clearInterval(this._keepAliveTimer);
|
|
101
|
+
this._keepAliveTimer = null;
|
|
102
|
+
}
|
|
103
|
+
// Clear all subscriptions
|
|
104
|
+
for (const subscriptionInfo of this._subscriptions.values()) {
|
|
105
|
+
for (const [_, unsubscribe] of subscriptionInfo.listeners) {
|
|
106
|
+
unsubscribe();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
48
111
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
112
|
+
* Sends a request to the Hyperliquid API via WebSocket.
|
|
113
|
+
* @param endpoint - The API endpoint to send the request to.
|
|
114
|
+
* @param payload - The payload to send with the request.
|
|
115
|
+
* @param signal - An optional abort signal.
|
|
116
|
+
* @returns A promise that resolves with parsed JSON response body.
|
|
117
|
+
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
118
|
+
* @note Explorer requests are not supported in the Hyperliquid WebSocket API.
|
|
51
119
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
value: void 0
|
|
57
|
-
});
|
|
58
|
-
/** Keep-alive configuration settings. */
|
|
59
|
-
Object.defineProperty(this, "keepAlive", {
|
|
60
|
-
enumerable: true,
|
|
61
|
-
configurable: true,
|
|
62
|
-
writable: true,
|
|
63
|
-
value: void 0
|
|
64
|
-
});
|
|
65
|
-
/** The WebSocket that is used for communication. */
|
|
66
|
-
Object.defineProperty(this, "socket", {
|
|
67
|
-
enumerable: true,
|
|
68
|
-
configurable: true,
|
|
69
|
-
writable: true,
|
|
70
|
-
value: void 0
|
|
71
|
-
});
|
|
72
|
-
this.socket = new reconnecting_websocket_js_1.ReconnectingWebSocket(options?.url ?? "wss://api.hyperliquid.xyz/ws", undefined, options?.reconnect);
|
|
73
|
-
this._hlEvents = new hyperliquid_event_target_js_1.HyperliquidEventTarget(this.socket);
|
|
74
|
-
this._wsRequester = new websocket_request_dispatcher_js_1.WebSocketRequestDispatcher(this.socket, this._hlEvents);
|
|
75
|
-
this.timeout = options?.timeout === undefined ? 10_000 : options.timeout;
|
|
76
|
-
this.keepAlive = {
|
|
77
|
-
interval: options?.keepAlive?.interval === undefined ? 20_000 : options.keepAlive.interval,
|
|
78
|
-
};
|
|
79
|
-
// Initialize listeners
|
|
80
|
-
this.socket.addEventListener("open", () => {
|
|
81
|
-
// Start keep-alive timer
|
|
82
|
-
if (this.keepAlive.interval && !this._keepAliveTimer) {
|
|
83
|
-
this._keepAliveTimer = setInterval(() => {
|
|
84
|
-
this.socket.send(JSON.stringify({ method: "ping" }));
|
|
85
|
-
}, this.keepAlive.interval);
|
|
120
|
+
request(type, payload, signal) {
|
|
121
|
+
// Reject explorer requests because they are not supported by the Hyperliquid WebSocket API
|
|
122
|
+
if (type === "explorer") {
|
|
123
|
+
return Promise.reject(new _websocket_request_dispatcher_js_1.WebSocketRequestError("Explorer requests are not supported in the Hyperliquid WebSocket API."));
|
|
86
124
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
125
|
+
// Send the request and wait for a response
|
|
126
|
+
const combinedSignal = this._combineTimeoutWithSignal(this.timeout, signal);
|
|
127
|
+
return this._wsRequester.request("post", { type, payload }, combinedSignal);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Subscribes to a Hyperliquid event channel.
|
|
131
|
+
* @param channel - The event channel to listen to.
|
|
132
|
+
* @param payload - A payload to send with the subscription request.
|
|
133
|
+
* @param listener - A function to call when the event is dispatched.
|
|
134
|
+
* @param signal - An optional abort signal for canceling the subscription request.
|
|
135
|
+
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
136
|
+
*/
|
|
137
|
+
async subscribe(channel, payload, listener, signal) {
|
|
138
|
+
// Create a unique identifier for the subscription
|
|
139
|
+
const id = _websocket_request_dispatcher_js_1.WebSocketRequestDispatcher.requestToId(payload);
|
|
140
|
+
// Initialize new subscription, if it doesn't exist
|
|
141
|
+
let subscription = this._subscriptions.get(id);
|
|
142
|
+
if (!subscription) {
|
|
143
|
+
// Send subscription request
|
|
144
|
+
const combinedSignal = this._combineTimeoutWithSignal(this.timeout, signal);
|
|
145
|
+
const requestPromise = this._wsRequester.request("subscribe", payload, combinedSignal);
|
|
146
|
+
// Cache subscription info
|
|
147
|
+
subscription = { listeners: new Map(), requestPromise };
|
|
148
|
+
this._subscriptions.set(id, subscription);
|
|
93
149
|
}
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
150
|
+
// Initialize new listener, if it doesn't exist
|
|
151
|
+
let unsubscribe = subscription.listeners.get(listener);
|
|
152
|
+
if (!unsubscribe) {
|
|
153
|
+
// Create new unsubscribe function
|
|
154
|
+
unsubscribe = async (signal) => {
|
|
155
|
+
// Remove listener and cleanup
|
|
97
156
|
this._hlEvents.removeEventListener(channel, listener);
|
|
98
|
-
|
|
157
|
+
const subscription = this._subscriptions.get(id);
|
|
158
|
+
subscription?.listeners.delete(listener);
|
|
159
|
+
// If no listeners remain, remove subscription entirely
|
|
160
|
+
if (subscription?.listeners.size === 0) {
|
|
161
|
+
// Cleanup subscription
|
|
162
|
+
this._subscriptions.delete(id);
|
|
163
|
+
// If the socket is open, send unsubscription request
|
|
164
|
+
if (this.socket.readyState === WebSocket.OPEN) {
|
|
165
|
+
const combinedSignal = this._combineTimeoutWithSignal(this.timeout, signal);
|
|
166
|
+
await this._wsRequester.request("unsubscribe", payload, combinedSignal);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
// Add listener and cache unsubscribe function
|
|
171
|
+
this._hlEvents.addEventListener(channel, listener);
|
|
172
|
+
subscription.listeners.set(listener, unsubscribe);
|
|
99
173
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Sends a request to the Hyperliquid API via WebSocket.
|
|
105
|
-
* @param endpoint - The API endpoint to send the request to.
|
|
106
|
-
* @param payload - The payload to send with the request.
|
|
107
|
-
* @param signal - An optional abort signal.
|
|
108
|
-
* @returns A promise that resolves with parsed JSON response body.
|
|
109
|
-
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
110
|
-
* @note Explorer requests are not supported in the Hyperliquid WebSocket API.
|
|
111
|
-
*/
|
|
112
|
-
request(type, payload, signal) {
|
|
113
|
-
// Reject explorer requests because they are not supported by the Hyperliquid WebSocket API
|
|
114
|
-
if (type === "explorer") {
|
|
115
|
-
throw new Error("Explorer requests are not supported in the Hyperliquid WebSocket API.");
|
|
116
|
-
}
|
|
117
|
-
// Send the request and wait for a response
|
|
118
|
-
return this._wsRequester.request("post", { type, payload }, this.combineSignals(signal));
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Subscribes to a Hyperliquid event channel.
|
|
122
|
-
* @param channel - The event channel to listen to.
|
|
123
|
-
* @param payload - A payload to send with the subscription request.
|
|
124
|
-
* @param listener - A function to call when the event is dispatched.
|
|
125
|
-
* @param signal - An optional abort signal for canceling the subscription request.
|
|
126
|
-
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
127
|
-
*/
|
|
128
|
-
async subscribe(channel, payload, listener, signal) {
|
|
129
|
-
// Create a unique identifier for the subscription
|
|
130
|
-
const id = `${channel}::${websocket_request_dispatcher_js_1.WebSocketRequestDispatcher.requestToId(payload)}`;
|
|
131
|
-
// Initialize new subscription, if it doesn't exist
|
|
132
|
-
let subscriptionInfo = this._subscriptions.get(id);
|
|
133
|
-
if (!subscriptionInfo) {
|
|
134
|
-
// Send subscription request
|
|
135
|
-
const requestPromise = this._wsRequester.request("subscribe", payload, this.combineSignals(signal))
|
|
136
|
-
.catch((error) => {
|
|
137
|
-
this._subscriptions.delete(id);
|
|
138
|
-
throw error;
|
|
139
|
-
});
|
|
140
|
-
// Cache subscription info
|
|
141
|
-
subscriptionInfo = { channel, listeners: new Map(), requestPromise };
|
|
142
|
-
this._subscriptions.set(id, subscriptionInfo);
|
|
143
|
-
}
|
|
144
|
-
// Check if we already have an unsubscribe function for this listener
|
|
145
|
-
let unsubscribe = subscriptionInfo.listeners.get(listener);
|
|
146
|
-
if (!unsubscribe) {
|
|
147
|
-
// Create new unsubscribe function if none exists
|
|
148
|
-
unsubscribe = async (signal) => {
|
|
174
|
+
// Wait for the initial subscription request to complete
|
|
175
|
+
await subscription.requestPromise.catch((error) => {
|
|
149
176
|
// Remove listener and cleanup
|
|
150
177
|
this._hlEvents.removeEventListener(channel, listener);
|
|
151
|
-
const
|
|
178
|
+
const subscription = this._subscriptions.get(id);
|
|
179
|
+
subscription?.listeners.delete(listener);
|
|
152
180
|
// If no listeners remain, remove subscription entirely
|
|
153
|
-
|
|
154
|
-
if (subscriptionInfo.listeners.size === 0 && isDeleted) {
|
|
181
|
+
if (subscription?.listeners.size === 0) {
|
|
155
182
|
this._subscriptions.delete(id);
|
|
156
|
-
await this._wsRequester.request("unsubscribe", payload, this.combineSignals(signal));
|
|
157
183
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
184
|
+
// Rethrow the error
|
|
185
|
+
throw error;
|
|
186
|
+
});
|
|
187
|
+
// Return subscription control object
|
|
188
|
+
return { unsubscribe };
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Waits until the WebSocket connection is ready.
|
|
192
|
+
* @param signal - An optional abort signal.
|
|
193
|
+
* @returns A promise that resolves when the connection is ready.
|
|
194
|
+
*/
|
|
195
|
+
ready(signal) {
|
|
196
|
+
return new Promise((resolve, reject) => {
|
|
197
|
+
const combinedSignal = signal
|
|
198
|
+
? AbortSignal.any([this.socket.terminationSignal, signal])
|
|
199
|
+
: this.socket.terminationSignal;
|
|
200
|
+
if (combinedSignal.aborted)
|
|
201
|
+
return reject(combinedSignal.reason);
|
|
202
|
+
if (this.socket.readyState === WebSocket.OPEN)
|
|
203
|
+
return resolve();
|
|
204
|
+
const handleOpen = () => {
|
|
205
|
+
combinedSignal.removeEventListener("abort", handleAbort);
|
|
206
|
+
resolve();
|
|
207
|
+
};
|
|
208
|
+
const handleAbort = () => {
|
|
209
|
+
this.socket.removeEventListener("open", handleOpen);
|
|
210
|
+
reject(combinedSignal.reason);
|
|
211
|
+
};
|
|
212
|
+
this.socket.addEventListener("open", handleOpen, { once: true });
|
|
213
|
+
combinedSignal.addEventListener("abort", handleAbort, { once: true });
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Closes the WebSocket connection and waits until it is fully closed.
|
|
218
|
+
* @param signal - An optional abort signal.
|
|
219
|
+
* @returns A promise that resolves when the connection is fully closed.
|
|
220
|
+
*/
|
|
221
|
+
close(signal) {
|
|
222
|
+
return new Promise((resolve, reject) => {
|
|
223
|
+
if (signal?.aborted)
|
|
224
|
+
return reject(signal.reason);
|
|
225
|
+
if (this.socket.readyState === WebSocket.CLOSED)
|
|
226
|
+
return resolve();
|
|
227
|
+
const handleClose = () => {
|
|
228
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
229
|
+
resolve();
|
|
230
|
+
};
|
|
231
|
+
const handleAbort = () => {
|
|
232
|
+
this.socket.removeEventListener("close", handleClose);
|
|
233
|
+
reject(signal?.reason);
|
|
234
|
+
};
|
|
235
|
+
this.socket.addEventListener("close", handleClose, { once: true });
|
|
236
|
+
signal?.addEventListener("abort", handleAbort, { once: true });
|
|
237
|
+
this.socket.close();
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
/** Combines a timeout with an optional abort signal. */
|
|
241
|
+
_combineTimeoutWithSignal(timeout, signal) {
|
|
242
|
+
if (typeof timeout !== "number")
|
|
243
|
+
return signal;
|
|
244
|
+
if (!(signal instanceof AbortSignal))
|
|
245
|
+
return AbortSignal.timeout(timeout);
|
|
246
|
+
return AbortSignal.any([AbortSignal.timeout(timeout), signal]);
|
|
162
247
|
}
|
|
163
|
-
// Wait for the initial subscription request to complete
|
|
164
|
-
await subscriptionInfo.requestPromise.catch((error) => {
|
|
165
|
-
// Cleanup the subscription and rethrow the error
|
|
166
|
-
this._hlEvents.removeEventListener(channel, listener);
|
|
167
|
-
subscriptionInfo.listeners.delete(listener);
|
|
168
|
-
throw error;
|
|
169
|
-
});
|
|
170
|
-
// Return subscription control object
|
|
171
|
-
return { unsubscribe };
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Waits until the WebSocket connection is ready.
|
|
175
|
-
* @param signal - An optional abort signal.
|
|
176
|
-
* @returns A promise that resolves when the connection is ready.
|
|
177
|
-
*/
|
|
178
|
-
ready(signal) {
|
|
179
|
-
return new Promise((resolve, reject) => {
|
|
180
|
-
const combinedSignal = signal
|
|
181
|
-
? AbortSignal.any([this.socket.terminationSignal, signal])
|
|
182
|
-
: this.socket.terminationSignal;
|
|
183
|
-
if (combinedSignal.aborted)
|
|
184
|
-
return reject(combinedSignal.reason);
|
|
185
|
-
if (this.socket.readyState === WebSocket.OPEN)
|
|
186
|
-
return resolve();
|
|
187
|
-
const handleOpen = () => {
|
|
188
|
-
combinedSignal.removeEventListener("abort", handleAbort);
|
|
189
|
-
resolve();
|
|
190
|
-
};
|
|
191
|
-
const handleAbort = () => {
|
|
192
|
-
this.socket.removeEventListener("open", handleOpen);
|
|
193
|
-
reject(combinedSignal.reason);
|
|
194
|
-
};
|
|
195
|
-
this.socket.addEventListener("open", handleOpen, { once: true });
|
|
196
|
-
combinedSignal.addEventListener("abort", handleAbort, { once: true });
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Closes the WebSocket connection and waits until it is fully closed.
|
|
201
|
-
* @param signal - An optional abort signal.
|
|
202
|
-
* @returns A promise that resolves when the connection is fully closed.
|
|
203
|
-
*/
|
|
204
|
-
close(signal) {
|
|
205
|
-
return new Promise((resolve, reject) => {
|
|
206
|
-
if (signal?.aborted)
|
|
207
|
-
return reject(signal.reason);
|
|
208
|
-
if (this.socket.readyState === WebSocket.CLOSED)
|
|
209
|
-
return resolve();
|
|
210
|
-
const handleClose = () => {
|
|
211
|
-
signal?.removeEventListener("abort", handleAbort);
|
|
212
|
-
resolve();
|
|
213
|
-
};
|
|
214
|
-
const handleAbort = () => {
|
|
215
|
-
this.socket.removeEventListener("close", handleClose);
|
|
216
|
-
reject(signal?.reason);
|
|
217
|
-
};
|
|
218
|
-
this.socket.addEventListener("close", handleClose, { once: true });
|
|
219
|
-
signal?.addEventListener("abort", handleAbort, { once: true });
|
|
220
|
-
this.socket.close();
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Combines timeout and user-provided signal.
|
|
225
|
-
* @param signal - A user-provided signal.
|
|
226
|
-
* @returns An AbortSignal that triggers when either the timeout or user-provided signal aborts.
|
|
227
|
-
*/
|
|
228
|
-
combineSignals(signal) {
|
|
229
|
-
const signals = [];
|
|
230
|
-
if (this.timeout)
|
|
231
|
-
signals.push(AbortSignal.timeout(this.timeout));
|
|
232
|
-
if (signal)
|
|
233
|
-
signals.push(signal);
|
|
234
|
-
return signals.length > 1 ? AbortSignal.any(signals) : signals[0];
|
|
235
248
|
}
|
|
236
|
-
|
|
237
|
-
|
|
249
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
250
|
+
});
|