I introduced the concept of *Runnable in a previous article. A Runnable is a Wasm module that you can run using programs developed using Reactr.
You can also serve a Runnable as a web service, thanks to the Suborbital's Sat project.
One of Sat's features is to provide it with the URL of the Wasm module to download before serving it. So we need a Wasm module registry.
Last month, Wasmer announced the release of WAPM (https://wasmer.io/posts/wapm-revamp): a WebAssembly package manager.
WAPM happens to be perfect for hosting the Runnables. Let’s see how to do this.
Prerequisites
First, you need to create an account on https://wapm.io/ and create a private token to login on https://wapm.io/ when publishing a Wasm module.
Then, we need to install the Suborbital CLI (to create and build a Runnable) and Sat (to serve the Runnable as a service).
And finally, we need the WAPM CLI.
Install the Suborbital CLI
brew tap suborbital/subo && \
brew install subo
See https://github.com/suborbital/subo for more information
Build and install Sat
git clone --depth=1 https://github.com/suborbital/sat.git
cd sat
go build -o .bin/sat -tags netgo,wasmtime .
sudo cp .bin/sat /usr/local/bin/sat
cd ..
rm -rf sat
WAPM CLI
You only need to install Wasmer CLI (it comes with the WAPM CLI):
sudo apt install libncurses5 libxkbcommon0 libtinfo5 libnss3-tools -y
curl https://get.wasmer.io -sSfL | sh
source /home/gitpod/.wasmer/wasmer.sh
Now, it’s time to create, build and publish a Runnable.
Create a Swift Runnable
To create a Runnable with SwiftLang, type the below command:
subo create runnable forty-two --lang swift
Change the source code of Source/forty-two/main.swift
import Suborbital
class FortyTwo: Suborbital.Runnable {
func run(input: String) -> String {
return """
{"value":42}
"""
}
}
Suborbital.Set(runnable: FortyTwo())
Build the Runnable with this command:
subo build forty-two
You’ll get a Wasm module named
forty-two.wasm
Prepare the Runnable for publication
Add three files:
-
README.md
(add the needed content) -
LICENSE
(add the needed content) wapm.toml
Add this content to wapm.toml
:
[package]
name = "k33g/forty-two"
version = "1.0.0"
description = "wasm module for sat"
license = "MIT"
repository = "https://github.com/bots-garden/satellites"
readme = "README.md"
[[module]]
name = "forty-two"
source = "forty-two.wasm"
about the name of the package, use:
<your wapm user name>/<module name>
.
To generate the toml file, you can use the following command: wapm init
and follow the instructions.
Publish the package
It’s simple:
wapm login ${WAPM_REGISTRY_TOKEN}
cd forty-two
wapm publish
WAPM_REGISTRY_TOKEN
is the private token you created on https://wapm.io/.- You should get this kind of message: "Successfully published package
k33g/forty-two@1.0.0
"
Now, you can read the details about the package on https://wapm.io/k33g/forty-two and the URL to download the module is: https://registry-cdn.wapm.io/contents/k33g/forty-two/1.0.0/forty-two.wasm
Serve "forty-two" module
SAT_HTTP_PORT=8080 sat https://registry-cdn.wapm.io/contents/k33g/forty-two/1.0.0/forty-two.wasm
You’ll obtain an output like this:
{"log_message":"(W) configured to use HTTP with no TLS","timestamp":"2022-04-05T19:48:29.694827282Z","level":2,"app":{"sat_version":"v0.1.1"}}
{"log_message":"(I) starting forty-two ...","timestamp":"2022-04-05T19:48:29.695400533Z","level":3,"app":{"sat_version":"v0.1.1"}}
{"log_message":"(I) serving on :8080","timestamp":"2022-04-05T19:48:29.695481504Z","level":3,"app":{"sat_version":"v0.1.1"}}
You can query the forty-two service:
curl http://localhost:8080
The output will be: {"value":42}
That’s it. It’s effortless. If you want to update the package, don’t forget to increase the version number in the toml file.
See you to the next episode 🙂
Cover Photo by Matthew Henry from Burst
Top comments (0)