Skip to content

fix(mobile): add native Android streaming client for SSE and relay streams - #104

Open
Satya-Siba-Nayak wants to merge 1 commit into
DuskBotAI:devfrom
Satya-Siba-Nayak:fix/android-native-stream
Open

Satya-Siba-Nayak wants to merge 1 commit into
DuskBotAI:devfrom
Satya-Siba-Nayak:fix/android-native-stream

Conversation

@Satya-Siba-Nayak

Copy link
Copy Markdown
Contributor

What and why

Fixes #103 (follow-up to #93 / #94, and addresses Android companion SSE streaming in #57).

#94 (ee66db7069) added network_security_config.xml and ACCESS_LOCAL_NETWORK so Android can pair and load chats over LAN via CapacitorHttp, while intentionally deferring the native streaming layer. Without native streaming on window.CapacitorWebFetch, Android connects initially and then drops to Reconnecting / Not connected:

  • Direct LAN / Tailscale (/api/events): Android WebView serves the app from https://localhost with allowMixedContent: false, blocking raw WebView fetch (CapacitorWebFetch) to plain-HTTP companion SSE streams before the request leaves the device.
  • Relay (/v1/channels/<id>/to-app?wait=25&stream=1): Raw WebView fetch sends a CORS preflight (Origin: https://localhost, Access-Control-Request-Headers: authorization), which link-service does not currently answer (404 with no Access-Control-Allow-* headers).
  • Dual-network Wi-Fi + cellular/VPN phones: Binding RFC 1918 and .local hosts to an active TRANSPORT_WIFI Network keeps LAN streams on Wi-Fi without affecting Tailscale (100.x.y.z / .ts.net) or public relay hosts.

Changes

  • apps/mobile/android/app/src/main/java/bot/dusk/mobile/SseStreamPlugin.java: Adds the DuskStream native Android plugin backed by HttpURLConnection (sends no Origin, respects network_security_config.xml, uses TextUtils.join for minSdkVersion 24 compatibility). Streams SSE (text/event-stream, /api/events, /stream) and relay NDJSON (application/x-ndjson, stream=1) incrementally via Capacitor chunk/end events, enforces http/https schemes only, and binds RFC 1918 / .local hosts to TRANSPORT_WIFI.
  • apps/mobile/android/app/src/main/java/bot/dusk/mobile/MainActivity.java: Registers SseStreamPlugin.class.
  • apps/mobile/src/native-platform.ts: Exports createNativeStreamFetch and wires DuskStream only onto window.CapacitorWebFetch on Android (idempotent). Leaves window.fetch (CapacitorHttp, installNativeAbort, nativeBody binary uploads, long timeouts), same-origin URLs (including /_capacitor_http_interceptor_), non-HTTP URLs (blob:, data:), and iOS completely untouched. Preserves AbortError/TimeoutError and redirect: "error" 3xx rejection.
  • src/mobile/native-stream.test.ts: Unit tests for SSE streaming, relay stream=1 NDJSON streaming + x-link-beat header passthrough, same-origin/_capacitor_http_interceptor_/data: passthrough, AbortSignal cancellation + native close(), redirect: "error" 3xx rejection, inline responses, and reader cancel cleanup.

Kept strictly scoped to Android native streaming (server-side CORS preflight support in apps/link-service/src/app.mjs is left out of this PR per one-concern-per-PR guidelines).

How it was checked

  • pnpm lint (oxlint --deny-warnings .): 0 warnings, 0 errors.

  • pnpm --filter @duskbot/mobile typecheck and tsc -b: passed cleanly.

  • pnpm --filter @duskbot/mobile build and node apps/mobile/scripts/release-gate.mjs: built cleanly and passed release gate.

  • vitest run src/mobile/: 62/62 test files passed (567/567 tests), plus scripts/testing/file-size-ratchet.test.ts and scripts/testing/comment-history.test.ts passed.

  • Local Gradle APK build was not run from this Windows machine; verifiable via android-smoke.yml.

  • pnpm lint, mobile typecheck, tsc -b, mobile build + release gate, and full src/mobile/ + guard tests pass (server/tsconfig.json has unrelated pre-existing errors on dev in server/mac-control/*)

  • New behaviour has a test (src/mobile/native-stream.test.ts)

  • No secrets, personal data or local paths in the diff or screenshots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant