UNPKG

jaysonic

Version:

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

169 lines (133 loc) 7.8 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 _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 _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } 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 JsonRpcServerProtocol = require("./base"); var _require = require("../../util/constants"), errorToStatus = _require.errorToStatus; /** * Creates instance of HttpServerProtocol * @extends JsonRpcServerProtocol * */ var HttpServerProtocol = /*#__PURE__*/function (_JsonRpcServerProtoco) { _inherits(HttpServerProtocol, _JsonRpcServerProtoco); var _super = _createSuper(HttpServerProtocol); /** * As well as all the params and properties in [JsonRpcServerProtocol]{@link JsonRpcServerProtocol} * the following properties are available. * * @property {class} response HTTP response object * @property {object} headers={"Content-Type":"application/json"} HTTP response headers */ function HttpServerProtocol(factory, client, response, version, delimiter) { var _this; _classCallCheck(this, HttpServerProtocol); _this = _super.call(this, factory, client, version, delimiter); _this.response = response; _this.headers = { "Content-Type": "application/json" }; _this.status = null; return _this; } /** * Send message to the client. * * @param {string} message Stringified JSON-RPC message object */ _createClass(HttpServerProtocol, [{ key: "writeToClient", value: function writeToClient(message) { var json = JSON.parse(message); if (!this.status) { // set the status code if it has not been overwritten if (json.error) { this.setResponseStatus({ errorCode: json.error.code }); } else { this.status = 200; } } this.response.writeHead(this.status, this.headers).end(message); } /** * Calls `emit` on factory with the event name being `message.method` and * the data being `message`. * * Responds to client with 204 status. * * @param {object} message JSON-RPC message object */ }, { key: "gotNotification", value: function gotNotification(message) { _get(_getPrototypeOf(HttpServerProtocol.prototype), "gotNotification", this).call(this, message.method, message); this.setResponseStatus({ notification: true }); this.response.writeHead(this.status, this.headers).end(); } /** * Registers the `event` data listener when client connects. * * Pushes received data into `messageBuffer` and calls * [_waitForData]{@link JsonRpcServerProtocol#_waitForData}. * * The HTTP server does not use the delimiter since the completion of a request indicates * the end of data. * * @extends JsonRpcServerProtocol.clientConnected */ }, { key: "clientConnected", value: function clientConnected() { var _this2 = this; this.client.on(this.event, function (chunk) { _this2.messageBuffer.push(chunk); }); this.client.on("end", function () { _this2._validateData(_this2.messageBuffer.emptyBuffer()); _this2.factory._removeFromArray(_this2, _this2.factory.pendingRequests); }); } /** * Set response status code * * @param {object} options * @param {number} options.errorCode The JSON-RPC error code to lookup a corresponding status code for * @param {number} options.status The HTTP status code (will override the errorCode) * @param {boolean} options.notification Inidicate if setting header for notification (will override other options with 204 status) */ }, { key: "setResponseStatus", value: function setResponseStatus(_ref) { var errorCode = _ref.errorCode, status = _ref.status, notification = _ref.notification; this.status = 200; if (errorCode) { this.status = errorToStatus[String(errorCode)]; } if (status) { this.status = status; } if (notification) { this.status = 204; // notification responses must be 204 per spec, so no point in allowing an override } } }]); return HttpServerProtocol; }(JsonRpcServerProtocol); module.exports = HttpServerProtocol;