bitmart-api
Version:
Complete & robust Node.js SDK for BitMart's REST APIs and WebSockets, with TypeScript declarations.
124 lines • 4.07 kB
JavaScript
import WebSocket from 'isomorphic-ws';
/** Should be one WS key per unique URL */
export const WS_KEY_MAP = {
spotPublicV1: 'spotPublicV1',
spotPrivateV1: 'spotPrivateV1',
futuresPublicV1: 'futuresPublicV1',
futuresPrivateV1: 'futuresPrivateV1',
futuresPublicV2: 'futuresPublicV2',
futuresPrivateV2: 'futuresPrivateV2',
};
export const WS_BASE_URL_MAP = {
spotPublicV1: {
livenet: 'wss://ws-manager-compress.bitmart.com/api?protocol=1.1',
demo: undefined,
},
spotPrivateV1: {
livenet: 'wss://ws-manager-compress.bitmart.com/user?protocol=1.1',
demo: undefined,
},
futuresPublicV1: {
livenet: 'wss://openapi-ws.bitmart.com/api?protocol=1.1',
demo: undefined,
},
futuresPrivateV1: {
livenet: 'wss://openapi-ws.bitmart.com/user?protocol=1.1',
demo: undefined,
},
futuresPublicV2: {
livenet: 'wss://openapi-ws-v2.bitmart.com/api?protocol=1.1',
demo: 'wss://openapi-wsdemo-v2.bitmart.com/api?protocol=1.1',
},
futuresPrivateV2: {
livenet: 'wss://openapi-ws-v2.bitmart.com/user?protocol=1.1',
demo: 'wss://openapi-wsdemo-v2.bitmart.com/user?protocol=1.1',
},
};
export const WS_ERROR_ENUM = {
INVALID_ACCESS_KEY: 'todo:',
};
export function neverGuard(x, msg) {
return new Error(`Unhandled value exception "${x}", ${msg}`);
}
/**
* ws.terminate() is undefined in browsers.
* This only works in node.js, not in browsers.
* Does nothing if `ws` is undefined. Does nothing in browsers.
*/
export function safeTerminateWs(ws, fallbackToClose) {
if (!ws) {
return false;
}
if (typeof ws['terminate'] === 'function') {
ws.terminate();
return true;
}
else if (fallbackToClose) {
ws.close();
}
return false;
}
/**
* Users can conveniently pass topics as strings or objects (object has topic name + optional params).
*
* This method normalises topics into objects (object has topic name + optional params).
*/
export function getNormalisedTopicRequests(wsTopicRequests) {
const normalisedTopicRequests = [];
for (const wsTopicRequest of wsTopicRequests) {
// passed as string, convert to object
if (typeof wsTopicRequest === 'string') {
const topicRequest = {
topic: wsTopicRequest,
payload: undefined,
};
normalisedTopicRequests.push(topicRequest);
continue;
}
// already a normalised object, thanks to user
normalisedTopicRequests.push(wsTopicRequest);
}
return normalisedTopicRequests;
}
/**
* WebSocket.ping() is not available in browsers. This is a simple check used to
* disable heartbeats in browers, for exchanges that use native WebSocket ping/pong frames.
*/
export function isWSPingFrameAvailable() {
return typeof WebSocket.prototype['ping'] === 'function';
}
/**
* WebSocket.pong() is not available in browsers. This is a simple check used to
* disable heartbeats in browers, for exchanges that use native WebSocket ping/pong frames.
*/
export function isWSPongFrameAvailable() {
return typeof WebSocket.prototype['pong'] === 'function';
}
export async function decompressMessageEvent(event) {
const data = event.data;
if (typeof data === 'string') {
return { ...event, data };
}
const ds = new DecompressionStream('deflate-raw');
const dataStream = new Response(data).body;
let decompressedStream;
if (!dataStream) {
const uint8 = new Uint8Array(data);
const rs = new ReadableStream({
start(controller) {
controller.enqueue(uint8);
controller.close();
},
});
decompressedStream = rs.pipeThrough(ds);
}
else {
decompressedStream = dataStream.pipeThrough(ds);
}
return {
...event,
type: 'message',
data: await new Response(decompressedStream).text(),
};
}
//# sourceMappingURL=websocket-util.js.map