@binance/common 2.1.0 → 2.1.1

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
@@ -112,6 +112,42 @@ function randomString() {
112
112
  return crypto.randomBytes(16).toString("hex");
113
113
  }
114
114
  /**
115
+ * Generates a cryptographically secure random 32-bit unsigned integer.
116
+ *
117
+ * Uses the Web Crypto API to generate a random value between 0 and 4,294,967,295 (2^32 - 1).
118
+ *
119
+ * @returns A random 32-bit unsigned integer.
120
+ */
121
+ function randomInteger() {
122
+ const array = new Uint32Array(1);
123
+ crypto.getRandomValues(array);
124
+ return array[0];
125
+ }
126
+ /**
127
+ * Normalizes a stream ID to ensure it is valid, generating a random ID if needed.
128
+ *
129
+ * For string inputs:
130
+ * - Returns the input if it's a valid 32-character hexadecimal string (case-insensitive)
131
+ * - Otherwise, generates a new random hexadecimal string using `randomString()`
132
+ *
133
+ * For number inputs:
134
+ * - Returns the input if it's a finite, non-negative integer within the safe integer range
135
+ * - Otherwise, generates a new random integer using `randomInteger()`
136
+ *
137
+ * For null or undefined inputs:
138
+ * - Generates a new random hexadecimal string using `randomString()`
139
+ *
140
+ * @param id - The stream ID to normalize (string, number, null, or undefined).
141
+ * @param streamIdIsStrictlyNumber - Boolean forcing an id to be a number or not.
142
+ * @returns A valid stream ID as either a 32-character hexadecimal string or a safe integer.
143
+ */
144
+ function normalizeStreamId(id, streamIdIsStrictlyNumber) {
145
+ const isValidNumber = typeof id === "number" && Number.isFinite(id) && Number.isInteger(id) && id >= 0 && id <= Number.MAX_SAFE_INTEGER;
146
+ if (streamIdIsStrictlyNumber || typeof id === "number") return isValidNumber ? id : randomInteger();
147
+ if (typeof id === "string") return id && /^[0-9a-f]{32}$/i.test(id) ? id : randomString();
148
+ return randomString();
149
+ }
150
+ /**
115
151
  * Validates the provided time unit string and returns it if it is either 'MILLISECOND' or 'MICROSECOND'.
116
152
  *
117
153
  * @param timeUnit - The time unit string to be validated.
@@ -1418,6 +1454,7 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
1418
1454
  constructor(configuration, connectionPool = []) {
1419
1455
  super(configuration, connectionPool);
1420
1456
  this.streamConnectionMap = /* @__PURE__ */ new Map();
1457
+ this.streamIdIsStrictlyNumber = false;
1421
1458
  this.streamCallbackMap = /* @__PURE__ */ new Map();
1422
1459
  this.logger = Logger.getInstance();
1423
1460
  this.configuration = configuration;
@@ -1477,7 +1514,7 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
1477
1514
  const payload = {
1478
1515
  method: "SUBSCRIBE",
1479
1516
  params: streams,
1480
- id: id && /^[0-9a-f]{32}$/.test(id) ? id : randomString()
1517
+ id: normalizeStreamId(id, this.streamIdIsStrictlyNumber)
1481
1518
  };
1482
1519
  this.logger.debug("SUBSCRIBE", payload);
1483
1520
  this.send(JSON.stringify(payload), void 0, false, 0, connection);
@@ -1584,7 +1621,7 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
1584
1621
  const payload = {
1585
1622
  method: "UNSUBSCRIBE",
1586
1623
  params: [stream$1],
1587
- id: id && /^[0-9a-f]{32}$/.test(id) ? id : randomString()
1624
+ id: normalizeStreamId(id, this.streamIdIsStrictlyNumber)
1588
1625
  };
1589
1626
  this.logger.debug("UNSUBSCRIBE", payload);
1590
1627
  this.send(JSON.stringify(payload), void 0, false, 0, connection);
@@ -1635,5 +1672,5 @@ function createStreamHandler(websocketBase, streamOrId, id) {
1635
1672
  }
1636
1673
 
1637
1674
  //#endregion
1638
- export { ALGO_REST_API_PROD_URL, BadRequestError, C2C_REST_API_PROD_URL, CONVERT_REST_API_PROD_URL, COPY_TRADING_REST_API_PROD_URL, CRYPTO_LOAN_REST_API_PROD_URL, ConfigurationRestAPI, ConfigurationWebsocketAPI, ConfigurationWebsocketStreams, ConnectorClientError, DERIVATIVES_TRADING_COIN_FUTURES_REST_API_PROD_URL, DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL, DERIVATIVES_TRADING_COIN_FUTURES_WS_API_PROD_URL, DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL, DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL, DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL, DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL, DERIVATIVES_TRADING_USDS_FUTURES_REST_API_PROD_URL, DERIVATIVES_TRADING_USDS_FUTURES_REST_API_TESTNET_URL, DERIVATIVES_TRADING_USDS_FUTURES_WS_API_PROD_URL, DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL, DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL, DUAL_INVESTMENT_REST_API_PROD_URL, FIAT_REST_API_PROD_URL, ForbiddenError, GIFT_CARD_REST_API_PROD_URL, LogLevel, Logger, MARGIN_TRADING_REST_API_PROD_URL, MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL, MARGIN_TRADING_WS_STREAMS_PROD_URL, MINING_REST_API_PROD_URL, NFT_REST_API_PROD_URL, NetworkError, NotFoundError, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, RequiredError, SIMPLE_EARN_REST_API_PROD_URL, SPOT_REST_API_MARKET_URL, SPOT_REST_API_PROD_URL, SPOT_REST_API_TESTNET_URL, SPOT_WS_API_PROD_URL, SPOT_WS_API_TESTNET_URL, SPOT_WS_STREAMS_MARKET_URL, SPOT_WS_STREAMS_PROD_URL, SPOT_WS_STREAMS_TESTNET_URL, STAKING_REST_API_PROD_URL, SUB_ACCOUNT_REST_API_PROD_URL, ServerError, TimeUnit, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, WebsocketCommon, WebsocketEventEmitter, WebsocketStreamsBase, assertParamExists, buildQueryString, buildUserAgent, buildWebsocketAPIMessage, clearSignerCache, createStreamHandler, delay, getSignature, getTimestamp, httpRequestFunction, normalizeScientificNumbers, parseCustomHeaders, parseRateLimitHeaders, randomString, removeEmptyValue, replaceWebsocketStreamsPlaceholders, sanitizeHeaderValue, sendRequest, setSearchParams, shouldRetryRequest, sortObject, toPathString, validateTimeUnit };
1675
+ export { ALGO_REST_API_PROD_URL, BadRequestError, C2C_REST_API_PROD_URL, CONVERT_REST_API_PROD_URL, COPY_TRADING_REST_API_PROD_URL, CRYPTO_LOAN_REST_API_PROD_URL, ConfigurationRestAPI, ConfigurationWebsocketAPI, ConfigurationWebsocketStreams, ConnectorClientError, DERIVATIVES_TRADING_COIN_FUTURES_REST_API_PROD_URL, DERIVATIVES_TRADING_COIN_FUTURES_REST_API_TESTNET_URL, DERIVATIVES_TRADING_COIN_FUTURES_WS_API_PROD_URL, DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL, DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL, DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL, DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL, DERIVATIVES_TRADING_USDS_FUTURES_REST_API_PROD_URL, DERIVATIVES_TRADING_USDS_FUTURES_REST_API_TESTNET_URL, DERIVATIVES_TRADING_USDS_FUTURES_WS_API_PROD_URL, DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL, DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL, DUAL_INVESTMENT_REST_API_PROD_URL, FIAT_REST_API_PROD_URL, ForbiddenError, GIFT_CARD_REST_API_PROD_URL, LogLevel, Logger, MARGIN_TRADING_REST_API_PROD_URL, MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL, MARGIN_TRADING_WS_STREAMS_PROD_URL, MINING_REST_API_PROD_URL, NFT_REST_API_PROD_URL, NetworkError, NotFoundError, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, RequiredError, SIMPLE_EARN_REST_API_PROD_URL, SPOT_REST_API_MARKET_URL, SPOT_REST_API_PROD_URL, SPOT_REST_API_TESTNET_URL, SPOT_WS_API_PROD_URL, SPOT_WS_API_TESTNET_URL, SPOT_WS_STREAMS_MARKET_URL, SPOT_WS_STREAMS_PROD_URL, SPOT_WS_STREAMS_TESTNET_URL, STAKING_REST_API_PROD_URL, SUB_ACCOUNT_REST_API_PROD_URL, ServerError, TimeUnit, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, WebsocketCommon, WebsocketEventEmitter, WebsocketStreamsBase, assertParamExists, buildQueryString, buildUserAgent, buildWebsocketAPIMessage, clearSignerCache, createStreamHandler, delay, getSignature, getTimestamp, httpRequestFunction, normalizeScientificNumbers, normalizeStreamId, parseCustomHeaders, parseRateLimitHeaders, randomInteger, randomString, removeEmptyValue, replaceWebsocketStreamsPlaceholders, sanitizeHeaderValue, sendRequest, setSearchParams, shouldRetryRequest, sortObject, toPathString, validateTimeUnit };
1639
1676
  //# sourceMappingURL=index.mjs.map