Skip to content

fix: transform image demo crash by pinning @emnapi to 1.8.1#450

Open
DaanyaalSobani wants to merge 1 commit intonapi-rs:mainfrom
DaanyaalSobani:fix/coep-shared-array-buffer
Open

fix: transform image demo crash by pinning @emnapi to 1.8.1#450
DaanyaalSobani wants to merge 1 commit intonapi-rs:mainfrom
DaanyaalSobani:fix/coep-shared-array-buffer

Conversation

@DaanyaalSobani
Copy link
Copy Markdown

Summary

Fixes the homepage's "Transform Image App" demo, which throws TypeError: Cannot read properties of undefined (reading 'whenLoaded') in Chrome on click.

Refs napi-rs/napi-rs#3262

Root cause

@napi-rs/image-wasm32-wasi's prebuilt .wasm is linked against emnapi 1.8.x, which uses an internal worker-pool to track threads. But @napi-rs/wasm-runtime resolves @emnapi/core to ^1.7.1, which in practice installs 1.10.0 — and from 1.9.0 onward, emnapi switched to delegating to pthread_create and stopped maintaining a separate pool. The JS runtime then reads a tid from a memory location the prebuilt wasm never populates, so PThread.pthreads[tid] is undefined when initWorkers tries to read .whenLoaded on it.

This is the same mismatch reported and explained in toyobayashi/emnapi#202.

Changes

  • package.json: pin @emnapi/core and @emnapi/runtime to 1.8.1 via resolutions so the JS runtime matches what the prebuilt .wasm was linked against.
  • next.config.js: add COOP/COEP at the document level and Cross-Origin-Resource-Policy: same-origin on /_next/static/*. middleware.js already set COOP/COEP on HTML, but Next.js middleware doesn't run on /_next/static/*, so static worker chunks lacked CORP — required under Cross-Origin-Embedder-Policy: require-corp for SharedArrayBuffer-backed wasm threading.

Test plan

  • yarn dev → open homepage → click Transform image to webp → completes successfully (previously threw)
  • DevTools console: crossOriginIsolated === true
  • All 5 image.wasm32-wasi.*.wasm requests return 200

The image transform demo on the homepage threw 'Cannot read
properties of undefined (reading whenLoaded)' in Chrome because
@emnapi/core 1.10.0 (resolved by @napi-rs/wasm-runtime) uses the
new pthread_create-based worker model, while the prebuilt
@napi-rs/image-wasm32-wasi .wasm is still linked against the older
worker-pool model from emnapi 1.8.x. Pinning the JS runtime to
1.8.1 realigns the two halves.

Also adds COOP/COEP/CORP response headers via next.config.js so
that /_next/static/* assets satisfy cross-origin isolation
(middleware.js can't reach those paths), which is a separate
prerequisite for SharedArrayBuffer-backed wasm threading.

Refs napi-rs/napi-rs#3262
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 30, 2026

@DaanyaalSobani is attempting to deploy a commit to the napi-rs Team on Vercel.

A member of the Team first needs to authorize it.

@DaanyaalSobani
Copy link
Copy Markdown
Author

Recording.2026-04-30.185711.mp4

Video showing the button is working after applying the fix.

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