Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exported url search params to be usable outside #18

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions gojaRuntime/gojaRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ var (
__nativeModules = nativeModules{
registered: map[string]*NativeModule{},
}
__afterVmSetupFunc = func(ctx context.Context, vm *goja.Runtime) {}
__afterVmSetupFunc = func(ctx context.Context, vm *goja.Runtime) {}
__beforeVmSetupFunc = func(ctx context.Context, vm *goja.Runtime) context.Context { return ctx }
)

func newGojaRunner() runtimesRegistry.Runner {
Expand Down Expand Up @@ -258,6 +259,13 @@ func AfterVMSetupFunc(afterVmSetup func(ctx context.Context, vm *goja.Runtime))
}
}

// BeforeVMSetupFunc allows to set a function that will be called before the VM is setup.
func BeforeVMSetupFunc(beforeVmSetup func(ctx context.Context, vm *goja.Runtime) context.Context) {
if beforeVmSetup != nil {
__beforeVmSetupFunc = beforeVmSetup
}
}

// RegisterNativeFunction registers a new native function which could be bound to and used at run-time.
func (module *NativeModule) RegisterNativeFunction(name string, fn func(ctx context.Context, binding runtimesRegistry.BindingSettings, jsContext JsContext, args ...interface{}) (interface{}, error)) {

Expand All @@ -277,6 +285,7 @@ func (module *NativeModule) RegisterNativeAPI(name string) *NativeModule {

func (e *GojaRunnerV1) Introspect(ctx context.Context, workflow runtimesRegistry.WorkflowDescriptor, options runtimesRegistry.IntrospectionOptions) (runtimesRegistry.IntrospectionResult, error) {
vm := goja.New()
ctx = __beforeVmSetupFunc(ctx, vm)
_, returnErr := e.setupVM(ctx, vm, workflow, options.Logger)
__afterVmSetupFunc(ctx, vm)

Expand Down Expand Up @@ -306,17 +315,18 @@ func (e *GojaRunnerV1) Introspect(ctx context.Context, workflow runtimesRegistry
func (e *GojaRunnerV1) Execute(ctx context.Context, workflow runtimesRegistry.WorkflowDescriptor, startOptions runtimesRegistry.StartOptions) (runtimesRegistry.ExecutionResult, error) {

vm := goja.New()
ctx = __beforeVmSetupFunc(ctx, vm)
executionResult, returnErr := e.setupVM(ctx, vm, workflow, startOptions.Loggger)
__afterVmSetupFunc(ctx, vm)

defer func(startedAt time.Time) {
executionResult.RunMetadata.ExecutionDuration = time.Since(startedAt)
}(executionResult.RunMetadata.StartedAt)

if returnErr != nil {
return executionResult, returnErr
}

defer func(startedAt time.Time) {
executionResult.RunMetadata.ExecutionDuration = time.Since(startedAt)
}(executionResult.RunMetadata.StartedAt)

module := vm.Get("module").ToObject(vm)
exportsJs := module.Get("exports")
if exportsJs == nil {
Expand Down
76 changes: 38 additions & 38 deletions gojaRuntime/url/nodeurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@
"strings"
)

type searchParam struct {
name string
value string
type SearchParam struct {
Name string
Value string
}

func (sp *searchParam) Encode() string {
func (sp *SearchParam) Encode() string {
return sp.string(true)
}

func escapeSearchParam(s string) string {
return escape(s, &tblEscapeURLQueryParam, true)
}

func (sp *searchParam) string(encode bool) string {
func (sp *SearchParam) string(encode bool) string {
if encode {
return escapeSearchParam(sp.name) + "=" + escapeSearchParam(sp.value)
return escapeSearchParam(sp.Name) + "=" + escapeSearchParam(sp.Value)
} else {
return sp.name + "=" + sp.value
return sp.Name + "=" + sp.Value

Check warning on line 25 in gojaRuntime/url/nodeurl.go

View check run for this annotation

Codecov / codecov/patch

gojaRuntime/url/nodeurl.go#L25

Added line #L25 was not covered by tests
evgenyk marked this conversation as resolved.
Show resolved Hide resolved
}
}

type searchParams []searchParam
type SearchParams []SearchParam

func (s searchParams) Len() int {
func (s SearchParams) Len() int {
return len(s)
}

func (s searchParams) Swap(i, j int) {
func (s SearchParams) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s searchParams) Less(i, j int) bool {
return strings.Compare(s[i].name, s[j].name) < 0
func (s SearchParams) Less(i, j int) bool {
return strings.Compare(s[i].Name, s[j].Name) < 0
}

func (s searchParams) Encode() string {
func (s SearchParams) Encode() string {
var sb strings.Builder
for i, v := range s {
if i > 0 {
Expand All @@ -51,7 +51,7 @@
return sb.String()
}

func (s searchParams) String() string {
func (s SearchParams) String() string {

Check warning on line 54 in gojaRuntime/url/nodeurl.go

View check run for this annotation

Codecov / codecov/patch

gojaRuntime/url/nodeurl.go#L54

Added line #L54 was not covered by tests
var sb strings.Builder
for i, v := range s {
if i > 0 {
Expand All @@ -63,68 +63,68 @@
}

type nodeURL struct {
url *url.URL
searchParams searchParams
Url *url.URL
SearchParams SearchParams
evgenyk marked this conversation as resolved.
Show resolved Hide resolved
}

type urlSearchParams nodeURL
type UrlSearchParams nodeURL

// This methods ensures that the url.URL has the proper RawQuery based on the searchParam
// structs. If a change is made to the searchParams we need to keep them in sync.
func (nu *nodeURL) syncSearchParams() {
if nu.rawQueryUpdateNeeded() {
nu.url.RawQuery = nu.searchParams.Encode()
nu.Url.RawQuery = nu.SearchParams.Encode()

Check warning on line 76 in gojaRuntime/url/nodeurl.go

View check run for this annotation

Codecov / codecov/patch

gojaRuntime/url/nodeurl.go#L76

Added line #L76 was not covered by tests
}
}

func (nu *nodeURL) rawQueryUpdateNeeded() bool {
return len(nu.searchParams) > 0 && nu.url.RawQuery == ""
return len(nu.SearchParams) > 0 && nu.Url.RawQuery == ""
}

func (nu *nodeURL) String() string {
return nu.url.String()
return nu.Url.String()
}

func (sp *urlSearchParams) hasName(name string) bool {
for _, v := range sp.searchParams {
if v.name == name {
func (sp *UrlSearchParams) hasName(name string) bool {
for _, v := range sp.SearchParams {
if v.Name == name {
return true
}
}
return false
}

func (sp *urlSearchParams) hasValue(name, value string) bool {
for _, v := range sp.searchParams {
if v.name == name && v.value == value {
func (sp *UrlSearchParams) hasValue(name, value string) bool {
for _, v := range sp.SearchParams {
if v.Name == name && v.Value == value {

Check warning on line 99 in gojaRuntime/url/nodeurl.go

View check run for this annotation

Codecov / codecov/patch

gojaRuntime/url/nodeurl.go#L97-L99

Added lines #L97 - L99 were not covered by tests
return true
}
}
return false
}

func (sp *urlSearchParams) getValues(name string) []string {
vals := make([]string, 0, len(sp.searchParams))
for _, v := range sp.searchParams {
if v.name == name {
vals = append(vals, v.value)
func (sp *UrlSearchParams) getValues(name string) []string {
vals := make([]string, 0, len(sp.SearchParams))
for _, v := range sp.SearchParams {
if v.Name == name {
vals = append(vals, v.Value)
}
}

return vals
}

func (sp *urlSearchParams) getFirstValue(name string) (string, bool) {
for _, v := range sp.searchParams {
if v.name == name {
return v.value, true
func (sp *UrlSearchParams) getFirstValue(name string) (string, bool) {
for _, v := range sp.SearchParams {
if v.Name == name {
return v.Value, true
}
}

return "", false
}

func parseSearchQuery(query string) (ret searchParams) {
func parseSearchQuery(query string) (ret SearchParams) {

Check warning on line 127 in gojaRuntime/url/nodeurl.go

View check run for this annotation

Codecov / codecov/patch

gojaRuntime/url/nodeurl.go#L127

Added line #L127 was not covered by tests
if query == "" {
return
}
Expand All @@ -138,9 +138,9 @@
pair := strings.SplitN(v, "=", 2)
l := len(pair)
if l == 1 {
ret = append(ret, searchParam{name: unescapeSearchParam(pair[0]), value: ""})
ret = append(ret, SearchParam{Name: unescapeSearchParam(pair[0]), Value: ""})

Check warning on line 141 in gojaRuntime/url/nodeurl.go

View check run for this annotation

Codecov / codecov/patch

gojaRuntime/url/nodeurl.go#L141

Added line #L141 was not covered by tests
} else if l == 2 {
ret = append(ret, searchParam{name: unescapeSearchParam(pair[0]), value: unescapeSearchParam(pair[1])})
ret = append(ret, SearchParam{Name: unescapeSearchParam(pair[0]), Value: unescapeSearchParam(pair[1])})

Check warning on line 143 in gojaRuntime/url/nodeurl.go

View check run for this annotation

Codecov / codecov/patch

gojaRuntime/url/nodeurl.go#L143

Added line #L143 was not covered by tests
}
}

Expand Down
Loading
Loading