nd-json-rpc-protocol
Version:
A protocol encapsulating json-rpc massages with new line delimited json attached data
205 lines (175 loc) • 6.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MethodNotFound = exports.JsonRpcError = exports.InvalidRequest = exports.InvalidParameters = exports.InvalidJson = exports.format = exports.parse = exports.NdJsonRpcError = undefined;
var _jsonRpcProtocol = require('json-rpc-protocol');
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;
}
});
var jsonRpcProtocol = _interopRequireWildcard(_jsonRpcProtocol);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _format = jsonRpcProtocol.format,
InvalidJson = jsonRpcProtocol.InvalidJson,
JsonRpcError = jsonRpcProtocol.JsonRpcError,
_parse = jsonRpcProtocol.parse;
var NdJsonRpcError = exports.NdJsonRpcError = function (_JsonRpcError) {
_inherits(NdJsonRpcError, _JsonRpcError);
function NdJsonRpcError() {
_classCallCheck(this, NdJsonRpcError);
return _possibleConstructorReturn(this, (NdJsonRpcError.__proto__ || Object.getPrototypeOf(NdJsonRpcError)).apply(this, arguments));
}
return 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 === undefined ? {} : _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 = void 0;
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 = void 0;
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
};