forked from wahello/peerio-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shim.js
222 lines (208 loc) · 6.53 KB
/
shim.js
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
/* eslint-disable */
// THIS file contains shims and polyfills which are
// applied before icebear is imported
// LEGACY: next paragraph is legacy and a candidate for removal
if (typeof __dirname === 'undefined') global.__dirname = '/'
if (typeof __filename === 'undefined') global.__filename = ''
if (typeof process === 'undefined') {
global.process = require('process');
} else {
var bProcess = require('process');
for (var p in bProcess) {
if (!(p in process)) {
process[p] = bProcess[p];
}
}
}
// telling our websocketio that we are not browser for sure
process.browser = false;
// JSCore buffer support
if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer;
// polyfill for join (jscore/safari)
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.join
if (!Uint8Array.prototype.join) {
Object.defineProperty(Uint8Array.prototype, 'join', {
value: Array.prototype.join
});
}
// polyfill for fill (jscore/safari)
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.fill
if (!Uint8Array.prototype.fill) {
Uint8Array.prototype.fill = Array.prototype.fill;
}
// determining dev environment
var isDev = typeof __DEV__ === 'boolean' && __DEV__;
const env = process.env;
// providing correct NODE_ENV for libraries which depend on it
env.NODE_ENV = isDev ? 'development' : 'production';
// if we are being executed in browser for some reason
// provide local storage with information about debug
if (typeof localStorage !== 'undefined') {
localStorage.debug = isDev ? '*' : '';
}
// initializing crypto and tweetnacl
require('./app/lib/crypto');
// patching react native websocket JS bridge
// for SSL pinning
// TODO: subject for review
const rnWebSocket = global.WebSocket;
global.WebSocket = function (url) {
// enforce TLS pinning for our main server
const options = url.startsWith('wss://') ? {
// for ios, name of asset containing verified cert
pinSSLCert: 'maincert.com',
// for Android, sha256 hash of verified cert
pinSSLHost: url.match(/\/\/(.*?)\//)[1],
pinSSLCertHash: 'sha256/hOTzKrLdAWvqPQuVV2lYC61JxrXUYyTudUmMhppBkVk='
} : null;
const r = new rnWebSocket(url, null, null, options);
r.binaryType = 'blob';
return r;
};
// codepointat polyfill for string (used for utf-8 substring extraction)
/*! https://mths.be/codepointat v0.2.0 by @mathias */
if (!String.prototype.codePointAt) {
(function () {
'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
var defineProperty = (function () {
// IE 8 only supports `Object.defineProperty` on DOM elements
try {
var object = {};
var $defineProperty = Object.defineProperty;
var result = $defineProperty(object, object, object) && $defineProperty;
} catch (error) { }
return result;
}());
var codePointAt = function (position) {
if (this == null) {
throw TypeError();
}
var string = String(this);
var size = string.length;
// `ToInteger`
var index = position ? Number(position) : 0;
if (index != index) { // better `isNaN`
index = 0;
}
// Account for out-of-bounds indices:
if (index < 0 || index >= size) {
return undefined;
}
// Get the first code unit
var first = string.charCodeAt(index);
var second;
if ( // check if it’s the start of a surrogate pair
first >= 0xD800 && first <= 0xDBFF && // high surrogate
size > index + 1 // there is a next code unit
) {
second = string.charCodeAt(index + 1);
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
}
}
return first;
};
if (defineProperty) {
defineProperty(String.prototype, 'codePointAt', {
'value': codePointAt,
'configurable': true,
'writable': true
});
} else {
String.prototype.codePointAt = codePointAt;
}
}());
}
// fromcodepointat polyfill for string (used for utf-8 substring extraction)
/*! https://mths.be/fromcodepoint v0.2.1 by @mathias */
if (!String.fromCodePoint) {
(function () {
var defineProperty = (function () {
// IE 8 only supports `Object.defineProperty` on DOM elements
try {
var object = {};
var $defineProperty = Object.defineProperty;
var result = $defineProperty(object, object, object) && $defineProperty;
} catch (error) { }
return result;
}());
var stringFromCharCode = String.fromCharCode;
var floor = Math.floor;
var fromCodePoint = function (_) {
var MAX_SIZE = 0x4000;
var codeUnits = [];
var highSurrogate;
var lowSurrogate;
var index = -1;
var length = arguments.length;
if (!length) {
return '';
}
var result = '';
while (++index < length) {
var codePoint = Number(arguments[index]);
if (
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
floor(codePoint) != codePoint // not an integer
) {
throw RangeError('Invalid code point: ' + codePoint);
}
if (codePoint <= 0xFFFF) { // BMP code point
codeUnits.push(codePoint);
} else { // Astral code point; split in surrogate halves
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = (codePoint % 0x400) + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
if (index + 1 == length || codeUnits.length > MAX_SIZE) {
result += stringFromCharCode.apply(null, codeUnits);
codeUnits.length = 0;
}
}
return result;
};
if (defineProperty) {
defineProperty(String, 'fromCodePoint', {
'value': fromCodePoint,
'configurable': true,
'writable': true
});
} else {
String.fromCodePoint = fromCodePoint;
}
}());
}
// random polyfill
if (!Array.prototype.random) {
Array.prototype.random = function () {
return this[Math.floor((Math.random() * this.length))];
}
}
// String.normalize is required by isemail
// Now used as a stub
// TODO: implement a proper polyfill
if (!String.prototype.normalize) {
String.prototype.normalize = function () {
return this;
}
}
// used by icebear benchmarks
// console.time and console.timeEnd polyfill
if (!console["time"] || !console["timeEnd"]) {
var timers = {};
console["time"] = function (id) {
timers[id] = new Date().getTime();
};
console["timeEnd"] = function (id) {
var start = timers[id];
if (start) {
console.log(id + ": " + (new Date().getTime() - start) + "ms");
delete timers[id];
}
};
}