Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

replace xhr,@cypress/request with fetch,node-fetch #461

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"ganache-core/**/elliptic": "^6.5.2"
},
"dependencies": {
"@cypress/request": "^3.0.0",
"@ethereumjs/statemanager": "^1.1.0",
"@ethereumjs/block": "^4.3.0",
"@ethereumjs/tx": "^4.2.0",
Expand All @@ -44,11 +43,11 @@
"clone": "^2.0.0",
"eth-block-tracker": "^8.0.0",
"json-stable-stringify": "^1.0.1",
"node-fetch": "^2.7.0",
"promise-to-callback": "^1.0.0",
"readable-stream": "^3.6.2",
"semaphore": "^1.0.3",
"ws": "^7.5.9",
"xhr": "^2.2.0",
"xtend": "^4.0.1"
},
"devDependencies": {
Expand Down
38 changes: 20 additions & 18 deletions subproviders/etherscan.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* - eth_listTransactions (non-standard)
*/

const xhr = process.browser ? require('xhr') : require('request')
const fetch = require('../util/fetch.js')
const inherits = require('util').inherits
const Subprovider = require('./subprovider.js')

Expand All @@ -38,18 +38,18 @@ function EtherscanProvider(opts) {
this.times = isNaN(opts.times) ? 4 : opts.times;
this.interval = isNaN(opts.interval) ? 1000 : opts.interval;
this.retryFailed = typeof opts.retryFailed === 'boolean' ? opts.retryFailed : true; // not built yet

setInterval(this.handleRequests, this.interval, this);
}

EtherscanProvider.prototype.handleRequests = function(self){
if(self.requests.length == 0) return;

//console.log('Handling the next ' + self.times + ' of ' + self.requests.length + ' requests');

for(var requestIndex = 0; requestIndex < self.times; requestIndex++) {
var requestItem = self.requests.shift()

if(typeof requestItem !== 'undefined')
handlePayload(requestItem.proto, requestItem.network, requestItem.payload, requestItem.next, requestItem.end)
}
Expand All @@ -58,15 +58,15 @@ EtherscanProvider.prototype.handleRequests = function(self){
EtherscanProvider.prototype.handleRequest = function(payload, next, end){
var requestObject = {proto: this.proto, network: this.network, payload: payload, next: next, end: end},
self = this;

if(this.retryFailed)
requestObject.end = function(err, result){
if(err === '403 - Forbidden: Access is denied.')
self.requests.push(requestObject);
else
end(err, result);
};

this.requests.push(requestObject);
}

Expand Down Expand Up @@ -189,32 +189,34 @@ function toQueryString(params) {

function etherscanXHR(useGetMethod, proto, network, module, action, params, end) {
var uri = proto + '://' + network + '.etherscan.io/api?' + toQueryString({ module: module, action: action }) + '&' + toQueryString(params)

xhr({
uri: uri,

fetch(uri, {
method: useGetMethod ? 'GET' : 'POST',
headers: {
'Accept': 'application/json',
// 'Content-Type': 'application/json',
},
rejectUnauthorized: false,
}, function(err, res, body) {
// console.log('[etherscan] response: ', err)
redirect: 'follow',
}).catch((err) => {
end(err)
}).then(resp => resp ? resp.body() : null)
.then(body => {

if (err) return end(err)

/*console.log('[etherscan request]'
if (!body) {
return
}
/*console.log('[etherscan request]'
+ ' method: ' + useGetMethod
+ ' proto: ' + proto
+ ' network: ' + network
+ ' module: ' + module
+ ' action: ' + action
+ ' params: ' + params
+ ' return body: ' + body);*/

if(body.indexOf('403 - Forbidden: Access is denied.') > -1)
return end('403 - Forbidden: Access is denied.')

var data
try {
data = JSON.parse(body)
Expand Down
30 changes: 12 additions & 18 deletions subproviders/rpc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const xhr = process.browser ? require('xhr') : require('request')
const fetch = require('../util/fetch.js')
const inherits = require('util').inherits
const createPayload = require('../util/create-payload.js')
const Subprovider = require('./subprovider.js')
Expand All @@ -22,21 +22,18 @@ RpcSource.prototype.handleRequest = function(payload, next, end){
const sanitizedPayload = sanitizePayload(payload)
const newPayload = createPayload(sanitizedPayload)

xhr({
uri: targetUrl,
fetch(targetUrl, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(newPayload),
rejectUnauthorized: false,
timeout: 20000,
}, function(err, res, body) {
if (err) return end(serializeError(err))

}).catch((err) => {
end(serializeError(err))
}).then(res => {
// check for error code
switch (res.statusCode) {
switch (res.status) {
case 405:
return end(rpcErrors.methodNotFound())
case 504: // Gateway timeout
Expand All @@ -58,15 +55,12 @@ RpcSource.prototype.handleRequest = function(payload, next, end){
return end(serializeError(err))
}
}

// parse response
let data
try {
data = JSON.parse(body)
} catch (err) {
console.error(err.stack)
return end(serializeError(err))
}
return res.body()
}).catch(err => {
console.error(err.stack)
end(serializeError(err))
}).then(data => {
if (!data) return
if (data.error) return end(data.error)

end(null, data.result)
Expand Down
12 changes: 12 additions & 0 deletions util/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (process.browser) {
module.exports = window.fetch;
} else {
/* eslint-disable no-undef */
if (typeof globalThis !== 'undefined' && globalThis.fetch) {
module.exports = globalThis.fetch;
} else if (typeof global !== 'undefined' && global.fetch) {
module.exports = global.fetch;
} else {
module.exports = require('node-fetch');
}
}
94 changes: 10 additions & 84 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -950,30 +950,6 @@
"@babel/helper-validator-identifier" "^7.22.20"
to-fast-properties "^2.0.0"

"@cypress/request@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@cypress/request/-/request-3.0.0.tgz#7f58dfda087615ed4e6aab1b25fffe7630d6dd85"
integrity sha512-GKFCqwZwMYmL3IBoNeR2MM1SnxRIGERsQOTWeQKoYBt2JLqcqiy7JXqO894FLrpjZYqGxW92MNwRH2BN56obdQ==
dependencies:
aws-sign2 "~0.7.0"
aws4 "^1.8.0"
caseless "~0.12.0"
combined-stream "~1.0.6"
extend "~3.0.2"
forever-agent "~0.6.1"
form-data "~2.3.2"
http-signature "~1.3.6"
is-typedarray "~1.0.0"
isstream "~0.1.2"
json-stringify-safe "~5.0.1"
mime-types "~2.1.19"
performance-now "^2.1.0"
qs "~6.10.3"
safe-buffer "^5.1.2"
tough-cookie "^4.1.3"
tunnel-agent "^0.6.0"
uuid "^8.3.2"

"@ethereumjs/block@^4.3.0":
version "4.3.0"
resolved "https://registry.yarnpkg.com/@ethereumjs/block/-/block-4.3.0.tgz#2b8dd6a2e37af9fe0e30dcff80c71eec8d6eba81"
Expand Down Expand Up @@ -5223,15 +5199,6 @@ http-signature@~1.2.0:
jsprim "^1.2.2"
sshpk "^1.7.0"

http-signature@~1.3.6:
version "1.3.6"
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.3.6.tgz#cb6fbfdf86d1c974f343be94e87f7fc128662cf9"
integrity sha512-3adrsD6zqo4GsTqtO7FyrejHNv+NgiIfAfv68+jVlFmSr9OGy7zrxONceFRLKvnnZA5jbxQBX1u9PpB6Wi32Gw==
dependencies:
assert-plus "^1.0.0"
jsprim "^2.0.2"
sshpk "^1.14.1"

http2-wrapper@^1.0.0-beta.5.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/http2-wrapper/-/http2-wrapper-1.0.3.tgz#b8f55e0c1f25d4ebd08b3b0c2c079f9590800b3d"
Expand Down Expand Up @@ -5833,16 +5800,6 @@ jsprim@^1.2.2:
json-schema "0.4.0"
verror "1.10.0"

jsprim@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-2.0.2.tgz#77ca23dbcd4135cd364800d22ff82c2185803d4d"
integrity sha512-gqXddjPqQ6G40VdnI6T6yObEC+pDNvyP95wdQhkWkg7crHH3km5qP1FsOXEkzEQwnz6gz5qGTn1c2Y52wP3OyQ==
dependencies:
assert-plus "1.0.0"
extsprintf "1.3.0"
json-schema "0.4.0"
verror "1.10.0"

[email protected], keccak@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/keccak/-/keccak-3.0.1.tgz#ae30a0e94dbe43414f741375cff6d64c8bea0bff"
Expand Down Expand Up @@ -6555,6 +6512,13 @@ node-fetch@^2.6.7:
dependencies:
whatwg-url "^5.0.0"

node-fetch@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
dependencies:
whatwg-url "^5.0.0"

node-fetch@~1.7.1:
version "1.7.3"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
Expand Down Expand Up @@ -6942,7 +6906,7 @@ pseudomap@^1.0.1:
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
integrity sha512-b/YwNhb8lk1Zz2+bXXpS/LK9OisiZZ1SNsSLxN1x2OXVEhW2Ckr/7mWE5vrC1ZTiJlD9g19jWszTmJsB+oEpFQ==

psl@^1.1.28, psl@^1.1.33:
psl@^1.1.28:
version "1.9.0"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.9.0.tgz#d0df2a137f00794565fcaf3b2c00cd09f8d5a5a7"
integrity sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==
Expand Down Expand Up @@ -7035,7 +6999,7 @@ punycode@^2.1.0, punycode@^2.1.1:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

[email protected], qs@~6.10.3:
[email protected]:
version "6.10.4"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.10.4.tgz#6a3003755add91c0ec9eacdc5f878b034e73f9e7"
integrity sha512-OQiU+C+Ds5qiH91qh/mg0w+8nwQuLjM4F4M/PbmhDOoYehPh+Fb0bDjtR1sOvy7YKxvj28Y/M0PhP5uVX0kB+g==
Expand Down Expand Up @@ -7073,11 +7037,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=

querystringify@^2.1.1:
version "2.2.0"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6"
integrity sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==

queue-microtask@^1.2.2, queue-microtask@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"
Expand Down Expand Up @@ -7326,11 +7285,6 @@ request@^2.79.0, request@^2.85.0:
tunnel-agent "^0.6.0"
uuid "^3.3.2"

requires-port@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
integrity sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==

resolve-alpn@^1.0.0:
version "1.2.1"
resolved "https://registry.yarnpkg.com/resolve-alpn/-/resolve-alpn-1.2.1.tgz#b7adbdac3546aaaec20b45e7d8265927072726f9"
Expand Down Expand Up @@ -7780,7 +7734,7 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=

sshpk@^1.14.1, sshpk@^1.7.0:
sshpk@^1.7.0:
version "1.17.0"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.17.0.tgz#578082d92d4fe612b13007496e543fa0fbcbe4c5"
integrity sha512-/9HIEs1ZXGhSPE8X6Ccm7Nam1z8KcoCqPdI7ecm1N33EzAetWahvQWVqLZtaZQ+IDKX4IyA2o0gBzqIMkAagHQ==
Expand Down Expand Up @@ -8152,16 +8106,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.1.tgz#3be34321a88a820ed1bd80dfaa33e479fbb8dd35"
integrity sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==

tough-cookie@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-4.1.3.tgz#97b9adb0728b42280aa3d814b6b999b2ff0318bf"
integrity sha512-aX/y5pVRkfRnfmuX+OdbSdXvPe6ieKX/G2s7e98f4poJHnqH3281gDPm/metm6E/WRamfx7WC4HUqkWHfQHprw==
dependencies:
psl "^1.1.33"
punycode "^2.1.1"
universalify "^0.2.0"
url-parse "^1.5.3"

tough-cookie@~2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"
Expand Down Expand Up @@ -8355,11 +8299,6 @@ universalify@^0.1.0:
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==

universalify@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.2.0.tgz#6451760566fa857534745ab1dde952d1b1761be0"
integrity sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==

unorm@^1.3.3:
version "1.6.0"
resolved "https://registry.yarnpkg.com/unorm/-/unorm-1.6.0.tgz#029b289661fba714f1a9af439eb51d9b16c205af"
Expand Down Expand Up @@ -8405,14 +8344,6 @@ url-parse-lax@^3.0.0:
dependencies:
prepend-http "^2.0.0"

url-parse@^1.5.3:
version "1.5.10"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.10.tgz#9d3c2f736c1d75dd3bd2be507dcc111f1e2ea9c1"
integrity sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==
dependencies:
querystringify "^2.1.1"
requires-port "^1.0.0"

url-set-query@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/url-set-query/-/url-set-query-1.0.0.tgz#016e8cfd7c20ee05cafe7795e892bd0702faa339"
Expand Down Expand Up @@ -8488,11 +8419,6 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

v8-compile-cache@^2.0.3:
version "2.1.0"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.1.0.tgz#e14de37b31a6d194f5690d67efc4e7f6fc6ab30e"
Expand Down
Loading