Skip to content

Commit

Permalink
Merge pull request #7 from kinde-oss/ev/shared-native-modules
Browse files Browse the repository at this point in the history
Made native modules shared between runtime instances
  • Loading branch information
kinde-engineering authored Sep 30, 2024
2 parents 1fdfacd + d26b24d commit 95d6d44
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
30 changes: 21 additions & 9 deletions gojaRuntime/gojaRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ type (
}

actionResult struct {
ConsoleLog []interface{} `json:"console_log"`
ConsoleError []interface{} `json:"console_error"`
Context *jsContext `json:"context"`
ExitResult interface{} `json:"exit_result"`
ConsoleLog []interface{} `json:"console_log"`
ConsoleError []interface{} `json:"console_error"`
Context *jsContext `json:"context"`
ExitResult interface{} `json:"exit_result"`
RunMetadata *runtimesRegistry.ExecutionMetadata `json:"run_metadata"`
}
introspectedExport struct {
value interface{}
Expand All @@ -46,6 +47,11 @@ type (
}
)

// ExecutionMetadata implements runtime_registry.ExecutionResult.
func (a *actionResult) ExecutionMetadata() runtimesRegistry.ExecutionMetadata {
return *a.RunMetadata
}

// BindingsFrom implements runtime_registry.IntrospectedExport.
func (i introspectedExport) BindingsFrom(exportName string) map[string]runtimesRegistry.BindingSettings {
return i.bindings
Expand Down Expand Up @@ -162,14 +168,16 @@ func init() {
registry.RegisterNativeModule("url", urlModule.Require)
}

var __nativeModules = nativeModules{
registered: map[string]*NativeModule{},
}

func newGojaRunner() runtimesRegistry.Runner {
runner := GojaRunnerV1{
cache: &gojaCache{
cache: map[string]*goja.Program{},
},
nativeModules: nativeModules{
registered: map[string]*NativeModule{},
},
nativeModules: __nativeModules,
}
return &runner
}
Expand Down Expand Up @@ -228,13 +236,13 @@ type NativeModule struct {
name string
}

func (runner GojaRunnerV1) RegisterNativeAPI(name string) *NativeModule {
func RegisterNativeAPI(name string) *NativeModule {
result := &NativeModule{
functions: map[string]func(binding runtimesRegistry.BindingSettings, jsContext JsContext, args ...interface{}) (interface{}, error){},
modules: map[string]*NativeModule{},
name: name,
}
runner.nativeModules.registered[name] = result
__nativeModules.registered[name] = result
return result
}

Expand Down Expand Up @@ -344,6 +352,7 @@ func (e *GojaRunnerV1) Execute(ctx context.Context, workflow runtimesRegistry.Wo
}

executionResult.ExitResult = promise.Result().Export()
executionResult.RunMetadata.ExecutionDuration = time.Since(executionResult.RunMetadata.StartedAt)

return executionResult, nil
}
Expand All @@ -360,6 +369,9 @@ func (runner *GojaRunnerV1) setupVM(ctx context.Context, vm *goja.Runtime, workf
Context: &jsContext{
data: map[string]interface{}{},
},
RunMetadata: &runtimesRegistry.ExecutionMetadata{
StartedAt: time.Now(),
},
}

for name, binding := range workflow.RequestedBindings {
Expand Down
10 changes: 5 additions & 5 deletions registry/runtimeRegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ type (
Settings map[string]interface{} `json:"settings"`
}

// Bindings struct {
// Global map[string]ModuleBinding `json:"global"`
// Native map[string]ModuleBinding `json:"native"`
// }

RuntimeLimits struct {
MaxExecutionDuration time.Duration `json:"max_execution_duration"`
}
Expand All @@ -52,7 +47,12 @@ type (
GetValueAsMap(key string) (map[string]interface{}, error)
}

ExecutionMetadata struct {
StartedAt time.Time `json:"started_at"`
ExecutionDuration time.Duration `json:"execution_duration"`
}
ExecutionResult interface {
ExecutionMetadata() ExecutionMetadata
GetExitResult() interface{}
GetConsoleLog() []interface{}
GetConsoleError() []interface{}
Expand Down
4 changes: 3 additions & 1 deletion runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func Test_GojaPrecompiledRuntime(t *testing.T) {
idTokenMap, err := result.GetContext().GetValueAsMap("idToken")
assert.Nil(err)
assert.Equal("bbb", idTokenMap["aaa"])
assert.Greater(result.ExecutionMetadata().ExecutionDuration.Nanoseconds(), int64(1))
assert.False(result.ExecutionMetadata().StartedAt.IsZero())
}
}

Expand Down Expand Up @@ -113,7 +115,7 @@ func testExecution(workflow projectBundler.KindeWorkflow, assert *assert.Asserti
func getGojaRunner() registry.Runner {
runtime, _ := GetRuntime("goja")

kindeAPI := runtime.(*gojaRuntime.GojaRunnerV1).RegisterNativeAPI("kinde")
kindeAPI := gojaRuntime.RegisterNativeAPI("kinde")
kindeAPI.RegisterNativeFunction("fetch", func(binding registry.BindingSettings, jsContext gojaRuntime.JsContext, args ...interface{}) (interface{}, error) {
return "fetch response", nil
})
Expand Down

0 comments on commit 95d6d44

Please sign in to comment.