Skip to content

fix(@jimp/wasm-webp): initialize WebP WASM from disk in Node#1403

Open
Aniketiitk21 wants to merge 1 commit intojimp-dev:mainfrom
Aniketiitk21:codex/fix-wasm-webp-node-init
Open

fix(@jimp/wasm-webp): initialize WebP WASM from disk in Node#1403
Aniketiitk21 wants to merge 1 commit intojimp-dev:mainfrom
Aniketiitk21:codex/fix-wasm-webp-node-init

Conversation

@Aniketiitk21
Copy link
Copy Markdown

Summary

  • fix @jimp/wasm-webp in Node by resolving the existing @jsquash/webp WASM files from disk and compiling them directly instead of relying on fetch(file://...)
  • keep the browser path unchanged while caching compiled decoder and encoder modules for repeated use
  • add a Node regression test that decodes the existing WebP fixture and round-trips an encoded WebP buffer

Root Cause

@jsquash/webp initializes its WASM modules through a fetch-based path that works in browsers, but in Node it resolves to a local file: URL. Node's fetch() cannot load that URL, so @jimp/wasm-webp fails during decoder or encoder initialization before any image work can happen.

What Changed

For Node only, @jimp/wasm-webp now:

  • resolves the decoder and encoder .wasm assets with Module.createRequire(import.meta.url)
  • reads and compiles those files with node:fs/promises and WebAssembly.compile
  • chooses the SIMD encoder binary only when the current runtime supports SIMD
  • resets failed init promises so a transient initialization error does not permanently poison the process

Impact

This keeps the existing @jsquash/webp integration and public API intact, but makes the package usable in Node again for both decoding and encoding WebP images.

Closes #1366

Validation

  • pnpm --filter @jimp/types build
  • pnpm --filter @jimp/wasm-webp build
  • pnpm --filter @jimp/wasm-webp lint
  • pnpm --filter @jimp/wasm-webp test -- --run

@Aniketiitk21 Aniketiitk21 marked this pull request as ready for review May 6, 2026 02:58
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.

The @jimp/wasm-webp doesn't seem to work in node environment.

1 participant