-
Notifications
You must be signed in to change notification settings - Fork 5.1k
web3-providers-http: Migrate from xhr2-cookies to cross-fetch #5085
Conversation
Pull Request Test Coverage Report for Build 2495828271
💛 - 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'); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); | ||
}); | ||
}; |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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
test/httpprovider.js
Outdated
var agent = new https.Agent(); | ||
var options = {agent: {https: agent}}; | ||
var provider = new HttpProvider('http://localhost:8545', options); | ||
var result = await provider._prepareRequest(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 Resolved those three comments |
There was a problem hiding this 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)
This pull request introduces 1 alert when merging f0f1fa0 into 2c0324a - view on LGTM.com new alerts:
|
@jdevcs @avkos @luu-alex @spacesailor24 @nazarhussain @nikoulai I have fixed the testcase, would like to have a look at? |
This pull request introduces 1 alert when merging 4a9235f into 2c0324a - view on LGTM.com new alerts:
|
There was a problem hiding this 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.
- 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 inpackage-lock.json
. - Add an entry to
CHANGELOG.md
describing what you are changing in this PR.
test/httpprovider.js
Outdated
var agent = new https.Agent(); | ||
var options = {agent: {https: agent}}; | ||
var provider = new HttpProvider('http://localhost:8545', options); | ||
var result = await provider._prepareRequest(); |
There was a problem hiding this comment.
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 @nikoulai @spacesailor24 Would like to ask your thought about removing |
@nazarhussain @nikoulai @spacesailor24 Addressed comments |
There was a problem hiding this 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.
@nazarhussain Oh, sorry fixed. |
@ayanamidev Please resolve the conflicts. Those probably will be in CHANGELOG.md. Then I would be able to merge the PR right away. |
@nazarhussain Done. Every conflicts resolved without changes from source code |
* 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>
closes #4868
I have replaced the old
xhr2-cookies
library tocross-fetch
since it was better, and thenpm i
forxhr2-cookies
was broken for the latest node LTS version as well.Working perfectly like before.