UNPKG

jaysonic

Version:

A feature rich JSON-RPC 1.0/2.0 compliant client and server library

305 lines (261 loc) 12.2 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var EventEmitter = require("events"); var JsonRpcClientProtocol = require("./protocol/base"); /** * Creates an instance of JsonRpcClientFactory. This is the base factory which * all other factories inherit from. * * @extends EventEmitter * @requires events */ var JsonRpcClientFactory = /*#__PURE__*/function (_EventEmitter) { _inherits(JsonRpcClientFactory, _EventEmitter); var _super = _createSuper(JsonRpcClientFactory); /** * @inheritdoc * @param {Object} options Connection options for the factory class * @param {string} [options.host="127.0.0.1"] IP of server to connect to * @param {number} [options.port=8100] Port of server to connect to * @param {number} [options.version=2] JSON-RPC version to use (1|2) * @param {string} [options.delimiter="\n"] Delimiter to use for requests * @param {number} [options.timeout=30] Timeout for request response * @param {number} [options.connectionTimeout=5000] Timeout for connection to server * @param {number} [options.retries=2] Number of connection retry attempts * @property {Object} protocol Instance of [JsonRpcClientProtocol]{@link JsonRpcClientProtocol} to use for managing client connections * @property {class} pcolInstance The [JsonRpcClientProtocol]{@link JsonRpcClientProtocol} instance * @property {object} timeouts Key value pairs of request IDs to `setTimeout` instance * @property {number} requestTimeout Same as `options.timeout` * @property {number} remainingRetries Same as `options.retries` * @property {number} connectionTimeout Same as `options.connectionTimeout` * @property {object} server Object of host and port `{host: options.host, port: options.port}` */ function JsonRpcClientFactory(options) { var _this; _classCallCheck(this, JsonRpcClientFactory); _this = _super.call(this); if (!(_assertThisInitialized(_this) instanceof JsonRpcClientFactory)) { return _possibleConstructorReturn(_this, new JsonRpcClientFactory(options)); } var defaults = { host: "127.0.0.1", port: 8100, version: 2, delimiter: "\n", timeout: 30, connectionTimeout: 5000, retries: 2 }; _this.options = _objectSpread(_objectSpread({}, defaults), options || {}); _this.protocol = JsonRpcClientProtocol; _this.pcolInstance = undefined; _this.timeouts = {}; _this.requestTimeout = _this.options.timeout * 1000; _this.remainingRetries = _this.options.retries; _this.connectionTimeout = _this.options.connectionTimeout; _this.server = { host: _this.options.host, port: _this.options.port }; return _this; } /** * Set the `pcolInstance` for the client factory * * @abstract * @example * this.pcolInstance = new JsonRpcClientProtocol() */ _createClass(JsonRpcClientFactory, [{ key: "buildProtocol", value: function buildProtocol() { throw new Error("function must be overwritten in subclass"); } /** * Calls `buildProtocol` method. * * Calls `connect()` on protocol instance * */ }, { key: "connect", value: function connect() { if (this.pcolInstance) { // not having this caused MaxEventListeners error return Promise.reject(Error("client already connected")); } this.buildProtocol(); return this.pcolInstance.connect(); } /** * Calls `end()` on protocol instance * * @abstract */ }, { key: "end", value: function end() { throw new Error("function must be overwritten in subclass"); } /** * Subscribe the function to the given event name * * @param {string} method Method name to subscribe to * @param {function} cb Name of callback function to invoke on event * @abstract */ }, { key: "subscribe", value: function subscribe() { throw new Error("function must be overwritten in subsclass"); } /** * Unsubscribe the function from the given event name * * @param {string} method Method to unsubscribe from * @param {function} cb Name of function to remove * @abstract */ }, { key: "unsubscribe", value: function unsubscribe() { throw new Error("function must be overwritten in subsclass"); } /** * Unsubscribe all functions from given event name * * @param {string} method Method to unsubscribe all listeners from * @abstract */ }, { key: "unsubscribeAll", value: function unsubscribeAll() { throw new Error("function must be overwritten in subsclass"); } /** * Calls `message()` on the protocol instance * * @param {string} method Name of the method to use in the request * @param {Array|JSON} params Params to send * @param {boolean=} id If true it will use instances `message_id` for the request id, if false will generate a notification request * @example * client.message("hello", ["world"]) // returns {"jsonrpc": "2.0", "method": "hello", "params": ["world"], "id": 1} * client.message("hello", ["world"], false) // returns {"jsonrpc": "2.0", "method": "hello", "params": ["world"]} */ }, { key: "message", value: function message(method, params, id) { return this.pcolInstance.message(method, params, id); } /** * Calls `send()` method on protocol instance * * Promise will resolve when a response has been received for the request. * * Promise will reject if the server responds with an error object, or if * the response is not received within the set `requestTimeout` * * @param {string} method Name of the method to use in the request * @param {Array|JSON} params Params to send * @returns Promise * @example * client.send("hello", {"foo": "bar"}) */ }, { key: "send", value: function send(method, params) { return this.pcolInstance.send(method, params); } /** * Calls `notify()` method on protocol instance * * Promise will resolve if the request was sucessfully sent, and reject if * there was an error sending the request. * * @param {string} method Name of the method to use in the notification * @param {Array|JSON} params Params to send * @return Promise * @example * client.notify("hello", ["world"]) */ }, { key: "notify", value: function notify(method, params) { return this.pcolInstance.notify(method, params); } /** * Calls `request()` method on protocol instance * * Plans to deprecate this in future versions. */ }, { key: "request", value: function request() { return this.pcolInstance.request(); } /** * Calls `batch()` method on protocol instance * * @param {JSON[]} requests Valid JSON-RPC batch request array */ }, { key: "batch", value: function batch(requests) { return this.pcolInstance.batch(requests); } /** * Listens for a `serverDisconnected` event, passing the callback function * * @param {function} cb */ }, { key: "serverDisconnected", value: function serverDisconnected(cb) { this.on("serverDisconnected", cb); } /** * Clears pending timeouts kept in `timeouts` property for the provided request IDs. * * @param {string[]|number[]} ids Array of request IDs */ }, { key: "cleanUp", value: function cleanUp(ids) { clearTimeout(this.timeouts[ids]); delete this.timeouts[ids]; } }]); return JsonRpcClientFactory; }(EventEmitter); module.exports = JsonRpcClientFactory; /** * TCP client constructor * @type TcpClientFactory * @static */ JsonRpcClientFactory.tcp = require("./tcp"); /** * HTTP client constructor * @type HttpClientFactory * @static */ JsonRpcClientFactory.http = require("./http"); /** * WebSocket client constructor * @type WsClientFactory * @static */ JsonRpcClientFactory.ws = require("./ws");