From 939f18c3d1c319c82ae7273796f9741f04cc96a8 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Thu, 6 Jun 2024 16:28:01 +0300 Subject: [PATCH] Change to using serbo instead of goja We are moving to a fork of goja under grafana org called serbo. More info in: - https://github.com/grafana/k6/issues/3772 - https://github.com/grafana/k6/issues/3773 --- go.mod | 8 +++-- go.sum | 38 +++++--------------- websockets/helpers.go | 4 +-- websockets/listeners.go | 24 ++++++------- websockets/params.go | 10 +++--- websockets/websockets.go | 76 ++++++++++++++++++++-------------------- 6 files changed, 70 insertions(+), 90 deletions(-) diff --git a/go.mod b/go.mod index 253f64b..00ebb90 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,14 @@ module github.com/grafana/xk6-websockets -go 1.18 +go 1.20 require ( - github.com/dop251/goja v0.0.0-20240220182346-e401ed450204 github.com/gorilla/websocket v1.5.1 + github.com/grafana/sobek v0.0.0-20240606091932-2da0e9e5f3e7 github.com/mstoykov/k6-taskqueue-lib v0.1.0 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.9.0 - go.k6.io/k6 v0.51.0 + go.k6.io/k6 v0.51.1-0.20240606120708-bd114fdbd683 go.uber.org/goleak v1.3.0 gopkg.in/guregu/null.v3 v3.3.0 ) @@ -22,6 +22,8 @@ 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/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 diff --git a/go.sum b/go.sum index cf2da40..3986b9b 100644 --- a/go.sum +++ b/go.sum @@ -10,22 +10,15 @@ github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsVi github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= -github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY= -github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic= -github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= -github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dlclark/regexp2 v1.9.0 h1:pTK/l/3qYIKaRXuHnEnIf7Y5NxfRPfpb7dis6/gdlVI= github.com/dlclark/regexp2 v1.9.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= -github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk= -github.com/dop251/goja v0.0.0-20240220182346-e401ed450204 h1:O7I1iuzEA7SG+dK8ocOBSlYAA9jBUmCYl/Qa7ey7JAM= -github.com/dop251/goja v0.0.0-20240220182346-e401ed450204/go.mod h1:QMWlm50DNe14hD7t24KEqZuUdC9sOTy8W6XbCU1mlw4= -github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y= -github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM= +github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2 h1:OFTHt+yJDo/uaIKMGjEKzc3DGhrpQZoqvMUIloZv6ZY= +github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2/go.mod h1:o31y53rb/qiIAONF7w3FHJZRqqP3fzHUr1HqanthByw= +github.com/evanw/esbuild v0.21.2 h1:CLplcGi794CfHLVmUbvVfTMKkykm+nyIHU8SU60KUTA= +github.com/evanw/esbuild v0.21.2/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -36,7 +29,6 @@ github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q= github.com/go-sourcemap/sourcemap v2.1.4+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= @@ -51,27 +43,21 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg= github.com/google/pprof v0.0.0-20230728192033-2ba5b33183c6 h1:ZgoomqkdjGbQ3+qQXCkvYMCDvGDNg2k5JJDjjdTB6jY= github.com/google/pprof v0.0.0-20230728192033-2ba5b33183c6/go.mod h1:Jh3hGz2jkYak8qXPD19ryItVnUgpgeqzdkY/D0EaeuA= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/grafana/sobek v0.0.0-20240606091932-2da0e9e5f3e7 h1:Ed0df3dkkPsjL0RKagJAv/821vrTBiB6GBk+198pxi4= +github.com/grafana/sobek v0.0.0-20240606091932-2da0e9e5f3e7/go.mod h1:6ZH0b0iOxyigeTh+/IlGoL0Hd3lVXA94xoXf0ldNgCM= github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 h1:Wqo399gCIufwto+VfwCSvsnfGpF/w5E9CNxSwbpD6No= github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYpldc7gVz2KMQwJ/QYCDIa7XU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/ianlancetaylor/demangle v0.0.0-20220319035150-800ac71e25c2/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg= github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -101,7 +87,6 @@ github.com/onsi/gomega v1.20.2 h1:8uQq0zMgLEfa0vRrrBgaJF2gyW9Da9BmfGV+OyUzfkY= github.com/onsi/gomega v1.20.2/go.mod h1:iYAIXgPSaDHak0LCMA+AWBpIKBr8WZicMxnE8luStNc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e h1:zWKUYT07mGmVBH+9UgnHXd/ekCK99C8EbDSAt5qsjXE= github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs= @@ -123,8 +108,8 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.k6.io/k6 v0.51.0 h1:TdMpPNwYuvtmBF1APoKFHuMnNzdIMI/XLaPklSjrIUw= -go.k6.io/k6 v0.51.0/go.mod h1:72d4MKuvKiRnkbPBGOk/gbz1aAcbKycX+GIlU7IOpuA= +go.k6.io/k6 v0.51.1-0.20240606120708-bd114fdbd683 h1:ockJVj41NNWzadyC3fvlSMRXT8QYxEzo13tVvE/WBEw= +go.k6.io/k6 v0.51.1-0.20240606120708-bd114fdbd683/go.mod h1:jKW0vrZjFqum5UGRPw/38ks4bYEywYuEo8vMccp/0Nc= go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 h1:t6wl9SPayj+c7lEIFgm4ooDBZVb01IhLB4InpomhRw8= @@ -179,7 +164,6 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -195,9 +179,7 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= @@ -229,10 +211,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/guregu/null.v3 v3.3.0 h1:8j3ggqq+NgKt/O7mbFVUFKUMWN+l1AmT5jQmJ6nPh2c= gopkg.in/guregu/null.v3 v3.3.0/go.mod h1:E4tX2Qe3h7QdL+uZ3a0vqvYwKQsRSQKM5V4YltdgH9Y= @@ -242,7 +221,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/websockets/helpers.go b/websockets/helpers.go index 0ea0dba..282b45f 100644 --- a/websockets/helpers.go +++ b/websockets/helpers.go @@ -1,12 +1,12 @@ package websockets import ( - "github.com/dop251/goja" + "github.com/grafana/sobek" "go.k6.io/k6/js/common" ) // must is a small helper that will panic if err is not nil. -func must(rt *goja.Runtime, err error) { +func must(rt *sobek.Runtime, err error) { if err != nil { common.Throw(rt, err) } diff --git a/websockets/listeners.go b/websockets/listeners.go index d3fe03f..0d6483a 100644 --- a/websockets/listeners.go +++ b/websockets/listeners.go @@ -3,7 +3,7 @@ package websockets import ( "fmt" - "github.com/dop251/goja" + "github.com/grafana/sobek" "github.com/grafana/xk6-websockets/websockets/events" ) @@ -34,10 +34,10 @@ func newEventListeners() *eventListeners { type eventListener struct { eventType string - // this return goja.value *and* error in order to return error on exception instead of panic + // this return sobek.value *and* error in order to return error on exception instead of panic // https://pkg.go.dev/github.com/dop251/goja#hdr-Functions - on func(goja.Value) (goja.Value, error) - list []func(goja.Value) (goja.Value, error) + on func(sobek.Value) (sobek.Value, error) + list []func(sobek.Value) (sobek.Value, error) } // newListener creates a new listener of a certain type @@ -48,27 +48,27 @@ func newListener(eventType string) *eventListener { } // add adds a listener to the listener list -func (l *eventListener) add(fn func(goja.Value) (goja.Value, error)) { +func (l *eventListener) add(fn func(sobek.Value) (sobek.Value, error)) { l.list = append(l.list, fn) } // setOn sets a listener for the on* properties, like onopen, onmessage, etc. -func (l *eventListener) setOn(fn func(goja.Value) (goja.Value, error)) { +func (l *eventListener) setOn(fn func(sobek.Value) (sobek.Value, error)) { l.on = fn } // getOn returns the on* property for a certain event type -func (l *eventListener) getOn() func(goja.Value) (goja.Value, error) { +func (l *eventListener) getOn() func(sobek.Value) (sobek.Value, error) { return l.on } // return all possible listeners for a certain event type -func (l *eventListener) all() []func(goja.Value) (goja.Value, error) { +func (l *eventListener) all() []func(sobek.Value) (sobek.Value, error) { if l.on == nil { return l.list } - return append([]func(goja.Value) (goja.Value, error){l.on}, l.list...) + return append([]func(sobek.Value) (sobek.Value, error){l.on}, l.list...) } // getTypes return event listener of a certain type @@ -92,7 +92,7 @@ func (l *eventListeners) getType(t string) *eventListener { } // add adds a listener to the listeners -func (l *eventListeners) add(t string, f func(goja.Value) (goja.Value, error)) error { +func (l *eventListeners) add(t string, f func(sobek.Value) (sobek.Value, error)) error { list := l.getType(t) if list == nil { @@ -105,11 +105,11 @@ func (l *eventListeners) add(t string, f func(goja.Value) (goja.Value, error)) e } // all returns all possible listeners for a certain event type or an empty array -func (l *eventListeners) all(t string) []func(goja.Value) (goja.Value, error) { +func (l *eventListeners) all(t string) []func(sobek.Value) (sobek.Value, error) { list := l.getType(t) if list == nil { - return []func(goja.Value) (goja.Value, error){} + return []func(sobek.Value) (sobek.Value, error){} } return list.all() diff --git a/websockets/params.go b/websockets/params.go index 8fa318c..395f688 100644 --- a/websockets/params.go +++ b/websockets/params.go @@ -6,7 +6,7 @@ import ( "net/http/cookiejar" "strings" - "github.com/dop251/goja" + "github.com/grafana/sobek" "go.k6.io/k6/js/common" httpModule "go.k6.io/k6/js/modules/k6/http" @@ -24,7 +24,7 @@ type wsParams struct { } // buildParams builds WebSocket params and configure some of them -func buildParams(state *lib.State, rt *goja.Runtime, raw goja.Value) (*wsParams, error) { +func buildParams(state *lib.State, rt *sobek.Runtime, raw sobek.Value) (*wsParams, error) { tagsAndMeta := state.Tags.GetCurrentValues() parsed := &wsParams{ @@ -35,7 +35,7 @@ func buildParams(state *lib.State, rt *goja.Runtime, raw goja.Value) (*wsParams, parsed.headers.Set("User-Agent", state.Options.UserAgent.String) - if raw == nil || goja.IsUndefined(raw) { + if raw == nil || sobek.IsUndefined(raw) { return parsed, nil } @@ -44,7 +44,7 @@ func buildParams(state *lib.State, rt *goja.Runtime, raw goja.Value) (*wsParams, switch k { case "headers": headersV := params.Get(k) - if goja.IsUndefined(headersV) || goja.IsNull(headersV) { + if sobek.IsUndefined(headersV) || sobek.IsNull(headersV) { continue } headersObj := headersV.ToObject(rt) @@ -60,7 +60,7 @@ func buildParams(state *lib.State, rt *goja.Runtime, raw goja.Value) (*wsParams, } case "jar": jarV := params.Get(k) - if goja.IsUndefined(jarV) || goja.IsNull(jarV) { + if sobek.IsUndefined(jarV) || sobek.IsNull(jarV) { continue } if v, ok := jarV.Export().(*httpModule.CookieJar); ok { diff --git a/websockets/websockets.go b/websockets/websockets.go index 7517e4d..7b4d268 100644 --- a/websockets/websockets.go +++ b/websockets/websockets.go @@ -13,8 +13,8 @@ import ( "sync" "time" - "github.com/dop251/goja" "github.com/gorilla/websocket" + "github.com/grafana/sobek" "github.com/grafana/xk6-websockets/websockets/events" "github.com/mstoykov/k6-taskqueue-lib/taskqueue" @@ -70,7 +70,7 @@ type webSocket struct { tagsAndMeta *metrics.TagsAndMeta tq *taskqueue.TaskQueue builtinMetrics *metrics.BuiltinMetrics - obj *goja.Object // the object that is given to js to interact with the WebSocket + obj *sobek.Object // the object that is given to js to interact with the WebSocket started time.Time done chan struct{} @@ -93,15 +93,15 @@ type ping struct { timestamps map[string]time.Time } -func isString(o *goja.Object, rt *goja.Runtime) bool { +func isString(o *sobek.Object, rt *sobek.Runtime) bool { return o.Prototype().Get("constructor") == rt.GlobalObject().Get("String") } -func isArray(o *goja.Object, rt *goja.Runtime) bool { +func isArray(o *sobek.Object, rt *sobek.Runtime) bool { return o.Prototype().Get("constructor") == rt.GlobalObject().Get("Array") } -func (r *WebSocketsAPI) websocket(c goja.ConstructorCall) *goja.Object { +func (r *WebSocketsAPI) websocket(c sobek.ConstructorCall) *sobek.Object { rt := r.vu.Runtime() url, err := parseURL(c.Argument(0)) @@ -149,8 +149,8 @@ func (r *WebSocketsAPI) websocket(c goja.ConstructorCall) *goja.Object { } // parseURL parses the url from the first constructor calls argument or returns an error -func parseURL(urlValue goja.Value) (*url.URL, error) { - if urlValue == nil || goja.IsUndefined(urlValue) { +func parseURL(urlValue sobek.Value) (*url.URL, error) { + if urlValue == nil || sobek.IsUndefined(urlValue) { return nil, errors.New("WebSocket requires a url") } @@ -176,30 +176,30 @@ const ( ) // defineWebsocket defines all properties and methods for the WebSocket -func defineWebsocket(rt *goja.Runtime, w *webSocket) { +func defineWebsocket(rt *sobek.Runtime, w *webSocket) { must(rt, w.obj.DefineDataProperty( - "addEventListener", rt.ToValue(w.addEventListener), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE)) + "addEventListener", rt.ToValue(w.addEventListener), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineDataProperty( - "send", rt.ToValue(w.send), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE)) + "send", rt.ToValue(w.send), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineDataProperty( - "ping", rt.ToValue(w.ping), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE)) + "ping", rt.ToValue(w.ping), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineDataProperty( - "close", rt.ToValue(w.close), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE)) + "close", rt.ToValue(w.close), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineDataProperty( - "url", rt.ToValue(w.url.String()), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE)) + "url", rt.ToValue(w.url.String()), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineAccessorProperty( // this needs to be with an accessor as we change the value "readyState", rt.ToValue(func() ReadyState { return w.readyState - }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE)) + }), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineAccessorProperty( - "bufferedAmount", rt.ToValue(func() goja.Value { return rt.ToValue(w.bufferedAmount) }), nil, - goja.FLAG_FALSE, goja.FLAG_TRUE)) + "bufferedAmount", rt.ToValue(func() sobek.Value { return rt.ToValue(w.bufferedAmount) }), nil, + sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineAccessorProperty("extensions", - rt.ToValue(func() goja.Value { return rt.ToValue(w.extensions) }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE)) + rt.ToValue(func() sobek.Value { return rt.ToValue(w.extensions) }), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineAccessorProperty( - "protocol", rt.ToValue(func() goja.Value { return rt.ToValue(w.protocol) }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE)) + "protocol", rt.ToValue(func() sobek.Value { return rt.ToValue(w.protocol) }), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineAccessorProperty( - "binaryType", rt.ToValue(func() goja.Value { + "binaryType", rt.ToValue(func() sobek.Value { return rt.ToValue(w.binaryType) }), rt.ToValue(func(s string) error { switch s { @@ -211,7 +211,7 @@ func defineWebsocket(rt *goja.Runtime, w *webSocket) { default: return fmt.Errorf("unknown binaryType %s, the supported one is arraybuffer", s) } - }), goja.FLAG_FALSE, goja.FLAG_TRUE)) + }), sobek.FLAG_FALSE, sobek.FLAG_TRUE)) setOn := func(property string, el *eventListener) { if el == nil { @@ -220,27 +220,27 @@ func defineWebsocket(rt *goja.Runtime, w *webSocket) { } must(rt, w.obj.DefineAccessorProperty( - property, rt.ToValue(func() goja.Value { + property, rt.ToValue(func() sobek.Value { return rt.ToValue(el.getOn) - }), rt.ToValue(func(call goja.FunctionCall) goja.Value { + }), rt.ToValue(func(call sobek.FunctionCall) sobek.Value { arg := call.Argument(0) // it's possible to unset handlers by setting them to null - if arg == nil || goja.IsUndefined(arg) || goja.IsNull(arg) { + if arg == nil || sobek.IsUndefined(arg) || sobek.IsNull(arg) { el.setOn(nil) return nil } - fn, isFunc := goja.AssertFunction(arg) + fn, isFunc := sobek.AssertFunction(arg) if !isFunc { common.Throw(rt, fmt.Errorf("a value for '%s' should be callable", property)) } - el.setOn(func(v goja.Value) (goja.Value, error) { return fn(goja.Undefined(), v) }) + el.setOn(func(v sobek.Value) (sobek.Value, error) { return fn(sobek.Undefined(), v) }) return nil - }), goja.FLAG_FALSE, goja.FLAG_TRUE)) + }), sobek.FLAG_FALSE, sobek.FLAG_TRUE)) } setOn("onmessage", w.eventListeners.getType(events.MESSAGE)) @@ -454,16 +454,16 @@ func (w *webSocket) queueMessage(msg *message) { } // TODO this technically could be BLOB , but we don't support that ab := rt.NewArrayBuffer(msg.data) - must(rt, ev.DefineDataProperty("data", rt.ToValue(ab), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE)) + must(rt, ev.DefineDataProperty("data", rt.ToValue(ab), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) } else { must( rt, - ev.DefineDataProperty("data", rt.ToValue(string(msg.data)), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE), + ev.DefineDataProperty("data", rt.ToValue(string(msg.data)), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE), ) } must( rt, - ev.DefineDataProperty("origin", rt.ToValue(w.url.String()), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE), + ev.DefineDataProperty("origin", rt.ToValue(w.url.String()), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE), ) for _, messageListener := range w.eventListeners.all(events.MESSAGE) { @@ -588,7 +588,7 @@ func (w *webSocket) writePump(wg *sync.WaitGroup) { } } -func (w *webSocket) send(msg goja.Value) { +func (w *webSocket) send(msg sobek.Value) { w.assertStateOpen() switch o := msg.Export().(type) { @@ -599,7 +599,7 @@ func (w *webSocket) send(msg goja.Value) { data: []byte(o), t: time.Now(), } - case *goja.ArrayBuffer: + case *sobek.ArrayBuffer: b := o.Bytes() w.bufferedAmount += len(b) w.writeQueueCh <- message{ @@ -607,7 +607,7 @@ func (w *webSocket) send(msg goja.Value) { data: b, t: time.Now(), } - case goja.ArrayBuffer: + case sobek.ArrayBuffer: b := o.Bytes() w.bufferedAmount += len(b) w.writeQueueCh <- message{ @@ -722,16 +722,16 @@ func (w *webSocket) connectionClosedWithError(err error) error { // newEvent return an event implementing "implements" https://dom.spec.whatwg.org/#event // needs to be called on the event loop // TODO: move to events -func (w *webSocket) newEvent(eventType string, t time.Time) *goja.Object { +func (w *webSocket) newEvent(eventType string, t time.Time) *sobek.Object { rt := w.vu.Runtime() o := rt.NewObject() must(rt, o.DefineAccessorProperty("type", rt.ToValue(func() string { return eventType - }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE)) + }), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, o.DefineAccessorProperty("target", rt.ToValue(func() interface{} { return w.obj - }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE)) + }), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) // skip srcElement // skip currentTarget ??!! // skip eventPhase ??!! @@ -743,7 +743,7 @@ func (w *webSocket) newEvent(eventType string, t time.Time) *goja.Object { must(rt, o.DefineAccessorProperty("timestamp", rt.ToValue(func() float64 { return float64(t.UnixNano()) / 1_000_000 // milliseconds as double as per the spec // https://w3c.github.io/hr-time/#dom-domhighrestimestamp - }), nil, goja.FLAG_FALSE, goja.FLAG_TRUE)) + }), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) return o } @@ -765,7 +765,7 @@ func (w *webSocket) callErrorListeners(e error) error { // TODO use the error ev ev := w.newEvent(events.ERROR, time.Now()) must(rt, ev.DefineDataProperty("error", rt.ToValue(e.Error()), - goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE)) + sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) for _, errorListener := range w.eventListeners.all(events.ERROR) { if _, err := errorListener(ev); err != nil { // TODO fix timestamp return err @@ -784,7 +784,7 @@ func (w *webSocket) callEventListeners(eventType string) error { return nil } -func (w *webSocket) addEventListener(event string, listener func(goja.Value) (goja.Value, error)) { +func (w *webSocket) addEventListener(event string, listener func(sobek.Value) (sobek.Value, error)) { // TODO support options https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#parameters if err := w.eventListeners.add(event, listener); err != nil { w.vu.State().Logger.Warnf("can't add event listener: %s", err)