@mentra/jspolyfill 3.4.0-dev.998 → 3.5.0-dev.1238

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
  ;
@@ -658,10 +668,20 @@
658
668
  return out;
659
669
  };
660
670
  }
671
+ const urlScheme = (url) => {
672
+ const match = /^[\u0000- ]*([A-Za-z][A-Za-z0-9+.-]*):/.exec(url);
673
+ return match ? match[1].toLowerCase() : null;
674
+ };
661
675
  if (typeof g.fetch !== "function") {
662
676
  ;
663
677
  g.fetch = async (input, init) => {
664
678
  const url = typeof input === "string" ? input : input.toString();
679
+ const scheme = urlScheme(url);
680
+ if (scheme !== "http" && scheme !== "https") {
681
+ throw new TypeError(
682
+ `fetch: only http: and https: URLs can be fetched, not ${scheme ? `${scheme}:` : "a relative URL"}`
683
+ );
684
+ }
665
685
  const method = (init?.method ?? "GET").toUpperCase();
666
686
  let bodyString = null;
667
687
  let bodyBase64;
@@ -772,6 +792,12 @@
772
792
  close: /* @__PURE__ */ new Set()
773
793
  };
774
794
  this.url = String(url);
795
+ const scheme = urlScheme(this.url);
796
+ if (scheme !== "ws" && scheme !== "wss" && scheme !== "http" && scheme !== "https") {
797
+ throw new SyntaxError(
798
+ `WebSocket: the URL's scheme must be ws:, wss:, http: or https:, not ${scheme ? `${scheme}:` : "none"}`
799
+ );
800
+ }
775
801
  const protoList = Array.isArray(protocols) ? protocols : protocols ? [protocols] : [];
776
802
  this.sid = `ws-${Date.now()}-${Math.floor(Math.random() * 1e9)}`;
777
803
  sockets.set(this.sid, this);
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
  ;
@@ -658,10 +668,20 @@
658
668
  return out;
659
669
  };
660
670
  }
671
+ const urlScheme = (url) => {
672
+ const match = /^[\u0000- ]*([A-Za-z][A-Za-z0-9+.-]*):/.exec(url);
673
+ return match ? match[1].toLowerCase() : null;
674
+ };
661
675
  if (typeof g.fetch !== "function") {
662
676
  ;
663
677
  g.fetch = async (input, init) => {
664
678
  const url = typeof input === "string" ? input : input.toString();
679
+ const scheme = urlScheme(url);
680
+ if (scheme !== "http" && scheme !== "https") {
681
+ throw new TypeError(
682
+ `fetch: only http: and https: URLs can be fetched, not ${scheme ? `${scheme}:` : "a relative URL"}`
683
+ );
684
+ }
665
685
  const method = (init?.method ?? "GET").toUpperCase();
666
686
  let bodyString = null;
667
687
  let bodyBase64;
@@ -772,6 +792,12 @@
772
792
  close: /* @__PURE__ */ new Set()
773
793
  };
774
794
  this.url = String(url);
795
+ const scheme = urlScheme(this.url);
796
+ if (scheme !== "ws" && scheme !== "wss" && scheme !== "http" && scheme !== "https") {
797
+ throw new SyntaxError(
798
+ `WebSocket: the URL's scheme must be ws:, wss:, http: or https:, not ${scheme ? `${scheme}:` : "none"}`
799
+ );
800
+ }
775
801
  const protoList = Array.isArray(protocols) ? protocols : protocols ? [protocols] : [];
776
802
  this.sid = `ws-${Date.now()}-${Math.floor(Math.random() * 1e9)}`;
777
803
  sockets.set(this.sid, this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/jspolyfill",
3
- "version": "3.4.0-dev.998",
3
+ "version": "3.5.0-dev.1238",
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
@@ -862,9 +877,24 @@ declare const __nativeClearTimer: (token: number) => void
862
877
  // responds with bodyBase64 whenever the bytes are not exactly UTF-8 text.
863
878
  // Either way arrayBuffer() is exact and text() follows the Fetch
864
879
  // Standard's UTF-8 decode.
880
+ //
881
+ // Only http: and https: URLs are fetched. Miniapp code is untrusted, and a
882
+ // file: URL would read the Mentra App's own files. Native refuses them too;
883
+ // this check gives the browser's TypeError instead of a bridge error.
884
+ // Leading C0 controls and spaces are skipped, as a URL parser does.
885
+ const urlScheme = (url: string): string | null => {
886
+ const match = /^[\u0000- ]*([A-Za-z][A-Za-z0-9+.-]*):/.exec(url)
887
+ return match ? match[1].toLowerCase() : null
888
+ }
865
889
  if (typeof (g as Record<string, unknown>).fetch !== "function") {
866
890
  ;(g as Record<string, unknown>).fetch = async (input: string | URL, init?: RequestInit) => {
867
891
  const url = typeof input === "string" ? input : input.toString()
892
+ const scheme = urlScheme(url)
893
+ if (scheme !== "http" && scheme !== "https") {
894
+ throw new TypeError(
895
+ `fetch: only http: and https: URLs can be fetched, not ${scheme ? `${scheme}:` : "a relative URL"}`,
896
+ )
897
+ }
868
898
  const method = (init?.method ?? "GET").toUpperCase()
869
899
  let bodyString: string | null = null
870
900
  let bodyBase64: string | undefined
@@ -1017,6 +1047,13 @@ declare const __nativeClearTimer: (token: number) => void
1017
1047
 
1018
1048
  constructor(url: string, protocols?: string | string[]) {
1019
1049
  this.url = String(url)
1050
+ // As in a browser, a URL that is not a network address throws here.
1051
+ const scheme = urlScheme(this.url)
1052
+ if (scheme !== "ws" && scheme !== "wss" && scheme !== "http" && scheme !== "https") {
1053
+ throw new SyntaxError(
1054
+ `WebSocket: the URL's scheme must be ws:, wss:, http: or https:, not ${scheme ? `${scheme}:` : "none"}`,
1055
+ )
1056
+ }
1020
1057
  const protoList = Array.isArray(protocols) ? protocols : protocols ? [protocols] : []
1021
1058
  // Allocate a session id JS-side so the dispatcher's response is a
1022
1059
  // simple ack rather than carrying the sid we then have to wait for.