From 80c2834b326dd39821d575ea6d303c3802aa6d55 Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Fri, 7 Aug 2020 08:55:00 -0700 Subject: [PATCH] Improve RequestManager send method (#3649) * improve send method --- CHANGELOG.md | 1 + .../web3-core-requestmanager/src/index.js | 64 ++++++++++++------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0938f9ca488..fa85c9c22dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -274,6 +274,7 @@ Released with 1.0.0-beta.37 code base. ### Changed +- Improve RequestManager send method (#3649) - `npm run build` now uses TSC to compile (.js allowed) and the build folder is now located under `lib` (#3652) - Modernized web3-core to use newer es syntax (#3652) - Bump lodash from 4.17.15 to 4.17.19 diff --git a/packages/web3-core-requestmanager/src/index.js b/packages/web3-core-requestmanager/src/index.js index 9b3c591d077..74f3a79092c 100644 --- a/packages/web3-core-requestmanager/src/index.js +++ b/packages/web3-core-requestmanager/src/index.js @@ -157,34 +157,19 @@ RequestManager.prototype.send = function (data, callback) { return callback(errors.InvalidProvider()); } - const payload = Jsonrpc.toPayload(data.method, data.params); + const { method, params } = data - const onJsonrpcResult = function (err, result) { - if(result && result.id && payload.id !== result.id) { - return callback(new Error(`Wrong response id ${result.id} (expected: ${payload.id}) in ${JSON.stringify(payload)}`)); - } - - if (err) { - return callback(err); - } - - if (result && result.error) { - return callback(errors.ErrorResponse(result)); - } - - if (!Jsonrpc.isValidResponse(result)) { - return callback(errors.InvalidResponse(result)); - } - - callback(null, result.result); - }; + const jsonrpcPayload = Jsonrpc.toPayload(method, params); + const jsonrpcResultCallback = this._jsonrpcResultCallback(callback, jsonrpcPayload) if (this.provider.request) { - callbackify(this.provider.request.bind(this.provider))(payload, callback); + const callbackRequest = callbackify(this.provider.request) + const requestArgs = { method, params } + callbackRequest(requestArgs, callback); } else if (this.provider.sendAsync) { - this.provider.sendAsync(payload, onJsonrpcResult); + this.provider.sendAsync(jsonrpcPayload, jsonrpcResultCallback); } else if (this.provider.send) { - this.provider.send(payload, onJsonrpcResult); + this.provider.send(jsonrpcPayload, jsonrpcResultCallback); } else { throw new Error('Provider does not have a request or send method to use.'); } @@ -315,6 +300,39 @@ RequestManager.prototype._isIpcCloseError = function (event) { return typeof event === 'boolean' && event; }; +/** + * The jsonrpc result callback for RequestManager.send + * + * @method _jsonrpcResultCallback + * + * @param {Function} callback the callback to use + * @param {Object} payload the jsonrpc payload + * + * @returns {Function} return callback of form (err, result) + * + */ +RequestManager.prototype._jsonrpcResultCallback = function (callback, payload) { + return function(err, result) { + if(result && result.id && payload.id !== result.id) { + return callback(new Error(`Wrong response id ${result.id} (expected: ${payload.id}) in ${JSON.stringify(payload)}`)); + } + + if (err) { + return callback(err); + } + + if (result && result.error) { + return callback(errors.ErrorResponse(result)); + } + + if (!Jsonrpc.isValidResponse(result)) { + return callback(errors.InvalidResponse(result)); + } + + callback(null, result.result); + } +}; + module.exports = { Manager: RequestManager, BatchManager: BatchManager