Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to using sobek instead of goja #189

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading