Skip to content

Commit

Permalink
Merge pull request #160 from AElfProject/feature/node-fetch
Browse files Browse the repository at this point in the history
feat: 🎸 node-fetch in node
  • Loading branch information
hzz780 authored Jun 20, 2024
2 parents 17feba2 + 65ac284 commit daf1677
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aelf-sdk",
"version": "3.4.12",
"version": "3.4.13-beta.0",
"description": "aelf-sdk js library",
"main": "dist/aelf.cjs.js",
"browser": "dist/aelf.umd.js",
Expand Down Expand Up @@ -56,6 +56,7 @@
"crypto-js": "^4.2.0",
"elliptic": "^6.4.1",
"hdkey": "^1.1.1",
"node-fetch": "2",
"query-string": "5.1.1",
"randombytes": "^2.1.0",
"scryptsy": "^2.1.0",
Expand Down
23 changes: 20 additions & 3 deletions src/util/httpProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const defaultHeaders = {
};

let RequestLibrary = {};
let RequestLibraryXMLOnly = null;
let isFetch = false;
if (process.env.RUNTIME_ENV === 'browser') {
// For browsers use DOM Api XMLHttpRequest
Expand All @@ -27,17 +28,23 @@ if (process.env.RUNTIME_ENV === 'browser') {
// For node use xmlhttprequest
// eslint-disable-next-line global-require
RequestLibrary = require('xmlhttprequest').XMLHttpRequest;
// eslint-disable-next-line global-require
RequestLibrary = require('node-fetch');
isFetch = true;
}

export default class HttpProvider {
constructor(
host = 'http://localhost:8545',
timeout = 8000,
headers = defaultHeaders
headers = defaultHeaders,
// support node-fetch options
options = {}
) {
this.host = host.replace(/\/$/, '');
this.timeout = timeout;
this.headers = {};
this.options = options;
if (Array.isArray(headers)) {
headers.forEach(({ name, value }) => {
this.headers[name] = value;
Expand Down Expand Up @@ -111,6 +118,7 @@ export default class HttpProvider {
myHeaders.append(header, this.headers[header]);
});
return request(uri, {
...this.options,
method: method.toUpperCase(),
headers: myHeaders,
body,
Expand Down Expand Up @@ -174,8 +182,17 @@ export default class HttpProvider {
}

send(requestConfig) {
if (isFetch) throw new Error("Can not get XMLHttpRequest, invalid parameter: 'sync'");
const request = new RequestLibrary();
let request;
if (isFetch) {
if (!RequestLibraryXMLOnly) {
// browser case, Chrome extension v3.
throw new Error("Can not get XMLHttpRequest, invalid parameter: 'sync'");
} else {
request = new RequestLibraryXMLOnly();
}
} else {
request = new RequestLibrary();
}
request.withCredentials = false;
this.requestSend(requestConfig, request);
let result = request.responseText;
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6136,6 +6136,13 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

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

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down Expand Up @@ -8321,6 +8328,11 @@ tr46@^1.0.1:
dependencies:
punycode "^2.1.0"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==

trim-newlines@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"
Expand Down Expand Up @@ -8641,6 +8653,11 @@ watchpack@^1.6.0:
graceful-fs "^4.1.2"
neo-async "^2.5.0"

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==

webidl-conversions@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"
Expand Down Expand Up @@ -8738,6 +8755,14 @@ whatwg-mimetype@^2.1.0, whatwg-mimetype@^2.2.0, whatwg-mimetype@^2.3.0:
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"
integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g==

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

whatwg-url@^6.4.1:
version "6.5.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-6.5.0.tgz#f2df02bff176fd65070df74ad5ccbb5a199965a8"
Expand Down

0 comments on commit daf1677

Please sign in to comment.