-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathshim.js
More file actions
632 lines (574 loc) · 22.7 KB
/
shim.js
File metadata and controls
632 lines (574 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/* eslint-disable import-x/no-nodejs-modules */
import { BackHandler, Platform } from 'react-native';
// RN 0.74+ removed `BackHandler.removeEventListener`. Some third-party
// libraries (notably `@metamask/design-system-react-native`'s `BottomSheet`)
// still call it on unmount, which crashes the screen with
// "BackHandler.removeEventListener is not a function".
// Restore the legacy API by tracking subscriptions returned from
// `addEventListener` and removing them by handler reference.
if (typeof BackHandler.removeEventListener !== 'function') {
const subscriptionsByHandler = new WeakMap();
const originalAddEventListener =
BackHandler.addEventListener.bind(BackHandler);
BackHandler.addEventListener = (eventName, handler) => {
const subscription = originalAddEventListener(eventName, handler);
if (handler && subscription) {
subscriptionsByHandler.set(handler, subscription);
}
return subscription;
};
BackHandler.removeEventListener = (_eventName, handler) => {
const subscription = handler && subscriptionsByHandler.get(handler);
if (subscription && typeof subscription.remove === 'function') {
subscription.remove();
subscriptionsByHandler.delete(handler);
}
};
}
import {
getRandomValues,
randomUUID,
subtle as quickCryptoSubtle,
} from 'react-native-quick-crypto';
import { LaunchArguments } from 'react-native-launch-arguments';
import {
E2E_DIAGNOSTICS_ENDPOINT,
FALLBACK_FIXTURE_SERVER_PORT,
FALLBACK_COMMAND_QUEUE_SERVER_PORT,
getE2ETestConfigDiagnostics,
isE2E,
isTest,
enableApiCallLogs,
testConfig,
} from './app/util/test/utils.js';
import { WS_SERVICES } from './tests/websocket/constants.ts';
import { defaultMockPort } from './tests/api-mocking/mock-config/mockUrlCollection.json';
import './shimPerf';
const getLaunchArgumentKeys = (raw) =>
raw && typeof raw === 'object' ? Object.keys(raw).sort() : [];
const recordLaunchArgumentDiagnostics = (raw) => {
testConfig.launchArgumentKeys = getLaunchArgumentKeys(raw);
testConfig.rawFixtureServerPort = raw?.fixtureServerPort ?? null;
testConfig.rawCommandQueueServerPort = raw?.commandQueueServerPort ?? null;
testConfig.rawMockServerPort = raw?.mockServerPort ?? null;
};
// Needed to polyfill random number generation
import 'react-native-get-random-values';
// Needed to polyfill WalletConnect
import '@walletconnect/react-native-compat';
// Needed to polyfill URL
import 'react-native-url-polyfill/auto';
// Needed to polyfill browser
require('react-native-browser-polyfill'); // eslint-disable-line import-x/no-commonjs
// Force Expo's WHATWG runtime install loop (`expo/src/winter/runtime.native.ts`)
// to execute, regardless of which app code happens to import from `expo`.
//
// Importing the package's main entry triggers the side-effect chain
// `expo/Expo.ts` → `Expo.fx.tsx` → `winter/index.ts` → `runtime.native.ts`,
// which installs `TextDecoder`, `TextDecoderStream`, `TextEncoderStream`,
// `URL`, `URLSearchParams`, `structuredClone`, `__ExpoImportMetaRegistry`,
// the `FormData` patch, and the `Symbol.asyncIterator` fallback onto
// `globalThis`. We previously relied on this chain being triggered
// transitively by feature code (e.g. `useOTAUpdates`,
// `Authentication/utils`, OAuth handlers). That's fragile: a refactor that
// switches every consumer to sub-path imports like `expo/fetch` or
// `expo-image` would silently drop these globals because sub-paths do not
// load `Expo.fx`.
//
// Note: `ReadableStream` and friends are NOT installed here in SDK 54+ —
// Expo moved those to a Metro polyfill (`expo/virtual/streams.js`) which we
// inject from `metro.config.js`. See the comment in `runtime.native.ts`:
// "// ReadableStream is injected by Metro as a global"
import 'expo';
// Log early if running in E2E mode to help diagnose accidental js.env flags
if (isE2E) {
// eslint-disable-next-line no-console
console.warn(
'[E2E MODE] App running with isE2E=true. If unexpected, check your .js.env and unset IS_TEST or METAMASK_ENVIRONMENT=e2e.',
);
// eslint-disable-next-line no-console
console.warn(
`IS_TEST=${process.env.IS_TEST || 'unset'} METAMASK_ENVIRONMENT=${
process.env.METAMASK_ENVIRONMENT || 'unset'
}`,
);
}
// In a testing environment, configure server ports for fixture and command queue servers.
//
// We pass dynamic ports via launchArgs in FixtureHelper.ts, but react-native-launch-arguments
// library behavior differs by platform:
//
// iOS: LaunchArguments.value() successfully reads Detox launchArgs → returns { fixtureServerPort: "30002", ... }
// App uses the dynamic port directly.
//
// Android: LaunchArguments.value() returns {} (library doesn't integrate with Detox on Android)
// → ALWAYS falls back to hardcoded ports (12345 for fixtures, 2446 for command queue)
// Since we need dynamic ports for parallel test execution, the E2E infrastructure uses
// adb reverse to transparently map these hardcoded ports to dynamically allocated ports.
// Example: App connects to localhost:12345, adb reverse maps it to host port 30002.
// See FixtureHelper.ts for the port mapping implementation.
if (isTest) {
const raw = LaunchArguments.value();
recordLaunchArgumentDiagnostics(raw);
testConfig.fixtureServerPort = raw?.fixtureServerPort
? raw.fixtureServerPort
: FALLBACK_FIXTURE_SERVER_PORT;
testConfig.commandQueueServerPort = raw?.commandQueueServerPort
? raw.commandQueueServerPort
: FALLBACK_COMMAND_QUEUE_SERVER_PORT;
testConfig.mockServerPort = raw?.mockServerPort ?? defaultMockPort;
}
// Fix for https://github.com/facebook/react-native/issues/5667
if (typeof global.self === 'undefined') {
global.self = global;
}
if (typeof __dirname === 'undefined') global.__dirname = '/';
if (typeof __filename === 'undefined') global.__filename = '';
if (typeof process === 'undefined') {
// Polyfill process if it's not available
global.process = require('process');
} else {
// Merge polyfill with process without overriding existing properties
const bProcess = require('process');
for (const p in bProcess) {
if (!(p in process)) {
process[p] = bProcess[p];
}
}
}
// Use faster Buffer implementation for React Native
global.Buffer = require('@craftzdog/react-native-buffer').Buffer; // eslint-disable-line import-x/no-commonjs
// Polyfill crypto after process is polyfilled
const crypto = require('crypto'); // eslint-disable-line import-x/no-commonjs
// Needed to polyfill crypto
global.crypto = {
...global.crypto,
...crypto,
randomUUID,
getRandomValues,
subtle: {
...global.crypto.subtle,
...crypto.subtle,
// Shimming just digest as it has been fully implemented.
digest: quickCryptoSubtle.digest,
},
};
process.browser = false;
// EventTarget polyfills for Hyperliquid SDK WebSocket support
if (
typeof global.EventTarget === 'undefined' ||
typeof global.Event === 'undefined'
) {
const { Event, EventTarget } = require('event-target-shim');
global.EventTarget = EventTarget;
global.Event = Event;
}
if (typeof global.CustomEvent === 'undefined') {
global.CustomEvent = function (type, params) {
params = params || {};
const event = new global.Event(type, params);
event.detail = params.detail || null;
return event;
};
}
// CloseEvent polyfill for @nktkas/rews v2 (used by Hyperliquid SDK WebSocket transport)
// React Native/Hermes does not provide CloseEvent as a global constructor
if (typeof global.CloseEvent === 'undefined') {
global.CloseEvent = function (type, params) {
params = params || {};
const event = new global.Event(type, params);
event.code = params.code ?? 0;
event.reason = params.reason ?? '';
event.wasClean = params.wasClean ?? false;
return event;
};
}
// MessageEvent polyfill for @nktkas/rews v2 (used by Hyperliquid SDK WebSocket transport)
// React Native/Hermes does not provide MessageEvent as a global constructor
if (typeof global.MessageEvent === 'undefined') {
global.MessageEvent = function (type, params) {
params = params || {};
const event = new global.Event(type, params);
event.data = params.data ?? null;
event.origin = params.origin ?? '';
event.lastEventId = params.lastEventId ?? '';
return event;
};
}
class AbortError extends Error {
constructor(message) {
super(message);
this.name = 'AbortError';
}
}
// The ReactNative polyfill for AbortController does not populate `signal.reason`.
class AbortControllerWithReason extends AbortController {
abort(reason) {
if (this.signal.aborted) {
return;
}
this.signal.reason =
reason === undefined
? new AbortError('Signal is aborted without reason')
: reason;
super.abort();
}
}
global.AbortController = AbortControllerWithReason;
if (typeof global.AbortSignal.timeout === 'undefined') {
// In the browser this is a DOMException.
class TimeoutError extends Error {
constructor(message) {
super(message);
this.name = 'TimeoutError';
}
}
global.AbortSignal.timeout = function (delay) {
const controller = new AbortController();
setTimeout(
() => controller.abort(new TimeoutError('Signal timed out')),
delay,
);
return controller.signal;
};
}
if (typeof global.Promise.withResolvers === 'undefined') {
global.Promise.withResolvers = function () {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
};
}
// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__;
Object.assign(process.env, { NODE_ENV: isDev ? 'development' : 'production' });
if (typeof localStorage !== 'undefined') {
// eslint-disable-next-line no-undef
localStorage.debug = isDev ? '*' : '';
}
if (enableApiCallLogs || isTest) {
(async () => {
const raw = LaunchArguments.value();
recordLaunchArgumentDiagnostics(raw);
const mockServerPort = raw?.mockServerPort ?? defaultMockPort;
testConfig.mockServerPort = mockServerPort;
const { fetch: originalFetch } = global;
// eslint-disable-next-line no-console
console.log(
`[E2E SHIM] Platform: ${Platform.OS}, mockServerPort: ${mockServerPort}`,
);
// Try multiple hosts to find available mock server
// Priority order:
// 1. localhost (works on iOS, works on Android with adb reverse)
// 2. 10.0.2.2 (Android emulator host - direct access without adb reverse!)
const hosts = ['localhost'];
if (Platform.OS === 'android') {
hosts.push('10.0.2.2');
}
let MOCKTTP_URL = '';
let isMockServerAvailable = false;
for (const host of hosts) {
const testUrl = `http://${host}:${mockServerPort}`;
// eslint-disable-next-line no-console
console.log(`[E2E SHIM] Trying mock server at: ${testUrl}`);
const available = await originalFetch(`${testUrl}/health-check`)
.then((res) => {
// eslint-disable-next-line no-console
console.log(`[E2E SHIM] ${host} health check: ${res.ok}`);
return res.ok;
})
.catch((err) => {
// eslint-disable-next-line no-console
console.log(`[E2E SHIM] ${host} health check failed: ${err.message}`);
return false;
});
if (available) {
MOCKTTP_URL = testUrl;
isMockServerAvailable = true;
// eslint-disable-next-line no-console
console.log(`[E2E SHIM] Mock server connected via ${host}`);
break;
}
}
const postE2EDiagnostics = async (phase) => {
if (!isMockServerAvailable || !MOCKTTP_URL) {
return;
}
const diagnostics = getE2ETestConfigDiagnostics({
source: 'shim',
phase,
platform: Platform.OS,
mockServerUrl: MOCKTTP_URL,
mockServerAvailable: String(isMockServerAvailable),
launchArgFixtureServerPort: raw?.fixtureServerPort ?? 'missing',
launchArgCommandQueueServerPort:
raw?.commandQueueServerPort ?? 'missing',
launchArgMockServerPort: raw?.mockServerPort ?? 'missing',
});
await originalFetch(`${MOCKTTP_URL}${E2E_DIAGNOSTICS_ENDPOINT}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(diagnostics),
}).catch((error) => {
// eslint-disable-next-line no-console
console.warn(`[E2E SHIM] Failed to post diagnostics: ${error.message}`);
});
};
await postE2EDiagnostics('mock-server-connected');
// if mockServer is off we route to original destination
global.fetch = async (url, options) => {
// Extract URL string from Request or URL objects
let urlString;
if (typeof url === 'string') {
urlString = url;
} else if (url instanceof URL) {
urlString = url.href;
} else if (url && typeof url === 'object' && url.url) {
// Request object has a 'url' property
urlString = url.url;
} else {
urlString = String(url);
}
return isMockServerAvailable
? originalFetch(
`${MOCKTTP_URL}/proxy?url=${encodeURIComponent(urlString)}`,
options,
).catch(() => originalFetch(url, options))
: originalFetch(url, options);
};
if (isMockServerAvailable) {
// Patch XMLHttpRequest for Axios and other libraries
const OriginalXHR = global.XMLHttpRequest;
if (OriginalXHR) {
global.XMLHttpRequest = function (...args) {
const xhr = new OriginalXHR(...args);
const originalOpen = xhr.open;
xhr.open = function (method, url, ...openArgs) {
try {
// Route external URLs through mock server proxy
if (
typeof url === 'string' &&
(url.startsWith('http://') || url.startsWith('https://'))
) {
// Bypass proxy for local command queue server
try {
const parsed = new URL(url);
const isLocalHost =
parsed.hostname === 'localhost' ||
parsed.hostname === '127.0.0.1' ||
parsed.hostname === '10.0.2.2';
const isCommandQueue =
isLocalHost &&
parsed.port ===
String(
testConfig.commandQueueServerPort ||
FALLBACK_COMMAND_QUEUE_SERVER_PORT,
);
if (isCommandQueue) {
return originalOpen.call(this, method, url, ...openArgs);
}
} catch (e) {
// ignore URL parse errors and continue to proxy
}
if (
!url.includes(`localhost:${mockServerPort}`) &&
!url.includes('/proxy')
) {
const originalUrl = url;
url = `${MOCKTTP_URL}/proxy?url=${encodeURIComponent(url)}`;
}
}
return originalOpen.call(this, method, url, ...openArgs);
} catch (error) {
return originalOpen.call(this, method, url, ...openArgs);
}
};
return xhr;
};
// Copy static properties and prototype chain
try {
Object.setPrototypeOf(global.XMLHttpRequest, OriginalXHR);
Object.assign(global.XMLHttpRequest, OriginalXHR);
// Store reference to verify patching worked
global.__MOCK_XHR_PATCHED = true;
global.__ORIGINAL_XHR = OriginalXHR;
// eslint-disable-next-line no-console
console.log(
'[XHR Patch] Successfully patched XMLHttpRequest for E2E testing',
);
} catch (error) {
console.warn('[XHR Patch] Failed to copy XHR properties:', error);
// Restore original if copying failed
global.XMLHttpRequest = OriginalXHR;
}
} else {
console.warn(
'[XHR Patch] XMLHttpRequest not available, skipping patch',
);
}
// Patch WebSocket to route production wss:// URLs to local mock servers.
// Each WS service gets its own mock port via WS_SERVICES config.
// Non-matching wss:// URLs pass through unchanged.
if (WS_SERVICES.length > 0 && global.WebSocket) {
const OriginalWebSocket = global.WebSocket;
const wsRoutes = {};
for (const svc of WS_SERVICES) {
const port = raw?.[svc.launchArgKey] ?? svc.fallbackPort;
wsRoutes[svc.url] = `ws://localhost:${port}`;
}
global.WebSocket = function (url, protocols) {
let targetUrl = url;
if (typeof url === 'string') {
for (const [prefix, localUrl] of Object.entries(wsRoutes)) {
if (url.startsWith(prefix)) {
targetUrl = localUrl;
break;
}
}
}
return protocols !== undefined
? new OriginalWebSocket(targetUrl, protocols)
: new OriginalWebSocket(targetUrl);
};
Object.setPrototypeOf(global.WebSocket, OriginalWebSocket);
Object.assign(global.WebSocket, OriginalWebSocket);
global.WebSocket.prototype = OriginalWebSocket.prototype;
// eslint-disable-next-line no-console
console.log(`[WS Patch] Routes: ${JSON.stringify(wsRoutes)}`);
}
// Patch expo/fetch so its native networking routes through the mock
// proxy. Bridge-controller's SSE `getQuoteStream` (and any other expo
// fetch consumer) MUST hit the mock or the swap/bridge E2E quote
// mocks never fire and tests time out waiting for "Rate" /
// "Network fee".
//
// Defence-in-depth: we patch THREE places because we cannot rely on
// any single one surviving Metro/Babel transpilation in `expo@54`:
//
// 1. The native module prototype `ExpoFetchModule.NativeRequest
// .prototype.start(url, init, body)`. This is the lowest layer
// that EVERY expo fetch ultimately calls, regardless of how
// `import { fetch } from 'expo/fetch'` was bundled or which
// module captured the reference. Bulletproof.
// 2. The re-exporter `expo/src/winter/fetch/index` (what
// `expo/fetch` resolves to). Catches consumers that destructure
// the binding from the re-exporter at module load time.
// 3. The source module `expo/src/winter/fetch/fetch`. Catches
// consumers that read through the live `export *` getter.
//
// If we miss the JS-level patches but get the native one, requests
// still get redirected — they just won't show the [E2E SHIM] log
// line at the JS layer.
const buildProxyUrl = (url) =>
`${MOCKTTP_URL}/proxy?url=${encodeURIComponent(url)}`;
const shouldProxy = (url) => {
if (typeof url !== 'string') return false;
if (!url.startsWith('http://') && !url.startsWith('https://'))
return false;
if (url.startsWith(MOCKTTP_URL)) return false;
return true;
};
// (1) Native prototype — bulletproof
try {
const {
ExpoFetchModule,
} = require('expo/src/winter/fetch/ExpoFetchModule');
const NativeRequest = ExpoFetchModule?.NativeRequest;
const proto = NativeRequest?.prototype;
if (proto && typeof proto.start === 'function' && !proto.__e2ePatched) {
const originalStart = proto.start;
proto.start = function patchedStart(url, init, body) {
const targetUrl = shouldProxy(url) ? buildProxyUrl(url) : url;
if (targetUrl !== url) {
// eslint-disable-next-line no-console
console.log(
`[E2E SHIM] expo/fetch (native): ${url} → ${targetUrl}`,
);
}
return originalStart.call(this, targetUrl, init, body);
};
proto.__e2ePatched = true;
// eslint-disable-next-line no-console
console.log(
'[E2E SHIM] Patched ExpoFetchModule.NativeRequest.prototype.start',
);
} else if (proto?.__e2ePatched) {
// eslint-disable-next-line no-console
console.log('[E2E SHIM] NativeRequest.start already patched');
} else {
// eslint-disable-next-line no-console
console.warn(
'[E2E SHIM] ExpoFetchModule.NativeRequest.prototype.start not patchable',
);
}
} catch (e) {
// eslint-disable-next-line no-console
console.warn(
'[E2E SHIM] Failed to patch ExpoFetchModule native prototype:',
e.message,
);
}
// (2) + (3) JS-level patches for log visibility and as a fallback.
// NOTE: each `require(...)` call MUST use a string literal — Metro
// statically analyses requires and rejects variable paths.
const patchExpoFetchModuleObject = (mod, label) => {
try {
const originalExpoFetch = mod.fetch;
if (typeof originalExpoFetch !== 'function') {
// eslint-disable-next-line no-console
console.warn(`[E2E SHIM] ${label}: no fetch export to patch`);
return;
}
const patchedExpoFetch = (url, options) => {
if (!shouldProxy(url)) {
return originalExpoFetch(url, options);
}
const proxyUrl = buildProxyUrl(url);
// eslint-disable-next-line no-console
console.log(`[E2E SHIM] ${label}: ${url} → ${proxyUrl}`);
return originalExpoFetch(proxyUrl, options);
};
Object.defineProperty(mod, 'fetch', {
configurable: true,
enumerable: true,
writable: true,
value: patchedExpoFetch,
});
const installed = mod.fetch === patchedExpoFetch;
// eslint-disable-next-line no-console
console.log(`[E2E SHIM] Patched ${label} (installed=${installed})`);
} catch (e) {
// eslint-disable-next-line no-console
console.warn(`[E2E SHIM] Failed to patch ${label}:`, e.message);
}
};
try {
patchExpoFetchModuleObject(
require('expo/src/winter/fetch/fetch'),
'expo/fetch source',
);
} catch (e) {
// eslint-disable-next-line no-console
console.warn(
'[E2E SHIM] Failed to require expo/fetch source:',
e.message,
);
}
try {
patchExpoFetchModuleObject(
require('expo/src/winter/fetch/index'),
'expo/fetch re-exporter',
);
} catch (e) {
// eslint-disable-next-line no-console
console.warn(
'[E2E SHIM] Failed to require expo/fetch re-exporter:',
e.message,
);
}
}
})();
}