@livx.cc/appwrap 0.61.23 → 0.61.24

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livx.cc/appwrap",
3
- "version": "0.61.23",
3
+ "version": "0.61.24",
4
4
  "description": "Wrap any PWA into a native app with native capabilities (appwrap runtime + @livx.cc/native-kit).",
5
5
  "license": "MIT",
6
6
  "author": "Elya Livshitz",
@@ -29,6 +29,8 @@ export function isTrustedOrigin(origin: string | null | undefined): boolean {
29
29
  const o = originOf(origin);
30
30
  if (!o) return false;
31
31
  if (o === 'app://localhost' || (SHELL_CONFIG.loader === 'file' && o === 'file://')) return true;
32
+ // Android serves the bundle from https://appwrap.local (custom-webview.android APP_ORIGIN, intercepted in-process).
33
+ if (SHELL_CONFIG.loader !== 'server' && SHELL_CONFIG.loader !== 'file' && o === 'https://appwrap.local') return true;
32
34
  if (SHELL_CONFIG.loader === 'server' && o === originOf(effectiveServerUrl())) return true;
33
35
  return (SHELL_CONFIG.appBoundDomains ?? []).some((h) => o === `https://${String(h).toLowerCase()}`);
34
36
  }
@@ -6,6 +6,7 @@ import { envGlobalsJs } from './env';
6
6
  import { handleWebPermissionRequest } from './android-helpers';
7
7
  import { showFileChooser } from './file-chooser.android';
8
8
  import { hostOf } from './env-switcher';
9
+ import { TRANSPORT_SHIM, PROMPT_PREFIX, CHUNK_PREFIX, takeChunk } from './prompt-transport';
9
10
 
10
11
  // `android` + `java` resolve to the real types-android namespaces (no declare needed).
11
12
  declare const androidx: any; // no NS types: androidx.webkit.* (WebViewFeature/WebViewCompat) not in the android-32 platform typings
@@ -13,18 +14,7 @@ declare const androidx: any; // no NS types: androidx.webkit.* (WebViewFeature/W
13
14
  /** Stable in-app origin — secure context, ES modules work (file:// blocks them). */
14
15
  export const APP_ORIGIN = 'https://appwrap.local';
15
16
 
16
- /**
17
- * Web→native transport shim. postMessage tunnels through window.prompt(), which
18
- * the shell intercepts synchronously in WebChromeClient.onJsPrompt — no compiled
19
- * @JavascriptInterface class, no polling. Injected at document start (androidx.webkit)
20
- * with an onPageStarted fallback; the guard makes double-injection a no-op.
21
- */
22
- const TRANSPORT_SHIM = `(function(){
23
- if (window.appwrapNative) return;
24
- window.appwrapNative = { postMessage: function(json){ window.prompt('__appwrap__:' + json); } };
25
- })();`;
26
-
27
- const PROMPT_PREFIX = '__appwrap__:';
17
+ // Web→native transport (prompt() tunnel, chunked past Chromium's ~10K prompt cap): ./prompt-transport.
28
18
 
29
19
  /** Document-start scripts: env hints + framework globals (backend origin) + bridge transport +
30
20
  * native-feel. Globals first so the page can read __APPWRAP__ / __APPWRAP_BACKEND_ORIGIN__ before its
@@ -60,6 +50,12 @@ function getChromeClientClass(): any {
60
50
  CustomWebView.forNative(view)?.onAppwrapMessage?.(message.slice(PROMPT_PREFIX.length), url);
61
51
  return true;
62
52
  }
53
+ if (typeof message === 'string' && message.startsWith(CHUNK_PREFIX)) {
54
+ result.confirm('');
55
+ const json = takeChunk(message.slice(CHUNK_PREFIX.length), url);
56
+ if (json !== null) CustomWebView.forNative(view)?.onAppwrapMessage?.(json, url);
57
+ return true;
58
+ }
63
59
  return false; // genuine page prompt — default handling
64
60
  },
65
61
 
@@ -152,6 +148,10 @@ export class CustomWebView extends WebView {
152
148
  settings.setDomStorageEnabled(true);
153
149
  settings.setAllowFileAccess(true);
154
150
  settings.setMediaPlaybackRequiresUserGesture(false); // speaker: autoplay allowed
151
+ // The bundle runs at https://appwrap.local, so Chromium blocks its fetch/XHR/ws to plain-http hosts as mixed
152
+ // content — iOS's app:// origin is not https and never hits this. Parity: a bundled app may talk to http LAN
153
+ // hosts (cleartext is already allowed in the manifest); a server-loaded page keeps the default block.
154
+ if (SHELL_CONFIG.loader !== 'server' && SHELL_CONFIG.loader !== 'file') settings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
155
155
  // Native feel: no pinch-zoom controls.
156
156
  settings.setSupportZoom(false);
157
157
  settings.setBuiltInZoomControls(false);
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Android web→native transport over window.prompt(). Chromium truncates a prompt() message at ~10K chars
3
+ * (device-measured: a 10.1K envelope arrives, 10.3K arrives cut → unparseable → the call hangs to its timeout),
4
+ * so an envelope longer than CHUNK goes as numbered parts `__appwrapc__:<id>:<i>:<n>:<part>`, reassembled here.
5
+ */
6
+ export const PROMPT_PREFIX = '__appwrap__:';
7
+ export const CHUNK_PREFIX = '__appwrapc__:';
8
+ const CHUNK = 8000;
9
+
10
+ /** Injected at document start; the guard makes double-injection a no-op. Never splits a surrogate pair. */
11
+ export const TRANSPORT_SHIM = `(function(){
12
+ if (window.appwrapNative) return;
13
+ var seq = 0;
14
+ window.appwrapNative = { postMessage: function(json){
15
+ if (json.length <= ${CHUNK}) return window.prompt('${PROMPT_PREFIX}' + json);
16
+ var parts = [], i = 0;
17
+ while (i < json.length) { var e = Math.min(json.length, i + ${CHUNK}); var c = json.charCodeAt(e - 1); if (e < json.length && c >= 0xD800 && c <= 0xDBFF) e--; parts.push(json.slice(i, e)); i = e; }
18
+ var id = (++seq) + Math.random().toString(36).slice(2);
19
+ for (var k = 0; k < parts.length; k++) window.prompt('${CHUNK_PREFIX}' + id + ':' + k + ':' + parts.length + ':' + parts[k]);
20
+ } };
21
+ })();`;
22
+
23
+ const pending = new Map<string, { url: string; parts: string[]; got: number }>();
24
+
25
+ /** One chunk in; the whole envelope out once its last part lands (all parts must come from the same page url). */
26
+ export function takeChunk(message: string, url: string): string | null {
27
+ const m = /^([^:]+):(\d+):(\d+):/.exec(message);
28
+ if (!m) return null;
29
+ const [head, id, i, n] = [m[0], m[1], +m[2], +m[3]];
30
+ let p = pending.get(id);
31
+ if (!p) { if (pending.size > 32) pending.clear(); p = { url, parts: new Array(n), got: 0 }; pending.set(id, p); }
32
+ if (p.url !== url || i >= p.parts.length || p.parts[i] !== undefined) { pending.delete(id); return null; }
33
+ p.parts[i] = message.slice(head.length);
34
+ if (++p.got < p.parts.length) return null;
35
+ pending.delete(id);
36
+ return p.parts.join('');
37
+ }
@@ -60,6 +60,17 @@ describe('Bridge origin gate — a foreign page must not reach the app’s store
60
60
  } finally { Object.assign(SHELL_CONFIG, prev); }
61
61
  });
62
62
 
63
+ test('Android bundle origin (https://appwrap.local) is trusted for a bundled loader, not for server', () => {
64
+ const prev = { loader: SHELL_CONFIG.loader, serverUrl: SHELL_CONFIG.serverUrl };
65
+ try {
66
+ Object.assign(SHELL_CONFIG, { loader: 'app' });
67
+ expect(bridgeAllows('https://appwrap.local/index.html', 'webview.eval')).toBe(true);
68
+ expect(bridgeAllows('https://appwrap.local.evil.io', 'webview.eval')).toBe(false);
69
+ Object.assign(SHELL_CONFIG, { loader: 'server', serverUrl: 'https://app.example.com' });
70
+ expect(bridgeAllows('https://appwrap.local', 'webview.eval')).toBe(false);
71
+ } finally { Object.assign(SHELL_CONFIG, prev); }
72
+ });
73
+
63
74
  test('originOf normalises', () => {
64
75
  expect(originOf('HTTP://U:p@Host:80/a?b#c')).toBe('http://host');
65
76
  expect(originOf('app://localhost/index.html')).toBe('app://localhost');
@@ -0,0 +1,39 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+ import { TRANSPORT_SHIM, PROMPT_PREFIX, CHUNK_PREFIX, takeChunk } from '../app/shell/prompt-transport';
3
+
4
+ /** Run the shim against a fake window; returns what native's onJsPrompt would see. */
5
+ const send = (json: string) => {
6
+ const prompts: string[] = [];
7
+ const window: any = { prompt: (m: string) => { prompts.push(m); } };
8
+ new Function('window', TRANSPORT_SHIM)(window);
9
+ window.appwrapNative.postMessage(json);
10
+ return prompts;
11
+ };
12
+ const deliver = (prompts: string[], url = 'https://appwrap.local/') => {
13
+ const out: string[] = [];
14
+ for (const m of prompts) {
15
+ if (m.startsWith(PROMPT_PREFIX)) out.push(m.slice(PROMPT_PREFIX.length));
16
+ else { const j = takeChunk(m.slice(CHUNK_PREFIX.length), url); if (j !== null) out.push(j); }
17
+ }
18
+ return out;
19
+ };
20
+
21
+ describe('prompt transport', () => {
22
+ test('a small envelope is one prompt', () => {
23
+ const p = send('{"a":1}');
24
+ expect(p).toEqual([PROMPT_PREFIX + '{"a":1}']);
25
+ });
26
+ test('a large envelope is chunked under the ~10K prompt cap and reassembled exactly (surrogates intact)', () => {
27
+ const json = JSON.stringify({ js: 'x'.repeat(7999) + '😀'.repeat(5000) + 'tail' });
28
+ const p = send(json);
29
+ expect(p.length).toBeGreaterThan(1);
30
+ for (const m of p) expect(m.length).toBeLessThan(10000);
31
+ for (const m of p) expect(/[\uD800-\uDBFF]$/.test(m)).toBe(false);
32
+ expect(deliver(p)).toEqual([json]);
33
+ });
34
+ test('parts from a different page url are dropped', () => {
35
+ const p = send('y'.repeat(20000));
36
+ const out: (string | null)[] = p.map((m, i) => takeChunk(m.slice(CHUNK_PREFIX.length), i ? 'https://evil.example/' : 'https://appwrap.local/'));
37
+ expect(out.every((x) => x === null)).toBe(true);
38
+ });
39
+ });