@binance/common 1.0.1 → 1.0.2
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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +36 -59
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +33 -56
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -2
package/dist/index.mjs
CHANGED
|
@@ -251,8 +251,7 @@ import fs from "fs";
|
|
|
251
251
|
import https from "https";
|
|
252
252
|
import globalAxios from "axios";
|
|
253
253
|
function buildQueryString(params) {
|
|
254
|
-
if (!params)
|
|
255
|
-
return "";
|
|
254
|
+
if (!params) return "";
|
|
256
255
|
return Object.entries(params).map(stringifyKeyValuePair).join("&");
|
|
257
256
|
}
|
|
258
257
|
function stringifyKeyValuePair([key, value]) {
|
|
@@ -320,8 +319,7 @@ var assertParamExists = function(functionName, paramName, paramValue) {
|
|
|
320
319
|
}
|
|
321
320
|
};
|
|
322
321
|
function setFlattenedQueryParams(urlSearchParams, parameter, key = "") {
|
|
323
|
-
if (parameter == null)
|
|
324
|
-
return;
|
|
322
|
+
if (parameter == null) return;
|
|
325
323
|
if (typeof parameter === "object") {
|
|
326
324
|
if (Array.isArray(parameter)) {
|
|
327
325
|
parameter.forEach((item) => setFlattenedQueryParams(urlSearchParams, item, key));
|
|
@@ -433,8 +431,7 @@ var httpRequestFunction = async function(axiosArgs, configuration) {
|
|
|
433
431
|
} else {
|
|
434
432
|
if (retries > 0 && attempt >= retries)
|
|
435
433
|
lastError = new Error(`Request failed after ${retries} retries`);
|
|
436
|
-
else
|
|
437
|
-
lastError = new NetworkError("Network error or request timeout.");
|
|
434
|
+
else lastError = new NetworkError("Network error or request timeout.");
|
|
438
435
|
break;
|
|
439
436
|
}
|
|
440
437
|
}
|
|
@@ -446,8 +443,7 @@ var parseRateLimitHeaders = function(headers) {
|
|
|
446
443
|
const rateLimits = [];
|
|
447
444
|
const parseIntervalDetails = (key) => {
|
|
448
445
|
const match = key.match(/x-mbx-used-weight-(\d+)([smhd])|x-mbx-order-count-(\d+)([smhd])/i);
|
|
449
|
-
if (!match)
|
|
450
|
-
return null;
|
|
446
|
+
if (!match) return null;
|
|
451
447
|
const intervalNum = parseInt(match[1] || match[3], 10);
|
|
452
448
|
const intervalLetter = (match[2] || match[4])?.toUpperCase();
|
|
453
449
|
let interval;
|
|
@@ -471,8 +467,7 @@ var parseRateLimitHeaders = function(headers) {
|
|
|
471
467
|
};
|
|
472
468
|
for (const [key, value] of Object.entries(headers)) {
|
|
473
469
|
const normalizedKey = key.toLowerCase();
|
|
474
|
-
if (value === void 0)
|
|
475
|
-
continue;
|
|
470
|
+
if (value === void 0) continue;
|
|
476
471
|
if (normalizedKey.startsWith("x-mbx-used-weight-")) {
|
|
477
472
|
const details = parseIntervalDetails(normalizedKey);
|
|
478
473
|
if (details) {
|
|
@@ -535,8 +530,7 @@ var sendRequest = function(configuration, endpoint, method, params = {}, timeUni
|
|
|
535
530
|
);
|
|
536
531
|
};
|
|
537
532
|
function removeEmptyValue(obj) {
|
|
538
|
-
if (!(obj instanceof Object))
|
|
539
|
-
return {};
|
|
533
|
+
if (!(obj instanceof Object)) return {};
|
|
540
534
|
return Object.fromEntries(
|
|
541
535
|
Object.entries(obj).filter(
|
|
542
536
|
([, value]) => value !== null && value !== void 0 && value !== ""
|
|
@@ -552,12 +546,13 @@ function sortObject(obj) {
|
|
|
552
546
|
function replaceWebsocketStreamsPlaceholders(str, variables) {
|
|
553
547
|
const normalizedVariables = Object.keys(variables).reduce(
|
|
554
548
|
(acc, key) => {
|
|
555
|
-
|
|
549
|
+
const normalizedKey = key.toLowerCase().replace(/[-_]/g, "");
|
|
550
|
+
acc[normalizedKey] = variables[key];
|
|
556
551
|
return acc;
|
|
557
552
|
},
|
|
558
553
|
{}
|
|
559
554
|
);
|
|
560
|
-
return str.replace(
|
|
555
|
+
return str.replace(/(@)?<([^>]+)>/g, (match, precedingAt, fieldName) => {
|
|
561
556
|
const normalizedFieldName = fieldName.toLowerCase().replace(/[-_]/g, "");
|
|
562
557
|
if (Object.prototype.hasOwnProperty.call(normalizedVariables, normalizedFieldName) && normalizedVariables[normalizedFieldName] != null) {
|
|
563
558
|
const value = normalizedVariables[normalizedFieldName];
|
|
@@ -568,7 +563,7 @@ function replaceWebsocketStreamsPlaceholders(str, variables) {
|
|
|
568
563
|
case "updatespeed":
|
|
569
564
|
return `@${value}`;
|
|
570
565
|
default:
|
|
571
|
-
return value;
|
|
566
|
+
return (precedingAt || "") + value;
|
|
572
567
|
}
|
|
573
568
|
}
|
|
574
569
|
return "";
|
|
@@ -614,7 +609,7 @@ var WebsocketEventEmitter = class {
|
|
|
614
609
|
this.eventEmitter.emit(event, ...args);
|
|
615
610
|
}
|
|
616
611
|
};
|
|
617
|
-
var
|
|
612
|
+
var WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
618
613
|
constructor(configuration, connectionPool = []) {
|
|
619
614
|
super();
|
|
620
615
|
this.configuration = configuration;
|
|
@@ -626,8 +621,10 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
626
621
|
this.connectionPool = connectionPool;
|
|
627
622
|
this.mode = this.configuration?.mode ?? "single";
|
|
628
623
|
this.poolSize = this.mode === "pool" && this.configuration?.poolSize ? this.configuration.poolSize : 1;
|
|
629
|
-
if (!connectionPool || connectionPool.length === 0)
|
|
630
|
-
|
|
624
|
+
if (!connectionPool || connectionPool.length === 0) this.initializePool(this.poolSize);
|
|
625
|
+
}
|
|
626
|
+
static {
|
|
627
|
+
this.MAX_CONNECTION_DURATION = 23 * 60 * 60 * 1e3;
|
|
631
628
|
}
|
|
632
629
|
/**
|
|
633
630
|
* Initializes the WebSocket connection pool by creating a specified number of connection objects
|
|
@@ -659,8 +656,7 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
659
656
|
* @returns {WebsocketConnection} The selected WebSocket connection.
|
|
660
657
|
*/
|
|
661
658
|
getConnection(allowNonEstablishedWebsockets = false) {
|
|
662
|
-
if (this.mode === "single")
|
|
663
|
-
return this.connectionPool[0];
|
|
659
|
+
if (this.mode === "single") return this.connectionPool[0];
|
|
664
660
|
const availableConnections = this.connectionPool.filter(
|
|
665
661
|
(connection) => this.isConnectionReady(connection, allowNonEstablishedWebsockets)
|
|
666
662
|
);
|
|
@@ -705,10 +701,8 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
705
701
|
const timers = this.connectionTimers.get(connection);
|
|
706
702
|
if (timers) {
|
|
707
703
|
timers.forEach(({ timer, type }) => {
|
|
708
|
-
if (type === "timeout")
|
|
709
|
-
|
|
710
|
-
else if (type === "interval")
|
|
711
|
-
clearInterval(timer);
|
|
704
|
+
if (type === "timeout") clearTimeout(timer);
|
|
705
|
+
else if (type === "interval") clearInterval(timer);
|
|
712
706
|
});
|
|
713
707
|
this.connectionTimers.delete(connection);
|
|
714
708
|
}
|
|
@@ -724,8 +718,7 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
724
718
|
* @returns A Promise that resolves when the queue has been fully processed.
|
|
725
719
|
*/
|
|
726
720
|
async processQueue(throttleRate = 1e3) {
|
|
727
|
-
if (this.queueProcessing)
|
|
728
|
-
return;
|
|
721
|
+
if (this.queueProcessing) return;
|
|
729
722
|
this.queueProcessing = true;
|
|
730
723
|
while (this.connectionQueue.length > 0) {
|
|
731
724
|
const { connection, url, isRenewal } = this.connectionQueue.shift();
|
|
@@ -758,8 +751,7 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
758
751
|
* @returns Promise that resolves when the connection is closed.
|
|
759
752
|
*/
|
|
760
753
|
async closeConnectionGracefully(WebsocketConnectionToClose, connection) {
|
|
761
|
-
if (!WebsocketConnectionToClose || !connection)
|
|
762
|
-
return;
|
|
754
|
+
if (!WebsocketConnectionToClose || !connection) return;
|
|
763
755
|
this.logger.debug("Waiting for pending requests to complete before disconnecting.");
|
|
764
756
|
const closePromise = new Promise((resolve) => {
|
|
765
757
|
this.scheduleTimer(
|
|
@@ -892,10 +884,8 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
892
884
|
this.logger.info(
|
|
893
885
|
`Establishing Websocket connection with id ${targetConnection.id} to: ${url}`
|
|
894
886
|
);
|
|
895
|
-
if (isRenewal)
|
|
896
|
-
|
|
897
|
-
else
|
|
898
|
-
targetConnection.ws = ws;
|
|
887
|
+
if (isRenewal) targetConnection.renewalPending = true;
|
|
888
|
+
else targetConnection.ws = ws;
|
|
899
889
|
this.scheduleTimer(
|
|
900
890
|
ws,
|
|
901
891
|
() => {
|
|
@@ -910,8 +900,7 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
910
900
|
);
|
|
911
901
|
ws.on("open", () => {
|
|
912
902
|
const oldWSConnection = targetConnection.ws;
|
|
913
|
-
if (targetConnection.renewalPending)
|
|
914
|
-
targetConnection.ws = ws;
|
|
903
|
+
if (targetConnection.renewalPending) targetConnection.ws = ws;
|
|
915
904
|
this.onOpen(url, targetConnection, oldWSConnection);
|
|
916
905
|
});
|
|
917
906
|
ws.on("message", (data) => {
|
|
@@ -975,8 +964,7 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
975
964
|
* @throws Error if the WebSocket client is not set.
|
|
976
965
|
*/
|
|
977
966
|
async disconnect() {
|
|
978
|
-
if (!this.isConnected())
|
|
979
|
-
this.logger.warn("No connection to close.");
|
|
967
|
+
if (!this.isConnected()) this.logger.warn("No connection to close.");
|
|
980
968
|
else {
|
|
981
969
|
this.connectionPool.forEach((connection) => {
|
|
982
970
|
connection.closeInitiated = true;
|
|
@@ -1026,25 +1014,20 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
1026
1014
|
if (!this.isConnected(connection)) {
|
|
1027
1015
|
const errorMsg = "Send can only be sent when connection is ready.";
|
|
1028
1016
|
this.logger.warn(errorMsg);
|
|
1029
|
-
if (promiseBased)
|
|
1030
|
-
|
|
1031
|
-
else
|
|
1032
|
-
throw new Error(errorMsg);
|
|
1017
|
+
if (promiseBased) return Promise.reject(new Error(errorMsg));
|
|
1018
|
+
else throw new Error(errorMsg);
|
|
1033
1019
|
}
|
|
1034
1020
|
const connectionToUse = connection ?? this.getConnection();
|
|
1035
1021
|
if (!connectionToUse.ws) {
|
|
1036
1022
|
const errorMsg = "Websocket Client not set";
|
|
1037
1023
|
this.logger.error(errorMsg);
|
|
1038
|
-
if (promiseBased)
|
|
1039
|
-
|
|
1040
|
-
else
|
|
1041
|
-
throw new Error(errorMsg);
|
|
1024
|
+
if (promiseBased) return Promise.reject(new Error(errorMsg));
|
|
1025
|
+
else throw new Error(errorMsg);
|
|
1042
1026
|
}
|
|
1043
1027
|
connectionToUse.ws.send(payload);
|
|
1044
1028
|
if (promiseBased) {
|
|
1045
1029
|
return new Promise((resolve, reject) => {
|
|
1046
|
-
if (!id)
|
|
1047
|
-
return reject(new Error("id is required for promise-based sending."));
|
|
1030
|
+
if (!id) return reject(new Error("id is required for promise-based sending."));
|
|
1048
1031
|
connectionToUse.pendingRequests.set(id, { resolve, reject });
|
|
1049
1032
|
this.scheduleTimer(
|
|
1050
1033
|
connectionToUse.ws,
|
|
@@ -1060,8 +1043,6 @@ var _WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
|
|
|
1060
1043
|
}
|
|
1061
1044
|
}
|
|
1062
1045
|
};
|
|
1063
|
-
_WebsocketCommon.MAX_CONNECTION_DURATION = 23 * 60 * 60 * 1e3;
|
|
1064
|
-
var WebsocketCommon = _WebsocketCommon;
|
|
1065
1046
|
var WebsocketAPIBase = class extends WebsocketCommon {
|
|
1066
1047
|
constructor(configuration, connectionPool = []) {
|
|
1067
1048
|
super(configuration, connectionPool);
|
|
@@ -1101,8 +1082,7 @@ var WebsocketAPIBase = class extends WebsocketCommon {
|
|
|
1101
1082
|
request?.reject(message.error);
|
|
1102
1083
|
} else {
|
|
1103
1084
|
const response = { data: message.result };
|
|
1104
|
-
if (message?.rateLimits)
|
|
1105
|
-
response.rateLimits = message.rateLimits;
|
|
1085
|
+
if (message?.rateLimits) response.rateLimits = message.rateLimits;
|
|
1106
1086
|
request?.resolve(response);
|
|
1107
1087
|
}
|
|
1108
1088
|
} else {
|
|
@@ -1124,8 +1104,7 @@ var WebsocketAPIBase = class extends WebsocketCommon {
|
|
|
1124
1104
|
return Promise.resolve();
|
|
1125
1105
|
}
|
|
1126
1106
|
return new Promise((resolve, reject) => {
|
|
1127
|
-
if (this.isConnecting)
|
|
1128
|
-
return;
|
|
1107
|
+
if (this.isConnecting) return;
|
|
1129
1108
|
this.isConnecting = true;
|
|
1130
1109
|
const timeout = setTimeout(() => {
|
|
1131
1110
|
this.isConnecting = false;
|
|
@@ -1221,15 +1200,13 @@ var WebsocketStreamsBase2 = class extends WebsocketCommon {
|
|
|
1221
1200
|
handleStreamAssignment(streams) {
|
|
1222
1201
|
const connectionStreamMap = /* @__PURE__ */ new Map();
|
|
1223
1202
|
streams.forEach((stream) => {
|
|
1224
|
-
if (!this.streamCallbackMap.has(stream))
|
|
1225
|
-
this.streamCallbackMap.set(stream, /* @__PURE__ */ new Set());
|
|
1203
|
+
if (!this.streamCallbackMap.has(stream)) this.streamCallbackMap.set(stream, /* @__PURE__ */ new Set());
|
|
1226
1204
|
let connection = this.streamConnectionMap.get(stream);
|
|
1227
1205
|
if (!connection || connection.closeInitiated || connection.reconnectionPending) {
|
|
1228
1206
|
connection = this.getConnection(true);
|
|
1229
1207
|
this.streamConnectionMap.set(stream, connection);
|
|
1230
1208
|
}
|
|
1231
|
-
if (!connectionStreamMap.has(connection))
|
|
1232
|
-
connectionStreamMap.set(connection, []);
|
|
1209
|
+
if (!connectionStreamMap.has(connection)) connectionStreamMap.set(connection, []);
|
|
1233
1210
|
connectionStreamMap.get(connection)?.push(stream);
|
|
1234
1211
|
});
|
|
1235
1212
|
return connectionStreamMap;
|