Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3-providers-http: Migrate from xhr2-cookies to cross-fetch #5085

Merged
merged 9 commits into from Jun 28, 2022
Merged

Conversation

ghost
Copy link

@ghost ghost commented May 29, 2022

closes #4868

I have replaced the old xhr2-cookies library to cross-fetch since it was better, and the npm i for xhr2-cookies was broken for the latest node LTS version as well.

Working perfectly like before.

@coveralls
Copy link

coveralls commented May 29, 2022

Pull Request Test Coverage Report for Build 2495828271

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 21 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.2%) to 72.001%

Files with Coverage Reduction New Missed Lines %
packages/web3-providers-http/lib/index.js 21 64.15%
Totals Coverage Status
Change from base Build 2460883276: -0.2%
Covered Lines: 3383
Relevant Lines: 4424

💛 - Coveralls

var http = require('http');
var https = require('https');

// Apply missing polyfill for IE
require('es6-promise').polyfill();
require('abortcontroller-polyfill/dist/polyfill-patch-fetch');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the polyfills in case of compatibility, but I think CI makes some complain about it


/**
* HttpProvider should be used to send rpc calls over http
*/
var HttpProvider = function HttpProvider(host, options) {
options = options || {};

this.withCredentials = options.withCredentials || false;
this.withCredentials = options.withCredentials;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -30,7 +30,6 @@ export interface HttpHeader {
}

export interface HttpProviderAgent {
baseUrl?: string;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have removed it since this isn't supported by fetch as well as this is not a standard for Http Agent

return new Promise(function(resolve, reject) {
resolve(response);
});
};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple version of fetch-mock function added

});

describe('send', function () {
it('should send basic async request', function (done) {
var provider = new HttpProvider();

provider.send({}, function (err, result) {
assert.equal(typeof result, 'object');
assert.equal(typeof result, 'undefined');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be an object as we didn't had any url to fetch

packages/web3-providers-http/src/index.js Outdated Show resolved Hide resolved
packages/web3-providers-http/src/index.js Outdated Show resolved Hide resolved
packages/web3-providers-http/types/index.d.ts Outdated Show resolved Hide resolved
var agent = new https.Agent();
var options = {agent: {https: agent}};
var provider = new HttpProvider('http://localhost:8545', options);
var result = await provider._prepareRequest();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not _prepareRequest create only a function which unless invoked should not return a promise?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nazarhussain You mean we should return the function like () => fetch(this.host, options) instead of the promise itself?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the _prepareRequest should not be async. It should return the request object synchronously. And later on we should be using request.send() to actually send and await.

If that seems to be a big change, then we can rename the function to _prepareAndSendRequest that will make the context right.

@nazarhussain nazarhussain added 1.x 1.0 related issues Review Needed Maintainer(s) need to review labels Jun 1, 2022
@ghost
Copy link
Author

ghost commented Jun 1, 2022

@nazarhussain Resolved those three comments

@ghost ghost requested a review from nazarhussain June 1, 2022 15:11
Copy link
Contributor

@nikoulai nikoulai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the failing tests (except for e2e_windows)

@lgtm-com
Copy link

lgtm-com bot commented Jun 3, 2022

This pull request introduces 1 alert when merging f0f1fa0 into 2c0324a - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

@ghost ghost requested a review from nikoulai June 3, 2022 18:45
@ghost
Copy link
Author

ghost commented Jun 3, 2022

@jdevcs @avkos @luu-alex @spacesailor24 @nazarhussain @nikoulai I have fixed the testcase, would like to have a look at?

@lgtm-com
Copy link

lgtm-com bot commented Jun 3, 2022

This pull request introduces 1 alert when merging 4a9235f into 2c0324a - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

Copy link
Contributor

@nazarhussain nazarhussain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ayanamidev Thanks for your contributions. Left one small comment. Rest seems good to me.

There are two other points that need to address.

  1. We don't allow community contributions to change the package-lock.json file. So please remove the change from that file in your PR. Some team member will merge the PR and include the changes in package-lock.json.
  2. Add an entry to CHANGELOG.md describing what you are changing in this PR.

var agent = new https.Agent();
var options = {agent: {https: agent}};
var provider = new HttpProvider('http://localhost:8545', options);
var result = await provider._prepareRequest();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the _prepareRequest should not be async. It should return the request object synchronously. And later on we should be using request.send() to actually send and await.

If that seems to be a big change, then we can rename the function to _prepareAndSendRequest that will make the context right.

@ghost
Copy link
Author

ghost commented Jun 13, 2022

@nazarhussain @nikoulai @spacesailor24 Would like to ask your thought about removing _prepareRequest function for web3-providers-http. This function was xhr2-cookies specific and doesn't export it to the wild. So I think it makes sense to remove with the shift to cross-fetch since it will only return promise with the current code.

@ghost ghost requested a review from nazarhussain June 14, 2022 13:50
@ghost
Copy link
Author

ghost commented Jun 14, 2022

@nazarhussain @nikoulai @spacesailor24 Addressed comments

Copy link
Contributor

@nazarhussain nazarhussain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ayanamidev LGTM. Please add an entry into CHANGELOG.md describing the change.

@ghost
Copy link
Author

ghost commented Jun 14, 2022

@nazarhussain Oh, sorry fixed.

@ghost ghost requested a review from nazarhussain June 14, 2022 14:10
@nazarhussain nazarhussain changed the base branch from 1.x to nh/cross-fetch-update June 22, 2022 18:15
@nazarhussain
Copy link
Contributor

@ayanamidev Please resolve the conflicts. Those probably will be in CHANGELOG.md. Then I would be able to merge the PR right away.

@nazarhussain nazarhussain added Needs Clarification Requires additional input and removed Review Needed Maintainer(s) need to review labels Jun 22, 2022
@ghost
Copy link
Author

ghost commented Jun 23, 2022

@nazarhussain Done. Every conflicts resolved without changes from source code

@nazarhussain nazarhussain merged commit 258ea95 into web3:nh/cross-fetch-update Jun 28, 2022
nazarhussain added a commit that referenced this pull request Jul 1, 2022
* web3-providers-http: Migrate from xhr2-cookies to cross-fetch (#5085)

* Update CHANGELOG.md

* Migrate from xhr2-cookies to cross-fetch

* Address comments for web3-providers-http

* web3-providers-http: Prevent global leakage of this.connected

* Return server side error without creating additional error object

* Fixed type of http.Agent

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export

* Removed unused variable

* Remove unnecessary catch block

* Remove unnecessary internal _prepareRequest function from web3-providers-http

* ⬆️ Update package-lock.json

* Update packages/web3-providers-http/src/index.js

Co-authored-by: Junaid <86780488+jdevcs@users.noreply.github.com>

* ⚰️ Remove dead code

* 🎨 Improve the with credentials check

Co-authored-by: Ayanami <ayanami0330@protonmail.com>
Co-authored-by: Junaid <86780488+jdevcs@users.noreply.github.com>
@jdevcs jdevcs mentioned this pull request Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Needs Clarification Requires additional input
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants