Skip to content
Merged
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
40 changes: 33 additions & 7 deletions src/node-binance-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,14 +656,19 @@ export default class Binance {
*/
async signedRequest(url: string, data: Dict = {}, method: HttpMethod = 'GET', noDataInSignature = false) {
this.requireApiSecret('signedRequest');
const isListenKeyEndpoint = url.includes('v3/userDataStream');
Copy link

Copilot AI Sep 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded URL path detection is fragile. Consider using a more robust method like checking against a constant or using URL parsing to identify listen key endpoints.

Copilot uses AI. Check for mistakes.

data.timestamp = new Date().getTime();
if (this.timeOffset) data.timestamp += this.timeOffset;

if (!data.recvWindow) data.recvWindow = this.Options.recvWindow;
const query = method === 'POST' && noDataInSignature ? '' : this.makeQueryString(data);

const signature = this.generateSignature(query);
let signature = undefined;
Copy link

Copilot AI Sep 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Initialize signature as undefined is redundant since variables declared with let without initialization are automatically undefined. Consider declaring it only when needed or initialize with a more meaningful default.

Suggested change
let signature = undefined;
let signature;

Copilot uses AI. Check for mistakes.
if (!noDataInSignature && !isListenKeyEndpoint) {
data.timestamp = new Date().getTime();

if (this.timeOffset) data.timestamp += this.timeOffset;

if (!data.recvWindow) data.recvWindow = this.Options.recvWindow;
signature = this.generateSignature(query);
}

if (method === 'POST') {
const opt = this.reqObjPOST(
Expand All @@ -672,12 +677,17 @@ export default class Binance {
method,
this.APIKEY
);
opt.form.signature = signature;
if (signature) {
opt.form.signature = signature;
}
const reqPost = await this.proxyRequest(opt);
return reqPost;
} else {
let encodedUrl = url;
if (query) encodedUrl += '?' + query;
if (signature) encodedUrl += '&signature=' + signature;
const opt = this.reqObj(
url + '?' + query + '&signature=' + signature,
encodedUrl,
data,
method,
this.APIKEY
Expand Down Expand Up @@ -3917,6 +3927,22 @@ export default class Binance {
return res;
}

async spotGetDataStream(params: Dict = {}) {
return await this.privateSpotRequest('v3/userDataStream', params, 'POST', true);
}

async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'PUT');
}

async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'DELETE');
Comment on lines +3934 to +3943
Copy link

Copilot AI Sep 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling logic for missing listenKey is duplicated across both methods. Consider extracting this validation into a private helper method to reduce code duplication.

Suggested change
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'PUT');
}
async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'DELETE');
/**
* Helper to validate and retrieve a listenKey from argument or options.
* Throws if neither is present.
*/
private _getValidListenKey(listenKey?: string): string {
const key = listenKey || this.Options.listenKey;
if (!key) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return key;
}
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
const validListenKey = this._getValidListenKey(listenKey);
return await this.privateSpotRequest('v3/userDataStream', { listenKey: validListenKey, ...params }, 'PUT');
}
async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
const validListenKey = this._getValidListenKey(listenKey);
return await this.privateSpotRequest('v3/userDataStream', { listenKey: validListenKey, ...params }, 'DELETE');

Copilot uses AI. Check for mistakes.
Comment on lines +3934 to +3943
Copy link

Copilot AI Sep 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling logic for missing listenKey is duplicated across both methods. Consider extracting this validation into a private helper method to reduce code duplication.

Suggested change
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'PUT');
}
async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
listenKey = listenKey || this.Options.listenKey;
if (!listenKey) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return await this.privateSpotRequest('v3/userDataStream', { listenKey, ...params }, 'DELETE');
/**
* Helper to get listenKey or throw error if missing
*/
private _getListenKeyOrThrow(listenKey?: string): string {
const key = listenKey || this.Options.listenKey;
if (!key) throw new Error('A listenKey is required, either as an argument or in this.Options.listenKey');
return key;
}
async spotKeepDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
const key = this._getListenKeyOrThrow(listenKey);
return await this.privateSpotRequest('v3/userDataStream', { listenKey: key, ...params }, 'PUT');
}
async spotCloseDataStream(listenKey: string | undefined = undefined, params: Dict = {}) {
const key = this._getListenKeyOrThrow(listenKey);
return await this.privateSpotRequest('v3/userDataStream', { listenKey: key, ...params }, 'DELETE');

Copilot uses AI. Check for mistakes.
}

// /**
// * Queries the public api
// * @param {string} url - the public api endpoint
Expand Down
Loading