@binance/common 1.1.3 → 1.2.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/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
@@ -104,6 +104,13 @@ declare class ConfigurationRestAPI {
104
104
  password: string;
105
105
  };
106
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[]>;
107
114
  /**
108
115
  * enables keep-alive functionality for the connection (if httpsAgent is set then we use httpsAgent.keepAlive instead)
109
116
  * @default true
@@ -141,10 +148,10 @@ declare class ConfigurationRestAPI {
141
148
  httpsAgent?: boolean | Agent;
142
149
  /**
143
150
  * private key
144
- * @type {Buffer}
151
+ * @type {string | Buffer}
145
152
  * @memberof ConfigurationRestAPI
146
153
  */
147
- privateKey?: Buffer;
154
+ privateKey?: string | Buffer;
148
155
  /**
149
156
  * private key passphrase
150
157
  * @type {string}
@@ -220,10 +227,10 @@ declare class ConfigurationWebsocketAPI {
220
227
  poolSize?: number;
221
228
  /**
222
229
  * private key
223
- * @type {Buffer}
230
+ * @type {string | Buffer}
224
231
  * @memberof ConfigurationWebsocketAPI
225
232
  */
226
- privateKey?: Buffer;
233
+ privateKey?: string | Buffer;
227
234
  /**
228
235
  * private key passphrase
229
236
  * @type {string}
@@ -236,6 +243,13 @@ declare class ConfigurationWebsocketAPI {
236
243
  * @memberof ConfigurationWebsocketAPI
237
244
  */
238
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;
239
253
  constructor(param?: ConfigurationWebsocketAPI);
240
254
  }
241
255
  declare class ConfigurationWebsocketStreams {
@@ -587,7 +601,6 @@ declare const shouldRetryRequest: (error: AxiosError | object, method?: string,
587
601
  * This function handles retries, rate limit handling, and error handling for the HTTP request.
588
602
  *
589
603
  * @param axiosArgs The request arguments to be passed to Axios.
590
- * @param axios The Axios instance to use for the request.
591
604
  * @param configuration The configuration options for the request.
592
605
  * @returns A Promise that resolves to the API response, including the data and rate limit headers.
593
606
  */
@@ -641,6 +654,38 @@ declare function replaceWebsocketStreamsPlaceholders(str: string, variables: Rec
641
654
  * @returns {string} A formatted user agent string including package details, Node.js version, platform, and architecture.
642
655
  */
643
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[]>;
644
689
 
645
690
  declare class WebsocketEventEmitter {
646
691
  private eventEmitter;
@@ -660,6 +705,12 @@ interface WebsocketConnection {
660
705
  }>;
661
706
  pendingSubscriptions?: string[];
662
707
  ws?: WebSocketClient;
708
+ isSessionLoggedOn?: boolean;
709
+ sessionLogonReq?: {
710
+ method: string;
711
+ payload: WebsocketSendMsgOptions;
712
+ options: WebsocketSendMsgConfig;
713
+ };
663
714
  }
664
715
  declare class WebsocketCommon extends WebsocketEventEmitter {
665
716
  protected configuration: ConfigurationWebsocketAPI | ConfigurationWebsocketStreams;
@@ -683,6 +734,14 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
683
734
  * @returns void
684
735
  */
685
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[];
686
745
  /**
687
746
  * Gets a WebSocket connection from the pool or single connection.
688
747
  * If the connection mode is 'single', it returns the first connection in the pool.
@@ -747,6 +806,14 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
747
806
  * @returns Promise that resolves when the connection is closed.
748
807
  */
749
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;
750
817
  /**
751
818
  * Cleans up WebSocket connection resources.
752
819
  * Removes all listeners and clears any associated timers for the provided WebSocket client.
@@ -764,8 +831,6 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
764
831
  * Handles the opening of a WebSocket connection.
765
832
  * @param url - The URL of the WebSocket server.
766
833
  * @param targetConnection - The WebSocket connection being opened.
767
- * @param oldConnection - The previous WebSocket connection, if this is a renewal.
768
- * @param isRenewal - Indicates whether this is a connection renewal.
769
834
  * @param oldWSConnection - The WebSocket client instance associated with the old connection.
770
835
  */
771
836
  protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
@@ -835,6 +900,12 @@ interface WebsocketSendMsgOptions {
835
900
  id?: string;
836
901
  [key: string]: string | number | boolean | object | undefined;
837
902
  }
903
+ interface WebsocketSendMsgConfig {
904
+ withApiKey?: boolean;
905
+ isSigned?: boolean;
906
+ isSessionLogon?: boolean;
907
+ isSessionLogout?: boolean;
908
+ }
838
909
  declare class WebsocketAPIBase extends WebsocketCommon {
839
910
  private isConnecting;
840
911
  streamCallbackMap: Map<string, Set<(data: unknown) => void>>;
@@ -858,19 +929,13 @@ declare class WebsocketAPIBase extends WebsocketCommon {
858
929
  * @throws Error if connection times out
859
930
  */
860
931
  connect(): Promise<void>;
861
- /**
862
- * Sends a message to the WebSocket API Server.
863
- * Supports both signed and unsigned messages.
864
- * @param method The API method to call
865
- * @param payload Message parameters and options
866
- * @param options Additional requests options (withApiKey, isSigned)
867
- * @returns Promise that resolves with the server response
868
- * @throws Error if not connected
869
- */
870
- sendMessage<T = unknown>(method: string, payload?: WebsocketSendMsgOptions, options?: {
871
- withApiKey?: boolean;
872
- isSigned?: boolean;
873
- }): 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>>;
874
939
  }
875
940
  declare class WebsocketStreamsBase extends WebsocketCommon {
876
941
  private streamConnectionMap;
@@ -926,7 +991,6 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
926
991
  * @param url The URL of the WebSocket connection.
927
992
  * @param targetConnection The WebSocket connection that was opened.
928
993
  * @param oldConnection The previous WebSocket connection, if any.
929
- * @param isRenewal Whether the connection is a renewal of an existing connection.
930
994
  */
931
995
  protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
932
996
  /**
@@ -988,4 +1052,4 @@ interface WebsocketStream<T> {
988
1052
  */
989
1053
  declare function createStreamHandler<T>(websocketBase: WebsocketAPIBase | WebsocketStreamsBase, streamOrId: string, id?: string): WebsocketStream<T>;
990
1054
 
991
- 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 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
@@ -104,6 +104,13 @@ declare class ConfigurationRestAPI {
104
104
  password: string;
105
105
  };
106
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[]>;
107
114
  /**
108
115
  * enables keep-alive functionality for the connection (if httpsAgent is set then we use httpsAgent.keepAlive instead)
109
116
  * @default true
@@ -141,10 +148,10 @@ declare class ConfigurationRestAPI {
141
148
  httpsAgent?: boolean | Agent;
142
149
  /**
143
150
  * private key
144
- * @type {Buffer}
151
+ * @type {string | Buffer}
145
152
  * @memberof ConfigurationRestAPI
146
153
  */
147
- privateKey?: Buffer;
154
+ privateKey?: string | Buffer;
148
155
  /**
149
156
  * private key passphrase
150
157
  * @type {string}
@@ -220,10 +227,10 @@ declare class ConfigurationWebsocketAPI {
220
227
  poolSize?: number;
221
228
  /**
222
229
  * private key
223
- * @type {Buffer}
230
+ * @type {string | Buffer}
224
231
  * @memberof ConfigurationWebsocketAPI
225
232
  */
226
- privateKey?: Buffer;
233
+ privateKey?: string | Buffer;
227
234
  /**
228
235
  * private key passphrase
229
236
  * @type {string}
@@ -236,6 +243,13 @@ declare class ConfigurationWebsocketAPI {
236
243
  * @memberof ConfigurationWebsocketAPI
237
244
  */
238
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;
239
253
  constructor(param?: ConfigurationWebsocketAPI);
240
254
  }
241
255
  declare class ConfigurationWebsocketStreams {
@@ -587,7 +601,6 @@ declare const shouldRetryRequest: (error: AxiosError | object, method?: string,
587
601
  * This function handles retries, rate limit handling, and error handling for the HTTP request.
588
602
  *
589
603
  * @param axiosArgs The request arguments to be passed to Axios.
590
- * @param axios The Axios instance to use for the request.
591
604
  * @param configuration The configuration options for the request.
592
605
  * @returns A Promise that resolves to the API response, including the data and rate limit headers.
593
606
  */
@@ -641,6 +654,38 @@ declare function replaceWebsocketStreamsPlaceholders(str: string, variables: Rec
641
654
  * @returns {string} A formatted user agent string including package details, Node.js version, platform, and architecture.
642
655
  */
643
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[]>;
644
689
 
645
690
  declare class WebsocketEventEmitter {
646
691
  private eventEmitter;
@@ -660,6 +705,12 @@ interface WebsocketConnection {
660
705
  }>;
661
706
  pendingSubscriptions?: string[];
662
707
  ws?: WebSocketClient;
708
+ isSessionLoggedOn?: boolean;
709
+ sessionLogonReq?: {
710
+ method: string;
711
+ payload: WebsocketSendMsgOptions;
712
+ options: WebsocketSendMsgConfig;
713
+ };
663
714
  }
664
715
  declare class WebsocketCommon extends WebsocketEventEmitter {
665
716
  protected configuration: ConfigurationWebsocketAPI | ConfigurationWebsocketStreams;
@@ -683,6 +734,14 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
683
734
  * @returns void
684
735
  */
685
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[];
686
745
  /**
687
746
  * Gets a WebSocket connection from the pool or single connection.
688
747
  * If the connection mode is 'single', it returns the first connection in the pool.
@@ -747,6 +806,14 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
747
806
  * @returns Promise that resolves when the connection is closed.
748
807
  */
749
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;
750
817
  /**
751
818
  * Cleans up WebSocket connection resources.
752
819
  * Removes all listeners and clears any associated timers for the provided WebSocket client.
@@ -764,8 +831,6 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
764
831
  * Handles the opening of a WebSocket connection.
765
832
  * @param url - The URL of the WebSocket server.
766
833
  * @param targetConnection - The WebSocket connection being opened.
767
- * @param oldConnection - The previous WebSocket connection, if this is a renewal.
768
- * @param isRenewal - Indicates whether this is a connection renewal.
769
834
  * @param oldWSConnection - The WebSocket client instance associated with the old connection.
770
835
  */
771
836
  protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
@@ -835,6 +900,12 @@ interface WebsocketSendMsgOptions {
835
900
  id?: string;
836
901
  [key: string]: string | number | boolean | object | undefined;
837
902
  }
903
+ interface WebsocketSendMsgConfig {
904
+ withApiKey?: boolean;
905
+ isSigned?: boolean;
906
+ isSessionLogon?: boolean;
907
+ isSessionLogout?: boolean;
908
+ }
838
909
  declare class WebsocketAPIBase extends WebsocketCommon {
839
910
  private isConnecting;
840
911
  streamCallbackMap: Map<string, Set<(data: unknown) => void>>;
@@ -858,19 +929,13 @@ declare class WebsocketAPIBase extends WebsocketCommon {
858
929
  * @throws Error if connection times out
859
930
  */
860
931
  connect(): Promise<void>;
861
- /**
862
- * Sends a message to the WebSocket API Server.
863
- * Supports both signed and unsigned messages.
864
- * @param method The API method to call
865
- * @param payload Message parameters and options
866
- * @param options Additional requests options (withApiKey, isSigned)
867
- * @returns Promise that resolves with the server response
868
- * @throws Error if not connected
869
- */
870
- sendMessage<T = unknown>(method: string, payload?: WebsocketSendMsgOptions, options?: {
871
- withApiKey?: boolean;
872
- isSigned?: boolean;
873
- }): 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>>;
874
939
  }
875
940
  declare class WebsocketStreamsBase extends WebsocketCommon {
876
941
  private streamConnectionMap;
@@ -926,7 +991,6 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
926
991
  * @param url The URL of the WebSocket connection.
927
992
  * @param targetConnection The WebSocket connection that was opened.
928
993
  * @param oldConnection The previous WebSocket connection, if any.
929
- * @param isRenewal Whether the connection is a renewal of an existing connection.
930
994
  */
931
995
  protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
932
996
  /**
@@ -988,4 +1052,4 @@ interface WebsocketStream<T> {
988
1052
  */
989
1053
  declare function createStreamHandler<T>(websocketBase: WebsocketAPIBase | WebsocketStreamsBase, streamOrId: string, id?: string): WebsocketStream<T>;
990
1054
 
991
- 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 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 };