@nktkas/hyperliquid 0.17.4 → 0.18.1

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 (106) hide show
  1. package/README.md +48 -36
  2. package/esm/deps/jsr.io/@std/async/1.0.12/delay.d.ts +43 -0
  3. package/esm/deps/jsr.io/@std/async/1.0.12/delay.d.ts.map +1 -0
  4. package/esm/deps/jsr.io/@std/async/1.0.12/delay.js +63 -0
  5. package/esm/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_common16.d.ts +1 -1
  6. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +1 -0
  7. package/esm/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_common16.js +1 -1
  8. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +1 -0
  9. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +1 -0
  10. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +1 -0
  11. package/esm/src/base.d.ts +2 -2
  12. package/esm/src/base.d.ts.map +1 -1
  13. package/esm/src/clients/event.d.ts +2 -1
  14. package/esm/src/clients/event.d.ts.map +1 -1
  15. package/esm/src/clients/event.js +3 -0
  16. package/esm/src/clients/public.d.ts +2 -1
  17. package/esm/src/clients/public.d.ts.map +1 -1
  18. package/esm/src/clients/public.js +3 -0
  19. package/esm/src/clients/wallet.d.ts +24 -13
  20. package/esm/src/clients/wallet.d.ts.map +1 -1
  21. package/esm/src/clients/wallet.js +88 -62
  22. package/esm/src/signing.d.ts +4 -1
  23. package/esm/src/signing.d.ts.map +1 -1
  24. package/esm/src/signing.js +46 -23
  25. package/esm/src/transports/http/http_transport.d.ts +9 -60
  26. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  27. package/esm/src/transports/http/http_transport.js +9 -57
  28. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +3 -12
  29. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  30. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts +22 -44
  31. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  32. package/esm/src/transports/websocket/_reconnecting_websocket.js +127 -193
  33. package/esm/src/transports/websocket/websocket_transport.d.ts +8 -1
  34. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  35. package/esm/src/transports/websocket/websocket_transport.js +26 -21
  36. package/esm/src/types/exchange/requests.d.ts +26 -2
  37. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  38. package/esm/src/types/exchange/responses.d.ts +6 -6
  39. package/esm/src/types/exchange/responses.d.ts.map +1 -1
  40. package/esm/src/types/info/accounts.d.ts +14 -0
  41. package/esm/src/types/info/accounts.d.ts.map +1 -1
  42. package/esm/src/types/info/assets.d.ts +18 -0
  43. package/esm/src/types/info/assets.d.ts.map +1 -1
  44. package/package.json +2 -1
  45. package/script/deps/jsr.io/@std/async/1.0.12/delay.d.ts +43 -0
  46. package/script/deps/jsr.io/@std/async/1.0.12/delay.d.ts.map +1 -0
  47. package/script/deps/jsr.io/@std/async/1.0.12/delay.js +76 -0
  48. package/script/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_common16.d.ts +1 -1
  49. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +1 -0
  50. package/script/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_common16.js +1 -1
  51. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +1 -0
  52. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +1 -0
  53. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +1 -0
  54. package/script/src/base.d.ts +2 -2
  55. package/script/src/base.d.ts.map +1 -1
  56. package/script/src/clients/event.d.ts +2 -1
  57. package/script/src/clients/event.d.ts.map +1 -1
  58. package/script/src/clients/event.js +3 -0
  59. package/script/src/clients/public.d.ts +2 -1
  60. package/script/src/clients/public.d.ts.map +1 -1
  61. package/script/src/clients/public.js +3 -0
  62. package/script/src/clients/wallet.d.ts +24 -13
  63. package/script/src/clients/wallet.d.ts.map +1 -1
  64. package/script/src/clients/wallet.js +88 -62
  65. package/script/src/signing.d.ts +4 -1
  66. package/script/src/signing.d.ts.map +1 -1
  67. package/script/src/signing.js +46 -23
  68. package/script/src/transports/http/http_transport.d.ts +9 -60
  69. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  70. package/script/src/transports/http/http_transport.js +9 -57
  71. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +3 -12
  72. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  73. package/script/src/transports/websocket/_reconnecting_websocket.d.ts +22 -44
  74. package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  75. package/script/src/transports/websocket/_reconnecting_websocket.js +129 -195
  76. package/script/src/transports/websocket/websocket_transport.d.ts +8 -1
  77. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  78. package/script/src/transports/websocket/websocket_transport.js +26 -21
  79. package/script/src/types/exchange/requests.d.ts +26 -2
  80. package/script/src/types/exchange/requests.d.ts.map +1 -1
  81. package/script/src/types/exchange/responses.d.ts +6 -6
  82. package/script/src/types/exchange/responses.d.ts.map +1 -1
  83. package/script/src/types/info/accounts.d.ts +14 -0
  84. package/script/src/types/info/accounts.d.ts.map +1 -1
  85. package/script/src/types/info/assets.d.ts +18 -0
  86. package/script/src/types/info/assets.d.ts.map +1 -1
  87. package/esm/deps/jsr.io/@std/encoding/1.0.9/_common16.d.ts.map +0 -1
  88. package/esm/deps/jsr.io/@std/encoding/1.0.9/_common_detach.d.ts.map +0 -1
  89. package/esm/deps/jsr.io/@std/encoding/1.0.9/_types.d.ts.map +0 -1
  90. package/esm/deps/jsr.io/@std/encoding/1.0.9/hex.d.ts.map +0 -1
  91. package/script/deps/jsr.io/@std/encoding/1.0.9/_common16.d.ts.map +0 -1
  92. package/script/deps/jsr.io/@std/encoding/1.0.9/_common_detach.d.ts.map +0 -1
  93. package/script/deps/jsr.io/@std/encoding/1.0.9/_types.d.ts.map +0 -1
  94. package/script/deps/jsr.io/@std/encoding/1.0.9/hex.d.ts.map +0 -1
  95. /package/esm/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_common_detach.d.ts +0 -0
  96. /package/esm/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_common_detach.js +0 -0
  97. /package/esm/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_types.d.ts +0 -0
  98. /package/esm/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_types.js +0 -0
  99. /package/esm/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/hex.d.ts +0 -0
  100. /package/esm/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/hex.js +0 -0
  101. /package/script/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_common_detach.d.ts +0 -0
  102. /package/script/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_common_detach.js +0 -0
  103. /package/script/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_types.d.ts +0 -0
  104. /package/script/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/_types.js +0 -0
  105. /package/script/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/hex.d.ts +0 -0
  106. /package/script/deps/jsr.io/@std/encoding/{1.0.9 → 1.0.10}/hex.js +0 -0
@@ -1,16 +1,17 @@
1
- // deno-lint-ignore-file no-explicit-any
2
1
  (function (factory) {
3
2
  if (typeof module === "object" && typeof module.exports === "object") {
4
3
  var v = factory(require, exports);
5
4
  if (v !== undefined) module.exports = v;
6
5
  }
7
6
  else if (typeof define === "function" && define.amd) {
8
- define(["require", "exports", "../../base.js"], factory);
7
+ define(["require", "exports", "../../../deps/jsr.io/@std/async/1.0.12/delay.js", "../../base.js"], factory);
9
8
  }
10
9
  })(function (require, exports) {
11
10
  "use strict";
12
11
  Object.defineProperty(exports, "__esModule", { value: true });
13
12
  exports.ReconnectingWebSocket = exports.ReconnectingWebSocketError = void 0;
13
+ // deno-lint-ignore-file no-explicit-any
14
+ const delay_js_1 = require("../../../deps/jsr.io/@std/async/1.0.12/delay.js");
14
15
  const base_js_1 = require("../../base.js");
15
16
  /** Simple FIFO (First In, First Out) buffer implementation. */
16
17
  class FIFOMessageBuffer {
@@ -57,192 +58,174 @@
57
58
  * Fully compatible with standard WebSocket API.
58
59
  */
59
60
  class ReconnectingWebSocket {
60
- /**
61
- * Creates a new reconnecting WebSocket.
62
- * @param url - The WebSocket URL to connect to.
63
- * @param protocols - The WebSocket protocols to use.
64
- * @param options - The configuration options.
65
- */
66
61
  constructor(url, protocols, options) {
67
- /** Controller for handling connection termination. */
68
- Object.defineProperty(this, "_terminationController", {
69
- enumerable: true,
70
- configurable: true,
71
- writable: true,
72
- value: new AbortController()
73
- });
74
- /** WebSocket protocols defined in constructor. */
75
- Object.defineProperty(this, "_protocols", {
76
- enumerable: true,
77
- configurable: true,
78
- writable: true,
79
- value: void 0
80
- });
81
- /** Non-permanent original instance of WebSocket. */
82
62
  Object.defineProperty(this, "_socket", {
83
63
  enumerable: true,
84
64
  configurable: true,
85
65
  writable: true,
86
66
  value: void 0
87
67
  });
88
- /** Current number of reconnection attempts */
89
- Object.defineProperty(this, "_reconnectCount", {
68
+ Object.defineProperty(this, "_protocols", {
90
69
  enumerable: true,
91
70
  configurable: true,
92
71
  writable: true,
93
- value: 0
72
+ value: void 0
94
73
  });
95
- /** The array of registered event listeners to recover from reconnection. */
96
- Object.defineProperty(this, "_eventListeners", {
74
+ Object.defineProperty(this, "_listeners", {
97
75
  enumerable: true,
98
76
  configurable: true,
99
77
  writable: true,
100
78
  value: []
101
79
  });
102
- /** WebSocket event handlers for reconnection. */
103
- Object.defineProperty(this, "_onclose", {
80
+ Object.defineProperty(this, "_attempt", {
104
81
  enumerable: true,
105
82
  configurable: true,
106
83
  writable: true,
107
- value: void 0
84
+ value: 0
108
85
  });
109
- /** WebSocket event handlers for reconnection. */
110
- Object.defineProperty(this, "_onerror", {
86
+ Object.defineProperty(this, "reconnectOptions", {
111
87
  enumerable: true,
112
88
  configurable: true,
113
89
  writable: true,
114
90
  value: void 0
115
91
  });
116
- /** WebSocket event handlers for reconnection. */
117
- Object.defineProperty(this, "_onmessage", {
92
+ Object.defineProperty(this, "reconnectAbortController", {
118
93
  enumerable: true,
119
94
  configurable: true,
120
95
  writable: true,
121
- value: void 0
96
+ value: new AbortController()
122
97
  });
123
- /** WebSocket event handlers for reconnection. */
124
- Object.defineProperty(this, "_onopen", {
98
+ Object.defineProperty(this, "_open", {
125
99
  enumerable: true,
126
100
  configurable: true,
127
101
  writable: true,
128
- value: void 0
102
+ value: () => {
103
+ // Reset the attempt counter
104
+ this._attempt = 0;
105
+ // Send all buffered messages
106
+ let message;
107
+ while ((message = this.reconnectOptions.messageBuffer.shift()) !== undefined) {
108
+ this._socket.send(message);
109
+ }
110
+ }
129
111
  });
130
- /** Configuration options for WebSocket reconnection. */
131
- Object.defineProperty(this, "reconnectOptions", {
112
+ Object.defineProperty(this, "_close", {
132
113
  enumerable: true,
133
114
  configurable: true,
134
115
  writable: true,
135
- value: void 0
116
+ value: async (event) => {
117
+ try {
118
+ // If the event was triggered but the socket is not closing, ignore it
119
+ if (this._socket.readyState !== ReconnectingWebSocket.CLOSING &&
120
+ this._socket.readyState !== ReconnectingWebSocket.CLOSED)
121
+ return;
122
+ // If the instance is terminated, do not attempt to reconnect
123
+ if (this.reconnectAbortController.signal.aborted)
124
+ return;
125
+ // Check if reconnection should be attempted
126
+ if (++this._attempt > this.reconnectOptions.maxRetries) {
127
+ this._cleanup("RECONNECTION_LIMIT_REACHED");
128
+ return;
129
+ }
130
+ const userDecision = await this.reconnectOptions.shouldReconnect(event, this.reconnectAbortController.signal);
131
+ if (this.reconnectAbortController.signal.aborted)
132
+ return;
133
+ if (!userDecision) {
134
+ this._cleanup("RECONNECTION_STOPPED_BY_USER");
135
+ return;
136
+ }
137
+ // Delay before reconnecting
138
+ const reconnectDelay = typeof this.reconnectOptions.connectionDelay === "number"
139
+ ? this.reconnectOptions.connectionDelay
140
+ : await this.reconnectOptions.connectionDelay(this._attempt, this.reconnectAbortController.signal);
141
+ if (this.reconnectAbortController.signal.aborted)
142
+ return;
143
+ await (0, delay_js_1.delay)(reconnectDelay, { signal: this.reconnectAbortController.signal });
144
+ // Create a new WebSocket instance
145
+ const { onclose, onerror, onmessage, onopen } = this._socket;
146
+ this._socket = this._createSocket(this._socket.url, this._protocols);
147
+ // Reconnect all listeners
148
+ this._setupEventListeners();
149
+ this._listeners.forEach(({ type, listenerProxy, options }) => {
150
+ this._socket.addEventListener(type, listenerProxy, options);
151
+ });
152
+ this._socket.onclose = onclose;
153
+ this._socket.onerror = onerror;
154
+ this._socket.onmessage = onmessage;
155
+ this._socket.onopen = onopen;
156
+ }
157
+ catch (error) {
158
+ this._cleanup("UNKNOWN_ERROR", error);
159
+ }
160
+ }
136
161
  });
137
- /** The signal that is aborted when the connection is permanently closed. */
138
- Object.defineProperty(this, "terminationSignal", {
162
+ Object.defineProperty(this, "CONNECTING", {
139
163
  enumerable: true,
140
164
  configurable: true,
141
165
  writable: true,
142
- value: this._terminationController.signal
166
+ value: 0
143
167
  });
144
- Object.defineProperty(this, "CLOSED", {
168
+ Object.defineProperty(this, "OPEN", {
145
169
  enumerable: true,
146
170
  configurable: true,
147
171
  writable: true,
148
- value: WebSocket.CLOSED
172
+ value: 1
149
173
  });
150
174
  Object.defineProperty(this, "CLOSING", {
151
175
  enumerable: true,
152
176
  configurable: true,
153
177
  writable: true,
154
- value: WebSocket.CLOSING
178
+ value: 2
155
179
  });
156
- Object.defineProperty(this, "CONNECTING", {
157
- enumerable: true,
158
- configurable: true,
159
- writable: true,
160
- value: WebSocket.CONNECTING
161
- });
162
- Object.defineProperty(this, "OPEN", {
180
+ Object.defineProperty(this, "CLOSED", {
163
181
  enumerable: true,
164
182
  configurable: true,
165
183
  writable: true,
166
- value: WebSocket.OPEN
184
+ value: 3
167
185
  });
168
- // Set the default options
169
186
  this.reconnectOptions = {
170
187
  maxRetries: options?.maxRetries ?? 3,
171
188
  connectionTimeout: options?.connectionTimeout === undefined ? 10_000 : options.connectionTimeout,
172
- connectionDelay: options?.connectionDelay ?? ((attempt) => Math.min(~~(1 << attempt) * 150, 10_000)),
189
+ connectionDelay: options?.connectionDelay ?? ((n) => Math.min(~~(1 << n) * 150, 10_000)),
173
190
  shouldReconnect: options?.shouldReconnect ?? (() => true),
174
191
  messageBuffer: options?.messageBuffer ?? new FIFOMessageBuffer(),
175
192
  };
193
+ this._socket = this._createSocket(url, protocols);
176
194
  this._protocols = protocols;
177
- // Create the WebSocket instance
178
- this._socket = createWebSocketWithTimeout(url, this._protocols, this.reconnectOptions.connectionTimeout);
179
- // Initialize the reconnection event listeners
180
- this._initEventListeners();
181
- // Store the original event listeners for reconnection
182
- this._onclose = this._socket.onclose;
183
- this._onerror = this._socket.onerror;
184
- this._onmessage = this._socket.onmessage;
185
- this._onopen = this._socket.onopen;
195
+ this._setupEventListeners();
186
196
  }
187
- /** Initializes the internal event listeners for the WebSocket. */
188
- _initEventListeners() {
189
- this._socket.addEventListener("open", () => {
190
- // Send all buffered messages
191
- let message;
192
- while ((message = this.reconnectOptions.messageBuffer.shift()) !== undefined) {
193
- this._socket.send(message);
194
- }
195
- }, { once: true });
196
- this._socket.addEventListener("close", async (event) => {
197
- try {
198
- // If the termination signal is already aborted, do not attempt to reconnect
199
- if (this._terminationController.signal.aborted)
200
- return;
201
- // Check if reconnection should be attempted
202
- if (++this._reconnectCount > this.reconnectOptions.maxRetries) {
203
- this._cleanup(new ReconnectingWebSocketError("RECONNECTION_LIMIT_REACHED"));
204
- return;
205
- }
206
- const userDecision = await this.reconnectOptions.shouldReconnect(event);
207
- if (this._terminationController.signal.aborted)
208
- return; // Check again after the await
209
- if (!userDecision) {
210
- this._cleanup(new ReconnectingWebSocketError("RECONNECTION_STOPPED_BY_USER"));
211
- return;
212
- }
213
- // Delay before reconnecting
214
- const delay = typeof this.reconnectOptions.connectionDelay === "number"
215
- ? this.reconnectOptions.connectionDelay
216
- : await this.reconnectOptions.connectionDelay(this._reconnectCount);
217
- if (this._terminationController.signal.aborted)
218
- return; // Check again after the await
219
- await sleep(delay, this._terminationController.signal);
220
- // Create a new WebSocket instance
221
- this._socket = createWebSocketWithTimeout(this.url, this._protocols, this.reconnectOptions.connectionTimeout);
222
- // Reconnect all listeners
223
- this._initEventListeners();
224
- this._eventListeners.forEach(({ type, listenerProxy, options }) => {
225
- this._socket.addEventListener(type, listenerProxy, options);
226
- });
227
- this._socket.onclose = this._onclose;
228
- this._socket.onerror = this._onerror;
229
- this._socket.onmessage = this._onmessage;
230
- this._socket.onopen = this._onopen;
231
- }
232
- catch (error) {
233
- this._cleanup(new ReconnectingWebSocketError("UNKNOWN_ERROR", error));
234
- }
235
- }, { once: true });
197
+ _createSocket(url, protocols) {
198
+ const socket = new WebSocket(url, protocols);
199
+ if (this.reconnectOptions.connectionTimeout === null)
200
+ return socket;
201
+ const timeoutId = setTimeout(() => {
202
+ socket.removeEventListener("open", openHandler);
203
+ socket.removeEventListener("close", closeHandler);
204
+ socket.close(3008, "Timeout"); // https://www.iana.org/assignments/websocket/websocket.xml#close-code-number
205
+ }, this.reconnectOptions.connectionTimeout);
206
+ const openHandler = () => {
207
+ socket.removeEventListener("close", closeHandler);
208
+ clearTimeout(timeoutId);
209
+ };
210
+ const closeHandler = () => {
211
+ socket.removeEventListener("open", openHandler);
212
+ clearTimeout(timeoutId);
213
+ };
214
+ socket.addEventListener("open", openHandler, { once: true });
215
+ socket.addEventListener("close", closeHandler, { once: true });
216
+ return socket;
236
217
  }
237
- /**
238
- * Clean up internal resources.
239
- * @param reason - The reason for cleanup.
240
- */
241
- _cleanup(reason) {
242
- this._terminationController.abort(reason);
218
+ /** Initializes the internal event listeners for the socket. */
219
+ _setupEventListeners() {
220
+ this._socket.addEventListener("open", this._open, { once: true });
221
+ this._socket.addEventListener("close", this._close, { once: true });
222
+ }
223
+ /** Clean up internal resources. */
224
+ _cleanup(code, error) {
225
+ this.reconnectAbortController.abort(new ReconnectingWebSocketError(code, error));
243
226
  this.reconnectOptions.messageBuffer.clear();
244
- this._eventListeners = [];
245
- this._socket.dispatchEvent(new CustomEvent("error", { detail: reason }));
227
+ this._listeners = [];
228
+ this._socket.close();
246
229
  }
247
230
  // WebSocket property implementations
248
231
  get url() {
@@ -271,43 +254,38 @@
271
254
  }
272
255
  set onclose(value) {
273
256
  this._socket.onclose = value;
274
- this._onclose = value; // Store the listener for reconnection
275
257
  }
276
258
  get onerror() {
277
259
  return this._socket.onerror;
278
260
  }
279
261
  set onerror(value) {
280
262
  this._socket.onerror = value;
281
- this._onerror = value; // Store the listener for reconnection
282
263
  }
283
264
  get onmessage() {
284
265
  return this._socket.onmessage;
285
266
  }
286
267
  set onmessage(value) {
287
268
  this._socket.onmessage = value;
288
- this._onmessage = value; // Store the listener for reconnection
289
269
  }
290
270
  get onopen() {
291
271
  return this._socket.onopen;
292
272
  }
293
273
  set onopen(value) {
294
274
  this._socket.onopen = value;
295
- this._onopen = value; // Store the listener for reconnection
296
275
  }
297
276
  /**
298
277
  * @param permanently - If `true`, the connection will be permanently closed. Default is `true`.
299
278
  */
300
279
  close(code, reason, permanently = true) {
301
280
  this._socket.close(code, reason);
302
- if (permanently) {
303
- this._cleanup(new ReconnectingWebSocketError("USER_INITIATED_CLOSE"));
304
- }
281
+ if (permanently)
282
+ this._cleanup("USER_INITIATED_CLOSE");
305
283
  }
306
284
  /**
307
285
  * @note If the connection is not open, the data will be buffered and sent when the connection is established.
308
286
  */
309
287
  send(data) {
310
- if (this._socket.readyState !== WebSocket.OPEN && !this._terminationController.signal.aborted) {
288
+ if (this._socket.readyState !== ReconnectingWebSocket.OPEN && !this.reconnectAbortController.signal.aborted) {
311
289
  this.reconnectOptions.messageBuffer.push(data);
312
290
  }
313
291
  else {
@@ -317,16 +295,16 @@
317
295
  addEventListener(type, listener, options) {
318
296
  // Wrap the listener to handle reconnection
319
297
  let listenerProxy;
320
- if (this._terminationController.signal.aborted) {
321
- // If the connection is permanently closed, use the original listener
298
+ if (this.reconnectAbortController.signal.aborted) {
299
+ // If the instance is terminated, use the original listener
322
300
  listenerProxy = listener;
323
301
  }
324
302
  else {
325
303
  // Check if the listener is already registered
326
- const index = this._eventListeners.findIndex((e) => listenersMatch(e, { type, listener, options }));
304
+ const index = this._listeners.findIndex((e) => listenersMatch(e, { type, listener, options }));
327
305
  if (index !== -1) {
328
306
  // Use the existing listener proxy
329
- listenerProxy = this._eventListeners[index].listenerProxy;
307
+ listenerProxy = this._listeners[index].listenerProxy;
330
308
  }
331
309
  else {
332
310
  // Wrap the original listener to follow the once option when reconnecting
@@ -340,15 +318,16 @@
340
318
  }
341
319
  }
342
320
  finally {
321
+ // If the listener is marked as once, remove it after the first invocation
343
322
  if (typeof options === "object" && options.once === true) {
344
- const index = this._eventListeners.findIndex((e) => listenersMatch(e, { type, listener, options }));
323
+ const index = this._listeners.findIndex((e) => listenersMatch(e, { type, listener, options }));
345
324
  if (index !== -1) {
346
- this._eventListeners.splice(index, 1);
325
+ this._listeners.splice(index, 1);
347
326
  }
348
327
  }
349
328
  }
350
329
  };
351
- this._eventListeners.push({ type, listener, options, listenerProxy });
330
+ this._listeners.push({ type, listener, options, listenerProxy });
352
331
  }
353
332
  }
354
333
  // Add the wrapped (or original) listener
@@ -356,11 +335,11 @@
356
335
  }
357
336
  removeEventListener(type, listener, options) {
358
337
  // Remove a wrapped listener, not an original listener
359
- const index = this._eventListeners.findIndex((e) => listenersMatch(e, { type, listener, options }));
338
+ const index = this._listeners.findIndex((e) => listenersMatch(e, { type, listener, options }));
360
339
  if (index !== -1) {
361
- const { listenerProxy } = this._eventListeners[index];
340
+ const { listenerProxy } = this._listeners[index];
362
341
  this._socket.removeEventListener(type, listenerProxy, options);
363
- this._eventListeners.splice(index, 1);
342
+ this._listeners.splice(index, 1);
364
343
  }
365
344
  else {
366
345
  // If the wrapped listener is not found, remove the original listener
@@ -372,52 +351,30 @@
372
351
  }
373
352
  }
374
353
  exports.ReconnectingWebSocket = ReconnectingWebSocket;
375
- Object.defineProperty(ReconnectingWebSocket, "CLOSED", {
354
+ Object.defineProperty(ReconnectingWebSocket, "CONNECTING", {
376
355
  enumerable: true,
377
356
  configurable: true,
378
357
  writable: true,
379
- value: WebSocket.CLOSED
358
+ value: 0
380
359
  });
381
- Object.defineProperty(ReconnectingWebSocket, "CLOSING", {
360
+ Object.defineProperty(ReconnectingWebSocket, "OPEN", {
382
361
  enumerable: true,
383
362
  configurable: true,
384
363
  writable: true,
385
- value: WebSocket.CLOSING
364
+ value: 1
386
365
  });
387
- Object.defineProperty(ReconnectingWebSocket, "CONNECTING", {
366
+ Object.defineProperty(ReconnectingWebSocket, "CLOSING", {
388
367
  enumerable: true,
389
368
  configurable: true,
390
369
  writable: true,
391
- value: WebSocket.CONNECTING
370
+ value: 2
392
371
  });
393
- Object.defineProperty(ReconnectingWebSocket, "OPEN", {
372
+ Object.defineProperty(ReconnectingWebSocket, "CLOSED", {
394
373
  enumerable: true,
395
374
  configurable: true,
396
375
  writable: true,
397
- value: WebSocket.OPEN
376
+ value: 3
398
377
  });
399
- /** Creates a WebSocket with connection timeout. */
400
- function createWebSocketWithTimeout(url, protocols, timeout) {
401
- const socket = new WebSocket(url, protocols);
402
- if (timeout === null || timeout === undefined)
403
- return socket;
404
- const timeoutId = setTimeout(() => {
405
- socket.removeEventListener("open", openHandler);
406
- socket.removeEventListener("close", closeHandler);
407
- socket.close(3008, "Timeout"); // https://www.iana.org/assignments/websocket/websocket.xml#close-code-number
408
- }, timeout);
409
- const openHandler = () => {
410
- socket.removeEventListener("close", closeHandler);
411
- clearTimeout(timeoutId);
412
- };
413
- const closeHandler = () => {
414
- socket.removeEventListener("open", openHandler);
415
- clearTimeout(timeoutId);
416
- };
417
- socket.addEventListener("open", openHandler, { once: true });
418
- socket.addEventListener("close", closeHandler, { once: true });
419
- return socket;
420
- }
421
378
  /** Check if two event listeners are the same (just like EventTarget). */
422
379
  function listenersMatch(a, b) {
423
380
  // EventTarget only compares capture in options, even if one is an object and the other is boolean
@@ -425,27 +382,4 @@
425
382
  const bCapture = Boolean(typeof b.options === "object" ? b.options.capture : b.options);
426
383
  return a.type === b.type && a.listener === b.listener && aCapture === bCapture;
427
384
  }
428
- /**
429
- * Returns a promise that resolves after the specified number of ms,
430
- * or rejects as soon as the given signal is aborted.
431
- * @param ms - The number of ms to sleep.
432
- * @param signal - An optional abort signal.
433
- * @returns A promise that resolves after the specified delay.
434
- */
435
- function sleep(ms, signal) {
436
- if (signal?.aborted)
437
- return Promise.reject(signal.reason);
438
- return new Promise((resolve, reject) => {
439
- const onAbort = () => {
440
- clearTimeout(timer);
441
- reject(signal?.reason);
442
- };
443
- const onTimeout = () => {
444
- signal?.removeEventListener("abort", onAbort);
445
- resolve();
446
- };
447
- const timer = setTimeout(onTimeout, ms);
448
- signal?.addEventListener("abort", onAbort, { once: true });
449
- });
450
- }
451
385
  });
@@ -41,7 +41,7 @@ export interface WebSocketTransportOptions {
41
41
  reconnect?: ReconnectingWebSocketOptions;
42
42
  }
43
43
  /** WebSocket implementation of the REST and Subscription transport interfaces. */
44
- export declare class WebSocketTransport implements IRequestTransport, ISubscriptionTransport {
44
+ export declare class WebSocketTransport implements IRequestTransport, ISubscriptionTransport, AsyncDisposable {
45
45
  /** The interval timer ID for keep-alive messages. */
46
46
  protected _keepAliveTimer: number | null;
47
47
  /** The WebSocket request dispatcher instance. */
@@ -109,5 +109,12 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
109
109
  * @returns A promise that resolves when the connection is fully closed.
110
110
  */
111
111
  close(signal?: AbortSignal): Promise<void>;
112
+ /**
113
+ * Combines the provided abort signal with the timeout signal.
114
+ * @param signal An optional abort signal.
115
+ * @returns A combined abort signal or undefined.
116
+ */
117
+ protected _getCombinedTimeoutSignal(signal?: AbortSignal): AbortSignal | undefined;
118
+ [Symbol.asyncDispose](): Promise<void>;
112
119
  }
113
120
  //# 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,EACH,KAAK,qBAAqB,EAC1B,qBAAqB,EACrB,0BAA0B,EAC1B,KAAK,4BAA4B,EACpC,MAAM,8BAA8B,CAAC;AACtC,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,OAAO,EAAE,KAAK,qBAAqB,EAAE,0BAA0B,EAAE,KAAK,4BAA4B,EAAE,CAAC;AAErG,+DAA+D;AAC/D,MAAM,WAAW,yBAAyB;IACtC;;;;;;;;;OASG;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,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC;IAiBzG;;;;;;;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;IAwExB;;;;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;CAoB7C"}
1
+ {"version":3,"file":"websocket_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/websocket_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,qBAAqB,EAC1B,qBAAqB,EACrB,0BAA0B,EAC1B,KAAK,4BAA4B,EACpC,MAAM,8BAA8B,CAAC;AACtC,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,OAAO,EAAE,KAAK,qBAAqB,EAAE,0BAA0B,EAAE,KAAK,4BAA4B,EAAE,CAAC;AAErG,+DAA+D;AAC/D,MAAM,WAAW,yBAAyB;IACtC;;;;;;;;;OASG;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,EAAE,eAAe;IACjG,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,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC;IAQzG;;;;;;;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;;;;OAIG;IACH,SAAS,CAAC,yBAAyB,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,WAAW,GAAG,SAAS;IAQ5E,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAG/C"}
@@ -119,15 +119,11 @@
119
119
  * @note Explorer requests are not supported in the Hyperliquid WebSocket API.
120
120
  */
121
121
  request(type, payload, signal) {
122
- // Send the request and wait for a response
123
- const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
124
- const combinedSignal = signal && timeoutSignal
125
- ? AbortSignal.any([signal, timeoutSignal])
126
- : signal ?? timeoutSignal;
122
+ const combinedTimeoutSignal = this._getCombinedTimeoutSignal(signal);
127
123
  return this._wsRequester.request("post", {
128
124
  type: type === "exchange" ? "action" : type,
129
125
  payload,
130
- }, combinedSignal);
126
+ }, combinedTimeoutSignal);
131
127
  }
132
128
  /**
133
129
  * Subscribes to a Hyperliquid event channel.
@@ -144,11 +140,8 @@
144
140
  let subscription = this._subscriptions.get(id);
145
141
  if (!subscription) {
146
142
  // Send subscription request
147
- const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
148
- const combinedSignal = signal && timeoutSignal
149
- ? AbortSignal.any([signal, timeoutSignal])
150
- : signal ?? timeoutSignal;
151
- const requestPromise = this._wsRequester.request("subscribe", payload, combinedSignal);
143
+ const combinedTimeoutSignal = this._getCombinedTimeoutSignal(signal);
144
+ const requestPromise = this._wsRequester.request("subscribe", payload, combinedTimeoutSignal);
152
145
  // Cache subscription info
153
146
  subscription = { listeners: new Map(), requestPromise };
154
147
  this._subscriptions.set(id, subscription);
@@ -167,12 +160,9 @@
167
160
  // Cleanup subscription
168
161
  this._subscriptions.delete(id);
169
162
  // If the socket is open, send unsubscription request
170
- if (this.socket.readyState === WebSocket.OPEN) {
171
- const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
172
- const combinedSignal = signal && timeoutSignal
173
- ? AbortSignal.any([signal, timeoutSignal])
174
- : signal ?? timeoutSignal;
175
- await this._wsRequester.request("unsubscribe", payload, combinedSignal);
163
+ if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN) {
164
+ const combinedTimeoutSignal = this._getCombinedTimeoutSignal(signal);
165
+ await this._wsRequester.request("unsubscribe", payload, combinedTimeoutSignal);
176
166
  }
177
167
  }
178
168
  };
@@ -204,11 +194,11 @@
204
194
  ready(signal) {
205
195
  return new Promise((resolve, reject) => {
206
196
  const combinedSignal = signal
207
- ? AbortSignal.any([this.socket.terminationSignal, signal])
208
- : this.socket.terminationSignal;
197
+ ? AbortSignal.any([this.socket.reconnectAbortController.signal, signal])
198
+ : this.socket.reconnectAbortController.signal;
209
199
  if (combinedSignal.aborted)
210
200
  return reject(combinedSignal.reason);
211
- if (this.socket.readyState === WebSocket.OPEN)
201
+ if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN)
212
202
  return resolve();
213
203
  const handleOpen = () => {
214
204
  combinedSignal.removeEventListener("abort", handleAbort);
@@ -231,7 +221,7 @@
231
221
  return new Promise((resolve, reject) => {
232
222
  if (signal?.aborted)
233
223
  return reject(signal.reason);
234
- if (this.socket.readyState === WebSocket.CLOSED)
224
+ if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.CLOSED)
235
225
  return resolve();
236
226
  const handleClose = () => {
237
227
  signal?.removeEventListener("abort", handleAbort);
@@ -246,6 +236,21 @@
246
236
  this.socket.close();
247
237
  });
248
238
  }
239
+ /**
240
+ * Combines the provided abort signal with the timeout signal.
241
+ * @param signal An optional abort signal.
242
+ * @returns A combined abort signal or undefined.
243
+ */
244
+ _getCombinedTimeoutSignal(signal) {
245
+ const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
246
+ const combinedSignal = signal && timeoutSignal
247
+ ? AbortSignal.any([signal, timeoutSignal])
248
+ : signal ?? timeoutSignal;
249
+ return combinedSignal;
250
+ }
251
+ async [Symbol.asyncDispose]() {
252
+ await this.close();
253
+ }
249
254
  }
250
255
  exports.WebSocketTransport = WebSocketTransport;
251
256
  });