@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.
Files changed (243) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +334 -107
  3. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  4. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.js +2 -0
  6. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  7. package/esm/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.js +1 -1
  9. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  10. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.js +2 -0
  12. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  13. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.js +1 -1
  14. package/{script/deps/jsr.io/@std/encoding/1.0.6 → esm/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  15. package/esm/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/hex.js +1 -1
  17. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  18. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +2 -0
  20. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  21. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.js +2 -2
  23. package/esm/mod.d.ts +4 -12
  24. package/esm/mod.d.ts.map +1 -1
  25. package/esm/mod.js +2 -2
  26. package/{script/src/transports → esm/src}/base.d.ts +10 -13
  27. package/esm/src/base.d.ts.map +1 -0
  28. package/esm/src/base.js +14 -0
  29. package/esm/src/clients/event.d.ts +59 -18
  30. package/esm/src/clients/event.d.ts.map +1 -1
  31. package/esm/src/clients/event.js +74 -18
  32. package/esm/src/clients/public.d.ts +348 -53
  33. package/esm/src/clients/public.d.ts.map +1 -1
  34. package/esm/src/clients/public.js +349 -50
  35. package/esm/src/clients/wallet.d.ts +345 -73
  36. package/esm/src/clients/wallet.d.ts.map +1 -1
  37. package/esm/src/clients/wallet.js +979 -229
  38. package/esm/src/signing.d.ts +135 -0
  39. package/esm/src/signing.d.ts.map +1 -0
  40. package/esm/src/signing.js +188 -0
  41. package/esm/src/transports/http/http_transport.d.ts +1 -1
  42. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  43. package/esm/src/transports/http/http_transport.js +1 -1
  44. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +64 -0
  45. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
  46. package/esm/src/transports/websocket/_hyperliquid_event_target.js +52 -0
  47. package/esm/src/transports/websocket/{reconnecting_websocket.d.ts → _reconnecting_websocket.d.ts} +26 -26
  48. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -0
  49. package/esm/src/transports/websocket/{reconnecting_websocket.js → _reconnecting_websocket.js} +91 -76
  50. package/{script/src/transports/websocket/websocket_request_dispatcher.d.ts → esm/src/transports/websocket/_websocket_request_dispatcher.d.ts} +3 -7
  51. package/esm/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +1 -0
  52. package/esm/src/transports/websocket/{websocket_request_dispatcher.js → _websocket_request_dispatcher.js} +6 -10
  53. package/esm/src/transports/websocket/websocket_transport.d.ts +15 -21
  54. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  55. package/esm/src/transports/websocket/websocket_transport.js +55 -52
  56. package/esm/src/types/exchange/requests.d.ts +298 -79
  57. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  58. package/esm/src/types/exchange/responses.d.ts +38 -38
  59. package/esm/src/types/exchange/responses.d.ts.map +1 -1
  60. package/esm/src/types/explorer/requests.d.ts +23 -10
  61. package/esm/src/types/explorer/requests.d.ts.map +1 -1
  62. package/esm/src/types/explorer/responses.d.ts +45 -3
  63. package/esm/src/types/explorer/responses.d.ts.map +1 -1
  64. package/esm/src/types/info/accounts.d.ts +279 -189
  65. package/esm/src/types/info/accounts.d.ts.map +1 -1
  66. package/esm/src/types/info/assets.d.ts +191 -185
  67. package/esm/src/types/info/assets.d.ts.map +1 -1
  68. package/esm/src/types/info/delegations.d.ts +117 -0
  69. package/esm/src/types/info/delegations.d.ts.map +1 -0
  70. package/esm/src/types/info/orders.d.ts +94 -94
  71. package/esm/src/types/info/orders.d.ts.map +1 -1
  72. package/esm/src/types/info/requests.d.ts +192 -64
  73. package/esm/src/types/info/requests.d.ts.map +1 -1
  74. package/esm/src/types/info/vaults.d.ts +42 -85
  75. package/esm/src/types/info/vaults.d.ts.map +1 -1
  76. package/esm/src/types/mod.d.ts +14 -0
  77. package/esm/src/types/mod.d.ts.map +1 -0
  78. package/esm/src/types/subscriptions/requests.d.ts +11 -1
  79. package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
  80. package/{script/src/types/subscriptions/common.d.ts → esm/src/types/subscriptions/responses.d.ts} +18 -3
  81. package/esm/src/types/subscriptions/responses.d.ts.map +1 -0
  82. package/package.json +20 -4
  83. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
  84. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
  85. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
  86. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
  87. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
  88. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
  89. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
  90. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  91. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  92. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
  93. package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  94. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  95. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
  96. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  97. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  98. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
  99. package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  100. package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
  101. package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  102. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  103. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
  104. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  105. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  106. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
  107. package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  108. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  109. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
  110. package/script/mod.d.ts +4 -12
  111. package/script/mod.d.ts.map +1 -1
  112. package/script/mod.js +21 -13
  113. package/{esm/src/transports → script/src}/base.d.ts +10 -13
  114. package/script/src/base.d.ts.map +1 -0
  115. package/script/src/base.js +29 -0
  116. package/script/src/clients/event.d.ts +59 -18
  117. package/script/src/clients/event.d.ts.map +1 -1
  118. package/script/src/clients/event.js +551 -485
  119. package/script/src/clients/public.d.ts +348 -53
  120. package/script/src/clients/public.d.ts.map +1 -1
  121. package/script/src/clients/public.js +1017 -708
  122. package/script/src/clients/wallet.d.ts +345 -73
  123. package/script/src/clients/wallet.d.ts.map +1 -1
  124. package/script/src/clients/wallet.js +1737 -977
  125. package/script/src/signing.d.ts +135 -0
  126. package/script/src/signing.d.ts.map +1 -0
  127. package/script/src/signing.js +203 -0
  128. package/script/src/transports/http/http_transport.d.ts +1 -1
  129. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  130. package/script/src/transports/http/http_transport.js +174 -164
  131. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +64 -0
  132. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
  133. package/script/src/transports/websocket/_hyperliquid_event_target.js +66 -0
  134. package/script/src/transports/websocket/{reconnecting_websocket.d.ts → _reconnecting_websocket.d.ts} +26 -26
  135. package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -0
  136. package/script/src/transports/websocket/_reconnecting_websocket.js +400 -0
  137. package/{esm/src/transports/websocket/websocket_request_dispatcher.d.ts → script/src/transports/websocket/_websocket_request_dispatcher.d.ts} +3 -7
  138. package/script/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +1 -0
  139. package/script/src/transports/websocket/_websocket_request_dispatcher.js +212 -0
  140. package/script/src/transports/websocket/websocket_transport.d.ts +15 -21
  141. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  142. package/script/src/transports/websocket/websocket_transport.js +235 -222
  143. package/script/src/types/exchange/requests.d.ts +298 -79
  144. package/script/src/types/exchange/requests.d.ts.map +1 -1
  145. package/script/src/types/exchange/requests.js +12 -2
  146. package/script/src/types/exchange/responses.d.ts +38 -38
  147. package/script/src/types/exchange/responses.d.ts.map +1 -1
  148. package/script/src/types/exchange/responses.js +12 -2
  149. package/script/src/types/explorer/requests.d.ts +23 -10
  150. package/script/src/types/explorer/requests.d.ts.map +1 -1
  151. package/script/src/types/explorer/requests.js +12 -2
  152. package/script/src/types/explorer/responses.d.ts +45 -3
  153. package/script/src/types/explorer/responses.d.ts.map +1 -1
  154. package/script/src/types/explorer/responses.js +12 -2
  155. package/script/src/types/info/accounts.d.ts +279 -189
  156. package/script/src/types/info/accounts.d.ts.map +1 -1
  157. package/script/src/types/info/accounts.js +12 -2
  158. package/script/src/types/info/assets.d.ts +191 -185
  159. package/script/src/types/info/assets.d.ts.map +1 -1
  160. package/script/src/types/info/assets.js +12 -2
  161. package/script/src/types/info/delegations.d.ts +117 -0
  162. package/script/src/types/info/delegations.d.ts.map +1 -0
  163. package/script/src/types/info/delegations.js +12 -0
  164. package/script/src/types/info/orders.d.ts +94 -94
  165. package/script/src/types/info/orders.d.ts.map +1 -1
  166. package/script/src/types/info/orders.js +12 -2
  167. package/script/src/types/info/requests.d.ts +192 -64
  168. package/script/src/types/info/requests.d.ts.map +1 -1
  169. package/script/src/types/info/requests.js +12 -2
  170. package/script/src/types/info/vaults.d.ts +42 -85
  171. package/script/src/types/info/vaults.d.ts.map +1 -1
  172. package/script/src/types/info/vaults.js +12 -2
  173. package/script/src/types/mod.d.ts +14 -0
  174. package/script/src/types/mod.d.ts.map +1 -0
  175. package/script/src/types/mod.js +12 -0
  176. package/script/src/types/subscriptions/requests.d.ts +11 -1
  177. package/script/src/types/subscriptions/requests.d.ts.map +1 -1
  178. package/script/src/types/subscriptions/requests.js +12 -2
  179. package/{esm/src/types/subscriptions/common.d.ts → script/src/types/subscriptions/responses.d.ts} +18 -3
  180. package/script/src/types/subscriptions/responses.d.ts.map +1 -0
  181. package/script/src/types/subscriptions/responses.js +12 -0
  182. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  183. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  184. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  185. package/esm/src/transports/base.d.ts.map +0 -1
  186. package/esm/src/transports/base.js +0 -14
  187. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +0 -66
  188. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
  189. package/esm/src/transports/websocket/hyperliquid_event_target.js +0 -33
  190. package/esm/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
  191. package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
  192. package/esm/src/types/common.d.ts +0 -3
  193. package/esm/src/types/common.d.ts.map +0 -1
  194. package/esm/src/types/exchange/common.d.ts +0 -36
  195. package/esm/src/types/exchange/common.d.ts.map +0 -1
  196. package/esm/src/types/explorer/common.d.ts +0 -37
  197. package/esm/src/types/explorer/common.d.ts.map +0 -1
  198. package/esm/src/types/subscriptions/common.d.ts.map +0 -1
  199. package/esm/src/types/subscriptions/common.js +0 -1
  200. package/esm/src/utils/key_sort.d.ts +0 -21
  201. package/esm/src/utils/key_sort.d.ts.map +0 -1
  202. package/esm/src/utils/key_sort.js +0 -124
  203. package/esm/src/utils/signing.d.ts +0 -109
  204. package/esm/src/utils/signing.d.ts.map +0 -1
  205. package/esm/src/utils/signing.js +0 -164
  206. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  207. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
  208. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
  209. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  210. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
  211. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  212. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
  213. package/script/src/transports/base.d.ts.map +0 -1
  214. package/script/src/transports/base.js +0 -18
  215. package/script/src/transports/websocket/hyperliquid_event_target.d.ts +0 -66
  216. package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
  217. package/script/src/transports/websocket/hyperliquid_event_target.js +0 -37
  218. package/script/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
  219. package/script/src/transports/websocket/reconnecting_websocket.js +0 -374
  220. package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
  221. package/script/src/transports/websocket/websocket_request_dispatcher.js +0 -206
  222. package/script/src/types/common.d.ts +0 -3
  223. package/script/src/types/common.d.ts.map +0 -1
  224. package/script/src/types/common.js +0 -2
  225. package/script/src/types/exchange/common.d.ts +0 -36
  226. package/script/src/types/exchange/common.d.ts.map +0 -1
  227. package/script/src/types/exchange/common.js +0 -2
  228. package/script/src/types/explorer/common.d.ts +0 -37
  229. package/script/src/types/explorer/common.d.ts.map +0 -1
  230. package/script/src/types/explorer/common.js +0 -2
  231. package/script/src/types/subscriptions/common.d.ts.map +0 -1
  232. package/script/src/types/subscriptions/common.js +0 -2
  233. package/script/src/utils/key_sort.d.ts +0 -21
  234. package/script/src/utils/key_sort.d.ts.map +0 -1
  235. package/script/src/utils/key_sort.js +0 -127
  236. package/script/src/utils/signing.d.ts +0 -109
  237. package/script/src/utils/signing.d.ts.map +0 -1
  238. package/script/src/utils/signing.js +0 -172
  239. /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
  240. /package/esm/src/types/{common.js → info/delegations.js} +0 -0
  241. /package/esm/src/types/{exchange/common.js → mod.js} +0 -0
  242. /package/esm/src/types/{explorer/common.js → subscriptions/responses.js} +0 -0
  243. /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 "./reconnecting_websocket.js";
2
- import { HyperliquidEventTarget } from "./hyperliquid_event_target.js";
3
- import { WebSocketRequestDispatcher } from "./websocket_request_dispatcher.js";
4
- import type { IRequestTransport, ISubscriptionTransport, Subscription } from "../base.js";
5
- /**
6
- * Configuration options for the WebSocket transport layer.
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 | undefined;
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 (stringified payload)
52
- * - Value: Subscription info containing payload and listener mappings
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
- channel: string;
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
- * Combines timeout and user-provided signal.
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,6BAA6B,CAAC;AACvG,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1F;;GAEG;AACH,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;;GAEG;AACH,qBAAa,kBAAmB,YAAW,iBAAiB,EAAE,sBAAsB;IAChF,qDAAqD;IACrD,SAAS,CAAC,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IAE9C,iDAAiD;IACjD,SAAS,CAAC,YAAY,EAAE,0BAA0B,CAAC;IAEnD,6CAA6C;IAC7C,SAAS,CAAC,SAAS,EAAE,sBAAsB,CAAC;IAE5C;;;;OAIG;IACH,SAAS,CAAC,cAAc,EAAE,GAAG,CACzB,MAAM,EACN;QACI,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,WAAW,KAAK,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QACjE,cAAc,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;KACpC,CACJ,CAUG;IAEJ;;;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;IAwC/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;IAUvG;;;;;;;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;IAqDxB;;;;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;;;;OAIG;IACH,SAAS,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,WAAW;CAM9D"}
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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebSocketTransport = void 0;
4
- const reconnecting_websocket_js_1 = require("./reconnecting_websocket.js");
5
- const hyperliquid_event_target_js_1 = require("./hyperliquid_event_target.js");
6
- const websocket_request_dispatcher_js_1 = require("./websocket_request_dispatcher.js");
7
- /**
8
- * WebSocket implementation of the REST and Subscription transport interfaces.
9
- */
10
- class WebSocketTransport {
11
- /**
12
- * Creates a new WebSocket transport instance.
13
- * @param options - Configuration options for the WebSocket transport layer.
14
- */
15
- constructor(options) {
16
- /** The interval timer ID for keep-alive messages. */
17
- Object.defineProperty(this, "_keepAliveTimer", {
18
- enumerable: true,
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
- * Map of active subscriptions.
39
- * - Key: Unique subscription identifier (stringified payload)
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
- Object.defineProperty(this, "_subscriptions", {
43
- enumerable: true,
44
- configurable: true,
45
- writable: true,
46
- value: new Map()
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
- * Request timeout in ms.
50
- * Set to `null` to disable.
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
- Object.defineProperty(this, "timeout", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
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
- this.socket.addEventListener("close", () => {
89
- // Clear keep-alive timer
90
- if (this._keepAliveTimer) {
91
- clearInterval(this._keepAliveTimer);
92
- this._keepAliveTimer = undefined;
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
- // Clear all subscriptions
95
- for (const [_, { channel, listeners }] of this._subscriptions.entries()) {
96
- for (const [listener] of listeners) {
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
- this._subscriptions.clear();
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 isDeleted = subscriptionInfo.listeners.delete(listener);
178
+ const subscription = this._subscriptions.get(id);
179
+ subscription?.listeners.delete(listener);
152
180
  // If no listeners remain, remove subscription entirely
153
- // `isDeleted` means that the map had a listener before and became 0 after that
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
- // Add event listener & Cache unsubscribe function
160
- this._hlEvents.addEventListener(channel, listener);
161
- subscriptionInfo.listeners.set(listener, unsubscribe);
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
- exports.WebSocketTransport = WebSocketTransport;
249
+ exports.WebSocketTransport = WebSocketTransport;
250
+ });