Skip to content

Commit

Permalink
Merge pull request #189 from grafana/useSerbo
Browse files Browse the repository at this point in the history
Change to using sobek instead of goja
  • Loading branch information
szkiba authored Jun 7, 2024
2 parents 7678d25 + adb08da commit 6327bc5
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 159 deletions.
36 changes: 18 additions & 18 deletions dashboard/customize.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"reflect"
"strings"

"github.com/dop251/goja"
"github.com/grafana/sobek"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/compiler"
"go.k6.io/k6/lib"
Expand Down Expand Up @@ -82,9 +82,9 @@ func exists(fs fsext.Fs, filename string) bool {
}

type configLoader struct {
runtime *goja.Runtime
runtime *sobek.Runtime
compiler *compiler.Compiler
defaultConfig *goja.Object
defaultConfig *sobek.Object
proc *process
}

Expand All @@ -96,9 +96,9 @@ func newConfigLoader(defaultConfig json.RawMessage, proc *process) (*configLoade

con := newConfigConsole(proc.logger)

runtime := goja.New()
runtime := sobek.New()

runtime.SetFieldNameMapper(goja.UncapFieldNameMapper())
runtime.SetFieldNameMapper(sobek.UncapFieldNameMapper())

if err := runtime.Set("console", con); err != nil {
return nil, err
Expand Down Expand Up @@ -140,11 +140,11 @@ func (loader *configLoader) load(filename string) (json.RawMessage, error) {
return obj.MarshalJSON()
}

func isObject(val goja.Value) bool {
func isObject(val sobek.Value) bool {
return val != nil && val.ExportType() != nil && val.ExportType().Kind() == reflect.Map
}

func (loader *configLoader) eval(src []byte, filename string) (*goja.Object, error) {
func (loader *configLoader) eval(src []byte, filename string) (*sobek.Object, error) {
prog, _, err := loader.compiler.Compile(string(src), filename, false)
if err != nil {
return nil, err
Expand All @@ -162,7 +162,7 @@ func (loader *configLoader) eval(src []byte, filename string) (*goja.Object, err
return nil, err
}

call, isCallable := goja.AssertFunction(val)
call, isCallable := sobek.AssertFunction(val)
if !isCallable {
return nil, fmt.Errorf("%w, file: %s", errNotFunction, filename)
}
Expand All @@ -177,7 +177,7 @@ func (loader *configLoader) eval(src []byte, filename string) (*goja.Object, err
return nil, fmt.Errorf("%w, file: %s", errNoExport, filename)
}

if call, isCallable = goja.AssertFunction(def); isCallable {
if call, isCallable = sobek.AssertFunction(def); isCallable {
def, err = call(exports, loader.defaultConfig)
if err != nil {
return nil, err
Expand All @@ -193,10 +193,10 @@ func (loader *configLoader) eval(src []byte, filename string) (*goja.Object, err

// toObject use JavaScript JSON.parse to create native goja object
// there could be a better solution.... (but Object.UnmarshallJSON is missing).
func toObject(runtime *goja.Runtime, bin json.RawMessage) (*goja.Object, error) {
func toObject(runtime *sobek.Runtime, bin json.RawMessage) (*sobek.Object, error) {
val := runtime.Get("JSON").ToObject(runtime).Get("parse")

call, _ := goja.AssertFunction(val)
call, _ := sobek.AssertFunction(val)

val, err := call(runtime.GlobalObject(), runtime.ToValue(string(bin)))
if err != nil {
Expand Down Expand Up @@ -229,7 +229,7 @@ func newConfigConsole(logger logrus.FieldLogger) *configConsole {
return &configConsole{logger.WithField("source", "console").WithField("extension", "dashboard")}
}

func (c configConsole) log(level logrus.Level, args ...goja.Value) {
func (c configConsole) log(level logrus.Level, args ...sobek.Value) {
var strs strings.Builder

for i := 0; i < len(args); i++ {
Expand Down Expand Up @@ -260,27 +260,27 @@ func (c configConsole) log(level logrus.Level, args ...goja.Value) {
}
}

func (c configConsole) Log(args ...goja.Value) {
func (c configConsole) Log(args ...sobek.Value) {
c.Info(args...)
}

func (c configConsole) Debug(args ...goja.Value) {
func (c configConsole) Debug(args ...sobek.Value) {
c.log(logrus.DebugLevel, args...)
}

func (c configConsole) Info(args ...goja.Value) {
func (c configConsole) Info(args ...sobek.Value) {
c.log(logrus.InfoLevel, args...)
}

func (c configConsole) Warn(args ...goja.Value) {
func (c configConsole) Warn(args ...sobek.Value) {
c.log(logrus.WarnLevel, args...)
}

func (c configConsole) Error(args ...goja.Value) {
func (c configConsole) Error(args ...sobek.Value) {
c.log(logrus.ErrorLevel, args...)
}

func (c configConsole) valueString(value goja.Value) string {
func (c configConsole) valueString(value sobek.Value) string {
mv, ok := value.(json.Marshaler)
if !ok {
return value.String()
Expand Down
6 changes: 3 additions & 3 deletions dashboard/customize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
_ "embed"
"testing"

"github.com/dop251/goja"
"github.com/grafana/sobek"
"github.com/sirupsen/logrus"
logtest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -89,9 +89,9 @@ func TestConfigInReadme(t *testing.T) {
func assertMessageAndLevel(t *testing.T, expr string, message string, level logrus.Level) {
t.Helper()

runtime := goja.New()
runtime := sobek.New()

runtime.SetFieldNameMapper(goja.UncapFieldNameMapper())
runtime.SetFieldNameMapper(sobek.UncapFieldNameMapper())

logger, hook := logtest.NewNullLogger()
_ = runtime.Set("console", newConfigConsole(logger))
Expand Down
55 changes: 28 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
module github.com/grafana/xk6-dashboard

go 1.19
go 1.20

require (
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d
github.com/grafana/sobek v0.0.0-20240607083612-4f0cd64f4e78
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/r3labs/sse/v2 v2.10.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/afero v1.1.2
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.0
go.k6.io/k6 v0.48.0
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1
go.k6.io/k6 v0.51.1-0.20240606120708-bd114fdbd683
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.9.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2 // indirect
github.com/evanw/esbuild v0.21.2 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/google/pprof v0.0.0-20230728192033-2ba5b33183c6 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
gopkg.in/guregu/null.v3 v3.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 6327bc5

Please sign in to comment.