@nktkas/hyperliquid 0.13.2 → 0.15.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/CONTRIBUTING.md +4 -7
- package/README.md +297 -103
- package/esm/mod.d.ts +3 -12
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +2 -2
- package/esm/src/{transports/base.d.ts → base.d.ts} +11 -14
- package/esm/src/base.d.ts.map +1 -0
- package/esm/src/base.js +14 -0
- package/esm/src/clients/event.d.ts +4 -4
- package/esm/src/clients/event.d.ts.map +1 -1
- package/esm/src/clients/event.js +2 -2
- package/esm/src/clients/public.d.ts +109 -6
- package/esm/src/clients/public.d.ts.map +1 -1
- package/esm/src/clients/public.js +106 -3
- package/esm/src/clients/wallet.d.ts +232 -29
- package/esm/src/clients/wallet.d.ts.map +1 -1
- package/esm/src/clients/wallet.js +859 -243
- 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 +2 -4
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +8 -13
- package/{script/src/transports/websocket/hyperliquid_event_target.d.ts → esm/src/transports/websocket/_hyperliquid_event_target.d.ts} +7 -15
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
- package/esm/src/transports/websocket/{hyperliquid_event_target.js → _hyperliquid_event_target.js} +4 -22
- 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/esm/src/transports/websocket/{websocket_request_dispatcher.d.ts → _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} +3 -7
- package/esm/src/transports/websocket/websocket_transport.d.ts +13 -16
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +49 -49
- package/esm/src/types/exchange/requests.d.ts +172 -20
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/exchange/responses.d.ts +32 -32
- package/esm/src/types/exchange/responses.d.ts.map +1 -1
- package/esm/src/types/explorer/requests.d.ts +12 -12
- package/esm/src/types/explorer/requests.d.ts.map +1 -1
- package/esm/src/types/explorer/responses.d.ts +36 -1
- package/esm/src/types/explorer/responses.d.ts.map +1 -1
- package/esm/src/types/info/accounts.d.ts +130 -105
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +154 -152
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/delegations.d.ts +20 -20
- package/esm/src/types/info/delegations.d.ts.map +1 -1
- package/esm/src/types/info/orders.d.ts +55 -55
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +73 -10
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/info/vaults.d.ts +1 -1
- 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 +1 -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} +16 -4
- package/esm/src/types/subscriptions/responses.d.ts.map +1 -0
- package/package.json +20 -4
- package/script/mod.d.ts +3 -12
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +3 -5
- package/script/src/{transports/base.d.ts → base.d.ts} +11 -14
- package/script/src/base.d.ts.map +1 -0
- package/script/src/{transports/base.js → base.js} +13 -12
- package/script/src/clients/event.d.ts +4 -4
- package/script/src/clients/event.d.ts.map +1 -1
- package/script/src/clients/event.js +2 -2
- package/script/src/clients/public.d.ts +109 -6
- package/script/src/clients/public.d.ts.map +1 -1
- package/script/src/clients/public.js +106 -3
- package/script/src/clients/wallet.d.ts +232 -29
- package/script/src/clients/wallet.d.ts.map +1 -1
- package/script/src/clients/wallet.js +860 -244
- 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 +2 -4
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +9 -14
- package/{esm/src/transports/websocket/hyperliquid_event_target.d.ts → script/src/transports/websocket/_hyperliquid_event_target.d.ts} +7 -15
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
- package/script/src/transports/websocket/{hyperliquid_event_target.js → _hyperliquid_event_target.js} +4 -22
- 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 → _reconnecting_websocket.js} +94 -78
- package/script/src/transports/websocket/{websocket_request_dispatcher.d.ts → _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 → _websocket_request_dispatcher.js} +4 -8
- package/script/src/transports/websocket/websocket_transport.d.ts +13 -16
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +55 -55
- package/script/src/types/exchange/requests.d.ts +172 -20
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/exchange/responses.d.ts +32 -32
- package/script/src/types/exchange/responses.d.ts.map +1 -1
- package/script/src/types/explorer/requests.d.ts +12 -12
- package/script/src/types/explorer/requests.d.ts.map +1 -1
- package/script/src/types/explorer/responses.d.ts +36 -1
- package/script/src/types/explorer/responses.d.ts.map +1 -1
- package/script/src/types/info/accounts.d.ts +130 -105
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/assets.d.ts +154 -152
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/delegations.d.ts +20 -20
- package/script/src/types/info/delegations.d.ts.map +1 -1
- package/script/src/types/info/orders.d.ts +55 -55
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/requests.d.ts +73 -10
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/vaults.d.ts +1 -1
- package/script/src/types/info/vaults.d.ts.map +1 -1
- package/script/src/types/mod.d.ts +14 -0
- package/script/src/types/mod.d.ts.map +1 -0
- package/script/src/types/subscriptions/requests.d.ts +1 -1
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/{esm/src/types/subscriptions/common.d.ts → script/src/types/subscriptions/responses.d.ts} +16 -4
- package/script/src/types/subscriptions/responses.d.ts.map +1 -0
- 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.map +0 -1
- 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/explorer/common.js +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/src/transports/base.d.ts.map +0 -1
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
- package/script/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
- package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
- package/script/src/types/common.d.ts +0 -3
- package/script/src/types/common.d.ts.map +0 -1
- 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/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 -12
- package/script/src/types/subscriptions/common.d.ts.map +0 -1
- package/script/src/types/subscriptions/common.js +0 -12
- 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 -137
- 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 -182
- /package/esm/src/types/{common.js → mod.js} +0 -0
- /package/esm/src/types/{exchange/common.js → subscriptions/responses.js} +0 -0
- /package/script/src/types/{common.js → mod.js} +0 -0
- /package/script/src/types/{exchange/common.js → subscriptions/responses.js} +0 -0
package/esm/src/transports/websocket/{reconnecting_websocket.js → _reconnecting_websocket.js}
RENAMED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// deno-lint-ignore-file no-explicit-any
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*/
|
|
2
|
+
import { TransportError } from "../../base.js";
|
|
3
|
+
/** Simple FIFO (First In, First Out) buffer implementation. */
|
|
5
4
|
class FIFOMessageBuffer {
|
|
6
5
|
constructor() {
|
|
7
6
|
Object.defineProperty(this, "messages", {
|
|
@@ -21,6 +20,25 @@ class FIFOMessageBuffer {
|
|
|
21
20
|
this.messages = [];
|
|
22
21
|
}
|
|
23
22
|
}
|
|
23
|
+
/** Error thrown when reconnection problems occur. */
|
|
24
|
+
export class ReconnectingWebSocketError extends TransportError {
|
|
25
|
+
constructor(code, originalError) {
|
|
26
|
+
super(`Error when reconnecting WebSocket: ${code}`);
|
|
27
|
+
Object.defineProperty(this, "code", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: code
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(this, "originalError", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: originalError
|
|
38
|
+
});
|
|
39
|
+
this.name = "ReconnectingWebSocketError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
24
42
|
/**
|
|
25
43
|
* A WebSocket that automatically reconnects when disconnected.
|
|
26
44
|
* Fully compatible with standard WebSocket API.
|
|
@@ -34,35 +52,35 @@ export class ReconnectingWebSocket {
|
|
|
34
52
|
*/
|
|
35
53
|
constructor(url, protocols, options) {
|
|
36
54
|
/** Controller for handling connection termination */
|
|
37
|
-
Object.defineProperty(this, "
|
|
55
|
+
Object.defineProperty(this, "_terminationController", {
|
|
38
56
|
enumerable: true,
|
|
39
57
|
configurable: true,
|
|
40
58
|
writable: true,
|
|
41
59
|
value: new AbortController()
|
|
42
60
|
});
|
|
43
61
|
/** WebSocket protocols */
|
|
44
|
-
Object.defineProperty(this, "
|
|
62
|
+
Object.defineProperty(this, "_protocols", {
|
|
45
63
|
enumerable: true,
|
|
46
64
|
configurable: true,
|
|
47
65
|
writable: true,
|
|
48
66
|
value: void 0
|
|
49
67
|
});
|
|
50
68
|
/** The underlying WebSocket instance */
|
|
51
|
-
Object.defineProperty(this, "
|
|
69
|
+
Object.defineProperty(this, "_socket", {
|
|
52
70
|
enumerable: true,
|
|
53
71
|
configurable: true,
|
|
54
72
|
writable: true,
|
|
55
73
|
value: void 0
|
|
56
74
|
});
|
|
57
75
|
/** Current number of reconnection attempts */
|
|
58
|
-
Object.defineProperty(this, "
|
|
76
|
+
Object.defineProperty(this, "_reconnectCount", {
|
|
59
77
|
enumerable: true,
|
|
60
78
|
configurable: true,
|
|
61
79
|
writable: true,
|
|
62
80
|
value: 0
|
|
63
81
|
});
|
|
64
82
|
/** Array of registered event listeners */
|
|
65
|
-
Object.defineProperty(this, "
|
|
83
|
+
Object.defineProperty(this, "_eventListeners", {
|
|
66
84
|
enumerable: true,
|
|
67
85
|
configurable: true,
|
|
68
86
|
writable: true,
|
|
@@ -80,7 +98,7 @@ export class ReconnectingWebSocket {
|
|
|
80
98
|
enumerable: true,
|
|
81
99
|
configurable: true,
|
|
82
100
|
writable: true,
|
|
83
|
-
value: this.
|
|
101
|
+
value: this._terminationController.signal
|
|
84
102
|
});
|
|
85
103
|
Object.defineProperty(this, "CLOSED", {
|
|
86
104
|
enumerable: true,
|
|
@@ -112,13 +130,12 @@ export class ReconnectingWebSocket {
|
|
|
112
130
|
connectionDelay: options?.connectionDelay ?? ((attempt) => Math.min(~~(1 << attempt) * 150, 10_000)),
|
|
113
131
|
shouldReconnect: options?.shouldReconnect ?? (() => true),
|
|
114
132
|
messageBuffer: options?.messageBuffer ?? new FIFOMessageBuffer(),
|
|
115
|
-
WebSocketConstructor: options?.WebSocketConstructor ?? WebSocket,
|
|
116
133
|
};
|
|
117
|
-
this.
|
|
118
|
-
this.
|
|
119
|
-
? this.
|
|
120
|
-
: new
|
|
121
|
-
this.
|
|
134
|
+
this._protocols = protocols;
|
|
135
|
+
this._socket = this.reconnectOptions.connectionTimeout
|
|
136
|
+
? this._connectWithTimeout(url, this._protocols, this.reconnectOptions.connectionTimeout)
|
|
137
|
+
: new WebSocket(url, this._protocols);
|
|
138
|
+
this._initEventListeners();
|
|
122
139
|
}
|
|
123
140
|
/**
|
|
124
141
|
* Creates a WebSocket connection with timeout.
|
|
@@ -127,8 +144,8 @@ export class ReconnectingWebSocket {
|
|
|
127
144
|
* @param timeout - The connection timeout in ms.
|
|
128
145
|
* @returns A new WebSocket instance.
|
|
129
146
|
*/
|
|
130
|
-
|
|
131
|
-
const socket = new
|
|
147
|
+
_connectWithTimeout(url, protocols, timeout) {
|
|
148
|
+
const socket = new WebSocket(url, protocols);
|
|
132
149
|
const timeoutId = setTimeout(() => {
|
|
133
150
|
socket.removeEventListener("open", openHandler);
|
|
134
151
|
socket.removeEventListener("close", closeHandler);
|
|
@@ -146,55 +163,53 @@ export class ReconnectingWebSocket {
|
|
|
146
163
|
socket.addEventListener("close", closeHandler, { once: true });
|
|
147
164
|
return socket;
|
|
148
165
|
}
|
|
149
|
-
/**
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
initEventListeners() {
|
|
153
|
-
this.socket.addEventListener("open", () => {
|
|
166
|
+
/** Initializes the internal event listeners for the WebSocket. */
|
|
167
|
+
_initEventListeners() {
|
|
168
|
+
this._socket.addEventListener("open", () => {
|
|
154
169
|
// Reset reconnection count
|
|
155
|
-
this.
|
|
170
|
+
this._reconnectCount = 0;
|
|
156
171
|
// Send all buffered messages
|
|
157
172
|
let message;
|
|
158
173
|
while ((message = this.reconnectOptions.messageBuffer.shift()) !== undefined) {
|
|
159
|
-
this.
|
|
174
|
+
this._socket.send(message);
|
|
160
175
|
}
|
|
161
176
|
}, { once: true });
|
|
162
|
-
this.
|
|
177
|
+
this._socket.addEventListener("close", async (event) => {
|
|
163
178
|
try {
|
|
164
179
|
// If the termination signal is already aborted, do not attempt to reconnect
|
|
165
180
|
if (this.terminationSignal.aborted)
|
|
166
181
|
return;
|
|
167
182
|
// Check if reconnection should be attempted
|
|
168
|
-
if (++this.
|
|
169
|
-
this.
|
|
183
|
+
if (++this._reconnectCount > this.reconnectOptions.maxRetries) {
|
|
184
|
+
this._cleanup(new ReconnectingWebSocketError("RECONNECTION_LIMIT_REACHED"));
|
|
170
185
|
return;
|
|
171
186
|
}
|
|
172
187
|
const userDecision = await this.reconnectOptions.shouldReconnect(event);
|
|
173
188
|
if (this.terminationSignal.aborted)
|
|
174
189
|
return; // Check again after the await
|
|
175
190
|
if (!userDecision) {
|
|
176
|
-
this.
|
|
191
|
+
this._cleanup(new ReconnectingWebSocketError("RECONNECTION_STOPPED_BY_USER"));
|
|
177
192
|
return;
|
|
178
193
|
}
|
|
179
194
|
// Delay before reconnecting
|
|
180
195
|
const delay = typeof this.reconnectOptions.connectionDelay === "number"
|
|
181
196
|
? this.reconnectOptions.connectionDelay
|
|
182
|
-
: await this.reconnectOptions.connectionDelay(this.
|
|
197
|
+
: await this.reconnectOptions.connectionDelay(this._reconnectCount);
|
|
183
198
|
if (this.terminationSignal.aborted)
|
|
184
199
|
return; // Check again after the await
|
|
185
200
|
await sleep(delay, this.terminationSignal);
|
|
186
201
|
// Reconnect the socket
|
|
187
|
-
this.
|
|
188
|
-
? this.
|
|
189
|
-
: new
|
|
202
|
+
this._socket = this.reconnectOptions.connectionTimeout
|
|
203
|
+
? this._connectWithTimeout(this.url, this._protocols, this.reconnectOptions.connectionTimeout)
|
|
204
|
+
: new WebSocket(this.url, this._protocols);
|
|
190
205
|
// Reconnect all listeners
|
|
191
|
-
this.
|
|
192
|
-
this.
|
|
193
|
-
this.
|
|
206
|
+
this._initEventListeners();
|
|
207
|
+
this._eventListeners.forEach(({ type, listenerProxy, options }) => {
|
|
208
|
+
this._socket.addEventListener(type, listenerProxy, options);
|
|
194
209
|
});
|
|
195
210
|
}
|
|
196
211
|
catch (error) {
|
|
197
|
-
this.
|
|
212
|
+
this._cleanup(new ReconnectingWebSocketError("UNKNOWN_ERROR", error));
|
|
198
213
|
}
|
|
199
214
|
}, { once: true });
|
|
200
215
|
}
|
|
@@ -202,10 +217,10 @@ export class ReconnectingWebSocket {
|
|
|
202
217
|
* Clean up internal resources.
|
|
203
218
|
* @param reason - The reason for cleanup.
|
|
204
219
|
*/
|
|
205
|
-
|
|
206
|
-
this.
|
|
220
|
+
_cleanup(reason) {
|
|
221
|
+
this._terminationController.abort(reason);
|
|
207
222
|
this.reconnectOptions.messageBuffer.clear();
|
|
208
|
-
this.
|
|
223
|
+
this._eventListeners = [];
|
|
209
224
|
}
|
|
210
225
|
/**
|
|
211
226
|
* Check if two event listeners are the same (just like EventTarget).
|
|
@@ -213,79 +228,79 @@ export class ReconnectingWebSocket {
|
|
|
213
228
|
* @param b - Second event listener configuration.
|
|
214
229
|
* @returns True if the listeners match.
|
|
215
230
|
*/
|
|
216
|
-
|
|
231
|
+
_isListenerMatch(a, b) {
|
|
217
232
|
const aCapture = Boolean(typeof a.options === "object" ? a.options.capture : a.options);
|
|
218
233
|
const bCapture = Boolean(typeof b.options === "object" ? b.options.capture : b.options);
|
|
219
234
|
return a.type === b.type && a.listener === b.listener && aCapture === bCapture;
|
|
220
235
|
}
|
|
221
236
|
// WebSocket property implementations
|
|
222
237
|
get url() {
|
|
223
|
-
return this.
|
|
238
|
+
return this._socket.url;
|
|
224
239
|
}
|
|
225
240
|
get readyState() {
|
|
226
|
-
return this.
|
|
241
|
+
return this._socket.readyState;
|
|
227
242
|
}
|
|
228
243
|
get bufferedAmount() {
|
|
229
|
-
return this.
|
|
244
|
+
return this._socket.bufferedAmount;
|
|
230
245
|
}
|
|
231
246
|
get extensions() {
|
|
232
|
-
return this.
|
|
247
|
+
return this._socket.extensions;
|
|
233
248
|
}
|
|
234
249
|
get protocol() {
|
|
235
|
-
return this.
|
|
250
|
+
return this._socket.protocol;
|
|
236
251
|
}
|
|
237
252
|
get binaryType() {
|
|
238
|
-
return this.
|
|
253
|
+
return this._socket.binaryType;
|
|
239
254
|
}
|
|
240
255
|
set binaryType(value) {
|
|
241
|
-
this.
|
|
256
|
+
this._socket.binaryType = value;
|
|
242
257
|
}
|
|
243
258
|
get onclose() {
|
|
244
|
-
return this.
|
|
259
|
+
return this._socket.onclose;
|
|
245
260
|
}
|
|
246
261
|
set onclose(value) {
|
|
247
|
-
this.
|
|
262
|
+
this._socket.onclose = value;
|
|
248
263
|
}
|
|
249
264
|
get onerror() {
|
|
250
|
-
return this.
|
|
265
|
+
return this._socket.onerror;
|
|
251
266
|
}
|
|
252
267
|
set onerror(value) {
|
|
253
|
-
this.
|
|
268
|
+
this._socket.onerror = value;
|
|
254
269
|
}
|
|
255
270
|
get onmessage() {
|
|
256
|
-
return this.
|
|
271
|
+
return this._socket.onmessage;
|
|
257
272
|
}
|
|
258
273
|
set onmessage(value) {
|
|
259
|
-
this.
|
|
274
|
+
this._socket.onmessage = value;
|
|
260
275
|
}
|
|
261
276
|
get onopen() {
|
|
262
|
-
return this.
|
|
277
|
+
return this._socket.onopen;
|
|
263
278
|
}
|
|
264
279
|
set onopen(value) {
|
|
265
|
-
this.
|
|
280
|
+
this._socket.onopen = value;
|
|
266
281
|
}
|
|
267
282
|
/**
|
|
268
283
|
* @param permanently - If true, the connection will be permanently closed. Default is true.
|
|
269
284
|
*/
|
|
270
285
|
close(code, reason, permanently = true) {
|
|
271
286
|
if (permanently)
|
|
272
|
-
this.
|
|
273
|
-
this.
|
|
287
|
+
this._cleanup(new ReconnectingWebSocketError("USER_INITIATED_CLOSE"));
|
|
288
|
+
this._socket.close(code, reason);
|
|
274
289
|
}
|
|
275
290
|
/**
|
|
276
291
|
* @note If the connection is not open, the data will be buffered and sent when the connection is established.
|
|
277
292
|
*/
|
|
278
293
|
send(data) {
|
|
279
|
-
if (this.
|
|
294
|
+
if (this._socket.readyState !== WebSocket.OPEN && !this.terminationSignal.aborted) {
|
|
280
295
|
this.reconnectOptions.messageBuffer.push(data);
|
|
281
296
|
}
|
|
282
297
|
else {
|
|
283
|
-
this.
|
|
298
|
+
this._socket.send(data);
|
|
284
299
|
}
|
|
285
300
|
}
|
|
286
301
|
addEventListener(type, listener, options) {
|
|
287
302
|
// Check if the listener is already added
|
|
288
|
-
const exists = this.
|
|
303
|
+
const exists = this._eventListeners.some((e) => this._isListenerMatch(e, { type, listener, options }));
|
|
289
304
|
if (exists)
|
|
290
305
|
return;
|
|
291
306
|
// Wrap the original listener to follow the once option when reconnecting
|
|
@@ -300,25 +315,25 @@ export class ReconnectingWebSocket {
|
|
|
300
315
|
}
|
|
301
316
|
finally {
|
|
302
317
|
if (typeof options === "object" && options.once === true) {
|
|
303
|
-
const index = this.
|
|
318
|
+
const index = this._eventListeners.findIndex((e) => this._isListenerMatch(e, { type, listener, options }));
|
|
304
319
|
if (index !== -1)
|
|
305
|
-
this.
|
|
320
|
+
this._eventListeners.splice(index, 1);
|
|
306
321
|
}
|
|
307
322
|
}
|
|
308
323
|
};
|
|
309
|
-
this.
|
|
310
|
-
this.
|
|
324
|
+
this._eventListeners.push({ type, listener, options, listenerProxy });
|
|
325
|
+
this._socket.addEventListener(type, listenerProxy, options);
|
|
311
326
|
}
|
|
312
327
|
removeEventListener(type, listener, options) {
|
|
313
|
-
const index = this.
|
|
328
|
+
const index = this._eventListeners.findIndex((e) => this._isListenerMatch(e, { type, listener, options }));
|
|
314
329
|
if (index !== -1) {
|
|
315
|
-
const { listenerProxy } = this.
|
|
316
|
-
this.
|
|
317
|
-
this.
|
|
330
|
+
const { listenerProxy } = this._eventListeners[index];
|
|
331
|
+
this._socket.removeEventListener(type, listenerProxy, options);
|
|
332
|
+
this._eventListeners.splice(index, 1);
|
|
318
333
|
}
|
|
319
334
|
}
|
|
320
335
|
dispatchEvent(event) {
|
|
321
|
-
return this.
|
|
336
|
+
return this._socket.dispatchEvent(event);
|
|
322
337
|
}
|
|
323
338
|
}
|
|
324
339
|
Object.defineProperty(ReconnectingWebSocket, "CLOSED", {
|
|
@@ -353,18 +368,18 @@ Object.defineProperty(ReconnectingWebSocket, "OPEN", {
|
|
|
353
368
|
* @returns A promise that resolves after the specified delay.
|
|
354
369
|
*/
|
|
355
370
|
function sleep(ms, signal) {
|
|
371
|
+
if (signal?.aborted)
|
|
372
|
+
return Promise.reject(signal.reason);
|
|
356
373
|
return new Promise((resolve, reject) => {
|
|
357
|
-
|
|
358
|
-
return reject(signal.reason);
|
|
359
|
-
}
|
|
360
|
-
const onAbort = function () {
|
|
374
|
+
const onAbort = () => {
|
|
361
375
|
clearTimeout(timer);
|
|
362
|
-
reject(
|
|
376
|
+
reject(signal?.reason);
|
|
363
377
|
};
|
|
364
|
-
const
|
|
378
|
+
const onTimeout = () => {
|
|
365
379
|
signal?.removeEventListener("abort", onAbort);
|
|
366
380
|
resolve();
|
|
367
|
-
}
|
|
381
|
+
};
|
|
382
|
+
const timer = setTimeout(onTimeout, ms);
|
|
368
383
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
369
384
|
});
|
|
370
385
|
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import { TransportError } from "
|
|
2
|
-
import type { HyperliquidEventTarget } from "./
|
|
1
|
+
import { TransportError } from "../../base.js";
|
|
2
|
+
import type { HyperliquidEventTarget } from "./_hyperliquid_event_target.js";
|
|
3
3
|
/**
|
|
4
4
|
* Error thrown when a WebSocket request fails:
|
|
5
5
|
* - When the WebSocket connection is closed
|
|
6
6
|
* - When the server responds with an error message
|
|
7
7
|
*/
|
|
8
8
|
export declare class WebSocketRequestError extends TransportError {
|
|
9
|
-
/**
|
|
10
|
-
* Creates a new WebSocket request error.
|
|
11
|
-
* @param message - The error message.
|
|
12
|
-
*/
|
|
13
9
|
constructor(message: string);
|
|
14
10
|
}
|
|
15
11
|
/**
|
|
@@ -60,4 +56,4 @@ export declare class WebSocketRequestDispatcher {
|
|
|
60
56
|
*/
|
|
61
57
|
static requestToId(value: unknown): string;
|
|
62
58
|
}
|
|
63
|
-
//# sourceMappingURL=
|
|
59
|
+
//# sourceMappingURL=_websocket_request_dispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_websocket_request_dispatcher.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_websocket_request_dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAE7E;;;;GAIG;AACH,qBAAa,qBAAsB,SAAQ,cAAc;gBACzC,OAAO,EAAE,MAAM;CAI9B;AAED;;;GAGG;AACH,qBAAa,0BAA0B;IAevB,SAAS,CAAC,MAAM,EAAE,SAAS;IAdvC,2BAA2B;IAC3B,SAAS,CAAC,MAAM,EAAE,MAAM,CAAK;IAE7B,oDAAoD;IACpD,SAAS,CAAC,OAAO,EAAE,GAAG,CAClB,MAAM,GAAG,MAAM,EACf;QAAE,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;QAAC,MAAM,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAA;KAAE,CAC3E,CAAa;IAEd;;;;OAIG;gBACmB,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,sBAAsB;IA2DzE,+BAA+B;IAC/B,SAAS,KAAK,MAAM,IAAI,MAAM,CAE7B;IAED;;;;;;OAMG;IACG,OAAO,CACT,MAAM,EAAE,MAAM,GAAG,WAAW,GAAG,aAAa,EAC5C,OAAO,EAAE,OAAO,EAChB,MAAM,CAAC,EAAE,WAAW,GACrB,OAAO,CAAC,OAAO,CAAC;IAkCnB;;;;OAIG;IACH,SAAS,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;IAK5D;;;;OAIG;IACH,SAAS,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI;IAK5D;;;;OAIG;IACH,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM;CAK7C"}
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import { TransportError } from "
|
|
1
|
+
import { TransportError } from "../../base.js";
|
|
2
2
|
/**
|
|
3
3
|
* Error thrown when a WebSocket request fails:
|
|
4
4
|
* - When the WebSocket connection is closed
|
|
5
5
|
* - When the server responds with an error message
|
|
6
6
|
*/
|
|
7
7
|
export class WebSocketRequestError extends TransportError {
|
|
8
|
-
/**
|
|
9
|
-
* Creates a new WebSocket request error.
|
|
10
|
-
* @param message - The error message.
|
|
11
|
-
*/
|
|
12
8
|
constructor(message) {
|
|
13
9
|
super(message);
|
|
14
10
|
this.name = "WebSocketRequestError";
|
|
@@ -104,7 +100,7 @@ export class WebSocketRequestDispatcher {
|
|
|
104
100
|
* @param signal - An optional abort signal.
|
|
105
101
|
* @returns A promise that resolves with the parsed JSON response body.
|
|
106
102
|
*/
|
|
107
|
-
request(method, payload, signal) {
|
|
103
|
+
async request(method, payload, signal) {
|
|
108
104
|
signal?.throwIfAborted();
|
|
109
105
|
// Create a request object
|
|
110
106
|
let id;
|
|
@@ -121,7 +117,7 @@ export class WebSocketRequestDispatcher {
|
|
|
121
117
|
this.socket.send(JSON.stringify(request));
|
|
122
118
|
// Wait for a response
|
|
123
119
|
let onAbort;
|
|
124
|
-
return new Promise((resolve, reject) => {
|
|
120
|
+
return await new Promise((resolve, reject) => {
|
|
125
121
|
// Add an abort listener
|
|
126
122
|
onAbort = () => reject(signal?.reason);
|
|
127
123
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
@@ -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,12 +36,10 @@ 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. */
|
|
@@ -53,10 +51,7 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
|
|
|
53
51
|
* and a map of listeners to their metadata (channel + unsubscribe function).
|
|
54
52
|
*/
|
|
55
53
|
protected _subscriptions: Map<string, {
|
|
56
|
-
listeners: Map<(data: CustomEvent) => void,
|
|
57
|
-
channel: string;
|
|
58
|
-
unsubscribe: (signal?: AbortSignal) => Promise<void>;
|
|
59
|
-
}>;
|
|
54
|
+
listeners: Map<(data: CustomEvent) => void, (signal?: AbortSignal) => Promise<void>>;
|
|
60
55
|
requestPromise: Promise<unknown>;
|
|
61
56
|
}>;
|
|
62
57
|
/**
|
|
@@ -88,7 +83,7 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
|
|
|
88
83
|
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
89
84
|
* @note Explorer requests are not supported in the Hyperliquid WebSocket API.
|
|
90
85
|
*/
|
|
91
|
-
request(type: "info" | "
|
|
86
|
+
request(type: "info" | "exchange" | "explorer", payload: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
92
87
|
/**
|
|
93
88
|
* Subscribes to a Hyperliquid event channel.
|
|
94
89
|
* @param channel - The event channel to listen to.
|
|
@@ -110,5 +105,7 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
|
|
|
110
105
|
* @returns A promise that resolves when the connection is fully closed.
|
|
111
106
|
*/
|
|
112
107
|
close(signal?: AbortSignal): Promise<void>;
|
|
108
|
+
/** Combines a timeout with an optional abort signal. */
|
|
109
|
+
protected _combineTimeoutWithSignal(timeout?: number | null, signal?: AbortSignal): AbortSignal | undefined;
|
|
113
110
|
}
|
|
114
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,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC;IAoBzG;;;;;;;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"}
|