Skip to content

Instantly share code, notes, and snippets.

@mickael-kerjean
Created January 5, 2026 06:02
Show Gist options
  • Select an option

  • Save mickael-kerjean/705d891e05870768946501b60ad19b8a to your computer and use it in GitHub Desktop.

Select an option

Save mickael-kerjean/705d891e05870768946501b60ad19b8a to your computer and use it in GitHub Desktop.
poc for wasm plugin in Filestash - to remove
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/tetratelabs/wazero"
)
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <wasm-file>\n", os.Args[0])
os.Exit(1)
}
// init
wasmFile := os.Args[1]
wasmBytes, err := os.ReadFile(wasmFile)
if err != nil {
log.Fatalf("Failed to read WASM file: %v", err)
}
ctx := context.Background()
runtime := wazero.NewRuntime(ctx)
defer runtime.Close(ctx)
compiledModule, err := runtime.CompileModule(ctx, wasmBytes)
if err != nil {
log.Fatalf("Failed to compile WASM: %v", err)
}
module, err := runtime.InstantiateModule(ctx, compiledModule, wazero.NewModuleConfig())
if err != nil {
log.Fatalf("Failed to instantiate WASM: %v", err)
}
httpFunc := module.ExportedFunction("http")
if httpFunc == nil {
log.Fatalf("WASM module does not export 'http' function")
}
// run
results, err := httpFunc.Call(ctx)
if err != nil {
log.Fatalf("Failed to call http function: %v", err)
} else if len(results) != 1 {
log.Fatalf("Failed to parse response")
return
}
// output - read null-terminated string
ptr := uint32(results[0])
memory := module.Memory()
var responseLength uint32
for offset := uint32(0); ; offset += 8192 {
chunk, ok := memory.Read(ptr+offset, 8192)
if !ok {
responseLength = offset
break
}
for i, b := range chunk {
if b == 0 {
responseLength = offset + uint32(i)
goto found
}
}
}
found:
responseBytes, _ := memory.Read(ptr, responseLength)
resp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(responseBytes)), nil)
defer resp.Body.Close()
fmt.Printf("- status %d\n", resp.StatusCode)
for head, value := range resp.Header {
fmt.Printf("- header %s => %v\n", head, value)
}
body, _ := io.ReadAll(resp.Body)
fmt.Printf("- body %s\n", body)
}
.PHONY: all build plugin clean
all: build plugin
build:
go build -o wasm-http main.go
plugin:
cargo build --target wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/plugin.wasm .
test: all
./wasm-http plugin.wasm
clean:
rm -f wasm-http plugin.wasm
#[no_mangle]
pub extern "C" fn http() -> *const u8 {
b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 13\r\n\r\nHello, WASM!\0".as_ptr()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment