added native echo.HandlerFunc support and .staticDirectoryHandler bind

This commit is contained in:
Gani Georgiev
2023-07-24 21:11:55 +03:00
parent 99ea916c14
commit 8dfc90985b
7 changed files with 6938 additions and 9881 deletions
+41 -18
View File
@@ -144,31 +144,19 @@ func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
}
func routerBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
loader.Set("routerAdd", func(method string, path string, handler string, middlewares ...goja.Value) {
loader.Set("routerAdd", func(method string, path string, handler goja.Value, middlewares ...goja.Value) {
wrappedMiddlewares, err := wrapMiddlewares(executors, middlewares...)
if err != nil {
panic("[routerAdd] failed to wrap middlewares: " + err.Error())
}
pr := goja.MustCompile("", "{("+handler+").apply(undefined, __args)}", true)
wrappedHandler, err := wrapHandler(executors, handler)
if err != nil {
panic("[routerAdd] failed to wrap handler: " + err.Error())
}
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.Add(strings.ToUpper(method), path, func(c echo.Context) error {
return executors.run(func(executor *goja.Runtime) error {
executor.Set("__args", []any{c})
res, err := executor.RunProgram(pr)
executor.Set("__args", goja.Undefined())
// check for returned error
if res != nil {
if v, ok := res.Export().(error); ok {
return v
}
}
return err
})
}, wrappedMiddlewares...)
e.Router.Add(strings.ToUpper(method), path, wrappedHandler, wrappedMiddlewares...)
return nil
})
@@ -199,6 +187,37 @@ func routerBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
})
}
func wrapHandler(executors *vmsPool, handler goja.Value) (echo.HandlerFunc, error) {
switch h := handler.Export().(type) {
case echo.HandlerFunc:
// "native" handler - no need to wrap
return h, nil
case func(goja.FunctionCall) goja.Value, string:
pr := goja.MustCompile("", "{("+handler.String()+").apply(undefined, __args)}", true)
wrappedHandler := func(c echo.Context) error {
return executors.run(func(executor *goja.Runtime) error {
executor.Set("__args", []any{c})
res, err := executor.RunProgram(pr)
executor.Set("__args", goja.Undefined())
// check for returned error
if res != nil {
if v, ok := res.Export().(error); ok {
return v
}
}
return err
})
}
return wrappedHandler, nil
default:
return nil, errors.New("unsupported goja handler type")
}
}
func wrapMiddlewares(executors *vmsPool, rawMiddlewares ...goja.Value) ([]echo.MiddlewareFunc, error) {
wrappedMiddlewares := make([]echo.MiddlewareFunc, len(rawMiddlewares))
@@ -531,6 +550,10 @@ func apisBinds(vm *goja.Runtime) {
obj := vm.NewObject()
vm.Set("$apis", obj)
obj.Set("staticDirectoryHandler", func(dir string, indexFallback bool) echo.HandlerFunc {
return apis.StaticDirectoryHandler(os.DirFS(dir), indexFallback)
})
// middlewares
obj.Set("requireRecordAuth", apis.RequireRecordAuth)
obj.Set("requireAdminAuth", apis.RequireAdminAuth)
+1 -1
View File
@@ -734,7 +734,7 @@ func TestApisBindsCount(t *testing.T) {
apisBinds(vm)
testBindsCount(vm, "this", 6, t)
testBindsCount(vm, "$apis", 10, t)
testBindsCount(vm, "$apis", 11, t)
}
func TestApisBindsApiError(t *testing.T) {
File diff suppressed because it is too large Load Diff
+8
View File
@@ -814,6 +814,14 @@ declare class UnauthorizedError implements apis.ApiError {
* @group PocketBase
*/
declare namespace $apis {
/**
* Route handler to serve static directory content (html, js, css, etc.).
*
* If a file resource is missing and indexFallback is set, the request
* will be forwarded to the base index.html (useful for SPA).
*/
export function staticDirectoryHandler(dir: string, indexFallback: boolean): echo.HandlerFunc
let requireRecordAuth: apis.requireRecordAuth
let requireAdminAuth: apis.requireAdminAuth
let requireAdminAuthOnlyIfAny: apis.requireAdminAuthOnlyIfAny