UNPKG

@hms-networks/kolibri-js-core

Version:

Kolibri Core library containing common kolibri models and utils

123 lines 3.96 kB
"use strict"; /* * Copyright 2021 HMS Industrial Networks AB * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http: //www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = exports.buildError = exports.buildResult = exports.buildRequest = exports.getType = exports.type = void 0; //-------------------------------------------------------------------------- // JSON-RPC types //-------------------------------------------------------------------------- exports.type = { REQUEST: 1, NOTIFICATION: 2, RESULT: 3, ERROR: 4, REQUEST_ROUTED: 5, NOTIFICATION_ROUTED: 6, RESULT_ROUTED: 7, ERROR_ROUTED: 8, INVALID: 9 }; //-------------------------------------------------------------------------- // Function: getType(rpc) //-------------------------------------------------------------------------- function getType(rpc) { // JSON-RPC 2.0 if (typeof rpc !== 'object' || !rpc.hasOwnProperty('jsonrpc') || rpc.jsonrpc !== '2.0') { return exports.type.INVALID; } // Request or notification if (rpc.hasOwnProperty('method')) { if (rpc.hasOwnProperty('result') || rpc.hasOwnProperty('error')) { return exports.type.INVALID; } if (rpc.hasOwnProperty('params') && typeof rpc.params !== 'object') { return exports.type.INVALID; } if (rpc.hasOwnProperty('_server')) { return rpc.hasOwnProperty('id') ? exports.type.REQUEST_ROUTED : exports.type.NOTIFICATION_ROUTED; } else { return rpc.hasOwnProperty('id') ? exports.type.REQUEST : exports.type.NOTIFICATION; } } // Successful response if (rpc.hasOwnProperty('result')) { if (rpc.hasOwnProperty('error')) { return exports.type.INVALID; } return rpc.hasOwnProperty('_server') ? exports.type.RESULT_ROUTED : exports.type.RESULT; } // Error response if (rpc.hasOwnProperty('error')) { if (typeof rpc.error !== 'object' || !rpc.error.hasOwnProperty('code') || !rpc.error.hasOwnProperty('message')) { return exports.type.INVALID; } return rpc.hasOwnProperty('_server') ? exports.type.ERROR_ROUTED : exports.type.ERROR; } return exports.type.INVALID; } exports.getType = getType; function buildRequest(method, id, params, server) { let rpc = { jsonrpc: '2.0', method: method, id: id }; if (server) { rpc._server = server; } if (params) { rpc.params = params; } return rpc; } exports.buildRequest = buildRequest; function buildResult(id, result, server) { let rpc = { jsonrpc: '2.0', id: id, result: typeof result === 'undefined' ? null : result }; if (server) { rpc._server = server; } return rpc; } exports.buildResult = buildResult; function buildError(id, error, server) { let rpc = { jsonrpc: '2.0', id: id, error: error }; if (server) { rpc._server = server; } return rpc; } exports.buildError = buildError; //-------------------------------------------------------------------------- // Function: parse(data) //-------------------------------------------------------------------------- function parse(data) { return JSON.parse(String(data)); } exports.parse = parse; //# sourceMappingURL=jsonrpc.js.map