@nktkas/hyperliquid 0.19.1 → 0.20.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 -2
- package/README.md +36 -35
- package/esm/_dnt.polyfills.d.ts +20 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +12 -0
- package/esm/mod.d.ts +3 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +2 -0
- package/esm/src/base.d.ts +1 -47
- package/esm/src/base.d.ts.map +1 -1
- package/esm/src/base.js +1 -8
- package/esm/src/clients/event.d.ts +6 -4
- package/esm/src/clients/event.d.ts.map +1 -1
- package/esm/src/clients/event.js +58 -77
- package/esm/src/clients/public.d.ts +26 -5
- package/esm/src/clients/public.d.ts.map +1 -1
- package/esm/src/clients/public.js +29 -41
- package/esm/src/clients/wallet.d.ts +200 -26
- package/esm/src/clients/wallet.d.ts.map +1 -1
- package/esm/src/clients/wallet.js +306 -284
- package/esm/src/signing.d.ts +80 -5
- package/esm/src/signing.d.ts.map +1 -1
- package/esm/src/signing.js +96 -7
- package/esm/src/transports/base.d.ts +49 -0
- package/esm/src/transports/base.d.ts.map +1 -0
- package/esm/src/transports/base.js +8 -0
- package/esm/src/transports/http/http_transport.d.ts +8 -5
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +15 -62
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +36 -39
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts +18 -25
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.js +80 -179
- package/{script/src/transports/websocket/_websocket_request_dispatcher.d.ts → esm/src/transports/websocket/_websocket_async_request.d.ts} +14 -18
- package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -0
- package/esm/src/transports/websocket/{_websocket_request_dispatcher.js → _websocket_async_request.js} +42 -75
- package/esm/src/transports/websocket/websocket_transport.d.ts +38 -28
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +61 -76
- package/esm/src/types/exchange/requests.d.ts +278 -66
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +0 -48
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/markets.d.ts +52 -0
- package/esm/src/types/info/markets.d.ts.map +1 -0
- package/esm/src/types/info/markets.js +1 -0
- package/esm/src/types/info/orders.d.ts +1 -1
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +14 -5
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/mod.d.ts +4 -0
- package/esm/src/types/mod.d.ts.map +1 -1
- package/esm/src/types/mod.js +3 -1
- package/esm/src/types/subscriptions/requests.d.ts +2 -0
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/package.json +12 -9
- package/script/_dnt.polyfills.d.ts +20 -0
- package/script/_dnt.polyfills.d.ts.map +1 -0
- package/script/_dnt.polyfills.js +23 -0
- package/script/mod.d.ts +3 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +3 -1
- package/script/src/base.d.ts +1 -47
- package/script/src/base.d.ts.map +1 -1
- package/script/src/base.js +2 -10
- package/script/src/clients/event.d.ts +6 -4
- package/script/src/clients/event.d.ts.map +1 -1
- package/script/src/clients/event.js +58 -77
- package/script/src/clients/public.d.ts +26 -5
- package/script/src/clients/public.d.ts.map +1 -1
- package/script/src/clients/public.js +29 -41
- package/script/src/clients/wallet.d.ts +200 -26
- package/script/src/clients/wallet.d.ts.map +1 -1
- package/script/src/clients/wallet.js +305 -283
- package/script/src/signing.d.ts +80 -5
- package/script/src/signing.d.ts.map +1 -1
- package/script/src/signing.js +148 -58
- package/script/src/transports/base.d.ts +49 -0
- package/script/src/transports/base.d.ts.map +1 -0
- package/script/src/transports/base.js +22 -0
- package/script/src/transports/http/http_transport.d.ts +8 -5
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +16 -63
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +36 -39
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts +18 -25
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.js +81 -180
- package/{esm/src/transports/websocket/_websocket_request_dispatcher.d.ts → script/src/transports/websocket/_websocket_async_request.d.ts} +14 -18
- package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -0
- package/script/src/transports/websocket/{_websocket_request_dispatcher.js → _websocket_async_request.js} +45 -78
- package/script/src/transports/websocket/websocket_transport.d.ts +38 -28
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +63 -78
- package/script/src/types/exchange/requests.d.ts +278 -66
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/info/assets.d.ts +0 -48
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/markets.d.ts +52 -0
- package/script/src/types/info/markets.d.ts.map +1 -0
- package/script/{deps/jsr.io/@noble/hashes/1.8.0/src/crypto.js → src/types/info/markets.js} +0 -2
- package/script/src/types/info/orders.d.ts +1 -1
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/requests.d.ts +14 -5
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/mod.d.ts +4 -0
- package/script/src/types/mod.d.ts.map +1 -1
- package/script/src/types/mod.js +25 -22
- package/script/src/types/subscriptions/requests.d.ts +2 -0
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts +0 -55
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.js +0 -66
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts +0 -2
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.js +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts +0 -53
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.js +0 -294
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts +0 -161
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.js +0 -280
- package/esm/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts +0 -55
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.js +0 -99
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts +0 -2
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts +0 -53
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.js +0 -309
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts +0 -161
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.js +0 -322
- package/script/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +0 -1
|
@@ -4,19 +4,21 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports", "
|
|
7
|
+
define(["require", "exports", "../base.js"], factory);
|
|
8
8
|
}
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.HttpTransport = exports.HttpRequestError = void 0;
|
|
13
|
-
const base_js_1 = require("
|
|
13
|
+
const base_js_1 = require("../base.js");
|
|
14
14
|
/**
|
|
15
15
|
* Error thrown when an HTTP response is deemed invalid:
|
|
16
16
|
* - Non-200 status code
|
|
17
17
|
* - Unexpected content type
|
|
18
18
|
*/
|
|
19
19
|
class HttpRequestError extends base_js_1.TransportError {
|
|
20
|
+
response;
|
|
21
|
+
responseBody;
|
|
20
22
|
/**
|
|
21
23
|
* Creates a new HTTP request error.
|
|
22
24
|
* @param response - The failed HTTP response.
|
|
@@ -27,65 +29,25 @@
|
|
|
27
29
|
if (responseBody)
|
|
28
30
|
message += `, body "${responseBody}"`;
|
|
29
31
|
super(message);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
configurable: true,
|
|
33
|
-
writable: true,
|
|
34
|
-
value: response
|
|
35
|
-
});
|
|
36
|
-
Object.defineProperty(this, "responseBody", {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
configurable: true,
|
|
39
|
-
writable: true,
|
|
40
|
-
value: responseBody
|
|
41
|
-
});
|
|
32
|
+
this.response = response;
|
|
33
|
+
this.responseBody = responseBody;
|
|
42
34
|
this.name = "HttpRequestError";
|
|
43
35
|
}
|
|
44
36
|
}
|
|
45
37
|
exports.HttpRequestError = HttpRequestError;
|
|
46
38
|
/** HTTP implementation of the REST transport interface. */
|
|
47
39
|
class HttpTransport {
|
|
40
|
+
isTestnet;
|
|
41
|
+
timeout;
|
|
42
|
+
server;
|
|
43
|
+
fetchOptions;
|
|
44
|
+
onRequest;
|
|
45
|
+
onResponse;
|
|
48
46
|
/**
|
|
49
47
|
* Creates a new HTTP transport instance.
|
|
50
48
|
* @param options - Configuration options for the HTTP transport layer.
|
|
51
49
|
*/
|
|
52
50
|
constructor(options) {
|
|
53
|
-
Object.defineProperty(this, "isTestnet", {
|
|
54
|
-
enumerable: true,
|
|
55
|
-
configurable: true,
|
|
56
|
-
writable: true,
|
|
57
|
-
value: void 0
|
|
58
|
-
});
|
|
59
|
-
Object.defineProperty(this, "timeout", {
|
|
60
|
-
enumerable: true,
|
|
61
|
-
configurable: true,
|
|
62
|
-
writable: true,
|
|
63
|
-
value: void 0
|
|
64
|
-
});
|
|
65
|
-
Object.defineProperty(this, "server", {
|
|
66
|
-
enumerable: true,
|
|
67
|
-
configurable: true,
|
|
68
|
-
writable: true,
|
|
69
|
-
value: void 0
|
|
70
|
-
});
|
|
71
|
-
Object.defineProperty(this, "fetchOptions", {
|
|
72
|
-
enumerable: true,
|
|
73
|
-
configurable: true,
|
|
74
|
-
writable: true,
|
|
75
|
-
value: void 0
|
|
76
|
-
});
|
|
77
|
-
Object.defineProperty(this, "onRequest", {
|
|
78
|
-
enumerable: true,
|
|
79
|
-
configurable: true,
|
|
80
|
-
writable: true,
|
|
81
|
-
value: void 0
|
|
82
|
-
});
|
|
83
|
-
Object.defineProperty(this, "onResponse", {
|
|
84
|
-
enumerable: true,
|
|
85
|
-
configurable: true,
|
|
86
|
-
writable: true,
|
|
87
|
-
value: void 0
|
|
88
|
-
});
|
|
89
51
|
this.isTestnet = options?.isTestnet ?? false;
|
|
90
52
|
this.timeout = options?.timeout === undefined ? 10_000 : options.timeout;
|
|
91
53
|
this.server = {
|
|
@@ -118,7 +80,6 @@
|
|
|
118
80
|
body: JSON.stringify(payload),
|
|
119
81
|
headers: {
|
|
120
82
|
"Accept-Encoding": "gzip, deflate, br, zstd",
|
|
121
|
-
"Connection": "keep-alive",
|
|
122
83
|
"Content-Type": "application/json",
|
|
123
84
|
},
|
|
124
85
|
keepalive: true,
|
|
@@ -157,29 +118,21 @@
|
|
|
157
118
|
}
|
|
158
119
|
}
|
|
159
120
|
exports.HttpTransport = HttpTransport;
|
|
160
|
-
/**
|
|
161
|
-
* Merges multiple `HeadersInit` objects into one.
|
|
162
|
-
* @param inits - A list of `HeadersInit` objects to merge.
|
|
163
|
-
* @returns A new `Headers` object that contains all headers from the input objects.
|
|
164
|
-
*/
|
|
121
|
+
/** Merges multiple {@linkcode HeadersInit} into one {@linkcode Headers}. */
|
|
165
122
|
function mergeHeadersInit(...inits) {
|
|
166
123
|
if (inits.length === 0 || inits.length === 1) {
|
|
167
124
|
return new Headers(inits[0]);
|
|
168
125
|
}
|
|
169
126
|
const merged = new Headers();
|
|
170
127
|
for (const headers of inits) {
|
|
171
|
-
const
|
|
172
|
-
for (const [key, value] of
|
|
128
|
+
const iterator = Symbol.iterator in headers ? headers : Object.entries(headers);
|
|
129
|
+
for (const [key, value] of iterator) {
|
|
173
130
|
merged.set(key, value);
|
|
174
131
|
}
|
|
175
132
|
}
|
|
176
133
|
return merged;
|
|
177
134
|
}
|
|
178
|
-
/**
|
|
179
|
-
* Merges multiple `RequestInit` objects into one.
|
|
180
|
-
* @param inits - A list of `RequestInit` objects to merge.
|
|
181
|
-
* @returns A new `RequestInit` object that contains all properties from the input objects.
|
|
182
|
-
*/
|
|
135
|
+
/** Merges multiple {@linkcode RequestInit} into one {@linkcode RequestInit}. */
|
|
183
136
|
function mergeRequestInit(...inits) {
|
|
184
137
|
const merged = inits.reduce((acc, init) => ({ ...acc, ...init }), {});
|
|
185
138
|
const headersList = inits.map((init) => init.headers)
|
|
@@ -1,51 +1,48 @@
|
|
|
1
1
|
import { TypedEventTarget } from "../../../deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js";
|
|
2
2
|
import type { BlockDetails, TxDetails } from "../../types/explorer/responses.js";
|
|
3
3
|
import type { BaseExchangeResponse } from "../../types/exchange/responses.js";
|
|
4
|
+
/** Response to subscribe to or unsubscribe from an event. */
|
|
5
|
+
interface SubscriptionResponse {
|
|
6
|
+
/** Type of subscription operation. */
|
|
7
|
+
method: "subscribe" | "unsubscribe";
|
|
8
|
+
/** Original subscription request. */
|
|
9
|
+
subscription: unknown;
|
|
10
|
+
}
|
|
11
|
+
/** Response to post request. */
|
|
12
|
+
interface PostResponse {
|
|
13
|
+
/** Unique request identifier. */
|
|
14
|
+
id: number;
|
|
15
|
+
/** Server response. */
|
|
16
|
+
response:
|
|
17
|
+
/** Response containing requested information. */
|
|
18
|
+
{
|
|
19
|
+
/** Indicates that this is an informational response. */
|
|
20
|
+
type: "info";
|
|
21
|
+
/** Contains the information data. */
|
|
22
|
+
payload: {
|
|
23
|
+
/** Type of information being returned. */
|
|
24
|
+
type: string;
|
|
25
|
+
/** Information specific data. */
|
|
26
|
+
data: unknown;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Response containing action result. */
|
|
30
|
+
| {
|
|
31
|
+
/** Indicates that this is an action response. */
|
|
32
|
+
type: "action";
|
|
33
|
+
/** Action result. */
|
|
34
|
+
payload: BaseExchangeResponse;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
4
37
|
/** Base system events and dynamic channel events for Hyperliquid WebSocket API. */
|
|
5
38
|
interface HyperliquidEventMap {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/** Type of subscription operation. */
|
|
9
|
-
method: "subscribe" | "unsubscribe";
|
|
10
|
-
/** Original subscription request. */
|
|
11
|
-
subscription: unknown;
|
|
12
|
-
}>;
|
|
13
|
-
/** Response to post request event. */
|
|
14
|
-
post: CustomEvent<{
|
|
15
|
-
/** Unique request identifier. */
|
|
16
|
-
id: number;
|
|
17
|
-
/** Server response. */
|
|
18
|
-
response:
|
|
19
|
-
/** Response containing requested information. */
|
|
20
|
-
{
|
|
21
|
-
/** Indicates that this is an informational response. */
|
|
22
|
-
type: "info";
|
|
23
|
-
/** Contains the information data. */
|
|
24
|
-
payload: {
|
|
25
|
-
/** Type of information being returned. */
|
|
26
|
-
type: string;
|
|
27
|
-
/** Information specific data. */
|
|
28
|
-
data: unknown;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
/** Response containing action result. */
|
|
32
|
-
| {
|
|
33
|
-
/** Indicates that this is an action response. */
|
|
34
|
-
type: "action";
|
|
35
|
-
/** Action result. */
|
|
36
|
-
payload: BaseExchangeResponse;
|
|
37
|
-
};
|
|
38
|
-
}>;
|
|
39
|
-
/** Error response for message event. */
|
|
39
|
+
subscriptionResponse: CustomEvent<SubscriptionResponse>;
|
|
40
|
+
post: CustomEvent<PostResponse>;
|
|
40
41
|
error: CustomEvent<string>;
|
|
41
|
-
/** Pong response event. */
|
|
42
42
|
pong: CustomEvent<undefined>;
|
|
43
|
-
/** Block explorer update event. */
|
|
44
43
|
_explorerBlock: CustomEvent<Omit<BlockDetails, "txs">[]>;
|
|
45
|
-
/** Transaction explorer update event. */
|
|
46
44
|
_explorerTxs: CustomEvent<TxDetails[]>;
|
|
47
|
-
|
|
48
|
-
[key: string]: CustomEvent<unknown>;
|
|
45
|
+
[key: string]: CustomEvent<any>;
|
|
49
46
|
}
|
|
50
47
|
/** Listens for WebSocket messages and sends them as Hyperliquid typed events. */
|
|
51
48
|
export declare class HyperliquidEventTarget extends TypedEventTarget<HyperliquidEventMap> {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_hyperliquid_event_target.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_hyperliquid_event_target.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oEAAoE,CAAC;AACtG,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AAU9E,
|
|
1
|
+
{"version":3,"file":"_hyperliquid_event_target.d.ts","sourceRoot":"","sources":["../../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AAC,CAAC;CACnC;AAED,iFAAiF;AACjF,qBAAa,sBAAuB,SAAQ,gBAAgB,CAAC,mBAAmB,CAAC;gBACjE,MAAM,EAAE,SAAS;CAiBhC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { MaybePromise } from "../../base.js";
|
|
2
|
+
import { TransportError } from "../base.js";
|
|
2
3
|
/** Configuration options for the `ReconnectingWebSocket`. */
|
|
3
4
|
export interface ReconnectingWebSocketOptions {
|
|
4
5
|
/**
|
|
@@ -34,46 +35,38 @@ export interface ReconnectingWebSocketOptions {
|
|
|
34
35
|
}
|
|
35
36
|
/** Message buffer strategy interface. */
|
|
36
37
|
export interface MessageBufferStrategy {
|
|
37
|
-
/** Array of buffered messages. */
|
|
38
|
-
messages: (string | ArrayBufferLike | Blob | ArrayBufferView)[];
|
|
39
|
-
/**
|
|
40
|
-
* Add a message to the buffer.
|
|
41
|
-
* @param data - The message to buffer.
|
|
42
|
-
*/
|
|
43
38
|
push(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
|
|
44
|
-
|
|
45
|
-
* Get and remove the next message from the buffer.
|
|
46
|
-
* @returns The next message or `undefined` if no more messages are available.
|
|
47
|
-
*/
|
|
48
|
-
shift(): (string | ArrayBufferLike | Blob | ArrayBufferView) | undefined;
|
|
49
|
-
/** Clear all buffered messages. */
|
|
50
|
-
clear(): void;
|
|
39
|
+
[Symbol.iterator](): Iterator<string | ArrayBufferLike | Blob | ArrayBufferView>;
|
|
51
40
|
}
|
|
52
41
|
/** Error thrown when reconnection problems occur. */
|
|
53
42
|
export declare class ReconnectingWebSocketError extends TransportError {
|
|
54
43
|
code: "RECONNECTION_LIMIT_REACHED" | "RECONNECTION_STOPPED_BY_USER" | "USER_INITIATED_CLOSE" | "UNKNOWN_ERROR";
|
|
55
|
-
|
|
56
|
-
constructor(code: "RECONNECTION_LIMIT_REACHED" | "RECONNECTION_STOPPED_BY_USER" | "USER_INITIATED_CLOSE" | "UNKNOWN_ERROR", originalError?: unknown);
|
|
44
|
+
constructor(code: "RECONNECTION_LIMIT_REACHED" | "RECONNECTION_STOPPED_BY_USER" | "USER_INITIATED_CLOSE" | "UNKNOWN_ERROR", cause?: unknown);
|
|
57
45
|
}
|
|
58
46
|
/**
|
|
59
47
|
* A WebSocket that automatically reconnects when disconnected.
|
|
60
48
|
* Fully compatible with standard WebSocket API.
|
|
61
49
|
*/
|
|
62
50
|
export declare class ReconnectingWebSocket implements WebSocket {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
51
|
+
protected _socket: WebSocket;
|
|
52
|
+
protected _protocols?: string | string[];
|
|
53
|
+
protected _listeners: {
|
|
54
|
+
type: string;
|
|
55
|
+
listener: EventListenerOrEventListenerObject;
|
|
56
|
+
options?: boolean | AddEventListenerOptions;
|
|
57
|
+
listenerProxy: EventListenerOrEventListenerObject;
|
|
58
|
+
}[];
|
|
59
|
+
protected _attempt: number;
|
|
67
60
|
reconnectOptions: Required<ReconnectingWebSocketOptions>;
|
|
68
61
|
readonly reconnectAbortController: AbortController;
|
|
69
62
|
constructor(url: string | URL, protocols?: string | string[], options?: ReconnectingWebSocketOptions);
|
|
70
|
-
|
|
63
|
+
protected _createSocket(url: string | URL, protocols?: string | string[]): WebSocket;
|
|
71
64
|
/** Initializes the internal event listeners for the socket. */
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
65
|
+
protected _setupEventListeners(): void;
|
|
66
|
+
protected _open: () => void;
|
|
67
|
+
protected _close: (event: CloseEvent) => Promise<void>;
|
|
75
68
|
/** Clean up internal resources. */
|
|
76
|
-
|
|
69
|
+
protected _cleanup(code: ConstructorParameters<typeof ReconnectingWebSocketError>[0], cause?: unknown): void;
|
|
77
70
|
get url(): string;
|
|
78
71
|
get readyState(): number;
|
|
79
72
|
get bufferedAmount(): number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_reconnecting_websocket.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_reconnecting_websocket.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"_reconnecting_websocket.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_reconnecting_websocket.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,6DAA6D;AAC7D,MAAM,WAAW,4BAA4B;IACzC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAElC;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;IAE5F;;;;;OAKG;IACH,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,KAAK,YAAY,CAAC,OAAO,CAAC,CAAC;IAEpF;;;OAGG;IACH,aAAa,CAAC,EAAE,qBAAqB,CAAC;CACzC;AAED,yCAAyC;AACzC,MAAM,WAAW,qBAAqB;IAClC,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,GAAG,eAAe,GAAG,IAAI,CAAC;IACpE,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,eAAe,GAAG,IAAI,GAAG,eAAe,CAAC,CAAC;CACpF;AAiBD,qDAAqD;AACrD,qBAAa,0BAA2B,SAAQ,cAAc;IAE/C,IAAI,EACL,4BAA4B,GAC5B,8BAA8B,GAC9B,sBAAsB,GACtB,eAAe;gBAJd,IAAI,EACL,4BAA4B,GAC5B,8BAA8B,GAC9B,sBAAsB,GACtB,eAAe,EACrB,KAAK,CAAC,EAAE,OAA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I,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,KAAK,KAAK,GAAG,CAAC,GAAG,IAAI,EAE7D;IAED;;OAEG;IACH,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,WAAW,GAAE,OAAc,GAAG,IAAI;IAKxE;;OAEG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,GAAG,eAAe,GAAG,IAAI;IAQnE,gBAAgB,CAAC,CAAC,SAAS,MAAM,iBAAiB,EAC9C,IAAI,EAAE,CAAC,EACP,QAAQ,EACF,CAAC,CAAC,IAAI,EAAE,qBAAqB,EAAE,EAAE,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,GAChE;QAAE,WAAW,EAAE,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,GAAG,CAAA;KAAE,EAC3D,OAAO,CAAC,EAAE,OAAO,GAAG,uBAAuB,GAC5C,IAAI;IA8CP,mBAAmB,CAAC,CAAC,SAAS,MAAM,iBAAiB,EACjD,IAAI,EAAE,CAAC,EACP,QAAQ,EACF,CAAC,CAAC,IAAI,EAAE,qBAAqB,EAAE,EAAE,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,GAChE;QAAE,WAAW,EAAE,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,GAAG,CAAA;KAAE,EAC3D,OAAO,CAAC,EAAE,OAAO,GAAG,oBAAoB,GACzC,IAAI;IAkBP,aAAa,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO;CAGvC"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports", "../../../deps/jsr.io/@std/async/1.0.13/delay.js", "
|
|
7
|
+
define(["require", "exports", "../../../deps/jsr.io/@std/async/1.0.13/delay.js", "../base.js"], factory);
|
|
8
8
|
}
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
@@ -12,44 +12,27 @@
|
|
|
12
12
|
exports.ReconnectingWebSocket = exports.ReconnectingWebSocketError = void 0;
|
|
13
13
|
// deno-lint-ignore-file no-explicit-any
|
|
14
14
|
const delay_js_1 = require("../../../deps/jsr.io/@std/async/1.0.13/delay.js");
|
|
15
|
-
const base_js_1 = require("
|
|
15
|
+
const base_js_1 = require("../base.js");
|
|
16
16
|
/** Simple FIFO (First In, First Out) buffer implementation. */
|
|
17
17
|
class FIFOMessageBuffer {
|
|
18
|
-
|
|
19
|
-
Object.defineProperty(this, "messages", {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
configurable: true,
|
|
22
|
-
writable: true,
|
|
23
|
-
value: []
|
|
24
|
-
});
|
|
25
|
-
}
|
|
18
|
+
queue = [];
|
|
26
19
|
push(data) {
|
|
27
|
-
this.
|
|
28
|
-
}
|
|
29
|
-
shift() {
|
|
30
|
-
return this.messages.shift();
|
|
20
|
+
this.queue.push(data);
|
|
31
21
|
}
|
|
32
|
-
|
|
33
|
-
this.
|
|
22
|
+
*[Symbol.iterator]() {
|
|
23
|
+
while (this.queue.length > 0) {
|
|
24
|
+
yield this.queue.shift();
|
|
25
|
+
}
|
|
34
26
|
}
|
|
35
27
|
}
|
|
36
28
|
/** Error thrown when reconnection problems occur. */
|
|
37
29
|
class ReconnectingWebSocketError extends base_js_1.TransportError {
|
|
38
|
-
|
|
30
|
+
code;
|
|
31
|
+
constructor(code, cause) {
|
|
39
32
|
super(`Error when reconnecting WebSocket: ${code}`);
|
|
40
|
-
|
|
41
|
-
enumerable: true,
|
|
42
|
-
configurable: true,
|
|
43
|
-
writable: true,
|
|
44
|
-
value: code
|
|
45
|
-
});
|
|
46
|
-
Object.defineProperty(this, "originalError", {
|
|
47
|
-
enumerable: true,
|
|
48
|
-
configurable: true,
|
|
49
|
-
writable: true,
|
|
50
|
-
value: originalError
|
|
51
|
-
});
|
|
33
|
+
this.code = code;
|
|
52
34
|
this.name = "ReconnectingWebSocketError";
|
|
35
|
+
this.cause = cause;
|
|
53
36
|
}
|
|
54
37
|
}
|
|
55
38
|
exports.ReconnectingWebSocketError = ReconnectingWebSocketError;
|
|
@@ -58,131 +41,13 @@
|
|
|
58
41
|
* Fully compatible with standard WebSocket API.
|
|
59
42
|
*/
|
|
60
43
|
class ReconnectingWebSocket {
|
|
44
|
+
_socket;
|
|
45
|
+
_protocols;
|
|
46
|
+
_listeners = [];
|
|
47
|
+
_attempt = 0;
|
|
48
|
+
reconnectOptions;
|
|
49
|
+
reconnectAbortController = new AbortController();
|
|
61
50
|
constructor(url, protocols, options) {
|
|
62
|
-
Object.defineProperty(this, "_socket", {
|
|
63
|
-
enumerable: true,
|
|
64
|
-
configurable: true,
|
|
65
|
-
writable: true,
|
|
66
|
-
value: void 0
|
|
67
|
-
});
|
|
68
|
-
Object.defineProperty(this, "_protocols", {
|
|
69
|
-
enumerable: true,
|
|
70
|
-
configurable: true,
|
|
71
|
-
writable: true,
|
|
72
|
-
value: void 0
|
|
73
|
-
});
|
|
74
|
-
Object.defineProperty(this, "_listeners", {
|
|
75
|
-
enumerable: true,
|
|
76
|
-
configurable: true,
|
|
77
|
-
writable: true,
|
|
78
|
-
value: []
|
|
79
|
-
});
|
|
80
|
-
Object.defineProperty(this, "_attempt", {
|
|
81
|
-
enumerable: true,
|
|
82
|
-
configurable: true,
|
|
83
|
-
writable: true,
|
|
84
|
-
value: 0
|
|
85
|
-
});
|
|
86
|
-
Object.defineProperty(this, "reconnectOptions", {
|
|
87
|
-
enumerable: true,
|
|
88
|
-
configurable: true,
|
|
89
|
-
writable: true,
|
|
90
|
-
value: void 0
|
|
91
|
-
});
|
|
92
|
-
Object.defineProperty(this, "reconnectAbortController", {
|
|
93
|
-
enumerable: true,
|
|
94
|
-
configurable: true,
|
|
95
|
-
writable: true,
|
|
96
|
-
value: new AbortController()
|
|
97
|
-
});
|
|
98
|
-
Object.defineProperty(this, "_open", {
|
|
99
|
-
enumerable: true,
|
|
100
|
-
configurable: true,
|
|
101
|
-
writable: true,
|
|
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
|
-
}
|
|
111
|
-
});
|
|
112
|
-
Object.defineProperty(this, "_close", {
|
|
113
|
-
enumerable: true,
|
|
114
|
-
configurable: true,
|
|
115
|
-
writable: true,
|
|
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
|
-
}
|
|
161
|
-
});
|
|
162
|
-
Object.defineProperty(this, "CONNECTING", {
|
|
163
|
-
enumerable: true,
|
|
164
|
-
configurable: true,
|
|
165
|
-
writable: true,
|
|
166
|
-
value: 0
|
|
167
|
-
});
|
|
168
|
-
Object.defineProperty(this, "OPEN", {
|
|
169
|
-
enumerable: true,
|
|
170
|
-
configurable: true,
|
|
171
|
-
writable: true,
|
|
172
|
-
value: 1
|
|
173
|
-
});
|
|
174
|
-
Object.defineProperty(this, "CLOSING", {
|
|
175
|
-
enumerable: true,
|
|
176
|
-
configurable: true,
|
|
177
|
-
writable: true,
|
|
178
|
-
value: 2
|
|
179
|
-
});
|
|
180
|
-
Object.defineProperty(this, "CLOSED", {
|
|
181
|
-
enumerable: true,
|
|
182
|
-
configurable: true,
|
|
183
|
-
writable: true,
|
|
184
|
-
value: 3
|
|
185
|
-
});
|
|
186
51
|
this.reconnectOptions = {
|
|
187
52
|
maxRetries: options?.maxRetries ?? 3,
|
|
188
53
|
connectionTimeout: options?.connectionTimeout === undefined ? 10_000 : options.connectionTimeout,
|
|
@@ -220,10 +85,62 @@
|
|
|
220
85
|
this._socket.addEventListener("open", this._open, { once: true });
|
|
221
86
|
this._socket.addEventListener("close", this._close, { once: true });
|
|
222
87
|
}
|
|
88
|
+
_open = () => {
|
|
89
|
+
// Reset the attempt counter
|
|
90
|
+
this._attempt = 0;
|
|
91
|
+
// Send all buffered messages
|
|
92
|
+
for (const message of this.reconnectOptions.messageBuffer) {
|
|
93
|
+
this._socket.send(message);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
_close = async (event) => {
|
|
97
|
+
try {
|
|
98
|
+
// If the event was triggered but the socket is not closing, ignore it
|
|
99
|
+
if (this._socket.readyState !== ReconnectingWebSocket.CLOSING &&
|
|
100
|
+
this._socket.readyState !== ReconnectingWebSocket.CLOSED)
|
|
101
|
+
return;
|
|
102
|
+
// If the instance is terminated, do not attempt to reconnect
|
|
103
|
+
if (this.reconnectAbortController.signal.aborted)
|
|
104
|
+
return;
|
|
105
|
+
// Check if reconnection should be attempted
|
|
106
|
+
if (++this._attempt > this.reconnectOptions.maxRetries) {
|
|
107
|
+
this._cleanup("RECONNECTION_LIMIT_REACHED");
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const userDecision = await this.reconnectOptions.shouldReconnect(event, this.reconnectAbortController.signal);
|
|
111
|
+
if (this.reconnectAbortController.signal.aborted)
|
|
112
|
+
return;
|
|
113
|
+
if (!userDecision) {
|
|
114
|
+
this._cleanup("RECONNECTION_STOPPED_BY_USER");
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// Delay before reconnecting
|
|
118
|
+
const reconnectDelay = typeof this.reconnectOptions.connectionDelay === "number"
|
|
119
|
+
? this.reconnectOptions.connectionDelay
|
|
120
|
+
: await this.reconnectOptions.connectionDelay(this._attempt, this.reconnectAbortController.signal);
|
|
121
|
+
if (this.reconnectAbortController.signal.aborted)
|
|
122
|
+
return;
|
|
123
|
+
await (0, delay_js_1.delay)(reconnectDelay, { signal: this.reconnectAbortController.signal });
|
|
124
|
+
// Create a new WebSocket instance
|
|
125
|
+
const { onclose, onerror, onmessage, onopen } = this._socket;
|
|
126
|
+
this._socket = this._createSocket(this._socket.url, this._protocols);
|
|
127
|
+
// Reconnect all listeners
|
|
128
|
+
this._setupEventListeners();
|
|
129
|
+
this._listeners.forEach(({ type, listenerProxy, options }) => {
|
|
130
|
+
this._socket.addEventListener(type, listenerProxy, options);
|
|
131
|
+
});
|
|
132
|
+
this._socket.onclose = onclose;
|
|
133
|
+
this._socket.onerror = onerror;
|
|
134
|
+
this._socket.onmessage = onmessage;
|
|
135
|
+
this._socket.onopen = onopen;
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
this._cleanup("UNKNOWN_ERROR", error);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
223
141
|
/** Clean up internal resources. */
|
|
224
|
-
_cleanup(code,
|
|
225
|
-
this.reconnectAbortController.abort(new ReconnectingWebSocketError(code,
|
|
226
|
-
this.reconnectOptions.messageBuffer.clear();
|
|
142
|
+
_cleanup(code, cause) {
|
|
143
|
+
this.reconnectAbortController.abort(new ReconnectingWebSocketError(code, cause));
|
|
227
144
|
this._listeners = [];
|
|
228
145
|
this._socket.close();
|
|
229
146
|
}
|
|
@@ -249,6 +166,14 @@
|
|
|
249
166
|
set binaryType(value) {
|
|
250
167
|
this._socket.binaryType = value;
|
|
251
168
|
}
|
|
169
|
+
CONNECTING = 0;
|
|
170
|
+
OPEN = 1;
|
|
171
|
+
CLOSING = 2;
|
|
172
|
+
CLOSED = 3;
|
|
173
|
+
static CONNECTING = 0;
|
|
174
|
+
static OPEN = 1;
|
|
175
|
+
static CLOSING = 2;
|
|
176
|
+
static CLOSED = 3;
|
|
252
177
|
get onclose() {
|
|
253
178
|
return this._socket.onclose;
|
|
254
179
|
}
|
|
@@ -351,30 +276,6 @@
|
|
|
351
276
|
}
|
|
352
277
|
}
|
|
353
278
|
exports.ReconnectingWebSocket = ReconnectingWebSocket;
|
|
354
|
-
Object.defineProperty(ReconnectingWebSocket, "CONNECTING", {
|
|
355
|
-
enumerable: true,
|
|
356
|
-
configurable: true,
|
|
357
|
-
writable: true,
|
|
358
|
-
value: 0
|
|
359
|
-
});
|
|
360
|
-
Object.defineProperty(ReconnectingWebSocket, "OPEN", {
|
|
361
|
-
enumerable: true,
|
|
362
|
-
configurable: true,
|
|
363
|
-
writable: true,
|
|
364
|
-
value: 1
|
|
365
|
-
});
|
|
366
|
-
Object.defineProperty(ReconnectingWebSocket, "CLOSING", {
|
|
367
|
-
enumerable: true,
|
|
368
|
-
configurable: true,
|
|
369
|
-
writable: true,
|
|
370
|
-
value: 2
|
|
371
|
-
});
|
|
372
|
-
Object.defineProperty(ReconnectingWebSocket, "CLOSED", {
|
|
373
|
-
enumerable: true,
|
|
374
|
-
configurable: true,
|
|
375
|
-
writable: true,
|
|
376
|
-
value: 3
|
|
377
|
-
});
|
|
378
279
|
/** Check if two event listeners are the same (just like EventTarget). */
|
|
379
280
|
function listenersMatch(a, b) {
|
|
380
281
|
// EventTarget only compares capture in options, even if one is an object and the other is boolean
|