UNPKG

@reactivemarkets/switchboard-sdk

Version:
150 lines (149 loc) 6.57 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MarketDataClient = void 0; const switchboard_api_1 = require("@reactivemarkets/switchboard-api"); const flatbuffers = __importStar(require("flatbuffers")); const reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket")); const tiny_typed_emitter_1 = require("tiny-typed-emitter"); const domain_1 = require("./domain"); class MarketDataClient extends tiny_typed_emitter_1.TypedEmitter { constructor(options) { super(); this.onClose = (event) => { this.emit("close", event.code, event.reason); }; this.onError = (event) => { this.emit("error", event.error); }; this.onOpen = () => { this.emit("open"); }; this.onMessage = (event) => { const byteBuffer = event.data; const bytes = new Uint8Array(byteBuffer); const buffer = new flatbuffers.ByteBuffer(bytes); const message = switchboard_api_1.Message.getRootAsMessage(buffer); const bodyType = message.bodyType(); switch (bodyType) { case switchboard_api_1.Body.Heartbeat: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.Heartbeat()); if (body !== null) { this.emit("heartbeat", body, { sendingTime }); } break; } case switchboard_api_1.Body.MarketDataRequestReject: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.MarketDataRequestReject()); if (body !== null) { this.emit("md-request-reject", body, { sendingTime }); } break; } case switchboard_api_1.Body.MarketDataSnapshot: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.MarketDataSnapshot()); if (body !== null) { this.emit("md-snapshot", body, { sendingTime }); } break; } case switchboard_api_1.Body.Reject: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.Reject()); if (body !== null) { this.emit("reject", body, { sendingTime }); } break; } case switchboard_api_1.Body.TestRequest: { const sendingTime = message.sendingTime(); const body = message.body(new switchboard_api_1.TestRequest()); if (body !== null) { this.emit("test-request", body, { sendingTime }); } break; } } }; const { apiKey = process.env.REACTIVE_SWITCHBOARD_API_KEY, apiUrl = "wss://md.switchboard.reactivemarkets.com/v1", minReconnectionDelay = 1000 + Math.random() * 1000, WebSocketCtor } = options, rest = __rest(options, ["apiKey", "apiUrl", "minReconnectionDelay", "WebSocketCtor"]); const url = `${apiUrl}?api_key=${apiKey}`; const protocol = "fbs"; this.websocket = new reconnecting_websocket_1.default(url, protocol, Object.assign({ WebSocket: WebSocketCtor, minReconnectionDelay }, rest)); this.websocket.binaryType = "arraybuffer"; this.websocket.onopen = this.onOpen; this.websocket.onclose = this.onClose; this.websocket.onerror = this.onError; this.websocket.onmessage = this.onMessage; } get isOpen() { return this.websocket.readyState === this.websocket.OPEN; } heartbeat(requestId) { const bytes = (0, domain_1.heartbeat)() .requestId(requestId) .build(); this.websocket.send(bytes); } subscribe(options) { const bytes = (0, domain_1.marketDataRequest)() .markets(options.markets) .requestId(options.requestId) .subscribe() .build(); this.websocket.send(bytes); } testRequest(requestId) { const bytes = (0, domain_1.testRequest)() .requestId(requestId) .build(); this.websocket.send(bytes); } unsubscribe(options) { const bytes = (0, domain_1.marketDataRequest)() .markets(options.markets) .requestId(options.requestId) .unsubscribe() .build(); this.websocket.send(bytes); } } exports.MarketDataClient = MarketDataClient;