Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
run: npm run ts-test-static
- name: Static Tests (JS CJS)
run: npm run static-test
- name: Futures WS endpoint migration tests
run: npm run ws-tests-migration
- name: Live Tests (TS ESM)
run: npm run ts-test-live
- name: CJS test
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"ws-tests": "mocha ./tests/binance-class-ws.test.ts",
"ws-tests-spot": "mocha ./tests/binance-ws-spot.test.ts --exit",
"ws-tests-futures": "mocha ./tests/binance-ws-futures.test.ts --exit",
"ws-tests-migration": "mocha ./tests/ws-endpoints-migration.test.ts --fgrep \"Live:\" --invert --exit",
"ws-api-userdata-tests": "mocha ./tests/binance-ws-api-userdata.test.ts --exit",
"ws-live-tests": "mocha ./tests/binance-ws-spot.test.ts ./tests/binance-ws-futures.test.ts ./tests/binance-ws-api-userdata.test.ts ./tests/binance-ws-api-ticker.test.ts --exit",
"test-debug": "mocha --inspect-brk",
Expand Down
9 changes: 9 additions & 0 deletions src/node-binance-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,16 @@ export default class Binance {
const httpsproxy = this.getHttpsProxy();
let socksproxy = this.getSocksProxy();
const queryParams = streams.join('/');
// Binance routes USDⓈ-M futures streams to separate endpoints by category
// (/public, /market, /private) and will not push cross-category streams on a
// single connection. Reject mixed-category combos so they fail loudly instead
// of silently dropping data — subscribe to each category on its own connection.
const category = this.classifyFuturesStream(streams[0]);
const mismatch = streams.find(s => this.classifyFuturesStream(s) !== category);
if (mismatch !== undefined) {
const mismatchCategory = this.classifyFuturesStream(mismatch);
throw new Error(`futuresSubscribe: cannot combine '${category}' stream "${streams[0]}" with '${mismatchCategory}' stream "${mismatch}" on one connection. Binance routes futures streams to separate /public, /market and /private endpoints; subscribe to each category separately.`);
}
Comment thread
carlosmiei marked this conversation as resolved.
const baseUrl = this.getFStreamUrl(category);
let ws: any = undefined;
if (socksproxy) {
Expand Down
28 changes: 28 additions & 0 deletions tests/ws-endpoints-migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,34 @@ describe('Private stream URL uses query params for listenKey', function () {
});
});

describe('futuresSubscribe rejects mixed-category combined streams', function () {
const listenKey = 'pqia91ma19a5s61cv6a81va65sdf19v8a65a1a5s61cv6a81va65sdf19v8a1a65a1a5s61cv6a81va65sd';

it('throws when mixing market and public streams', function () {
assert.throws(
() => binance.futuresSubscribe(['btcusdt@aggTrade', 'btcusdt@depth'], () => { }),
/cannot combine/
);
});

it('throws when mixing public and private streams', function () {
assert.throws(
() => binance.futuresSubscribe(['btcusdt@bookTicker', listenKey], () => { }),
/cannot combine/
);
});

it('names both offending categories in the error message', function () {
try {
binance.futuresSubscribe(['btcusdt@markPrice', 'btcusdt@bookTicker'], () => { });
assert.fail('expected futuresSubscribe to throw');
} catch (e) {
assert.match(e.message, /market/);
assert.match(e.message, /public/);
}
});
});

describe('Live: production market stream (aggTrade via /market/)', function () {
let trade;
let cnt = 0;
Expand Down
Loading