Skip to content

Commit

Permalink
Merge 9c0833c into 54abc8b
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Aug 4, 2020
2 parents 54abc8b + 9c0833c commit 6187f0e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 41 additions & 23 deletions packages/web3-core-requestmanager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6187f0e

Please sign in to comment.