UNPKG

@vates/nd-json-rpc-protocol

Version:

A protocol encapsulating json-rpc massages with new line delimited json attached data

189 lines (187 loc) 8.28 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "InvalidJson", { enumerable: true, get: function get() { return jsonRpcProtocol.InvalidJson; } }); Object.defineProperty(exports, "InvalidParameters", { enumerable: true, get: function get() { return jsonRpcProtocol.InvalidParameters; } }); Object.defineProperty(exports, "InvalidRequest", { enumerable: true, get: function get() { return jsonRpcProtocol.InvalidRequest; } }); Object.defineProperty(exports, "JsonRpcError", { enumerable: true, get: function get() { return jsonRpcProtocol.JsonRpcError; } }); Object.defineProperty(exports, "MethodNotFound", { enumerable: true, get: function get() { return jsonRpcProtocol.MethodNotFound; } }); exports.parse = exports.format = exports.NdJsonRpcError = void 0; var jsonRpcProtocol = _interopRequireWildcard(require("json-rpc-protocol")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } var _format = jsonRpcProtocol.format, InvalidJson = jsonRpcProtocol.InvalidJson, JsonRpcError = jsonRpcProtocol.JsonRpcError, _parse = jsonRpcProtocol.parse; var NdJsonRpcError = exports.NdJsonRpcError = /*#__PURE__*/function (_JsonRpcError) { function NdJsonRpcError() { _classCallCheck(this, NdJsonRpcError); return _callSuper(this, NdJsonRpcError, arguments); } _inherits(NdJsonRpcError, _JsonRpcError); return _createClass(NdJsonRpcError); }(JsonRpcError); var _do = function _do(_ref) { var _f = _ref._f, method = _ref.method, error = _ref.error, id = _ref.id, data = _ref.data, _ref$params = _ref.params, params = _ref$params === void 0 ? {} : _ref$params; if (!Array.isArray(data)) { throw new NdJsonRpcError("data is expected as an Array"); } if (params.size !== undefined) { throw new NdJsonRpcError("size is a reserved nd-json-rpc response property"); } params.size = data.length; var firstChunk; switch (_f) { case "notification": firstChunk = _format.notification(method, params); break; case "request": firstChunk = _format.request(id, method, params); break; case "response": firstChunk = _format.response(id, params); break; case "error": firstChunk = _format.error(id, error); break; default: throw new Error("Unknown or unhandled jsonrpc message format"); } var output = firstChunk; data.forEach(function (element) { output = output.concat("\n"); output = output.concat(JSON.stringify(element)); }); return output; }; var notification = function notification(method, params) { var data = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; return _do({ _f: "notification", method: method, params: params, data: data }); }; var request = function request(id, method, params, data) { return _do({ _f: "request", id: id, method: method, params: params, data: data }); }; var response = function response(id, params) { var data = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; return _do({ _f: "response", id: id, params: params, data: data }); }; var error = function error(id, _error) { return _do({ _f: "error", id: id, error: _error }); }; /* * returns { jsonrpcMessage, data} * * where `jsonrpcMessage` is a json-rpc message object, and `data` an array * * `jsonrpcMessage` can be a json-rpc error * `data` can be undefined * * If `data` is defined, `jsonrpc.size` will give its number of elements */ var parse = exports.parse = function parse(response) { response = response.split("\n"); var firstChunk; try { firstChunk = _parse(response.shift()); } catch (error) { if (error instanceof InvalidJson) { throw new NdJsonRpcError("First chunk is not a well-formed json-rpc response", error.code); } throw error; } var _firstChunk = firstChunk, error = _firstChunk.error; if (error !== undefined) { return { jsonrpcMessage: firstChunk }; } if (firstChunk.result.size !== response.length) { throw new NdJsonRpcError("Data does not match its given size"); } try { response = response.map(function (chunk) { return JSON.parse(chunk); }); } catch (error) { if (error instanceof SyntaxError) { throw new NdJsonRpcError("Some data items are not well-formed JSON"); } } return { jsonrpcMessage: firstChunk, data: response }; }; var format = exports.format = { error: error, notification: notification, request: request, response: response };