json_rpc.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. /*
  3. This file is part of web3.js.
  4. web3.js is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. web3.js is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with web3.js. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.isBatchRequest = exports.toBatchPayload = exports.toPayload = exports.setRequestIdStart = exports.isBatchResponse = exports.isValidResponse = exports.validateResponse = exports.isSubscriptionResult = exports.isResponseWithNotification = exports.isResponseWithError = exports.isResponseWithResult = exports.isResponseRpcError = void 0;
  17. const web3_validator_1 = require("web3-validator");
  18. const web3_errors_1 = require("web3-errors");
  19. const uuid_js_1 = require("./uuid.js");
  20. // check if code is a valid rpc server error code
  21. const isResponseRpcError = (rpcError) => {
  22. const errorCode = rpcError.error.code;
  23. return web3_errors_1.rpcErrorsMap.has(errorCode) || (errorCode >= -32099 && errorCode <= -32000);
  24. };
  25. exports.isResponseRpcError = isResponseRpcError;
  26. const isResponseWithResult = (response) => !Array.isArray(response) &&
  27. !!response &&
  28. response.jsonrpc === '2.0' &&
  29. // JSON RPC consider "null" as valid response
  30. 'result' in response &&
  31. (0, web3_validator_1.isNullish)(response.error) &&
  32. (typeof response.id === 'number' || typeof response.id === 'string');
  33. exports.isResponseWithResult = isResponseWithResult;
  34. // To avoid circular package dependency, copied to code here. If you update this please update same function in `response_errors.ts`
  35. const isResponseWithError = (response) => !Array.isArray(response) &&
  36. response.jsonrpc === '2.0' &&
  37. !!response &&
  38. (0, web3_validator_1.isNullish)(response.result) &&
  39. // JSON RPC consider "null" as valid response
  40. 'error' in response &&
  41. (typeof response.id === 'number' || typeof response.id === 'string');
  42. exports.isResponseWithError = isResponseWithError;
  43. const isResponseWithNotification = (response) => !Array.isArray(response) &&
  44. !!response &&
  45. response.jsonrpc === '2.0' &&
  46. !(0, web3_validator_1.isNullish)(response.params) &&
  47. !(0, web3_validator_1.isNullish)(response.method);
  48. exports.isResponseWithNotification = isResponseWithNotification;
  49. const isSubscriptionResult = (response) => !Array.isArray(response) &&
  50. !!response &&
  51. response.jsonrpc === '2.0' &&
  52. 'id' in response &&
  53. // JSON RPC consider "null" as valid response
  54. 'result' in response;
  55. exports.isSubscriptionResult = isSubscriptionResult;
  56. const validateResponse = (response) => (0, exports.isResponseWithResult)(response) || (0, exports.isResponseWithError)(response);
  57. exports.validateResponse = validateResponse;
  58. const isValidResponse = (response) => Array.isArray(response) ? response.every(exports.validateResponse) : (0, exports.validateResponse)(response);
  59. exports.isValidResponse = isValidResponse;
  60. const isBatchResponse = (response) => Array.isArray(response) && response.length > 1 && (0, exports.isValidResponse)(response);
  61. exports.isBatchResponse = isBatchResponse;
  62. // internal optional variable to increment and use for the jsonrpc `id`
  63. let requestIdSeed;
  64. /**
  65. * Optionally use to make the jsonrpc `id` start from a specific number.
  66. * Without calling this function, the `id` will be filled with a Uuid.
  67. * But after this being called with a number, the `id` will be a number staring from the provided `start` variable.
  68. * However, if `undefined` was passed to this function, the `id` will be a Uuid again.
  69. * @param start - a number to start incrementing from.
  70. * Or `undefined` to use a new Uuid (this is the default behavior)
  71. */
  72. const setRequestIdStart = (start) => {
  73. requestIdSeed = start;
  74. };
  75. exports.setRequestIdStart = setRequestIdStart;
  76. const toPayload = (request) => {
  77. var _a, _b, _c, _d;
  78. if (typeof requestIdSeed !== 'undefined') {
  79. requestIdSeed += 1;
  80. }
  81. return {
  82. jsonrpc: (_a = request.jsonrpc) !== null && _a !== void 0 ? _a : '2.0',
  83. id: (_c = (_b = request.id) !== null && _b !== void 0 ? _b : requestIdSeed) !== null && _c !== void 0 ? _c : (0, uuid_js_1.uuidV4)(),
  84. method: request.method,
  85. params: (_d = request.params) !== null && _d !== void 0 ? _d : undefined,
  86. };
  87. };
  88. exports.toPayload = toPayload;
  89. const toBatchPayload = (requests) => requests.map(request => (0, exports.toPayload)(request));
  90. exports.toBatchPayload = toBatchPayload;
  91. const isBatchRequest = (request) => Array.isArray(request) && request.length > 1;
  92. exports.isBatchRequest = isBatchRequest;
  93. //# sourceMappingURL=json_rpc.js.map