MCPcopy Index your code
hub / github.com/pocketbase/pocketbase / wrapMiddlewares

Function wrapMiddlewares

plugins/jsvm/binds.go:221–290  ·  view source on GitHub ↗
(executors *vmsPool, rawMiddlewares ...goja.Value)

Source from the content-addressed store, hash-verified

219}
220
221func wrapMiddlewares(executors *vmsPool, rawMiddlewares ...goja.Value) ([]*hook.Handler[*core.RequestEvent], error) {
222 wrappedMiddlewares := make([]*hook.Handler[*core.RequestEvent], len(rawMiddlewares))
223
224 for i, m := range rawMiddlewares {
225 if m == nil {
226 return nil, errors.New("middleware must be non-nil")
227 }
228
229 switch v := m.Export().(type) {
230 case *hook.Handler[*core.RequestEvent]:
231 // "native" middleware handler - no need to wrap
232 wrappedMiddlewares[i] = v
233 case func(*core.RequestEvent) error:
234 // "native" middleware func - wrap as handler
235 wrappedMiddlewares[i] = &hook.Handler[*core.RequestEvent]{
236 Func: v,
237 }
238 case *gojaHookHandler:
239 if v.serializedFunc == "" {
240 return nil, errors.New("missing or invalid Middleware function")
241 }
242
243 pr := goja.MustCompile(defaultScriptPath, "{("+v.serializedFunc+").apply(undefined, __args)}", true)
244
245 wrappedMiddlewares[i] = &hook.Handler[*core.RequestEvent]{
246 Id: v.id,
247 Priority: v.priority,
248 Func: func(e *core.RequestEvent) error {
249 return executors.run(func(executor *goja.Runtime) error {
250 executor.Set("$app", e.App) // overwrite the global $app with the hook scoped instance
251 executor.Set("__args", []any{e})
252 res, err := executor.RunProgram(pr)
253 executor.Set("__args", goja.Undefined())
254
255 // check for returned Go error value
256 if resErr := checkGojaValueForError(e.App, res); resErr != nil {
257 return resErr
258 }
259
260 return normalizeException(err)
261 })
262 },
263 }
264 case func(goja.FunctionCall) goja.Value, string:
265 pr := goja.MustCompile(defaultScriptPath, "{("+m.String()+").apply(undefined, __args)}", true)
266
267 wrappedMiddlewares[i] = &hook.Handler[*core.RequestEvent]{
268 Func: func(e *core.RequestEvent) error {
269 return executors.run(func(executor *goja.Runtime) error {
270 executor.Set("$app", e.App) // overwrite the global $app with the hook scoped instance
271 executor.Set("__args", []any{e})
272 res, err := executor.RunProgram(pr)
273 executor.Set("__args", goja.Undefined())
274
275 // check for returned Go error value
276 if resErr := checkGojaValueForError(e.App, res); resErr != nil {
277 return resErr
278 }

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…