Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
Scarjit committed Feb 3, 2025
1 parent 4db3728 commit 2f950aa
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 0 deletions.
15 changes: 15 additions & 0 deletions wasm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: wasm build

wasm:
GOOS=js GOARCH=wasm go build -o output/main.wasm ./cmd/main.go && \
cp "$$(go env GOROOT)/lib/wasm/wasm_exec.js" ./output/ || \
cp "$$(go env GOROOT)/misc/wasm/wasm_exec.js" ./output/ || \
cp "/usr/local/go/lib/wasm/wasm_exec.js" ./output/ || \
cp "/usr/local/go/misc/wasm/wasm_exec.js" ./output/ || \
wget -O ./output/wasm_exec.js https://raw.githubusercontent.com/golang/go/refs/heads/release-branch.go1.24/lib/wasm/wasm_exec.js

build: wasm

webserver:
# This runs a simple webserver via python serving the files in the output directory
cd output && python3 -m http.server 8080
61 changes: 61 additions & 0 deletions wasm/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"encoding/json"
"fmt"
"syscall/js"

"github.com/redpanda-data/benthos/v4/public/bloblang"
"github.com/redpanda-data/benthos/v4/public/service"
)

func main() {
c := make(chan struct{}, 0)

js.Global().Set("blobl", js.FuncOf(blobl))

<-c // Keep the program running
}

func blobl(_ js.Value, args []js.Value) any {
if len(args) != 2 {
return fmt.Sprintf("Expected two arguments, received %d instead", len(args))
}

mapping, err := bloblang.NewEnvironment().Parse(args[0].String())
if err != nil {
return fmt.Sprintf("Failed to parse mapping: %s", err)
}

msg, err := service.NewMessage([]byte(args[1].String())).BloblangQuery(mapping)
if err != nil {
return fmt.Sprintf("Failed to execute mapping: %s", err)
}

message, err := msg.AsStructured()
if err != nil {
return fmt.Sprintf("Failed to marshal message: %s", err)
}

var metadata map[string]any
msg.MetaWalkMut(func(key string, value any) error {
if metadata == nil {
metadata = make(map[string]any)
}
metadata[key] = value
return nil
})

var output []byte
if output, err = json.MarshalIndent(struct {
Msg any `json:"msg"`
Meta map[string]any `json:"meta,omitempty"`
}{
Msg: message,
Meta: metadata,
}, "", " "); err != nil {
return fmt.Sprintf("Failed to marshal output: %s", err)
}

return string(output)
}
48 changes: 48 additions & 0 deletions wasm/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module github.com/united-manufacturing-hub/benthos-umh-wasm

go 1.23.4

require github.com/redpanda-data/benthos/v4 v4.41.1

require (
cuelang.org/go v0.9.2 // indirect
github.com/Jeffail/gabs/v2 v2.7.0 // indirect
github.com/Jeffail/shutdown v1.0.0 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/gorilla/handlers v1.5.2 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/matoous/go-nanoid/v2 v2.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/ksuid v1.0.4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tilinna/z85 v1.0.0 // indirect
github.com/urfave/cli/v2 v2.27.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 2f950aa

Please sign in to comment.