Distribute a wasm SPA as HTML by wrapping it as a polyglot "html+wasm+zip"

Overview

A packer that adds a webpage to WASM module, making it self-hosted!

Motivation

At the moment, Browsers can not execute WebAssembly as a native single page app. Hopefully, this will change at some point. This 'packer' allows you to preserve the WebAssembly interpretation, unchanged, while adding an HTML loader as a form of polyfill for the native loader environment.

That said you could also regard it as a more general tool for WebAssembly plugins, as a form of hypervisor. Provide a stage2 loader that emulates the host environment within an HTML page. This could be, for instance, a WASI environment with a file system in local storage. Such an app can be ran natively or deployed to a browser as a 'hardware-agnostic' alternative, from a single binary file.

How to use it

The 'packer' acts as a filter for a WebAssembly module. It's a standard clap app if you want to explore its command line options.

# Assume you want to deploy a 'TodoMVC' app:
wasm-as-html --index-html /my/index.html /my/todomvc.js < /my/todomvc_bg.wasm > todomvc.html

See [examples/yew/Readme.md] for a specific description.

Or TodoMVC deployed on gh-pages.

Experimental

There's an experimental --edit flag. This replaces stage1 with an auto-reload driver, which will periodically refetch the document to compare hashes. It will the invoke the entrypoint with the new bytes, again.

How it works

The packer takes two arguments: A webassembly module, and a stage 2 loader javascript file that will be given control over the page and passed the a (fake) Promise to a Response that resolves to the web assembly bytes. The recommended stage 2 is some web-sys implementation that then passes control directly to a webassembly module. Its init and default export take this exact argument to instantiate the bindings marked as its main function.

The technical inner workings:

  • The first section in WASM is a custom one that parses as HTML. The trick take from here implemented in an program fashion. Note that this stage0 is not of arbitrary size. Otherwise the WebAssembly length encoding of the section's length will cause problems as they parse as good characters. This reads the first custom section of name polyglot_stage1.

  • The second section is another custom one, which is our stage1 with arbitrary contents. We almost immediately dispatch here from stage0 leaving behind all notions of HTML that we started with. We're now preparing the web page for the module packed as stage2. That is, loading a new skeleton with expected elements that the module might need to bootstrap itself.

    It reads the section title polyglot_stage1_html for contents of a new page, as well as the first polyglot_stage2 for the subsequent module.

WIP: additionally, an auxiliary .zip file can be passed. The packer then ensures that the result is also a valid zip archive with all files intact and such that they are accessible from the webassembly module as a custom module.

You might also like...
Just a little game I made in a day to try out the WASM-4 fantasy console.

Dodgeball This is just a little game I made in a day to try out the WASM-4 fantasy console. Play it here. The palette is SODA-CAP by Cappuchi. License

WebAssembly (Wasm) interpreter.

Continuous Integration Test Coverage Documentation Crates.io wasmi- WebAssembly (Wasm) Interpreter wasmi was conceived as a component of parity-ethere

📝 A template for creating WASM + Typescript + Rust workflow libraries.
📝 A template for creating WASM + Typescript + Rust workflow libraries.

Create Rust + TypeScript libraries with ease! PR'S WELCOMED! ✨ Inspiration I wanted to create a WebAssembly/Rust library with additional JS features,

Parametric surfaces drawn using the Rust + WASM toolchain with WebGL, React, and TypeScript.

Parametric Surfaces in the Browser My.Movie.3.mp4 Wanted to experiment with WebGL using the Rust + WASM toolchain, with React and TypeScript to glue e

Rust implementation of the Mina protocol, targeting Wasm and ARM architectures.

Mina-rs An implementation of Mina protocol in Rust, with focus on web and Wasm compatibility ** As you can probably tell this is a WIP! Don't use for

A self-guided learning project that includes Rust + Wasm together
A self-guided learning project that includes Rust + Wasm together

A self-guided learning project that includes Rust + Wasm together. Who knows, maybe Typescript and React joins too..

WASM-compiled built-in actors used by all Filecoin clients

Built-in Filecoin actors (v8) This repo contains the code for the on-chain built-in actors that power the Filecoin network starting from network versi

WASM bindings for React - enables you to write and use React components in Rust

This library enables you to write and use React components in Rust, which then can be exported to JS to be reused or rendered.

Realtime audio processing / synthesis using Rust/WASM in the browser.
Realtime audio processing / synthesis using Rust/WASM in the browser.

Rust Audio About This repo is my investigation into using Rust for creative audio coding on various platforms (e.g. desktop, web, etc.), but especiall

Owner
Andreas Molzer
Maintaining image-rs as a hobby :grimacing:
Andreas Molzer
The polyglot bindings generator for your library (C#, C, Python, …) 🐙

Interoptopus ?? The polyglot bindings generator for your library. Interoptopus allows you to deliver high-quality system libraries to your users, and

Ralf Biedert 155 Jan 3, 2023
A small application to convert a 3D model in .obj format into HTML and CSS

obj-to-html is a small application that converts a 3D model in .obj format into HTML and CSS that will display that model in a web browser, spinning a

Andrea 7 Dec 30, 2022
📦✨ your favorite rust -> wasm workflow tool!

?? ✨ wasm-pack Your favorite Rust → Wasm workflow tool! Docs | Contributing | Chat Built with ?? ?? by The Rust and WebAssembly Working Group About Th

Rust and WebAssembly 4.8k Jan 5, 2023
Facilitating high-level interactions between Wasm modules and JavaScript

wasm-bindgen Facilitating high-level interactions between Wasm modules and JavaScript. Guide | API Docs | Contributing | Chat Built with ?? ?? by The

Rust and WebAssembly 5.9k Jan 8, 2023
Gun port in rust & wasm

gun-rs-wasm Rust & WASM port of Gun. For a non-wasm version, check out gun-rs Example (source) Use npm install rusty-gun import { Node as Gun } from "

Martti Malmi 39 Dec 19, 2022
Install `wasm-pack` by downloading the executable

wasm-pack-action Install wasm-pack by downloading the executable (much faster than cargo install wasm-pack, seconds vs minutes). Usage - uses: jetli/w

Jet Li 33 Nov 23, 2022
`wasm-snip` replaces a WebAssembly function's body with an `unreachable`

wasm-snip wasm-snip replaces a Wasm function's body with an unreachable instruction. API Docs | Contributing | Chat Built with ?? ?? by The Rust and W

Rust and WebAssembly 177 Dec 28, 2022
gc-sections for wasm

wasm-gc Note: you probably don't need to use this project. This project is no longer necessary to run by hand, nor do you need the wasm-gc executable

Alex Crichton 245 Oct 22, 2022
List the symbols within a wasm file

wasm-nm List the symbols within a wasm file. Library Executable License Contributing Executable To install the wasm-nm executable, run $ cargo install

Nick Fitzgerald 38 Nov 6, 2022
Instrument and transform wasm modules.

wasm-instrument A Rust library containing a collection of wasm module instrumentations and transformations mainly useful for wasm based block chains a

Parity Technologies 31 Dec 16, 2022