Skip to content
Snippets Groups Projects
Commit 6663cc6a authored by Jono Wenger's avatar Jono Wenger
Browse files

Fix request function

parent 80c3ce0c
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!5XX-4050 / SendE2e test
...@@ -11,8 +11,6 @@ package main ...@@ -11,8 +11,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/bindings" "gitlab.com/elixxir/client/bindings"
"gitlab.com/elixxir/xxdk-wasm/utils" "gitlab.com/elixxir/xxdk-wasm/utils"
"gitlab.com/elixxir/xxdk-wasm/wasm" "gitlab.com/elixxir/xxdk-wasm/wasm"
...@@ -27,6 +25,7 @@ func main() { ...@@ -27,6 +25,7 @@ func main() {
// utils/array.go // utils/array.go
js.Global().Set("Uint8ArrayToBase64", js.FuncOf(utils.Uint8ArrayToBase64)) js.Global().Set("Uint8ArrayToBase64", js.FuncOf(utils.Uint8ArrayToBase64))
js.Global().Set("Base64ToUint8Array", js.FuncOf(utils.Base64ToUint8Array)) js.Global().Set("Base64ToUint8Array", js.FuncOf(utils.Base64ToUint8Array))
js.Global().Set("Uint8ArrayEquals", js.FuncOf(utils.Uint8ArrayEquals))
// wasm/backup.go // wasm/backup.go
js.Global().Set("NewCmixFromBackup", js.FuncOf(wasm.NewCmixFromBackup)) js.Global().Set("NewCmixFromBackup", js.FuncOf(wasm.NewCmixFromBackup))
...@@ -126,14 +125,6 @@ func main() { ...@@ -126,14 +125,6 @@ func main() {
js.Global().Set("GetGitVersion", js.FuncOf(wasm.GetGitVersion)) js.Global().Set("GetGitVersion", js.FuncOf(wasm.GetGitVersion))
js.Global().Set("GetDependencies", js.FuncOf(wasm.GetDependencies)) js.Global().Set("GetDependencies", js.FuncOf(wasm.GetDependencies))
defer func() {
jww.CRITICAL.Printf("Before recover\n")
if rec := recover(); rec != nil {
utils.Throw(utils.TypeError, errors.Errorf(fmt.Sprintf("%+v", rec)))
}
jww.CRITICAL.Printf("After recover\n")
}()
<-make(chan bool) <-make(chan bool)
os.Exit(0) os.Exit(0)
} }
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
package utils package utils
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"syscall/js" "syscall/js"
) )
...@@ -39,3 +40,19 @@ func Base64ToUint8Array(_ js.Value, args []js.Value) interface{} { ...@@ -39,3 +40,19 @@ func Base64ToUint8Array(_ js.Value, args []js.Value) interface{} {
return CopyBytesToJS(b) return CopyBytesToJS(b)
} }
// Uint8ArrayEquals returns true if the two Uint8Array are equal and false
// otherwise.
//
// Parameters:
// - args[0] - array A (Uint8Array)
// - args[1] - array B (Uint8Array)
//
// Returns:
// - If the two arrays are equal (boolean).
func Uint8ArrayEquals(_ js.Value, args []js.Value) interface{} {
a := CopyBytesToGo(args[0])
b := CopyBytesToGo(args[1])
return bytes.Equal(a, b)
}
...@@ -24,6 +24,11 @@ import ( ...@@ -24,6 +24,11 @@ import (
"syscall/js" "syscall/js"
) )
var (
promiseConstructor = js.Global().Get("Promise")
Uint8Array = js.Global().Get("Uint8Array")
)
// CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is // CopyBytesToGo copies the Uint8Array stored in the js.Value to []byte. This is
// a wrapper for js.CopyBytesToGo to make it more convenient. // a wrapper for js.CopyBytesToGo to make it more convenient.
func CopyBytesToGo(src js.Value) []byte { func CopyBytesToGo(src js.Value) []byte {
...@@ -35,7 +40,7 @@ func CopyBytesToGo(src js.Value) []byte { ...@@ -35,7 +40,7 @@ func CopyBytesToGo(src js.Value) []byte {
// CopyBytesToJS copies the []byte to a Uint8Array stored in a js.Value. This is // CopyBytesToJS copies the []byte to a Uint8Array stored in a js.Value. This is
// a wrapper for js.CopyBytesToJS to make it more convenient. // a wrapper for js.CopyBytesToJS to make it more convenient.
func CopyBytesToJS(src []byte) js.Value { func CopyBytesToJS(src []byte) js.Value {
dst := js.Global().Get("Uint8Array").New(len(src)) dst := Uint8Array.New(len(src))
js.CopyBytesToJS(dst, src) js.CopyBytesToJS(dst, src)
return dst return dst
} }
...@@ -95,20 +100,15 @@ type PromiseFn func(resolve, reject func(args ...interface{}) js.Value) ...@@ -95,20 +100,15 @@ type PromiseFn func(resolve, reject func(args ...interface{}) js.Value)
func CreatePromise(f PromiseFn) interface{} { func CreatePromise(f PromiseFn) interface{} {
// Create handler for promise (this will be a Javascript function) // Create handler for promise (this will be a Javascript function)
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} { handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
// It receives two arguments, which are JS functions: resolve and reject
resolve := args[0]
reject := args[1]
// Spawn a new go routine to perform the blocking function // Spawn a new go routine to perform the blocking function
go func() { go func(resolve, reject js.Value) {
f(resolve.Invoke, reject.Invoke) f(resolve.Invoke, reject.Invoke)
}() }(args[0], args[1])
return nil return nil
}) })
// Create and return the Promise object // Create and return the Promise object
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler) return promiseConstructor.New(handler)
} }
......
...@@ -222,8 +222,8 @@ func (a *authCallbacks) Confirm( ...@@ -222,8 +222,8 @@ func (a *authCallbacks) Confirm(
a.confirm(utils.CopyBytesToJS(contact), utils.CopyBytesToJS(receptionId), a.confirm(utils.CopyBytesToJS(contact), utils.CopyBytesToJS(receptionId),
ephemeralId, roundId) ephemeralId, roundId)
} }
} }
func (a *authCallbacks) Reset( func (a *authCallbacks) Reset(
contact, receptionId []byte, ephemeralId, roundId int64) { contact, receptionId []byte, ephemeralId, roundId int64) {
if a.reset != nil { if a.reset != nil {
......
...@@ -38,7 +38,7 @@ import ( ...@@ -38,7 +38,7 @@ import (
// //
// Returns: // Returns:
// - A promise that returns the ID of the round (int). // - A promise that returns the ID of the round (int).
// - Throws error if the request fails. // - Throws an error if the request fails.
func (e *E2e) Request(_ js.Value, args []js.Value) interface{} { func (e *E2e) Request(_ js.Value, args []js.Value) interface{} {
partnerContact := utils.CopyBytesToGo(args[0]) partnerContact := utils.CopyBytesToGo(args[0])
factsListJson := utils.CopyBytesToGo(args[1]) factsListJson := utils.CopyBytesToGo(args[1])
...@@ -75,17 +75,21 @@ func (e *E2e) Request(_ js.Value, args []js.Value) interface{} { ...@@ -75,17 +75,21 @@ func (e *E2e) Request(_ js.Value, args []js.Value) interface{} {
// - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array). // - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array).
// //
// Returns: // Returns:
// - ID of the round (int). // - A promise that returns the ID of the round (int).
// - Throws TypeError if the confirmation fails. // - Throws an error if the confirmation fails.
func (e *E2e) Confirm(_ js.Value, args []js.Value) interface{} { func (e *E2e) Confirm(_ js.Value, args []js.Value) interface{} {
partnerContact := utils.CopyBytesToGo(args[0]) partnerContact := utils.CopyBytesToGo(args[0])
rid, err := e.api.Confirm(partnerContact)
if err != nil { promiseFn := func(resolve, reject func(args ...interface{}) js.Value) {
utils.Throw(utils.TypeError, err) rid, err := e.api.Confirm(partnerContact)
return nil if err != nil {
reject(utils.JsTrace(err))
} else {
resolve(rid)
}
} }
return rid return utils.CreatePromise(promiseFn)
} }
// Reset sends a contact reset request from the user identity in the imported // Reset sends a contact reset request from the user identity in the imported
...@@ -106,17 +110,21 @@ func (e *E2e) Confirm(_ js.Value, args []js.Value) interface{} { ...@@ -106,17 +110,21 @@ func (e *E2e) Confirm(_ js.Value, args []js.Value) interface{} {
// - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array). // - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array).
// //
// Returns: // Returns:
// - ID of the round (int). // - A promise that returns the ID of the round (int).
// - Throws TypeError if the reset fails. // - Throws an error if the reset fails.
func (e *E2e) Reset(_ js.Value, args []js.Value) interface{} { func (e *E2e) Reset(_ js.Value, args []js.Value) interface{} {
partnerContact := utils.CopyBytesToGo(args[0]) partnerContact := utils.CopyBytesToGo(args[0])
rid, err := e.api.Reset(partnerContact)
if err != nil { promiseFn := func(resolve, reject func(args ...interface{}) js.Value) {
utils.Throw(utils.TypeError, err) rid, err := e.api.Reset(partnerContact)
return nil if err != nil {
reject(utils.JsTrace(err))
} else {
resolve(rid)
}
} }
return rid return utils.CreatePromise(promiseFn)
} }
// ReplayConfirm resends a confirmation to the partner. It will fail to send if // ReplayConfirm resends a confirmation to the partner. It will fail to send if
...@@ -131,17 +139,21 @@ func (e *E2e) Reset(_ js.Value, args []js.Value) interface{} { ...@@ -131,17 +139,21 @@ func (e *E2e) Reset(_ js.Value, args []js.Value) interface{} {
// - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array). // - args[0] - marshalled bytes of the partner [contact.Contact] (Uint8Array).
// //
// Returns: // Returns:
// - ID of the round (int). // - A promise that returns the ID of the round (int).
// - Throws TypeError if the confirmation fails. // - Throws an error if the confirmation fails.
func (e *E2e) ReplayConfirm(_ js.Value, args []js.Value) interface{} { func (e *E2e) ReplayConfirm(_ js.Value, args []js.Value) interface{} {
partnerContact := utils.CopyBytesToGo(args[0]) partnerContact := utils.CopyBytesToGo(args[0])
rid, err := e.api.ReplayConfirm(partnerContact)
if err != nil { promiseFn := func(resolve, reject func(args ...interface{}) js.Value) {
utils.Throw(utils.TypeError, err) rid, err := e.api.ReplayConfirm(partnerContact)
return nil if err != nil {
reject(utils.JsTrace(err))
} else {
resolve(rid)
}
} }
return rid return utils.CreatePromise(promiseFn)
} }
// CallAllReceivedRequests will iterate through all pending contact requests and // CallAllReceivedRequests will iterate through all pending contact requests and
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment