MCPcopy
hub / github.com/pocketbase/pocketbase / wrapHandlerFunc

Function wrapHandlerFunc

plugins/jsvm/binds.go:181–213  ·  view source on GitHub ↗
(executors *vmsPool, handler goja.Value)

Source from the content-addressed store, hash-verified

179}
180
181func 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
215type gojaHookHandler struct {
216 id string

Callers 1

routerBindsFunction · 0.85

Calls 5

checkGojaValueForErrorFunction · 0.85
normalizeExceptionFunction · 0.85
runMethod · 0.65
SetMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…