@mentra/jspolyfill 3.3.0-dev.943 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/startup.js +183 -87
- package/dist/startup.js +183 -87
- package/package.json +1 -1
- package/src/startup.ts +226 -103
package/assets/startup.js
CHANGED
|
@@ -436,39 +436,183 @@
|
|
|
436
436
|
}
|
|
437
437
|
;
|
|
438
438
|
g.crypto = cryptoNs;
|
|
439
|
+
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
440
|
+
const BASE64_VALUES = new Uint8Array(128).fill(255);
|
|
441
|
+
for (let i = 0; i < BASE64_ALPHABET.length; i++) BASE64_VALUES[BASE64_ALPHABET.charCodeAt(i)] = i;
|
|
442
|
+
const CHARS_PER_CHUNK = 8192;
|
|
443
|
+
function bytesOf(data) {
|
|
444
|
+
return data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
445
|
+
}
|
|
446
|
+
function bytesToBase64(bytes) {
|
|
447
|
+
const parts = [];
|
|
448
|
+
const chars = [];
|
|
449
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
450
|
+
const b1 = bytes[i];
|
|
451
|
+
const b2 = i + 1 < bytes.length ? bytes[i + 1] : 0;
|
|
452
|
+
const b3 = i + 2 < bytes.length ? bytes[i + 2] : 0;
|
|
453
|
+
chars.push(
|
|
454
|
+
BASE64_ALPHABET.charCodeAt(b1 >> 2),
|
|
455
|
+
BASE64_ALPHABET.charCodeAt((b1 & 3) << 4 | b2 >> 4),
|
|
456
|
+
i + 1 < bytes.length ? BASE64_ALPHABET.charCodeAt((b2 & 15) << 2 | b3 >> 6) : 61,
|
|
457
|
+
i + 2 < bytes.length ? BASE64_ALPHABET.charCodeAt(b3 & 63) : 61
|
|
458
|
+
);
|
|
459
|
+
if (chars.length >= CHARS_PER_CHUNK) {
|
|
460
|
+
parts.push(String.fromCharCode.apply(null, chars));
|
|
461
|
+
chars.length = 0;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
parts.push(String.fromCharCode.apply(null, chars));
|
|
465
|
+
return parts.join("");
|
|
466
|
+
}
|
|
467
|
+
function base64ToBytes(base64) {
|
|
468
|
+
const length = base64.length;
|
|
469
|
+
if (length % 4 !== 0) throw new TypeError("Invalid base64 from the native bridge");
|
|
470
|
+
const padding = length === 0 || base64.charCodeAt(length - 1) !== 61 ? 0 : base64.charCodeAt(length - 2) === 61 ? 2 : 1;
|
|
471
|
+
const out = new Uint8Array(length / 4 * 3 - padding);
|
|
472
|
+
const unpadded = padding === 0 ? length : length - 4;
|
|
473
|
+
let codes = 0;
|
|
474
|
+
let values = 0;
|
|
475
|
+
let o = 0;
|
|
476
|
+
for (let i = 0; i < unpadded; i += 4) {
|
|
477
|
+
const c1 = base64.charCodeAt(i);
|
|
478
|
+
const c2 = base64.charCodeAt(i + 1);
|
|
479
|
+
const c3 = base64.charCodeAt(i + 2);
|
|
480
|
+
const c4 = base64.charCodeAt(i + 3);
|
|
481
|
+
const v1 = BASE64_VALUES[c1 & 127];
|
|
482
|
+
const v2 = BASE64_VALUES[c2 & 127];
|
|
483
|
+
const v3 = BASE64_VALUES[c3 & 127];
|
|
484
|
+
const v4 = BASE64_VALUES[c4 & 127];
|
|
485
|
+
codes |= c1 | c2 | c3 | c4;
|
|
486
|
+
values |= v1 | v2 | v3 | v4;
|
|
487
|
+
out[o++] = v1 << 2 | v2 >> 4;
|
|
488
|
+
out[o++] = (v2 & 15) << 4 | v3 >> 2;
|
|
489
|
+
out[o++] = (v3 & 3) << 6 | v4;
|
|
490
|
+
}
|
|
491
|
+
if (padding > 0) {
|
|
492
|
+
const c1 = base64.charCodeAt(unpadded);
|
|
493
|
+
const c2 = base64.charCodeAt(unpadded + 1);
|
|
494
|
+
const c3 = padding === 1 ? base64.charCodeAt(unpadded + 2) : 65;
|
|
495
|
+
const v1 = BASE64_VALUES[c1 & 127];
|
|
496
|
+
const v2 = BASE64_VALUES[c2 & 127];
|
|
497
|
+
const v3 = BASE64_VALUES[c3 & 127];
|
|
498
|
+
codes |= c1 | c2 | c3;
|
|
499
|
+
values |= v1 | v2 | v3;
|
|
500
|
+
out[o++] = v1 << 2 | v2 >> 4;
|
|
501
|
+
if (padding === 1) out[o++] = (v2 & 15) << 4 | v3 >> 2;
|
|
502
|
+
}
|
|
503
|
+
if (codes > 127 || values > 63) throw new TypeError("Invalid base64 from the native bridge");
|
|
504
|
+
return out;
|
|
505
|
+
}
|
|
506
|
+
function decodeUtf8(bytes, fatal, ignoreBOM) {
|
|
507
|
+
const length = bytes.length;
|
|
508
|
+
const parts = [];
|
|
509
|
+
const units = [];
|
|
510
|
+
let i = !ignoreBOM && length >= 3 && bytes[0] === 239 && bytes[1] === 187 && bytes[2] === 191 ? 3 : 0;
|
|
511
|
+
while (i < length) {
|
|
512
|
+
const asciiStart = i;
|
|
513
|
+
while (i < length && bytes[i] < 128) i++;
|
|
514
|
+
if (i > asciiStart) {
|
|
515
|
+
if (units.length > 0) {
|
|
516
|
+
parts.push(String.fromCharCode.apply(null, units));
|
|
517
|
+
units.length = 0;
|
|
518
|
+
}
|
|
519
|
+
for (let start = asciiStart; start < i; start += CHARS_PER_CHUNK) {
|
|
520
|
+
const chunk = bytes.subarray(start, Math.min(i, start + CHARS_PER_CHUNK));
|
|
521
|
+
parts.push(String.fromCharCode.apply(null, chunk));
|
|
522
|
+
}
|
|
523
|
+
if (i >= length) break;
|
|
524
|
+
}
|
|
525
|
+
const lead = bytes[i++];
|
|
526
|
+
let codePoint;
|
|
527
|
+
let needed;
|
|
528
|
+
let lower = 128;
|
|
529
|
+
let upper = 191;
|
|
530
|
+
if (lead >= 194 && lead <= 223) {
|
|
531
|
+
needed = 1;
|
|
532
|
+
codePoint = lead & 31;
|
|
533
|
+
} else if (lead >= 224 && lead <= 239) {
|
|
534
|
+
if (lead === 224) lower = 160;
|
|
535
|
+
if (lead === 237) upper = 159;
|
|
536
|
+
needed = 2;
|
|
537
|
+
codePoint = lead & 15;
|
|
538
|
+
} else if (lead >= 240 && lead <= 244) {
|
|
539
|
+
if (lead === 240) lower = 144;
|
|
540
|
+
if (lead === 244) upper = 143;
|
|
541
|
+
needed = 3;
|
|
542
|
+
codePoint = lead & 7;
|
|
543
|
+
} else {
|
|
544
|
+
needed = -1;
|
|
545
|
+
codePoint = 0;
|
|
546
|
+
}
|
|
547
|
+
while (needed > 0) {
|
|
548
|
+
const next = i < length ? bytes[i] : -1;
|
|
549
|
+
if (next < lower || next > upper) break;
|
|
550
|
+
codePoint = codePoint << 6 | next & 63;
|
|
551
|
+
lower = 128;
|
|
552
|
+
upper = 191;
|
|
553
|
+
needed--;
|
|
554
|
+
i++;
|
|
555
|
+
}
|
|
556
|
+
if (needed !== 0) {
|
|
557
|
+
if (fatal) throw new TypeError("The encoded data was not valid utf-8");
|
|
558
|
+
units.push(65533);
|
|
559
|
+
} else if (codePoint > 65535) {
|
|
560
|
+
codePoint -= 65536;
|
|
561
|
+
units.push(55296 + (codePoint >> 10), 56320 + (codePoint & 1023));
|
|
562
|
+
} else {
|
|
563
|
+
units.push(codePoint);
|
|
564
|
+
}
|
|
565
|
+
if (units.length >= CHARS_PER_CHUNK) {
|
|
566
|
+
parts.push(String.fromCharCode.apply(null, units));
|
|
567
|
+
units.length = 0;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
if (units.length > 0) parts.push(String.fromCharCode.apply(null, units));
|
|
571
|
+
return parts.join("");
|
|
572
|
+
}
|
|
439
573
|
if (typeof g.TextEncoder !== "function") {
|
|
574
|
+
const codePointAt = (str, i) => {
|
|
575
|
+
const code = str.charCodeAt(i);
|
|
576
|
+
if (code >= 55296 && code <= 56319 && i + 1 < str.length) {
|
|
577
|
+
const next = str.charCodeAt(i + 1);
|
|
578
|
+
if (next >= 56320 && next <= 57343) return (code - 55296 << 10) + (next - 56320) + 65536;
|
|
579
|
+
}
|
|
580
|
+
return code;
|
|
581
|
+
};
|
|
440
582
|
class TextEncoderPolyfill {
|
|
441
583
|
constructor() {
|
|
442
584
|
this.encoding = "utf-8";
|
|
443
585
|
}
|
|
444
586
|
encode(input) {
|
|
445
587
|
const str = input ?? "";
|
|
446
|
-
|
|
588
|
+
let length = 0;
|
|
447
589
|
for (let i = 0; i < str.length; i++) {
|
|
448
|
-
|
|
449
|
-
if (code
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
590
|
+
const code = codePointAt(str, i);
|
|
591
|
+
if (code > 65535) i++;
|
|
592
|
+
length += code < 128 ? 1 : code < 2048 ? 2 : code < 65536 ? 3 : 4;
|
|
593
|
+
}
|
|
594
|
+
const out = new Uint8Array(length);
|
|
595
|
+
let at = 0;
|
|
596
|
+
for (let i = 0; i < str.length; i++) {
|
|
597
|
+
const code = codePointAt(str, i);
|
|
598
|
+
if (code > 65535) i++;
|
|
456
599
|
if (code < 128) {
|
|
457
|
-
out
|
|
600
|
+
out[at++] = code;
|
|
458
601
|
} else if (code < 2048) {
|
|
459
|
-
out
|
|
602
|
+
out[at++] = 192 | code >> 6;
|
|
603
|
+
out[at++] = 128 | code & 63;
|
|
460
604
|
} else if (code < 65536) {
|
|
461
|
-
out
|
|
605
|
+
out[at++] = 224 | code >> 12;
|
|
606
|
+
out[at++] = 128 | code >> 6 & 63;
|
|
607
|
+
out[at++] = 128 | code & 63;
|
|
462
608
|
} else {
|
|
463
|
-
out
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
128 | code & 63
|
|
468
|
-
);
|
|
609
|
+
out[at++] = 240 | code >> 18;
|
|
610
|
+
out[at++] = 128 | code >> 12 & 63;
|
|
611
|
+
out[at++] = 128 | code >> 6 & 63;
|
|
612
|
+
out[at++] = 128 | code & 63;
|
|
469
613
|
}
|
|
470
614
|
}
|
|
471
|
-
return
|
|
615
|
+
return out;
|
|
472
616
|
}
|
|
473
617
|
}
|
|
474
618
|
;
|
|
@@ -476,36 +620,14 @@
|
|
|
476
620
|
}
|
|
477
621
|
if (typeof g.TextDecoder !== "function") {
|
|
478
622
|
class TextDecoderPolyfill {
|
|
479
|
-
constructor(encoding = "utf-8") {
|
|
623
|
+
constructor(encoding = "utf-8", options) {
|
|
480
624
|
this.encoding = encoding;
|
|
625
|
+
this.fatal = options?.fatal === true;
|
|
626
|
+
this.ignoreBOM = options?.ignoreBOM === true;
|
|
481
627
|
}
|
|
482
628
|
decode(buffer) {
|
|
483
629
|
if (!buffer) return "";
|
|
484
|
-
|
|
485
|
-
let out = "";
|
|
486
|
-
let i = 0;
|
|
487
|
-
while (i < bytes.length) {
|
|
488
|
-
const b1 = bytes[i++];
|
|
489
|
-
let code;
|
|
490
|
-
if (b1 < 128) {
|
|
491
|
-
code = b1;
|
|
492
|
-
} else if (b1 < 192) {
|
|
493
|
-
code = 65533;
|
|
494
|
-
} else if (b1 < 224) {
|
|
495
|
-
code = (b1 & 31) << 6 | bytes[i++] & 63;
|
|
496
|
-
} else if (b1 < 240) {
|
|
497
|
-
code = (b1 & 15) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63;
|
|
498
|
-
} else {
|
|
499
|
-
code = (b1 & 7) << 18 | (bytes[i++] & 63) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63;
|
|
500
|
-
}
|
|
501
|
-
if (code > 65535) {
|
|
502
|
-
code -= 65536;
|
|
503
|
-
out += String.fromCharCode(55296 + (code >> 10), 56320 + (code & 1023));
|
|
504
|
-
} else {
|
|
505
|
-
out += String.fromCharCode(code);
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
return out;
|
|
630
|
+
return decodeUtf8(bytesOf(buffer), this.fatal, this.ignoreBOM);
|
|
509
631
|
}
|
|
510
632
|
}
|
|
511
633
|
;
|
|
@@ -552,9 +674,12 @@
|
|
|
552
674
|
const url = typeof input === "string" ? input : input.toString();
|
|
553
675
|
const method = (init?.method ?? "GET").toUpperCase();
|
|
554
676
|
let bodyString = null;
|
|
677
|
+
let bodyBase64;
|
|
555
678
|
const reqBody = init?.body;
|
|
556
679
|
if (typeof reqBody === "string") {
|
|
557
680
|
bodyString = reqBody;
|
|
681
|
+
} else if (reqBody instanceof ArrayBuffer || ArrayBuffer.isView(reqBody)) {
|
|
682
|
+
bodyBase64 = bytesToBase64(bytesOf(reqBody));
|
|
558
683
|
} else if (reqBody && typeof reqBody.toString === "function") {
|
|
559
684
|
bodyString = reqBody.toString();
|
|
560
685
|
}
|
|
@@ -578,9 +703,10 @@
|
|
|
578
703
|
}
|
|
579
704
|
const sendRequest = g.__mentraSendRequest;
|
|
580
705
|
const result = await sendRequest("fetch", "request", [
|
|
581
|
-
{ url, method, headers, body: bodyString }
|
|
706
|
+
bodyBase64 === void 0 ? { url, method, headers, body: bodyString } : { url, method, headers, bodyBase64 }
|
|
582
707
|
]);
|
|
583
|
-
const
|
|
708
|
+
const responseText = typeof result.body === "string" ? result.body : null;
|
|
709
|
+
const responseBase64 = typeof result.bodyBase64 === "string" ? result.bodyBase64 : "";
|
|
584
710
|
const responseHeaders = new Map(Object.entries(result.headers ?? {}));
|
|
585
711
|
class ResponseLike {
|
|
586
712
|
constructor() {
|
|
@@ -597,11 +723,13 @@
|
|
|
597
723
|
};
|
|
598
724
|
}
|
|
599
725
|
async text() {
|
|
600
|
-
return
|
|
726
|
+
if (responseText === null) return decodeUtf8(base64ToBytes(responseBase64), false, false);
|
|
727
|
+
return responseText.charCodeAt(0) === 65279 ? responseText.slice(1) : responseText;
|
|
601
728
|
}
|
|
602
729
|
async json() {
|
|
730
|
+
const text = await this.text();
|
|
603
731
|
try {
|
|
604
|
-
return JSON.parse(
|
|
732
|
+
return JSON.parse(text);
|
|
605
733
|
} catch (e) {
|
|
606
734
|
const err = new Error(
|
|
607
735
|
`Failed to parse JSON response from ${url}: ${e instanceof Error ? e.message : String(e)}`
|
|
@@ -611,52 +739,21 @@
|
|
|
611
739
|
}
|
|
612
740
|
}
|
|
613
741
|
async arrayBuffer() {
|
|
742
|
+
if (responseText === null) return base64ToBytes(responseBase64).buffer;
|
|
614
743
|
const enc = new g.TextEncoder();
|
|
615
|
-
return enc.encode(
|
|
744
|
+
return enc.encode(responseText).buffer;
|
|
616
745
|
}
|
|
617
746
|
}
|
|
618
747
|
return new ResponseLike();
|
|
619
748
|
};
|
|
620
749
|
}
|
|
621
750
|
if (typeof g.WebSocket !== "function") {
|
|
622
|
-
let
|
|
623
|
-
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
624
|
-
let out = "";
|
|
625
|
-
let i = 0;
|
|
626
|
-
while (i < bytes.length) {
|
|
627
|
-
const c1 = bytes[i++];
|
|
628
|
-
const c2 = i < bytes.length ? bytes[i++] : NaN;
|
|
629
|
-
const c3 = i < bytes.length ? bytes[i++] : NaN;
|
|
630
|
-
const e1 = c1 >> 2;
|
|
631
|
-
const e2 = (c1 & 3) << 4 | (Number.isNaN(c2) ? 0 : c2 >> 4);
|
|
632
|
-
const e3 = Number.isNaN(c2) ? 64 : (c2 & 15) << 2 | (Number.isNaN(c3) ? 0 : c3 >> 6);
|
|
633
|
-
const e4 = Number.isNaN(c3) ? 64 : c3 & 63;
|
|
634
|
-
out += b64[e1] + b64[e2] + (e3 === 64 ? "=" : b64[e3]) + (e4 === 64 ? "=" : b64[e4]);
|
|
635
|
-
}
|
|
636
|
-
return out;
|
|
637
|
-
}, base64ToBytes2 = function(s) {
|
|
638
|
-
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
639
|
-
const clean = s.replace(/=+$/, "");
|
|
640
|
-
const out = [];
|
|
641
|
-
let buf = 0;
|
|
642
|
-
let bits = 0;
|
|
643
|
-
for (let i = 0; i < clean.length; i++) {
|
|
644
|
-
const idx = b64.indexOf(clean[i]);
|
|
645
|
-
if (idx < 0) continue;
|
|
646
|
-
buf = buf << 6 | idx;
|
|
647
|
-
bits += 6;
|
|
648
|
-
if (bits >= 8) {
|
|
649
|
-
bits -= 8;
|
|
650
|
-
out.push(buf >> bits & 255);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
return new Uint8Array(out);
|
|
654
|
-
}, queueMicrotaskSafe2 = function(fn) {
|
|
751
|
+
let queueMicrotaskSafe2 = function(fn) {
|
|
655
752
|
const q = g.queueMicrotask;
|
|
656
753
|
if (typeof q === "function") q(fn);
|
|
657
754
|
else g.Promise.resolve().then(fn);
|
|
658
755
|
};
|
|
659
|
-
var
|
|
756
|
+
var queueMicrotaskSafe = queueMicrotaskSafe2;
|
|
660
757
|
const sockets = /* @__PURE__ */ new Map();
|
|
661
758
|
g.__mentraDeliverWebSocketEvent = (sid, type, payload) => {
|
|
662
759
|
const sock = sockets.get(sid);
|
|
@@ -718,8 +815,7 @@
|
|
|
718
815
|
payload = data;
|
|
719
816
|
} else {
|
|
720
817
|
kind = "binary";
|
|
721
|
-
|
|
722
|
-
payload = bytesToBase642(bytes);
|
|
818
|
+
payload = bytesToBase64(bytesOf(data));
|
|
723
819
|
}
|
|
724
820
|
try {
|
|
725
821
|
__dispatch("ws", "send", JSON.stringify([{ sid: this.sid, kind, payload }]));
|
|
@@ -752,7 +848,7 @@
|
|
|
752
848
|
}
|
|
753
849
|
const synth = { type, target: this, ...ev };
|
|
754
850
|
if (type === "message" && ev.kind === "binary" && typeof ev.data === "string") {
|
|
755
|
-
synth.data =
|
|
851
|
+
synth.data = base64ToBytes(ev.data).buffer;
|
|
756
852
|
} else if (type === "message" && ev.kind === "text") {
|
|
757
853
|
synth.data = ev.data;
|
|
758
854
|
}
|
package/dist/startup.js
CHANGED
|
@@ -436,39 +436,183 @@
|
|
|
436
436
|
}
|
|
437
437
|
;
|
|
438
438
|
g.crypto = cryptoNs;
|
|
439
|
+
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
440
|
+
const BASE64_VALUES = new Uint8Array(128).fill(255);
|
|
441
|
+
for (let i = 0; i < BASE64_ALPHABET.length; i++) BASE64_VALUES[BASE64_ALPHABET.charCodeAt(i)] = i;
|
|
442
|
+
const CHARS_PER_CHUNK = 8192;
|
|
443
|
+
function bytesOf(data) {
|
|
444
|
+
return data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
445
|
+
}
|
|
446
|
+
function bytesToBase64(bytes) {
|
|
447
|
+
const parts = [];
|
|
448
|
+
const chars = [];
|
|
449
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
450
|
+
const b1 = bytes[i];
|
|
451
|
+
const b2 = i + 1 < bytes.length ? bytes[i + 1] : 0;
|
|
452
|
+
const b3 = i + 2 < bytes.length ? bytes[i + 2] : 0;
|
|
453
|
+
chars.push(
|
|
454
|
+
BASE64_ALPHABET.charCodeAt(b1 >> 2),
|
|
455
|
+
BASE64_ALPHABET.charCodeAt((b1 & 3) << 4 | b2 >> 4),
|
|
456
|
+
i + 1 < bytes.length ? BASE64_ALPHABET.charCodeAt((b2 & 15) << 2 | b3 >> 6) : 61,
|
|
457
|
+
i + 2 < bytes.length ? BASE64_ALPHABET.charCodeAt(b3 & 63) : 61
|
|
458
|
+
);
|
|
459
|
+
if (chars.length >= CHARS_PER_CHUNK) {
|
|
460
|
+
parts.push(String.fromCharCode.apply(null, chars));
|
|
461
|
+
chars.length = 0;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
parts.push(String.fromCharCode.apply(null, chars));
|
|
465
|
+
return parts.join("");
|
|
466
|
+
}
|
|
467
|
+
function base64ToBytes(base64) {
|
|
468
|
+
const length = base64.length;
|
|
469
|
+
if (length % 4 !== 0) throw new TypeError("Invalid base64 from the native bridge");
|
|
470
|
+
const padding = length === 0 || base64.charCodeAt(length - 1) !== 61 ? 0 : base64.charCodeAt(length - 2) === 61 ? 2 : 1;
|
|
471
|
+
const out = new Uint8Array(length / 4 * 3 - padding);
|
|
472
|
+
const unpadded = padding === 0 ? length : length - 4;
|
|
473
|
+
let codes = 0;
|
|
474
|
+
let values = 0;
|
|
475
|
+
let o = 0;
|
|
476
|
+
for (let i = 0; i < unpadded; i += 4) {
|
|
477
|
+
const c1 = base64.charCodeAt(i);
|
|
478
|
+
const c2 = base64.charCodeAt(i + 1);
|
|
479
|
+
const c3 = base64.charCodeAt(i + 2);
|
|
480
|
+
const c4 = base64.charCodeAt(i + 3);
|
|
481
|
+
const v1 = BASE64_VALUES[c1 & 127];
|
|
482
|
+
const v2 = BASE64_VALUES[c2 & 127];
|
|
483
|
+
const v3 = BASE64_VALUES[c3 & 127];
|
|
484
|
+
const v4 = BASE64_VALUES[c4 & 127];
|
|
485
|
+
codes |= c1 | c2 | c3 | c4;
|
|
486
|
+
values |= v1 | v2 | v3 | v4;
|
|
487
|
+
out[o++] = v1 << 2 | v2 >> 4;
|
|
488
|
+
out[o++] = (v2 & 15) << 4 | v3 >> 2;
|
|
489
|
+
out[o++] = (v3 & 3) << 6 | v4;
|
|
490
|
+
}
|
|
491
|
+
if (padding > 0) {
|
|
492
|
+
const c1 = base64.charCodeAt(unpadded);
|
|
493
|
+
const c2 = base64.charCodeAt(unpadded + 1);
|
|
494
|
+
const c3 = padding === 1 ? base64.charCodeAt(unpadded + 2) : 65;
|
|
495
|
+
const v1 = BASE64_VALUES[c1 & 127];
|
|
496
|
+
const v2 = BASE64_VALUES[c2 & 127];
|
|
497
|
+
const v3 = BASE64_VALUES[c3 & 127];
|
|
498
|
+
codes |= c1 | c2 | c3;
|
|
499
|
+
values |= v1 | v2 | v3;
|
|
500
|
+
out[o++] = v1 << 2 | v2 >> 4;
|
|
501
|
+
if (padding === 1) out[o++] = (v2 & 15) << 4 | v3 >> 2;
|
|
502
|
+
}
|
|
503
|
+
if (codes > 127 || values > 63) throw new TypeError("Invalid base64 from the native bridge");
|
|
504
|
+
return out;
|
|
505
|
+
}
|
|
506
|
+
function decodeUtf8(bytes, fatal, ignoreBOM) {
|
|
507
|
+
const length = bytes.length;
|
|
508
|
+
const parts = [];
|
|
509
|
+
const units = [];
|
|
510
|
+
let i = !ignoreBOM && length >= 3 && bytes[0] === 239 && bytes[1] === 187 && bytes[2] === 191 ? 3 : 0;
|
|
511
|
+
while (i < length) {
|
|
512
|
+
const asciiStart = i;
|
|
513
|
+
while (i < length && bytes[i] < 128) i++;
|
|
514
|
+
if (i > asciiStart) {
|
|
515
|
+
if (units.length > 0) {
|
|
516
|
+
parts.push(String.fromCharCode.apply(null, units));
|
|
517
|
+
units.length = 0;
|
|
518
|
+
}
|
|
519
|
+
for (let start = asciiStart; start < i; start += CHARS_PER_CHUNK) {
|
|
520
|
+
const chunk = bytes.subarray(start, Math.min(i, start + CHARS_PER_CHUNK));
|
|
521
|
+
parts.push(String.fromCharCode.apply(null, chunk));
|
|
522
|
+
}
|
|
523
|
+
if (i >= length) break;
|
|
524
|
+
}
|
|
525
|
+
const lead = bytes[i++];
|
|
526
|
+
let codePoint;
|
|
527
|
+
let needed;
|
|
528
|
+
let lower = 128;
|
|
529
|
+
let upper = 191;
|
|
530
|
+
if (lead >= 194 && lead <= 223) {
|
|
531
|
+
needed = 1;
|
|
532
|
+
codePoint = lead & 31;
|
|
533
|
+
} else if (lead >= 224 && lead <= 239) {
|
|
534
|
+
if (lead === 224) lower = 160;
|
|
535
|
+
if (lead === 237) upper = 159;
|
|
536
|
+
needed = 2;
|
|
537
|
+
codePoint = lead & 15;
|
|
538
|
+
} else if (lead >= 240 && lead <= 244) {
|
|
539
|
+
if (lead === 240) lower = 144;
|
|
540
|
+
if (lead === 244) upper = 143;
|
|
541
|
+
needed = 3;
|
|
542
|
+
codePoint = lead & 7;
|
|
543
|
+
} else {
|
|
544
|
+
needed = -1;
|
|
545
|
+
codePoint = 0;
|
|
546
|
+
}
|
|
547
|
+
while (needed > 0) {
|
|
548
|
+
const next = i < length ? bytes[i] : -1;
|
|
549
|
+
if (next < lower || next > upper) break;
|
|
550
|
+
codePoint = codePoint << 6 | next & 63;
|
|
551
|
+
lower = 128;
|
|
552
|
+
upper = 191;
|
|
553
|
+
needed--;
|
|
554
|
+
i++;
|
|
555
|
+
}
|
|
556
|
+
if (needed !== 0) {
|
|
557
|
+
if (fatal) throw new TypeError("The encoded data was not valid utf-8");
|
|
558
|
+
units.push(65533);
|
|
559
|
+
} else if (codePoint > 65535) {
|
|
560
|
+
codePoint -= 65536;
|
|
561
|
+
units.push(55296 + (codePoint >> 10), 56320 + (codePoint & 1023));
|
|
562
|
+
} else {
|
|
563
|
+
units.push(codePoint);
|
|
564
|
+
}
|
|
565
|
+
if (units.length >= CHARS_PER_CHUNK) {
|
|
566
|
+
parts.push(String.fromCharCode.apply(null, units));
|
|
567
|
+
units.length = 0;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
if (units.length > 0) parts.push(String.fromCharCode.apply(null, units));
|
|
571
|
+
return parts.join("");
|
|
572
|
+
}
|
|
439
573
|
if (typeof g.TextEncoder !== "function") {
|
|
574
|
+
const codePointAt = (str, i) => {
|
|
575
|
+
const code = str.charCodeAt(i);
|
|
576
|
+
if (code >= 55296 && code <= 56319 && i + 1 < str.length) {
|
|
577
|
+
const next = str.charCodeAt(i + 1);
|
|
578
|
+
if (next >= 56320 && next <= 57343) return (code - 55296 << 10) + (next - 56320) + 65536;
|
|
579
|
+
}
|
|
580
|
+
return code;
|
|
581
|
+
};
|
|
440
582
|
class TextEncoderPolyfill {
|
|
441
583
|
constructor() {
|
|
442
584
|
this.encoding = "utf-8";
|
|
443
585
|
}
|
|
444
586
|
encode(input) {
|
|
445
587
|
const str = input ?? "";
|
|
446
|
-
|
|
588
|
+
let length = 0;
|
|
447
589
|
for (let i = 0; i < str.length; i++) {
|
|
448
|
-
|
|
449
|
-
if (code
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
590
|
+
const code = codePointAt(str, i);
|
|
591
|
+
if (code > 65535) i++;
|
|
592
|
+
length += code < 128 ? 1 : code < 2048 ? 2 : code < 65536 ? 3 : 4;
|
|
593
|
+
}
|
|
594
|
+
const out = new Uint8Array(length);
|
|
595
|
+
let at = 0;
|
|
596
|
+
for (let i = 0; i < str.length; i++) {
|
|
597
|
+
const code = codePointAt(str, i);
|
|
598
|
+
if (code > 65535) i++;
|
|
456
599
|
if (code < 128) {
|
|
457
|
-
out
|
|
600
|
+
out[at++] = code;
|
|
458
601
|
} else if (code < 2048) {
|
|
459
|
-
out
|
|
602
|
+
out[at++] = 192 | code >> 6;
|
|
603
|
+
out[at++] = 128 | code & 63;
|
|
460
604
|
} else if (code < 65536) {
|
|
461
|
-
out
|
|
605
|
+
out[at++] = 224 | code >> 12;
|
|
606
|
+
out[at++] = 128 | code >> 6 & 63;
|
|
607
|
+
out[at++] = 128 | code & 63;
|
|
462
608
|
} else {
|
|
463
|
-
out
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
128 | code & 63
|
|
468
|
-
);
|
|
609
|
+
out[at++] = 240 | code >> 18;
|
|
610
|
+
out[at++] = 128 | code >> 12 & 63;
|
|
611
|
+
out[at++] = 128 | code >> 6 & 63;
|
|
612
|
+
out[at++] = 128 | code & 63;
|
|
469
613
|
}
|
|
470
614
|
}
|
|
471
|
-
return
|
|
615
|
+
return out;
|
|
472
616
|
}
|
|
473
617
|
}
|
|
474
618
|
;
|
|
@@ -476,36 +620,14 @@
|
|
|
476
620
|
}
|
|
477
621
|
if (typeof g.TextDecoder !== "function") {
|
|
478
622
|
class TextDecoderPolyfill {
|
|
479
|
-
constructor(encoding = "utf-8") {
|
|
623
|
+
constructor(encoding = "utf-8", options) {
|
|
480
624
|
this.encoding = encoding;
|
|
625
|
+
this.fatal = options?.fatal === true;
|
|
626
|
+
this.ignoreBOM = options?.ignoreBOM === true;
|
|
481
627
|
}
|
|
482
628
|
decode(buffer) {
|
|
483
629
|
if (!buffer) return "";
|
|
484
|
-
|
|
485
|
-
let out = "";
|
|
486
|
-
let i = 0;
|
|
487
|
-
while (i < bytes.length) {
|
|
488
|
-
const b1 = bytes[i++];
|
|
489
|
-
let code;
|
|
490
|
-
if (b1 < 128) {
|
|
491
|
-
code = b1;
|
|
492
|
-
} else if (b1 < 192) {
|
|
493
|
-
code = 65533;
|
|
494
|
-
} else if (b1 < 224) {
|
|
495
|
-
code = (b1 & 31) << 6 | bytes[i++] & 63;
|
|
496
|
-
} else if (b1 < 240) {
|
|
497
|
-
code = (b1 & 15) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63;
|
|
498
|
-
} else {
|
|
499
|
-
code = (b1 & 7) << 18 | (bytes[i++] & 63) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63;
|
|
500
|
-
}
|
|
501
|
-
if (code > 65535) {
|
|
502
|
-
code -= 65536;
|
|
503
|
-
out += String.fromCharCode(55296 + (code >> 10), 56320 + (code & 1023));
|
|
504
|
-
} else {
|
|
505
|
-
out += String.fromCharCode(code);
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
return out;
|
|
630
|
+
return decodeUtf8(bytesOf(buffer), this.fatal, this.ignoreBOM);
|
|
509
631
|
}
|
|
510
632
|
}
|
|
511
633
|
;
|
|
@@ -552,9 +674,12 @@
|
|
|
552
674
|
const url = typeof input === "string" ? input : input.toString();
|
|
553
675
|
const method = (init?.method ?? "GET").toUpperCase();
|
|
554
676
|
let bodyString = null;
|
|
677
|
+
let bodyBase64;
|
|
555
678
|
const reqBody = init?.body;
|
|
556
679
|
if (typeof reqBody === "string") {
|
|
557
680
|
bodyString = reqBody;
|
|
681
|
+
} else if (reqBody instanceof ArrayBuffer || ArrayBuffer.isView(reqBody)) {
|
|
682
|
+
bodyBase64 = bytesToBase64(bytesOf(reqBody));
|
|
558
683
|
} else if (reqBody && typeof reqBody.toString === "function") {
|
|
559
684
|
bodyString = reqBody.toString();
|
|
560
685
|
}
|
|
@@ -578,9 +703,10 @@
|
|
|
578
703
|
}
|
|
579
704
|
const sendRequest = g.__mentraSendRequest;
|
|
580
705
|
const result = await sendRequest("fetch", "request", [
|
|
581
|
-
{ url, method, headers, body: bodyString }
|
|
706
|
+
bodyBase64 === void 0 ? { url, method, headers, body: bodyString } : { url, method, headers, bodyBase64 }
|
|
582
707
|
]);
|
|
583
|
-
const
|
|
708
|
+
const responseText = typeof result.body === "string" ? result.body : null;
|
|
709
|
+
const responseBase64 = typeof result.bodyBase64 === "string" ? result.bodyBase64 : "";
|
|
584
710
|
const responseHeaders = new Map(Object.entries(result.headers ?? {}));
|
|
585
711
|
class ResponseLike {
|
|
586
712
|
constructor() {
|
|
@@ -597,11 +723,13 @@
|
|
|
597
723
|
};
|
|
598
724
|
}
|
|
599
725
|
async text() {
|
|
600
|
-
return
|
|
726
|
+
if (responseText === null) return decodeUtf8(base64ToBytes(responseBase64), false, false);
|
|
727
|
+
return responseText.charCodeAt(0) === 65279 ? responseText.slice(1) : responseText;
|
|
601
728
|
}
|
|
602
729
|
async json() {
|
|
730
|
+
const text = await this.text();
|
|
603
731
|
try {
|
|
604
|
-
return JSON.parse(
|
|
732
|
+
return JSON.parse(text);
|
|
605
733
|
} catch (e) {
|
|
606
734
|
const err = new Error(
|
|
607
735
|
`Failed to parse JSON response from ${url}: ${e instanceof Error ? e.message : String(e)}`
|
|
@@ -611,52 +739,21 @@
|
|
|
611
739
|
}
|
|
612
740
|
}
|
|
613
741
|
async arrayBuffer() {
|
|
742
|
+
if (responseText === null) return base64ToBytes(responseBase64).buffer;
|
|
614
743
|
const enc = new g.TextEncoder();
|
|
615
|
-
return enc.encode(
|
|
744
|
+
return enc.encode(responseText).buffer;
|
|
616
745
|
}
|
|
617
746
|
}
|
|
618
747
|
return new ResponseLike();
|
|
619
748
|
};
|
|
620
749
|
}
|
|
621
750
|
if (typeof g.WebSocket !== "function") {
|
|
622
|
-
let
|
|
623
|
-
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
624
|
-
let out = "";
|
|
625
|
-
let i = 0;
|
|
626
|
-
while (i < bytes.length) {
|
|
627
|
-
const c1 = bytes[i++];
|
|
628
|
-
const c2 = i < bytes.length ? bytes[i++] : NaN;
|
|
629
|
-
const c3 = i < bytes.length ? bytes[i++] : NaN;
|
|
630
|
-
const e1 = c1 >> 2;
|
|
631
|
-
const e2 = (c1 & 3) << 4 | (Number.isNaN(c2) ? 0 : c2 >> 4);
|
|
632
|
-
const e3 = Number.isNaN(c2) ? 64 : (c2 & 15) << 2 | (Number.isNaN(c3) ? 0 : c3 >> 6);
|
|
633
|
-
const e4 = Number.isNaN(c3) ? 64 : c3 & 63;
|
|
634
|
-
out += b64[e1] + b64[e2] + (e3 === 64 ? "=" : b64[e3]) + (e4 === 64 ? "=" : b64[e4]);
|
|
635
|
-
}
|
|
636
|
-
return out;
|
|
637
|
-
}, base64ToBytes2 = function(s) {
|
|
638
|
-
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
639
|
-
const clean = s.replace(/=+$/, "");
|
|
640
|
-
const out = [];
|
|
641
|
-
let buf = 0;
|
|
642
|
-
let bits = 0;
|
|
643
|
-
for (let i = 0; i < clean.length; i++) {
|
|
644
|
-
const idx = b64.indexOf(clean[i]);
|
|
645
|
-
if (idx < 0) continue;
|
|
646
|
-
buf = buf << 6 | idx;
|
|
647
|
-
bits += 6;
|
|
648
|
-
if (bits >= 8) {
|
|
649
|
-
bits -= 8;
|
|
650
|
-
out.push(buf >> bits & 255);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
return new Uint8Array(out);
|
|
654
|
-
}, queueMicrotaskSafe2 = function(fn) {
|
|
751
|
+
let queueMicrotaskSafe2 = function(fn) {
|
|
655
752
|
const q = g.queueMicrotask;
|
|
656
753
|
if (typeof q === "function") q(fn);
|
|
657
754
|
else g.Promise.resolve().then(fn);
|
|
658
755
|
};
|
|
659
|
-
var
|
|
756
|
+
var queueMicrotaskSafe = queueMicrotaskSafe2;
|
|
660
757
|
const sockets = /* @__PURE__ */ new Map();
|
|
661
758
|
g.__mentraDeliverWebSocketEvent = (sid, type, payload) => {
|
|
662
759
|
const sock = sockets.get(sid);
|
|
@@ -718,8 +815,7 @@
|
|
|
718
815
|
payload = data;
|
|
719
816
|
} else {
|
|
720
817
|
kind = "binary";
|
|
721
|
-
|
|
722
|
-
payload = bytesToBase642(bytes);
|
|
818
|
+
payload = bytesToBase64(bytesOf(data));
|
|
723
819
|
}
|
|
724
820
|
try {
|
|
725
821
|
__dispatch("ws", "send", JSON.stringify([{ sid: this.sid, kind, payload }]));
|
|
@@ -752,7 +848,7 @@
|
|
|
752
848
|
}
|
|
753
849
|
const synth = { type, target: this, ...ev };
|
|
754
850
|
if (type === "message" && ev.kind === "binary" && typeof ev.data === "string") {
|
|
755
|
-
synth.data =
|
|
851
|
+
synth.data = base64ToBytes(ev.data).buffer;
|
|
756
852
|
} else if (type === "message" && ev.kind === "text") {
|
|
757
853
|
synth.data = ev.data;
|
|
758
854
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mentra/jspolyfill",
|
|
3
|
-
"version": "3.3.0
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "MentraJS polyfill bundle — installed into each per-miniapp JSContext (iOS-JSC, Android-QuickJS) at spawn time. Provides browser-like globals (console, timers, fetch, WebSocket, localStorage, crypto.subtle) over a single __dispatch native bridge, plus the __deliver / signalReady plumbing the host runtime relies on.",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
package/src/startup.ts
CHANGED
|
@@ -598,41 +598,216 @@ declare const __nativeClearTimer: (token: number) => void
|
|
|
598
598
|
}
|
|
599
599
|
;(g as Record<string, unknown>).crypto = cryptoNs
|
|
600
600
|
|
|
601
|
+
// ---------- bytes over the JSON bridge -----------------------------------
|
|
602
|
+
// __dispatch and __deliver carry JSON text only, so binary data (fetch
|
|
603
|
+
// bodies that are not UTF-8 text, WebSocket binary frames) crosses as
|
|
604
|
+
// base64 and is decoded here.
|
|
605
|
+
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
606
|
+
// Values 0-63; 0xff marks a character outside the alphabet.
|
|
607
|
+
const BASE64_VALUES = new Uint8Array(128).fill(0xff)
|
|
608
|
+
for (let i = 0; i < BASE64_ALPHABET.length; i++) BASE64_VALUES[BASE64_ALPHABET.charCodeAt(i)] = i
|
|
609
|
+
// String.fromCharCode.apply takes one argument per code unit and engines
|
|
610
|
+
// cap argument counts near 64K, so strings are built from bounded chunks.
|
|
611
|
+
const CHARS_PER_CHUNK = 8192
|
|
612
|
+
|
|
613
|
+
function bytesOf(data: ArrayBuffer | ArrayBufferView): Uint8Array {
|
|
614
|
+
return data instanceof ArrayBuffer
|
|
615
|
+
? new Uint8Array(data)
|
|
616
|
+
: new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
function bytesToBase64(bytes: Uint8Array): string {
|
|
620
|
+
const parts: string[] = []
|
|
621
|
+
const chars: number[] = []
|
|
622
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
623
|
+
const b1 = bytes[i]!
|
|
624
|
+
const b2 = i + 1 < bytes.length ? bytes[i + 1]! : 0
|
|
625
|
+
const b3 = i + 2 < bytes.length ? bytes[i + 2]! : 0
|
|
626
|
+
chars.push(
|
|
627
|
+
BASE64_ALPHABET.charCodeAt(b1 >> 2),
|
|
628
|
+
BASE64_ALPHABET.charCodeAt(((b1 & 3) << 4) | (b2 >> 4)),
|
|
629
|
+
i + 1 < bytes.length ? BASE64_ALPHABET.charCodeAt(((b2 & 15) << 2) | (b3 >> 6)) : 0x3d,
|
|
630
|
+
i + 2 < bytes.length ? BASE64_ALPHABET.charCodeAt(b3 & 63) : 0x3d,
|
|
631
|
+
)
|
|
632
|
+
if (chars.length >= CHARS_PER_CHUNK) {
|
|
633
|
+
parts.push(String.fromCharCode.apply(null, chars))
|
|
634
|
+
chars.length = 0
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
parts.push(String.fromCharCode.apply(null, chars))
|
|
638
|
+
return parts.join("")
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** Strict: the native bridges always emit padded RFC 4648 base64. */
|
|
642
|
+
function base64ToBytes(base64: string): Uint8Array {
|
|
643
|
+
const length = base64.length
|
|
644
|
+
if (length % 4 !== 0) throw new TypeError("Invalid base64 from the native bridge")
|
|
645
|
+
const padding =
|
|
646
|
+
length === 0 || base64.charCodeAt(length - 1) !== 0x3d ? 0 : base64.charCodeAt(length - 2) === 0x3d ? 2 : 1
|
|
647
|
+
const out = new Uint8Array((length / 4) * 3 - padding)
|
|
648
|
+
// Groups before the padded one decode without per-character branches;
|
|
649
|
+
// the accumulated codes and values are checked once at the end.
|
|
650
|
+
const unpadded = padding === 0 ? length : length - 4
|
|
651
|
+
let codes = 0
|
|
652
|
+
let values = 0
|
|
653
|
+
let o = 0
|
|
654
|
+
for (let i = 0; i < unpadded; i += 4) {
|
|
655
|
+
const c1 = base64.charCodeAt(i)
|
|
656
|
+
const c2 = base64.charCodeAt(i + 1)
|
|
657
|
+
const c3 = base64.charCodeAt(i + 2)
|
|
658
|
+
const c4 = base64.charCodeAt(i + 3)
|
|
659
|
+
const v1 = BASE64_VALUES[c1 & 0x7f]!
|
|
660
|
+
const v2 = BASE64_VALUES[c2 & 0x7f]!
|
|
661
|
+
const v3 = BASE64_VALUES[c3 & 0x7f]!
|
|
662
|
+
const v4 = BASE64_VALUES[c4 & 0x7f]!
|
|
663
|
+
codes |= c1 | c2 | c3 | c4
|
|
664
|
+
values |= v1 | v2 | v3 | v4
|
|
665
|
+
out[o++] = (v1 << 2) | (v2 >> 4)
|
|
666
|
+
out[o++] = ((v2 & 15) << 4) | (v3 >> 2)
|
|
667
|
+
out[o++] = ((v3 & 3) << 6) | v4
|
|
668
|
+
}
|
|
669
|
+
if (padding > 0) {
|
|
670
|
+
const c1 = base64.charCodeAt(unpadded)
|
|
671
|
+
const c2 = base64.charCodeAt(unpadded + 1)
|
|
672
|
+
const c3 = padding === 1 ? base64.charCodeAt(unpadded + 2) : 0x41
|
|
673
|
+
const v1 = BASE64_VALUES[c1 & 0x7f]!
|
|
674
|
+
const v2 = BASE64_VALUES[c2 & 0x7f]!
|
|
675
|
+
const v3 = BASE64_VALUES[c3 & 0x7f]!
|
|
676
|
+
codes |= c1 | c2 | c3
|
|
677
|
+
values |= v1 | v2 | v3
|
|
678
|
+
out[o++] = (v1 << 2) | (v2 >> 4)
|
|
679
|
+
if (padding === 1) out[o++] = ((v2 & 15) << 4) | (v3 >> 2)
|
|
680
|
+
}
|
|
681
|
+
// A non-ASCII code, or a character outside the alphabet (value 0xff).
|
|
682
|
+
if (codes > 0x7f || values > 0x3f) throw new TypeError("Invalid base64 from the native bridge")
|
|
683
|
+
return out
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* The WHATWG Encoding Standard's UTF-8 decoder: each maximal malformed
|
|
688
|
+
* subsequence becomes one U+FFFD, or throws when `fatal` is set. Runs of
|
|
689
|
+
* ASCII, most of any JSON body, are copied straight from the bytes.
|
|
690
|
+
*/
|
|
691
|
+
function decodeUtf8(bytes: Uint8Array, fatal: boolean, ignoreBOM: boolean): string {
|
|
692
|
+
const length = bytes.length
|
|
693
|
+
const parts: string[] = []
|
|
694
|
+
const units: number[] = []
|
|
695
|
+
let i = !ignoreBOM && length >= 3 && bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf ? 3 : 0
|
|
696
|
+
while (i < length) {
|
|
697
|
+
const asciiStart = i
|
|
698
|
+
while (i < length && bytes[i]! < 0x80) i++
|
|
699
|
+
if (i > asciiStart) {
|
|
700
|
+
if (units.length > 0) {
|
|
701
|
+
parts.push(String.fromCharCode.apply(null, units))
|
|
702
|
+
units.length = 0
|
|
703
|
+
}
|
|
704
|
+
for (let start = asciiStart; start < i; start += CHARS_PER_CHUNK) {
|
|
705
|
+
const chunk = bytes.subarray(start, Math.min(i, start + CHARS_PER_CHUNK))
|
|
706
|
+
parts.push(String.fromCharCode.apply(null, chunk as unknown as number[]))
|
|
707
|
+
}
|
|
708
|
+
if (i >= length) break
|
|
709
|
+
}
|
|
710
|
+
const lead = bytes[i++]!
|
|
711
|
+
let codePoint: number
|
|
712
|
+
let needed: number
|
|
713
|
+
let lower = 0x80
|
|
714
|
+
let upper = 0xbf
|
|
715
|
+
if (lead >= 0xc2 && lead <= 0xdf) {
|
|
716
|
+
needed = 1
|
|
717
|
+
codePoint = lead & 0x1f
|
|
718
|
+
} else if (lead >= 0xe0 && lead <= 0xef) {
|
|
719
|
+
if (lead === 0xe0) lower = 0xa0
|
|
720
|
+
if (lead === 0xed) upper = 0x9f
|
|
721
|
+
needed = 2
|
|
722
|
+
codePoint = lead & 0x0f
|
|
723
|
+
} else if (lead >= 0xf0 && lead <= 0xf4) {
|
|
724
|
+
if (lead === 0xf0) lower = 0x90
|
|
725
|
+
if (lead === 0xf4) upper = 0x8f
|
|
726
|
+
needed = 3
|
|
727
|
+
codePoint = lead & 0x07
|
|
728
|
+
} else {
|
|
729
|
+
needed = -1
|
|
730
|
+
codePoint = 0
|
|
731
|
+
}
|
|
732
|
+
while (needed > 0) {
|
|
733
|
+
const next = i < length ? bytes[i]! : -1
|
|
734
|
+
// A byte outside the allowed range ends the sequence unconsumed, so
|
|
735
|
+
// it is decoded again as the start of the next one.
|
|
736
|
+
if (next < lower || next > upper) break
|
|
737
|
+
codePoint = (codePoint << 6) | (next & 0x3f)
|
|
738
|
+
lower = 0x80
|
|
739
|
+
upper = 0xbf
|
|
740
|
+
needed--
|
|
741
|
+
i++
|
|
742
|
+
}
|
|
743
|
+
if (needed !== 0) {
|
|
744
|
+
if (fatal) throw new TypeError("The encoded data was not valid utf-8")
|
|
745
|
+
units.push(0xfffd)
|
|
746
|
+
} else if (codePoint > 0xffff) {
|
|
747
|
+
codePoint -= 0x10000
|
|
748
|
+
units.push(0xd800 + (codePoint >> 10), 0xdc00 + (codePoint & 0x3ff))
|
|
749
|
+
} else {
|
|
750
|
+
units.push(codePoint)
|
|
751
|
+
}
|
|
752
|
+
if (units.length >= CHARS_PER_CHUNK) {
|
|
753
|
+
parts.push(String.fromCharCode.apply(null, units))
|
|
754
|
+
units.length = 0
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
if (units.length > 0) parts.push(String.fromCharCode.apply(null, units))
|
|
758
|
+
return parts.join("")
|
|
759
|
+
}
|
|
760
|
+
|
|
601
761
|
// ---------- TextEncoder / TextDecoder ------------------------------------
|
|
602
762
|
// Both engines lack these. Tiny implementations sufficient for the SDK's
|
|
603
763
|
// existing usage (UTF-8 only; we don't expose stream encoding because the
|
|
604
764
|
// miniapp doesn't need it).
|
|
605
765
|
if (typeof (g as Record<string, unknown>).TextEncoder !== "function") {
|
|
766
|
+
// The code point starting at `i`: a surrogate pair combines, and a lone
|
|
767
|
+
// surrogate stays as its own code unit.
|
|
768
|
+
const codePointAt = (str: string, i: number): number => {
|
|
769
|
+
const code = str.charCodeAt(i)
|
|
770
|
+
if (code >= 0xd800 && code <= 0xdbff && i + 1 < str.length) {
|
|
771
|
+
const next = str.charCodeAt(i + 1)
|
|
772
|
+
if (next >= 0xdc00 && next <= 0xdfff) return ((code - 0xd800) << 10) + (next - 0xdc00) + 0x10000
|
|
773
|
+
}
|
|
774
|
+
return code
|
|
775
|
+
}
|
|
606
776
|
class TextEncoderPolyfill {
|
|
607
777
|
readonly encoding = "utf-8"
|
|
608
778
|
encode(input?: string): Uint8Array {
|
|
609
779
|
const str = input ?? ""
|
|
610
|
-
|
|
780
|
+
// Size the output first and write bytes straight into it. A number[]
|
|
781
|
+
// costs a full JS value per byte, so fetch().arrayBuffer() on a
|
|
782
|
+
// two-megabyte text body exhausted the 32 MB miniapp heap.
|
|
783
|
+
let length = 0
|
|
611
784
|
for (let i = 0; i < str.length; i++) {
|
|
612
|
-
|
|
613
|
-
if (code
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
785
|
+
const code = codePointAt(str, i)
|
|
786
|
+
if (code > 0xffff) i++
|
|
787
|
+
length += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4
|
|
788
|
+
}
|
|
789
|
+
const out = new Uint8Array(length)
|
|
790
|
+
let at = 0
|
|
791
|
+
for (let i = 0; i < str.length; i++) {
|
|
792
|
+
const code = codePointAt(str, i)
|
|
793
|
+
if (code > 0xffff) i++
|
|
620
794
|
if (code < 0x80) {
|
|
621
|
-
out
|
|
795
|
+
out[at++] = code
|
|
622
796
|
} else if (code < 0x800) {
|
|
623
|
-
out
|
|
797
|
+
out[at++] = 0xc0 | (code >> 6)
|
|
798
|
+
out[at++] = 0x80 | (code & 0x3f)
|
|
624
799
|
} else if (code < 0x10000) {
|
|
625
|
-
out
|
|
800
|
+
out[at++] = 0xe0 | (code >> 12)
|
|
801
|
+
out[at++] = 0x80 | ((code >> 6) & 0x3f)
|
|
802
|
+
out[at++] = 0x80 | (code & 0x3f)
|
|
626
803
|
} else {
|
|
627
|
-
out
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
0x80 | (code & 0x3f),
|
|
632
|
-
)
|
|
804
|
+
out[at++] = 0xf0 | (code >> 18)
|
|
805
|
+
out[at++] = 0x80 | ((code >> 12) & 0x3f)
|
|
806
|
+
out[at++] = 0x80 | ((code >> 6) & 0x3f)
|
|
807
|
+
out[at++] = 0x80 | (code & 0x3f)
|
|
633
808
|
}
|
|
634
809
|
}
|
|
635
|
-
return
|
|
810
|
+
return out
|
|
636
811
|
}
|
|
637
812
|
}
|
|
638
813
|
;(g as Record<string, unknown>).TextEncoder = TextEncoderPolyfill
|
|
@@ -640,46 +815,16 @@ declare const __nativeClearTimer: (token: number) => void
|
|
|
640
815
|
if (typeof (g as Record<string, unknown>).TextDecoder !== "function") {
|
|
641
816
|
class TextDecoderPolyfill {
|
|
642
817
|
readonly encoding: string
|
|
643
|
-
|
|
818
|
+
readonly fatal: boolean
|
|
819
|
+
readonly ignoreBOM: boolean
|
|
820
|
+
constructor(encoding = "utf-8", options?: {fatal?: boolean; ignoreBOM?: boolean}) {
|
|
644
821
|
this.encoding = encoding
|
|
822
|
+
this.fatal = options?.fatal === true
|
|
823
|
+
this.ignoreBOM = options?.ignoreBOM === true
|
|
645
824
|
}
|
|
646
825
|
decode(buffer?: ArrayBuffer | ArrayBufferView | null): string {
|
|
647
826
|
if (!buffer) return ""
|
|
648
|
-
|
|
649
|
-
buffer instanceof Uint8Array
|
|
650
|
-
? buffer
|
|
651
|
-
: buffer instanceof ArrayBuffer
|
|
652
|
-
? new Uint8Array(buffer)
|
|
653
|
-
: new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
|
|
654
|
-
let out = ""
|
|
655
|
-
let i = 0
|
|
656
|
-
while (i < bytes.length) {
|
|
657
|
-
const b1 = bytes[i++]!
|
|
658
|
-
let code: number
|
|
659
|
-
if (b1 < 0x80) {
|
|
660
|
-
code = b1
|
|
661
|
-
} else if (b1 < 0xc0) {
|
|
662
|
-
// Invalid start byte; emit replacement character.
|
|
663
|
-
code = 0xfffd
|
|
664
|
-
} else if (b1 < 0xe0) {
|
|
665
|
-
code = ((b1 & 0x1f) << 6) | (bytes[i++]! & 0x3f)
|
|
666
|
-
} else if (b1 < 0xf0) {
|
|
667
|
-
code = ((b1 & 0x0f) << 12) | ((bytes[i++]! & 0x3f) << 6) | (bytes[i++]! & 0x3f)
|
|
668
|
-
} else {
|
|
669
|
-
code =
|
|
670
|
-
((b1 & 0x07) << 18) |
|
|
671
|
-
((bytes[i++]! & 0x3f) << 12) |
|
|
672
|
-
((bytes[i++]! & 0x3f) << 6) |
|
|
673
|
-
(bytes[i++]! & 0x3f)
|
|
674
|
-
}
|
|
675
|
-
if (code > 0xffff) {
|
|
676
|
-
code -= 0x10000
|
|
677
|
-
out += String.fromCharCode(0xd800 + (code >> 10), 0xdc00 + (code & 0x3ff))
|
|
678
|
-
} else {
|
|
679
|
-
out += String.fromCharCode(code)
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
return out
|
|
827
|
+
return decodeUtf8(bytesOf(buffer), this.fatal, this.ignoreBOM)
|
|
683
828
|
}
|
|
684
829
|
}
|
|
685
830
|
;(g as Record<string, unknown>).TextDecoder = TextDecoderPolyfill
|
|
@@ -723,17 +868,26 @@ declare const __nativeClearTimer: (token: number) => void
|
|
|
723
868
|
}
|
|
724
869
|
|
|
725
870
|
// ---------- fetch (thin native bridge) -----------------------------------
|
|
726
|
-
// Async — uses __mentraSendRequest under the hood with iface "fetch".
|
|
727
|
-
//
|
|
728
|
-
//
|
|
871
|
+
// Async — uses __mentraSendRequest under the hood with iface "fetch".
|
|
872
|
+
// Bodies cross in either direction as `body`, a string whose UTF-8
|
|
873
|
+
// encoding is exactly the body, or `bodyBase64`, the bytes in base64:
|
|
874
|
+
// request {url, method, headers, body | bodyBase64}
|
|
875
|
+
// response {status, statusText, headers, ok, body | bodyBase64}
|
|
876
|
+
// Requests use bodyBase64 for ArrayBuffer and typed-array bodies; native
|
|
877
|
+
// responds with bodyBase64 whenever the bytes are not exactly UTF-8 text.
|
|
878
|
+
// Either way arrayBuffer() is exact and text() follows the Fetch
|
|
879
|
+
// Standard's UTF-8 decode.
|
|
729
880
|
if (typeof (g as Record<string, unknown>).fetch !== "function") {
|
|
730
881
|
;(g as Record<string, unknown>).fetch = async (input: string | URL, init?: RequestInit) => {
|
|
731
882
|
const url = typeof input === "string" ? input : input.toString()
|
|
732
883
|
const method = (init?.method ?? "GET").toUpperCase()
|
|
733
884
|
let bodyString: string | null = null
|
|
885
|
+
let bodyBase64: string | undefined
|
|
734
886
|
const reqBody = init?.body as unknown
|
|
735
887
|
if (typeof reqBody === "string") {
|
|
736
888
|
bodyString = reqBody
|
|
889
|
+
} else if (reqBody instanceof ArrayBuffer || ArrayBuffer.isView(reqBody)) {
|
|
890
|
+
bodyBase64 = bytesToBase64(bytesOf(reqBody))
|
|
737
891
|
} else if (reqBody && typeof (reqBody as {toString?: () => string}).toString === "function") {
|
|
738
892
|
bodyString = (reqBody as {toString: () => string}).toString()
|
|
739
893
|
}
|
|
@@ -761,15 +915,17 @@ declare const __nativeClearTimer: (token: number) => void
|
|
|
761
915
|
args: unknown[],
|
|
762
916
|
) => Promise<unknown>
|
|
763
917
|
const result = (await sendRequest("fetch", "request", [
|
|
764
|
-
{url, method, headers, body: bodyString},
|
|
918
|
+
bodyBase64 === undefined ? {url, method, headers, body: bodyString} : {url, method, headers, bodyBase64},
|
|
765
919
|
])) as {
|
|
766
920
|
status: number
|
|
767
921
|
statusText?: string
|
|
768
922
|
headers?: Record<string, string>
|
|
769
|
-
body?: string
|
|
923
|
+
body?: string
|
|
924
|
+
bodyBase64?: string
|
|
770
925
|
ok?: boolean
|
|
771
926
|
}
|
|
772
|
-
const
|
|
927
|
+
const responseText = typeof result.body === "string" ? result.body : null
|
|
928
|
+
const responseBase64 = typeof result.bodyBase64 === "string" ? result.bodyBase64 : ""
|
|
773
929
|
const responseHeaders = new Map(Object.entries(result.headers ?? {}))
|
|
774
930
|
// Minimal Response polyfill — enough for SDK usage.
|
|
775
931
|
class ResponseLike {
|
|
@@ -785,11 +941,14 @@ declare const __nativeClearTimer: (token: number) => void
|
|
|
785
941
|
},
|
|
786
942
|
}
|
|
787
943
|
async text(): Promise<string> {
|
|
788
|
-
return
|
|
944
|
+
if (responseText === null) return decodeUtf8(base64ToBytes(responseBase64), false, false)
|
|
945
|
+
// UTF-8 decode drops a leading byte order mark.
|
|
946
|
+
return responseText.charCodeAt(0) === 0xfeff ? responseText.slice(1) : responseText
|
|
789
947
|
}
|
|
790
948
|
async json(): Promise<unknown> {
|
|
949
|
+
const text = await this.text()
|
|
791
950
|
try {
|
|
792
|
-
return JSON.parse(
|
|
951
|
+
return JSON.parse(text)
|
|
793
952
|
} catch (e) {
|
|
794
953
|
// Match the browser fetch().json() behaviour — a SyntaxError
|
|
795
954
|
// is thrown that names the response in its message so the
|
|
@@ -804,8 +963,10 @@ declare const __nativeClearTimer: (token: number) => void
|
|
|
804
963
|
}
|
|
805
964
|
}
|
|
806
965
|
async arrayBuffer(): Promise<ArrayBuffer> {
|
|
966
|
+
if (responseText === null) return base64ToBytes(responseBase64).buffer as ArrayBuffer
|
|
967
|
+
// Exact: native sends `body` only when this re-encoding is the body.
|
|
807
968
|
const enc = new (g as unknown as {TextEncoder: new () => TextEncoder}).TextEncoder()
|
|
808
|
-
return enc.encode(
|
|
969
|
+
return enc.encode(responseText).buffer as ArrayBuffer
|
|
809
970
|
}
|
|
810
971
|
}
|
|
811
972
|
return new ResponseLike()
|
|
@@ -910,11 +1071,7 @@ declare const __nativeClearTimer: (token: number) => void
|
|
|
910
1071
|
payload = data
|
|
911
1072
|
} else {
|
|
912
1073
|
kind = "binary"
|
|
913
|
-
|
|
914
|
-
data instanceof ArrayBuffer
|
|
915
|
-
? new Uint8Array(data)
|
|
916
|
-
: new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
|
|
917
|
-
payload = bytesToBase64(bytes)
|
|
1074
|
+
payload = bytesToBase64(bytesOf(data))
|
|
918
1075
|
}
|
|
919
1076
|
// bufferedAmount is intentionally a static 0 — native owns the
|
|
920
1077
|
// real queue (URLSessionWebSocketTask / OkHttp); reading it
|
|
@@ -1006,40 +1163,6 @@ declare const __nativeClearTimer: (token: number) => void
|
|
|
1006
1163
|
}
|
|
1007
1164
|
}
|
|
1008
1165
|
|
|
1009
|
-
function bytesToBase64(bytes: Uint8Array): string {
|
|
1010
|
-
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
1011
|
-
let out = ""
|
|
1012
|
-
let i = 0
|
|
1013
|
-
while (i < bytes.length) {
|
|
1014
|
-
const c1 = bytes[i++]!
|
|
1015
|
-
const c2 = i < bytes.length ? bytes[i++]! : NaN
|
|
1016
|
-
const c3 = i < bytes.length ? bytes[i++]! : NaN
|
|
1017
|
-
const e1 = c1 >> 2
|
|
1018
|
-
const e2 = ((c1 & 3) << 4) | (Number.isNaN(c2) ? 0 : c2 >> 4)
|
|
1019
|
-
const e3 = Number.isNaN(c2) ? 64 : ((c2 & 15) << 2) | (Number.isNaN(c3) ? 0 : c3 >> 6)
|
|
1020
|
-
const e4 = Number.isNaN(c3) ? 64 : c3 & 63
|
|
1021
|
-
out += b64[e1]! + b64[e2]! + (e3 === 64 ? "=" : b64[e3]!) + (e4 === 64 ? "=" : b64[e4]!)
|
|
1022
|
-
}
|
|
1023
|
-
return out
|
|
1024
|
-
}
|
|
1025
|
-
function base64ToBytes(s: string): Uint8Array {
|
|
1026
|
-
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
1027
|
-
const clean = s.replace(/=+$/, "")
|
|
1028
|
-
const out: number[] = []
|
|
1029
|
-
let buf = 0
|
|
1030
|
-
let bits = 0
|
|
1031
|
-
for (let i = 0; i < clean.length; i++) {
|
|
1032
|
-
const idx = b64.indexOf(clean[i]!)
|
|
1033
|
-
if (idx < 0) continue
|
|
1034
|
-
buf = (buf << 6) | idx
|
|
1035
|
-
bits += 6
|
|
1036
|
-
if (bits >= 8) {
|
|
1037
|
-
bits -= 8
|
|
1038
|
-
out.push((buf >> bits) & 0xff)
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
return new Uint8Array(out)
|
|
1042
|
-
}
|
|
1043
1166
|
function queueMicrotaskSafe(fn: () => void): void {
|
|
1044
1167
|
const q = (g as Record<string, unknown>).queueMicrotask as ((cb: () => void) => void) | undefined
|
|
1045
1168
|
if (typeof q === "function") q(fn)
|