Skip to content

feat(backend): polywasm-backed undici on iOS, re-enable maps plugin#62

Merged
gmaclennan merged 4 commits into
mainfrom
claude/condescending-varahamihira-d49de2
May 7, 2026
Merged

feat(backend): polywasm-backed undici on iOS, re-enable maps plugin#62
gmaclennan merged 4 commits into
mainfrom
claude/condescending-varahamihira-d49de2

Conversation

@gmaclennan

@gmaclennan gmaclennan commented May 7, 2026

Copy link
Copy Markdown
Member

Summary

  • Replaces the iOS undici stub with polywasm, a pure-JS WebAssembly polyfill, so undici (and therefore the maps fastify plugin) works under nodejs-mobile's jitless V8 on iOS.
  • Re-enables the real maps fastify plugin in the iOS bundle by deleting backend/lib/maps-stub.js and removing the iOS-only stubComapeoMapsPlugin from rollup. Tile fetching is no longer broken on iOS by construction.

Why polywasm + a SIMD alias

nodejs-mobile on iOS runs V8 with --jitless and is built with v8_enable_webassembly=false, so globalThis.WebAssembly is absent. The previous workaround stubbed the only direct undici user (@comapeo/core's maps plugin) and passed --no-experimental-fetch to keep the bundled fetch out of the same load path.

Polywasm fills the gap, but its 0.2 release doesn't implement WASM SIMD (opcode 0xfd). Undici's lazyllhttp() tries the SIMD-optimized llhttp wasm first, falls back to non-SIMD on compile error — but polywasm compiles SIMD bytes successfully and only throws lazily on the first export call, which undici's catch doesn't intercept. The aliasUndiciSimdWasmPlugin redirects require('../llhttp/llhttp_simd-wasm.js') to the non-SIMD bytes beside it, scoped to importers in undici/lib/dispatcher/ so unrelated packages aren't caught. iOS-only — Android keeps the native, SIMD-capable WASM path.

Files

File Change
backend/index.ios.js New iOS entry. Imports lib/install-polywasm.js first so polywasm is in place before index.js (and undici through the maps plugin) evaluates. ESM source order guarantees this. Filename uses RN's .<platform>.js convention.
backend/lib/install-polywasm.js New. Installs polywasm whenever native WebAssembly.compile isn't callable. Tighter than typeof globalThis.WebAssembly === "undefined" because partial stubs would otherwise slip past.
backend/index.js Unchanged behavior; only minor doc edit.
backend/rollup.config.ts Drops stubComapeoMapsPlugin. Adds aliasUndiciSimdWasmPlugin (iOS-only). Switches iOS input to index.ios.js.
backend/lib/maps-stub.js Deleted.
backend/package.json Adds polywasm@0.2.0.

Verification

  • ✅ Polywasm install verified on Node 18 jitless desktop: WebAssembly.compile + WebAssembly.instantiate + llhttp_alloc(1) returns a valid pointer.
  • ✅ Bundled iOS path verified to use non-SIMD wasm: 48,615 bytes (matches node_modules/undici/lib/llhttp/llhttp-wasm.js); both WebAssembly.compile calls in the bundled lazyllhttp now reference the same bytes function.
  • ✅ End-to-end fetch through bundled undici verified on iPhone 16 simulator under nodejs-mobile jitless V8 via a temporary smoke test (now removed — see commit be91565). The smoke test wrote PASS to <privateStorageDir>/undici-smoke-result.txt during diagnostic, confirming polywasm + non-SIMD undici handles HTTP through llhttp at runtime.

Pre-existing iOS test failure (unrelated)

apps/example/tests/ios/CoreManagerSmokeTest fails locally with service.state == .error — but the same failure reproduces against 8655ff8 (the base of this branch) without any of these changes. Not introduced by this PR; the IPC handshake between native sending init and the backend receiving it doesn't complete on the local simulator. Filing separately.

Test plan

  • npm run backend:build produces ios/nodejs-project/index.mjs containing the polywasm install (search for globalThis.WebAssembly) and a single non-SIMD wasm bytes function.
  • On a real iOS device or fresh simulator: app reaches STARTED lifecycle state, no undici/WASM errors in node logs.
  • Android: existing build path unchanged — Android bundle still uses native WebAssembly, no polywasm cost. Verify Android tests still green.

🤖 Generated with Claude Code

gmaclennan and others added 3 commits May 7, 2026 21:41
nodejs-mobile on iOS runs V8 with --jitless and is built with
v8_enable_webassembly=false, so globalThis.WebAssembly is absent and
undici crashes the process at module-init when its lazy llhttp loader
runs. The previous workaround stubbed @comapeo/core's maps fastify
plugin (the only direct undici user) and passed --no-experimental-fetch
to keep Node's bundled fetch from re-introducing the same load path.

This change replaces that stub with polywasm, a pure-JS WebAssembly
polyfill:

- New `backend/index-ios.js` entry imports `lib/install-polywasm.js`
  before `index.js`, so polywasm is `globalThis.WebAssembly` by the
  time undici evaluates.
- `lib/install-polywasm.js` swaps in polywasm only when native
  `WebAssembly.compile` isn't callable — the `typeof undefined` check
  alone misses partial stubs.
- Rollup `aliasUndiciSimdWasmPlugin` redirects undici's
  `llhttp_simd-wasm.js` to the non-SIMD wasm beside it. polywasm 0.2
  doesn't implement WASM SIMD (opcode 0xfd), and undici's try/catch
  around `compile` doesn't intercept the lazy-throw on first
  `llhttp_alloc`. The alias is iOS-only — Android still uses the
  native, SIMD-capable WASM path.
- `lib/maps-stub.js` is gone; the iOS bundle now ships the real maps
  fastify plugin.

Verified end-to-end on the iPhone 16 simulator under nodejs-mobile
jitless V8: the temporary smoke test below produced PASS in
<privateStorageDir>/undici-smoke-result.txt during the diagnostic
phase, confirming polywasm + non-SIMD undici handles HTTP through
llhttp at runtime.

`backend/lib/undici-smoke-test.js` (TEMPORARY) spins up a
127.0.0.1:0 HTTP server, fetches it through undici, validates the
response. Wired into the boot IIFE between `started` and `init`;
failure surfaces as `phase: "undici-smoke-test"` on the lifecycle
error channel. Removable once the maps plugin's first real tile
fetch has confirmed the path on a device.

`apps/example/App.tsx` adds a state observer so the example app
shows the comapeo lifecycle state and a derived smoke-test status
(PASSED / FAILED: <msg> / pending).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- Rename `backend/index-ios.js` → `backend/index.ios.js` (RN's
  `.<platform>.js` convention).
- Remove `backend/lib/undici-smoke-test.js` and the boot-phase wiring
  in `backend/index.js`. Its job (proving polywasm + undici fetch
  works on jitless V8) was done during diagnostic; it was never
  meant to ship.
- Revert the smoke-test UI/state observer additions in
  `apps/example/App.tsx` — irrelevant once the smoke test is gone.

The polywasm install (`backend/lib/install-polywasm.js`) and the
SIMD-wasm alias (`aliasUndiciSimdWasmPlugin` in `rollup.config.ts`)
stay — those are the actual fix.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an iOS-specific backend bundle entry that installs a WebAssembly polyfill so undici can load under nodejs-mobile’s jitless V8, and re-enables the real maps Fastify plugin on iOS by removing the previous maps stub. This is implemented by introducing a polywasm bootstrap on iOS and a Rollup-time alias to force undici’s non-SIMD llhttp WASM module.

Changes:

  • Add backend/index.ios.js + backend/lib/install-polywasm.js to install polywasm before the shared backend entry evaluates on iOS.
  • Update backend/rollup.config.ts to use the iOS-specific input and to alias undici’s SIMD WASM module to the non-SIMD module on iOS.
  • Remove the iOS maps stub and add polywasm@0.2.0 to backend dependencies.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
backend/rollup.config.ts Switch iOS input to index.ios.js and add an iOS-only alias plugin to redirect undici SIMD WASM to non-SIMD.
backend/package.json Add polywasm dependency for the iOS WebAssembly polyfill.
backend/lib/maps-stub.js Delete the iOS-only maps plugin stub to re-enable real maps behavior on iOS.
backend/lib/install-polywasm.js Install polywasm when native WebAssembly.compile isn’t callable.
backend/index.ios.js New iOS-only entry that imports the polywasm installer before the shared index.js.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/rollup.config.ts
* because nodejs-mobile iOS runs V8 with `--jitless` and undici's
* WebAssembly init would crash module load.
* (and therefore the maps fastify plugin) loads cleanly. iOS uses a wrapper
* entry (`index-ios.js`) that installs polywasm as `globalThis.WebAssembly`
// module-init and would throw `WebAssembly is not defined` before the
// entry can boot.
//
// Imported only from `index-ios.js` — Android keeps native WebAssembly
Merging main brought in `@sentry/rollup-plugin` (and its transitive
deps) into `backend/package.json`, but the lock file regenerated
when polywasm landed predates that — so CI's `npm ci --ignore-scripts
--prefix backend` aborted with "Missing: @sentry/cli-darwin@2.58.5
from lock file" (and ~40 other entries). Re-resolve from
`package.json` so the lock matches.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@gmaclennan gmaclennan enabled auto-merge (squash) May 7, 2026 21:26
@gmaclennan gmaclennan merged commit d8131a9 into main May 7, 2026
7 checks passed
@gmaclennan gmaclennan deleted the claude/condescending-varahamihira-d49de2 branch May 7, 2026 21:28
gmaclennan added a commit that referenced this pull request Jun 22, 2026
## Optic Release Automation

This **draft** PR is opened by Github action
[optic-release-automation-action](https://github.com/nearform-actions/optic-release-automation-action).

A new **draft** GitHub release
[v1.0.0-pre.2](https://github.com/digidem/comapeo-core-react-native/releases/tag/untagged-c499977757c9745e56b2)
has been created.

Release author: @gmaclennan

#### If you want to go ahead with the release, please merge this PR.
When you merge:

- The GitHub release will be published

- The npm package with tag pre will be published according to the
publishing rules you have configured



- No major or minor tags will be updated as configured


#### If you close the PR

- The new draft release will be deleted and nothing will change

## What's Changed
* Android Testing Infrastructure & Bug Fixes by @gmaclennan in
#3
* chore: prebuild example/android; harden instrumented tests by
@gmaclennan in
#10
* Integrate @comapeo/core via IPC over Unix sockets by @gmaclennan in
#5
* chore: adjust repo setup by @achou11 in
#12
* chore: minor fixes based on expo-doctor by @achou11 in
#13
* Add iOS support & test infrastructure by @gmaclennan in
#6
* chore: add architecture docs & plans by @gmaclennan in
#11
* update some native deps used in backend by @achou11 in
#14
* iOS Phase 1: unified JS bundle + smoke test (simulator-only) by
@gmaclennan in
#15
* iOS Phase 2: xcframework Embed & Sign for native addons by @gmaclennan
in #16
* Phase 2 Android: jniLibs packaging + unified rollup loader plugin by
@gmaclennan in
#17
* chore: post-Phase-2 cleanup — comments, plan docs, agents.md by
@gmaclennan in
#33
* android: read abiFilters from reactNativeArchitectures (#30) by
@gmaclennan in
#35
* refactor: simplify build-backend.ts; rollup writes directly to native
asset trees by @gmaclennan in
#34
* chore: fix eslint configuration by @achou11 in
#41
* android: audit 16 KB page alignment on every shipped .so by
@gmaclennan in
#43
* Add rootkey persistence and lifecycle state management by @gmaclennan
in #36
* chore: move example app into apps directory by @achou11 in
#18
* refactor: per-component lifecycle state with derived ComapeoState by
@gmaclennan in
#47
* android: fold waitForFile into connect retry loop by @gmaclennan in
#52
* chore: add e2e testing app by @achou11 in
#49
* fix(android): drop setUnlockedDeviceRequired from rootkey wrapper key
by @gmaclennan in
#57
* fix(backend): cache stopping/error frames for late joiners by
@gmaclennan in
#58
* fix(ios-tests): wait for STOPPING before signalling node exit by
@gmaclennan in
#59
* fix(android): drain JNI stdio pumps before returning from node::Start
by @gmaclennan in
#60
* Sentry integration: Phase 1 + Phase 2a + Phase 2b by @gmaclennan in
#54
* feat(backend): polywasm-backed undici on iOS, re-enable maps plugin by
@gmaclennan in
#62
* ci: drop unreliable Android emulator snapshot caching by @gmaclennan
in #64
* feat(sentry): land Phase 3 — backend loader + RPC tracing by
@gmaclennan in
#63
* fix(ios-tests): serialise STOPPING/STOPPED observers in
testFullLifecycleStateTransitions by @gmaclennan in
#71
* use npm list instead of custom traversal to get native module versions
by @achou11 in
#70
* feat(sentry): land Phases 6 + 7a — Android exit reasons & iOS
MetricKit app-exit telemetry by @gmaclennan in
#72
* fix(sentry): make exit telemetry lossless and stop cross-process
clobbering by @gmaclennan in
#84
* chore(e2e): add e2e tests on browserstack via Maestro by @achou11 in
#56
* feat(sentry): migrate to @sentry/react-native v8; exit telemetry as
Application Metrics by @gmaclennan in
#73
* Map server integration by @gmaclennan in
#86
* chore(deps): upgrade to Expo SDK 56 (React Native 0.85) by @gmaclennan
in #87
* chore(ci): add release workflow by @gmaclennan in
#90
* chore: fix npm script and release build script by @gmaclennan in
#91
* chore(pack): don't try to package build files by @gmaclennan in
#92
* fix: start fastify listening by @gmaclennan in
#93
* perf(backend): switch bundler from rollup to rolldown by @gmaclennan
in #94
* fix(ci): ignore-scripts in ios npm installs by @gmaclennan in
#96
* fix(ci): replace --ignore-scripts with npm strict-allow-scripts
allowlist by @gmaclennan in
#106
* feat(config): let the consuming app supply the default project config
by @gmaclennan in
#95
* chore(release): merge prerelease branch. by @gmaclennan in
#110

## New Contributors
* @achou11 made their first contribution in
#12

**Full Changelog**:
https://github.com/digidem/comapeo-core-react-native/commits/v1.0.0-pre.2

<!--

<release-meta>{"id":342868678,"version":"v1.0.0-pre.2","npmTag":"pre","opticUrl":"https://optic-zf3votdk5a-ew.a.run.app/api/generate/"}</release-meta>
-->
@gmaclennan gmaclennan added the feature New feature (changelog) label Jun 22, 2026
gmaclennan added a commit that referenced this pull request Jun 22, 2026
## Optic Release Automation

This **draft** PR is opened by Github action
[optic-release-automation-action](https://github.com/nearform-actions/optic-release-automation-action).

A new **draft** GitHub release
[v1.0.0-pre.2](https://github.com/digidem/comapeo-core-react-native/releases/tag/untagged-352a6c41c12fd02dec37)
has been created.

Release author: @gmaclennan

#### If you want to go ahead with the release, please merge this PR.
When you merge:

- The GitHub release will be published

- The npm package with tag pre will be published according to the
publishing rules you have configured



- No major or minor tags will be updated as configured


#### If you close the PR

- The new draft release will be deleted and nothing will change

<!-- Release notes generated using configuration in .github/release.yml
at 7fe80b4 -->

## What's Changed
### 🚀 Features
* Integrate @comapeo/core via IPC over Unix sockets by @gmaclennan in
#5
* Add iOS support & test infrastructure by @gmaclennan in
#6
* iOS Phase 1: unified JS bundle + smoke test (simulator-only) by
@gmaclennan in
#15
* iOS Phase 2: xcframework Embed & Sign for native addons by @gmaclennan
in #16
* Phase 2 Android: jniLibs packaging + unified rollup loader plugin by
@gmaclennan in
#17
* android: read abiFilters from reactNativeArchitectures (#30) by
@gmaclennan in
#35
* Add rootkey persistence and lifecycle state management by @gmaclennan
in #36
* Sentry integration: Phase 1 + Phase 2a + Phase 2b by @gmaclennan in
#54
* feat(backend): polywasm-backed undici on iOS, re-enable maps plugin by
@gmaclennan in
#62
* feat(sentry): land Phase 3 — backend loader + RPC tracing by
@gmaclennan in
#63
* feat(sentry): land Phases 6 + 7a — Android exit reasons & iOS
MetricKit app-exit telemetry by @gmaclennan in
#72
* feat(sentry): migrate to @sentry/react-native v8; exit telemetry as
Application Metrics by @gmaclennan in
#73
* Map server integration by @gmaclennan in
#86
* feat(config): let the consuming app supply the default project config
by @gmaclennan in
#95
### 🐛 Bug Fixes
* fix(android): drop setUnlockedDeviceRequired from rootkey wrapper key
by @gmaclennan in
#57
* fix(backend): cache stopping/error frames for late joiners by
@gmaclennan in
#58
* fix(ios-tests): wait for STOPPING before signalling node exit by
@gmaclennan in
#59
* fix(android): drain JNI stdio pumps before returning from node::Start
by @gmaclennan in
#60
* fix(ios-tests): serialise STOPPING/STOPPED observers in
testFullLifecycleStateTransitions by @gmaclennan in
#71
* fix(sentry): make exit telemetry lossless and stop cross-process
clobbering by @gmaclennan in
#84
* fix: start fastify listening by @gmaclennan in
#93
* fix(ci): ignore-scripts in ios npm installs by @gmaclennan in
#96
* fix(ci): replace --ignore-scripts with npm strict-allow-scripts
allowlist by @gmaclennan in
#106
* fix(release): stop `npm pack --dry-run` leaking dry-run into backend
install by @gmaclennan in
#129
### ⚡ Performance
* perf(backend): switch bundler from rollup to rolldown by @gmaclennan
in #94
### ⬆️ Dependencies
* update some native deps used in backend by @achou11 in
#14
* chore(deps): upgrade to Expo SDK 56 (React Native 0.85) by @gmaclennan
in #87
### 🏗️ Maintenance
* Android Testing Infrastructure & Bug Fixes by @gmaclennan in
#3
* chore: prebuild example/android; harden instrumented tests by
@gmaclennan in
#10
* chore: adjust repo setup by @achou11 in
#12
* chore: minor fixes based on expo-doctor by @achou11 in
#13
* chore: add architecture docs & plans by @gmaclennan in
#11
* chore: post-Phase-2 cleanup — comments, plan docs, agents.md by
@gmaclennan in
#33
* refactor: simplify build-backend.ts; rollup writes directly to native
asset trees by @gmaclennan in
#34
* chore: fix eslint configuration by @achou11 in
#41
* android: audit 16 KB page alignment on every shipped .so by
@gmaclennan in
#43
* chore: move example app into apps directory by @achou11 in
#18
* refactor: per-component lifecycle state with derived ComapeoState by
@gmaclennan in
#47
* android: fold waitForFile into connect retry loop by @gmaclennan in
#52
* chore: add e2e testing app by @achou11 in
#49
* ci: drop unreliable Android emulator snapshot caching by @gmaclennan
in #64
* use npm list instead of custom traversal to get native module versions
by @achou11 in
#70
* chore(e2e): add e2e tests on browserstack via Maestro by @achou11 in
#56
* chore(ci): add release workflow by @gmaclennan in
#90
* chore: fix npm script and release build script by @gmaclennan in
#91
* chore(pack): don't try to package build files by @gmaclennan in
#92
* chore(release): merge prerelease branch. by @gmaclennan in
#110
* ci(e2e): retry BrowserStack builds on infra-class flakes by
@gmaclennan in
#113
### Other Changes
* ci: derive changelog labels from PR titles + add Dependabot by
@gmaclennan in
#114

## New Contributors
* @achou11 made their first contribution in
#12
* @optic-release-automation[bot] made their first contribution in
#112

**Full Changelog**:
https://github.com/digidem/comapeo-core-react-native/commits/v1.0.0-pre.2

<!--

<release-meta>{"id":342970724,"version":"v1.0.0-pre.2","npmTag":"pre","opticUrl":"https://optic-zf3votdk5a-ew.a.run.app/api/generate/"}</release-meta>
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature (changelog)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants