@binance/common 1.2.2 → 1.2.3

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.mjs CHANGED
@@ -777,10 +777,24 @@ var WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
777
777
  * @returns Timer handle
778
778
  */
779
779
  scheduleTimer(connection, callback, delay2, type = "timeout") {
780
- const timer = type === "timeout" ? setTimeout(callback, delay2) : setInterval(callback, delay2);
781
- if (!this.connectionTimers.has(connection))
782
- this.connectionTimers.set(connection, /* @__PURE__ */ new Set());
783
- this.connectionTimers.get(connection)?.add({ timer, type });
780
+ let timers = this.connectionTimers.get(connection);
781
+ if (!timers) {
782
+ timers = /* @__PURE__ */ new Set();
783
+ this.connectionTimers.set(connection, timers);
784
+ }
785
+ const timerRecord = { type };
786
+ const wrappedTimeout = () => {
787
+ try {
788
+ callback();
789
+ } finally {
790
+ timers.delete(timerRecord);
791
+ }
792
+ };
793
+ let timer;
794
+ if (type === "timeout") timer = setTimeout(wrappedTimeout, delay2);
795
+ else timer = setInterval(callback, delay2);
796
+ timerRecord.timer = timer;
797
+ timers.add(timerRecord);
784
798
  return timer;
785
799
  }
786
800
  /**
@@ -1042,13 +1056,13 @@ var WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
1042
1056
  this.onMessage(data.toString(), targetConnection);
1043
1057
  });
1044
1058
  ws.on("ping", () => {
1045
- this.logger.info("Received PING from server");
1059
+ this.logger.debug("Received PING from server");
1046
1060
  this.emit("ping");
1047
1061
  ws.pong();
1048
- this.logger.info("Responded PONG to server's PING message");
1062
+ this.logger.debug("Responded PONG to server's PING message");
1049
1063
  });
1050
1064
  ws.on("pong", () => {
1051
- this.logger.info("Received PONG from server");
1065
+ this.logger.debug("Received PONG from server");
1052
1066
  this.emit("pong");
1053
1067
  });
1054
1068
  ws.on("error", (err) => {
@@ -1128,7 +1142,7 @@ var WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
1128
1142
  this.logger.warn("Ping only can be sent when connection is ready.");
1129
1143
  return;
1130
1144
  }
1131
- this.logger.info("Sending PING to all connected Websocket servers.");
1145
+ this.logger.debug("Sending PING to all connected Websocket servers.");
1132
1146
  connectedConnections.forEach((connection) => {
1133
1147
  if (connection.ws) {
1134
1148
  connection.ws.ping();
@@ -1150,7 +1164,7 @@ var WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
1150
1164
  */
1151
1165
  send(payload, id, promiseBased = true, timeout = 5e3, connection) {
1152
1166
  if (!this.isConnected(connection)) {
1153
- const errorMsg = "Send can only be sent when connection is ready.";
1167
+ const errorMsg = "Unable to send message \u2014 connection is not available.";
1154
1168
  this.logger.warn(errorMsg);
1155
1169
  if (promiseBased) return Promise.reject(new Error(errorMsg));
1156
1170
  else throw new Error(errorMsg);
@@ -1166,17 +1180,22 @@ var WebsocketCommon = class _WebsocketCommon extends WebsocketEventEmitter {
1166
1180
  if (promiseBased) {
1167
1181
  return new Promise((resolve, reject) => {
1168
1182
  if (!id) return reject(new Error("id is required for promise-based sending."));
1169
- connectionToUse.pendingRequests.set(id, { resolve, reject });
1170
- this.scheduleTimer(
1171
- connectionToUse.ws,
1172
- () => {
1173
- if (connectionToUse.pendingRequests.has(id)) {
1174
- connectionToUse.pendingRequests.delete(id);
1175
- reject(new Error(`Request timeout for id: ${id}`));
1176
- }
1183
+ const timeoutHandle = setTimeout(() => {
1184
+ if (connectionToUse.pendingRequests.has(id)) {
1185
+ connectionToUse.pendingRequests.delete(id);
1186
+ reject(new Error(`Request timeout for id: ${id}`));
1187
+ }
1188
+ }, timeout);
1189
+ connectionToUse.pendingRequests.set(id, {
1190
+ resolve: (v) => {
1191
+ clearTimeout(timeoutHandle);
1192
+ resolve(v);
1177
1193
  },
1178
- timeout
1179
- );
1194
+ reject: (e) => {
1195
+ clearTimeout(timeoutHandle);
1196
+ reject(e);
1197
+ }
1198
+ });
1180
1199
  });
1181
1200
  }
1182
1201
  }
@@ -1256,13 +1275,13 @@ var WebsocketAPIBase = class extends WebsocketCommon {
1256
1275
  reject(new Error("Websocket connection timed out"));
1257
1276
  }, 1e4);
1258
1277
  this.connectPool(this.prepareURL(this.configuration.wsURL)).then(() => {
1259
- clearTimeout(timeout);
1260
1278
  this.isConnecting = false;
1261
1279
  resolve();
1262
1280
  }).catch((error) => {
1263
- clearTimeout(timeout);
1264
1281
  this.isConnecting = false;
1265
1282
  reject(error);
1283
+ }).finally(() => {
1284
+ clearTimeout(timeout);
1266
1285
  });
1267
1286
  });
1268
1287
  }
@@ -1433,13 +1452,7 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
1433
1452
  const timeout = setTimeout(() => {
1434
1453
  reject(new Error("Websocket connection timed out"));
1435
1454
  }, 1e4);
1436
- this.connectPool(this.prepareURL(streams)).then(() => {
1437
- clearTimeout(timeout);
1438
- resolve();
1439
- }).catch((error) => {
1440
- clearTimeout(timeout);
1441
- reject(error);
1442
- });
1455
+ this.connectPool(this.prepareURL(streams)).then(() => resolve()).catch((error) => reject(error)).finally(() => clearTimeout(timeout));
1443
1456
  });
1444
1457
  }
1445
1458
  /**