新增goja
This commit is contained in:
49
pkg/filters/goja/goja.go
Normal file
49
pkg/filters/goja/goja.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package goja
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/require"
|
||||
"github.com/dop251/goja_nodejs/url"
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||
)
|
||||
|
||||
var FilterInstGoja core.FilterInstance = func(config core.FilterParams) (core.FilterCall, error) {
|
||||
var param struct {
|
||||
Exec string `json:"exec"`
|
||||
Debug bool `json:"debug"`
|
||||
}
|
||||
if err := config.Unmarshal(¶m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if param.Exec == "" {
|
||||
return nil, errors.Errorf("no exec specified")
|
||||
}
|
||||
return func(ctx core.FilterContext, writer http.ResponseWriter, request *http.Request, next core.NextCall) error {
|
||||
js, err := ctx.ReadString(ctx, param.Exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newRegistry(ctx)
|
||||
registry := newRegistry(ctx)
|
||||
vm := goja.New()
|
||||
_ = registry.Enable(vm)
|
||||
url.Enable(vm)
|
||||
vm.GlobalObject().Set("")
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newRegistry(ctx core.FilterContext) *require.Registry {
|
||||
registry := require.NewRegistry(
|
||||
require.WithLoader(func(path string) ([]byte, error) {
|
||||
return ctx.PageVFS.Read(ctx, path)
|
||||
}),
|
||||
require.WithPathResolver(func(base, path string) string {
|
||||
return filepath.Join(base, filepath.FromSlash(path))
|
||||
}))
|
||||
return registry
|
||||
}
|
||||
1
pkg/filters/goja/var_request.go
Normal file
1
pkg/filters/goja/var_request.go
Normal file
@@ -0,0 +1 @@
|
||||
package goja
|
||||
@@ -35,6 +35,7 @@ var FilterInstQuickJS core.FilterInstance = func(config core.FilterParams) (core
|
||||
|
||||
rt := quickjs.NewRuntime()
|
||||
rt.SetExecuteTimeout(5)
|
||||
rt.SetMemoryLimit(10 * 1024 * 1024)
|
||||
defer rt.Close()
|
||||
jsCtx := rt.NewContext()
|
||||
defer jsCtx.Close()
|
||||
|
||||
Reference in New Issue
Block a user