Skip to content

Commit d217b6b

Browse files
authored
fix: fork URL and fetch for React Native in web3.js (#26604)
1 parent e8390c6 commit d217b6b

File tree

8 files changed

+61
-12
lines changed

8 files changed

+61
-12
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"./lib/index.cjs.js": "./lib/index.browser.cjs.js",
2424
"./lib/index.esm.js": "./lib/index.browser.esm.js"
2525
},
26+
"react-native": "lib/index.native.js",
2627
"main": "lib/index.cjs.js",
2728
"module": "lib/index.esm.js",
2829
"types": "lib/index.d.ts",
@@ -68,6 +69,7 @@
6869
"jayson": "^3.4.4",
6970
"js-sha3": "^0.8.0",
7071
"node-fetch": "2",
72+
"react-native-url-polyfill": "^1.3.0",
7173
"rpc-websockets": "^7.5.0",
7274
"secp256k1": "^4.0.2",
7375
"superstruct": "^0.14.2",

rollup.config.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const env = process.env.NODE_ENV;
1212
const extensions = ['.js', '.ts'];
1313

1414
function generateConfig(configType, format) {
15-
const browser = configType === 'browser';
15+
const browser = configType === 'browser' || configType === 'react-native';
1616
const bundle = format === 'iife';
1717

1818
const config = {
@@ -91,7 +91,7 @@ function generateConfig(configType, format) {
9191
},
9292
};
9393

94-
if (configType !== 'browser') {
94+
if (!browser) {
9595
// Prevent dependencies from being bundled
9696
config.external = [
9797
/@babel\/runtime/,
@@ -114,6 +114,7 @@ function generateConfig(configType, format) {
114114

115115
switch (configType) {
116116
case 'browser':
117+
case 'react-native':
117118
switch (format) {
118119
case 'iife': {
119120
config.external = ['http', 'https', 'node-fetch'];
@@ -139,16 +140,20 @@ function generateConfig(configType, format) {
139140
default: {
140141
config.output = [
141142
{
142-
file: 'lib/index.browser.cjs.js',
143+
file: `lib/index.${
144+
configType === 'react-native' ? 'native' : 'browser.cjs'
145+
}.js`,
143146
format: 'cjs',
144147
sourcemap: true,
145148
},
146-
{
147-
file: 'lib/index.browser.esm.js',
148-
format: 'es',
149-
sourcemap: true,
150-
},
151-
];
149+
configType === 'browser'
150+
? {
151+
file: 'lib/index.browser.esm.js',
152+
format: 'es',
153+
sourcemap: true,
154+
}
155+
: null,
156+
].filter(Boolean);
152157

153158
// Prevent dependencies from being bundled
154159
config.external = [
@@ -165,6 +170,7 @@ function generateConfig(configType, format) {
165170
'jayson/lib/client/browser',
166171
'js-sha3',
167172
'node-fetch',
173+
'react-native-url-polyfill',
168174
'rpc-websockets',
169175
'secp256k1',
170176
'superstruct',
@@ -205,4 +211,5 @@ export default [
205211
generateConfig('node'),
206212
generateConfig('browser'),
207213
generateConfig('browser', 'iife'),
214+
generateConfig('react-native'),
208215
];
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const Headers = globalThis.Headers;
2+
export const Request = globalThis.Request;
3+
export const Response = globalThis.Response;
4+
export default globalThis.fetch;

src/connection.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {Struct} from 'superstruct';
2424
import {Client as RpcWebSocketClient} from 'rpc-websockets';
2525
import RpcClient from 'jayson/lib/client/browser';
2626

27+
import {URL} from './util/url-impl';
2728
import {AgentManager} from './agent-manager';
2829
import {EpochSchedule} from './epoch-schedule';
2930
import {SendTransactionError, SolanaJSONRPCError} from './errors';
@@ -41,7 +42,7 @@ import {
4142
TransactionExpiredBlockheightExceededError,
4243
TransactionExpiredTimeoutError,
4344
} from './util/tx-expiry-custom-errors';
44-
import {makeWebsocketUrl} from './util/url';
45+
import {makeWebsocketUrl} from './util/makeWebsocketUrl';
4546
import type {Blockhash} from './blockhash';
4647
import type {FeeCalculator} from './fee-calculator';
4748
import type {TransactionSignature} from './transaction';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {default} from 'react-native-url-polyfill';
2+
export * from 'react-native-url-polyfill';

src/util/url.ts renamed to src/util/makeWebsocketUrl.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {URL} from './url-impl';
2+
13
export function makeWebsocketUrl(endpoint: string) {
24
let url = new URL(endpoint);
35
const useHttps = url.protocol === 'https:';

src/util/url-impl.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const URL = globalThis.URL;
2+
export const URLSearchParams = globalThis.URLSearchParams;

yarn.lock

+31-2
Original file line numberDiff line numberDiff line change
@@ -2460,6 +2460,14 @@ [email protected]:
24602460
base64-js "^1.3.1"
24612461
ieee754 "^1.2.1"
24622462

2463+
buffer@^5.4.3:
2464+
version "5.7.1"
2465+
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0"
2466+
integrity sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==
2467+
dependencies:
2468+
base64-js "^1.3.1"
2469+
ieee754 "^1.1.13"
2470+
24632471
buffer@~6.0.3:
24642472
version "6.0.3"
24652473
resolved "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz"
@@ -4217,7 +4225,7 @@ [email protected], iconv-lite@^0.6.2:
42174225
dependencies:
42184226
safer-buffer ">= 2.1.2 < 3.0.0"
42194227

4220-
ieee754@^1.2.1:
4228+
ieee754@^1.1.13, ieee754@^1.2.1:
42214229
version "1.2.1"
42224230
resolved "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz"
42234231

@@ -6215,7 +6223,7 @@ punycode@^1.3.2:
62156223
version "1.4.1"
62166224
resolved "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz"
62176225

6218-
punycode@^2.1.0:
6226+
punycode@^2.1.0, punycode@^2.1.1:
62196227
version "2.1.1"
62206228
resolved "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz"
62216229

@@ -6277,6 +6285,13 @@ rc@^1.2.8:
62776285
minimist "^1.2.0"
62786286
strip-json-comments "~2.0.1"
62796287

6288+
react-native-url-polyfill@^1.3.0:
6289+
version "1.3.0"
6290+
resolved "https://registry.yarnpkg.com/react-native-url-polyfill/-/react-native-url-polyfill-1.3.0.tgz#c1763de0f2a8c22cc3e959b654c8790622b6ef6a"
6291+
integrity sha512-w9JfSkvpqqlix9UjDvJjm1EjSt652zVQ6iwCIj1cVVkwXf4jQhQgTNXY6EVTwuAmUjg6BC6k9RHCBynoLFo3IQ==
6292+
dependencies:
6293+
whatwg-url-without-unicode "8.0.0-3"
6294+
62806295
read-cmd-shim@^3.0.0:
62816296
version "3.0.0"
62826297
resolved "https://registry.yarnpkg.com/read-cmd-shim/-/read-cmd-shim-3.0.0.tgz#62b8c638225c61e6cc607f8f4b779f3b8238f155"
@@ -7578,12 +7593,26 @@ webidl-conversions@^3.0.0:
75787593
version "3.0.1"
75797594
resolved "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz"
75807595

7596+
webidl-conversions@^5.0.0:
7597+
version "5.0.0"
7598+
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff"
7599+
integrity sha512-VlZwKPCkYKxQgeSbH5EyngOmRp7Ww7I9rQLERETtf5ofd9pGeswWiOtogpEO850jziPRarreGxn5QIiTqpb2wA==
7600+
75817601
whatwg-encoding@^2.0.0:
75827602
version "2.0.0"
75837603
resolved "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-2.0.0.tgz"
75847604
dependencies:
75857605
iconv-lite "0.6.3"
75867606

7607+
7608+
version "8.0.0-3"
7609+
resolved "https://registry.yarnpkg.com/whatwg-url-without-unicode/-/whatwg-url-without-unicode-8.0.0-3.tgz#ab6df4bf6caaa6c85a59f6e82c026151d4bb376b"
7610+
integrity sha512-HoKuzZrUlgpz35YO27XgD28uh/WJH4B0+3ttFqRo//lmq+9T/mIOJ6kqmINI9HpUpz1imRC/nR/lxKpJiv0uig==
7611+
dependencies:
7612+
buffer "^5.4.3"
7613+
punycode "^2.1.1"
7614+
webidl-conversions "^5.0.0"
7615+
75877616
whatwg-url@^5.0.0:
75887617
version "5.0.0"
75897618
resolved "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz"

0 commit comments

Comments
 (0)