Merge pull request #241 from tiagosiebler/parallelsign

fix(#240): fix sign error on parallel requests due to pointer mutation
This commit is contained in:
Tiago
2023-03-21 12:25:58 +00:00
committed by GitHub
3 changed files with 28 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "bybit-api", "name": "bybit-api",
"version": "3.5.2", "version": "3.5.3",
"description": "Complete & robust Node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & strong end to end tests.", "description": "Complete & robust Node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",

View File

@@ -11,16 +11,19 @@ import {
} from './requestUtils'; } from './requestUtils';
import { signMessage } from './node-support'; import { signMessage } from './node-support';
// axios.interceptors.request.use((request) => {
// console.log(new Date(), 'Starting Request', JSON.stringify(request, null, 2));
// return request;
// });
if ( if (
typeof process === 'object' && typeof process === 'object' &&
typeof process.env === 'object' && typeof process.env === 'object' &&
process.env.BYBITTRACE process.env.BYBITTRACE
) { ) {
// axios.interceptors.request.use((request) => {
// console.log(
// new Date(),
// 'Starting Request',
// JSON.stringify(request, null, 2)
// );
// return request;
// });
axios.interceptors.response.use((response) => { axios.interceptors.response.use((response) => {
console.log(new Date(), 'Response:', { console.log(new Date(), 'Response:', {
request: { request: {
@@ -231,10 +234,6 @@ export default abstract class BaseRestClient {
// USDC endpoints, unified margin and a few others use a different way of authenticating requests (headers instead of params) // USDC endpoints, unified margin and a few others use a different way of authenticating requests (headers instead of params)
if (this.clientType === REST_CLIENT_TYPE_ENUM.v3) { if (this.clientType === REST_CLIENT_TYPE_ENUM.v3) {
if (!options.headers) {
options.headers = {};
}
const signResult = await this.prepareSignParams( const signResult = await this.prepareSignParams(
method, method,
'v5auth', 'v5auth',
@@ -242,21 +241,26 @@ export default abstract class BaseRestClient {
isPublicApi isPublicApi
); );
options.headers['X-BAPI-SIGN-TYPE'] = 2; const headers = {
options.headers['X-BAPI-API-KEY'] = this.key; 'X-BAPI-SIGN-TYPE': 2,
options.headers['X-BAPI-TIMESTAMP'] = signResult.timestamp; 'X-BAPI-API-KEY': this.key,
options.headers['X-BAPI-SIGN'] = signResult.sign; 'X-BAPI-TIMESTAMP': signResult.timestamp,
options.headers['X-BAPI-RECV-WINDOW'] = signResult.recvWindow; 'X-BAPI-SIGN': signResult.sign,
'X-BAPI-RECV-WINDOW': signResult.recvWindow,
...options.headers,
};
if (method === 'GET') { if (method === 'GET') {
return { return {
...options, ...options,
headers,
params: signResult.originalParams, params: signResult.originalParams,
}; };
} }
return { return {
...options, ...options,
headers,
data: signResult.originalParams, data: signResult.originalParams,
}; };
} }
@@ -391,10 +395,14 @@ export default abstract class BaseRestClient {
: JSON.stringify(res.originalParams); : JSON.stringify(res.originalParams);
const paramsStr = timestamp + key + recvWindow + signRequestParams; const paramsStr = timestamp + key + recvWindow + signRequestParams;
res.sign = await signMessage(paramsStr, this.secret); res.sign = await signMessage(paramsStr, this.secret);
res.serializedParams = signRequestParams; res.serializedParams = signRequestParams;
// console.log('sign req: ', paramsStr); // console.log('sign req: ', {
// req: paramsStr,
// sign: res.sign,
// });
return res; return res;
} }

View File

@@ -45,9 +45,10 @@ describe('Private Linear REST API GET Endpoints', () => {
}); });
it('getActiveOrderList()', async () => { it('getActiveOrderList()', async () => {
expect(await api.getActiveOrderList({ symbol: symbol })).toMatchObject( expect(await api.getActiveOrderList({ symbol: symbol })).toMatchObject({
successResponseObject() ...successResponseObject(),
); // ret_msg: 'ok',
});
}); });
it('queryActiveOrder()', async () => { it('queryActiveOrder()', async () => {