fix: web3.js fork fetch for browsers

This commit is contained in:
steveluscher 2022-05-26 14:36:57 -07:00 committed by Steven Luscher
parent 9f1b876c74
commit da28badcaa
8 changed files with 2466 additions and 19 deletions

2421
web3.js/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -64,10 +64,10 @@
"borsh": "^0.7.0",
"bs58": "^4.0.1",
"buffer": "6.0.1",
"cross-fetch": "^3.1.4",
"fast-stable-stringify": "^1.0.0",
"jayson": "^3.4.4",
"js-sha3": "^0.8.0",
"node-fetch": "2",
"rpc-websockets": "^7.4.2",
"secp256k1": "^4.0.2",
"superstruct": "^0.14.2",
@ -98,6 +98,7 @@
"@types/mocha": "^9.0.0",
"@types/mz": "^2.7.3",
"@types/node": "^17.0.24",
"@types/node-fetch": "2",
"@types/secp256k1": "^4.0.1",
"@types/sinon": "^10.0.0",
"@types/sinon-chai": "^3.2.8",
@ -138,4 +139,4 @@
"engines": {
"node": ">=12.20.0"
}
}
}

View File

@ -101,7 +101,7 @@ function generateConfig(configType, format) {
'crypto-hash',
'jayson/lib/client/browser',
'js-sha3',
'cross-fetch',
'node-fetch',
'rpc-websockets',
'secp256k1',
'superstruct',
@ -113,7 +113,7 @@ function generateConfig(configType, format) {
case 'browser':
switch (format) {
case 'iife': {
config.external = ['http', 'https'];
config.external = ['http', 'https', 'node-fetch'];
config.output = [
{
@ -161,6 +161,7 @@ function generateConfig(configType, format) {
'https',
'jayson/lib/client/browser',
'js-sha3',
'node-fetch',
'rpc-websockets',
'secp256k1',
'superstruct',

View File

@ -0,0 +1,4 @@
export const Headers = globalThis.Headers;
export const Request = globalThis.Request;
export const Response = globalThis.Response;
export default globalThis.fetch;

View File

@ -1,6 +1,5 @@
import bs58 from 'bs58';
import {Buffer} from 'buffer';
import crossFetch from 'cross-fetch';
// @ts-ignore
import fastStableStringify from 'fast-stable-stringify';
import {
@ -28,6 +27,7 @@ import RpcClient from 'jayson/lib/client/browser';
import {AgentManager} from './agent-manager';
import {EpochSchedule} from './epoch-schedule';
import {SendTransactionError} from './errors';
import fetchImpl, {Response} from './fetch-impl';
import {NonceAccount} from './nonce-account';
import {PublicKey} from './publickey';
import {Signer} from './keypair';
@ -959,7 +959,7 @@ function createRpcClient(
fetchMiddleware?: FetchMiddleware,
disableRetryOnRateLimit?: boolean,
): RpcClient {
const fetch = customFetch ? customFetch : crossFetch;
const fetch = customFetch ? customFetch : fetchImpl;
let agentManager: AgentManager | undefined;
if (!process.env.BROWSER) {
agentManager = new AgentManager(useHttps);
@ -2160,7 +2160,10 @@ export type ConfirmedSignatureInfo = {
*/
export type HttpHeaders = {[header: string]: string};
export type FetchFn = typeof crossFetch;
/**
* The type of the JavaScript `fetch()` API
*/
export type FetchFn = typeof fetchImpl;
/**
* A callback used to augment the outgoing HTTP request

13
web3.js/src/fetch-impl.ts Normal file
View File

@ -0,0 +1,13 @@
import * as nodeFetch from 'node-fetch';
export * from 'node-fetch';
export default async function (
input: nodeFetch.RequestInfo,
init?: nodeFetch.RequestInit,
): Promise<nodeFetch.Response> {
const processedInput =
typeof input === 'string' && input.slice(0, 2) === '//'
? 'https:' + input
: input;
return await nodeFetch.default(processedInput, init);
}

View File

@ -2,5 +2,6 @@
"entryPoints": ["src/index.ts"],
"excludeInternal": true,
"excludePrivate": true,
"intentionallyNotExported": ["src/fetch-impl.ts:default"],
"out": "doc"
}

View File

@ -1852,6 +1852,14 @@
dependencies:
"@types/node" "*"
"@types/node-fetch@2":
version "2.6.1"
resolved "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.1.tgz#8f127c50481db65886800ef496f20bbf15518975"
integrity sha512-oMqjURCaxoSIsHSr1E47QHzbmzNR5rK8McHuNb11BOM9cHcIK3Avy0s/b2JlXHoQGTYS3NsvWzV1M0iK7l0wbA==
dependencies:
"@types/node" "*"
form-data "^3.0.0"
"@types/node@*", "@types/node@>=12", "@types/node@^17.0.24":
version "17.0.35"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.35.tgz#635b7586086d51fb40de0a2ec9d1014a5283ba4a"
@ -2745,9 +2753,10 @@ columnify@~1.5.4:
strip-ansi "^3.0.0"
wcwidth "^1.0.0"
combined-stream@^1.0.6, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz"
resolved "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"
@ -3758,6 +3767,15 @@ forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz"
form-data@^3.0.0:
version "3.0.1"
resolved "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f"
integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"
form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz"
@ -5454,9 +5472,10 @@ node-emoji@^1.10.0:
dependencies:
lodash "^4.17.21"
node-fetch@2.6.7, node-fetch@^2.6.1, node-fetch@^2.6.7, node-fetch@~2.6.1:
node-fetch@2, node-fetch@2.6.7, node-fetch@^2.6.1, node-fetch@^2.6.7, node-fetch@~2.6.1:
version "2.6.7"
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz"
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"