mirror of
https://github.com/pocketbase/pocketbase.git
synced 2026-05-19 14:21:28 -04:00
set the current working directory as a default goja script path when executing inline JS strings
This commit is contained in:
@@ -61,7 +61,7 @@ func hooksBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
|
||||
|
||||
// register the hook to the loader
|
||||
loader.Set(jsName, func(callback string, tags ...string) {
|
||||
pr := goja.MustCompile("", "{("+callback+").apply(undefined, __args)}", true)
|
||||
pr := goja.MustCompile(defaultScriptPath, "{("+callback+").apply(undefined, __args)}", true)
|
||||
|
||||
tagsAsValues := make([]reflect.Value, len(tags))
|
||||
for i, tag := range tags {
|
||||
@@ -114,7 +114,7 @@ func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
|
||||
var wasServeTriggered bool
|
||||
|
||||
loader.Set("cronAdd", func(jobId, cronExpr, handler string) {
|
||||
pr := goja.MustCompile("", "{("+handler+").apply(undefined)}", true)
|
||||
pr := goja.MustCompile(defaultScriptPath, "{("+handler+").apply(undefined)}", true)
|
||||
|
||||
err := scheduler.Add(jobId, cronExpr, func() {
|
||||
err := executors.run(func(executor *goja.Runtime) error {
|
||||
@@ -215,7 +215,7 @@ func wrapHandler(executors *vmsPool, handler goja.Value) (echo.HandlerFunc, erro
|
||||
// "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)
|
||||
pr := goja.MustCompile(defaultScriptPath, "{("+handler.String()+").apply(undefined, __args)}", true)
|
||||
|
||||
wrappedHandler := func(c echo.Context) error {
|
||||
return executors.run(func(executor *goja.Runtime) error {
|
||||
@@ -253,7 +253,7 @@ func wrapMiddlewares(executors *vmsPool, rawMiddlewares ...goja.Value) ([]echo.M
|
||||
// "native" middleware - no need to wrap
|
||||
wrappedMiddlewares[i] = v
|
||||
case func(goja.FunctionCall) goja.Value, string:
|
||||
pr := goja.MustCompile("", "{(("+m.String()+").apply(undefined, __args)).apply(undefined, __args2)}", true)
|
||||
pr := goja.MustCompile(defaultScriptPath, "{(("+m.String()+").apply(undefined, __args)).apply(undefined, __args2)}", true)
|
||||
|
||||
wrappedMiddlewares[i] = func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
|
||||
@@ -35,9 +35,25 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/template"
|
||||
)
|
||||
|
||||
const (
|
||||
typesFileName = "types.d.ts"
|
||||
)
|
||||
const typesFileName = "types.d.ts"
|
||||
|
||||
var defaultScriptPath = "pb.js"
|
||||
|
||||
func init() {
|
||||
// For backward compatibility and consistency with the Go exposed
|
||||
// methods that accept relative paths (e.g. `$os.writeFile`),
|
||||
// we define the "current JS module" as if it is a file in the current working directory
|
||||
// (the filename itself doesn't really matter and in our case the hook handlers are executed as separate "programs").
|
||||
//
|
||||
// This is necessary for `require(module)` to properly traverse parents node_modules (goja_nodejs#95).
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
// truly rare case, log just for debug purposes
|
||||
color.Yellow("Failed to retrieve the current working directory: %v", err)
|
||||
} else {
|
||||
defaultScriptPath = filepath.Join(cwd, defaultScriptPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Config defines the config options of the jsvm plugin.
|
||||
type Config struct {
|
||||
@@ -187,7 +203,7 @@ func (p *plugin) registerMigrations() error {
|
||||
p.config.OnInit(vm)
|
||||
}
|
||||
|
||||
_, err := vm.RunString(string(content))
|
||||
_, err := vm.RunScript(defaultScriptPath, string(content))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to run migration %s: %w", file, err)
|
||||
}
|
||||
@@ -301,7 +317,7 @@ func (p *plugin) registerHooks() error {
|
||||
}
|
||||
}()
|
||||
|
||||
_, err := loader.RunString(string(content))
|
||||
_, err := loader.RunScript(defaultScriptPath, string(content))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user