UNPKG

bitmart-api

Version:

Complete & robust Node.js SDK for BitMart's REST APIs and WebSockets, with TypeScript declarations.

136 lines 4.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WS_ERROR_ENUM = exports.WS_BASE_URL_MAP = exports.WS_KEY_MAP = void 0; exports.neverGuard = neverGuard; exports.safeTerminateWs = safeTerminateWs; exports.getNormalisedTopicRequests = getNormalisedTopicRequests; exports.isWSPingFrameAvailable = isWSPingFrameAvailable; exports.isWSPongFrameAvailable = isWSPongFrameAvailable; exports.decompressMessageEvent = decompressMessageEvent; const isomorphic_ws_1 = __importDefault(require("isomorphic-ws")); /** Should be one WS key per unique URL */ exports.WS_KEY_MAP = { spotPublicV1: 'spotPublicV1', spotPrivateV1: 'spotPrivateV1', futuresPublicV1: 'futuresPublicV1', futuresPrivateV1: 'futuresPrivateV1', futuresPublicV2: 'futuresPublicV2', futuresPrivateV2: 'futuresPrivateV2', }; exports.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', }, }; exports.WS_ERROR_ENUM = { INVALID_ACCESS_KEY: 'todo:', }; 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. */ 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). */ 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. */ function isWSPingFrameAvailable() { return typeof isomorphic_ws_1.default.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. */ function isWSPongFrameAvailable() { return typeof isomorphic_ws_1.default.prototype['pong'] === 'function'; } 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