Skip to content

Capsule Project: the nano wasm runners

What's new?

v0.4.2 ⛱️ [beach umbrella]:

  • Update with the last version of Wazero v1.4.0.
  • Capsule HTTP is faster thanks to the Wazero optimisations.
  • Capsule HDK: v0.0.7 (🐞 fix with Redis + Wazero v1.4.0)
  • Capsule MDK: v0.0.6 (🐞 fix with Redis)

v0.4.1 πŸ«‘ [pepper]:

  • Update with the last version of Wazero v1.3.0.
  • More "practical" sample: GitLab webhook sample.
  • Capsule HTTP is faster thanks to the Wazero optimisations.
  • Capsule HDK: v0.0.6
  • Capsule MDK: v0.0.5

What is the Capsule project?

Capsule is a set of WASM runners. Right now, the Capsule project is composed of:

  • Capsule CLI: to simply execute a WebAssembly module in a terminal
  • Capsule HTTP server to serve a WebAssembly module like a micro service or a function.
  • Capsule applications are developed with GoLang and thanks to the πŸ’œ Wazero project.
  • The wasm modules are developed in GoLang and compiled with TinyGo πŸ’œ (with the WASI specification)

Host DK & Module DK

πŸŽ‰ That means, since now, it's possible to develop various runners thanks to the Capsule Host SDK

Tutorials

More tutorials are coming soon!

  • Capsule: the WASM runners project: with this blog post I explain how to create WASM modules (with the MDK) for the Capsule CLI and the Capsule HTTP server, but too, how to create your Capsule application (with the HDK).

What does a WASM Capsule module look like?

WASM Module for the Capsule CLI

package main

import (
    capsule "github.com/bots-garden/capsule-module-sdk"
)

func main() {
    capsule.SetHandle(Handle)
}

// Handle function
func Handle(params []byte) ([]byte, error) {

    capsule.Print("Environment variable β†’ MESSAGE: " + capsule.GetEnv("MESSAGE"))

    err := capsule.WriteFile("./hello.txt", []byte("πŸ‘‹ Hello World! 🌍"))
    if err != nil {
        capsule.Print(err.Error())
    }

    data, err := capsule.ReadFile("./hello.txt")
    if err != nil {
        capsule.Print(err.Error())
    }
    capsule.Print("πŸ“: " + string(data))

    return []byte("πŸ‘‹ Hello " + string(params)), nil
}

WASM Module for the Capsule HTTP server

// Package main
package main

import (
    "strconv"
    "github.com/bots-garden/capsule-module-sdk"
    "github.com/valyala/fastjson"
)

func main() {
    capsule.SetHandleHTTP(Handle)
}

// Handle function 
func Handle(param capsule.HTTPRequest) (capsule.HTTPResponse, error) {

    capsule.Print("πŸ“: " + param.Body)
    capsule.Print("πŸ” : " + param.Method)
    capsule.Print("🌍: " + param.URI)
    capsule.Print("πŸ‘’: " + param.Headers)

    var p fastjson.Parser
    v, err := p.Parse(param.Body)
    if err != nil {
        capsule.Log(err.Error())
    }
    message := string(v.GetStringBytes("name")) + " " + strconv.Itoa(v.GetInt("age"))
    capsule.Log(message)

    response := capsule.HTTPResponse{
        JSONBody: `{"message": "`+message+`", "things":{"emoji":"🐯"}}`,
        Headers: `{"Content-Type": "application/json; charset=utf-8"}`,
        StatusCode: 200,
    }

    return response, nil
}

What are the added values of Capsule?

Capsule applications bring superpowers to the WASM modules with host functions. Thanks to these host functions, a WASM function can, for example, prints a message, reads files, writes to files, makes HTTP requests, ... See the host functions section.

Useful information for this project

History

Releases

  • v0.4.2 ⛱️ [beach umbrella]: update with the last version of Wazero v1.4.0 + 🐞 fix with Redis.
  • v0.4.1 πŸ«‘ [pepper]: update with the last version of Wazero v1.3.0 + GitLab webhook sample
  • v0.4.0 🌢️ [chili pepper]: update of HDK 0.0.4 then 0.0.5, (πŸŽ‰ performances: more than x 2 πŸš€). capsule-http: add of 2 endpoints (/metricsand /health) triggering the OnMetrics and OnHealthCheck functions of the WASM module.
  • v0.3.9 πŸ₯’ [cucumber]: update of HDK 0.0.3 with Wazero 1.2.0 and MDK 0.0.3 (encoding of the HTML string into JSON string, then it's easier to serve HTML)
  • v0.3.8 πŸ₯¬ [leafy greens]: πŸ› fixes of the FaaS mode
  • v0.3.7 πŸ₯¦ [broccoli]: πŸš€ FaaS mode (documentation in progress) + NGrok integration
  • v0.3.6 🫐 [blueberries]: Prometheus metrics + 🐳 Docker images
  • v0.3.5 πŸ“ [strawberry]: Update with HDK & MDK v0.0.2
  • v0.3.4 πŸ‹ [lemon]: Capsule next generation (performances: x 10 πŸš€)
  • 🌍 Downloads: https://github.com/bots-garden/capsule/releases/tag/v0.3.8
  • 🀚 With the previous version of the project, Capsule was an only one application to run as an HTTP server, a CLI, a NATS subscriber and publisher and a MQTT subscriber and publisher. In the future, we will reintroduce the capabilities of NATS and MQTT, but with separate runners.