Skip to content

Commit

Permalink
support fetch() for real.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Nov 25, 2023
1 parent 975b45f commit 6c6d515
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
65 changes: 47 additions & 18 deletions custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package main
import (
"context"
"os"
"runtime"

"github.com/fiatjaf/quickjs-go"
"github.com/fiatjaf/quickjs-go/polyfill/pkg/console"
"github.com/fiatjaf/quickjs-go/polyfill/pkg/fetch"
"github.com/fiatjaf/quickjs-go/polyfill/pkg/timer"
"github.com/nbd-wtf/go-nostr"
"github.com/quickjs-go/quickjs-go"
)

type scriptPath string
Expand Down Expand Up @@ -41,10 +45,10 @@ func rejectEvent(ctx context.Context, event *nostr.Event) (reject bool, msg stri
jsEvent.Set("kind", qjs.Int32(int32(event.Kind)))
jsEvent.Set("created_at", qjs.Int64(int64(event.CreatedAt)))
jsTags := qjs.Array()
for i, tag := range event.Tags {
jsTags.SetByUint32(uint32(i), qjsStringArray(qjs, tag))
for _, tag := range event.Tags {
jsTags.Push(qjsStringArray(qjs, tag))
}
jsEvent.Set("tags", jsTags)
jsEvent.Set("tags", jsTags.ToValue())
return jsEvent
})
}
Expand Down Expand Up @@ -84,46 +88,71 @@ func rejectFilter(ctx context.Context, filter nostr.Filter) (reject bool, msg st
}

func runAndGetResult(scriptPath scriptPath, makeArgs ...func(qjs *quickjs.Context) quickjs.Value) (reject bool, msg string) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

rt := quickjs.NewRuntime()
defer rt.Free()
// defer rt.Close()

qjs := rt.NewContext()
defer qjs.Free()
// defer qjs.Close()

// inject fetch and setTimeout
fetch.InjectTo(qjs)
timer.InjectTo(qjs)
console.InjectTo(qjs)

// read code from user file
code, err := os.ReadFile("scripts/reject-event.js")
if err != nil {
return true, "missing policy"
}

// sane defaults
reject = true
msg = "failed to run policy script"

// function to get values back from js to here
qjs.Globals().Set("____grab", qjs.Function(func(qjs *quickjs.Context, this quickjs.Value, args []quickjs.Value) quickjs.Value {
if args[0].IsString() {
reject = true
msg = args[0].String()
} else if args[0].IsObject() && args[0].Has("then") {
args[0].Call("then", qjs.Function(func(qjs *quickjs.Context, this quickjs.Value, args []quickjs.Value) quickjs.Value {
if args[0].IsString() {
reject = true
msg = args[0].String()
} else {
reject = false
}
return qjs.Null()
}))
rt.ExecuteAllPendingJobs()
} else {
reject = false
}
return qjs.Undefined()
}))

// globals
args := qjs.Array()
for i, makeArg := range makeArgs {
args.SetByUint32(uint32(i), makeArg(qjs))
for _, makeArg := range makeArgs {
args.Push(makeArg(qjs))
}
qjs.Globals().Set("args", args)
qjs.Globals().Set("args", args.ToValue())

val, err := qjs.EvalFile(string(code), quickjs.EVAL_MODULE, "reject-event.js")
val, err := qjs.EvalFile(string(code), "reject-event.js")
defer val.Free()
if err != nil {
log.Warn().Err(err).Str("script", string(scriptPath)).Msg("error reading policy script")
return true, "error reading policy script"
}

val, err = qjs.Eval(`
import rejectEvent from './`+string(scriptPath)+`'
import rejectEvent from './` + string(scriptPath) + `'
let msg = rejectEvent(...args)
____grab(msg)
`, quickjs.EVAL_MODULE)
`)
defer val.Free()
if err != nil {
log.Warn().Err(err).Str("script", string(scriptPath)).Msg("error applying policy script")
Expand All @@ -135,16 +164,16 @@ ____grab(msg)

func qjsStringArray(qjs *quickjs.Context, src []string) quickjs.Value {
arr := qjs.Array()
for j, item := range src {
arr.SetByUint32(uint32(j), qjs.String(item))
for _, item := range src {
arr.Push(qjs.String(item))
}
return arr
return arr.ToValue()
}

func qjsIntArray(qjs *quickjs.Context, src []int) quickjs.Value {
arr := qjs.Array()
for j, item := range src {
arr.SetByUint32(uint32(j), qjs.Int32(int32(item)))
for _, item := range src {
arr.Push(qjs.Int32(int32(item)))
}
return arr
return arr.ToValue()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ toolchain go1.21.4
require (
github.com/fiatjaf/eventstore v0.2.11
github.com/fiatjaf/khatru v0.0.13
github.com/fiatjaf/quickjs-go v0.2.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/nbd-wtf/go-nostr v0.25.7
github.com/quickjs-go/quickjs-go v0.0.0-20230414054158-b72900cb68c1
github.com/rs/zerolog v1.31.0
github.com/urfave/cli/v2 v2.25.7
)
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/fiatjaf/eventstore v0.2.11 h1:hb3ImjJAX+aNvMd19sttbZD2PzgohRuZtOgVQLs
github.com/fiatjaf/eventstore v0.2.11/go.mod h1:Zx1XqwICh7RxxKLkgc0aXlVo298ABs4W5awP/1/bYYs=
github.com/fiatjaf/khatru v0.0.13 h1:yWKGnAvgNsxqG1OX0K60+yDd0nrcnGZrAPw14cgA+Tg=
github.com/fiatjaf/khatru v0.0.13/go.mod h1:ShdaRrzoZpCKjillsH7OS1O8xnxQcJP4bA5niBPUb/o=
github.com/fiatjaf/quickjs-go v0.2.1 h1:nTLusdLA9xRxnkha+xFLjKWP7LwmOBARZkNBa4TaL60=
github.com/fiatjaf/quickjs-go v0.2.1/go.mod h1:lYXCC+EmJ6YxXs128amkCmMXnimlO4dFCqY6fjwGC0M=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
Expand Down Expand Up @@ -120,8 +122,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/puzpuzpuz/xsync/v2 v2.5.1 h1:mVGYAvzDSu52+zaGyNjC+24Xw2bQi3kTr4QJ6N9pIIU=
github.com/puzpuzpuz/xsync/v2 v2.5.1/go.mod h1:gD2H2krq/w52MfPLE+Uy64TzJDVY7lP2znR9qmR35kU=
github.com/quickjs-go/quickjs-go v0.0.0-20230414054158-b72900cb68c1 h1:ewpMPEbRRTe0ifOn2zix4nGt8hDtVdvqH+ivifPvccs=
github.com/quickjs-go/quickjs-go v0.0.0-20230414054158-b72900cb68c1/go.mod h1:N0i1BZeGJnNJpvvrRXKRX3gE6Wdtpzmfx6PfoENb41s=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
Expand All @@ -135,7 +135,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
Expand Down

0 comments on commit 6c6d515

Please sign in to comment.