Skip to content

Commit

Permalink
webcrypto: make WebCrypto globally available
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Feb 20, 2025
1 parent 90b59cc commit f300d31
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
15 changes: 14 additions & 1 deletion internal/js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.k6.io/k6/internal/event"
"go.k6.io/k6/internal/js/compiler"
"go.k6.io/k6/internal/js/eventloop"
"go.k6.io/k6/internal/js/modules/k6/webcrypto"
"go.k6.io/k6/internal/js/tc55/timers"
"go.k6.io/k6/internal/loader"
"go.k6.io/k6/js/common"
Expand Down Expand Up @@ -314,10 +315,11 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64) (*BundleInstance
modSys := modules.NewModuleSystem(b.ModuleResolver, vuImpl)
b.setInitGlobals(rt, vuImpl, modSys)

err = timers.SetupGlobally(vuImpl)
err = registerGlobals(vuImpl)
if err != nil {
return nil, err
}

vuImpl.initEnv = initenv
defer func() {
vuImpl.initEnv = nil
Expand Down Expand Up @@ -383,6 +385,17 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64) (*BundleInstance
return bi, nil
}

// registerGlobals registers the globals for the runtime.
// e.g. timers and webcrypto.
func registerGlobals(vuImpl *moduleVUImpl) error {
err := timers.SetupGlobally(vuImpl)
if err != nil {
return err
}

return webcrypto.SetupGlobally(vuImpl)
}

func (b *Bundle) setupJSRuntime(rt *sobek.Runtime, vuID uint64, logger logrus.FieldLogger) error {
rt.SetFieldNameMapper(common.FieldNameMapper{})
rt.SetRandSource(common.NewRandSource())
Expand Down
27 changes: 13 additions & 14 deletions internal/js/jsmodules.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ import (

func getInternalJSModules() map[string]interface{} {
return map[string]interface{}{
"k6": k6.New(),
"k6/crypto": crypto.New(),
"k6/crypto/x509": x509.New(),
"k6/data": data.New(),
"k6/encoding": encoding.New(),
"k6/timers": timers.New(),
"k6/execution": execution.New(),
"k6/experimental/csv": csv.New(),
"k6/experimental/redis": redis.New(),
"k6/experimental/streams": streams.New(),
"k6/experimental/webcrypto": webcrypto.New(),
"k6": k6.New(),
"k6/crypto": crypto.New(),
"k6/crypto/x509": x509.New(),
"k6/data": data.New(),
"k6/encoding": encoding.New(),
"k6/timers": timers.New(),
"k6/execution": execution.New(),
"k6/experimental/csv": csv.New(),
"k6/experimental/redis": redis.New(),
"k6/experimental/streams": streams.New(),
"k6/experimental/webcrypto": newWarnExperimentalModule(webcrypto.New(),
"k6/experimental/webcrypto is now part of the k6 core, and globally available. You could just remove import."+
" The k6/experimental/webcrypto will be removed in k6 v1.1.0"),
"k6/experimental/websockets": expws.New(),
"k6/experimental/timers": newRemovedModule(
"k6/experimental/timers has been graduated, please use k6/timers instead."),
Expand Down Expand Up @@ -80,14 +82,12 @@ func getJSModules() map[string]interface{} {
return result
}

//nolint:unused // this is likely going to be used again even if isn't currently used
type warnExperimentalModule struct {
once *sync.Once
msg string
base modules.Module
}

//nolint:unused // this is likely going to be used again even if isn't currently used
func newWarnExperimentalModule(base modules.Module, msg string) modules.Module {
return &warnExperimentalModule{
msg: msg,
Expand All @@ -96,7 +96,6 @@ func newWarnExperimentalModule(base modules.Module, msg string) modules.Module {
}
}

//nolint:unused // this is likely going to be used again even if isn't currently used
func (w *warnExperimentalModule) NewModuleInstance(vu modules.VU) modules.Instance {
w.once.Do(func() { vu.InitEnv().Logger.Warn(w.msg) })
return w.base.NewModuleInstance(vu)
Expand Down
13 changes: 12 additions & 1 deletion internal/js/modules/k6/webcrypto/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"go.k6.io/k6/js/modules"
)

const cryptoGlobalIdentifier = "crypto"

type (
// RootModule is the global module instance that will create Client
// instances for each VU.
Expand Down Expand Up @@ -43,10 +45,19 @@ func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
// the exports of the JS module.
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{Named: map[string]interface{}{
"crypto": newCryptoObject(mi.vu),
"crypto": mi.vu.Runtime().GlobalObject().Get(cryptoGlobalIdentifier),
}}
}

// SetupGlobally sets the crypto object globally.
func SetupGlobally(vu modules.VU) error {
if err := vu.Runtime().Set(cryptoGlobalIdentifier, newCryptoObject(vu)); err != nil {
return fmt.Errorf("unable to set crypto object globally; reason: %w", err)
}

return nil
}

func newCryptoObject(vu modules.VU) *sobek.Object {
rt := vu.Runtime()

Expand Down
10 changes: 2 additions & 8 deletions internal/js/modules/k6/webcrypto/tests/test_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package tests
import (
"testing"

"go.k6.io/k6/internal/js/compiler"
k6encoding "go.k6.io/k6/internal/js/modules/k6/encoding"
"go.k6.io/k6/internal/js/modules/k6/webcrypto"
"go.k6.io/k6/js/modulestest"
Expand All @@ -14,7 +13,7 @@ import (
)

const initGlobals = `
globalThis.CryptoKey = require("k6/x/webcrypto").CryptoKey;
globalThis.CryptoKey = crypto.CryptoKey;
`

// newConfiguredRuntime initializes a new test setup.
Expand All @@ -29,12 +28,7 @@ func newConfiguredRuntime(t testing.TB) *modulestest.Runtime {
_, err = rt.VU.Runtime().RunString("var self = this;")
require.NoError(t, err)

err = rt.SetupModuleSystem(
map[string]interface{}{"k6/x/webcrypto": webcrypto.New()},
nil,
compiler.New(rt.VU.InitEnv().Logger),
)
require.NoError(t, err)
require.NoError(t, webcrypto.SetupGlobally(rt.VU))

// We compile the Web Platform testharness script into a sobek.Program
compileAndRun(t, rt, "./wpt/resources", "testharness.js")
Expand Down
3 changes: 3 additions & 0 deletions js/modulestest/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"go.k6.io/k6/internal/js/compiler"
"go.k6.io/k6/internal/js/eventloop"
"go.k6.io/k6/internal/js/tc55/timers"

"go.k6.io/k6/internal/js/modules/k6/webcrypto"
"go.k6.io/k6/internal/lib/testutils"
"go.k6.io/k6/internal/usage"
"go.k6.io/k6/js/common"
Expand Down Expand Up @@ -56,6 +58,7 @@ func NewRuntime(t testing.TB) *Runtime {
BuiltinMetrics: metrics.RegisterBuiltinMetrics(vu.InitEnvField.Registry),
}
require.NoError(t, timers.SetupGlobally(vu))
require.NoError(t, webcrypto.SetupGlobally(vu))
// let's cancel again in case it has changed
t.Cleanup(func() { result.CancelContext() })
return result
Expand Down

0 comments on commit f300d31

Please sign in to comment.