重构部分 block 逻辑,调整 js 执行器的 kv 流程
This commit is contained in:
@@ -2,6 +2,7 @@ package filters
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||
)
|
||||
@@ -16,7 +17,7 @@ func FilterInstBlock(_ core.Params) (core.FilterInstance, error) {
|
||||
return nil, err
|
||||
}
|
||||
if param.Code == 0 {
|
||||
param.Code = http.StatusForbidden
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
if param.Message == "" {
|
||||
param.Message = http.StatusText(param.Code)
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
)
|
||||
|
||||
// todo: 新增超时配置
|
||||
|
||||
func FilterInstGoJa(gl core.Params) (core.FilterInstance, error) {
|
||||
var global struct {
|
||||
Timeout time.Duration `json:"timeout"`
|
||||
@@ -50,7 +51,7 @@ func FilterInstGoJa(gl core.Params) (core.FilterInstance, error) {
|
||||
|
||||
debug := NewDebug(global.EnableDebug && param.Debug && request.URL.Query().
|
||||
Get("debug") == "true", request, w)
|
||||
program, err := goja.Compile("main.js", js, false)
|
||||
program, err := goja.Compile(param.Exec, js, false)
|
||||
if err != nil {
|
||||
return debug.Flush(err)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package goja
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/pkg/errors"
|
||||
@@ -12,22 +11,21 @@ import (
|
||||
|
||||
func KVInject(ctx core.FilterContext, jsCtx *goja.Runtime) error {
|
||||
return jsCtx.GlobalObject().Set("kv", map[string]interface{}{
|
||||
"repo": func(group string) (goja.Value, error) {
|
||||
return kvResult(ctx.RepoDB)(ctx, jsCtx, group)
|
||||
"repo": func(group ...string) (goja.Value, error) {
|
||||
return kvResult(ctx.RepoDB)(ctx, jsCtx, group...)
|
||||
},
|
||||
"org": func(group string) (goja.Value, error) {
|
||||
return kvResult(ctx.OrgDB)(ctx, jsCtx, group)
|
||||
"org": func(group ...string) (goja.Value, error) {
|
||||
return kvResult(ctx.OrgDB)(ctx, jsCtx, group...)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func kvResult(db kv.CursorPagedKV) func(ctx core.FilterContext, jsCtx *goja.Runtime, group string) (goja.Value, error) {
|
||||
return func(ctx core.FilterContext, jsCtx *goja.Runtime, group string) (goja.Value, error) {
|
||||
group = strings.TrimSpace(group)
|
||||
if group == "" {
|
||||
func kvResult(db kv.CursorPagedKV) func(ctx core.FilterContext, jsCtx *goja.Runtime, group ...string) (goja.Value, error) {
|
||||
return func(ctx core.FilterContext, jsCtx *goja.Runtime, group ...string) (goja.Value, error) {
|
||||
if len(group) == 0 {
|
||||
return goja.Undefined(), errors.New("invalid group")
|
||||
}
|
||||
db := db.Child(group).(kv.CursorPagedKV)
|
||||
db := db.Child(group...).(kv.CursorPagedKV)
|
||||
return jsCtx.ToValue(map[string]interface{}{
|
||||
"get": func(key string) (goja.Value, error) {
|
||||
get, err := db.Get(ctx, key)
|
||||
|
||||
Reference in New Issue
Block a user