(executors *vmsPool, handler goja.Value)
| 179 | } |
| 180 | |
| 181 | func wrapHandlerFunc(executors *vmsPool, handler goja.Value) (func(*core.RequestEvent) error, error) { |
| 182 | if handler == nil { |
| 183 | return nil, errors.New("handler must be non-nil") |
| 184 | } |
| 185 | |
| 186 | switch h := handler.Export().(type) { |
| 187 | case func(*core.RequestEvent) error: |
| 188 | // "native" handler func - no need to wrap |
| 189 | return h, nil |
| 190 | case func(goja.FunctionCall) goja.Value, string: |
| 191 | pr := goja.MustCompile(defaultScriptPath, "{("+handler.String()+").apply(undefined, __args)}", true) |
| 192 | |
| 193 | wrappedHandler := func(e *core.RequestEvent) error { |
| 194 | return executors.run(func(executor *goja.Runtime) error { |
| 195 | executor.Set("$app", e.App) // overwrite the global $app with the hook scoped instance |
| 196 | executor.Set("__args", []any{e}) |
| 197 | res, err := executor.RunProgram(pr) |
| 198 | executor.Set("__args", goja.Undefined()) |
| 199 | |
| 200 | // check for returned Go error value |
| 201 | if resErr := checkGojaValueForError(e.App, res); resErr != nil { |
| 202 | return resErr |
| 203 | } |
| 204 | |
| 205 | return normalizeException(err) |
| 206 | }) |
| 207 | } |
| 208 | |
| 209 | return wrappedHandler, nil |
| 210 | default: |
| 211 | return nil, errors.New("unsupported goja handler type") |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | type gojaHookHandler struct { |
| 216 | id string |
no test coverage detected
searching dependent graphs…