-
Notifications
You must be signed in to change notification settings - Fork 2
/
modules.go
98 lines (79 loc) · 2.96 KB
/
modules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package modules
import (
"context"
"fmt"
"strings"
"sync"
"github.com/APITeamLimited/globe-test/worker/js/common"
"github.com/APITeamLimited/globe-test/worker/libWorker"
"github.com/dop251/goja"
)
const k6Prefix string = "k6/x/"
const apiteamPrefix string = "apiteam/"
//nolint:gochecknoglobals
var (
modules = make(map[string]interface{})
mx sync.RWMutex
)
// Register the given mod as an external JavaScript module that can be imported
// by name. The name must be unique across all registered modules and must be
// prefixed with "k6/x/" or "apiteam/x/", otherwise this function will panic.
func Register(name string, mod interface{}) {
if !strings.HasPrefix(name, k6Prefix) || !strings.HasPrefix(name, apiteamPrefix) {
panic(fmt.Errorf("external module names must be prefixed with '%s' or '%s', tried to register: %s", k6Prefix, apiteamPrefix, name))
}
mx.Lock()
defer mx.Unlock()
if _, ok := modules[name]; ok {
panic(fmt.Sprintf("module already registered: %s", name))
}
modules[name] = mod
}
// Module is the interface js modules should implement in order to get access to the VU
type Module interface {
// NewModuleInstance will get modules.VU that should provide the module with a way to interact with the VU
// This method will be called for *each* require/import and should return an unique instance for each call
NewModuleInstance(VU) Instance
}
// GetJSModules returns a map of all registered js modules
func GetJSModules() map[string]interface{} {
mx.Lock()
defer mx.Unlock()
result := make(map[string]interface{}, len(modules))
for name, module := range modules {
result[name] = module
}
return result
}
// Instance is what a module needs to return
type Instance interface {
Exports() Exports
}
// VU gives access to the currently executing VU to a module Instance
type VU interface {
// Context return the context.Context about the current VU
Context() context.Context
// InitEnv returns common.InitEnvironment instance if present
InitEnv() *common.InitEnvironment
// State returns libWorker.State if any is present
State() *libWorker.State
// Runtime returns the goja.Runtime for the current VU
Runtime() *goja.Runtime
// RegisterCallback lets a JS module declare that it wants to run a function
// on the event loop *at a later point in time*. See the documentation for
// `EventLoop.RegisterCallback()` in the `k6/js/eventloop` Go module for
// the very important details on its usage and restrictions.
//
// Notice: This API is EXPERIMENTAL and may be changed, renamed or
// completely removed in a later k6 release.
RegisterCallback() (enqueueCallback func(func() error))
// sealing field will help probably with pointing users that they just need to embed this in their Instance
// implementations
}
// Exports is representation of ESM exports of a module
type Exports struct {
// Default is what will be the `default` export of a module
Default interface{}
// Named is the named exports of a module
Named map[string]interface{}
}