@binance/common 2.1.1 → 2.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/dist/index.d.mts +42 -9
- package/dist/index.d.ts +42 -9
- package/dist/index.js +110 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +109 -41
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -28,7 +28,9 @@ declare const DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL = "wss://testn
|
|
|
28
28
|
declare const DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL = "wss://fstream.binance.com";
|
|
29
29
|
declare const DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL = "wss://stream.binancefuture.com";
|
|
30
30
|
declare const DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL = "https://eapi.binance.com";
|
|
31
|
-
declare const
|
|
31
|
+
declare const DERIVATIVES_TRADING_OPTIONS_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
32
|
+
declare const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL = "wss://fstream.binance.com";
|
|
33
|
+
declare const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_TESTNET_URL = "wss://fstream.binancefuture.com";
|
|
32
34
|
declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL = "https://papi.binance.com";
|
|
33
35
|
declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
34
36
|
declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm";
|
|
@@ -759,6 +761,7 @@ interface WebsocketConnection {
|
|
|
759
761
|
}>;
|
|
760
762
|
pendingSubscriptions?: string[];
|
|
761
763
|
ws?: WebSocketClient;
|
|
764
|
+
urlPath?: string;
|
|
762
765
|
isSessionLoggedOn?: boolean;
|
|
763
766
|
sessionLogonReq?: {
|
|
764
767
|
method: string;
|
|
@@ -793,18 +796,20 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
|
|
|
793
796
|
* In 'single' mode, returns the first connection in the pool.
|
|
794
797
|
* In 'pool' mode, filters and returns connections that are ready for use.
|
|
795
798
|
* @param allowNonEstablishedWebsockets - Optional flag to include non-established WebSocket connections.
|
|
799
|
+
* @param urlPath - Optional URL path to filter connections.
|
|
796
800
|
* @returns An array of available WebSocket connections.
|
|
797
801
|
*/
|
|
798
|
-
protected getAvailableConnections(allowNonEstablishedWebsockets?: boolean): WebsocketConnection[];
|
|
802
|
+
protected getAvailableConnections(allowNonEstablishedWebsockets?: boolean, urlPath?: string): WebsocketConnection[];
|
|
799
803
|
/**
|
|
800
804
|
* Gets a WebSocket connection from the pool or single connection.
|
|
801
805
|
* If the connection mode is 'single', it returns the first connection in the pool.
|
|
802
806
|
* If the connection mode is 'pool', it returns an available connection from the pool,
|
|
803
807
|
* using a round-robin selection strategy. If no available connections are found, it throws an error.
|
|
804
808
|
* @param allowNonEstablishedWebsockets - A boolean indicating whether to allow connections that are not established.
|
|
809
|
+
* @param urlPath - An optional URL path to filter connections.
|
|
805
810
|
* @returns {WebsocketConnection} The selected WebSocket connection.
|
|
806
811
|
*/
|
|
807
|
-
protected getConnection(allowNonEstablishedWebsockets?: boolean): WebsocketConnection;
|
|
812
|
+
protected getConnection(allowNonEstablishedWebsockets?: boolean, urlPath?: string): WebsocketConnection;
|
|
808
813
|
/**
|
|
809
814
|
* Checks if the provided WebSocket connection is ready for use.
|
|
810
815
|
* A connection is considered ready if it is open, has no pending reconnection, and has not been closed.
|
|
@@ -899,9 +904,10 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
|
|
|
899
904
|
/**
|
|
900
905
|
* Connects all WebSocket connections in the pool
|
|
901
906
|
* @param url - The Websocket server URL.
|
|
907
|
+
* @param connections - An optional array of WebSocket connections to connect. If not provided, all connections in the pool are connected.
|
|
902
908
|
* @returns A promise that resolves when all connections are established.
|
|
903
909
|
*/
|
|
904
|
-
protected connectPool(url: string): Promise<void>;
|
|
910
|
+
protected connectPool(url: string, connections?: WebsocketConnection[]): Promise<void>;
|
|
905
911
|
/**
|
|
906
912
|
* Creates a new WebSocket client instance.
|
|
907
913
|
* @param url - The URL to connect to.
|
|
@@ -993,15 +999,31 @@ declare class WebsocketAPIBase extends WebsocketCommon {
|
|
|
993
999
|
}
|
|
994
1000
|
declare class WebsocketStreamsBase extends WebsocketCommon {
|
|
995
1001
|
private streamConnectionMap;
|
|
1002
|
+
protected urlPaths: string[];
|
|
996
1003
|
protected configuration: ConfigurationWebsocketStreams;
|
|
997
1004
|
protected wsURL: string;
|
|
998
1005
|
streamIdIsStrictlyNumber?: boolean;
|
|
999
1006
|
streamCallbackMap: Map<string, Set<(data: unknown) => void>>;
|
|
1000
1007
|
logger: Logger;
|
|
1001
|
-
constructor(configuration: ConfigurationWebsocketStreams, connectionPool?: WebsocketConnection[]);
|
|
1008
|
+
constructor(configuration: ConfigurationWebsocketStreams, connectionPool?: WebsocketConnection[], urlPaths?: string[]);
|
|
1009
|
+
/**
|
|
1010
|
+
* Ensures the connection pool has the required size based on the configured mode and number of URL paths.
|
|
1011
|
+
*
|
|
1012
|
+
* If no URL paths are configured, the method returns early without modifications.
|
|
1013
|
+
* In 'pool' mode, the pool size is multiplied by the number of URL paths.
|
|
1014
|
+
* In 'single' mode, only one connection per URL path is maintained.
|
|
1015
|
+
*
|
|
1016
|
+
* New connections are initialized with unique IDs and default state flags when the pool
|
|
1017
|
+
* size is less than the expected size.
|
|
1018
|
+
*
|
|
1019
|
+
* @private
|
|
1020
|
+
* @returns {void}
|
|
1021
|
+
*/
|
|
1022
|
+
private ensurePoolSizeForUrlPaths;
|
|
1002
1023
|
/**
|
|
1003
1024
|
* Formats the WebSocket URL for a given stream or streams.
|
|
1004
1025
|
* @param streams - Array of stream names to include in the URL.
|
|
1026
|
+
* @param urlPath - Optional URL path to include in the WebSocket URL.
|
|
1005
1027
|
* @returns The formatted WebSocket URL with the provided streams.
|
|
1006
1028
|
*/
|
|
1007
1029
|
private prepareURL;
|
|
@@ -1015,6 +1037,7 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
|
|
|
1015
1037
|
/**
|
|
1016
1038
|
* Handles subscription to streams and assigns them to specific connections
|
|
1017
1039
|
* @param streams Array of stream names to subscribe to
|
|
1040
|
+
* @param urlPath Optional URL path for the streams
|
|
1018
1041
|
* @returns Map of connections to streams
|
|
1019
1042
|
*/
|
|
1020
1043
|
private handleStreamAssignment;
|
|
@@ -1048,6 +1071,13 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
|
|
|
1048
1071
|
* @param oldConnection The previous WebSocket connection, if any.
|
|
1049
1072
|
*/
|
|
1050
1073
|
protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
|
|
1074
|
+
/**
|
|
1075
|
+
* Generates a stream key by combining a stream name with an optional URL path.
|
|
1076
|
+
* @param stream - The stream name to use as the key or suffix.
|
|
1077
|
+
* @param urlPath - Optional URL path to prepend to the stream name.
|
|
1078
|
+
* @returns A stream key in the format `urlPath::stream` if urlPath is provided, otherwise just the stream name.
|
|
1079
|
+
*/
|
|
1080
|
+
streamKey(stream: string, urlPath?: string): string;
|
|
1051
1081
|
/**
|
|
1052
1082
|
* Connects to the WebSocket server and subscribes to the specified streams.
|
|
1053
1083
|
* This method returns a Promise that resolves when the connection is established,
|
|
@@ -1066,17 +1096,19 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
|
|
|
1066
1096
|
* Handles both single and pool modes
|
|
1067
1097
|
* @param stream Single stream name or array of stream names to subscribe to
|
|
1068
1098
|
* @param id Optional subscription ID
|
|
1099
|
+
* @param urlPath Optional URL path for the streams
|
|
1069
1100
|
* @returns void
|
|
1070
1101
|
*/
|
|
1071
|
-
subscribe(stream: string | string[], id?: number | string): void;
|
|
1102
|
+
subscribe(stream: string | string[], id?: number | string, urlPath?: string): void;
|
|
1072
1103
|
/**
|
|
1073
1104
|
* Unsubscribes from one or multiple WebSocket streams
|
|
1074
1105
|
* Handles both single and pool modes
|
|
1075
1106
|
* @param stream Single stream name or array of stream names to unsubscribe from
|
|
1076
1107
|
* @param id Optional unsubscription ID
|
|
1108
|
+
* @param urlPath Optional URL path for the streams
|
|
1077
1109
|
* @returns void
|
|
1078
1110
|
*/
|
|
1079
|
-
unsubscribe(stream: string | string[], id?: number | string): void;
|
|
1111
|
+
unsubscribe(stream: string | string[], id?: number | string, urlPath?: string): void;
|
|
1080
1112
|
/**
|
|
1081
1113
|
* Checks if the specified stream is currently subscribed.
|
|
1082
1114
|
* @param stream - The name of the stream to check.
|
|
@@ -1103,9 +1135,10 @@ interface WebsocketStream<T> {
|
|
|
1103
1135
|
* @param {WebsocketAPIBase | WebsocketStreamsBase} websocketBase The WebSocket base instance
|
|
1104
1136
|
* @param {string} streamOrId The stream identifier
|
|
1105
1137
|
* @param {string} [id] Optional additional identifier
|
|
1138
|
+
* @param {string} [urlPath] Optional URL path for the stream
|
|
1106
1139
|
* @returns {WebsocketStream<T>} A stream handler with methods to register callbacks and unsubscribe
|
|
1107
1140
|
*/
|
|
1108
|
-
declare function createStreamHandler<T>(websocketBase: WebsocketAPIBase | WebsocketStreamsBase, streamOrId: string, id?: number | string): WebsocketStream<T>;
|
|
1141
|
+
declare function createStreamHandler<T>(websocketBase: WebsocketAPIBase | WebsocketStreamsBase, streamOrId: string, id?: number | string, urlPath?: string): WebsocketStream<T>;
|
|
1109
1142
|
//#endregion
|
|
1110
|
-
export { ALGO_REST_API_PROD_URL, 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, ObjectType, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, RequestArgs, RequiredError, RestApiRateLimit, 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, SendMessageOptions, ServerError, TimeUnit, TimerRecord, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, WebsocketApiRateLimit, WebsocketApiResponse, WebsocketCommon, WebsocketConnection, WebsocketEventEmitter, WebsocketSendMsgConfig, WebsocketSendMsgOptions, WebsocketStream, 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 };
|
|
1143
|
+
export { ALGO_REST_API_PROD_URL, 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_REST_API_TESTNET_URL, DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_TESTNET_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, ObjectType, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, RequestArgs, RequiredError, RestApiRateLimit, 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, SendMessageOptions, ServerError, TimeUnit, TimerRecord, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, WebsocketApiRateLimit, WebsocketApiResponse, WebsocketCommon, WebsocketConnection, WebsocketEventEmitter, WebsocketSendMsgConfig, WebsocketSendMsgOptions, WebsocketStream, 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 };
|
|
1111
1144
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -28,7 +28,9 @@ declare const DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL = "wss://testn
|
|
|
28
28
|
declare const DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL = "wss://fstream.binance.com";
|
|
29
29
|
declare const DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL = "wss://stream.binancefuture.com";
|
|
30
30
|
declare const DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL = "https://eapi.binance.com";
|
|
31
|
-
declare const
|
|
31
|
+
declare const DERIVATIVES_TRADING_OPTIONS_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
32
|
+
declare const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL = "wss://fstream.binance.com";
|
|
33
|
+
declare const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_TESTNET_URL = "wss://fstream.binancefuture.com";
|
|
32
34
|
declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL = "https://papi.binance.com";
|
|
33
35
|
declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
34
36
|
declare const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm";
|
|
@@ -759,6 +761,7 @@ interface WebsocketConnection {
|
|
|
759
761
|
}>;
|
|
760
762
|
pendingSubscriptions?: string[];
|
|
761
763
|
ws?: WebSocketClient;
|
|
764
|
+
urlPath?: string;
|
|
762
765
|
isSessionLoggedOn?: boolean;
|
|
763
766
|
sessionLogonReq?: {
|
|
764
767
|
method: string;
|
|
@@ -793,18 +796,20 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
|
|
|
793
796
|
* In 'single' mode, returns the first connection in the pool.
|
|
794
797
|
* In 'pool' mode, filters and returns connections that are ready for use.
|
|
795
798
|
* @param allowNonEstablishedWebsockets - Optional flag to include non-established WebSocket connections.
|
|
799
|
+
* @param urlPath - Optional URL path to filter connections.
|
|
796
800
|
* @returns An array of available WebSocket connections.
|
|
797
801
|
*/
|
|
798
|
-
protected getAvailableConnections(allowNonEstablishedWebsockets?: boolean): WebsocketConnection[];
|
|
802
|
+
protected getAvailableConnections(allowNonEstablishedWebsockets?: boolean, urlPath?: string): WebsocketConnection[];
|
|
799
803
|
/**
|
|
800
804
|
* Gets a WebSocket connection from the pool or single connection.
|
|
801
805
|
* If the connection mode is 'single', it returns the first connection in the pool.
|
|
802
806
|
* If the connection mode is 'pool', it returns an available connection from the pool,
|
|
803
807
|
* using a round-robin selection strategy. If no available connections are found, it throws an error.
|
|
804
808
|
* @param allowNonEstablishedWebsockets - A boolean indicating whether to allow connections that are not established.
|
|
809
|
+
* @param urlPath - An optional URL path to filter connections.
|
|
805
810
|
* @returns {WebsocketConnection} The selected WebSocket connection.
|
|
806
811
|
*/
|
|
807
|
-
protected getConnection(allowNonEstablishedWebsockets?: boolean): WebsocketConnection;
|
|
812
|
+
protected getConnection(allowNonEstablishedWebsockets?: boolean, urlPath?: string): WebsocketConnection;
|
|
808
813
|
/**
|
|
809
814
|
* Checks if the provided WebSocket connection is ready for use.
|
|
810
815
|
* A connection is considered ready if it is open, has no pending reconnection, and has not been closed.
|
|
@@ -899,9 +904,10 @@ declare class WebsocketCommon extends WebsocketEventEmitter {
|
|
|
899
904
|
/**
|
|
900
905
|
* Connects all WebSocket connections in the pool
|
|
901
906
|
* @param url - The Websocket server URL.
|
|
907
|
+
* @param connections - An optional array of WebSocket connections to connect. If not provided, all connections in the pool are connected.
|
|
902
908
|
* @returns A promise that resolves when all connections are established.
|
|
903
909
|
*/
|
|
904
|
-
protected connectPool(url: string): Promise<void>;
|
|
910
|
+
protected connectPool(url: string, connections?: WebsocketConnection[]): Promise<void>;
|
|
905
911
|
/**
|
|
906
912
|
* Creates a new WebSocket client instance.
|
|
907
913
|
* @param url - The URL to connect to.
|
|
@@ -993,15 +999,31 @@ declare class WebsocketAPIBase extends WebsocketCommon {
|
|
|
993
999
|
}
|
|
994
1000
|
declare class WebsocketStreamsBase extends WebsocketCommon {
|
|
995
1001
|
private streamConnectionMap;
|
|
1002
|
+
protected urlPaths: string[];
|
|
996
1003
|
protected configuration: ConfigurationWebsocketStreams;
|
|
997
1004
|
protected wsURL: string;
|
|
998
1005
|
streamIdIsStrictlyNumber?: boolean;
|
|
999
1006
|
streamCallbackMap: Map<string, Set<(data: unknown) => void>>;
|
|
1000
1007
|
logger: Logger;
|
|
1001
|
-
constructor(configuration: ConfigurationWebsocketStreams, connectionPool?: WebsocketConnection[]);
|
|
1008
|
+
constructor(configuration: ConfigurationWebsocketStreams, connectionPool?: WebsocketConnection[], urlPaths?: string[]);
|
|
1009
|
+
/**
|
|
1010
|
+
* Ensures the connection pool has the required size based on the configured mode and number of URL paths.
|
|
1011
|
+
*
|
|
1012
|
+
* If no URL paths are configured, the method returns early without modifications.
|
|
1013
|
+
* In 'pool' mode, the pool size is multiplied by the number of URL paths.
|
|
1014
|
+
* In 'single' mode, only one connection per URL path is maintained.
|
|
1015
|
+
*
|
|
1016
|
+
* New connections are initialized with unique IDs and default state flags when the pool
|
|
1017
|
+
* size is less than the expected size.
|
|
1018
|
+
*
|
|
1019
|
+
* @private
|
|
1020
|
+
* @returns {void}
|
|
1021
|
+
*/
|
|
1022
|
+
private ensurePoolSizeForUrlPaths;
|
|
1002
1023
|
/**
|
|
1003
1024
|
* Formats the WebSocket URL for a given stream or streams.
|
|
1004
1025
|
* @param streams - Array of stream names to include in the URL.
|
|
1026
|
+
* @param urlPath - Optional URL path to include in the WebSocket URL.
|
|
1005
1027
|
* @returns The formatted WebSocket URL with the provided streams.
|
|
1006
1028
|
*/
|
|
1007
1029
|
private prepareURL;
|
|
@@ -1015,6 +1037,7 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
|
|
|
1015
1037
|
/**
|
|
1016
1038
|
* Handles subscription to streams and assigns them to specific connections
|
|
1017
1039
|
* @param streams Array of stream names to subscribe to
|
|
1040
|
+
* @param urlPath Optional URL path for the streams
|
|
1018
1041
|
* @returns Map of connections to streams
|
|
1019
1042
|
*/
|
|
1020
1043
|
private handleStreamAssignment;
|
|
@@ -1048,6 +1071,13 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
|
|
|
1048
1071
|
* @param oldConnection The previous WebSocket connection, if any.
|
|
1049
1072
|
*/
|
|
1050
1073
|
protected onOpen(url: string, targetConnection: WebsocketConnection, oldWSConnection: WebSocketClient): void;
|
|
1074
|
+
/**
|
|
1075
|
+
* Generates a stream key by combining a stream name with an optional URL path.
|
|
1076
|
+
* @param stream - The stream name to use as the key or suffix.
|
|
1077
|
+
* @param urlPath - Optional URL path to prepend to the stream name.
|
|
1078
|
+
* @returns A stream key in the format `urlPath::stream` if urlPath is provided, otherwise just the stream name.
|
|
1079
|
+
*/
|
|
1080
|
+
streamKey(stream: string, urlPath?: string): string;
|
|
1051
1081
|
/**
|
|
1052
1082
|
* Connects to the WebSocket server and subscribes to the specified streams.
|
|
1053
1083
|
* This method returns a Promise that resolves when the connection is established,
|
|
@@ -1066,17 +1096,19 @@ declare class WebsocketStreamsBase extends WebsocketCommon {
|
|
|
1066
1096
|
* Handles both single and pool modes
|
|
1067
1097
|
* @param stream Single stream name or array of stream names to subscribe to
|
|
1068
1098
|
* @param id Optional subscription ID
|
|
1099
|
+
* @param urlPath Optional URL path for the streams
|
|
1069
1100
|
* @returns void
|
|
1070
1101
|
*/
|
|
1071
|
-
subscribe(stream: string | string[], id?: number | string): void;
|
|
1102
|
+
subscribe(stream: string | string[], id?: number | string, urlPath?: string): void;
|
|
1072
1103
|
/**
|
|
1073
1104
|
* Unsubscribes from one or multiple WebSocket streams
|
|
1074
1105
|
* Handles both single and pool modes
|
|
1075
1106
|
* @param stream Single stream name or array of stream names to unsubscribe from
|
|
1076
1107
|
* @param id Optional unsubscription ID
|
|
1108
|
+
* @param urlPath Optional URL path for the streams
|
|
1077
1109
|
* @returns void
|
|
1078
1110
|
*/
|
|
1079
|
-
unsubscribe(stream: string | string[], id?: number | string): void;
|
|
1111
|
+
unsubscribe(stream: string | string[], id?: number | string, urlPath?: string): void;
|
|
1080
1112
|
/**
|
|
1081
1113
|
* Checks if the specified stream is currently subscribed.
|
|
1082
1114
|
* @param stream - The name of the stream to check.
|
|
@@ -1103,9 +1135,10 @@ interface WebsocketStream<T> {
|
|
|
1103
1135
|
* @param {WebsocketAPIBase | WebsocketStreamsBase} websocketBase The WebSocket base instance
|
|
1104
1136
|
* @param {string} streamOrId The stream identifier
|
|
1105
1137
|
* @param {string} [id] Optional additional identifier
|
|
1138
|
+
* @param {string} [urlPath] Optional URL path for the stream
|
|
1106
1139
|
* @returns {WebsocketStream<T>} A stream handler with methods to register callbacks and unsubscribe
|
|
1107
1140
|
*/
|
|
1108
|
-
declare function createStreamHandler<T>(websocketBase: WebsocketAPIBase | WebsocketStreamsBase, streamOrId: string, id?: number | string): WebsocketStream<T>;
|
|
1141
|
+
declare function createStreamHandler<T>(websocketBase: WebsocketAPIBase | WebsocketStreamsBase, streamOrId: string, id?: number | string, urlPath?: string): WebsocketStream<T>;
|
|
1109
1142
|
//#endregion
|
|
1110
|
-
export { ALGO_REST_API_PROD_URL, 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, ObjectType, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, RequestArgs, RequiredError, RestApiRateLimit, 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, SendMessageOptions, ServerError, TimeUnit, TimerRecord, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, WebsocketApiRateLimit, WebsocketApiResponse, WebsocketCommon, WebsocketConnection, WebsocketEventEmitter, WebsocketSendMsgConfig, WebsocketSendMsgOptions, WebsocketStream, 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 };
|
|
1143
|
+
export { ALGO_REST_API_PROD_URL, 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_REST_API_TESTNET_URL, DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL, DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_TESTNET_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, ObjectType, PAY_REST_API_PROD_URL, REBATE_REST_API_PROD_URL, RateLimitBanError, RequestArgs, RequiredError, RestApiRateLimit, 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, SendMessageOptions, ServerError, TimeUnit, TimerRecord, TooManyRequestsError, UnauthorizedError, VIP_LOAN_REST_API_PROD_URL, WALLET_REST_API_PROD_URL, WebsocketAPIBase, WebsocketApiRateLimit, WebsocketApiResponse, WebsocketCommon, WebsocketConnection, WebsocketEventEmitter, WebsocketSendMsgConfig, WebsocketSendMsgOptions, WebsocketStream, 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 };
|
|
1111
1144
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -718,7 +718,9 @@ const DERIVATIVES_TRADING_USDS_FUTURES_WS_API_TESTNET_URL = "wss://testnet.binan
|
|
|
718
718
|
const DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_PROD_URL = "wss://fstream.binance.com";
|
|
719
719
|
const DERIVATIVES_TRADING_USDS_FUTURES_WS_STREAMS_TESTNET_URL = "wss://stream.binancefuture.com";
|
|
720
720
|
const DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL = "https://eapi.binance.com";
|
|
721
|
-
const
|
|
721
|
+
const DERIVATIVES_TRADING_OPTIONS_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
722
|
+
const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL = "wss://fstream.binance.com";
|
|
723
|
+
const DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_TESTNET_URL = "wss://fstream.binancefuture.com";
|
|
722
724
|
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL = "https://papi.binance.com";
|
|
723
725
|
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_TESTNET_URL = "https://testnet.binancefuture.com";
|
|
724
726
|
const DERIVATIVES_TRADING_PORTFOLIO_MARGIN_WS_STREAMS_PROD_URL = "wss://fstream.binance.com/pm";
|
|
@@ -980,10 +982,11 @@ var WebsocketCommon = class WebsocketCommon extends WebsocketEventEmitter {
|
|
|
980
982
|
* In 'single' mode, returns the first connection in the pool.
|
|
981
983
|
* In 'pool' mode, filters and returns connections that are ready for use.
|
|
982
984
|
* @param allowNonEstablishedWebsockets - Optional flag to include non-established WebSocket connections.
|
|
985
|
+
* @param urlPath - Optional URL path to filter connections.
|
|
983
986
|
* @returns An array of available WebSocket connections.
|
|
984
987
|
*/
|
|
985
|
-
getAvailableConnections(allowNonEstablishedWebsockets = false) {
|
|
986
|
-
if (this.mode === "single") return [this.connectionPool[0]];
|
|
988
|
+
getAvailableConnections(allowNonEstablishedWebsockets = false, urlPath) {
|
|
989
|
+
if (this.mode === "single" && !urlPath) return [this.connectionPool[0]];
|
|
987
990
|
return this.connectionPool.filter((connection) => this.isConnectionReady(connection, allowNonEstablishedWebsockets));
|
|
988
991
|
}
|
|
989
992
|
/**
|
|
@@ -992,10 +995,14 @@ var WebsocketCommon = class WebsocketCommon extends WebsocketEventEmitter {
|
|
|
992
995
|
* If the connection mode is 'pool', it returns an available connection from the pool,
|
|
993
996
|
* using a round-robin selection strategy. If no available connections are found, it throws an error.
|
|
994
997
|
* @param allowNonEstablishedWebsockets - A boolean indicating whether to allow connections that are not established.
|
|
998
|
+
* @param urlPath - An optional URL path to filter connections.
|
|
995
999
|
* @returns {WebsocketConnection} The selected WebSocket connection.
|
|
996
1000
|
*/
|
|
997
|
-
getConnection(allowNonEstablishedWebsockets = false) {
|
|
998
|
-
const availableConnections = this.getAvailableConnections(allowNonEstablishedWebsockets)
|
|
1001
|
+
getConnection(allowNonEstablishedWebsockets = false, urlPath) {
|
|
1002
|
+
const availableConnections = this.getAvailableConnections(allowNonEstablishedWebsockets, urlPath).filter((connection) => {
|
|
1003
|
+
if (urlPath) return connection.urlPath === urlPath;
|
|
1004
|
+
return true;
|
|
1005
|
+
});
|
|
999
1006
|
if (availableConnections.length === 0) throw new Error("No available Websocket connections are ready.");
|
|
1000
1007
|
const selectedConnection = availableConnections[this.roundRobinIndex % availableConnections.length];
|
|
1001
1008
|
this.roundRobinIndex = (this.roundRobinIndex + 1) % availableConnections.length;
|
|
@@ -1193,14 +1200,15 @@ var WebsocketCommon = class WebsocketCommon extends WebsocketEventEmitter {
|
|
|
1193
1200
|
/**
|
|
1194
1201
|
* Connects all WebSocket connections in the pool
|
|
1195
1202
|
* @param url - The Websocket server URL.
|
|
1203
|
+
* @param connections - An optional array of WebSocket connections to connect. If not provided, all connections in the pool are connected.
|
|
1196
1204
|
* @returns A promise that resolves when all connections are established.
|
|
1197
1205
|
*/
|
|
1198
|
-
async connectPool(url) {
|
|
1199
|
-
const connectPromises = this.connectionPool.map((connection) => new Promise((resolve, reject) => {
|
|
1206
|
+
async connectPool(url, connections) {
|
|
1207
|
+
const connectPromises = (connections ?? this.connectionPool).map((connection) => new Promise((resolve, reject) => {
|
|
1200
1208
|
this.initConnect(url, false, connection);
|
|
1201
|
-
connection.ws?.
|
|
1202
|
-
connection.ws?.
|
|
1203
|
-
connection.ws?.
|
|
1209
|
+
connection.ws?.once("open", () => resolve());
|
|
1210
|
+
connection.ws?.once("error", (err) => reject(err));
|
|
1211
|
+
connection.ws?.once("close", () => reject(/* @__PURE__ */ new Error("Connection closed unexpectedly.")));
|
|
1204
1212
|
}));
|
|
1205
1213
|
await Promise.all(connectPromises);
|
|
1206
1214
|
}
|
|
@@ -1479,7 +1487,7 @@ var WebsocketAPIBase = class extends WebsocketCommon {
|
|
|
1479
1487
|
}
|
|
1480
1488
|
};
|
|
1481
1489
|
var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
1482
|
-
constructor(configuration, connectionPool = []) {
|
|
1490
|
+
constructor(configuration, connectionPool = [], urlPaths = []) {
|
|
1483
1491
|
super(configuration, connectionPool);
|
|
1484
1492
|
this.streamConnectionMap = /* @__PURE__ */ new Map();
|
|
1485
1493
|
this.streamIdIsStrictlyNumber = false;
|
|
@@ -1487,14 +1495,42 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1487
1495
|
this.logger = Logger.getInstance();
|
|
1488
1496
|
this.configuration = configuration;
|
|
1489
1497
|
this.wsURL = configuration.wsURL;
|
|
1498
|
+
this.urlPaths = urlPaths;
|
|
1499
|
+
this.ensurePoolSizeForUrlPaths();
|
|
1500
|
+
}
|
|
1501
|
+
/**
|
|
1502
|
+
* Ensures the connection pool has the required size based on the configured mode and number of URL paths.
|
|
1503
|
+
*
|
|
1504
|
+
* If no URL paths are configured, the method returns early without modifications.
|
|
1505
|
+
* In 'pool' mode, the pool size is multiplied by the number of URL paths.
|
|
1506
|
+
* In 'single' mode, only one connection per URL path is maintained.
|
|
1507
|
+
*
|
|
1508
|
+
* New connections are initialized with unique IDs and default state flags when the pool
|
|
1509
|
+
* size is less than the expected size.
|
|
1510
|
+
*
|
|
1511
|
+
* @private
|
|
1512
|
+
* @returns {void}
|
|
1513
|
+
*/
|
|
1514
|
+
ensurePoolSizeForUrlPaths() {
|
|
1515
|
+
if (this.urlPaths.length === 0) return;
|
|
1516
|
+
const expected = ((this.configuration?.mode ?? "single") === "pool" && this.configuration?.poolSize ? this.configuration.poolSize : 1) * this.urlPaths.length;
|
|
1517
|
+
while (this.connectionPool.length < expected) this.connectionPool.push({
|
|
1518
|
+
id: randomString(),
|
|
1519
|
+
closeInitiated: false,
|
|
1520
|
+
reconnectionPending: false,
|
|
1521
|
+
renewalPending: false,
|
|
1522
|
+
pendingRequests: /* @__PURE__ */ new Map(),
|
|
1523
|
+
pendingSubscriptions: []
|
|
1524
|
+
});
|
|
1490
1525
|
}
|
|
1491
1526
|
/**
|
|
1492
1527
|
* Formats the WebSocket URL for a given stream or streams.
|
|
1493
1528
|
* @param streams - Array of stream names to include in the URL.
|
|
1529
|
+
* @param urlPath - Optional URL path to include in the WebSocket URL.
|
|
1494
1530
|
* @returns The formatted WebSocket URL with the provided streams.
|
|
1495
1531
|
*/
|
|
1496
|
-
prepareURL(streams = []) {
|
|
1497
|
-
let url = `${this.wsURL}/stream?streams=${streams.join("/")}`;
|
|
1532
|
+
prepareURL(streams = [], urlPath) {
|
|
1533
|
+
let url = `${urlPath ? `${this.wsURL}/${urlPath}` : this.wsURL}/stream?streams=${streams.join("/")}`;
|
|
1498
1534
|
if (this.configuration?.timeUnit) try {
|
|
1499
1535
|
const _timeUnit = validateTimeUnit(this.configuration.timeUnit);
|
|
1500
1536
|
url = `${url}${url.includes("?") ? "&" : "?"}timeUnit=${_timeUnit}`;
|
|
@@ -1510,22 +1546,24 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1510
1546
|
* @returns The formatted WebSocket URL with streams and optional parameters.
|
|
1511
1547
|
*/
|
|
1512
1548
|
getReconnectURL(url, targetConnection) {
|
|
1513
|
-
const streams = Array.from(this.streamConnectionMap.keys()).filter((stream) => this.streamConnectionMap.get(stream) === targetConnection);
|
|
1514
|
-
return this.prepareURL(streams);
|
|
1549
|
+
const streams = Array.from(this.streamConnectionMap.keys()).filter((stream) => this.streamConnectionMap.get(stream) === targetConnection).map((key) => key.includes("::") ? key.split("::").slice(1).join("::") : key);
|
|
1550
|
+
return this.prepareURL(streams, targetConnection?.urlPath);
|
|
1515
1551
|
}
|
|
1516
1552
|
/**
|
|
1517
1553
|
* Handles subscription to streams and assigns them to specific connections
|
|
1518
1554
|
* @param streams Array of stream names to subscribe to
|
|
1555
|
+
* @param urlPath Optional URL path for the streams
|
|
1519
1556
|
* @returns Map of connections to streams
|
|
1520
1557
|
*/
|
|
1521
|
-
handleStreamAssignment(streams) {
|
|
1558
|
+
handleStreamAssignment(streams, urlPath) {
|
|
1522
1559
|
const connectionStreamMap = /* @__PURE__ */ new Map();
|
|
1523
1560
|
streams.forEach((stream) => {
|
|
1524
|
-
|
|
1525
|
-
|
|
1561
|
+
const key = this.streamKey(stream, urlPath);
|
|
1562
|
+
if (!this.streamCallbackMap.has(key)) this.streamCallbackMap.set(key, /* @__PURE__ */ new Set());
|
|
1563
|
+
let connection = this.streamConnectionMap.get(key);
|
|
1526
1564
|
if (!connection || connection.closeInitiated || connection.reconnectionPending) {
|
|
1527
|
-
connection = this.getConnection(true);
|
|
1528
|
-
this.streamConnectionMap.set(
|
|
1565
|
+
connection = this.getConnection(true, urlPath);
|
|
1566
|
+
this.streamConnectionMap.set(key, connection);
|
|
1529
1567
|
}
|
|
1530
1568
|
if (!connectionStreamMap.has(connection)) connectionStreamMap.set(connection, []);
|
|
1531
1569
|
connectionStreamMap.get(connection)?.push(stream);
|
|
@@ -1571,7 +1609,10 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1571
1609
|
try {
|
|
1572
1610
|
const parsedData = (0, json_with_bigint.JSONParse)(data);
|
|
1573
1611
|
const streamName = parsedData?.stream;
|
|
1574
|
-
if (streamName
|
|
1612
|
+
if (streamName) {
|
|
1613
|
+
const key = this.streamKey(streamName, connection?.urlPath);
|
|
1614
|
+
if (this.streamCallbackMap.has(key)) this.streamCallbackMap.get(key)?.forEach((callback) => callback(parsedData.data));
|
|
1615
|
+
}
|
|
1575
1616
|
} catch (error) {
|
|
1576
1617
|
this.logger.error("Failed to parse WebSocket message:", data, error);
|
|
1577
1618
|
}
|
|
@@ -1589,6 +1630,15 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1589
1630
|
super.onOpen(url, targetConnection, oldWSConnection);
|
|
1590
1631
|
}
|
|
1591
1632
|
/**
|
|
1633
|
+
* Generates a stream key by combining a stream name with an optional URL path.
|
|
1634
|
+
* @param stream - The stream name to use as the key or suffix.
|
|
1635
|
+
* @param urlPath - Optional URL path to prepend to the stream name.
|
|
1636
|
+
* @returns A stream key in the format `urlPath::stream` if urlPath is provided, otherwise just the stream name.
|
|
1637
|
+
*/
|
|
1638
|
+
streamKey(stream, urlPath) {
|
|
1639
|
+
return urlPath ? `${urlPath}::${stream}` : stream;
|
|
1640
|
+
}
|
|
1641
|
+
/**
|
|
1592
1642
|
* Connects to the WebSocket server and subscribes to the specified streams.
|
|
1593
1643
|
* This method returns a Promise that resolves when the connection is established,
|
|
1594
1644
|
* or rejects with an error if the connection fails to be established within 10 seconds.
|
|
@@ -1601,7 +1651,15 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1601
1651
|
const timeout = setTimeout(() => {
|
|
1602
1652
|
reject(/* @__PURE__ */ new Error("Websocket connection timed out"));
|
|
1603
1653
|
}, 1e4);
|
|
1604
|
-
|
|
1654
|
+
const basePoolSize = (this.configuration?.mode ?? "single") === "pool" && this.configuration?.poolSize ? this.configuration.poolSize : 1;
|
|
1655
|
+
const connections = this.urlPaths.length > 0 ? this.urlPaths.map((path, i) => {
|
|
1656
|
+
const start = i * basePoolSize;
|
|
1657
|
+
const end = start + basePoolSize;
|
|
1658
|
+
const subset = this.connectionPool.slice(start, end);
|
|
1659
|
+
subset.forEach((c) => c.urlPath = path);
|
|
1660
|
+
return this.connectPool(this.prepareURL(streams, path), subset);
|
|
1661
|
+
}) : [this.connectPool(this.prepareURL(streams))];
|
|
1662
|
+
Promise.all(connections).then(() => resolve()).catch((error) => reject(error)).finally(() => clearTimeout(timeout));
|
|
1605
1663
|
});
|
|
1606
1664
|
}
|
|
1607
1665
|
/**
|
|
@@ -1618,17 +1676,21 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1618
1676
|
* Handles both single and pool modes
|
|
1619
1677
|
* @param stream Single stream name or array of stream names to subscribe to
|
|
1620
1678
|
* @param id Optional subscription ID
|
|
1679
|
+
* @param urlPath Optional URL path for the streams
|
|
1621
1680
|
* @returns void
|
|
1622
1681
|
*/
|
|
1623
|
-
subscribe(stream, id) {
|
|
1624
|
-
const streams = (Array.isArray(stream) ? stream : [stream]).filter((stream$1) =>
|
|
1625
|
-
|
|
1682
|
+
subscribe(stream, id, urlPath) {
|
|
1683
|
+
const streams = (Array.isArray(stream) ? stream : [stream]).filter((stream$1) => {
|
|
1684
|
+
const key = this.streamKey(stream$1, urlPath);
|
|
1685
|
+
return !this.streamConnectionMap.has(key);
|
|
1686
|
+
});
|
|
1687
|
+
this.handleStreamAssignment(streams, urlPath).forEach((assignedStreams, connection) => {
|
|
1626
1688
|
if (!this.isConnected(connection)) {
|
|
1627
|
-
this.logger.info(`Connection ${connection.id} is not ready. Queuing subscription for streams: ${
|
|
1628
|
-
connection.pendingSubscriptions?.push(...
|
|
1689
|
+
this.logger.info(`Connection ${connection.id} is not ready. Queuing subscription for streams: ${assignedStreams}`);
|
|
1690
|
+
connection.pendingSubscriptions?.push(...assignedStreams);
|
|
1629
1691
|
return;
|
|
1630
1692
|
}
|
|
1631
|
-
this.sendSubscriptionPayload(connection,
|
|
1693
|
+
this.sendSubscriptionPayload(connection, assignedStreams, id);
|
|
1632
1694
|
});
|
|
1633
1695
|
}
|
|
1634
1696
|
/**
|
|
@@ -1636,16 +1698,18 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1636
1698
|
* Handles both single and pool modes
|
|
1637
1699
|
* @param stream Single stream name or array of stream names to unsubscribe from
|
|
1638
1700
|
* @param id Optional unsubscription ID
|
|
1701
|
+
* @param urlPath Optional URL path for the streams
|
|
1639
1702
|
* @returns void
|
|
1640
1703
|
*/
|
|
1641
|
-
unsubscribe(stream, id) {
|
|
1704
|
+
unsubscribe(stream, id, urlPath) {
|
|
1642
1705
|
(Array.isArray(stream) ? stream : [stream]).forEach((stream$1) => {
|
|
1643
|
-
const
|
|
1706
|
+
const key = this.streamKey(stream$1, urlPath);
|
|
1707
|
+
const connection = this.streamConnectionMap.get(key);
|
|
1644
1708
|
if (!connection || !connection.ws || !this.isConnected(connection)) {
|
|
1645
1709
|
this.logger.warn(`Stream ${stream$1} not associated with an active connection.`);
|
|
1646
1710
|
return;
|
|
1647
1711
|
}
|
|
1648
|
-
if (!this.streamCallbackMap.has(
|
|
1712
|
+
if (!this.streamCallbackMap.has(key) || this.streamCallbackMap.get(key)?.size === 0) {
|
|
1649
1713
|
const payload = {
|
|
1650
1714
|
method: "UNSUBSCRIBE",
|
|
1651
1715
|
params: [stream$1],
|
|
@@ -1653,8 +1717,8 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1653
1717
|
};
|
|
1654
1718
|
this.logger.debug("UNSUBSCRIBE", payload);
|
|
1655
1719
|
this.send(JSON.stringify(payload), void 0, false, 0, connection);
|
|
1656
|
-
this.streamConnectionMap.delete(
|
|
1657
|
-
this.streamCallbackMap.delete(
|
|
1720
|
+
this.streamConnectionMap.delete(key);
|
|
1721
|
+
this.streamCallbackMap.delete(key);
|
|
1658
1722
|
}
|
|
1659
1723
|
});
|
|
1660
1724
|
}
|
|
@@ -1664,7 +1728,9 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1664
1728
|
* @returns `true` if the stream is currently subscribed, `false` otherwise.
|
|
1665
1729
|
*/
|
|
1666
1730
|
isSubscribed(stream) {
|
|
1667
|
-
|
|
1731
|
+
if (this.streamConnectionMap.has(stream)) return true;
|
|
1732
|
+
for (const key of this.streamConnectionMap.keys()) if (key.endsWith(`::${stream}`)) return true;
|
|
1733
|
+
return false;
|
|
1668
1734
|
}
|
|
1669
1735
|
};
|
|
1670
1736
|
/**
|
|
@@ -1674,10 +1740,12 @@ var WebsocketStreamsBase = class extends WebsocketCommon {
|
|
|
1674
1740
|
* @param {WebsocketAPIBase | WebsocketStreamsBase} websocketBase The WebSocket base instance
|
|
1675
1741
|
* @param {string} streamOrId The stream identifier
|
|
1676
1742
|
* @param {string} [id] Optional additional identifier
|
|
1743
|
+
* @param {string} [urlPath] Optional URL path for the stream
|
|
1677
1744
|
* @returns {WebsocketStream<T>} A stream handler with methods to register callbacks and unsubscribe
|
|
1678
1745
|
*/
|
|
1679
|
-
function createStreamHandler(websocketBase, streamOrId, id) {
|
|
1680
|
-
|
|
1746
|
+
function createStreamHandler(websocketBase, streamOrId, id, urlPath) {
|
|
1747
|
+
const key = websocketBase instanceof WebsocketStreamsBase ? websocketBase.streamKey(streamOrId, urlPath) : streamOrId;
|
|
1748
|
+
if (websocketBase instanceof WebsocketStreamsBase) websocketBase.subscribe(streamOrId, id, urlPath);
|
|
1681
1749
|
let registeredCallback;
|
|
1682
1750
|
return {
|
|
1683
1751
|
on: (event, callback) => {
|
|
@@ -1687,14 +1755,14 @@ function createStreamHandler(websocketBase, streamOrId, id) {
|
|
|
1687
1755
|
websocketBase.logger.error(`Error in stream callback: ${err}`);
|
|
1688
1756
|
});
|
|
1689
1757
|
};
|
|
1690
|
-
const callbackSet = websocketBase.streamCallbackMap.get(
|
|
1758
|
+
const callbackSet = websocketBase.streamCallbackMap.get(key) ?? /* @__PURE__ */ new Set();
|
|
1691
1759
|
callbackSet.add(registeredCallback);
|
|
1692
|
-
websocketBase.streamCallbackMap.set(
|
|
1760
|
+
websocketBase.streamCallbackMap.set(key, callbackSet);
|
|
1693
1761
|
}
|
|
1694
1762
|
},
|
|
1695
1763
|
unsubscribe: () => {
|
|
1696
|
-
if (registeredCallback) websocketBase.streamCallbackMap.get(
|
|
1697
|
-
if (websocketBase instanceof WebsocketStreamsBase) websocketBase.unsubscribe(streamOrId, id);
|
|
1764
|
+
if (registeredCallback) websocketBase.streamCallbackMap.get(key)?.delete(registeredCallback);
|
|
1765
|
+
if (websocketBase instanceof WebsocketStreamsBase) websocketBase.unsubscribe(streamOrId, id, urlPath);
|
|
1698
1766
|
}
|
|
1699
1767
|
};
|
|
1700
1768
|
}
|
|
@@ -1717,7 +1785,9 @@ exports.DERIVATIVES_TRADING_COIN_FUTURES_WS_API_TESTNET_URL = DERIVATIVES_TRADIN
|
|
|
1717
1785
|
exports.DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL = DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_PROD_URL;
|
|
1718
1786
|
exports.DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL = DERIVATIVES_TRADING_COIN_FUTURES_WS_STREAMS_TESTNET_URL;
|
|
1719
1787
|
exports.DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL = DERIVATIVES_TRADING_OPTIONS_REST_API_PROD_URL;
|
|
1788
|
+
exports.DERIVATIVES_TRADING_OPTIONS_REST_API_TESTNET_URL = DERIVATIVES_TRADING_OPTIONS_REST_API_TESTNET_URL;
|
|
1720
1789
|
exports.DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL = DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL;
|
|
1790
|
+
exports.DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_TESTNET_URL = DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_TESTNET_URL;
|
|
1721
1791
|
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_REST_API_PROD_URL;
|
|
1722
1792
|
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_PRO_WS_STREAMS_PROD_URL;
|
|
1723
1793
|
exports.DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL = DERIVATIVES_TRADING_PORTFOLIO_MARGIN_REST_API_PROD_URL;
|