Skip to content

RFC: Env flag for npm packages to conditionally load WASM binaries instead of native binaries #286

Open
@EricSimons

Description

@EricSimons

UPDATE

This proposal has changed and instead relies on import/export maps, and has officially landed in Node.js core.

I have kept the original (now outdated) proposal below for future reference:

view original (outdated) proposal

The problem

Web development toolchains are increasingly including binaries for compilers/bundlers as they provide substantially faster performance than JS equivalents. The ecosystem is also increasingly adopting WebAssembly for distributing & executing binaries as it provides far greater security guarantees, are directly inspectable, and "build once and run everywhere".

Today esbuild and swc are the most popular of these new tools, and both release a WASM version in a separate package in lockstep with their native binaries (esbuild-wasm and @swc/wasm). As such, consumers of these packages need to determine at runtime whether they should use the native binaries or the WASM binary.

Parcel v2 currently uses a custom flag process.env.PARCEL_SWC_WASM that indicates it should require the WASM binary and not the platform specific variant. Other toolchains that have adopted swc/esbuild (Vite, Next.js, Snowpack, etc) need a similar way of determining this as well, and custom flags for each package is not ideal.

Proposed Solution: process.env.REQUIRES_WASM

I propose we standardize on a new flag, process.env.REQUIRES_WASM, that instructs npm packages to use WASM binaries instead of unsandboxed platform specific binaries.

This is important for environments that require high security guarantees (i.e. targets of supply chain attacks like enterprises and OSS devs), WebAssembly based containers like WebContainer or kubelet, in-browser playgrounds, and other cases where native binaries cannot/should not be executed.

Indicates WASM is required, not just preferred.

Importantly, this flag does not indicate that the host environment prefers WASM. Instead it is an instruction that the host requires WASM binaries and cannot (or does not) allow execution of unsandboxed native binaries.

Intended functionality

If process.env.REQUIRES_WASM is true at runtime, npm packages should import WASM binaries (or a JS file generated via emscripten, etc) instead of corresponding platform specific binaries.

Should a package not have a WASM binary available (and/or can't polyfill the functionality otherwise), the program should throw an error if process.env.REQUIRES_WASM is present.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions