@binance/common 2.0.0 → 2.1.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/dist/index.d.mts +666 -658
- package/dist/index.d.ts +666 -658
- package/dist/index.js +1675 -1684
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1562 -1562
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,1766 +1,1757 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
8
|
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
29
22
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL: () => DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL,
|
|
45
|
-
DERIVATIVES_TRADING_COIN_FUTURES_WS_API_PROD_URL: () => DERIVATIVES_TRADING_COIN_FUTURES_WS_API_PROD_URL,
|
|
46
|
-
DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL: () => DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL,
|
|
47
|
-
DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL: () => DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL,
|
|
48
|
-
DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL: () => DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL,
|
|
49
|
-
DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL: () => DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL,
|
|
50
|
-
DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL: () => DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL,
|
|
51
|
-
DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL: () => DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL,
|
|
52
|
-
DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL: () => DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL,
|
|
53
|
-
DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL: () => DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL,
|
|
54
|
-
DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL: () => DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL,
|
|
55
|
-
DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL: () => DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL,
|
|
56
|
-
DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL: () => DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL,
|
|
57
|
-
DERIVATIVES_TRADING_USDS_FUTURES_REST_API_PROD_URL: () => DERIVATIVES_TRADING_USDS_FUTURES_REST_API_PROD_URL,
|
|
58
|
-
DERIVATIVES_TRADING_USDS_FUTURES_REST_API_TESTNET_URL: () => DERIVATIVES_TRADING_USDS_FUTURES_REST_API_TESTNET_URL,
|
|
59
|
-
DERIVATIVES_TRADING_USDS_FUTURES_WS_API_PROD_URL: () => DERIVATIVES_TRADING_USDS_FUTURES_WS_API_PROD_URL,
|
|
60
|
-
DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL: () => DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL,
|
|
61
|
-
DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL: () => DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL,
|
|
62
|
-
DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL: () => DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL,
|
|
63
|
-
DUAL_INVESTMENT_REST_API_PROD_URL: () => DUAL_INVESTMENT_REST_API_PROD_URL,
|
|
64
|
-
FIAT_REST_API_PROD_URL: () => FIAT_REST_API_PROD_URL,
|
|
65
|
-
ForbiddenError: () => ForbiddenError,
|
|
66
|
-
GIFT_CARD_REST_API_PROD_URL: () => GIFT_CARD_REST_API_PROD_URL,
|
|
67
|
-
LogLevel: () => LogLevel,
|
|
68
|
-
Logger: () => Logger,
|
|
69
|
-
MARGIN_TRADING_REST_API_PROD_URL: () => MARGIN_TRADING_REST_API_PROD_URL,
|
|
70
|
-
MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL: () => MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL,
|
|
71
|
-
MARGIN_TRADING_WS_STREAMS_PROD_URL: () => MARGIN_TRADING_WS_STREAMS_PROD_URL,
|
|
72
|
-
MINING_REST_API_PROD_URL: () => MINING_REST_API_PROD_URL,
|
|
73
|
-
NFT_REST_API_PROD_URL: () => NFT_REST_API_PROD_URL,
|
|
74
|
-
NetworkError: () => NetworkError,
|
|
75
|
-
NotFoundError: () => NotFoundError,
|
|
76
|
-
PAY_REST_API_PROD_URL: () => PAY_REST_API_PROD_URL,
|
|
77
|
-
REBATE_REST_API_PROD_URL: () => REBATE_REST_API_PROD_URL,
|
|
78
|
-
RateLimitBanError: () => RateLimitBanError,
|
|
79
|
-
RequiredError: () => RequiredError,
|
|
80
|
-
SIMPLE_EARN_REST_API_PROD_URL: () => SIMPLE_EARN_REST_API_PROD_URL,
|
|
81
|
-
SPOT_REST_API_MARKET_URL: () => SPOT_REST_API_MARKET_URL,
|
|
82
|
-
SPOT_REST_API_PROD_URL: () => SPOT_REST_API_PROD_URL,
|
|
83
|
-
SPOT_REST_API_TESTNET_URL: () => SPOT_REST_API_TESTNET_URL,
|
|
84
|
-
SPOT_WS_API_PROD_URL: () => SPOT_WS_API_PROD_URL,
|
|
85
|
-
SPOT_WS_API_TESTNET_URL: () => SPOT_WS_API_TESTNET_URL,
|
|
86
|
-
SPOT_WS_STREAMS_MARKET_URL: () => SPOT_WS_STREAMS_MARKET_URL,
|
|
87
|
-
SPOT_WS_STREAMS_PROD_URL: () => SPOT_WS_STREAMS_PROD_URL,
|
|
88
|
-
SPOT_WS_STREAMS_TESTNET_URL: () => SPOT_WS_STREAMS_TESTNET_URL,
|
|
89
|
-
STAKING_REST_API_PROD_URL: () => STAKING_REST_API_PROD_URL,
|
|
90
|
-
SUB_ACCOUNT_REST_API_PROD_URL: () => SUB_ACCOUNT_REST_API_PROD_URL,
|
|
91
|
-
ServerError: () => ServerError,
|
|
92
|
-
TimeUnit: () => TimeUnit,
|
|
93
|
-
TooManyRequestsError: () => TooManyRequestsError,
|
|
94
|
-
UnauthorizedError: () => UnauthorizedError,
|
|
95
|
-
VIP_LOAN_REST_API_PROD_URL: () => VIP_LOAN_REST_API_PROD_URL,
|
|
96
|
-
WALLET_REST_API_PROD_URL: () => WALLET_REST_API_PROD_URL,
|
|
97
|
-
WebsocketAPIBase: () => WebsocketAPIBase,
|
|
98
|
-
WebsocketCommon: () => WebsocketCommon,
|
|
99
|
-
WebsocketEventEmitter: () => WebsocketEventEmitter,
|
|
100
|
-
WebsocketStreamsBase: () => WebsocketStreamsBase,
|
|
101
|
-
assertParamExists: () => assertParamExists,
|
|
102
|
-
buildQueryString: () => buildQueryString,
|
|
103
|
-
buildUserAgent: () => buildUserAgent,
|
|
104
|
-
buildWebsocketAPIMessage: () => buildWebsocketAPIMessage,
|
|
105
|
-
clearSignerCache: () => clearSignerCache,
|
|
106
|
-
createStreamHandler: () => createStreamHandler,
|
|
107
|
-
delay: () => delay,
|
|
108
|
-
getSignature: () => getSignature,
|
|
109
|
-
getTimestamp: () => getTimestamp,
|
|
110
|
-
httpRequestFunction: () => httpRequestFunction,
|
|
111
|
-
normalizeScientificNumbers: () => normalizeScientificNumbers,
|
|
112
|
-
parseCustomHeaders: () => parseCustomHeaders,
|
|
113
|
-
parseRateLimitHeaders: () => parseRateLimitHeaders,
|
|
114
|
-
randomString: () => randomString,
|
|
115
|
-
removeEmptyValue: () => removeEmptyValue,
|
|
116
|
-
replaceWebsocketStreamsPlaceholders: () => replaceWebsocketStreamsPlaceholders,
|
|
117
|
-
sanitizeHeaderValue: () => sanitizeHeaderValue,
|
|
118
|
-
sendRequest: () => sendRequest,
|
|
119
|
-
setSearchParams: () => setSearchParams,
|
|
120
|
-
shouldRetryRequest: () => shouldRetryRequest,
|
|
121
|
-
sortObject: () => sortObject,
|
|
122
|
-
toPathString: () => toPathString,
|
|
123
|
-
validateTimeUnit: () => validateTimeUnit
|
|
124
|
-
});
|
|
125
|
-
module.exports = __toCommonJS(index_exports);
|
|
23
|
+
//#endregion
|
|
24
|
+
let crypto = require("crypto");
|
|
25
|
+
crypto = __toESM(crypto);
|
|
26
|
+
let fs = require("fs");
|
|
27
|
+
fs = __toESM(fs);
|
|
28
|
+
let https = require("https");
|
|
29
|
+
https = __toESM(https);
|
|
30
|
+
let json_with_bigint = require("json-with-bigint");
|
|
31
|
+
let os = require("os");
|
|
32
|
+
let axios = require("axios");
|
|
33
|
+
axios = __toESM(axios);
|
|
34
|
+
let events = require("events");
|
|
35
|
+
let ws = require("ws");
|
|
36
|
+
ws = __toESM(ws);
|
|
126
37
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
38
|
+
//#region src/utils.ts
|
|
39
|
+
/**
|
|
40
|
+
* A weak cache for storing RequestSigner instances based on configuration parameters.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Uses a WeakMap to cache and reuse RequestSigner instances for configurations with
|
|
44
|
+
* apiSecret, privateKey, and privateKeyPassphrase, allowing efficient memory management.
|
|
45
|
+
*/
|
|
46
|
+
let signerCache = /* @__PURE__ */ new WeakMap();
|
|
47
|
+
/**
|
|
48
|
+
* Represents a request signer for generating signatures using HMAC-SHA256 or asymmetric key signing.
|
|
49
|
+
*
|
|
50
|
+
* Supports two signing methods:
|
|
51
|
+
* 1. HMAC-SHA256 using an API secret
|
|
52
|
+
* 2. Asymmetric signing using RSA or ED25519 private keys
|
|
53
|
+
*
|
|
54
|
+
* @throws {Error} If neither API secret nor private key is provided, or if the private key is invalid
|
|
55
|
+
*/
|
|
135
56
|
var RequestSigner = class {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const data = Buffer.from(params);
|
|
168
|
-
if (this.keyType === "rsa")
|
|
169
|
-
return import_crypto.default.sign("RSA-SHA256", data, this.keyObject).toString("base64");
|
|
170
|
-
if (this.keyType === "ed25519")
|
|
171
|
-
return import_crypto.default.sign(null, data, this.keyObject).toString("base64");
|
|
172
|
-
throw new Error("Unsupported private key type. Must be RSA or ED25519.");
|
|
173
|
-
}
|
|
174
|
-
throw new Error("Signer is not properly initialized.");
|
|
175
|
-
}
|
|
57
|
+
constructor(configuration) {
|
|
58
|
+
if (configuration.apiSecret && !configuration.privateKey) {
|
|
59
|
+
this.apiSecret = configuration.apiSecret;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (configuration.privateKey) {
|
|
63
|
+
let privateKey = configuration.privateKey;
|
|
64
|
+
if (typeof privateKey === "string" && fs.default.existsSync(privateKey)) privateKey = fs.default.readFileSync(privateKey, "utf-8");
|
|
65
|
+
const keyInput = { key: privateKey };
|
|
66
|
+
if (configuration.privateKeyPassphrase && typeof configuration.privateKeyPassphrase === "string") keyInput.passphrase = configuration.privateKeyPassphrase;
|
|
67
|
+
try {
|
|
68
|
+
this.keyObject = crypto.default.createPrivateKey(keyInput);
|
|
69
|
+
this.keyType = this.keyObject.asymmetricKeyType;
|
|
70
|
+
} catch {
|
|
71
|
+
throw new Error("Invalid private key. Please provide a valid RSA or ED25519 private key.");
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
throw new Error("Either 'apiSecret' or 'privateKey' must be provided for signed requests.");
|
|
76
|
+
}
|
|
77
|
+
sign(queryParams, bodyParams) {
|
|
78
|
+
const params = buildQueryString(queryParams) + (bodyParams ? buildQueryString(bodyParams) : "");
|
|
79
|
+
if (this.apiSecret) return crypto.default.createHmac("sha256", this.apiSecret).update(params).digest("hex");
|
|
80
|
+
if (this.keyObject && this.keyType) {
|
|
81
|
+
const data = Buffer.from(params);
|
|
82
|
+
if (this.keyType === "rsa") return crypto.default.sign("RSA-SHA256", data, this.keyObject).toString("base64");
|
|
83
|
+
if (this.keyType === "ed25519") return crypto.default.sign(null, data, this.keyObject).toString("base64");
|
|
84
|
+
throw new Error("Unsupported private key type. Must be RSA or ED25519.");
|
|
85
|
+
}
|
|
86
|
+
throw new Error("Signer is not properly initialized.");
|
|
87
|
+
}
|
|
176
88
|
};
|
|
177
|
-
|
|
178
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Resets the signer cache to a new empty WeakMap.
|
|
91
|
+
*
|
|
92
|
+
* This function clears the existing signer cache, creating a fresh WeakMap
|
|
93
|
+
* to store RequestSigner instances associated with configuration objects.
|
|
94
|
+
*/
|
|
95
|
+
const clearSignerCache = function() {
|
|
96
|
+
signerCache = /* @__PURE__ */ new WeakMap();
|
|
179
97
|
};
|
|
98
|
+
/**
|
|
99
|
+
* Serializes a value to a string representation.
|
|
100
|
+
*
|
|
101
|
+
* - If the value is `null` or `undefined`, returns an empty string.
|
|
102
|
+
* - If the value is an array or a non-null object, returns its JSON string representation.
|
|
103
|
+
* - Otherwise, converts the value to a string using `String()`.
|
|
104
|
+
*
|
|
105
|
+
* @param value - The value to serialize.
|
|
106
|
+
* @returns The serialized string representation of the value.
|
|
107
|
+
*/
|
|
180
108
|
function serializeValue(value) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return String(value);
|
|
109
|
+
if (value === null || value === void 0) return "";
|
|
110
|
+
if (Array.isArray(value) || typeof value === "object" && value !== null) return JSON.stringify(value);
|
|
111
|
+
return String(value);
|
|
185
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Builds a URL query string from the given parameters object.
|
|
115
|
+
*
|
|
116
|
+
* Iterates over the key-value pairs in the `params` object, serializes each value,
|
|
117
|
+
* and encodes it for use in a URL. Only keys with non-null and non-undefined values
|
|
118
|
+
* are included in the resulting query string.
|
|
119
|
+
*
|
|
120
|
+
* @param params - An object containing key-value pairs to be serialized into a query string.
|
|
121
|
+
* @returns A URL-encoded query string representing the provided parameters.
|
|
122
|
+
*/
|
|
186
123
|
function buildQueryString(params) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
124
|
+
if (!params) return "";
|
|
125
|
+
const pairs = [];
|
|
126
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
127
|
+
if (value !== null && value !== void 0) {
|
|
128
|
+
const serializedValue = serializeValue(value);
|
|
129
|
+
pairs.push(`${key}=${encodeURIComponent(serializedValue)}`);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return pairs.join("&");
|
|
196
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Generates a random string of 16 hexadecimal characters.
|
|
136
|
+
*
|
|
137
|
+
* @returns A random string of 16 hexadecimal characters.
|
|
138
|
+
*/
|
|
197
139
|
function randomString() {
|
|
198
|
-
|
|
140
|
+
return crypto.default.randomBytes(16).toString("hex");
|
|
199
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Validates the provided time unit string and returns it if it is either 'MILLISECOND' or 'MICROSECOND'.
|
|
144
|
+
*
|
|
145
|
+
* @param timeUnit - The time unit string to be validated.
|
|
146
|
+
* @returns The validated time unit string, or `undefined` if the input is falsy.
|
|
147
|
+
* @throws {Error} If the time unit is not 'MILLISECOND' or 'MICROSECOND'.
|
|
148
|
+
*/
|
|
200
149
|
function validateTimeUnit(timeUnit) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
throw new Error("timeUnit must be either 'MILLISECOND' or 'MICROSECOND'");
|
|
205
|
-
}
|
|
206
|
-
return timeUnit;
|
|
150
|
+
if (!timeUnit) return;
|
|
151
|
+
else if (timeUnit !== TimeUnit.MILLISECOND && timeUnit !== TimeUnit.MICROSECOND && timeUnit !== TimeUnit.millisecond && timeUnit !== TimeUnit.microsecond) throw new Error("timeUnit must be either 'MILLISECOND' or 'MICROSECOND'");
|
|
152
|
+
return timeUnit;
|
|
207
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Delays the execution of the current function for the specified number of milliseconds.
|
|
156
|
+
*
|
|
157
|
+
* @param ms - The number of milliseconds to delay the function.
|
|
158
|
+
* @returns A Promise that resolves after the specified delay.
|
|
159
|
+
*/
|
|
208
160
|
async function delay(ms) {
|
|
209
|
-
|
|
161
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
210
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Generates the current timestamp in milliseconds.
|
|
165
|
+
*
|
|
166
|
+
* @returns The current timestamp in milliseconds.
|
|
167
|
+
*/
|
|
211
168
|
function getTimestamp() {
|
|
212
|
-
|
|
169
|
+
return Date.now();
|
|
213
170
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
171
|
+
/**
|
|
172
|
+
* Generates a signature for the given configuration and query parameters using a cached request signer.
|
|
173
|
+
*
|
|
174
|
+
* @param configuration - Configuration object containing API secret, private key, and optional passphrase.
|
|
175
|
+
* @param queryParams - The query parameters to be signed.
|
|
176
|
+
* @returns A string representing the generated signature.
|
|
177
|
+
*/
|
|
178
|
+
const getSignature = function(configuration, queryParams, bodyParams) {
|
|
179
|
+
let signer = signerCache.get(configuration);
|
|
180
|
+
if (!signer) {
|
|
181
|
+
signer = new RequestSigner(configuration);
|
|
182
|
+
signerCache.set(configuration, signer);
|
|
183
|
+
}
|
|
184
|
+
return signer.sign(queryParams, bodyParams);
|
|
221
185
|
};
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Asserts that a function parameter exists and is not null or undefined.
|
|
188
|
+
*
|
|
189
|
+
* @param functionName - The name of the function that the parameter belongs to.
|
|
190
|
+
* @param paramName - The name of the parameter to check.
|
|
191
|
+
* @param paramValue - The value of the parameter to check.
|
|
192
|
+
* @throws {RequiredError} If the parameter is null or undefined.
|
|
193
|
+
*/
|
|
194
|
+
const assertParamExists = function(functionName, paramName, paramValue) {
|
|
195
|
+
if (paramValue === null || paramValue === void 0) throw new RequiredError(paramName, `Required parameter ${paramName} was null or undefined when calling ${functionName}.`);
|
|
229
196
|
};
|
|
197
|
+
/**
|
|
198
|
+
* Sets the search parameters of a given URL object based on the provided key-value pairs.
|
|
199
|
+
* Only parameters with non-null and non-undefined values are included.
|
|
200
|
+
* Values are serialized using the `serializeValue` function before being set.
|
|
201
|
+
*
|
|
202
|
+
* @param url - The URL object whose search parameters will be updated.
|
|
203
|
+
* @param params - An object containing key-value pairs to be set as search parameters.
|
|
204
|
+
*/
|
|
230
205
|
function setSearchParams(url, params) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
206
|
+
const searchParams = new URLSearchParams();
|
|
207
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
208
|
+
if (value !== null && value !== void 0) {
|
|
209
|
+
const serializedValue = serializeValue(value);
|
|
210
|
+
searchParams.set(key, serializedValue);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
url.search = searchParams.toString();
|
|
239
214
|
}
|
|
240
|
-
|
|
241
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Converts a URL object to a full path string, including pathname, search parameters, and hash.
|
|
217
|
+
*
|
|
218
|
+
* @param url The URL object to convert to a path string.
|
|
219
|
+
* @returns A complete path string representation of the URL.
|
|
220
|
+
*/
|
|
221
|
+
const toPathString = function(url) {
|
|
222
|
+
return url.pathname + url.search + url.hash;
|
|
242
223
|
};
|
|
224
|
+
/**
|
|
225
|
+
* Normalizes scientific notation numbers in an object or array to a fixed number of decimal places.
|
|
226
|
+
*
|
|
227
|
+
* This function recursively processes objects, arrays, and numbers, converting scientific notation
|
|
228
|
+
* to a fixed decimal representation. Non-numeric values are left unchanged.
|
|
229
|
+
*
|
|
230
|
+
* @template T The type of the input object or value
|
|
231
|
+
* @param obj The object, array, or value to normalize
|
|
232
|
+
* @returns A new object or value with scientific notation numbers normalized
|
|
233
|
+
*/
|
|
243
234
|
function normalizeScientificNumbers(obj) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return (isNegative ? "-" : "") + digits.slice(0, point) + "." + digits.slice(point);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
} else {
|
|
273
|
-
return obj;
|
|
274
|
-
}
|
|
235
|
+
if (Array.isArray(obj)) return obj.map((item) => normalizeScientificNumbers(item));
|
|
236
|
+
else if (typeof obj === "object" && obj !== null) {
|
|
237
|
+
const result = {};
|
|
238
|
+
for (const key of Object.keys(obj)) result[key] = normalizeScientificNumbers(obj[key]);
|
|
239
|
+
return result;
|
|
240
|
+
} else if (typeof obj === "number") {
|
|
241
|
+
if (!Number.isFinite(obj)) return obj;
|
|
242
|
+
const abs = Math.abs(obj);
|
|
243
|
+
if (abs === 0 || abs >= 1e-6 && abs < 1e21) return String(obj);
|
|
244
|
+
const isNegative = obj < 0;
|
|
245
|
+
const [rawMantissa, rawExponent] = abs.toExponential().split("e");
|
|
246
|
+
const exponent = +rawExponent;
|
|
247
|
+
const digits = rawMantissa.replace(".", "");
|
|
248
|
+
if (exponent < 0) {
|
|
249
|
+
const zeros = "0".repeat(Math.abs(exponent) - 1);
|
|
250
|
+
return (isNegative ? "-" : "") + "0." + zeros + digits;
|
|
251
|
+
} else {
|
|
252
|
+
const pad = exponent - (digits.length - 1);
|
|
253
|
+
if (pad >= 0) return (isNegative ? "-" : "") + digits + "0".repeat(pad);
|
|
254
|
+
else {
|
|
255
|
+
const point = digits.length + pad;
|
|
256
|
+
return (isNegative ? "-" : "") + digits.slice(0, point) + "." + digits.slice(point);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
} else return obj;
|
|
275
260
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
261
|
+
/**
|
|
262
|
+
* Determines whether a request should be retried based on the provided error.
|
|
263
|
+
*
|
|
264
|
+
* This function checks the HTTP method, response status, and number of retries left to determine if a request should be retried.
|
|
265
|
+
*
|
|
266
|
+
* @param error The error object to check.
|
|
267
|
+
* @param method The HTTP method of the request (optional).
|
|
268
|
+
* @param retriesLeft The number of retries left (optional).
|
|
269
|
+
* @returns `true` if the request should be retried, `false` otherwise.
|
|
270
|
+
*/
|
|
271
|
+
const shouldRetryRequest = function(error, method, retriesLeft) {
|
|
272
|
+
const isRetriableMethod = ["GET", "DELETE"].includes(method ?? "");
|
|
273
|
+
const isRetriableStatus = [
|
|
274
|
+
500,
|
|
275
|
+
502,
|
|
276
|
+
503,
|
|
277
|
+
504
|
|
278
|
+
].includes(error?.response?.status ?? 0);
|
|
279
|
+
return (retriesLeft ?? 0) > 0 && isRetriableMethod && (isRetriableStatus || !error?.response);
|
|
282
280
|
};
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
throw new ConnectorClientError(errorMsg);
|
|
360
|
-
}
|
|
361
|
-
} else {
|
|
362
|
-
if (retries > 0 && attempt >= retries)
|
|
363
|
-
lastError = new Error(`Request failed after ${retries} retries`);
|
|
364
|
-
else lastError = new NetworkError("Network error or request timeout.");
|
|
365
|
-
break;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
throw lastError;
|
|
281
|
+
/**
|
|
282
|
+
* Performs an HTTP request using the provided Axios instance and configuration.
|
|
283
|
+
*
|
|
284
|
+
* This function handles retries, rate limit handling, and error handling for the HTTP request.
|
|
285
|
+
*
|
|
286
|
+
* @param axiosArgs The request arguments to be passed to Axios.
|
|
287
|
+
* @param configuration The configuration options for the request.
|
|
288
|
+
* @returns A Promise that resolves to the API response, including the data and rate limit headers.
|
|
289
|
+
*/
|
|
290
|
+
const httpRequestFunction = async function(axiosArgs, configuration) {
|
|
291
|
+
const axiosRequestArgs = {
|
|
292
|
+
...axiosArgs.options,
|
|
293
|
+
url: (axios.default.defaults?.baseURL ? "" : configuration?.basePath ?? "") + axiosArgs.url
|
|
294
|
+
};
|
|
295
|
+
if (configuration?.keepAlive && !configuration?.baseOptions?.httpsAgent) axiosRequestArgs.httpsAgent = new https.default.Agent({ keepAlive: true });
|
|
296
|
+
if (configuration?.compression) axiosRequestArgs.headers = {
|
|
297
|
+
...axiosRequestArgs.headers,
|
|
298
|
+
"Accept-Encoding": "gzip, deflate, br"
|
|
299
|
+
};
|
|
300
|
+
const retries = configuration?.retries ?? 0;
|
|
301
|
+
const backoff = configuration?.backoff ?? 0;
|
|
302
|
+
let attempt = 0;
|
|
303
|
+
let lastError;
|
|
304
|
+
while (attempt <= retries) try {
|
|
305
|
+
const response = await axios.default.request({
|
|
306
|
+
...axiosRequestArgs,
|
|
307
|
+
responseType: "text"
|
|
308
|
+
});
|
|
309
|
+
const rateLimits = parseRateLimitHeaders(response.headers);
|
|
310
|
+
return {
|
|
311
|
+
data: async () => {
|
|
312
|
+
try {
|
|
313
|
+
return (0, json_with_bigint.JSONParse)(response.data);
|
|
314
|
+
} catch (err) {
|
|
315
|
+
throw new Error(`Failed to parse JSON response: ${err}`);
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
status: response.status,
|
|
319
|
+
headers: response.headers,
|
|
320
|
+
rateLimits
|
|
321
|
+
};
|
|
322
|
+
} catch (error) {
|
|
323
|
+
attempt++;
|
|
324
|
+
const axiosError = error;
|
|
325
|
+
if (shouldRetryRequest(axiosError, axiosRequestArgs?.method?.toUpperCase(), retries - attempt)) await delay(backoff * attempt);
|
|
326
|
+
else if (axiosError.response && axiosError.response.status) {
|
|
327
|
+
const status = axiosError.response?.status;
|
|
328
|
+
const responseData = axiosError.response.data;
|
|
329
|
+
let data = {};
|
|
330
|
+
if (responseData && responseData !== null) {
|
|
331
|
+
if (typeof responseData === "string" && responseData !== "") try {
|
|
332
|
+
data = (0, json_with_bigint.JSONParse)(responseData);
|
|
333
|
+
} catch {
|
|
334
|
+
data = {};
|
|
335
|
+
}
|
|
336
|
+
else if (typeof responseData === "object") data = responseData;
|
|
337
|
+
}
|
|
338
|
+
const errorMsg = data.msg;
|
|
339
|
+
switch (status) {
|
|
340
|
+
case 400: throw new BadRequestError(errorMsg);
|
|
341
|
+
case 401: throw new UnauthorizedError(errorMsg);
|
|
342
|
+
case 403: throw new ForbiddenError(errorMsg);
|
|
343
|
+
case 404: throw new NotFoundError(errorMsg);
|
|
344
|
+
case 418: throw new RateLimitBanError(errorMsg);
|
|
345
|
+
case 429: throw new TooManyRequestsError(errorMsg);
|
|
346
|
+
default:
|
|
347
|
+
if (status >= 500 && status < 600) throw new ServerError(`Server error: ${status}`, status);
|
|
348
|
+
throw new ConnectorClientError(errorMsg);
|
|
349
|
+
}
|
|
350
|
+
} else {
|
|
351
|
+
if (retries > 0 && attempt >= retries) lastError = /* @__PURE__ */ new Error(`Request failed after ${retries} retries`);
|
|
352
|
+
else lastError = new NetworkError("Network error or request timeout.");
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
throw lastError;
|
|
371
357
|
};
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
358
|
+
/**
|
|
359
|
+
* Parses the rate limit headers from the Axios response headers and returns an array of `RestApiRateLimit` objects.
|
|
360
|
+
*
|
|
361
|
+
* @param headers - The Axios response headers.
|
|
362
|
+
* @returns An array of `RestApiRateLimit` objects containing the parsed rate limit information.
|
|
363
|
+
*/
|
|
364
|
+
const parseRateLimitHeaders = function(headers) {
|
|
365
|
+
const rateLimits = [];
|
|
366
|
+
const parseIntervalDetails = (key) => {
|
|
367
|
+
const match = key.match(/x-mbx-used-weight-(\d+)([smhd])|x-mbx-order-count-(\d+)([smhd])/i);
|
|
368
|
+
if (!match) return null;
|
|
369
|
+
const intervalNum = parseInt(match[1] || match[3], 10);
|
|
370
|
+
const intervalLetter = (match[2] || match[4])?.toUpperCase();
|
|
371
|
+
let interval;
|
|
372
|
+
switch (intervalLetter) {
|
|
373
|
+
case "S":
|
|
374
|
+
interval = "SECOND";
|
|
375
|
+
break;
|
|
376
|
+
case "M":
|
|
377
|
+
interval = "MINUTE";
|
|
378
|
+
break;
|
|
379
|
+
case "H":
|
|
380
|
+
interval = "HOUR";
|
|
381
|
+
break;
|
|
382
|
+
case "D":
|
|
383
|
+
interval = "DAY";
|
|
384
|
+
break;
|
|
385
|
+
default: return null;
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
interval,
|
|
389
|
+
intervalNum
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
393
|
+
const normalizedKey = key.toLowerCase();
|
|
394
|
+
if (value === void 0) continue;
|
|
395
|
+
if (normalizedKey.startsWith("x-mbx-used-weight-")) {
|
|
396
|
+
const details = parseIntervalDetails(normalizedKey);
|
|
397
|
+
if (details) rateLimits.push({
|
|
398
|
+
rateLimitType: "REQUEST_WEIGHT",
|
|
399
|
+
interval: details.interval,
|
|
400
|
+
intervalNum: details.intervalNum,
|
|
401
|
+
count: parseInt(value, 10)
|
|
402
|
+
});
|
|
403
|
+
} else if (normalizedKey.startsWith("x-mbx-order-count-")) {
|
|
404
|
+
const details = parseIntervalDetails(normalizedKey);
|
|
405
|
+
if (details) rateLimits.push({
|
|
406
|
+
rateLimitType: "ORDERS",
|
|
407
|
+
interval: details.interval,
|
|
408
|
+
intervalNum: details.intervalNum,
|
|
409
|
+
count: parseInt(value, 10)
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (headers["retry-after"]) {
|
|
414
|
+
const retryAfter = parseInt(headers["retry-after"], 10);
|
|
415
|
+
for (const limit of rateLimits) limit.retryAfter = retryAfter;
|
|
416
|
+
}
|
|
417
|
+
return rateLimits;
|
|
430
418
|
};
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
419
|
+
/**
|
|
420
|
+
* Generic function to send a request with optional API key and signature.
|
|
421
|
+
* @param endpoint - The API endpoint to call.
|
|
422
|
+
* @param method - HTTP method to use (GET, POST, DELETE, etc.).
|
|
423
|
+
* @param params - Query parameters for the request.
|
|
424
|
+
* @param timeUnit - The time unit for the request.
|
|
425
|
+
* @param options - Additional request options (isSigned).
|
|
426
|
+
* @returns A promise resolving to the response data object.
|
|
427
|
+
*/
|
|
428
|
+
const sendRequest = function(configuration, endpoint, method, queryParams = {}, bodyParams = {}, timeUnit, options = {}) {
|
|
429
|
+
const localVarUrlObj = new URL(endpoint, configuration?.basePath);
|
|
430
|
+
const localVarRequestOptions = {
|
|
431
|
+
method,
|
|
432
|
+
...configuration?.baseOptions
|
|
433
|
+
};
|
|
434
|
+
const localVarQueryParameter = { ...normalizeScientificNumbers(queryParams) };
|
|
435
|
+
const localVarBodyParameter = { ...normalizeScientificNumbers(bodyParams) };
|
|
436
|
+
if (options.isSigned) {
|
|
437
|
+
localVarQueryParameter["timestamp"] = getTimestamp();
|
|
438
|
+
const signature = getSignature(configuration, localVarQueryParameter, localVarBodyParameter);
|
|
439
|
+
if (signature) localVarQueryParameter["signature"] = signature;
|
|
440
|
+
}
|
|
441
|
+
setSearchParams(localVarUrlObj, localVarQueryParameter);
|
|
442
|
+
if (Object.keys(localVarBodyParameter).length > 0) {
|
|
443
|
+
const searchParams = new URLSearchParams();
|
|
444
|
+
for (const [key, value] of Object.entries(localVarBodyParameter)) {
|
|
445
|
+
if (value === null || value === void 0) continue;
|
|
446
|
+
const serializedValue = serializeValue(value);
|
|
447
|
+
searchParams.append(key, serializedValue);
|
|
448
|
+
}
|
|
449
|
+
localVarRequestOptions.data = searchParams.toString();
|
|
450
|
+
localVarRequestOptions.headers = {
|
|
451
|
+
...localVarRequestOptions.headers || {},
|
|
452
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
if (timeUnit && localVarRequestOptions.headers) {
|
|
456
|
+
const _timeUnit = validateTimeUnit(timeUnit);
|
|
457
|
+
localVarRequestOptions.headers = {
|
|
458
|
+
...localVarRequestOptions.headers,
|
|
459
|
+
"X-MBX-TIME-UNIT": _timeUnit
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
return httpRequestFunction({
|
|
463
|
+
url: toPathString(localVarUrlObj),
|
|
464
|
+
options: localVarRequestOptions
|
|
465
|
+
}, configuration);
|
|
461
466
|
};
|
|
467
|
+
/**
|
|
468
|
+
* Removes any null, undefined, or empty string values from the provided object.
|
|
469
|
+
*
|
|
470
|
+
* @param obj - The object to remove empty values from.
|
|
471
|
+
* @returns A new object with empty values removed.
|
|
472
|
+
*/
|
|
462
473
|
function removeEmptyValue(obj) {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
Object.entries(obj).filter(
|
|
466
|
-
([, value]) => value !== null && value !== void 0 && value !== ""
|
|
467
|
-
)
|
|
468
|
-
);
|
|
474
|
+
if (!(obj instanceof Object)) return {};
|
|
475
|
+
return Object.fromEntries(Object.entries(obj).filter(([, value]) => value !== null && value !== void 0 && value !== ""));
|
|
469
476
|
}
|
|
477
|
+
/**
|
|
478
|
+
* Sorts the properties of the provided object in alphabetical order and returns a new object with the sorted properties.
|
|
479
|
+
*
|
|
480
|
+
* @param obj - The object to be sorted.
|
|
481
|
+
* @returns A new object with the properties sorted in alphabetical order.
|
|
482
|
+
*/
|
|
470
483
|
function sortObject(obj) {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
484
|
+
return Object.keys(obj).sort().reduce((res, key) => {
|
|
485
|
+
res[key] = obj[key];
|
|
486
|
+
return res;
|
|
487
|
+
}, {});
|
|
475
488
|
}
|
|
489
|
+
/**
|
|
490
|
+
* Replaces placeholders in the format <field> with corresponding values from the provided variables object.
|
|
491
|
+
*
|
|
492
|
+
* @param {string} str - The input string containing placeholders.
|
|
493
|
+
* @param {Object} variables - An object where keys correspond to placeholder names and values are the replacements.
|
|
494
|
+
* @returns {string} - The resulting string with placeholders replaced by their corresponding values.
|
|
495
|
+
*/
|
|
476
496
|
function replaceWebsocketStreamsPlaceholders(str, variables) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
default:
|
|
496
|
-
return (precedingAt || "") + value;
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
return "";
|
|
500
|
-
});
|
|
497
|
+
const normalizedVariables = Object.keys(variables).reduce((acc, key) => {
|
|
498
|
+
const normalizedKey = key.toLowerCase().replace(/[-_]/g, "");
|
|
499
|
+
acc[normalizedKey] = variables[key];
|
|
500
|
+
return acc;
|
|
501
|
+
}, {});
|
|
502
|
+
return str.replace(/(@)?<([^>]+)>/g, (match, precedingAt, fieldName) => {
|
|
503
|
+
const normalizedFieldName = fieldName.toLowerCase().replace(/[-_]/g, "");
|
|
504
|
+
if (Object.prototype.hasOwnProperty.call(normalizedVariables, normalizedFieldName) && normalizedVariables[normalizedFieldName] != null) {
|
|
505
|
+
const value = normalizedVariables[normalizedFieldName];
|
|
506
|
+
switch (normalizedFieldName) {
|
|
507
|
+
case "symbol":
|
|
508
|
+
case "windowsize": return value.toLowerCase();
|
|
509
|
+
case "updatespeed": return `@${value}`;
|
|
510
|
+
default: return (precedingAt || "") + value;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return "";
|
|
514
|
+
});
|
|
501
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* Generates a standardized user agent string for the application.
|
|
518
|
+
*
|
|
519
|
+
* @param {string} packageName - The name of the package/application.
|
|
520
|
+
* @param {string} packageVersion - The version of the package/application.
|
|
521
|
+
* @returns {string} A formatted user agent string including package details, Node.js version, platform, and architecture.
|
|
522
|
+
*/
|
|
502
523
|
function buildUserAgent(packageName, packageVersion) {
|
|
503
|
-
|
|
524
|
+
return `${packageName}/${packageVersion} (Node.js/${process.version}; ${(0, os.platform)()}; ${(0, os.arch)()})`;
|
|
504
525
|
}
|
|
526
|
+
/**
|
|
527
|
+
* Builds a WebSocket API message with optional authentication and signature.
|
|
528
|
+
*
|
|
529
|
+
* @param {ConfigurationWebsocketAPI} configuration - The WebSocket API configuration.
|
|
530
|
+
* @param {string} method - The method name for the WebSocket message.
|
|
531
|
+
* @param {WebsocketSendMsgOptions} payload - The payload data to be sent.
|
|
532
|
+
* @param {WebsocketSendMsgConfig} options - Configuration options for message sending.
|
|
533
|
+
* @param {boolean} [skipAuth=false] - Flag to skip authentication if needed.
|
|
534
|
+
* @returns {Object} A structured WebSocket message with id, method, and params.
|
|
535
|
+
*/
|
|
505
536
|
function buildWebsocketAPIMessage(configuration, method, payload, options, skipAuth = false) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
537
|
+
const id = payload.id && /^[0-9a-f]{32}$/.test(payload.id) ? payload.id : randomString();
|
|
538
|
+
delete payload.id;
|
|
539
|
+
let params = normalizeScientificNumbers(removeEmptyValue(payload));
|
|
540
|
+
if ((options.withApiKey || options.isSigned) && !skipAuth) params.apiKey = configuration.apiKey;
|
|
541
|
+
if (options.isSigned) {
|
|
542
|
+
params.timestamp = getTimestamp();
|
|
543
|
+
params = sortObject(params);
|
|
544
|
+
if (!skipAuth) params.signature = getSignature(configuration, params);
|
|
545
|
+
}
|
|
546
|
+
return {
|
|
547
|
+
id,
|
|
548
|
+
method,
|
|
549
|
+
params
|
|
550
|
+
};
|
|
516
551
|
}
|
|
552
|
+
/**
|
|
553
|
+
* Sanitizes a header value by checking for and preventing carriage return and line feed characters.
|
|
554
|
+
*
|
|
555
|
+
* @param {string | string[]} value - The header value or array of header values to sanitize.
|
|
556
|
+
* @returns {string | string[]} The sanitized header value(s).
|
|
557
|
+
* @throws {Error} If the header value contains CR/LF characters.
|
|
558
|
+
*/
|
|
517
559
|
function sanitizeHeaderValue(value) {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
560
|
+
const sanitizeOne = (v) => {
|
|
561
|
+
if (/\r|\n/.test(v)) throw new Error(`Invalid header value (contains CR/LF): "${v}"`);
|
|
562
|
+
return v;
|
|
563
|
+
};
|
|
564
|
+
return Array.isArray(value) ? value.map(sanitizeOne) : sanitizeOne(value);
|
|
523
565
|
}
|
|
566
|
+
/**
|
|
567
|
+
* Parses and sanitizes custom headers, filtering out forbidden headers.
|
|
568
|
+
*
|
|
569
|
+
* @param {Record<string, string | string[]>} headers - The input headers to be parsed.
|
|
570
|
+
* @returns {Record<string, string | string[]>} A new object with sanitized and allowed headers.
|
|
571
|
+
* @description Removes forbidden headers like 'host', 'authorization', and 'cookie',
|
|
572
|
+
* and sanitizes remaining header values to prevent injection of carriage return or line feed characters.
|
|
573
|
+
*/
|
|
524
574
|
function parseCustomHeaders(headers) {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
575
|
+
if (!headers || Object.keys(headers).length === 0) return {};
|
|
576
|
+
const forbidden = new Set([
|
|
577
|
+
"host",
|
|
578
|
+
"authorization",
|
|
579
|
+
"cookie",
|
|
580
|
+
":method",
|
|
581
|
+
":path"
|
|
582
|
+
]);
|
|
583
|
+
const parsedHeaders = {};
|
|
584
|
+
for (const [rawName, rawValue] of Object.entries(headers || {})) {
|
|
585
|
+
const name = rawName.trim();
|
|
586
|
+
if (forbidden.has(name.toLowerCase())) {
|
|
587
|
+
Logger.getInstance().warn(`Dropping forbidden header: ${name}`);
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
try {
|
|
591
|
+
parsedHeaders[name] = sanitizeHeaderValue(rawValue);
|
|
592
|
+
} catch {
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return parsedHeaders;
|
|
541
597
|
}
|
|
542
598
|
|
|
543
|
-
|
|
599
|
+
//#endregion
|
|
600
|
+
//#region src/configuration.ts
|
|
544
601
|
var ConfigurationRestAPI = class {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
602
|
+
constructor(param = { apiKey: "" }) {
|
|
603
|
+
this.apiKey = param.apiKey;
|
|
604
|
+
this.apiSecret = param.apiSecret;
|
|
605
|
+
this.basePath = param.basePath;
|
|
606
|
+
this.keepAlive = param.keepAlive ?? true;
|
|
607
|
+
this.compression = param.compression ?? true;
|
|
608
|
+
this.retries = param.retries ?? 3;
|
|
609
|
+
this.backoff = param.backoff ?? 1e3;
|
|
610
|
+
this.privateKey = param.privateKey;
|
|
611
|
+
this.privateKeyPassphrase = param.privateKeyPassphrase;
|
|
612
|
+
this.timeUnit = param.timeUnit;
|
|
613
|
+
this.baseOptions = {
|
|
614
|
+
timeout: param.timeout ?? 1e3,
|
|
615
|
+
proxy: param.proxy && {
|
|
616
|
+
host: param.proxy.host,
|
|
617
|
+
port: param.proxy.port,
|
|
618
|
+
...param.proxy.protocol && { protocol: param.proxy.protocol },
|
|
619
|
+
...param.proxy.auth && { auth: param.proxy.auth }
|
|
620
|
+
},
|
|
621
|
+
httpsAgent: param.httpsAgent ?? false,
|
|
622
|
+
headers: {
|
|
623
|
+
...parseCustomHeaders(param.customHeaders || {}),
|
|
624
|
+
"Content-Type": "application/json",
|
|
625
|
+
"X-MBX-APIKEY": param.apiKey
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
}
|
|
572
629
|
};
|
|
573
|
-
var
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
630
|
+
var ConfigurationWebsocketAPI = class {
|
|
631
|
+
constructor(param = { apiKey: "" }) {
|
|
632
|
+
this.apiKey = param.apiKey;
|
|
633
|
+
this.apiSecret = param.apiSecret;
|
|
634
|
+
this.wsURL = param.wsURL;
|
|
635
|
+
this.timeout = param.timeout ?? 5e3;
|
|
636
|
+
this.reconnectDelay = param.reconnectDelay ?? 5e3;
|
|
637
|
+
this.compression = param.compression ?? true;
|
|
638
|
+
this.agent = param.agent ?? false;
|
|
639
|
+
this.mode = param.mode ?? "single";
|
|
640
|
+
this.poolSize = param.poolSize ?? 1;
|
|
641
|
+
this.privateKey = param.privateKey;
|
|
642
|
+
this.privateKeyPassphrase = param.privateKeyPassphrase;
|
|
643
|
+
this.timeUnit = param.timeUnit;
|
|
644
|
+
this.autoSessionReLogon = param.autoSessionReLogon ?? true;
|
|
645
|
+
}
|
|
589
646
|
};
|
|
590
647
|
var ConfigurationWebsocketStreams = class {
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
648
|
+
constructor(param = {}) {
|
|
649
|
+
this.wsURL = param.wsURL;
|
|
650
|
+
this.reconnectDelay = param.reconnectDelay ?? 5e3;
|
|
651
|
+
this.compression = param.compression ?? true;
|
|
652
|
+
this.agent = param.agent ?? false;
|
|
653
|
+
this.mode = param.mode ?? "single";
|
|
654
|
+
this.poolSize = param.poolSize ?? 1;
|
|
655
|
+
this.timeUnit = param.timeUnit;
|
|
656
|
+
}
|
|
600
657
|
};
|
|
601
658
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
659
|
+
//#endregion
|
|
660
|
+
//#region src/constants.ts
|
|
661
|
+
const TimeUnit = {
|
|
662
|
+
MILLISECOND: "MILLISECOND",
|
|
663
|
+
millisecond: "millisecond",
|
|
664
|
+
MICROSECOND: "MICROSECOND",
|
|
665
|
+
microsecond: "microsecond"
|
|
608
666
|
};
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
667
|
+
const ALGO_REST_API_PROD_URL = "https://api.binance.com";
|
|
668
|
+
const C2C_REST_API_PROD_URL = "https://api.binance.com";
|
|
669
|
+
const CONVERT_REST_API_PROD_URL = "https://api.binance.com";
|
|
670
|
+
const COPY_TRADING_REST_API_PROD_URL = "https://api.binance.com";
|
|
671
|
+
const CRYPTO_LOAN_REST_API_PROD_URL = "https://api.binance.com";
|
|
672
|
+
const DERIVATIVES_TRADING_COIN_FUTURES_REST_API_PROD_URL = "https://dapi.binance.com";
|
|
673
|
+
const DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
674
|
+
const DERIVATIVES_TRADING_COIN_FUTURES_WS_API_PROD_URL = "wss://ws-dapi.binance.com/ws-dapi/v1";
|
|
675
|
+
const DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL = "wss://testnet.binancefuture.com/ws-dapi/v1";
|
|
676
|
+
const DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL = "wss://dstream.binance.com";
|
|
677
|
+
const DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL = "wss://dstream.binancefuture.com";
|
|
678
|
+
const DERIVATIVES_TRADING_USDS_FUTURES_REST_API_PROD_URL = "https://fapi.binance.com";
|
|
679
|
+
const DERIVATIVES_TRADING_USDS_FUTURES_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
680
|
+
const DERIVATIVES_TRADING_USDS_FUTURES_WS_API_PROD_URL = "wss://ws-fapi.binance.com/ws-fapi/v1";
|
|
681
|
+
const DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL = "wss://testnet.binancefuture.com/ws-fapi/v1";
|
|
682
|
+
const DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL = "wss://fstream.binance.com";
|
|
683
|
+
const DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL = "wss://stream.binancefuture.com";
|
|
684
|
+
const DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL = "https://eapi.binance.com";
|
|
685
|
+
const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL = "wss://nbstream.binance.com/eoptions";
|
|
686
|
+
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL = "https://papi.binance.com";
|
|
687
|
+
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
688
|
+
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm";
|
|
689
|
+
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL = "wss://fstream.binancefuture.com/pm";
|
|
690
|
+
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL = "https://api.binance.com";
|
|
691
|
+
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm-classic";
|
|
692
|
+
const DUAL_INVESTMENT_REST_API_PROD_URL = "https://api.binance.com";
|
|
693
|
+
const FIAT_REST_API_PROD_URL = "https://api.binance.com";
|
|
694
|
+
const GIFT_CARD_REST_API_PROD_URL = "https://api.binance.com";
|
|
695
|
+
const MARGIN_TRADING_REST_API_PROD_URL = "https://api.binance.com";
|
|
696
|
+
const MARGIN_TRADING_WS_STREAMS_PROD_URL = "wss://stream.binance.com:9443";
|
|
697
|
+
const MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL = "wss://margin-stream.binance.com";
|
|
698
|
+
const MINING_REST_API_PROD_URL = "https://api.binance.com";
|
|
699
|
+
const NFT_REST_API_PROD_URL = "https://api.binance.com";
|
|
700
|
+
const PAY_REST_API_PROD_URL = "https://api.binance.com";
|
|
701
|
+
const REBATE_REST_API_PROD_URL = "https://api.binance.com";
|
|
702
|
+
const SIMPLE_EARN_REST_API_PROD_URL = "https://api.binance.com";
|
|
703
|
+
const SPOT_REST_API_PROD_URL = "https://api.binance.com";
|
|
704
|
+
const SPOT_REST_API_TESTNET_URL = "https://testnet.binance.vision";
|
|
705
|
+
const SPOT_WS_API_PROD_URL = "wss://ws-api.binance.com:443/ws-api/v3";
|
|
706
|
+
const SPOT_WS_API_TESTNET_URL = "wss://ws-api.testnet.binance.vision/ws-api/v3";
|
|
707
|
+
const SPOT_WS_STREAMS_PROD_URL = "wss://stream.binance.com:9443";
|
|
708
|
+
const SPOT_WS_STREAMS_TESTNET_URL = "wss://stream.testnet.binance.vision";
|
|
709
|
+
const SPOT_REST_API_MARKET_URL = "https://data-api.binance.vision";
|
|
710
|
+
const SPOT_WS_STREAMS_MARKET_URL = "wss://data-stream.binance.vision";
|
|
711
|
+
const STAKING_REST_API_PROD_URL = "https://api.binance.com";
|
|
712
|
+
const SUB_ACCOUNT_REST_API_PROD_URL = "https://api.binance.com";
|
|
713
|
+
const VIP_LOAN_REST_API_PROD_URL = "https://api.binance.com";
|
|
714
|
+
const WALLET_REST_API_PROD_URL = "https://api.binance.com";
|
|
657
715
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
716
|
+
//#endregion
|
|
717
|
+
//#region src/errors.ts
|
|
718
|
+
/**
|
|
719
|
+
* Represents an error that occurred in the Connector client.
|
|
720
|
+
* @param msg - An optional error message.
|
|
721
|
+
*/
|
|
722
|
+
var ConnectorClientError = class ConnectorClientError extends Error {
|
|
723
|
+
constructor(msg) {
|
|
724
|
+
super(msg || "An unexpected error occurred.");
|
|
725
|
+
Object.setPrototypeOf(this, ConnectorClientError.prototype);
|
|
726
|
+
this.name = "ConnectorClientError";
|
|
727
|
+
}
|
|
665
728
|
};
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
729
|
+
/**
|
|
730
|
+
* Represents an error that occurs when a required parameter is missing or undefined.
|
|
731
|
+
* @param field - The name of the missing parameter.
|
|
732
|
+
* @param msg - An optional error message.
|
|
733
|
+
*/
|
|
734
|
+
var RequiredError = class RequiredError extends Error {
|
|
735
|
+
constructor(field, msg) {
|
|
736
|
+
super(msg || `Required parameter ${field} was null or undefined.`);
|
|
737
|
+
this.field = field;
|
|
738
|
+
Object.setPrototypeOf(this, RequiredError.prototype);
|
|
739
|
+
this.name = "RequiredError";
|
|
740
|
+
}
|
|
673
741
|
};
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
742
|
+
/**
|
|
743
|
+
* Represents an error that occurs when a client is unauthorized to access a resource.
|
|
744
|
+
* @param msg - An optional error message.
|
|
745
|
+
*/
|
|
746
|
+
var UnauthorizedError = class UnauthorizedError extends Error {
|
|
747
|
+
constructor(msg) {
|
|
748
|
+
super(msg || "Unauthorized access. Authentication required.");
|
|
749
|
+
Object.setPrototypeOf(this, UnauthorizedError.prototype);
|
|
750
|
+
this.name = "UnauthorizedError";
|
|
751
|
+
}
|
|
680
752
|
};
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
753
|
+
/**
|
|
754
|
+
* Represents an error that occurs when a resource is forbidden to the client.
|
|
755
|
+
* @param msg - An optional error message.
|
|
756
|
+
*/
|
|
757
|
+
var ForbiddenError = class ForbiddenError extends Error {
|
|
758
|
+
constructor(msg) {
|
|
759
|
+
super(msg || "Access to the requested resource is forbidden.");
|
|
760
|
+
Object.setPrototypeOf(this, ForbiddenError.prototype);
|
|
761
|
+
this.name = "ForbiddenError";
|
|
762
|
+
}
|
|
687
763
|
};
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
764
|
+
/**
|
|
765
|
+
* Represents an error that occurs when client is doing too many requests.
|
|
766
|
+
* @param msg - An optional error message.
|
|
767
|
+
*/
|
|
768
|
+
var TooManyRequestsError = class TooManyRequestsError extends Error {
|
|
769
|
+
constructor(msg) {
|
|
770
|
+
super(msg || "Too many requests. You are being rate-limited.");
|
|
771
|
+
Object.setPrototypeOf(this, TooManyRequestsError.prototype);
|
|
772
|
+
this.name = "TooManyRequestsError";
|
|
773
|
+
}
|
|
694
774
|
};
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
775
|
+
/**
|
|
776
|
+
* Represents an error that occurs when client's IP has been banned.
|
|
777
|
+
* @param msg - An optional error message.
|
|
778
|
+
*/
|
|
779
|
+
var RateLimitBanError = class RateLimitBanError extends Error {
|
|
780
|
+
constructor(msg) {
|
|
781
|
+
super(msg || "The IP address has been banned for exceeding rate limits.");
|
|
782
|
+
Object.setPrototypeOf(this, RateLimitBanError.prototype);
|
|
783
|
+
this.name = "RateLimitBanError";
|
|
784
|
+
}
|
|
701
785
|
};
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
786
|
+
/**
|
|
787
|
+
* Represents an error that occurs when there is an internal server error.
|
|
788
|
+
* @param msg - An optional error message.
|
|
789
|
+
* @param statusCode - An optional HTTP status code associated with the error.
|
|
790
|
+
*/
|
|
791
|
+
var ServerError = class ServerError extends Error {
|
|
792
|
+
constructor(msg, statusCode) {
|
|
793
|
+
super(msg || "An internal server error occurred.");
|
|
794
|
+
this.statusCode = statusCode;
|
|
795
|
+
Object.setPrototypeOf(this, ServerError.prototype);
|
|
796
|
+
this.name = "ServerError";
|
|
797
|
+
}
|
|
709
798
|
};
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
799
|
+
/**
|
|
800
|
+
* Represents an error that occurs when a network error occurs.
|
|
801
|
+
* @param msg - An optional error message.
|
|
802
|
+
*/
|
|
803
|
+
var NetworkError = class NetworkError extends Error {
|
|
804
|
+
constructor(msg) {
|
|
805
|
+
super(msg || "A network error occurred.");
|
|
806
|
+
Object.setPrototypeOf(this, NetworkError.prototype);
|
|
807
|
+
this.name = "NetworkError";
|
|
808
|
+
}
|
|
716
809
|
};
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
810
|
+
/**
|
|
811
|
+
* Represents an error that occurs when the requested resource was not found.
|
|
812
|
+
* @param msg - An optional error message.
|
|
813
|
+
*/
|
|
814
|
+
var NotFoundError = class NotFoundError extends Error {
|
|
815
|
+
constructor(msg) {
|
|
816
|
+
super(msg || "The requested resource was not found.");
|
|
817
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
818
|
+
this.name = "NotFoundError";
|
|
819
|
+
}
|
|
723
820
|
};
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
821
|
+
/**
|
|
822
|
+
* Represents an error that occurs when a request is invalid or cannot be otherwise served.
|
|
823
|
+
* @param msg - An optional error message.
|
|
824
|
+
*/
|
|
825
|
+
var BadRequestError = class BadRequestError extends Error {
|
|
826
|
+
constructor(msg) {
|
|
827
|
+
super(msg || "The request was invalid or cannot be otherwise served.");
|
|
828
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
829
|
+
this.name = "BadRequestError";
|
|
830
|
+
}
|
|
730
831
|
};
|
|
731
832
|
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
}
|
|
833
|
+
//#endregion
|
|
834
|
+
//#region src/logger.ts
|
|
835
|
+
let LogLevel = /* @__PURE__ */ function(LogLevel$1) {
|
|
836
|
+
LogLevel$1["NONE"] = "";
|
|
837
|
+
LogLevel$1["DEBUG"] = "debug";
|
|
838
|
+
LogLevel$1["INFO"] = "info";
|
|
839
|
+
LogLevel$1["WARN"] = "warn";
|
|
840
|
+
LogLevel$1["ERROR"] = "error";
|
|
841
|
+
return LogLevel$1;
|
|
842
|
+
}({});
|
|
843
|
+
var Logger = class Logger {
|
|
844
|
+
constructor() {
|
|
845
|
+
this.minLogLevel = LogLevel.INFO;
|
|
846
|
+
this.levelsOrder = [
|
|
847
|
+
LogLevel.NONE,
|
|
848
|
+
LogLevel.DEBUG,
|
|
849
|
+
LogLevel.INFO,
|
|
850
|
+
LogLevel.WARN,
|
|
851
|
+
LogLevel.ERROR
|
|
852
|
+
];
|
|
853
|
+
const envLevel = process.env.LOG_LEVEL?.toLowerCase();
|
|
854
|
+
this.minLogLevel = envLevel && this.isValidLogLevel(envLevel) ? envLevel : LogLevel.INFO;
|
|
855
|
+
}
|
|
856
|
+
static getInstance() {
|
|
857
|
+
if (!Logger.instance) Logger.instance = new Logger();
|
|
858
|
+
return Logger.instance;
|
|
859
|
+
}
|
|
860
|
+
setMinLogLevel(level) {
|
|
861
|
+
if (!this.isValidLogLevel(level)) throw new Error(`Invalid log level: ${level}`);
|
|
862
|
+
this.minLogLevel = level;
|
|
863
|
+
}
|
|
864
|
+
isValidLogLevel(level) {
|
|
865
|
+
return this.levelsOrder.includes(level);
|
|
866
|
+
}
|
|
867
|
+
log(level, ...message) {
|
|
868
|
+
if (level === LogLevel.NONE || !this.allowLevelLog(level)) return;
|
|
869
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
870
|
+
console[level](`[${timestamp}] [${level.toLowerCase()}]`, ...message);
|
|
871
|
+
}
|
|
872
|
+
allowLevelLog(level) {
|
|
873
|
+
if (!this.isValidLogLevel(level)) throw new Error(`Invalid log level: ${level}`);
|
|
874
|
+
return this.levelsOrder.indexOf(level) >= this.levelsOrder.indexOf(this.minLogLevel);
|
|
875
|
+
}
|
|
876
|
+
debug(...message) {
|
|
877
|
+
this.log(LogLevel.DEBUG, ...message);
|
|
878
|
+
}
|
|
879
|
+
info(...message) {
|
|
880
|
+
this.log(LogLevel.INFO, ...message);
|
|
881
|
+
}
|
|
882
|
+
warn(...message) {
|
|
883
|
+
this.log(LogLevel.WARN, ...message);
|
|
884
|
+
}
|
|
885
|
+
error(...message) {
|
|
886
|
+
this.log(LogLevel.ERROR, ...message);
|
|
887
|
+
}
|
|
788
888
|
};
|
|
789
889
|
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
var import_ws = __toESM(require("ws"));
|
|
793
|
-
var import_json_with_bigint2 = require("json-with-bigint");
|
|
890
|
+
//#endregion
|
|
891
|
+
//#region src/websocket.ts
|
|
794
892
|
var WebsocketEventEmitter = class {
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
emit(event, ...args) {
|
|
808
|
-
this.eventEmitter.emit(event, ...args);
|
|
809
|
-
}
|
|
893
|
+
constructor() {
|
|
894
|
+
this.eventEmitter = new events.EventEmitter();
|
|
895
|
+
}
|
|
896
|
+
on(event, listener) {
|
|
897
|
+
this.eventEmitter.on(event, listener);
|
|
898
|
+
}
|
|
899
|
+
off(event, listener) {
|
|
900
|
+
this.eventEmitter.off(event, listener);
|
|
901
|
+
}
|
|
902
|
+
emit(event, ...args) {
|
|
903
|
+
this.eventEmitter.emit(event, ...args);
|
|
904
|
+
}
|
|
810
905
|
};
|
|
811
|
-
var WebsocketCommon = class
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
await Promise.all(disconnectPromises);
|
|
1249
|
-
this.logger.info("Disconnected with Binance Websocket Server");
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
/**
|
|
1253
|
-
* Sends a ping message to all connected Websocket servers in the pool.
|
|
1254
|
-
* If no connections are ready, a warning is logged.
|
|
1255
|
-
* For each active connection, the ping message is sent, and debug logs provide details.
|
|
1256
|
-
* @throws Error if a Websocket client is not set for a connection.
|
|
1257
|
-
*/
|
|
1258
|
-
pingServer() {
|
|
1259
|
-
const connectedConnections = this.connectionPool.filter(
|
|
1260
|
-
(connection) => this.isConnected(connection)
|
|
1261
|
-
);
|
|
1262
|
-
if (connectedConnections.length === 0) {
|
|
1263
|
-
this.logger.warn("Ping only can be sent when connection is ready.");
|
|
1264
|
-
return;
|
|
1265
|
-
}
|
|
1266
|
-
this.logger.debug("Sending PING to all connected Websocket servers.");
|
|
1267
|
-
connectedConnections.forEach((connection) => {
|
|
1268
|
-
if (connection.ws) {
|
|
1269
|
-
connection.ws.ping();
|
|
1270
|
-
this.logger.debug(`PING sent to connection with id ${connection.id}`);
|
|
1271
|
-
} else {
|
|
1272
|
-
this.logger.error("WebSocket Client not set for a connection.");
|
|
1273
|
-
}
|
|
1274
|
-
});
|
|
1275
|
-
}
|
|
1276
|
-
/**
|
|
1277
|
-
* Sends a payload through the WebSocket connection.
|
|
1278
|
-
* @param payload - Message to send.
|
|
1279
|
-
* @param id - Optional request identifier.
|
|
1280
|
-
* @param promiseBased - Whether to return a promise.
|
|
1281
|
-
* @param timeout - Timeout duration in milliseconds.
|
|
1282
|
-
* @param connection - The WebSocket connection to use.
|
|
1283
|
-
* @returns A promise if `promiseBased` is true, void otherwise.
|
|
1284
|
-
* @throws Error if not connected or WebSocket client is not set.
|
|
1285
|
-
*/
|
|
1286
|
-
send(payload, id, promiseBased = true, timeout = 5e3, connection) {
|
|
1287
|
-
if (!this.isConnected(connection)) {
|
|
1288
|
-
const errorMsg = "Unable to send message \u2014 connection is not available.";
|
|
1289
|
-
this.logger.warn(errorMsg);
|
|
1290
|
-
if (promiseBased) return Promise.reject(new Error(errorMsg));
|
|
1291
|
-
else throw new Error(errorMsg);
|
|
1292
|
-
}
|
|
1293
|
-
const connectionToUse = connection ?? this.getConnection();
|
|
1294
|
-
if (!connectionToUse.ws) {
|
|
1295
|
-
const errorMsg = "Websocket Client not set";
|
|
1296
|
-
this.logger.error(errorMsg);
|
|
1297
|
-
if (promiseBased) return Promise.reject(new Error(errorMsg));
|
|
1298
|
-
else throw new Error(errorMsg);
|
|
1299
|
-
}
|
|
1300
|
-
connectionToUse.ws.send(payload);
|
|
1301
|
-
if (promiseBased) {
|
|
1302
|
-
return new Promise((resolve, reject) => {
|
|
1303
|
-
if (!id) return reject(new Error("id is required for promise-based sending."));
|
|
1304
|
-
const timeoutHandle = setTimeout(() => {
|
|
1305
|
-
if (connectionToUse.pendingRequests.has(id)) {
|
|
1306
|
-
connectionToUse.pendingRequests.delete(id);
|
|
1307
|
-
reject(new Error(`Request timeout for id: ${id}`));
|
|
1308
|
-
}
|
|
1309
|
-
}, timeout);
|
|
1310
|
-
connectionToUse.pendingRequests.set(id, {
|
|
1311
|
-
resolve: (v) => {
|
|
1312
|
-
clearTimeout(timeoutHandle);
|
|
1313
|
-
resolve(v);
|
|
1314
|
-
},
|
|
1315
|
-
reject: (e) => {
|
|
1316
|
-
clearTimeout(timeoutHandle);
|
|
1317
|
-
reject(e);
|
|
1318
|
-
}
|
|
1319
|
-
});
|
|
1320
|
-
});
|
|
1321
|
-
}
|
|
1322
|
-
}
|
|
906
|
+
var WebsocketCommon = class WebsocketCommon extends WebsocketEventEmitter {
|
|
907
|
+
static {
|
|
908
|
+
this.MAX_CONNECTION_DURATION = 1380 * 60 * 1e3;
|
|
909
|
+
}
|
|
910
|
+
constructor(configuration, connectionPool = []) {
|
|
911
|
+
super();
|
|
912
|
+
this.configuration = configuration;
|
|
913
|
+
this.connectionQueue = [];
|
|
914
|
+
this.queueProcessing = false;
|
|
915
|
+
this.connectionTimers = /* @__PURE__ */ new Map();
|
|
916
|
+
this.roundRobinIndex = 0;
|
|
917
|
+
this.logger = Logger.getInstance();
|
|
918
|
+
this.connectionPool = connectionPool;
|
|
919
|
+
this.mode = this.configuration?.mode ?? "single";
|
|
920
|
+
this.poolSize = this.mode === "pool" && this.configuration?.poolSize ? this.configuration.poolSize : 1;
|
|
921
|
+
if (!connectionPool || connectionPool.length === 0) this.initializePool(this.poolSize);
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Initializes the WebSocket connection pool by creating a specified number of connection objects
|
|
925
|
+
* and adding them to the `connectionPool` array. Each connection object has the following properties:
|
|
926
|
+
* - `closeInitiated`: a boolean indicating whether the connection has been closed
|
|
927
|
+
* - `reconnectionPending`: a boolean indicating whether a reconnection is pending
|
|
928
|
+
* - `pendingRequests`: a Map that tracks pending requests for the connection
|
|
929
|
+
* @param size - The number of connection objects to create and add to the pool.
|
|
930
|
+
* @returns void
|
|
931
|
+
*/
|
|
932
|
+
initializePool(size) {
|
|
933
|
+
for (let i = 0; i < size; i++) this.connectionPool.push({
|
|
934
|
+
id: randomString(),
|
|
935
|
+
closeInitiated: false,
|
|
936
|
+
reconnectionPending: false,
|
|
937
|
+
renewalPending: false,
|
|
938
|
+
pendingRequests: /* @__PURE__ */ new Map(),
|
|
939
|
+
pendingSubscriptions: []
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Retrieves available WebSocket connections based on the connection mode and readiness.
|
|
944
|
+
* In 'single' mode, returns the first connection in the pool.
|
|
945
|
+
* In 'pool' mode, filters and returns connections that are ready for use.
|
|
946
|
+
* @param allowNonEstablishedWebsockets - Optional flag to include non-established WebSocket connections.
|
|
947
|
+
* @returns An array of available WebSocket connections.
|
|
948
|
+
*/
|
|
949
|
+
getAvailableConnections(allowNonEstablishedWebsockets = false) {
|
|
950
|
+
if (this.mode === "single") return [this.connectionPool[0]];
|
|
951
|
+
return this.connectionPool.filter((connection) => this.isConnectionReady(connection, allowNonEstablishedWebsockets));
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Gets a WebSocket connection from the pool or single connection.
|
|
955
|
+
* If the connection mode is 'single', it returns the first connection in the pool.
|
|
956
|
+
* If the connection mode is 'pool', it returns an available connection from the pool,
|
|
957
|
+
* using a round-robin selection strategy. If no available connections are found, it throws an error.
|
|
958
|
+
* @param allowNonEstablishedWebsockets - A boolean indicating whether to allow connections that are not established.
|
|
959
|
+
* @returns {WebsocketConnection} The selected WebSocket connection.
|
|
960
|
+
*/
|
|
961
|
+
getConnection(allowNonEstablishedWebsockets = false) {
|
|
962
|
+
const availableConnections = this.getAvailableConnections(allowNonEstablishedWebsockets);
|
|
963
|
+
if (availableConnections.length === 0) throw new Error("No available Websocket connections are ready.");
|
|
964
|
+
const selectedConnection = availableConnections[this.roundRobinIndex % availableConnections.length];
|
|
965
|
+
this.roundRobinIndex = (this.roundRobinIndex + 1) % availableConnections.length;
|
|
966
|
+
return selectedConnection;
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Checks if the provided WebSocket connection is ready for use.
|
|
970
|
+
* A connection is considered ready if it is open, has no pending reconnection, and has not been closed.
|
|
971
|
+
* @param connection - The WebSocket connection to check.
|
|
972
|
+
* @param allowNonEstablishedWebsockets - An optional flag to allow non-established WebSocket connections.
|
|
973
|
+
* @returns `true` if the connection is ready, `false` otherwise.
|
|
974
|
+
*/
|
|
975
|
+
isConnectionReady(connection, allowNonEstablishedWebsockets = false) {
|
|
976
|
+
return (allowNonEstablishedWebsockets || connection.ws?.readyState === ws.default.OPEN) && !connection.reconnectionPending && !connection.closeInitiated;
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Schedules a timer for a WebSocket connection and tracks it
|
|
980
|
+
* @param connection WebSocket client instance
|
|
981
|
+
* @param callback Function to execute when timer triggers
|
|
982
|
+
* @param delay Time in milliseconds before callback execution
|
|
983
|
+
* @param type Timer type ('timeout' or 'interval')
|
|
984
|
+
* @returns Timer handle
|
|
985
|
+
*/
|
|
986
|
+
scheduleTimer(connection, callback, delay$1, type = "timeout") {
|
|
987
|
+
let timers = this.connectionTimers.get(connection);
|
|
988
|
+
if (!timers) {
|
|
989
|
+
timers = /* @__PURE__ */ new Set();
|
|
990
|
+
this.connectionTimers.set(connection, timers);
|
|
991
|
+
}
|
|
992
|
+
const timerRecord = { type };
|
|
993
|
+
const wrappedTimeout = () => {
|
|
994
|
+
try {
|
|
995
|
+
callback();
|
|
996
|
+
} finally {
|
|
997
|
+
timers.delete(timerRecord);
|
|
998
|
+
}
|
|
999
|
+
};
|
|
1000
|
+
let timer;
|
|
1001
|
+
if (type === "timeout") timer = setTimeout(wrappedTimeout, delay$1);
|
|
1002
|
+
else timer = setInterval(callback, delay$1);
|
|
1003
|
+
timerRecord.timer = timer;
|
|
1004
|
+
timers.add(timerRecord);
|
|
1005
|
+
return timer;
|
|
1006
|
+
}
|
|
1007
|
+
/**
|
|
1008
|
+
* Clears all timers associated with a WebSocket connection.
|
|
1009
|
+
* @param connection - The WebSocket client instance to clear timers for.
|
|
1010
|
+
* @returns void
|
|
1011
|
+
*/
|
|
1012
|
+
clearTimers(connection) {
|
|
1013
|
+
const timers = this.connectionTimers.get(connection);
|
|
1014
|
+
if (timers) {
|
|
1015
|
+
timers.forEach(({ timer, type }) => {
|
|
1016
|
+
if (type === "timeout") clearTimeout(timer);
|
|
1017
|
+
else if (type === "interval") clearInterval(timer);
|
|
1018
|
+
});
|
|
1019
|
+
this.connectionTimers.delete(connection);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
/**
|
|
1023
|
+
* Processes the connection queue, reconnecting or renewing connections as needed.
|
|
1024
|
+
* This method is responsible for iterating through the connection queue and initiating
|
|
1025
|
+
* the reconnection or renewal process for each connection in the queue. It throttles
|
|
1026
|
+
* the queue processing to avoid overwhelming the server with too many connection
|
|
1027
|
+
* requests at once.
|
|
1028
|
+
* @param throttleRate - The time in milliseconds to wait between processing each
|
|
1029
|
+
* connection in the queue.
|
|
1030
|
+
* @returns A Promise that resolves when the queue has been fully processed.
|
|
1031
|
+
*/
|
|
1032
|
+
async processQueue(throttleRate = 1e3) {
|
|
1033
|
+
if (this.queueProcessing) return;
|
|
1034
|
+
this.queueProcessing = true;
|
|
1035
|
+
while (this.connectionQueue.length > 0) {
|
|
1036
|
+
const { connection, url, isRenewal } = this.connectionQueue.shift();
|
|
1037
|
+
this.initConnect(url, isRenewal, connection);
|
|
1038
|
+
await delay(throttleRate);
|
|
1039
|
+
}
|
|
1040
|
+
this.queueProcessing = false;
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* Enqueues a reconnection or renewal for a WebSocket connection.
|
|
1044
|
+
* This method adds the connection, URL, and renewal flag to the connection queue,
|
|
1045
|
+
* and then calls the `processQueue` method to initiate the reconnection or renewal
|
|
1046
|
+
* process.
|
|
1047
|
+
* @param connection - The WebSocket connection to reconnect or renew.
|
|
1048
|
+
* @param url - The URL to use for the reconnection or renewal.
|
|
1049
|
+
* @param isRenewal - A flag indicating whether this is a renewal (true) or a reconnection (false).
|
|
1050
|
+
*/
|
|
1051
|
+
enqueueReconnection(connection, url, isRenewal) {
|
|
1052
|
+
this.connectionQueue.push({
|
|
1053
|
+
connection,
|
|
1054
|
+
url,
|
|
1055
|
+
isRenewal
|
|
1056
|
+
});
|
|
1057
|
+
this.processQueue();
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* Gracefully closes a WebSocket connection after pending requests complete.
|
|
1061
|
+
* This method waits for any pending requests to complete before closing the connection.
|
|
1062
|
+
* It sets up a timeout to force-close the connection after 30 seconds if the pending requests
|
|
1063
|
+
* do not complete. Once all pending requests are completed, the connection is closed.
|
|
1064
|
+
* @param connectionToClose - The WebSocket client instance to close.
|
|
1065
|
+
* @param WebsocketConnectionToClose - The WebSocket connection to close.
|
|
1066
|
+
* @param connection - The WebSocket connection to close.
|
|
1067
|
+
* @returns Promise that resolves when the connection is closed.
|
|
1068
|
+
*/
|
|
1069
|
+
async closeConnectionGracefully(WebsocketConnectionToClose, connection) {
|
|
1070
|
+
if (!WebsocketConnectionToClose || !connection) return;
|
|
1071
|
+
this.logger.debug(`Waiting for pending requests to complete before disconnecting websocket on connection ${connection.id}.`);
|
|
1072
|
+
await new Promise((resolve) => {
|
|
1073
|
+
this.scheduleTimer(WebsocketConnectionToClose, () => {
|
|
1074
|
+
this.logger.warn(`Force-closing websocket connection after 30 seconds on connection ${connection.id}.`);
|
|
1075
|
+
resolve();
|
|
1076
|
+
}, 3e4);
|
|
1077
|
+
this.scheduleTimer(WebsocketConnectionToClose, () => {
|
|
1078
|
+
if (connection.pendingRequests.size === 0) {
|
|
1079
|
+
this.logger.debug(`All pending requests completed, closing websocket connection on connection ${connection.id}.`);
|
|
1080
|
+
resolve();
|
|
1081
|
+
}
|
|
1082
|
+
}, 1e3, "interval");
|
|
1083
|
+
});
|
|
1084
|
+
this.logger.info(`Closing Websocket connection on connection ${connection.id}.`);
|
|
1085
|
+
WebsocketConnectionToClose.close();
|
|
1086
|
+
this.cleanup(WebsocketConnectionToClose);
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Attempts to re-establish a session for a WebSocket connection.
|
|
1090
|
+
* If a session logon request exists and the connection is not already logged on,
|
|
1091
|
+
* it sends an authentication request and updates the connection's logged-on status.
|
|
1092
|
+
* @param connection - The WebSocket connection to re-authenticate.
|
|
1093
|
+
* @private
|
|
1094
|
+
*/
|
|
1095
|
+
async sessionReLogon(connection) {
|
|
1096
|
+
const req = connection.sessionLogonReq;
|
|
1097
|
+
if (req && !connection.isSessionLoggedOn) {
|
|
1098
|
+
const data = buildWebsocketAPIMessage(this.configuration, req.method, req.payload, req.options);
|
|
1099
|
+
this.logger.debug(`Session re-logon on connection ${connection.id}`, data);
|
|
1100
|
+
try {
|
|
1101
|
+
await this.send(JSON.stringify(data), data.id, true, this.configuration.timeout, connection);
|
|
1102
|
+
this.logger.debug(`Session re-logon on connection ${connection.id} was successful.`);
|
|
1103
|
+
connection.isSessionLoggedOn = true;
|
|
1104
|
+
} catch (err) {
|
|
1105
|
+
this.logger.error(`Session re-logon on connection ${connection.id} failed:`, err);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* Cleans up WebSocket connection resources.
|
|
1111
|
+
* Removes all listeners and clears any associated timers for the provided WebSocket client.
|
|
1112
|
+
* @param ws - The WebSocket client to clean up.
|
|
1113
|
+
* @returns void
|
|
1114
|
+
*/
|
|
1115
|
+
cleanup(ws$1) {
|
|
1116
|
+
if (ws$1) {
|
|
1117
|
+
ws$1.removeAllListeners();
|
|
1118
|
+
this.clearTimers(ws$1);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Handles incoming WebSocket messages
|
|
1123
|
+
* @param data Raw message data received
|
|
1124
|
+
* @param connection Websocket connection
|
|
1125
|
+
*/
|
|
1126
|
+
onMessage(data, connection) {
|
|
1127
|
+
this.emit("message", data.toString(), connection);
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* Handles the opening of a WebSocket connection.
|
|
1131
|
+
* @param url - The URL of the WebSocket server.
|
|
1132
|
+
* @param targetConnection - The WebSocket connection being opened.
|
|
1133
|
+
* @param oldWSConnection - The WebSocket client instance associated with the old connection.
|
|
1134
|
+
*/
|
|
1135
|
+
onOpen(url, targetConnection, oldWSConnection) {
|
|
1136
|
+
this.logger.info(`Connected to the Websocket Server with id ${targetConnection.id}: ${url}`);
|
|
1137
|
+
if (targetConnection.renewalPending) {
|
|
1138
|
+
targetConnection.renewalPending = false;
|
|
1139
|
+
this.closeConnectionGracefully(oldWSConnection, targetConnection);
|
|
1140
|
+
} else if (targetConnection.closeInitiated) this.closeConnectionGracefully(targetConnection.ws, targetConnection);
|
|
1141
|
+
else {
|
|
1142
|
+
targetConnection.reconnectionPending = false;
|
|
1143
|
+
this.emit("open", this);
|
|
1144
|
+
}
|
|
1145
|
+
this.sessionReLogon(targetConnection);
|
|
1146
|
+
}
|
|
1147
|
+
/**
|
|
1148
|
+
* Returns the URL to use when reconnecting.
|
|
1149
|
+
* Derived classes should override this to provide dynamic URLs.
|
|
1150
|
+
* @param defaultURL The URL originally passed during the first connection.
|
|
1151
|
+
* @param targetConnection The WebSocket connection being connected.
|
|
1152
|
+
* @returns The URL to reconnect to.
|
|
1153
|
+
*/
|
|
1154
|
+
getReconnectURL(defaultURL, targetConnection) {
|
|
1155
|
+
return defaultURL;
|
|
1156
|
+
}
|
|
1157
|
+
/**
|
|
1158
|
+
* Connects all WebSocket connections in the pool
|
|
1159
|
+
* @param url - The Websocket server URL.
|
|
1160
|
+
* @returns A promise that resolves when all connections are established.
|
|
1161
|
+
*/
|
|
1162
|
+
async connectPool(url) {
|
|
1163
|
+
const connectPromises = this.connectionPool.map((connection) => new Promise((resolve, reject) => {
|
|
1164
|
+
this.initConnect(url, false, connection);
|
|
1165
|
+
connection.ws?.on("open", () => resolve());
|
|
1166
|
+
connection.ws?.on("error", (err) => reject(err));
|
|
1167
|
+
connection.ws?.on("close", () => reject(/* @__PURE__ */ new Error("Connection closed unexpectedly.")));
|
|
1168
|
+
}));
|
|
1169
|
+
await Promise.all(connectPromises);
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Creates a new WebSocket client instance.
|
|
1173
|
+
* @param url - The URL to connect to.
|
|
1174
|
+
* @returns A new WebSocket client instance.
|
|
1175
|
+
*/
|
|
1176
|
+
createWebSocket(url) {
|
|
1177
|
+
const wsClientOptions = {
|
|
1178
|
+
perMessageDeflate: this.configuration?.compression,
|
|
1179
|
+
agent: this.configuration?.agent
|
|
1180
|
+
};
|
|
1181
|
+
if (this.configuration.userAgent) wsClientOptions.headers = { "User-Agent": this.configuration.userAgent };
|
|
1182
|
+
return new ws.default(url, wsClientOptions);
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* Initializes a WebSocket connection.
|
|
1186
|
+
* @param url - The Websocket server URL.
|
|
1187
|
+
* @param isRenewal - Whether this is a connection renewal.
|
|
1188
|
+
* @param connection - An optional WebSocket connection to use.
|
|
1189
|
+
* @returns The WebSocket connection.
|
|
1190
|
+
*/
|
|
1191
|
+
initConnect(url, isRenewal = false, connection) {
|
|
1192
|
+
const targetConnection = connection || this.getConnection();
|
|
1193
|
+
if (targetConnection.renewalPending && isRenewal) {
|
|
1194
|
+
this.logger.warn(`Connection renewal with id ${targetConnection.id} is already in progress`);
|
|
1195
|
+
return;
|
|
1196
|
+
}
|
|
1197
|
+
if (targetConnection.ws && targetConnection.ws.readyState === ws.default.OPEN && !isRenewal) {
|
|
1198
|
+
this.logger.warn(`Connection with id ${targetConnection.id} already exists`);
|
|
1199
|
+
return;
|
|
1200
|
+
}
|
|
1201
|
+
const ws$1 = this.createWebSocket(url);
|
|
1202
|
+
this.logger.info(`Establishing Websocket connection with id ${targetConnection.id} to: ${url}`);
|
|
1203
|
+
if (isRenewal) targetConnection.renewalPending = true;
|
|
1204
|
+
else targetConnection.ws = ws$1;
|
|
1205
|
+
targetConnection.isSessionLoggedOn = false;
|
|
1206
|
+
this.scheduleTimer(ws$1, () => {
|
|
1207
|
+
this.logger.info(`Renewing Websocket connection with id ${targetConnection.id}`);
|
|
1208
|
+
targetConnection.isSessionLoggedOn = false;
|
|
1209
|
+
this.enqueueReconnection(targetConnection, this.getReconnectURL(url, targetConnection), true);
|
|
1210
|
+
}, WebsocketCommon.MAX_CONNECTION_DURATION);
|
|
1211
|
+
ws$1.on("open", () => {
|
|
1212
|
+
const oldWSConnection = targetConnection.ws;
|
|
1213
|
+
if (targetConnection.renewalPending) targetConnection.ws = ws$1;
|
|
1214
|
+
this.onOpen(url, targetConnection, oldWSConnection);
|
|
1215
|
+
});
|
|
1216
|
+
ws$1.on("message", (data) => {
|
|
1217
|
+
this.onMessage(data.toString(), targetConnection);
|
|
1218
|
+
});
|
|
1219
|
+
ws$1.on("ping", () => {
|
|
1220
|
+
this.logger.debug("Received PING from server");
|
|
1221
|
+
this.emit("ping");
|
|
1222
|
+
ws$1.pong();
|
|
1223
|
+
this.logger.debug("Responded PONG to server's PING message");
|
|
1224
|
+
});
|
|
1225
|
+
ws$1.on("pong", () => {
|
|
1226
|
+
this.logger.debug("Received PONG from server");
|
|
1227
|
+
this.emit("pong");
|
|
1228
|
+
});
|
|
1229
|
+
ws$1.on("error", (err) => {
|
|
1230
|
+
this.logger.error("Received error from server");
|
|
1231
|
+
this.logger.error(err);
|
|
1232
|
+
this.emit("error", err);
|
|
1233
|
+
});
|
|
1234
|
+
ws$1.on("close", (closeEventCode, reason) => {
|
|
1235
|
+
this.emit("close", closeEventCode, reason);
|
|
1236
|
+
if (!targetConnection.closeInitiated && !isRenewal) {
|
|
1237
|
+
this.cleanup(ws$1);
|
|
1238
|
+
this.logger.warn(`Connection with id ${targetConnection.id} closed due to ${closeEventCode}: ${reason}`);
|
|
1239
|
+
this.scheduleTimer(ws$1, () => {
|
|
1240
|
+
this.logger.info(`Reconnecting conection with id ${targetConnection.id} to the server.`);
|
|
1241
|
+
targetConnection.isSessionLoggedOn = false;
|
|
1242
|
+
targetConnection.reconnectionPending = true;
|
|
1243
|
+
this.enqueueReconnection(targetConnection, this.getReconnectURL(url, targetConnection), false);
|
|
1244
|
+
}, this.configuration?.reconnectDelay ?? 5e3);
|
|
1245
|
+
}
|
|
1246
|
+
});
|
|
1247
|
+
return targetConnection;
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* Checks if the WebSocket connection is currently open.
|
|
1251
|
+
* @param connection - An optional WebSocket connection to check. If not provided, the entire connection pool is checked.
|
|
1252
|
+
* @returns `true` if the connection is open, `false` otherwise.
|
|
1253
|
+
*/
|
|
1254
|
+
isConnected(connection) {
|
|
1255
|
+
return (connection ? [connection] : this.connectionPool).some((connection$1) => this.isConnectionReady(connection$1));
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Disconnects from the WebSocket server.
|
|
1259
|
+
* If there is no active connection, a warning is logged.
|
|
1260
|
+
* Otherwise, all connections in the connection pool are closed gracefully,
|
|
1261
|
+
* and a message is logged indicating that the connection has been disconnected.
|
|
1262
|
+
* @returns A Promise that resolves when all connections have been closed.
|
|
1263
|
+
* @throws Error if the WebSocket client is not set.
|
|
1264
|
+
*/
|
|
1265
|
+
async disconnect() {
|
|
1266
|
+
if (!this.isConnected()) this.logger.warn("No connection to close.");
|
|
1267
|
+
else {
|
|
1268
|
+
this.connectionPool.forEach((connection) => {
|
|
1269
|
+
connection.closeInitiated = true;
|
|
1270
|
+
connection.isSessionLoggedOn = false;
|
|
1271
|
+
connection.sessionLogonReq = void 0;
|
|
1272
|
+
});
|
|
1273
|
+
const disconnectPromises = this.connectionPool.map((connection) => this.closeConnectionGracefully(connection.ws, connection));
|
|
1274
|
+
await Promise.all(disconnectPromises);
|
|
1275
|
+
this.logger.info("Disconnected with Binance Websocket Server");
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* Sends a ping message to all connected Websocket servers in the pool.
|
|
1280
|
+
* If no connections are ready, a warning is logged.
|
|
1281
|
+
* For each active connection, the ping message is sent, and debug logs provide details.
|
|
1282
|
+
* @throws Error if a Websocket client is not set for a connection.
|
|
1283
|
+
*/
|
|
1284
|
+
pingServer() {
|
|
1285
|
+
const connectedConnections = this.connectionPool.filter((connection) => this.isConnected(connection));
|
|
1286
|
+
if (connectedConnections.length === 0) {
|
|
1287
|
+
this.logger.warn("Ping only can be sent when connection is ready.");
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
this.logger.debug("Sending PING to all connected Websocket servers.");
|
|
1291
|
+
connectedConnections.forEach((connection) => {
|
|
1292
|
+
if (connection.ws) {
|
|
1293
|
+
connection.ws.ping();
|
|
1294
|
+
this.logger.debug(`PING sent to connection with id ${connection.id}`);
|
|
1295
|
+
} else this.logger.error("WebSocket Client not set for a connection.");
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* Sends a payload through the WebSocket connection.
|
|
1300
|
+
* @param payload - Message to send.
|
|
1301
|
+
* @param id - Optional request identifier.
|
|
1302
|
+
* @param promiseBased - Whether to return a promise.
|
|
1303
|
+
* @param timeout - Timeout duration in milliseconds.
|
|
1304
|
+
* @param connection - The WebSocket connection to use.
|
|
1305
|
+
* @returns A promise if `promiseBased` is true, void otherwise.
|
|
1306
|
+
* @throws Error if not connected or WebSocket client is not set.
|
|
1307
|
+
*/
|
|
1308
|
+
send(payload, id, promiseBased = true, timeout = 5e3, connection) {
|
|
1309
|
+
if (!this.isConnected(connection)) {
|
|
1310
|
+
const errorMsg = "Unable to send message — connection is not available.";
|
|
1311
|
+
this.logger.warn(errorMsg);
|
|
1312
|
+
if (promiseBased) return Promise.reject(new Error(errorMsg));
|
|
1313
|
+
else throw new Error(errorMsg);
|
|
1314
|
+
}
|
|
1315
|
+
const connectionToUse = connection ?? this.getConnection();
|
|
1316
|
+
if (!connectionToUse.ws) {
|
|
1317
|
+
const errorMsg = "Websocket Client not set";
|
|
1318
|
+
this.logger.error(errorMsg);
|
|
1319
|
+
if (promiseBased) return Promise.reject(new Error(errorMsg));
|
|
1320
|
+
else throw new Error(errorMsg);
|
|
1321
|
+
}
|
|
1322
|
+
connectionToUse.ws.send(payload);
|
|
1323
|
+
if (promiseBased) return new Promise((resolve, reject) => {
|
|
1324
|
+
if (!id) return reject(/* @__PURE__ */ new Error("id is required for promise-based sending."));
|
|
1325
|
+
const timeoutHandle = setTimeout(() => {
|
|
1326
|
+
if (connectionToUse.pendingRequests.has(id)) {
|
|
1327
|
+
connectionToUse.pendingRequests.delete(id);
|
|
1328
|
+
reject(/* @__PURE__ */ new Error(`Request timeout for id: ${id}`));
|
|
1329
|
+
}
|
|
1330
|
+
}, timeout);
|
|
1331
|
+
connectionToUse.pendingRequests.set(id, {
|
|
1332
|
+
resolve: (v) => {
|
|
1333
|
+
clearTimeout(timeoutHandle);
|
|
1334
|
+
resolve(v);
|
|
1335
|
+
},
|
|
1336
|
+
reject: (e) => {
|
|
1337
|
+
clearTimeout(timeoutHandle);
|
|
1338
|
+
reject(e);
|
|
1339
|
+
}
|
|
1340
|
+
});
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1323
1343
|
};
|
|
1324
1344
|
var WebsocketAPIBase = class extends WebsocketCommon {
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
const responses = await Promise.all(
|
|
1425
|
-
connections.map(
|
|
1426
|
-
(connection) => this.send(
|
|
1427
|
-
JSON.stringify(data),
|
|
1428
|
-
data.id,
|
|
1429
|
-
true,
|
|
1430
|
-
this.configuration.timeout,
|
|
1431
|
-
connection
|
|
1432
|
-
)
|
|
1433
|
-
)
|
|
1434
|
-
);
|
|
1435
|
-
if (isSessionReq && this.configuration.autoSessionReLogon) {
|
|
1436
|
-
connections.forEach((connection) => {
|
|
1437
|
-
if (options.isSessionLogon) {
|
|
1438
|
-
connection.isSessionLoggedOn = true;
|
|
1439
|
-
connection.sessionLogonReq = { method, payload, options };
|
|
1440
|
-
} else {
|
|
1441
|
-
connection.isSessionLoggedOn = false;
|
|
1442
|
-
connection.sessionLogonReq = void 0;
|
|
1443
|
-
}
|
|
1444
|
-
});
|
|
1445
|
-
}
|
|
1446
|
-
return connections.length === 1 && !isSessionReq ? responses[0] : responses;
|
|
1447
|
-
}
|
|
1345
|
+
constructor(configuration, connectionPool = []) {
|
|
1346
|
+
super(configuration, connectionPool);
|
|
1347
|
+
this.isConnecting = false;
|
|
1348
|
+
this.streamCallbackMap = /* @__PURE__ */ new Map();
|
|
1349
|
+
this.logger = Logger.getInstance();
|
|
1350
|
+
this.configuration = configuration;
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Prepares the WebSocket URL by adding optional timeUnit parameter
|
|
1354
|
+
* @param wsUrl The base WebSocket URL
|
|
1355
|
+
* @returns The formatted WebSocket URL with parameters
|
|
1356
|
+
*/
|
|
1357
|
+
prepareURL(wsUrl) {
|
|
1358
|
+
let url = wsUrl;
|
|
1359
|
+
if (this?.configuration.timeUnit) try {
|
|
1360
|
+
const _timeUnit = validateTimeUnit(this.configuration.timeUnit);
|
|
1361
|
+
url = `${url}${url.includes("?") ? "&" : "?"}timeUnit=${_timeUnit}`;
|
|
1362
|
+
} catch (err) {
|
|
1363
|
+
this.logger.error(err);
|
|
1364
|
+
}
|
|
1365
|
+
return url;
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Processes incoming WebSocket messages
|
|
1369
|
+
* @param data The raw message data received
|
|
1370
|
+
*/
|
|
1371
|
+
onMessage(data, connection) {
|
|
1372
|
+
try {
|
|
1373
|
+
const message = (0, json_with_bigint.JSONParse)(data);
|
|
1374
|
+
const { id, status } = message;
|
|
1375
|
+
if (id && connection.pendingRequests.has(id)) {
|
|
1376
|
+
const request = connection.pendingRequests.get(id);
|
|
1377
|
+
connection.pendingRequests.delete(id);
|
|
1378
|
+
if (status && status >= 400) request?.reject(message.error);
|
|
1379
|
+
else {
|
|
1380
|
+
const response = {
|
|
1381
|
+
data: message.result ?? message.response,
|
|
1382
|
+
...message.rateLimits && { rateLimits: message.rateLimits }
|
|
1383
|
+
};
|
|
1384
|
+
request?.resolve(response);
|
|
1385
|
+
}
|
|
1386
|
+
} else if ("event" in message && "e" in message["event"] && this.streamCallbackMap.size > 0) this.streamCallbackMap.forEach((callbacks) => callbacks.forEach((callback) => callback(message["event"])));
|
|
1387
|
+
else this.logger.warn("Received response for unknown or timed-out request:", message);
|
|
1388
|
+
} catch (error) {
|
|
1389
|
+
this.logger.error("Failed to parse WebSocket message:", data, error);
|
|
1390
|
+
}
|
|
1391
|
+
super.onMessage(data, connection);
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Establishes a WebSocket connection to Binance
|
|
1395
|
+
* @returns Promise that resolves when connection is established
|
|
1396
|
+
* @throws Error if connection times out
|
|
1397
|
+
*/
|
|
1398
|
+
connect() {
|
|
1399
|
+
if (this.isConnected()) {
|
|
1400
|
+
this.logger.info("WebSocket connection already established");
|
|
1401
|
+
return Promise.resolve();
|
|
1402
|
+
}
|
|
1403
|
+
return new Promise((resolve, reject) => {
|
|
1404
|
+
if (this.isConnecting) return;
|
|
1405
|
+
this.isConnecting = true;
|
|
1406
|
+
const timeout = setTimeout(() => {
|
|
1407
|
+
this.isConnecting = false;
|
|
1408
|
+
reject(/* @__PURE__ */ new Error("Websocket connection timed out"));
|
|
1409
|
+
}, 1e4);
|
|
1410
|
+
this.connectPool(this.prepareURL(this.configuration.wsURL)).then(() => {
|
|
1411
|
+
this.isConnecting = false;
|
|
1412
|
+
resolve();
|
|
1413
|
+
}).catch((error) => {
|
|
1414
|
+
this.isConnecting = false;
|
|
1415
|
+
reject(error);
|
|
1416
|
+
}).finally(() => {
|
|
1417
|
+
clearTimeout(timeout);
|
|
1418
|
+
});
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
async sendMessage(method, payload = {}, options = {}) {
|
|
1422
|
+
if (!this.isConnected()) throw new Error("Not connected");
|
|
1423
|
+
const isSessionReq = options.isSessionLogon || options.isSessionLogout;
|
|
1424
|
+
const connections = isSessionReq ? this.getAvailableConnections() : [this.getConnection()];
|
|
1425
|
+
const skipAuth = isSessionReq ? false : this.configuration.autoSessionReLogon && connections[0].isSessionLoggedOn;
|
|
1426
|
+
const data = buildWebsocketAPIMessage(this.configuration, method, payload, options, skipAuth);
|
|
1427
|
+
this.logger.debug("Send message to Binance WebSocket API Server:", data);
|
|
1428
|
+
const responses = await Promise.all(connections.map((connection) => this.send(JSON.stringify(data), data.id, true, this.configuration.timeout, connection)));
|
|
1429
|
+
if (isSessionReq && this.configuration.autoSessionReLogon) connections.forEach((connection) => {
|
|
1430
|
+
if (options.isSessionLogon) {
|
|
1431
|
+
connection.isSessionLoggedOn = true;
|
|
1432
|
+
connection.sessionLogonReq = {
|
|
1433
|
+
method,
|
|
1434
|
+
payload,
|
|
1435
|
+
options
|
|
1436
|
+
};
|
|
1437
|
+
} else {
|
|
1438
|
+
connection.isSessionLoggedOn = false;
|
|
1439
|
+
connection.sessionLogonReq = void 0;
|
|
1440
|
+
}
|
|
1441
|
+
});
|
|
1442
|
+
return connections.length === 1 && !isSessionReq ? responses[0] : responses;
|
|
1443
|
+
}
|
|
1448
1444
|
};
|
|
1449
1445
|
var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
}
|
|
1637
|
-
});
|
|
1638
|
-
}
|
|
1639
|
-
/**
|
|
1640
|
-
* Checks if the specified stream is currently subscribed.
|
|
1641
|
-
* @param stream - The name of the stream to check.
|
|
1642
|
-
* @returns `true` if the stream is currently subscribed, `false` otherwise.
|
|
1643
|
-
*/
|
|
1644
|
-
isSubscribed(stream) {
|
|
1645
|
-
return this.streamConnectionMap.has(stream);
|
|
1646
|
-
}
|
|
1446
|
+
constructor(configuration, connectionPool = []) {
|
|
1447
|
+
super(configuration, connectionPool);
|
|
1448
|
+
this.streamConnectionMap = /* @__PURE__ */ new Map();
|
|
1449
|
+
this.streamCallbackMap = /* @__PURE__ */ new Map();
|
|
1450
|
+
this.logger = Logger.getInstance();
|
|
1451
|
+
this.configuration = configuration;
|
|
1452
|
+
this.wsURL = configuration.wsURL;
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* Formats the WebSocket URL for a given stream or streams.
|
|
1456
|
+
* @param streams - Array of stream names to include in the URL.
|
|
1457
|
+
* @returns The formatted WebSocket URL with the provided streams.
|
|
1458
|
+
*/
|
|
1459
|
+
prepareURL(streams = []) {
|
|
1460
|
+
let url = `${this.wsURL}/stream?streams=${streams.join("/")}`;
|
|
1461
|
+
if (this.configuration?.timeUnit) try {
|
|
1462
|
+
const _timeUnit = validateTimeUnit(this.configuration.timeUnit);
|
|
1463
|
+
url = `${url}${url.includes("?") ? "&" : "?"}timeUnit=${_timeUnit}`;
|
|
1464
|
+
} catch (err) {
|
|
1465
|
+
this.logger.error(err);
|
|
1466
|
+
}
|
|
1467
|
+
return url;
|
|
1468
|
+
}
|
|
1469
|
+
/**
|
|
1470
|
+
* Formats the WebSocket URL with stream and configuration parameters to be used for reconnection.
|
|
1471
|
+
* @param url - The base WebSocket URL.
|
|
1472
|
+
* @param targetConnection - The target WebSocket connection.
|
|
1473
|
+
* @returns The formatted WebSocket URL with streams and optional parameters.
|
|
1474
|
+
*/
|
|
1475
|
+
getReconnectURL(url, targetConnection) {
|
|
1476
|
+
const streams = Array.from(this.streamConnectionMap.keys()).filter((stream) => this.streamConnectionMap.get(stream) === targetConnection);
|
|
1477
|
+
return this.prepareURL(streams);
|
|
1478
|
+
}
|
|
1479
|
+
/**
|
|
1480
|
+
* Handles subscription to streams and assigns them to specific connections
|
|
1481
|
+
* @param streams Array of stream names to subscribe to
|
|
1482
|
+
* @returns Map of connections to streams
|
|
1483
|
+
*/
|
|
1484
|
+
handleStreamAssignment(streams) {
|
|
1485
|
+
const connectionStreamMap = /* @__PURE__ */ new Map();
|
|
1486
|
+
streams.forEach((stream) => {
|
|
1487
|
+
if (!this.streamCallbackMap.has(stream)) this.streamCallbackMap.set(stream, /* @__PURE__ */ new Set());
|
|
1488
|
+
let connection = this.streamConnectionMap.get(stream);
|
|
1489
|
+
if (!connection || connection.closeInitiated || connection.reconnectionPending) {
|
|
1490
|
+
connection = this.getConnection(true);
|
|
1491
|
+
this.streamConnectionMap.set(stream, connection);
|
|
1492
|
+
}
|
|
1493
|
+
if (!connectionStreamMap.has(connection)) connectionStreamMap.set(connection, []);
|
|
1494
|
+
connectionStreamMap.get(connection)?.push(stream);
|
|
1495
|
+
});
|
|
1496
|
+
return connectionStreamMap;
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Sends a subscription payload for specified streams on a given connection.
|
|
1500
|
+
* @param connection The WebSocket connection to use for sending the subscription.
|
|
1501
|
+
* @param streams The streams to subscribe to.
|
|
1502
|
+
* @param id Optional ID for the subscription.
|
|
1503
|
+
*/
|
|
1504
|
+
sendSubscriptionPayload(connection, streams, id) {
|
|
1505
|
+
const payload = {
|
|
1506
|
+
method: "SUBSCRIBE",
|
|
1507
|
+
params: streams,
|
|
1508
|
+
id: id && /^[0-9a-f]{32}$/.test(id) ? id : randomString()
|
|
1509
|
+
};
|
|
1510
|
+
this.logger.debug("SUBSCRIBE", payload);
|
|
1511
|
+
this.send(JSON.stringify(payload), void 0, false, 0, connection);
|
|
1512
|
+
}
|
|
1513
|
+
/**
|
|
1514
|
+
* Processes pending subscriptions for a given connection.
|
|
1515
|
+
* Sends all queued subscriptions in a single payload.
|
|
1516
|
+
* @param connection The WebSocket connection to process.
|
|
1517
|
+
*/
|
|
1518
|
+
processPendingSubscriptions(connection) {
|
|
1519
|
+
if (connection.pendingSubscriptions && connection.pendingSubscriptions.length > 0) {
|
|
1520
|
+
this.logger.info("Processing queued subscriptions for connection");
|
|
1521
|
+
this.sendSubscriptionPayload(connection, connection.pendingSubscriptions);
|
|
1522
|
+
connection.pendingSubscriptions = [];
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
/**
|
|
1526
|
+
* Handles incoming WebSocket messages, parsing the data and invoking the appropriate callback function.
|
|
1527
|
+
* If the message contains a stream name that is registered in the `streamCallbackMap`, the corresponding
|
|
1528
|
+
* callback function is called with the message data.
|
|
1529
|
+
* If the message cannot be parsed, an error is logged.
|
|
1530
|
+
* @param data The raw WebSocket message data.
|
|
1531
|
+
* @param connection The WebSocket connection that received the message.
|
|
1532
|
+
*/
|
|
1533
|
+
onMessage(data, connection) {
|
|
1534
|
+
try {
|
|
1535
|
+
const parsedData = (0, json_with_bigint.JSONParse)(data);
|
|
1536
|
+
const streamName = parsedData?.stream;
|
|
1537
|
+
if (streamName && this.streamCallbackMap.has(streamName)) this.streamCallbackMap.get(streamName)?.forEach((callback) => callback(parsedData.data));
|
|
1538
|
+
} catch (error) {
|
|
1539
|
+
this.logger.error("Failed to parse WebSocket message:", data, error);
|
|
1540
|
+
}
|
|
1541
|
+
super.onMessage(data, connection);
|
|
1542
|
+
}
|
|
1543
|
+
/**
|
|
1544
|
+
* Called when the WebSocket connection is opened.
|
|
1545
|
+
* Processes any pending subscriptions for the target connection.
|
|
1546
|
+
* @param url The URL of the WebSocket connection.
|
|
1547
|
+
* @param targetConnection The WebSocket connection that was opened.
|
|
1548
|
+
* @param oldConnection The previous WebSocket connection, if any.
|
|
1549
|
+
*/
|
|
1550
|
+
onOpen(url, targetConnection, oldWSConnection) {
|
|
1551
|
+
this.processPendingSubscriptions(targetConnection);
|
|
1552
|
+
super.onOpen(url, targetConnection, oldWSConnection);
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* Connects to the WebSocket server and subscribes to the specified streams.
|
|
1556
|
+
* This method returns a Promise that resolves when the connection is established,
|
|
1557
|
+
* or rejects with an error if the connection fails to be established within 10 seconds.
|
|
1558
|
+
* @param stream - A single stream name or an array of stream names to subscribe to.
|
|
1559
|
+
* @returns A Promise that resolves when the connection is established.
|
|
1560
|
+
*/
|
|
1561
|
+
connect(stream = []) {
|
|
1562
|
+
const streams = Array.isArray(stream) ? stream : [stream];
|
|
1563
|
+
return new Promise((resolve, reject) => {
|
|
1564
|
+
const timeout = setTimeout(() => {
|
|
1565
|
+
reject(/* @__PURE__ */ new Error("Websocket connection timed out"));
|
|
1566
|
+
}, 1e4);
|
|
1567
|
+
this.connectPool(this.prepareURL(streams)).then(() => resolve()).catch((error) => reject(error)).finally(() => clearTimeout(timeout));
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
/**
|
|
1571
|
+
* Disconnects the WebSocket connection and clears the stream callback map.
|
|
1572
|
+
* This method is called to clean up the connection and associated resources.
|
|
1573
|
+
*/
|
|
1574
|
+
async disconnect() {
|
|
1575
|
+
this.streamCallbackMap.clear();
|
|
1576
|
+
this.streamConnectionMap.clear();
|
|
1577
|
+
super.disconnect();
|
|
1578
|
+
}
|
|
1579
|
+
/**
|
|
1580
|
+
* Subscribes to one or multiple WebSocket streams
|
|
1581
|
+
* Handles both single and pool modes
|
|
1582
|
+
* @param stream Single stream name or array of stream names to subscribe to
|
|
1583
|
+
* @param id Optional subscription ID
|
|
1584
|
+
* @returns void
|
|
1585
|
+
*/
|
|
1586
|
+
subscribe(stream, id) {
|
|
1587
|
+
const streams = (Array.isArray(stream) ? stream : [stream]).filter((stream$1) => !this.streamConnectionMap.has(stream$1));
|
|
1588
|
+
this.handleStreamAssignment(streams).forEach((streams$1, connection) => {
|
|
1589
|
+
if (!this.isConnected(connection)) {
|
|
1590
|
+
this.logger.info(`Connection ${connection.id} is not ready. Queuing subscription for streams: ${streams$1}`);
|
|
1591
|
+
connection.pendingSubscriptions?.push(...streams$1);
|
|
1592
|
+
return;
|
|
1593
|
+
}
|
|
1594
|
+
this.sendSubscriptionPayload(connection, streams$1, id);
|
|
1595
|
+
});
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* Unsubscribes from one or multiple WebSocket streams
|
|
1599
|
+
* Handles both single and pool modes
|
|
1600
|
+
* @param stream Single stream name or array of stream names to unsubscribe from
|
|
1601
|
+
* @param id Optional unsubscription ID
|
|
1602
|
+
* @returns void
|
|
1603
|
+
*/
|
|
1604
|
+
unsubscribe(stream, id) {
|
|
1605
|
+
(Array.isArray(stream) ? stream : [stream]).forEach((stream$1) => {
|
|
1606
|
+
const connection = this.streamConnectionMap.get(stream$1);
|
|
1607
|
+
if (!connection || !connection.ws || !this.isConnected(connection)) {
|
|
1608
|
+
this.logger.warn(`Stream ${stream$1} not associated with an active connection.`);
|
|
1609
|
+
return;
|
|
1610
|
+
}
|
|
1611
|
+
if (!this.streamCallbackMap.has(stream$1) || this.streamCallbackMap.get(stream$1)?.size === 0) {
|
|
1612
|
+
const payload = {
|
|
1613
|
+
method: "UNSUBSCRIBE",
|
|
1614
|
+
params: [stream$1],
|
|
1615
|
+
id: id && /^[0-9a-f]{32}$/.test(id) ? id : randomString()
|
|
1616
|
+
};
|
|
1617
|
+
this.logger.debug("UNSUBSCRIBE", payload);
|
|
1618
|
+
this.send(JSON.stringify(payload), void 0, false, 0, connection);
|
|
1619
|
+
this.streamConnectionMap.delete(stream$1);
|
|
1620
|
+
this.streamCallbackMap.delete(stream$1);
|
|
1621
|
+
}
|
|
1622
|
+
});
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Checks if the specified stream is currently subscribed.
|
|
1626
|
+
* @param stream - The name of the stream to check.
|
|
1627
|
+
* @returns `true` if the stream is currently subscribed, `false` otherwise.
|
|
1628
|
+
*/
|
|
1629
|
+
isSubscribed(stream) {
|
|
1630
|
+
return this.streamConnectionMap.has(stream);
|
|
1631
|
+
}
|
|
1647
1632
|
};
|
|
1633
|
+
/**
|
|
1634
|
+
* Creates a WebSocket stream handler for managing stream subscriptions and callbacks.
|
|
1635
|
+
*
|
|
1636
|
+
* @template T The type of data expected in the stream messages
|
|
1637
|
+
* @param {WebsocketAPIBase | WebsocketStreamsBase} websocketBase The WebSocket base instance
|
|
1638
|
+
* @param {string} streamOrId The stream identifier
|
|
1639
|
+
* @param {string} [id] Optional additional identifier
|
|
1640
|
+
* @returns {WebsocketStream<T>} A stream handler with methods to register callbacks and unsubscribe
|
|
1641
|
+
*/
|
|
1648
1642
|
function createStreamHandler(websocketBase, streamOrId, id) {
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
}
|
|
1670
|
-
};
|
|
1643
|
+
if (websocketBase instanceof WebsocketStreamsBase) websocketBase.subscribe(streamOrId, id);
|
|
1644
|
+
let registeredCallback;
|
|
1645
|
+
return {
|
|
1646
|
+
on: (event, callback) => {
|
|
1647
|
+
if (event === "message") {
|
|
1648
|
+
registeredCallback = (data) => {
|
|
1649
|
+
Promise.resolve(callback(data)).catch((err) => {
|
|
1650
|
+
websocketBase.logger.error(`Error in stream callback: ${err}`);
|
|
1651
|
+
});
|
|
1652
|
+
};
|
|
1653
|
+
const callbackSet = websocketBase.streamCallbackMap.get(streamOrId) ?? /* @__PURE__ */ new Set();
|
|
1654
|
+
callbackSet.add(registeredCallback);
|
|
1655
|
+
websocketBase.streamCallbackMap.set(streamOrId, callbackSet);
|
|
1656
|
+
}
|
|
1657
|
+
},
|
|
1658
|
+
unsubscribe: () => {
|
|
1659
|
+
if (registeredCallback) websocketBase.streamCallbackMap.get(streamOrId)?.delete(registeredCallback);
|
|
1660
|
+
if (websocketBase instanceof WebsocketStreamsBase) websocketBase.unsubscribe(streamOrId, id);
|
|
1661
|
+
}
|
|
1662
|
+
};
|
|
1671
1663
|
}
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
});
|
|
1664
|
+
|
|
1665
|
+
//#endregion
|
|
1666
|
+
exports.ALGO_REST_API_PROD_URL = ALGO_REST_API_PROD_URL;
|
|
1667
|
+
exports.BadRequestError = BadRequestError;
|
|
1668
|
+
exports.C2C_REST_API_PROD_URL = C2C_REST_API_PROD_URL;
|
|
1669
|
+
exports.CONVERT_REST_API_PROD_URL = CONVERT_REST_API_PROD_URL;
|
|
1670
|
+
exports.COPY_TRADING_REST_API_PROD_URL = COPY_TRADING_REST_API_PROD_URL;
|
|
1671
|
+
exports.CRYPTO_LOAN_REST_API_PROD_URL = CRYPTO_LOAN_REST_API_PROD_URL;
|
|
1672
|
+
exports.ConfigurationRestAPI = ConfigurationRestAPI;
|
|
1673
|
+
exports.ConfigurationWebsocketAPI = ConfigurationWebsocketAPI;
|
|
1674
|
+
exports.ConfigurationWebsocketStreams = ConfigurationWebsocketStreams;
|
|
1675
|
+
exports.ConnectorClientError = ConnectorClientError;
|
|
1676
|
+
exports.DERIVATIVES_TRADING_COIN_FUTURES_REST_API_PROD_URL = DERIVATIVES_TRADING_COIN_FUTURES_REST_API_PROD_URL;
|
|
1677
|
+
exports.DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL = DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL;
|
|
1678
|
+
exports.DERIVATIVES_TRADING_COIN_FUTURES_WS_API_PROD_URL = DERIVATIVES_TRADING_COIN_FUTURES_WS_API_PROD_URL;
|
|
1679
|
+
exports.DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL = DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL;
|
|
1680
|
+
exports.DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL = DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL;
|
|
1681
|
+
exports.DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL = DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL;
|
|
1682
|
+
exports.DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL = DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL;
|
|
1683
|
+
exports.DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL = DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL;
|
|
1684
|
+
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL;
|
|
1685
|
+
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL;
|
|
1686
|
+
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL;
|
|
1687
|
+
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL;
|
|
1688
|
+
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL;
|
|
1689
|
+
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL;
|
|
1690
|
+
exports.DERIVATIVES_TRADING_USDS_FUTURES_REST_API_PROD_URL = DERIVATIVES_TRADING_USDS_FUTURES_REST_API_PROD_URL;
|
|
1691
|
+
exports.DERIVATIVES_TRADING_USDS_FUTURES_REST_API_TESTNET_URL = DERIVATIVES_TRADING_USDS_FUTURES_REST_API_TESTNET_URL;
|
|
1692
|
+
exports.DERIVATIVES_TRADING_USDS_FUTURES_WS_API_PROD_URL = DERIVATIVES_TRADING_USDS_FUTURES_WS_API_PROD_URL;
|
|
1693
|
+
exports.DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL = DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL;
|
|
1694
|
+
exports.DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL = DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL;
|
|
1695
|
+
exports.DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL = DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL;
|
|
1696
|
+
exports.DUAL_INVESTMENT_REST_API_PROD_URL = DUAL_INVESTMENT_REST_API_PROD_URL;
|
|
1697
|
+
exports.FIAT_REST_API_PROD_URL = FIAT_REST_API_PROD_URL;
|
|
1698
|
+
exports.ForbiddenError = ForbiddenError;
|
|
1699
|
+
exports.GIFT_CARD_REST_API_PROD_URL = GIFT_CARD_REST_API_PROD_URL;
|
|
1700
|
+
exports.LogLevel = LogLevel;
|
|
1701
|
+
exports.Logger = Logger;
|
|
1702
|
+
exports.MARGIN_TRADING_REST_API_PROD_URL = MARGIN_TRADING_REST_API_PROD_URL;
|
|
1703
|
+
exports.MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL = MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL;
|
|
1704
|
+
exports.MARGIN_TRADING_WS_STREAMS_PROD_URL = MARGIN_TRADING_WS_STREAMS_PROD_URL;
|
|
1705
|
+
exports.MINING_REST_API_PROD_URL = MINING_REST_API_PROD_URL;
|
|
1706
|
+
exports.NFT_REST_API_PROD_URL = NFT_REST_API_PROD_URL;
|
|
1707
|
+
exports.NetworkError = NetworkError;
|
|
1708
|
+
exports.NotFoundError = NotFoundError;
|
|
1709
|
+
exports.PAY_REST_API_PROD_URL = PAY_REST_API_PROD_URL;
|
|
1710
|
+
exports.REBATE_REST_API_PROD_URL = REBATE_REST_API_PROD_URL;
|
|
1711
|
+
exports.RateLimitBanError = RateLimitBanError;
|
|
1712
|
+
exports.RequiredError = RequiredError;
|
|
1713
|
+
exports.SIMPLE_EARN_REST_API_PROD_URL = SIMPLE_EARN_REST_API_PROD_URL;
|
|
1714
|
+
exports.SPOT_REST_API_MARKET_URL = SPOT_REST_API_MARKET_URL;
|
|
1715
|
+
exports.SPOT_REST_API_PROD_URL = SPOT_REST_API_PROD_URL;
|
|
1716
|
+
exports.SPOT_REST_API_TESTNET_URL = SPOT_REST_API_TESTNET_URL;
|
|
1717
|
+
exports.SPOT_WS_API_PROD_URL = SPOT_WS_API_PROD_URL;
|
|
1718
|
+
exports.SPOT_WS_API_TESTNET_URL = SPOT_WS_API_TESTNET_URL;
|
|
1719
|
+
exports.SPOT_WS_STREAMS_MARKET_URL = SPOT_WS_STREAMS_MARKET_URL;
|
|
1720
|
+
exports.SPOT_WS_STREAMS_PROD_URL = SPOT_WS_STREAMS_PROD_URL;
|
|
1721
|
+
exports.SPOT_WS_STREAMS_TESTNET_URL = SPOT_WS_STREAMS_TESTNET_URL;
|
|
1722
|
+
exports.STAKING_REST_API_PROD_URL = STAKING_REST_API_PROD_URL;
|
|
1723
|
+
exports.SUB_ACCOUNT_REST_API_PROD_URL = SUB_ACCOUNT_REST_API_PROD_URL;
|
|
1724
|
+
exports.ServerError = ServerError;
|
|
1725
|
+
exports.TimeUnit = TimeUnit;
|
|
1726
|
+
exports.TooManyRequestsError = TooManyRequestsError;
|
|
1727
|
+
exports.UnauthorizedError = UnauthorizedError;
|
|
1728
|
+
exports.VIP_LOAN_REST_API_PROD_URL = VIP_LOAN_REST_API_PROD_URL;
|
|
1729
|
+
exports.WALLET_REST_API_PROD_URL = WALLET_REST_API_PROD_URL;
|
|
1730
|
+
exports.WebsocketAPIBase = WebsocketAPIBase;
|
|
1731
|
+
exports.WebsocketCommon = WebsocketCommon;
|
|
1732
|
+
exports.WebsocketEventEmitter = WebsocketEventEmitter;
|
|
1733
|
+
exports.WebsocketStreamsBase = WebsocketStreamsBase;
|
|
1734
|
+
exports.assertParamExists = assertParamExists;
|
|
1735
|
+
exports.buildQueryString = buildQueryString;
|
|
1736
|
+
exports.buildUserAgent = buildUserAgent;
|
|
1737
|
+
exports.buildWebsocketAPIMessage = buildWebsocketAPIMessage;
|
|
1738
|
+
exports.clearSignerCache = clearSignerCache;
|
|
1739
|
+
exports.createStreamHandler = createStreamHandler;
|
|
1740
|
+
exports.delay = delay;
|
|
1741
|
+
exports.getSignature = getSignature;
|
|
1742
|
+
exports.getTimestamp = getTimestamp;
|
|
1743
|
+
exports.httpRequestFunction = httpRequestFunction;
|
|
1744
|
+
exports.normalizeScientificNumbers = normalizeScientificNumbers;
|
|
1745
|
+
exports.parseCustomHeaders = parseCustomHeaders;
|
|
1746
|
+
exports.parseRateLimitHeaders = parseRateLimitHeaders;
|
|
1747
|
+
exports.randomString = randomString;
|
|
1748
|
+
exports.removeEmptyValue = removeEmptyValue;
|
|
1749
|
+
exports.replaceWebsocketStreamsPlaceholders = replaceWebsocketStreamsPlaceholders;
|
|
1750
|
+
exports.sanitizeHeaderValue = sanitizeHeaderValue;
|
|
1751
|
+
exports.sendRequest = sendRequest;
|
|
1752
|
+
exports.setSearchParams = setSearchParams;
|
|
1753
|
+
exports.shouldRetryRequest = shouldRetryRequest;
|
|
1754
|
+
exports.sortObject = sortObject;
|
|
1755
|
+
exports.toPathString = toPathString;
|
|
1756
|
+
exports.validateTimeUnit = validateTimeUnit;
|
|
1766
1757
|
//# sourceMappingURL=index.js.map
|