forked from infinitescroll/react-native-ipfs-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shim.js
605 lines (540 loc) · 19.3 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
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
import Promise from 'bluebird';
import { Headers, Response, Request } from 'node-fetch'
const URL_Parse = require('url-parse');
const whatwg_fetch = require('whatwg-fetch').fetch;
const URLSearchParams = require('@ungap/url-search-params')
import setGlobalVars from 'indexeddbshim';
global.URL = URL_Parse;
global.URLSearchParams = URLSearchParams;
global.Request = Request;
global.Response = Response;
global.Headers = Headers
whatwg_fetch.promise = Promise
console.log(whatwg_fetch, 'whatwg_fetch')
// To see all the requests in the chrome Dev tools in the network tab.
XMLHttpRequest = GLOBAL.originalXMLHttpRequest ?
GLOBAL.originalXMLHttpRequest :
GLOBAL.XMLHttpRequest;
// fetch logger
global._fetch = fetch;
global.fetch = (uri, options, ...args) => {
options.headers = new Headers(options.headers)
console.log('fetching..', uri, options, ...args);
return whatwg_fetch(uri, options, ...args).then((response) => {
console.log('fetched: ', { request: { uri, options, ...args }, response });
console.log('fetched response:', { response });
return response;
}).catch((e) => { console.warn('fetch error:', e) });
};
// ---
// TOGGLE THIS CODE BLOCK ON AND OFF TO USE RN-FETCH-BLOB OR NOT
// ---
/* import RNFetchBlob from "rn-fetch-blob";
const Fetch = RNFetchBlob.polyfill.Fetch;
// // replace built-in fetch
// // window.fetch = new Fetch({
global.fetch = new Fetch({
// // enable this option so that the response data conversion handled
// // automatically
auto: true, // TODO: Decide if auto conversion is the best option here
// // when receiving response data, the module will match its Content-Type header
// // with strings in this array. If it contains any one of string in this array,
// // the response body will be considered as binary data and the data will be
// // stored in file system instead of in memory.
// // By default, it only store response data to file system when Content-Type
// // contains string `application/octet`.
binaryContentTypes: ["image/", "video/", "audio/"],
fileCache: true // TODO: Decide if this approach to storing files is the best
}).build();
global.Headers = global.fetch.Headers;
console.log(global.Headers)
console.log(global.Headers, fetch.Headers, fetch); */
// ---
// END OF CODE BLOCK TO TOGGLE
// ---
//
// Experiment with using a manually transpiled local version
// global.fetch = require("./fetch.js").fetch;
// Best fetch + streams polyfill I found:
// import "@stardazed/streams-polyfill";
// const ReadableStream = require("web-streams-polyfill/es2018").ReadableStream;
// global.ReadableStream = ReadableStream;
// response.clone() is undefined
// https://github.com/github/fetch/issues/746#issuecomment-573891642
// https://github.com/jonnyreeves/fetch-readablestream/issues/12
//global.fetch = require("fetch-readablestream");
// Going with import until we figure out exactly what would need to be
// done manually with a require style and directly editing the globals
// global.fetch = require("@stardazed/streams-polyfill");
// ***
// Used this fetch polyfill most recently - once this PR
// https://github.com/ipfs/js-ipfs-http-client/pull/1224
// lands in js-ipfs-http-client we won't need this, and same when
// using a local version of js-ifps-http-client with those changes
// import "@stardazed/streams-polyfill";
// ***
//global.Symbol = require("core-js/es6/symbol");
// import symbol from "core-js/es6/symbol";
// global.Symbol = symbol;
//require("core-js/fn/symbol/iterator");
//require("core-js/fn/symbol/async-iterator");
// import "regenerator-runtime/runtime";
// global.Symbol = require("core-js/es6/symbol");
// require("core-js/fn/symbol/iterator");
// require("core-js/fn/symbol/async-iterator");
// Note to Jon:
// Another attempt to have the iterator piece working:
// Add more global polyfills for this syntax for the various data
// structures it might be used with.
// ---
// require("core-js/fn/map");
// require("core-js/fn/set");
// require("core-js/fn/array/find");
// ---
// To address
// Error: FileReader.readAsArrayBuffer is not implemented
// from: https://stackoverflow.com/questions/42829838/react-native-atob-btoa-not-working-without-remote-js-debugging
// const chars =
// "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// const atob = (input = "") => {
// console.log({ input });
// let str = input.replace(/=+$/, "");
// let output = "";
// if (str.length % 4 == 1) {
// throw new Error(
// "'atob' failed: The string to be decoded is not correctly encoded."
// );
// }
// for (
// let bc = 0, bs = 0, buffer, i = 0;
// (buffer = str.charAt(i++));
// ~buffer && ((bs = bc % 4 ? bs * 64 + buffer : buffer), bc++ % 4)
// ? (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6))))
// : 0
// ) {
// buffer = chars.indexOf(buffer);
// }
// return output;
// };
// Hugo's version of atob
// from: https://stackoverflow.com/questions/42829838/react-native-atob-btoa-not-working-without-remote-js-debugging
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const atob = (input = "") => {
let str = input.replace(/[=]+$/, "");
let output = "";
if (str.length % 4 == 1) {
throw new Error(
"'atob' failed: The string to be decoded is not correctly encoded."
);
}
for (
let bc = 0, bs = 0, buffer, i = 0;
(buffer = str.charAt(i++));
~buffer && ((bs = bc % 4 ? bs * 64 + buffer : buffer), bc++ % 4)
? (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6))))
: 0
) {
buffer = chars.indexOf(buffer);
}
return output;
};
// Hugo's version
FileReader.prototype.readAsArrayBuffer = function (blob) {
if (this.readyState === this.LOADING) {
throw new Error("InvalidStateError");
}
this._setReadyState(this.LOADING);
this._result = null;
this._error = null;
const fr = new FileReader();
fr.onloadend = () => {
console.log("fr.result", fr.result);
const content = atob(
fr.result.substr("data:application/octet-stream;base64,".length)
);
console.log({ content });
const buffer = new ArrayBuffer(content.length);
const view = new Uint8Array(buffer);
view.set(Array.from(content).map(c => c.charCodeAt(0)));
this._result = buffer;
this._setReadyState(this.DONE);
};
console.log("blob", blob);
fr.readAsDataURL(blob);
};
// Modified version to see internals
// FileReader.prototype.readAsArrayBuffer = function(blob) {
// console.log({ data: blob._data });
// if (this.readyState === this.LOADING) throw new Error("InvalidStateError");
// this._setReadyState(this.LOADING);
// this._result = null;
// this._error = null;
// const fr = new FileReader();
// fr.onloadend = () => {
// console.log("fr.result", fr.result);
// let content;
// // TODO: Remove this includes modification
// if (fr.result.includes("data:application/octet-stream;base64,")) {
// content = atob(
// fr.result.substr("data:application/octet-stream;base64,".length)
// );
// } else {
// content = atob(fr.result);
// }
// console.log("content", content);
// // const newContent = String(content);
// const buffer = new ArrayBuffer(content.length);
// const view = new Uint8Array(buffer);
// view.set(Array.from(content).map(c => c.charCodeAt(0)));
// this._result = buffer;
// this._setReadyState(this.DONE);
// };
// if (!blob._data) {
// return;
// }
// console.log("blob", blob);
// fr.readAsDataURL(blob);
// };
/// ---
// // Latest:
import symbol from "core-js/es6/symbol";
import iterator from "core-js/fn/symbol/iterator";
import asyncIterator from "core-js/fn/symbol/async-iterator";
console.log("symbol", symbol);
global.Symbol = symbol;
// global.Symbol.iterator = iterator;
// global.Symbol.iterator = Symbol("Symbol.iterator");
// global.Symbol.asyncIterator = Symbol("Symbol.asyncIterator");
// Note to Jon:
// One attempt to have the iterator piece working:
// Attempt to directly define what it's doing here (I think it's doing this?)
// ---
// Originally with arrays rather than objects
// TODO: Possibly readd these if that's what it-all is using
// ---
// Decoder
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 codePoint = encoded.charCodeAt(0) << 24;
var leadingOnes = clz32(~codePoint) | 0;
var endPos = 0,
stringLen = encoded.length | 0;
var result = "";
if (leadingOnes < 5 && stringLen >= leadingOnes) {
codePoint = (codePoint << leadingOnes) >>> (24 + leadingOnes);
for (endPos = 1; endPos < leadingOnes; endPos = (endPos + 1) | 0)
codePoint =
(codePoint << 6) | (encoded.charCodeAt(endPos) & 0x3f) /*0b00111111*/;
if (codePoint <= 0xffff) {
// BMP code point
result += fromCharCode(codePoint);
} else if (codePoint <= 0x10ffff) {
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint = (codePoint - 0x10000) | 0;
result += fromCharCode(
((codePoint >> 10) + 0xd800) | 0, // highSurrogate
((codePoint & 0x3ff) + 0xdc00) | 0 // lowSurrogate
);
} else endPos = 0; // to fill it in with INVALIDs
}
for (; endPos < stringLen; endPos = (endPos + 1) | 0) result += "\ufffd"; // replacement character
return result;
}
function TextDecoder() { }
TextDecoder["prototype"]["decode"] = function (inputArrayOrBuffer) {
console.log({ inputArrayOrBuffer });
var buffer =
(inputArrayOrBuffer && inputArrayOrBuffer.buffer) || inputArrayOrBuffer;
var asObjectString = Object_prototype_toString.call(buffer);
if (
asObjectString !== arrayBufferString &&
asObjectString !== sharedArrayBufferString
)
throw Error(
"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, 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]*/g, decoderReplacer);
};
global.TextDecoder = TextDecoder;
// if (typeof TextEncoder === "undefined") {
// TextEncoder = function TextEncoder() {};
// TextEncoder.prototype.encode = function encode(str) {
// "use strict";
// var Len = str.length,
// resPos = -1;
// // The Uint8Array's length must be at least 3x the length of the string because an invalid UTF-16
// // takes up the equivelent space of 3 UTF-8 characters to encode it properly. However, Array's
// // have an auto expanding length and 1.5x should be just the right balance for most uses.
// var resArr =
// typeof Uint8Array === "undefined"
// ? new Array(Len * 1.5)
// : new Uint8Array(Len * 3);
// for (var point = 0, nextcode = 0, i = 0; i !== Len; ) {
// (point = str.charCodeAt(i)), (i += 1);
// if (point >= 0xd800 && point <= 0xdbff) {
// if (i === Len) {
// resArr[(resPos += 1)] = 0xef /*0b11101111*/;
// resArr[(resPos += 1)] = 0xbf /*0b10111111*/;
// resArr[(resPos += 1)] = 0xbd /*0b10111101*/;
// break;
// }
// // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
// nextcode = str.charCodeAt(i);
// if (nextcode >= 0xdc00 && nextcode <= 0xdfff) {
// point = (point - 0xd800) * 0x400 + nextcode - 0xdc00 + 0x10000;
// i += 1;
// if (point > 0xffff) {
// resArr[(resPos += 1)] = (0x1e /*0b11110*/ << 3) | (point >>> 18);
// resArr[(resPos += 1)] =
// (0x2 /*0b10*/ << 6) | ((point >>> 12) & 0x3f) /*0b00111111*/;
// resArr[(resPos += 1)] =
// (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f) /*0b00111111*/;
// resArr[(resPos += 1)] =
// (0x2 /*0b10*/ << 6) | (point & 0x3f) /*0b00111111*/;
// continue;
// }
// } else {
// resArr[(resPos += 1)] = 0xef /*0b11101111*/;
// resArr[(resPos += 1)] = 0xbf /*0b10111111*/;
// resArr[(resPos += 1)] = 0xbd /*0b10111101*/;
// continue;
// }
// }
// if (point <= 0x007f) {
// resArr[(resPos += 1)] = (0x0 /*0b0*/ << 7) | point;
// } else if (point <= 0x07ff) {
// resArr[(resPos += 1)] = (0x6 /*0b110*/ << 5) | (point >>> 6);
// resArr[(resPos += 1)] =
// (0x2 /*0b10*/ << 6) | (point & 0x3f) /*0b00111111*/;
// } else {
// resArr[(resPos += 1)] = (0xe /*0b1110*/ << 4) | (point >>> 12);
// resArr[(resPos += 1)] =
// (0x2 /*0b10*/ << 6) | ((point >>> 6) & 0x3f) /*0b00111111*/;
// resArr[(resPos += 1)] =
// (0x2 /*0b10*/ << 6) | (point & 0x3f) /*0b00111111*/;
// }
// }
// if (typeof Uint8Array !== "undefined")
// return resArr.subarray(0, resPos + 1);
// // else // IE 6-9
// resArr.length = resPos + 1; // trim off extra weight
// return resArr;
// };
// TextEncoder.prototype.toString = function() {
// return "[object TextEncoder]";
// };
// try {
// // Object.defineProperty only works on DOM prototypes in IE8
// Object.defineProperty(TextEncoder.prototype, "encoding", {
// get: function() {
// if (TextEncoder.prototype.isPrototypeOf(this)) return "utf-8";
// else throw TypeError("Illegal invocation");
// }
// });
// } catch (e) {
// /*IE6-8 fallback*/ TextEncoder.prototype.encoding = "utf-8";
// }
// if (typeof Symbol !== "undefined")
// TextEncoder.prototype[Symbol.toStringTag] = "TextEncoder";
// }
// if (Array.prototype["@@iterator"] === undefined) {
// console.log("Manually defining array @@iterator");
// Array.prototype["@@iterator"] = function() {
// let i = 0;
// return {
// next: () => {
// console.log("--- next called");
// return {
// done: i >= this.length,
// value: this[i++]
// };
// }
// };
// };
// } else {
// console.log("Array has @@iterator");
// }
// if (Array.prototype[Symbol.iterator] === undefined) {
// console.log("Manually defining array Symbol.iterator");
// Array.prototype[Symbol.iterator] = function() {
// let i = 0;
// return {
// next: () => {
// console.log("--- next called");
// return {
// done: i >= this.length,
// value: this[i++]
// };
// }
// };
// };
// } else {
// console.log("Array has Symbol.iterator");
// }
// // if (Array.prototype[Symbol.asyncIterator] === undefined) {
// console.log("Manually defining array async iterator");
// Array.prototype[Symbol.asyncIterator] = function() {
// let i = 0;
// return {
// async next() {
// console.log("--- Async next called");
// await new Promise(resolve => setTimeout(resolve, 1000)); // (3)
// if (i <= this.length) {
// return { done: false, value: this[i++] };
// } else {
// return { done: true };
// }
// }
// };
// };
// } else {
// console.log("Array has async iterator");
// }
// String
// Array
// Array-like objects (e.g., arguments or NodeList)
// TypedArray;
// Map;
// Set;
// if (String.prototype["@@iterator"] === undefined) {
// String.prototype[Symbol.asyncIterator] = function() {
// let i = 0;
// return {
// async next() {
// console.log("--- Async next called");
// await new Promise(resolve => setTimeout(resolve, 1000)); // (3)
// if (i <= this.length) {
// return { done: false, value: this[i++] };
// } else {
// return { done: true };
// }
// }
// };
// };
// Map.prototype[Symbol.asyncIterator] = function() {
// let i = 0;
// return {
// async next() {
// console.log("--- Async next called");
// await new Promise(resolve => setTimeout(resolve, 1000)); // (3)
// if (i <= this.length) {
// return { done: false, value: this[i++] };
// } else {
// return { done: true };
// }
// }
// };
// };
// Set.prototype[Symbol.asyncIterator] = function() {
// let i = 0;
// return {
// async next() {
// console.log("--- Async next called");
// await new Promise(resolve => setTimeout(resolve, 1000)); // (3)
// if (i <= this.length) {
// return { done: false, value: this[i++] };
// } else {
// return { done: true };
// }
// }
// };
// };
// TypedArray.prototype[Symbol.asyncIterator] = function() {
// let i = 0;
// return {
// async next() {
// console.log("--- Async next called");
// await new Promise(resolve => setTimeout(resolve, 1000)); // (3)
// if (i <= this.length) {
// return { done: false, value: this[i++] };
// } else {
// return { done: true };
// }
// }
// };
// };
// }
/// ---
// import "core-js";
// if (Platform.OS === 'android') {
// if (typeof Symbol === 'undefined') {
// if (Array.prototype['@@iterator'] === undefined) {
// Array.prototype['@@iterator'] = function() {
// let i = 0;
// return {
// next: () => ({
// done: i >= this.length,
// value: this[i++],
// }),
// };
// };
// }
// }
// }
if (typeof __dirname === "undefined") global.__dirname = "/";
if (typeof __filename === "undefined") global.__filename = "";
if (typeof process === "undefined") {
global.process = require("process");
} else {
const bProcess = require("process");
for (var p in bProcess) {
if (!(p in process)) {
process[p] = bProcess[p];
}
}
}
process.browser = true;
if (typeof Buffer === "undefined") global.Buffer = require("buffer").Buffer;
// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === "boolean" && __DEV__;
process.env["NODE_ENV"] = isDev ? "development" : "production";
if (typeof localStorage !== "undefined") {
localStorage.debug = isDev ? "*" : "";
}
// If using the crypto shim, uncomment the following line to ensure
// crypto is loaded first, so it can populate global.crypto
require("crypto");
// We use the "Bluebird" lib for Promises, because it shows good perf
// and it implements the "unhandledrejection" event:
global.Promise = Promise;
// Global catch of unhandled Promise rejections:
global.onunhandledrejection = function onunhandledrejection(error) {
// Warning: when running in "remote debug" mode (JS environment is Chrome browser),
// this handler is called a second time by Bluebird with a custom "dom-event".
// We need to filter this case out:
if (error instanceof Error) {
console.log(error); // Your custom error logging/reporting code
}
};