Run WebAssembly Components Inside Node.js With Jco
Written by Nikos Vaggalis   
Thursday, 28 March 2024

Jco 1.0 has been just announced by the Bytecode Alliance.It's a native JavaScript WebAssembly toolchain and runtime that runs Wasm components inside Node.js. Why is that useful?


In simple terms, a Wasm component is like a shared library, which maybe linked to other code and so on. Jco's mission then is to take such libraries written in a variety of programming languages and execute them from Node.js.

As such Jco is comprised of both a runtime that adheres to WASI 0.2 specification and a toolchain that turns programs in various languages to WASI interface's components. WASI (WebAssembly System Interface) is a modular system interface for WebAssembly (Wasm) that enables developers to run Wasm programs on any platform, including the browser, the cloud, and the edge.

WASI is designed to be secure, fast, and portable, allowing developers to write their programs once and run them anywhere. Under the WASI 0.2 API surface, components can access the network, filesystem, and other system APIs available in the runtime.

However Wasm Components do not just provide a way for JavaScript and Wasm to talk to each other. They can do so for any programming language. The point of Wasm Components and WASI is that we can connect any number of languages to each other via a strongly typed ABI, while Jco is the toolchain specifically for the JavaScript bindings.

Jco features include:

  • Transpile Wasm Component binaries into ES modules that can run in any JS environment.
  • WASI Preview2 support in Node.js (undergoing stabilization) & browsers (experimental).
  • Component builds of Wasm Tools helpers, available for use as a library or CLI commands for use in native JS environments.
  • Run and serve commands like Wasmtime, as JS implementations of the Command and HTTP Proxy worlds.
  • "Componentize" command to easily create components written in JavaScript (wrapper of ComponentizeJS).

To transpile a component into JS from the CLI you do:

jco transpile component.wasm -o out-dir

The resultant file then can be imported providing the bindings of the component as if it were imported directly:

import { fn } from './out-dir/component.js';

fn();

For Wasm components that implement the WASI CLI world, a jco run utility is provided to run these applications in Node.js as:

jco run component. wasm

Of course you have to write the component in some language, say Rust.

At this point it's important to note that there's a similar project, that of Wasmer JS SDK, not from the Bytecode Alliance but from Wasmer. That SDK supports WASIX, a propriety extension to WASI which also runs wasm applications on both the browser and server. The added bonus is that the SDK, can run any UNIX program that can use threads, signals, subprocesses and other POSIX features all inside the browser!

However WASIX has lately been caught in the middle of a turmoil, with the Alliance claiming that WASIX is non standard and that the Alliance doesn't promote non-standard system interfaces.

In any case and whichever interface you choose to go for, you can now port your programs written in any language to WebAssembly and enjoy all the advantages such a move bears.

 

More Information

Announcing Jco 1.0

jco

Related Articles

Wasmer JS SDK - WebAssembly In The Browser 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Grow with Google Launches Generative AI Course
26/04/2024

Grow with Google, in collaboration with MIT RAISE (Responsible AI for Social Empowerment and Education), is launching a no-cost Generative AI for Educators course.



Quadrupedal Parkour
31/03/2024

What is it with robots and parkour? First Atlas and now ANYmal want to impress us with their prowess. For the roboticist, however, emulating the skills of free running can enhance the capabilities of  [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Thursday, 28 March 2024 )