-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill.ts
110 lines (107 loc) · 3.44 KB
/
polyfill.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// FIXME: Remove polyfills when Expo SDK 51 drops which has TextEncoder and TextDecoder built-in
// https://discord.com/channels/795981131316985866/1151827019684905000/1233104222954852454
(function (window) {
"use strict";
var log = Math.log;
var LN2 = Math.LN2;
var clz32 =
Math.clz32 ||
function (x) {
return (31 - log(x >>> 0) / LN2) | 0;
};
var fromCharCode = String.fromCharCode;
var Object_prototype_toString = {}.toString;
var NativeSharedArrayBuffer = window["SharedArrayBuffer"];
var sharedArrayBufferString = NativeSharedArrayBuffer
? Object_prototype_toString.call(NativeSharedArrayBuffer)
: "";
var NativeUint8Array = window.Uint8Array;
var patchedU8Array = NativeUint8Array || Array;
var arrayBufferString = Object_prototype_toString.call(
(NativeUint8Array ? ArrayBuffer : patchedU8Array).prototype,
);
function decoderReplacer(encoded) {
var cp0 = encoded.charCodeAt(0),
codePoint = 0x110000,
i = 0,
stringLen = encoded.length | 0,
result = "";
switch (cp0 >>> 4) {
// no 1 byte sequences
case 12:
case 13:
codePoint = ((cp0 & 0x1f) << 6) | (encoded.charCodeAt(1) & 0x3f);
i = codePoint < 0x80 ? 0 : 2;
break;
case 14:
codePoint =
((cp0 & 0x0f) << 12) |
((encoded.charCodeAt(1) & 0x3f) << 6) |
(encoded.charCodeAt(2) & 0x3f);
i = codePoint < 0x800 ? 0 : 3;
break;
case 15:
if (cp0 >>> 3 === 30) {
codePoint =
((cp0 & 0x07) << 18) |
((encoded.charCodeAt(1) & 0x3f) << 12) |
((encoded.charCodeAt(2) & 0x3f) << 6) |
encoded.charCodeAt(3);
i = codePoint < 0x10000 ? 0 : 4;
}
}
if (i) {
if (stringLen < i) {
i = 0;
} else if (codePoint < 0x10000) {
// BMP code point
result = fromCharCode(codePoint);
} else if (codePoint < 0x110000) {
codePoint = (codePoint - 0x10080) | 0; //- 0x10000|0;
result = fromCharCode(
((codePoint >>> 10) + 0xd800) | 0, // highSurrogate
((codePoint & 0x3ff) + 0xdc00) | 0, // lowSurrogate
);
} else i = 0; // to fill it in with INVALIDs
}
for (; i < stringLen; i = (i + 1) | 0) result += "\ufffd"; // fill rest with replacement character
return result;
}
function TextDecoder(_, opts) {
/*this["ignoreBOM"] = !!opts && !!opts["ignoreBOM"]*/
}
TextDecoder["prototype"]["decode"] = function (inputArrayOrBuffer) {
var buffer =
(inputArrayOrBuffer && inputArrayOrBuffer.buffer) || inputArrayOrBuffer;
var asObjectString = Object_prototype_toString.call(buffer);
if (
asObjectString !== arrayBufferString &&
asObjectString !== sharedArrayBufferString &&
inputArrayOrBuffer !== undefined
)
throw TypeError(
"Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'",
);
var inputAs8 = NativeUint8Array ? new patchedU8Array(buffer) : buffer;
var resultingString = "";
for (
var index = 0 /*inputAs8[0]!==0xEF||inputAs8[1]!==0xBB||inputAs8[2]!==0xBF||this["ignoreBOM"]?0:3*/,
len = inputAs8.length | 0;
index < len;
index = (index + 32768) | 0
)
resultingString += fromCharCode.apply(
0,
inputAs8[NativeUint8Array ? "subarray" : "slice"](
index,
(index + 32768) | 0,
),
);
return resultingString.replace(
/[\xc0-\xff][\x80-\xbf]+|[\x80-\xff]/g,
decoderReplacer,
);
};
if (!window["TextDecoder"]) window["TextDecoder"] = TextDecoder;
// })(typeof global == "" + void 0 ? typeof self == "" + void 0 ? this : self : global);
})(globalThis);