-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.ts
87 lines (74 loc) · 2.03 KB
/
globals.ts
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
import url from "url";
/**
* This polyfill is used where the Uint8Array.fill is not available on given platform.
* It will port the Array.fill function to Uint8Array.fill. So, when Uint8Array.fill is called,
* then the implementation of Array.fill will be invoked.
*
* @platfroms IE11, Safari, Android for React Native Apps.
* @author Mozilla Development Network
*/
if (!Uint8Array.prototype.fill) {
Object.defineProperty(Array.prototype, "fill", {
value: function(value: any) {
// Steps 1-2.
if (this == null) {
throw new TypeError("this is null or not defined");
}
const O = Object(this);
// Steps 3-5.
// tslint:disable-next-line
const len = O.length >>> 0;
// Steps 6-7.
const start = arguments[1];
// tslint:disable-next-line
const relativeStart = start >> 0;
// Step 8.
let k =
relativeStart < 0
? Math.max(len + relativeStart, 0)
: Math.min(relativeStart, len);
// Steps 9-10.
const end = arguments[2];
// tslint:disable-next-line
const relativeEnd = end === undefined ? len : end >> 0;
// Step 11.
const final =
relativeEnd < 0
? Math.max(len + relativeEnd, 0)
: Math.min(relativeEnd, len);
// Step 12.
while (k < final) {
O[k] = value;
k++;
}
// Step 13.
return O;
}
});
// @ts-ignore
Uint8Array.prototype.fill = Array.prototype.fill;
}
global.URL = class URL {
constructor(inputUrl) {
return url.parse(inputUrl);
}
};
if (typeof btoa === "undefined") {
global.btoa = function(str) {
return new Buffer(str, "binary").toString("base64");
};
}
if (typeof atob === "undefined") {
global.atob = function(b64Encoded) {
return new Buffer(b64Encoded, "base64").toString("binary");
};
}
if (window.nacl) {
window.nacl.setPRNG(function(x, n) {
var i,
v = new Uint8Array(n);
crypto.getRandomValues(v);
for (i = 0; i < n; i++) x[i] = v[i];
window.nacl.cleanup(v);
});
}