UNPKG

ib-client

Version:

Interactive Brokers API client library for Node.js

88 lines (74 loc) 3.58 kB
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports["default"] = void 0;var _errors = require("./errors"); var _constants = require("./constants");function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}function _createClass(Constructor, protoProps, staticProps) {if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;}var BufferParser = /*#__PURE__*/function () { function BufferParser() {var buffer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];_classCallCheck(this, BufferParser); this._buffer = buffer; }_createClass(BufferParser, [{ key: "isEmpty", value: function isEmpty() { return this._buffer.length === 0; } }, { key: "read", value: function read() { if (this.isEmpty()) throw new _errors.UnderrunError(); return this._buffer.shift(); } }, { key: "readBool", value: function readBool() { return !!parseInt(this.readString(), 10); } }, { key: "readFloat", value: function readFloat() { return parseFloat(this.readString()); } }, { key: "readInt", value: function readInt() { return parseInt(this.readString(), 10); } }, { key: "readFloatMax", value: function readFloatMax() { var str = this.readString(); if (!str) return Number.MAX_VALUE;else parseInt(str, 10); } }, { key: "readIntMax", value: function readIntMax() { var str = this.readString(); if (!str) return 0x7fffffff;else parseInt(str, 10); } }, { key: "readString", value: function readString() { var str = ''; while (!this.isEmpty()) { var element = String.fromCharCode(this.read().toString()); if (element === _constants.EOL) break; str = str.concat(element); } return str; } }, { key: "readLengthHeader", value: function readLengthHeader() { var mask = 0xffffffff; return ( (mask & this.read()) << 24 | (mask & this.read()) << 16 | (mask & this.read()) << 8 | mask & this.read()); } }, { key: "process", value: function process() {var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function (err, methodName) {}; try { var responseCode = this.readInt(); var responseFunctionName = this._responseCodeToString(responseCode); if (!responseFunctionName) { cb(new Error('Unknown response code ', responseCode), null); } else { return cb(null, responseFunctionName); } } catch (e) { throw e; } } }, { key: "_responseCodeToString", value: function _responseCodeToString( responseCode) { for (var key in _constants.INCOMING) { if (_constants.INCOMING[key] === responseCode) { return key; } } return false; } }, { key: "_restoreLastChunk", value: function _restoreLastChunk( chunk) { this._buffer = this._buffer.concat(chunk); } }]);return BufferParser;}();var _default = BufferParser;exports["default"] = _default;