@binance/common 1.1.2 → 1.2.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/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  [![npm Downloads](https://img.shields.io/npm/dm/@binance/common.svg)](https://www.npmjs.com/package/@binance/common)
6
6
  ![Node.js Version](https://img.shields.io/badge/Node.js-%3E=22.12.0-brightgreen)
7
7
  [![Known Vulnerabilities](https://snyk.io/test/github/binance/binance-connector-js/badge.svg)](https://snyk.io/test/github/binance/binance-connector-js)
8
+ [![Docs](https://img.shields.io/badge/docs-online-blue?style=flat-square)](https://binance.github.io/binance-connector-js/modules/_binance_common.html)
8
9
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9
10
 
10
11
  `@binance/common` is a **utility package** for Binance modular connectors, providing commonly used functions and helpers for working with Binance REST/WebSocket APIs. It ensures reusable and optimized utilities to streamline development.
package/dist/index.d.mts CHANGED
@@ -31,11 +31,16 @@ declare const DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL = "https://eapi.bina
31
31
  declare const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL = "wss://nbstream.binance.com/eoptions";
32
32
  declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL = "https://papi.binance.com";
33
33
  declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
34
+ declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm";
35
+ declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL = "wss://fstream.binancefuture.com/pm";
34
36
  declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL = "https://api.binance.com";
37
+ declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm-classic";
35
38
  declare const DUAL_INVESTMENT_REST_API_PROD_URL = "https://api.binance.com";
36
39
  declare const FIAT_REST_API_PROD_URL = "https://api.binance.com";
37
40
  declare const GIFT_CARD_REST_API_PROD_URL = "https://api.binance.com";
38
41
  declare const MARGIN_TRADING_REST_API_PROD_URL = "https://api.binance.com";
42
+ declare const MARGIN_TRADING_WS_STREAMS_PROD_URL = "wss://stream.binance.com:9443";
43
+ declare const MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL = "wss://margin-stream.binance.com";
39
44
  declare const MINING_REST_API_PROD_URL = "https://api.binance.com";
40
45
  declare const NFT_REST_API_PROD_URL = "https://api.binance.com";
41
46
  declare const PAY_REST_API_PROD_URL = "https://api.binance.com";
@@ -99,6 +104,13 @@ declare class ConfigurationRestAPI {
99
104
  password: string;
100
105
  };
101
106
  };
107
+ /**
108
+ * Optional custom headers to be sent with the request
109
+ * @default {}
110
+ * @type {Record<string, string | string[]>}
111
+ * @memberof ConfigurationRestAPI
112
+ */
113
+ customHeaders?: Record<string, string | string[]>;
102
114
  /**
103
115
  * enables keep-alive functionality for the connection (if httpsAgent is set then we use httpsAgent.keepAlive instead)
104
116
  * @default true
@@ -136,10 +148,10 @@ declare class ConfigurationRestAPI {
136
148
  httpsAgent?: boolean | Agent;
137
149
  /**
138
150
  * private key
139
- * @type {Buffer}
151
+ * @type {string | Buffer}
140
152
  * @memberof ConfigurationRestAPI
141
153
  */
142
- privateKey?: Buffer;
154
+ privateKey?: string | Buffer;
143
155
  /**
144
156
  * private key passphrase
145
157
  * @type {string}
@@ -215,10 +227,10 @@ declare class ConfigurationWebsocketAPI {
215
227
  poolSize?: number;
216
228
  /**
217
229
  * private key
218
- * @type {Buffer}
230
+ * @type {string | Buffer}
219
231
  * @memberof ConfigurationWebsocketAPI
220
232
  */
221
- privateKey?: Buffer;
233
+ privateKey?: string | Buffer;
222
234
  /**
223
235
  * private key passphrase
224
236
  * @type {string}
@@ -231,6 +243,13 @@ declare class ConfigurationWebsocketAPI {
231
243
  * @memberof ConfigurationWebsocketAPI
232
244
  */
233
245
  timeUnit?: TimeUnit;
246
+ /**
247
+ * auto session re-logon on reconnects/renewals
248
+ * @default true
249
+ * @type {boolean}
250
+ * @memberof ConfigurationWebsocketAPI
251
+ */
252
+ autoSessionReLogon?: boolean;
234
253
  constructor(param?: ConfigurationWebsocketAPI);
235
254
  }
236
255
  declare class ConfigurationWebsocketStreams {
@@ -582,7 +601,6 @@ declare const shouldRetryRequest: (error: AxiosError | object, method?: string,
582
601
  * This function handles retries, rate limit handling, and error handling for the HTTP request.
583
602
  *
584
603
  * @param axiosArgs The request arguments to be passed to Axios.
585
- * @param axios The Axios instance to use for the request.
586
604
  * @param configuration The configuration options for the request.
587
605
  * @returns A Promise that resolves to the API response, including the data and rate limit headers.
588
606
  */
@@ -636,6 +654,38 @@ declare function replaceWebsocketStreamsPlaceholders(str: string, variables: Rec
636
654
  * @returns {string} A formatted user agent string including package details, Node.js version, platform, and architecture.
637
655
  */
638
656
  declare function buildUserAgent(packageName: string, packageVersion: string): string;
657
+ /**
658
+ * Builds a WebSocket API message with optional authentication and signature.
659
+ *
660
+ * @param {ConfigurationWebsocketAPI} configuration - The WebSocket API configuration.
661
+ * @param {string} method - The method name for the WebSocket message.
662
+ * @param {WebsocketSendMsgOptions} payload - The payload data to be sent.
663
+ * @param {WebsocketSendMsgConfig} options - Configuration options for message sending.
664
+ * @param {boolean} [skipAuth=false] - Flag to skip authentication if needed.
665
+ * @returns {Object} A structured WebSocket message with id, method, and params.
666
+ */
667
+ declare function buildWebsocketAPIMessage(configuration: ConfigurationWebsocketAPI, method: string, payload: WebsocketSendMsgOptions, options: WebsocketSendMsgConfig, skipAuth?: boolean): {
668
+ id: string;
669
+ method: string;
670
+ params: SendMessageOptions;
671
+ };
672
+ /**
673
+ * Sanitizes a header value by checking for and preventing carriage return and line feed characters.
674
+ *
675
+ * @param {string | string[]} value - The header value or array of header values to sanitize.
676
+ * @returns {string | string[]} The sanitized header value(s).
677
+ * @throws {Error} If the header value contains CR/LF characters.
678
+ */
679
+ declare function sanitizeHeaderValue(value: string | string[]): string | string[];
680
+ /**
681
+ * Parses and sanitizes custom headers, filtering out forbidden headers.
682
+ *
683
+ * @param {Record<string, string | string[]>} headers - The input headers to be parsed.
684
+ * @returns {Record<string, string | string[]>} A new object with sanitized and allowed headers.
685
+ * @description Removes forbidden headers like 'host', 'authorization', and 'cookie',
686
+ * and sanitizes remaining header values to prevent injection of carriage return or line feed characters.
687
+ */
688
+ declare function parseCustomHeaders(headers: Record<string, string | string[]>): Record<string, string | string[]>;
639
689
 
640
690
  declare class WebsocketEventEmitter {
641
691
  private eventEmitter;
@@ -655,6 +705,12 @@ interface WebsocketConnection {
655
705
  }>;
656
706
  pendingSubscriptions?: string[];
657
707
  ws?: WebSocketClient;
708
+ isSessionLoggedOn?: boolean;
709
+ sessionLogonReq?: {
710
+ method: string;
711
+ payload: WebsocketSendMsgOptions;
712
+ options: WebsocketSendMsgConfig;
713
+ };
658
714
  }
659
715
  declare class WebsocketCommon extends WebsocketEventEmitter {
660
716
  protected configuration: ConfigurationWebsocketAPI | ConfigurationWebsocketStreams;
@@ -678,6 +734,14 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
678
734
  * @returns void
679
735
  */
680
736
  private initializePool;
737
+ /**
738
+ * Retrieves available WebSocket connections based on the connection mode and readiness.
739
+ * In 'single' mode, returns the first connection in the pool.
740
+ * In 'pool' mode, filters and returns connections that are ready for use.
741
+ * @param allowNonEstablishedWebsockets - Optional flag to include non-established WebSocket connections.
742
+ * @returns An array of available WebSocket connections.
743
+ */
744
+ protected getAvailableConnections(allowNonEstablishedWebsockets?: boolean): WebsocketConnection[];
681
745
  /**
682
746
  * Gets a WebSocket connection from the pool or single connection.
683
747
  * If the connection mode is 'single', it returns the first connection in the pool.
@@ -742,6 +806,14 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
742
806
  * @returns Promise that resolves when the connection is closed.
743
807
  */
744
808
  private closeConnectionGracefully;
809
+ /**
810
+ * Attempts to re-establish a session for a WebSocket connection.
811
+ * If a session logon request exists and the connection is not already logged on,
812
+ * it sends an authentication request and updates the connection's logged-on status.
813
+ * @param connection - The WebSocket connection to re-authenticate.
814
+ * @private
815
+ */
816
+ private sessionReLogon;
745
817
  /**
746
818
  * Cleans up WebSocket connection resources.
747
819
  * Removes all listeners and clears any associated timers for the provided WebSocket client.
@@ -759,8 +831,6 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
759
831
  * Handles the opening of a WebSocket connection.
760
832
  * @param url - The URL of the WebSocket server.
761
833
  * @param targetConnection - The WebSocket connection being opened.
762
- * @param oldConnection - The previous WebSocket connection, if this is a renewal.
763
- * @param isRenewal - Indicates whether this is a connection renewal.
764
834
  * @param oldWSConnection - The WebSocket client instance associated with the old connection.
765
835
  */
766
836
  protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
@@ -830,6 +900,12 @@ interface WebsocketSendMsgOptions {
830
900
  id?: string;
831
901
  [key: string]: string | number | boolean | object | undefined;
832
902
  }
903
+ interface WebsocketSendMsgConfig {
904
+ withApiKey?: boolean;
905
+ isSigned?: boolean;
906
+ isSessionLogon?: boolean;
907
+ isSessionLogout?: boolean;
908
+ }
833
909
  declare class WebsocketAPIBase extends WebsocketCommon {
834
910
  private isConnecting;
835
911
  streamCallbackMap: Map<string, Set<(data: unknown) => void>>;
@@ -853,19 +929,13 @@ declare class WebsocketAPIBase extends WebsocketCommon {
853
929
  * @throws Error if connection times out
854
930
  */
855
931
  connect(): Promise<void>;
856
- /**
857
- * Sends a message to the WebSocket API Server.
858
- * Supports both signed and unsigned messages.
859
- * @param method The API method to call
860
- * @param payload Message parameters and options
861
- * @param options Additional requests options (withApiKey, isSigned)
862
- * @returns Promise that resolves with the server response
863
- * @throws Error if not connected
864
- */
865
- sendMessage<T = unknown>(method: string, payload?: WebsocketSendMsgOptions, options?: {
866
- withApiKey?: boolean;
867
- isSigned?: boolean;
868
- }): Promise<WebsocketApiResponse<T>>;
932
+ sendMessage<T>(method: string, payload: WebsocketSendMsgOptions, options: WebsocketSendMsgConfig & {
933
+ isSessionLogon: true;
934
+ }): Promise<WebsocketApiResponse<T>[]>;
935
+ sendMessage<T>(method: string, payload: WebsocketSendMsgOptions, options: WebsocketSendMsgConfig & {
936
+ isSessionLogout: true;
937
+ }): Promise<WebsocketApiResponse<T>[]>;
938
+ sendMessage<T>(method: string, payload?: WebsocketSendMsgOptions, options?: WebsocketSendMsgConfig): Promise<WebsocketApiResponse<T>>;
869
939
  }
870
940
  declare class WebsocketStreamsBase extends WebsocketCommon {
871
941
  private streamConnectionMap;
@@ -921,7 +991,6 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
921
991
  * @param url The URL of the WebSocket connection.
922
992
  * @param targetConnection The WebSocket connection that was opened.
923
993
  * @param oldConnection The previous WebSocket connection, if any.
924
- * @param isRenewal Whether the connection is a renewal of an existing connection.
925
994
  */
926
995
  protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
927
996
  /**
@@ -983,4 +1052,4 @@ interface WebsocketStream<T> {
983
1052
  */
984
1053
  declare function createStreamHandler<T>(websocketBase: WebsocketAPIBase | WebsocketStreamsBase, streamOrId: string, id?: string): WebsocketStream<T>;
985
1054
 
986
- export { ALGO_REST_API_PROD_URL, AUTO_INVEST_REST_API_PROD_URL, type AxiosRequestArgs, 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_REST_API_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_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, MINING_REST_API_PROD_URL, NFT_REST_API_PROD_URL, NetworkError, NotFoundError, type ObjectType, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, type RequestArgs, RequiredError, type RestApiRateLimit, type RestApiResponse, 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, type SendMessageOptions, ServerError, TimeUnit, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, type WebsocketApiRateLimit, type WebsocketApiResponse, WebsocketCommon, type WebsocketConnection, WebsocketEventEmitter, type WebsocketSendMsgOptions, type WebsocketStream, WebsocketStreamsBase, assertParamExists, buildQueryString, buildUserAgent, clearSignerCache, createStreamHandler, delay, getSignature, getTimestamp, httpRequestFunction, parseRateLimitHeaders, randomString, removeEmptyValue, replaceWebsocketStreamsPlaceholders, sendRequest, setFlattenedQueryParams, setSearchParams, shouldRetryRequest, sortObject, toPathString, validateTimeUnit };
1055
+ export { ALGO_REST_API_PROD_URL, AUTO_INVEST_REST_API_PROD_URL, type AxiosRequestArgs, 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, type ObjectType, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, type RequestArgs, RequiredError, type RestApiRateLimit, type RestApiResponse, 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, type SendMessageOptions, ServerError, TimeUnit, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, type WebsocketApiRateLimit, type WebsocketApiResponse, WebsocketCommon, type WebsocketConnection, WebsocketEventEmitter, type WebsocketSendMsgConfig, type WebsocketSendMsgOptions, type WebsocketStream, WebsocketStreamsBase, assertParamExists, buildQueryString, buildUserAgent, buildWebsocketAPIMessage, clearSignerCache, createStreamHandler, delay, getSignature, getTimestamp, httpRequestFunction, parseCustomHeaders, parseRateLimitHeaders, randomString, removeEmptyValue, replaceWebsocketStreamsPlaceholders, sanitizeHeaderValue, sendRequest, setFlattenedQueryParams, setSearchParams, shouldRetryRequest, sortObject, toPathString, validateTimeUnit };
package/dist/index.d.ts CHANGED
@@ -31,11 +31,16 @@ declare const DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL = "https://eapi.bina
31
31
  declare const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL = "wss://nbstream.binance.com/eoptions";
32
32
  declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL = "https://papi.binance.com";
33
33
  declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
34
+ declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm";
35
+ declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_TESTNET_URL = "wss://fstream.binancefuture.com/pm";
34
36
  declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL = "https://api.binance.com";
37
+ declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm-classic";
35
38
  declare const DUAL_INVESTMENT_REST_API_PROD_URL = "https://api.binance.com";
36
39
  declare const FIAT_REST_API_PROD_URL = "https://api.binance.com";
37
40
  declare const GIFT_CARD_REST_API_PROD_URL = "https://api.binance.com";
38
41
  declare const MARGIN_TRADING_REST_API_PROD_URL = "https://api.binance.com";
42
+ declare const MARGIN_TRADING_WS_STREAMS_PROD_URL = "wss://stream.binance.com:9443";
43
+ declare const MARGIN_TRADING_RISK_WS_STREAMS_PROD_URL = "wss://margin-stream.binance.com";
39
44
  declare const MINING_REST_API_PROD_URL = "https://api.binance.com";
40
45
  declare const NFT_REST_API_PROD_URL = "https://api.binance.com";
41
46
  declare const PAY_REST_API_PROD_URL = "https://api.binance.com";
@@ -99,6 +104,13 @@ declare class ConfigurationRestAPI {
99
104
  password: string;
100
105
  };
101
106
  };
107
+ /**
108
+ * Optional custom headers to be sent with the request
109
+ * @default {}
110
+ * @type {Record<string, string | string[]>}
111
+ * @memberof ConfigurationRestAPI
112
+ */
113
+ customHeaders?: Record<string, string | string[]>;
102
114
  /**
103
115
  * enables keep-alive functionality for the connection (if httpsAgent is set then we use httpsAgent.keepAlive instead)
104
116
  * @default true
@@ -136,10 +148,10 @@ declare class ConfigurationRestAPI {
136
148
  httpsAgent?: boolean | Agent;
137
149
  /**
138
150
  * private key
139
- * @type {Buffer}
151
+ * @type {string | Buffer}
140
152
  * @memberof ConfigurationRestAPI
141
153
  */
142
- privateKey?: Buffer;
154
+ privateKey?: string | Buffer;
143
155
  /**
144
156
  * private key passphrase
145
157
  * @type {string}
@@ -215,10 +227,10 @@ declare class ConfigurationWebsocketAPI {
215
227
  poolSize?: number;
216
228
  /**
217
229
  * private key
218
- * @type {Buffer}
230
+ * @type {string | Buffer}
219
231
  * @memberof ConfigurationWebsocketAPI
220
232
  */
221
- privateKey?: Buffer;
233
+ privateKey?: string | Buffer;
222
234
  /**
223
235
  * private key passphrase
224
236
  * @type {string}
@@ -231,6 +243,13 @@ declare class ConfigurationWebsocketAPI {
231
243
  * @memberof ConfigurationWebsocketAPI
232
244
  */
233
245
  timeUnit?: TimeUnit;
246
+ /**
247
+ * auto session re-logon on reconnects/renewals
248
+ * @default true
249
+ * @type {boolean}
250
+ * @memberof ConfigurationWebsocketAPI
251
+ */
252
+ autoSessionReLogon?: boolean;
234
253
  constructor(param?: ConfigurationWebsocketAPI);
235
254
  }
236
255
  declare class ConfigurationWebsocketStreams {
@@ -582,7 +601,6 @@ declare const shouldRetryRequest: (error: AxiosError | object, method?: string,
582
601
  * This function handles retries, rate limit handling, and error handling for the HTTP request.
583
602
  *
584
603
  * @param axiosArgs The request arguments to be passed to Axios.
585
- * @param axios The Axios instance to use for the request.
586
604
  * @param configuration The configuration options for the request.
587
605
  * @returns A Promise that resolves to the API response, including the data and rate limit headers.
588
606
  */
@@ -636,6 +654,38 @@ declare function replaceWebsocketStreamsPlaceholders(str: string, variables: Rec
636
654
  * @returns {string} A formatted user agent string including package details, Node.js version, platform, and architecture.
637
655
  */
638
656
  declare function buildUserAgent(packageName: string, packageVersion: string): string;
657
+ /**
658
+ * Builds a WebSocket API message with optional authentication and signature.
659
+ *
660
+ * @param {ConfigurationWebsocketAPI} configuration - The WebSocket API configuration.
661
+ * @param {string} method - The method name for the WebSocket message.
662
+ * @param {WebsocketSendMsgOptions} payload - The payload data to be sent.
663
+ * @param {WebsocketSendMsgConfig} options - Configuration options for message sending.
664
+ * @param {boolean} [skipAuth=false] - Flag to skip authentication if needed.
665
+ * @returns {Object} A structured WebSocket message with id, method, and params.
666
+ */
667
+ declare function buildWebsocketAPIMessage(configuration: ConfigurationWebsocketAPI, method: string, payload: WebsocketSendMsgOptions, options: WebsocketSendMsgConfig, skipAuth?: boolean): {
668
+ id: string;
669
+ method: string;
670
+ params: SendMessageOptions;
671
+ };
672
+ /**
673
+ * Sanitizes a header value by checking for and preventing carriage return and line feed characters.
674
+ *
675
+ * @param {string | string[]} value - The header value or array of header values to sanitize.
676
+ * @returns {string | string[]} The sanitized header value(s).
677
+ * @throws {Error} If the header value contains CR/LF characters.
678
+ */
679
+ declare function sanitizeHeaderValue(value: string | string[]): string | string[];
680
+ /**
681
+ * Parses and sanitizes custom headers, filtering out forbidden headers.
682
+ *
683
+ * @param {Record<string, string | string[]>} headers - The input headers to be parsed.
684
+ * @returns {Record<string, string | string[]>} A new object with sanitized and allowed headers.
685
+ * @description Removes forbidden headers like 'host', 'authorization', and 'cookie',
686
+ * and sanitizes remaining header values to prevent injection of carriage return or line feed characters.
687
+ */
688
+ declare function parseCustomHeaders(headers: Record<string, string | string[]>): Record<string, string | string[]>;
639
689
 
640
690
  declare class WebsocketEventEmitter {
641
691
  private eventEmitter;
@@ -655,6 +705,12 @@ interface WebsocketConnection {
655
705
  }>;
656
706
  pendingSubscriptions?: string[];
657
707
  ws?: WebSocketClient;
708
+ isSessionLoggedOn?: boolean;
709
+ sessionLogonReq?: {
710
+ method: string;
711
+ payload: WebsocketSendMsgOptions;
712
+ options: WebsocketSendMsgConfig;
713
+ };
658
714
  }
659
715
  declare class WebsocketCommon extends WebsocketEventEmitter {
660
716
  protected configuration: ConfigurationWebsocketAPI | ConfigurationWebsocketStreams;
@@ -678,6 +734,14 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
678
734
  * @returns void
679
735
  */
680
736
  private initializePool;
737
+ /**
738
+ * Retrieves available WebSocket connections based on the connection mode and readiness.
739
+ * In 'single' mode, returns the first connection in the pool.
740
+ * In 'pool' mode, filters and returns connections that are ready for use.
741
+ * @param allowNonEstablishedWebsockets - Optional flag to include non-established WebSocket connections.
742
+ * @returns An array of available WebSocket connections.
743
+ */
744
+ protected getAvailableConnections(allowNonEstablishedWebsockets?: boolean): WebsocketConnection[];
681
745
  /**
682
746
  * Gets a WebSocket connection from the pool or single connection.
683
747
  * If the connection mode is 'single', it returns the first connection in the pool.
@@ -742,6 +806,14 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
742
806
  * @returns Promise that resolves when the connection is closed.
743
807
  */
744
808
  private closeConnectionGracefully;
809
+ /**
810
+ * Attempts to re-establish a session for a WebSocket connection.
811
+ * If a session logon request exists and the connection is not already logged on,
812
+ * it sends an authentication request and updates the connection's logged-on status.
813
+ * @param connection - The WebSocket connection to re-authenticate.
814
+ * @private
815
+ */
816
+ private sessionReLogon;
745
817
  /**
746
818
  * Cleans up WebSocket connection resources.
747
819
  * Removes all listeners and clears any associated timers for the provided WebSocket client.
@@ -759,8 +831,6 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
759
831
  * Handles the opening of a WebSocket connection.
760
832
  * @param url - The URL of the WebSocket server.
761
833
  * @param targetConnection - The WebSocket connection being opened.
762
- * @param oldConnection - The previous WebSocket connection, if this is a renewal.
763
- * @param isRenewal - Indicates whether this is a connection renewal.
764
834
  * @param oldWSConnection - The WebSocket client instance associated with the old connection.
765
835
  */
766
836
  protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
@@ -830,6 +900,12 @@ interface WebsocketSendMsgOptions {
830
900
  id?: string;
831
901
  [key: string]: string | number | boolean | object | undefined;
832
902
  }
903
+ interface WebsocketSendMsgConfig {
904
+ withApiKey?: boolean;
905
+ isSigned?: boolean;
906
+ isSessionLogon?: boolean;
907
+ isSessionLogout?: boolean;
908
+ }
833
909
  declare class WebsocketAPIBase extends WebsocketCommon {
834
910
  private isConnecting;
835
911
  streamCallbackMap: Map<string, Set<(data: unknown) => void>>;
@@ -853,19 +929,13 @@ declare class WebsocketAPIBase extends WebsocketCommon {
853
929
  * @throws Error if connection times out
854
930
  */
855
931
  connect(): Promise<void>;
856
- /**
857
- * Sends a message to the WebSocket API Server.
858
- * Supports both signed and unsigned messages.
859
- * @param method The API method to call
860
- * @param payload Message parameters and options
861
- * @param options Additional requests options (withApiKey, isSigned)
862
- * @returns Promise that resolves with the server response
863
- * @throws Error if not connected
864
- */
865
- sendMessage<T = unknown>(method: string, payload?: WebsocketSendMsgOptions, options?: {
866
- withApiKey?: boolean;
867
- isSigned?: boolean;
868
- }): Promise<WebsocketApiResponse<T>>;
932
+ sendMessage<T>(method: string, payload: WebsocketSendMsgOptions, options: WebsocketSendMsgConfig & {
933
+ isSessionLogon: true;
934
+ }): Promise<WebsocketApiResponse<T>[]>;
935
+ sendMessage<T>(method: string, payload: WebsocketSendMsgOptions, options: WebsocketSendMsgConfig & {
936
+ isSessionLogout: true;
937
+ }): Promise<WebsocketApiResponse<T>[]>;
938
+ sendMessage<T>(method: string, payload?: WebsocketSendMsgOptions, options?: WebsocketSendMsgConfig): Promise<WebsocketApiResponse<T>>;
869
939
  }
870
940
  declare class WebsocketStreamsBase extends WebsocketCommon {
871
941
  private streamConnectionMap;
@@ -921,7 +991,6 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
921
991
  * @param url The URL of the WebSocket connection.
922
992
  * @param targetConnection The WebSocket connection that was opened.
923
993
  * @param oldConnection The previous WebSocket connection, if any.
924
- * @param isRenewal Whether the connection is a renewal of an existing connection.
925
994
  */
926
995
  protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
927
996
  /**
@@ -983,4 +1052,4 @@ interface WebsocketStream<T> {
983
1052
  */
984
1053
  declare function createStreamHandler<T>(websocketBase: WebsocketAPIBase | WebsocketStreamsBase, streamOrId: string, id?: string): WebsocketStream<T>;
985
1054
 
986
- export { ALGO_REST_API_PROD_URL, AUTO_INVEST_REST_API_PROD_URL, type AxiosRequestArgs, 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_REST_API_PROD_URL, DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_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, MINING_REST_API_PROD_URL, NFT_REST_API_PROD_URL, NetworkError, NotFoundError, type ObjectType, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, type RequestArgs, RequiredError, type RestApiRateLimit, type RestApiResponse, 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, type SendMessageOptions, ServerError, TimeUnit, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, type WebsocketApiRateLimit, type WebsocketApiResponse, WebsocketCommon, type WebsocketConnection, WebsocketEventEmitter, type WebsocketSendMsgOptions, type WebsocketStream, WebsocketStreamsBase, assertParamExists, buildQueryString, buildUserAgent, clearSignerCache, createStreamHandler, delay, getSignature, getTimestamp, httpRequestFunction, parseRateLimitHeaders, randomString, removeEmptyValue, replaceWebsocketStreamsPlaceholders, sendRequest, setFlattenedQueryParams, setSearchParams, shouldRetryRequest, sortObject, toPathString, validateTimeUnit };
1055
+ export { ALGO_REST_API_PROD_URL, AUTO_INVEST_REST_API_PROD_URL, type AxiosRequestArgs, 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, type ObjectType, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, type RequestArgs, RequiredError, type RestApiRateLimit, type RestApiResponse, 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, type SendMessageOptions, ServerError, TimeUnit, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, type WebsocketApiRateLimit, type WebsocketApiResponse, WebsocketCommon, type WebsocketConnection, WebsocketEventEmitter, type WebsocketSendMsgConfig, type WebsocketSendMsgOptions, type WebsocketStream, WebsocketStreamsBase, assertParamExists, buildQueryString, buildUserAgent, buildWebsocketAPIMessage, clearSignerCache, createStreamHandler, delay, getSignature, getTimestamp, httpRequestFunction, parseCustomHeaders, parseRateLimitHeaders, randomString, removeEmptyValue, replaceWebsocketStreamsPlaceholders, sanitizeHeaderValue, sendRequest, setFlattenedQueryParams, setSearchParams, shouldRetryRequest, sortObject, toPathString, validateTimeUnit };