forked from Axionvera/pocketpay-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshim.js
More file actions
112 lines (104 loc) · 3.7 KB
/
Copy pathshim.js
File metadata and controls
112 lines (104 loc) · 3.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
import "react-native-get-random-values";
import * as ExpoCrypto from "expo-crypto";
import "text-encoding";
import { Buffer } from "buffer";
global.Buffer = Buffer;
// Hermes's TypedArray methods (subarray/slice/map/filter) don't follow the
// ECMAScript SpeciesConstructor spec (facebook/hermes#1495), so calling
// .subarray() on a Buffer returns a plain Uint8Array instead of a Buffer,
// silently losing Buffer.prototype.toString's encoding support (e.g.
// 'base64' is ignored, falling back to a comma-joined byte list). The
// buffer package already works around this for its own .slice(), but
// @stellar/js-xdr's XdrWriter.finalize() calls the native .subarray()
// directly. Re-apply Buffer's prototype whenever subarray is called on
// something that was already a Buffer.
{
const originalSubarray = Uint8Array.prototype.subarray;
Uint8Array.prototype.subarray = function patchedSubarray(...args) {
const result = originalSubarray.apply(this, args);
if (Object.getPrototypeOf(this) === Buffer.prototype) {
Object.setPrototypeOf(result, Buffer.prototype);
}
return result;
};
}
if (
typeof global.crypto !== "object" ||
typeof global.crypto.getRandomValues !== "function"
) {
const cryptoPolyfill = {
...(typeof global.crypto === "object" ? global.crypto : null),
getRandomValues: (array) => ExpoCrypto.getRandomValues(array),
};
try {
Object.defineProperty(global, "crypto", {
configurable: true,
enumerable: true,
value: cryptoPolyfill,
});
} catch (e) {
global.crypto = cryptoPolyfill;
}
}
console.log(
"[shim] crypto.getRandomValues installed:",
typeof global.crypto === "object" &&
typeof global.crypto.getRandomValues === "function",
);
// Polyfill AbortSignal.timeout for the Stellar SDK's fetch-based HTTP client
// (via the feaxios dependency, which calls it unguarded). Hermes has
// AbortController/AbortSignal but not this newer static helper.
if (
typeof AbortSignal !== "undefined" &&
typeof AbortSignal.timeout !== "function"
) {
AbortSignal.timeout = function timeout(ms) {
const controller = new AbortController();
setTimeout(() => {
const reason =
typeof DOMException !== "undefined"
? new DOMException("The operation timed out.", "TimeoutError")
: Object.assign(new Error("The operation timed out."), {
name: "TimeoutError",
});
controller.abort(reason);
}, ms);
return controller.signal;
};
}
// React Native's fetch does not know how to serialize a URLSearchParams body
// (convertRequestBody.js only recognizes string/Blob/FormData/ArrayBuffer) —
// it silently sends an empty body instead. The Stellar SDK's HTTP client
// (feaxios) converts urlencoded string bodies into URLSearchParams, which is
// how submitTransaction's `tx=<xdr>` payload was arriving at Horizon empty.
// Stringify it back before it reaches RN's fetch.
if (typeof global.fetch === "function") {
const originalFetch = global.fetch;
global.fetch = function patchedFetch(input, init) {
if (
init &&
typeof URLSearchParams !== "undefined" &&
init.body instanceof URLSearchParams
) {
init = { ...init, body: init.body.toString() };
}
return originalFetch(input, init);
};
}
// Polyfill process for Stellar SDK.
// 'process/browser' avoids Metro treating this as the Node built-in module.
const bProcess = require("process/browser");
if (typeof process === "undefined") {
global.process = bProcess;
} else {
for (var p in bProcess) {
if (!(p in process)) {
process[p] = bProcess[p];
}
}
}
// Polyfill global env
if (!global.process.env) {
global.process.env = {};
}
global.process.env.NODE_ENV = __DEV__ ? "development" : "production";