jaysonic
Version:
A feature rich JSON-RPC 1.0/2.0 compliant client and server library
251 lines (206 loc) • 10.7 kB
JavaScript
;
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 http = require("http");
var https = require("https");
var JsonRpcClientProtocol = require("./base");
var logging = require("../../util/logger");
/**
* Creates an instance of HttpClientProtocol, which has some tweaks from the base class
* required to work with the `node.http` package
*
* @extends JsonRpcClientProtocol
* @requires http
* @requires https
*/
var HttpClientProtocol = /*#__PURE__*/function (_JsonRpcClientProtoco) {
_inherits(HttpClientProtocol, _JsonRpcClientProtoco);
var _super = _createSuper(HttpClientProtocol);
/**
* In addition to the params and properties of [JsonRpcClientProtocol]{@link JsonRpcClientProtocol},
* the HttpClientProtocol has the following properties
*
* @property {object} headers HTTP headers passed to the factory instance
* @property {string} encoding Encoding type passed to the factory instance
* @property {('http'|'https')} scheme The scheme to use to connect to the server
*/
function HttpClientProtocol(factory, version, delimiter) {
var _this;
_classCallCheck(this, HttpClientProtocol);
_this = _super.call(this, factory, version, delimiter);
_this.headers = _this.factory.headers;
_this.encoding = _this.factory.encoding;
_this.scheme = _this.factory.scheme;
return _this;
}
/**
* Send a message to the server. Sets the request headers passed into `headers`
*
* Calls [listen]{@link JsonRpcClientProtocol#listen} to start listening for recieved data from server.
*
* Ends connection when all data received from the server.
*
* Emits a `serverDisconnected` event when connection is closed.
*
* Throws an error if there was an `error` event received when sending the request
*
* @param {string} request Stringified JSON-RPC message object
* @param {function=} cb Callback function to be called when message has been sent
*/
_createClass(HttpClientProtocol, [{
key: "write",
value: function write(request, cb) {
var _this2 = this;
var options = _objectSpread(_objectSpread({}, this.factory.options), this.headers);
this.headers["Content-Length"] = Buffer.byteLength(request, this.encoding);
var responseCallback = function responseCallback(response) {
if (cb) {
response.on("end", cb);
}
_this2.listener = response;
_this2.listen();
};
if (this.scheme === "http") {
this.connector = http.request(options, responseCallback);
} else if (this.scheme === "https") {
this.connector = https.request(options, responseCallback);
} else {
throw Error("Invalid scheme");
}
this.connector.on("close", function () {
_this2.factory.emit("serverDisconnected");
});
this.connector.on("error", function (error) {
throw error;
});
this.connector.write(request, this.encoding);
this.connector.end();
}
/**
* Setup `this.listener.on("data")` event to listen for data coming into the client.
*
* The HTTP client does not use the delimiter since the completion of a response indicates
* the end of data.
*
* Calls [_waitForData]{@link JsonRpcClientProtocol#_waitForData}
*/
}, {
key: "listen",
value: function listen() {
var _this3 = this;
this.listener.on("data", function (chunk) {
_this3.messageBuffer.push(chunk);
});
this.listener.on("end", function () {
if (_this3.messageBuffer.buffer !== "") {
_this3._waitForData(_this3.messageBuffer.emptyBuffer());
}
});
}
/**
* Pass incoming data to [verifyData]{@link JsonRpcClientProtocol#verifyData}
*
* @private
*
*/
}, {
key: "_waitForData",
value: function _waitForData(data) {
try {
this.verifyData(data);
} catch (e) {
this.gotError(e);
}
}
/**
* Send a notification to the server.
*
* Promise will resolve if the request was sucessfully sent, and reject if
* there was an error sending the request. For the [HttpClientProtocol]{@link HttpClientProtocol}, the resolved promise
* will return the http response object with a `204` response code per the spec.
*
* @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) {
var _this4 = this;
return new Promise(function (resolve, reject) {
var request = _this4.message(method, params, false);
try {
_this4.write(request, function () {
if (_this4.listener.statusCode === 204) {
resolve({
body: _this4.listener.body || null,
headers: _objectSpread({}, _this4.listener.headers),
statusCode: _this4.listener.statusCode
});
} else {
reject(new Error("no response receieved for notification"));
}
});
} catch (e) {
// this.connector is probably undefined
reject(e);
}
});
}
/** @inheritdoc */
}, {
key: "getResponse",
value: function getResponse(id) {
return {
body: this.responseQueue[id],
headers: _objectSpread({}, this.listener.headers),
statusCode: this.listener.statusCode
};
}
/** @inheritdoc */
}, {
key: "getBatchResponse",
value: function getBatchResponse(batch) {
return {
body: batch,
headers: _objectSpread({}, this.listener.headers),
statusCode: this.listener.statusCode
};
}
/** @inheritdoc */
}, {
key: "rejectPendingCalls",
value: function rejectPendingCalls(error) {
var err = {
body: error,
headers: _objectSpread({}, this.listener.headers),
statusCode: this.listener.statusCode
};
try {
this.pendingCalls[err.body.id].reject(err);
this.factory.cleanUp(err.body.id);
} catch (e) {
if (e instanceof TypeError) {
// probably a parse error, which might not have an id
logging.getLogger().error("Message has no outstanding calls: ".concat(JSON.stringify(err.body)));
}
}
}
}]);
return HttpClientProtocol;
}(JsonRpcClientProtocol);
module.exports = HttpClientProtocol;