Skip to content

Commit

Permalink
added jsvm subscriptions.Message binding
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Oct 7, 2023
1 parent 49e3f4a commit e2f806d
Show file tree
Hide file tree
Showing 6 changed files with 5,070 additions and 4,987 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
- Removed the explicit charset from the realtime response due to compatability issues with IIS ([#3461](https://github.com/pocketbase/pocketbase/issues/3461)).
_The `Connection:keep-alive` realtime response header was also removed as it is not really used with HTTP2 anyway._

- Added new JSVM bindings:
- `new Cookie({ ... })` constructor for creating `*http.Cookie` equivalent value.
- `new SubscriptionMessage({ ... })` constructor for creating a realtime subscription payload.


## v0.18.9

Expand Down
6 changes: 6 additions & 0 deletions plugins/jsvm/binds.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/pocketbase/pocketbase/tools/mailer"
"github.com/pocketbase/pocketbase/tools/rest"
"github.com/pocketbase/pocketbase/tools/security"
"github.com/pocketbase/pocketbase/tools/subscriptions"
"github.com/pocketbase/pocketbase/tools/types"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -417,6 +418,11 @@ func baseBinds(vm *goja.Runtime) {
instance := &http.Cookie{}
return structConstructor(vm, call, instance)
})

vm.Set("SubscriptionMessage", func(call goja.ConstructorCall) *goja.Object {
instance := &subscriptions.Message{}
return structConstructor(vm, call, instance)
})
}

func dbxBinds(vm *goja.Runtime) {
Expand Down
33 changes: 32 additions & 1 deletion plugins/jsvm/binds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestBaseBindsCount(t *testing.T) {
vm := goja.New()
baseBinds(vm)

testBindsCount(vm, "this", 15, t)
testBindsCount(vm, "this", 16, t)
}

func TestBaseBindsReaderToString(t *testing.T) {
Expand Down Expand Up @@ -101,6 +101,37 @@ func TestBaseBindsCookie(t *testing.T) {
}
}

func TestBaseBindsSubscriptionMessage(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()

vm := goja.New()
baseBinds(vm)
vm.Set("bytesToString", func(b []byte) string {
return string(b)
})

_, err := vm.RunString(`
const payload = {
name: "test",
data: '{"test":123}'
}
const result = new SubscriptionMessage(payload);
if (result.name != payload.name) {
throw new("Expected name " + payload.name + ", got " + result.name);
}
if (bytesToString(result.data) != payload.data) {
throw new("Expected data '" + payload.data + "', got '" + bytesToString(result.data) + "'");
}
`)
if err != nil {
t.Fatal(err)
}
}

func TestBaseBindsRecord(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
Expand Down
Loading

0 comments on commit e2f806d

Please sign in to comment.