From 335d99cbb30bbb45921d63104298a3a4f6808ba8 Mon Sep 17 00:00:00 2001 From: Ivan <2103732+codebien@users.noreply.github.com> Date: Wed, 9 Feb 2022 17:15:44 +0100 Subject: [PATCH] Deprecated context-based utils --- api/common/context.go | 6 ++++++ js/common/bridge.go | 3 +++ js/common/context.go | 11 +++++++++++ js/initcontext.go | 10 ++++++++++ lib/context.go | 11 ++++++++++- 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/api/common/context.go b/api/common/context.go index 04cddcc00a8..2af957c26d1 100644 --- a/api/common/context.go +++ b/api/common/context.go @@ -30,10 +30,16 @@ type ContextKey int const ctxKeyEngine = ContextKey(1) +// WithEngine sets the k6 running Engine in the under the hood context. +// +// Deprecated: Use directly the Engine as dependency. func WithEngine(ctx context.Context, engine *core.Engine) context.Context { return context.WithValue(ctx, ctxKeyEngine, engine) } +// GetEngine returns the k6 running Engine fetching it from the context. +// +// Deprecated: Use directly the Engine as dependency. func GetEngine(ctx context.Context) *core.Engine { return ctx.Value(ctxKeyEngine).(*core.Engine) } diff --git a/js/common/bridge.go b/js/common/bridge.go index 3df71210086..b7b2ce389c7 100644 --- a/js/common/bridge.go +++ b/js/common/bridge.go @@ -134,6 +134,9 @@ func BindToGlobal(rt *goja.Runtime, data map[string]interface{}) func() { } // Bind the provided value v to the provided runtime +// +// Deprecated: JS modules can implement the modules.VU interface for getting the context, +// goja runtime and the VU State, so the goja.Runtime.Set method can be used for data binding. func Bind(rt *goja.Runtime, v interface{}, ctxPtr *context.Context) map[string]interface{} { exports := make(map[string]interface{}) diff --git a/js/common/context.go b/js/common/context.go index 9f992525ab8..125c66f2b7f 100644 --- a/js/common/context.go +++ b/js/common/context.go @@ -26,6 +26,9 @@ import ( "github.com/dop251/goja" ) +// TODO: https://github.com/grafana/k6/issues/2385 +// Rid all the context-based utils functions + type ctxKey int const ( @@ -34,11 +37,15 @@ const ( ) // WithRuntime attaches the given goja runtime to the context. +// +// Deprecated: Implement the modules.VU interface for sharing the Runtime. func WithRuntime(ctx context.Context, rt *goja.Runtime) context.Context { return context.WithValue(ctx, ctxKeyRuntime, rt) } // GetRuntime retrieves the attached goja runtime from the given context. +// +// Deprecated: Use modules.VU for get the Runtime. func GetRuntime(ctx context.Context) *goja.Runtime { v := ctx.Value(ctxKeyRuntime) if v == nil { @@ -48,11 +55,15 @@ func GetRuntime(ctx context.Context) *goja.Runtime { } // WithInitEnv attaches the given init environment to the context. +// +// Deprecated: Implement the modules.VU interface for sharing the init environment. func WithInitEnv(ctx context.Context, initEnv *InitEnvironment) context.Context { return context.WithValue(ctx, ctxKeyInitEnv, initEnv) } // GetInitEnv retrieves the attached init environment struct from the given context. +// +// Deprecated: Use modules.VU for get the init environment. func GetInitEnv(ctx context.Context) *InitEnvironment { v := ctx.Value(ctxKeyInitEnv) if v == nil { diff --git a/js/initcontext.go b/js/initcontext.go index 3eab0d553e7..7932abaf174 100644 --- a/js/initcontext.go +++ b/js/initcontext.go @@ -28,6 +28,7 @@ import ( "path/filepath" "runtime" "strings" + "sync" "github.com/dop251/goja" "github.com/sirupsen/logrus" @@ -201,6 +202,9 @@ func toESModuleExports(exp modules.Exports) interface{} { return result } +// TODO: https://github.com/grafana/k6/issues/2385 +var onceBindWarning sync.Once //nolint: gochecknoglobals + func (i *InitContext) requireModule(name string) (goja.Value, error) { mod, ok := i.modules[name] if !ok { @@ -214,6 +218,12 @@ func (i *InitContext) requireModule(name string) (goja.Value, error) { mod = perInstance.NewModuleInstancePerVU() } + onceBindWarning.Do(func() { + i.logger.Warnf(`Module '%s' is using deprecated APIs that will be removed in k6 v0.38.0,`+ + ` for more details on how to update it see`+ + ` https://k6.io/docs/extensions/guides/create-an-extension/#advanced-javascript-extension`, name) + }) + return i.moduleVUImpl.runtime.ToValue(common.Bind(i.moduleVUImpl.runtime, mod, i.moduleVUImpl.ctxPtr)), nil } diff --git a/lib/context.go b/lib/context.go index 64df24f3942..01606fda78e 100644 --- a/lib/context.go +++ b/lib/context.go @@ -20,7 +20,9 @@ package lib -import "context" +import ( + "context" +) type ctxKey int @@ -30,12 +32,19 @@ const ( ctxKeyScenario ) +// TODO: https://github.com/grafana/k6/issues/2385 +// Rid the State's context-based utils functions + // WithState embeds a State in ctx. +// +// Deprecated: Implement the modules.VU interface for sharing the State. func WithState(ctx context.Context, state *State) context.Context { return context.WithValue(ctx, ctxKeyState, state) } // GetState returns a State from ctx. +// +// Deprecated: Use modules.VU for get the State. func GetState(ctx context.Context) *State { v := ctx.Value(ctxKeyState) if v == nil {