npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wasm

v1.0.0

Published

**An experimental WebAssembly loader for Node.js**

Downloads

334

Readme

node-wasm

An experimental WebAssembly loader for Node.js

Usage

Load a .wasm file as a stringified JavaScript function:

var wasm = require('wasm')

var fnStr = wasm('/path/to/mod.wasm')

Alternatively, use the Node module system to load executable functions:

require('wasm/require') // requiring .wasm files now works

var fn = require('/path/to/mod.wasm')

Wat?

WebAssembly? Eh?

WebAssembly can be thought of as a step further than asm.js toward making an optimal compile-to target for the web. Emscripten does a nice job of encoding C++ code to asm.js but the resulting code is bloated and parse-time heavy. WebAssembly is intended to be compact and quick to load. Eventually WebAssembly is likely to make it possible to run all kinds of code in the browser without the need to compile directly to JavaScript.

Wow, what can I do with this?

Hold up there cowboy! You can't do much with this yet so don't get too excited.

This project is experimental for now and piggy-backs off the WebAssembly Polyfill prototype for the browser which compiles asm.js files to the current experimental WebAssembly binary format, which is far from standardised. So for now, this project is an exploration in what might be possible by combining WebAssembly and Node.js.

Does this need to be a native add-on?

Technically no, the current polyfill uses Emscripten to put the .wasm decoder into the browser so we could do the same with Node.

However, the simplest path for getting this running and allowing experimentation is to connect a decoder directly to Node via a native add-on. Perhaps in the future it will make more sense to distribute this as pure JavaScript.

Why bother putting this in Node.js?

WebAssembly on the server has the potential to be even more useful and interesting in the browser, depending on what you're trying to achieve. An efficient compile-to target with a single runtime could do for the server what the JVM attempted to do, except without the bloat and with the runtime model of Node.js.

Example

Found in the examples subdirectory.

Given an asm.js-compatible Fibonacci number calculator:

function fib(stdlib, foreign, heap) {
  "use asm";

  function fib(n) {
    n = n|0;
    var f1=0;
    var f2=0;
    if (n >>> 0 < 3) {
      return 1|0;
    }
    f1=fib(n-1)|0;
    f2=fib(n-2)|0;
    return f1 + f2;
  }
 
  return fib;
}

Compiled to .wasm. using the WebAssembly Polyfill prototype asm.js packer:

$ pack-asmjs fib.js fib.wasm

Gives us a 54 byte file:

7761 736d 3903 0000 0000 0001 0001 0000
0000 0000 0000 0001 0000 8204 1135 c0a3
1001 0fa1 8116 001f c0a1 8216 001f c0a2
0f1e c1c2 0000

Now we can execute it, passing global in place of the asm.js stdlib argument:

$ node -pe 'require("../require"); require("./fib.wasm")(global)(35)'
9227465

Authors and Contributors

Contributions are welcomed from experimenters wanting to join the fun!

License & Copyright

node-wasm is Copyright (c) 2015 NodeSource and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.