Skip to content

Commit 2f8452e

Browse files
authored
Merge pull request #208 from AElfProject/feature/node-fetch-fix
fix: 🐛 node-fetch, Headers check node/browser; update esm pack
2 parents 7bbfe96 + 7400154 commit 2f8452e

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

build/webpack.esModule.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
/* eslint-env node */
77
import { merge } from 'webpack-merge';
8+
import webpack from 'webpack';
89
import baseConfig from './webpack.common.js';
910
import { OUTPUT_PATH } from './utils.js';
1011

@@ -21,6 +22,7 @@ const nodeConfig = {
2122
resolve: {
2223
alias: {},
2324
fallback: {
25+
buffer: 'buffer',
2426
crypto: 'crypto-browserify',
2527
stream: 'stream-browserify',
2628
https: false,
@@ -29,7 +31,12 @@ const nodeConfig = {
2931
fs: false,
3032
url: false
3133
}
32-
}
34+
},
35+
plugins: [
36+
new webpack.ProvidePlugin({
37+
Buffer: ['buffer', 'Buffer']
38+
})
39+
]
3340
};
3441

3542
export default merge(baseConfig, nodeConfig);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aelf-sdk",
3-
"version": "3.4.20",
3+
"version": "3.4.21",
44
"description": "aelf-sdk js library",
55
"type": "module",
66
"main": "dist/aelf.cjs",

src/util/httpProvider.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author atom-yang
44
*/
55
import { stringify } from 'query-string';
6-
import NodeFetch, { Headers } from 'node-fetch';
6+
import NodeFetch, { Headers as NodeHeaders } from 'node-fetch';
77
import { XMLHttpRequest as XHR } from 'xmlhttprequest';
88

99
const defaultHeaders = {
@@ -14,12 +14,14 @@ const defaultHeaders = {
1414
let RequestLibrary = {};
1515
let RequestLibraryXMLOnly = null;
1616
let isFetch = false;
17+
let HeadersClass = null;
1718
if (process.env.RUNTIME_ENV === 'browser') {
1819
// For browsers use DOM Api XMLHttpRequest
1920
// serviceworker without window and document, only with self
2021
// eslint-disable-next-line no-restricted-globals
2122
const _self = typeof self === 'object' ? self : {};
2223
const _window = typeof window === 'object' ? window : _self;
24+
HeadersClass = _window.Headers || null;
2325
if (typeof _window.XMLHttpRequest !== 'undefined') {
2426
RequestLibrary = _window.XMLHttpRequest;
2527
isFetch = false;
@@ -31,6 +33,7 @@ if (process.env.RUNTIME_ENV === 'browser') {
3133
// For node use xmlhttprequest
3234
RequestLibraryXMLOnly = XHR;
3335
RequestLibrary = NodeFetch;
36+
HeadersClass = NodeHeaders;
3437
isFetch = true;
3538
}
3639

@@ -104,7 +107,7 @@ export default class HttpProvider {
104107
const { url, method = 'POST', params = {}, signal } = requestConfig;
105108
const path = `/api/${url}`.replace(/\/\//g, '/');
106109
let uri = `${this.host}${path}`.replace();
107-
const myHeaders = new Headers();
110+
const myHeaders = HeadersClass ? new HeadersClass() : {};
108111
let body = JSON.stringify(params);
109112
if (method.toUpperCase() === 'GET' || method.toUpperCase() === 'DELETE') {
110113
uri = Object.keys(params).length > 0 ? `${uri}?${stringify(params)}` : uri;

0 commit comments

Comments
 (0)