@glassly/jspolyfill 0.3.5 → 0.3.6-dev.846

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
@@ -254,9 +254,16 @@
254
254
  let env;
255
255
  try {
256
256
  env = JSON.parse(envelopeJson);
257
- } catch {
257
+ } catch (e) {
258
258
  try {
259
- __hostError(JSON.stringify({ message: "Bad __deliver envelope JSON", source: "__deliver" }));
259
+ const reason = e instanceof Error ? `${e.name}: ${e.message}` : String(e);
260
+ const length = typeof envelopeJson === "string" ? envelopeJson.length : -1;
261
+ __hostError(
262
+ JSON.stringify({
263
+ message: `Bad __deliver envelope JSON (${reason}; length=${length}, type=${typeof envelopeJson})`,
264
+ source: "__deliver"
265
+ })
266
+ );
260
267
  } catch {
261
268
  }
262
269
  return;
@@ -546,15 +553,36 @@
546
553
  return out;
547
554
  };
548
555
  }
556
+ function bytesToBase64(bytes) {
557
+ const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
558
+ let out = "";
559
+ let i = 0;
560
+ while (i < bytes.length) {
561
+ const c1 = bytes[i++];
562
+ const c2 = i < bytes.length ? bytes[i++] : NaN;
563
+ const c3 = i < bytes.length ? bytes[i++] : NaN;
564
+ const e1 = c1 >> 2;
565
+ const e2 = (c1 & 3) << 4 | (Number.isNaN(c2) ? 0 : c2 >> 4);
566
+ const e3 = Number.isNaN(c2) ? 64 : (c2 & 15) << 2 | (Number.isNaN(c3) ? 0 : c3 >> 6);
567
+ const e4 = Number.isNaN(c3) ? 64 : c3 & 63;
568
+ out += b64[e1] + b64[e2] + (e3 === 64 ? "=" : b64[e3]) + (e4 === 64 ? "=" : b64[e4]);
569
+ }
570
+ return out;
571
+ }
549
572
  if (typeof g.fetch !== "function") {
550
573
  ;
551
574
  g.fetch = async (input, init) => {
552
575
  const url = typeof input === "string" ? input : input.toString();
553
576
  const method = (init?.method ?? "GET").toUpperCase();
554
577
  let bodyString = null;
578
+ let bodyBase64 = null;
555
579
  const reqBody = init?.body;
556
580
  if (typeof reqBody === "string") {
557
581
  bodyString = reqBody;
582
+ } else if (reqBody instanceof ArrayBuffer) {
583
+ bodyBase64 = bytesToBase64(new Uint8Array(reqBody));
584
+ } else if (ArrayBuffer.isView(reqBody)) {
585
+ bodyBase64 = bytesToBase64(new Uint8Array(reqBody.buffer, reqBody.byteOffset, reqBody.byteLength));
558
586
  } else if (reqBody && typeof reqBody.toString === "function") {
559
587
  bodyString = reqBody.toString();
560
588
  }
@@ -577,7 +605,7 @@
577
605
  }
578
606
  }
579
607
  const sendRequest = g.__sendRequest;
580
- const result = await sendRequest("fetch", "request", [{ url, method, headers, body: bodyString }]);
608
+ const result = await sendRequest("fetch", "request", [{ url, method, headers, body: bodyString, bodyBase64 }]);
581
609
  const bodyText = typeof result.body === "string" ? result.body : "";
582
610
  const responseHeaders = new Map(Object.entries(result.headers ?? {}));
583
611
  class ResponseLike {
@@ -617,22 +645,7 @@
617
645
  };
618
646
  }
619
647
  if (typeof g.WebSocket !== "function") {
620
- let bytesToBase642 = function(bytes) {
621
- const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
622
- let out = "";
623
- let i = 0;
624
- while (i < bytes.length) {
625
- const c1 = bytes[i++];
626
- const c2 = i < bytes.length ? bytes[i++] : NaN;
627
- const c3 = i < bytes.length ? bytes[i++] : NaN;
628
- const e1 = c1 >> 2;
629
- const e2 = (c1 & 3) << 4 | (Number.isNaN(c2) ? 0 : c2 >> 4);
630
- const e3 = Number.isNaN(c2) ? 64 : (c2 & 15) << 2 | (Number.isNaN(c3) ? 0 : c3 >> 6);
631
- const e4 = Number.isNaN(c3) ? 64 : c3 & 63;
632
- out += b64[e1] + b64[e2] + (e3 === 64 ? "=" : b64[e3]) + (e4 === 64 ? "=" : b64[e4]);
633
- }
634
- return out;
635
- }, base64ToBytes2 = function(s) {
648
+ let base64ToBytes2 = function(s) {
636
649
  const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
637
650
  const clean = s.replace(/=+$/, "");
638
651
  const out = [];
@@ -654,7 +667,7 @@
654
667
  if (typeof q === "function") q(fn);
655
668
  else g.Promise.resolve().then(fn);
656
669
  };
657
- var bytesToBase64 = bytesToBase642, base64ToBytes = base64ToBytes2, queueMicrotaskSafe = queueMicrotaskSafe2;
670
+ var base64ToBytes = base64ToBytes2, queueMicrotaskSafe = queueMicrotaskSafe2;
658
671
  const sockets = /* @__PURE__ */ new Map();
659
672
  g.__deliverWebSocketEvent = (sid, type, payload) => {
660
673
  const sock = sockets.get(sid);
@@ -713,7 +726,7 @@
713
726
  } else {
714
727
  kind = "binary";
715
728
  const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
716
- payload = bytesToBase642(bytes);
729
+ payload = bytesToBase64(bytes);
717
730
  }
718
731
  try {
719
732
  __dispatch("ws", "send", JSON.stringify([{ sid: this.sid, kind, payload }]));
package/dist/startup.js CHANGED
@@ -254,9 +254,16 @@
254
254
  let env;
255
255
  try {
256
256
  env = JSON.parse(envelopeJson);
257
- } catch {
257
+ } catch (e) {
258
258
  try {
259
- __hostError(JSON.stringify({ message: "Bad __deliver envelope JSON", source: "__deliver" }));
259
+ const reason = e instanceof Error ? `${e.name}: ${e.message}` : String(e);
260
+ const length = typeof envelopeJson === "string" ? envelopeJson.length : -1;
261
+ __hostError(
262
+ JSON.stringify({
263
+ message: `Bad __deliver envelope JSON (${reason}; length=${length}, type=${typeof envelopeJson})`,
264
+ source: "__deliver"
265
+ })
266
+ );
260
267
  } catch {
261
268
  }
262
269
  return;
@@ -546,15 +553,36 @@
546
553
  return out;
547
554
  };
548
555
  }
556
+ function bytesToBase64(bytes) {
557
+ const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
558
+ let out = "";
559
+ let i = 0;
560
+ while (i < bytes.length) {
561
+ const c1 = bytes[i++];
562
+ const c2 = i < bytes.length ? bytes[i++] : NaN;
563
+ const c3 = i < bytes.length ? bytes[i++] : NaN;
564
+ const e1 = c1 >> 2;
565
+ const e2 = (c1 & 3) << 4 | (Number.isNaN(c2) ? 0 : c2 >> 4);
566
+ const e3 = Number.isNaN(c2) ? 64 : (c2 & 15) << 2 | (Number.isNaN(c3) ? 0 : c3 >> 6);
567
+ const e4 = Number.isNaN(c3) ? 64 : c3 & 63;
568
+ out += b64[e1] + b64[e2] + (e3 === 64 ? "=" : b64[e3]) + (e4 === 64 ? "=" : b64[e4]);
569
+ }
570
+ return out;
571
+ }
549
572
  if (typeof g.fetch !== "function") {
550
573
  ;
551
574
  g.fetch = async (input, init) => {
552
575
  const url = typeof input === "string" ? input : input.toString();
553
576
  const method = (init?.method ?? "GET").toUpperCase();
554
577
  let bodyString = null;
578
+ let bodyBase64 = null;
555
579
  const reqBody = init?.body;
556
580
  if (typeof reqBody === "string") {
557
581
  bodyString = reqBody;
582
+ } else if (reqBody instanceof ArrayBuffer) {
583
+ bodyBase64 = bytesToBase64(new Uint8Array(reqBody));
584
+ } else if (ArrayBuffer.isView(reqBody)) {
585
+ bodyBase64 = bytesToBase64(new Uint8Array(reqBody.buffer, reqBody.byteOffset, reqBody.byteLength));
558
586
  } else if (reqBody && typeof reqBody.toString === "function") {
559
587
  bodyString = reqBody.toString();
560
588
  }
@@ -577,7 +605,7 @@
577
605
  }
578
606
  }
579
607
  const sendRequest = g.__sendRequest;
580
- const result = await sendRequest("fetch", "request", [{ url, method, headers, body: bodyString }]);
608
+ const result = await sendRequest("fetch", "request", [{ url, method, headers, body: bodyString, bodyBase64 }]);
581
609
  const bodyText = typeof result.body === "string" ? result.body : "";
582
610
  const responseHeaders = new Map(Object.entries(result.headers ?? {}));
583
611
  class ResponseLike {
@@ -617,22 +645,7 @@
617
645
  };
618
646
  }
619
647
  if (typeof g.WebSocket !== "function") {
620
- let bytesToBase642 = function(bytes) {
621
- const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
622
- let out = "";
623
- let i = 0;
624
- while (i < bytes.length) {
625
- const c1 = bytes[i++];
626
- const c2 = i < bytes.length ? bytes[i++] : NaN;
627
- const c3 = i < bytes.length ? bytes[i++] : NaN;
628
- const e1 = c1 >> 2;
629
- const e2 = (c1 & 3) << 4 | (Number.isNaN(c2) ? 0 : c2 >> 4);
630
- const e3 = Number.isNaN(c2) ? 64 : (c2 & 15) << 2 | (Number.isNaN(c3) ? 0 : c3 >> 6);
631
- const e4 = Number.isNaN(c3) ? 64 : c3 & 63;
632
- out += b64[e1] + b64[e2] + (e3 === 64 ? "=" : b64[e3]) + (e4 === 64 ? "=" : b64[e4]);
633
- }
634
- return out;
635
- }, base64ToBytes2 = function(s) {
648
+ let base64ToBytes2 = function(s) {
636
649
  const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
637
650
  const clean = s.replace(/=+$/, "");
638
651
  const out = [];
@@ -654,7 +667,7 @@
654
667
  if (typeof q === "function") q(fn);
655
668
  else g.Promise.resolve().then(fn);
656
669
  };
657
- var bytesToBase64 = bytesToBase642, base64ToBytes = base64ToBytes2, queueMicrotaskSafe = queueMicrotaskSafe2;
670
+ var base64ToBytes = base64ToBytes2, queueMicrotaskSafe = queueMicrotaskSafe2;
658
671
  const sockets = /* @__PURE__ */ new Map();
659
672
  g.__deliverWebSocketEvent = (sid, type, payload) => {
660
673
  const sock = sockets.get(sid);
@@ -713,7 +726,7 @@
713
726
  } else {
714
727
  kind = "binary";
715
728
  const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
716
- payload = bytesToBase642(bytes);
729
+ payload = bytesToBase64(bytes);
717
730
  }
718
731
  try {
719
732
  __dispatch("ws", "send", JSON.stringify([{ sid: this.sid, kind, payload }]));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glassly/jspolyfill",
3
- "version": "0.3.5",
3
+ "version": "0.3.6-dev.846",
4
4
  "description": "JSRuntime 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
@@ -373,9 +373,18 @@ declare const __nativeClearTimer: (token: number) => void
373
373
  }
374
374
  try {
375
375
  env = JSON.parse(envelopeJson)
376
- } catch {
376
+ } catch (e) {
377
+ // Parse failures include QuickJS memory/stack errors, so name the error and
378
+ // the size: a syntax error and an InternalError need different fixes.
377
379
  try {
378
- __hostError(JSON.stringify({message: "Bad __deliver envelope JSON", source: "__deliver"}))
380
+ const reason = e instanceof Error ? `${e.name}: ${e.message}` : String(e)
381
+ const length = typeof envelopeJson === "string" ? envelopeJson.length : -1
382
+ __hostError(
383
+ JSON.stringify({
384
+ message: `Bad __deliver envelope JSON (${reason}; length=${length}, type=${typeof envelopeJson})`,
385
+ source: "__deliver",
386
+ }),
387
+ )
379
388
  } catch {
380
389
  /* ignore */
381
390
  }
@@ -726,6 +735,23 @@ declare const __nativeClearTimer: (token: number) => void
726
735
  }
727
736
  }
728
737
 
738
+ function bytesToBase64(bytes: Uint8Array): string {
739
+ const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
740
+ let out = ""
741
+ let i = 0
742
+ while (i < bytes.length) {
743
+ const c1 = bytes[i++]!
744
+ const c2 = i < bytes.length ? bytes[i++]! : NaN
745
+ const c3 = i < bytes.length ? bytes[i++]! : NaN
746
+ const e1 = c1 >> 2
747
+ const e2 = ((c1 & 3) << 4) | (Number.isNaN(c2) ? 0 : c2 >> 4)
748
+ const e3 = Number.isNaN(c2) ? 64 : ((c2 & 15) << 2) | (Number.isNaN(c3) ? 0 : c3 >> 6)
749
+ const e4 = Number.isNaN(c3) ? 64 : c3 & 63
750
+ out += b64[e1]! + b64[e2]! + (e3 === 64 ? "=" : b64[e3]!) + (e4 === 64 ? "=" : b64[e4]!)
751
+ }
752
+ return out
753
+ }
754
+
729
755
  // ---------- fetch (thin native bridge) -----------------------------------
730
756
  // Async — uses __sendRequest under the hood with iface "fetch". The
731
757
  // native handler is expected to return {status, statusText, headers, body}
@@ -735,9 +761,14 @@ declare const __nativeClearTimer: (token: number) => void
735
761
  const url = typeof input === "string" ? input : input.toString()
736
762
  const method = (init?.method ?? "GET").toUpperCase()
737
763
  let bodyString: string | null = null
764
+ let bodyBase64: string | null = null
738
765
  const reqBody = init?.body as unknown
739
766
  if (typeof reqBody === "string") {
740
767
  bodyString = reqBody
768
+ } else if (reqBody instanceof ArrayBuffer) {
769
+ bodyBase64 = bytesToBase64(new Uint8Array(reqBody))
770
+ } else if (ArrayBuffer.isView(reqBody)) {
771
+ bodyBase64 = bytesToBase64(new Uint8Array(reqBody.buffer, reqBody.byteOffset, reqBody.byteLength))
741
772
  } else if (reqBody && typeof (reqBody as {toString?: () => string}).toString === "function") {
742
773
  bodyString = (reqBody as {toString: () => string}).toString()
743
774
  }
@@ -764,7 +795,7 @@ declare const __nativeClearTimer: (token: number) => void
764
795
  method: string,
765
796
  args: unknown[],
766
797
  ) => Promise<unknown>
767
- const result = (await sendRequest("fetch", "request", [{url, method, headers, body: bodyString}])) as {
798
+ const result = (await sendRequest("fetch", "request", [{url, method, headers, body: bodyString, bodyBase64}])) as {
768
799
  status: number
769
800
  statusText?: string
770
801
  headers?: Record<string, string>
@@ -998,22 +1029,6 @@ declare const __nativeClearTimer: (token: number) => void
998
1029
  }
999
1030
  }
1000
1031
 
1001
- function bytesToBase64(bytes: Uint8Array): string {
1002
- const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
1003
- let out = ""
1004
- let i = 0
1005
- while (i < bytes.length) {
1006
- const c1 = bytes[i++]!
1007
- const c2 = i < bytes.length ? bytes[i++]! : NaN
1008
- const c3 = i < bytes.length ? bytes[i++]! : NaN
1009
- const e1 = c1 >> 2
1010
- const e2 = ((c1 & 3) << 4) | (Number.isNaN(c2) ? 0 : c2 >> 4)
1011
- const e3 = Number.isNaN(c2) ? 64 : ((c2 & 15) << 2) | (Number.isNaN(c3) ? 0 : c3 >> 6)
1012
- const e4 = Number.isNaN(c3) ? 64 : c3 & 63
1013
- out += b64[e1]! + b64[e2]! + (e3 === 64 ? "=" : b64[e3]!) + (e4 === 64 ? "=" : b64[e4]!)
1014
- }
1015
- return out
1016
- }
1017
1032
  function base64ToBytes(s: string): Uint8Array {
1018
1033
  const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
1019
1034
  const clean = s.replace(/=+$/, "")