@mentra/jspolyfill 3.3.0-beta.1005 → 3.3.0-beta.1012

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 CHANGED
@@ -571,38 +571,48 @@
571
571
  return parts.join("");
572
572
  }
573
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
+ };
574
582
  class TextEncoderPolyfill {
575
583
  constructor() {
576
584
  this.encoding = "utf-8";
577
585
  }
578
586
  encode(input) {
579
587
  const str = input ?? "";
580
- const out = [];
588
+ let length = 0;
581
589
  for (let i = 0; i < str.length; i++) {
582
- let code = str.charCodeAt(i);
583
- if (code >= 55296 && code <= 56319 && i + 1 < str.length) {
584
- const next = str.charCodeAt(i + 1);
585
- if (next >= 56320 && next <= 57343) {
586
- code = (code - 55296 << 10) + (next - 56320) + 65536;
587
- i++;
588
- }
589
- }
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++;
590
599
  if (code < 128) {
591
- out.push(code);
600
+ out[at++] = code;
592
601
  } else if (code < 2048) {
593
- out.push(192 | code >> 6, 128 | code & 63);
602
+ out[at++] = 192 | code >> 6;
603
+ out[at++] = 128 | code & 63;
594
604
  } else if (code < 65536) {
595
- out.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
605
+ out[at++] = 224 | code >> 12;
606
+ out[at++] = 128 | code >> 6 & 63;
607
+ out[at++] = 128 | code & 63;
596
608
  } else {
597
- out.push(
598
- 240 | code >> 18,
599
- 128 | code >> 12 & 63,
600
- 128 | code >> 6 & 63,
601
- 128 | code & 63
602
- );
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;
603
613
  }
604
614
  }
605
- return new Uint8Array(out);
615
+ return out;
606
616
  }
607
617
  }
608
618
  ;
package/dist/startup.js CHANGED
@@ -571,38 +571,48 @@
571
571
  return parts.join("");
572
572
  }
573
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
+ };
574
582
  class TextEncoderPolyfill {
575
583
  constructor() {
576
584
  this.encoding = "utf-8";
577
585
  }
578
586
  encode(input) {
579
587
  const str = input ?? "";
580
- const out = [];
588
+ let length = 0;
581
589
  for (let i = 0; i < str.length; i++) {
582
- let code = str.charCodeAt(i);
583
- if (code >= 55296 && code <= 56319 && i + 1 < str.length) {
584
- const next = str.charCodeAt(i + 1);
585
- if (next >= 56320 && next <= 57343) {
586
- code = (code - 55296 << 10) + (next - 56320) + 65536;
587
- i++;
588
- }
589
- }
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++;
590
599
  if (code < 128) {
591
- out.push(code);
600
+ out[at++] = code;
592
601
  } else if (code < 2048) {
593
- out.push(192 | code >> 6, 128 | code & 63);
602
+ out[at++] = 192 | code >> 6;
603
+ out[at++] = 128 | code & 63;
594
604
  } else if (code < 65536) {
595
- out.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
605
+ out[at++] = 224 | code >> 12;
606
+ out[at++] = 128 | code >> 6 & 63;
607
+ out[at++] = 128 | code & 63;
596
608
  } else {
597
- out.push(
598
- 240 | code >> 18,
599
- 128 | code >> 12 & 63,
600
- 128 | code >> 6 & 63,
601
- 128 | code & 63
602
- );
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;
603
613
  }
604
614
  }
605
- return new Uint8Array(out);
615
+ return out;
606
616
  }
607
617
  }
608
618
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/jspolyfill",
3
- "version": "3.3.0-beta.1005",
3
+ "version": "3.3.0-beta.1012",
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
@@ -763,36 +763,51 @@ declare const __nativeClearTimer: (token: number) => void
763
763
  // existing usage (UTF-8 only; we don't expose stream encoding because the
764
764
  // miniapp doesn't need it).
765
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
+ }
766
776
  class TextEncoderPolyfill {
767
777
  readonly encoding = "utf-8"
768
778
  encode(input?: string): Uint8Array {
769
779
  const str = input ?? ""
770
- const out: number[] = []
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
771
784
  for (let i = 0; i < str.length; i++) {
772
- let code = str.charCodeAt(i)
773
- if (code >= 0xd800 && code <= 0xdbff && i + 1 < str.length) {
774
- const next = str.charCodeAt(i + 1)
775
- if (next >= 0xdc00 && next <= 0xdfff) {
776
- code = ((code - 0xd800) << 10) + (next - 0xdc00) + 0x10000
777
- i++
778
- }
779
- }
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++
780
794
  if (code < 0x80) {
781
- out.push(code)
795
+ out[at++] = code
782
796
  } else if (code < 0x800) {
783
- out.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f))
797
+ out[at++] = 0xc0 | (code >> 6)
798
+ out[at++] = 0x80 | (code & 0x3f)
784
799
  } else if (code < 0x10000) {
785
- out.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f))
800
+ out[at++] = 0xe0 | (code >> 12)
801
+ out[at++] = 0x80 | ((code >> 6) & 0x3f)
802
+ out[at++] = 0x80 | (code & 0x3f)
786
803
  } else {
787
- out.push(
788
- 0xf0 | (code >> 18),
789
- 0x80 | ((code >> 12) & 0x3f),
790
- 0x80 | ((code >> 6) & 0x3f),
791
- 0x80 | (code & 0x3f),
792
- )
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)
793
808
  }
794
809
  }
795
- return new Uint8Array(out)
810
+ return out
796
811
  }
797
812
  }
798
813
  ;(g as Record<string, unknown>).TextEncoder = TextEncoderPolyfill