From 022fcdc45f26743c404cd804503d62a4d684ed7b Mon Sep 17 00:00:00 2001 From: Jono Wenger <jono@elixxir.io> Date: Mon, 9 Jan 2023 14:19:15 -0800 Subject: [PATCH] Get JS file path from bindings instead of constant --- .../impl/channels/channelsIndexedDbWorker.js | 2 +- indexedDb/impl/dm/dmIndexedDbWorker.js | 2 +- indexedDb/worker/channels/init.go | 13 ++-- indexedDb/worker/dm/init.go | 9 +-- wasm/channels.go | 67 +++++++++++-------- wasm/dm.go | 36 +++++----- worker/manager.go | 3 +- 7 files changed, 67 insertions(+), 65 deletions(-) diff --git a/indexedDb/impl/channels/channelsIndexedDbWorker.js b/indexedDb/impl/channels/channelsIndexedDbWorker.js index 198ab729..9e69bdd7 100644 --- a/indexedDb/impl/channels/channelsIndexedDbWorker.js +++ b/indexedDb/impl/channels/channelsIndexedDbWorker.js @@ -11,7 +11,7 @@ const go = new Go(); const binPath = 'xxdk-channelsIndexedDkWorker.wasm' WebAssembly.instantiateStreaming(fetch(binPath), go.importObject).then((result) => { go.run(result.instance); - LogLevel(2); + LogLevel(1); }).catch((err) => { console.error(err); }); \ No newline at end of file diff --git a/indexedDb/impl/dm/dmIndexedDbWorker.js b/indexedDb/impl/dm/dmIndexedDbWorker.js index 67378654..e199a7bb 100644 --- a/indexedDb/impl/dm/dmIndexedDbWorker.js +++ b/indexedDb/impl/dm/dmIndexedDbWorker.js @@ -11,7 +11,7 @@ const go = new Go(); const binPath = 'xxdk-dmIndexedDkWorker.wasm' WebAssembly.instantiateStreaming(fetch(binPath), go.importObject).then((result) => { go.run(result.instance); - LogLevel(2); + LogLevel(1); }).catch((err) => { console.error(err); }); \ No newline at end of file diff --git a/indexedDb/worker/channels/init.go b/indexedDb/worker/channels/init.go index 1f8cc850..0159319f 100644 --- a/indexedDb/worker/channels/init.go +++ b/indexedDb/worker/channels/init.go @@ -23,10 +23,6 @@ import ( "gitlab.com/xx_network/primitives/id" ) -// WorkerJavascriptFileURL is the URL of the script the worker will execute to -// launch the worker WASM binary. It must obey the same-origin policy. -const WorkerJavascriptFileURL = "/integrations/assets/channelsIndexedDbWorker.js" - // MessageReceivedCallback is called any time a message is received or updated. // // update is true if the row is old and was edited. @@ -35,10 +31,10 @@ type MessageReceivedCallback func(uuid uint64, channelID *id.ID, update bool) // NewWASMEventModelBuilder returns an EventModelBuilder which allows // the channel manager to define the path but the callback is the same // across the board. -func NewWASMEventModelBuilder(encryption cryptoChannel.Cipher, +func NewWASMEventModelBuilder(wasmJsPath string, encryption cryptoChannel.Cipher, cb MessageReceivedCallback) channels.EventModelBuilder { fn := func(path string) (channels.EventModel, error) { - return NewWASMEventModel(path, encryption, cb) + return NewWASMEventModel(path, wasmJsPath, encryption, cb) } return fn } @@ -52,11 +48,10 @@ type NewWASMEventModelMessage struct { // NewWASMEventModel returns a [channels.EventModel] backed by a wasmModel. // The name should be a base64 encoding of the users public key. -func NewWASMEventModel(path string, encryption cryptoChannel.Cipher, +func NewWASMEventModel(path, wasmJsPath string, encryption cryptoChannel.Cipher, cb MessageReceivedCallback) (channels.EventModel, error) { - // TODO: bring in URL and name from caller - wm, err := worker.NewManager(WorkerJavascriptFileURL, "channelsIndexedDb") + wm, err := worker.NewManager(wasmJsPath, "channelsIndexedDb") if err != nil { return nil, err } diff --git a/indexedDb/worker/dm/init.go b/indexedDb/worker/dm/init.go index 0ec1d8fc..b37cbd09 100644 --- a/indexedDb/worker/dm/init.go +++ b/indexedDb/worker/dm/init.go @@ -23,10 +23,6 @@ import ( "gitlab.com/elixxir/xxdk-wasm/worker" ) -// WorkerJavascriptFileURL is the URL of the script the worker will execute to -// launch the worker WASM binary. It must obey the same-origin policy. -const WorkerJavascriptFileURL = "/integrations/assets/dmIndexedDbWorker.js" - // MessageReceivedCallback is called any time a message is received or updated. // // update is true if the row is old and was edited. @@ -42,11 +38,10 @@ type NewWASMEventModelMessage struct { // NewWASMEventModel returns a [channels.EventModel] backed by a wasmModel. // The name should be a base64 encoding of the users public key. -func NewWASMEventModel(path string, encryption cryptoChannel.Cipher, +func NewWASMEventModel(path, wasmJsPath string, encryption cryptoChannel.Cipher, cb MessageReceivedCallback) (dm.EventModel, error) { - // TODO: bring in URL and name from caller - wh, err := worker.NewManager(WorkerJavascriptFileURL, "dmIndexedDb") + wh, err := worker.NewManager(wasmJsPath, "dmIndexedDb") if err != nil { return nil, err } diff --git a/wasm/channels.go b/wasm/channels.go index de8d1fc8..df07f009 100644 --- a/wasm/channels.go +++ b/wasm/channels.go @@ -317,16 +317,17 @@ func LoadChannelsManager(_ js.Value, args []js.Value) any { // Parameters: // - args[0] - ID of [Cmix] object in tracker (int). This can be retrieved // using [Cmix.GetID]. -// - args[1] - Bytes of a private identity ([channel.PrivateIdentity]) that is +// - args[1] - Path to Javascript file that starts the worker (string). +// - args[2] - Bytes of a private identity ([channel.PrivateIdentity]) that is // generated by [GenerateChannelIdentity] (Uint8Array). -// - args[2] - Function that takes in the same parameters as +// - args[3] - Function that takes in the same parameters as // [indexedDb.MessageReceivedCallback]. On the Javascript side, the UUID is // returned as an int and the channelID as a Uint8Array. The row in the // database that was updated can be found using the UUID. The channel ID is // provided so that the recipient can filter if they want to the processes // the update now or not. An "update" bool is present which tells you if the // row is new or if it is an edited old row. -// - args[3] - ID of [ChannelDbCipher] object in tracker (int). Create this +// - args[4] - ID of [ChannelDbCipher] object in tracker (int). Create this // object with [NewChannelsDatabaseCipher] and get its id with // [ChannelDbCipher.GetID]. // @@ -336,9 +337,10 @@ func LoadChannelsManager(_ js.Value, args []js.Value) any { // - Throws a TypeError if the cipher ID does not correspond to a cipher. func NewChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) any { cmixID := args[0].Int() - privateIdentity := utils.CopyBytesToGo(args[1]) - messageReceivedCB := args[2] - cipherID := args[3].Int() + wasmJsPath := args[1].String() + privateIdentity := utils.CopyBytesToGo(args[2]) + messageReceivedCB := args[3] + cipherID := args[4].Int() cipher, err := bindings.GetChannelDbCipherTrackerFromID(cipherID) if err != nil { @@ -346,7 +348,7 @@ func NewChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) any { } return newChannelsManagerWithIndexedDb( - cmixID, privateIdentity, messageReceivedCB, cipher) + cmixID, wasmJsPath, privateIdentity, messageReceivedCB, cipher) } // NewChannelsManagerWithIndexedDbUnsafe creates a new [ChannelsManager] from a @@ -364,9 +366,10 @@ func NewChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) any { // Parameters: // - args[0] - ID of [Cmix] object in tracker (int). This can be retrieved // using [Cmix.GetID]. -// - args[1] - Bytes of a private identity ([channel.PrivateIdentity]) that is +// - args[1] - Path to Javascript file that starts the worker (string). +// - args[2] - Bytes of a private identity ([channel.PrivateIdentity]) that is // generated by [GenerateChannelIdentity] (Uint8Array). -// - args[2] - Function that takes in the same parameters as +// - args[3] - Function that takes in the same parameters as // [indexedDb.MessageReceivedCallback]. On the Javascript side, the UUID is // returned as an int and the channelID as a Uint8Array. The row in the // database that was updated can be found using the UUID. The channel ID is @@ -379,21 +382,22 @@ func NewChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) any { // - Rejected with an error if loading indexedDb or the manager fails. func NewChannelsManagerWithIndexedDbUnsafe(_ js.Value, args []js.Value) any { cmixID := args[0].Int() - privateIdentity := utils.CopyBytesToGo(args[1]) - messageReceivedCB := args[2] + wasmJsPath := args[1].String() + privateIdentity := utils.CopyBytesToGo(args[2]) + messageReceivedCB := args[3] return newChannelsManagerWithIndexedDb( - cmixID, privateIdentity, messageReceivedCB, nil) + cmixID, wasmJsPath, privateIdentity, messageReceivedCB, nil) } -func newChannelsManagerWithIndexedDb(cmixID int, privateIdentity []byte, - cb js.Value, cipher *bindings.ChannelDbCipher) any { +func newChannelsManagerWithIndexedDb(cmixID int, wasmJsPath string, + privateIdentity []byte, cb js.Value, cipher *bindings.ChannelDbCipher) any { messageReceivedCB := func(uuid uint64, channelID *id.ID, update bool) { cb.Invoke(uuid, utils.CopyBytesToJS(channelID.Marshal()), update) } - model := channelsDb.NewWASMEventModelBuilder(cipher, messageReceivedCB) + model := channelsDb.NewWASMEventModelBuilder(wasmJsPath, cipher, messageReceivedCB) promiseFn := func(resolve, reject func(args ...any) js.Value) { cm, err := bindings.NewChannelsManagerGoEventModel( @@ -419,16 +423,17 @@ func newChannelsManagerWithIndexedDb(cmixID int, privateIdentity []byte, // Parameters: // - args[0] - ID of [Cmix] object in tracker (int). This can be retrieved // using [Cmix.GetID]. -// - args[1] - The storage tag associated with the previously created channel +// - args[1] - Path to Javascript file that starts the worker (string). +// - args[2] - The storage tag associated with the previously created channel // manager and retrieved with [ChannelsManager.GetStorageTag] (string). -// - args[2] - Function that takes in the same parameters as +// - args[3] - Function that takes in the same parameters as // [indexedDb.MessageReceivedCallback]. On the Javascript side, the UUID is // returned as an int and the channelID as a Uint8Array. The row in the // database that was updated can be found using the UUID. The channel ID is // provided so that the recipient can filter if they want to the processes // the update now or not. An "update" bool is present which tells you if the // row is new or if it is an edited old row. -// - args[3] - ID of [ChannelDbCipher] object in tracker (int). Create this +// - args[4] - ID of [ChannelDbCipher] object in tracker (int). Create this // object with [NewChannelsDatabaseCipher] and get its id with // [ChannelDbCipher.GetID]. // @@ -438,9 +443,10 @@ func newChannelsManagerWithIndexedDb(cmixID int, privateIdentity []byte, // - Throws a TypeError if the cipher ID does not correspond to a cipher. func LoadChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) any { cmixID := args[0].Int() - storageTag := args[1].String() - messageReceivedCB := args[2] - cipherID := args[3].Int() + wasmJsPath := args[1].String() + storageTag := args[2].String() + messageReceivedCB := args[3] + cipherID := args[4].Int() cipher, err := bindings.GetChannelDbCipherTrackerFromID(cipherID) if err != nil { @@ -448,7 +454,7 @@ func LoadChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) any { } return loadChannelsManagerWithIndexedDb( - cmixID, storageTag, messageReceivedCB, cipher) + cmixID, wasmJsPath, storageTag, messageReceivedCB, cipher) } // LoadChannelsManagerWithIndexedDbUnsafe loads an existing [ChannelsManager] @@ -464,9 +470,10 @@ func LoadChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) any { // Parameters: // - args[0] - ID of [Cmix] object in tracker (int). This can be retrieved // using [Cmix.GetID]. -// - args[1] - The storage tag associated with the previously created channel +// - args[1] - Path to Javascript file that starts the worker (string). +// - args[2] - The storage tag associated with the previously created channel // manager and retrieved with [ChannelsManager.GetStorageTag] (string). -// - args[2] - Function that takes in the same parameters as +// - args[3] - Function that takes in the same parameters as // [indexedDb.MessageReceivedCallback]. On the Javascript side, the UUID is // returned as an int and the channelID as a Uint8Array. The row in the // database that was updated can be found using the UUID. The channel ID is @@ -479,20 +486,22 @@ func LoadChannelsManagerWithIndexedDb(_ js.Value, args []js.Value) any { // - Rejected with an error if loading indexedDb or the manager fails. func LoadChannelsManagerWithIndexedDbUnsafe(_ js.Value, args []js.Value) any { cmixID := args[0].Int() - storageTag := args[1].String() - messageReceivedCB := args[2] + wasmJsPath := args[1].String() + storageTag := args[2].String() + messageReceivedCB := args[3] return loadChannelsManagerWithIndexedDb( - cmixID, storageTag, messageReceivedCB, nil) + cmixID, wasmJsPath, storageTag, messageReceivedCB, nil) } -func loadChannelsManagerWithIndexedDb(cmixID int, storageTag string, +func loadChannelsManagerWithIndexedDb(cmixID int, wasmJsPath, storageTag string, cb js.Value, cipher *bindings.ChannelDbCipher) any { messageReceivedCB := func(uuid uint64, channelID *id.ID, updated bool) { cb.Invoke(uuid, utils.CopyBytesToJS(channelID.Marshal()), updated) } - model := channelsDb.NewWASMEventModelBuilder(cipher, messageReceivedCB) + model := channelsDb.NewWASMEventModelBuilder( + wasmJsPath, cipher, messageReceivedCB) promiseFn := func(resolve, reject func(args ...any) js.Value) { cm, err := bindings.LoadChannelsManagerGoEventModel( diff --git a/wasm/dm.go b/wasm/dm.go index 54c0e0fb..04696435 100644 --- a/wasm/dm.go +++ b/wasm/dm.go @@ -142,16 +142,17 @@ func NewDMClient(_ js.Value, args []js.Value) any { // Parameters: // - args[0] - ID of [Cmix] object in tracker (int). This can be retrieved // using [Cmix.GetID]. -// - args[1] - Bytes of a private identity ([channel.PrivateIdentity]) that is +// - args[1] - Path to Javascript file that starts the worker (string). +// - args[2] - Bytes of a private identity ([channel.PrivateIdentity]) that is // generated by [GenerateChannelIdentity] (Uint8Array). -// - args[2] - Function that takes in the same parameters as +// - args[3] - Function that takes in the same parameters as // [indexedDbWorker.MessageReceivedCallback]. On the Javascript side, the UUID is // returned as an int and the channelID as a Uint8Array. The row in the // database that was updated can be found using the UUID. The channel ID is // provided so that the recipient can filter if they want to the processes // the update now or not. An "update" bool is present which tells you if the // row is new or if it is an edited old row. -// - args[3] - ID of [ChannelDbCipher] object in tracker (int). Create this +// - args[4] - ID of [ChannelDbCipher] object in tracker (int). Create this // object with [NewChannelsDatabaseCipher] and get its id with // [ChannelDbCipher.GetID]. // @@ -161,9 +162,10 @@ func NewDMClient(_ js.Value, args []js.Value) any { // - Throws a TypeError if the cipher ID does not correspond to a cipher. func NewDMClientWithIndexedDb(_ js.Value, args []js.Value) any { cmixID := args[0].Int() - privateIdentity := utils.CopyBytesToGo(args[1]) - messageReceivedCB := args[2] - cipherID := args[3].Int() + wasmJsPath := args[1].String() + privateIdentity := utils.CopyBytesToGo(args[2]) + messageReceivedCB := args[3] + cipherID := args[4].Int() cipher, err := bindings.GetChannelDbCipherTrackerFromID(cipherID) if err != nil { @@ -171,7 +173,7 @@ func NewDMClientWithIndexedDb(_ js.Value, args []js.Value) any { } return newDMClientWithIndexedDb( - cmixID, privateIdentity, messageReceivedCB, cipher) + cmixID, wasmJsPath, privateIdentity, messageReceivedCB, cipher) } // NewDMClientWithIndexedDbUnsafe creates a new [DMClient] from a @@ -189,9 +191,10 @@ func NewDMClientWithIndexedDb(_ js.Value, args []js.Value) any { // Parameters: // - args[0] - ID of [Cmix] object in tracker (int). This can be retrieved // using [Cmix.GetID]. -// - args[1] - Bytes of a private identity ([channel.PrivateIdentity]) that is +// - args[1] - Path to Javascript file that starts the worker (string). +// - args[2] - Bytes of a private identity ([channel.PrivateIdentity]) that is // generated by [GenerateChannelIdentity] (Uint8Array). -// - args[2] - Function that takes in the same parameters as +// - args[3] - Function that takes in the same parameters as // [indexedDbWorker.MessageReceivedCallback]. On the Javascript side, the UUID is // returned as an int and the channelID as a Uint8Array. The row in the // database that was updated can be found using the UUID. The channel ID is @@ -204,15 +207,16 @@ func NewDMClientWithIndexedDb(_ js.Value, args []js.Value) any { // - Rejected with an error if loading indexedDbWorker or the manager fails. func NewDMClientWithIndexedDbUnsafe(_ js.Value, args []js.Value) any { cmixID := args[0].Int() - privateIdentity := utils.CopyBytesToGo(args[1]) - messageReceivedCB := args[2] + wasmJsPath := args[1].String() + privateIdentity := utils.CopyBytesToGo(args[2]) + messageReceivedCB := args[3] return newDMClientWithIndexedDb( - cmixID, privateIdentity, messageReceivedCB, nil) + cmixID, wasmJsPath, privateIdentity, messageReceivedCB, nil) } -func newDMClientWithIndexedDb(cmixID int, privateIdentity []byte, - cb js.Value, cipher *bindings.ChannelDbCipher) any { +func newDMClientWithIndexedDb(cmixID int, wasmJsPath string, + privateIdentity []byte, cb js.Value, cipher *bindings.ChannelDbCipher) any { messageReceivedCB := func(uuid uint64, pubKey ed25519.PublicKey, update bool) { @@ -226,8 +230,8 @@ func newDMClientWithIndexedDb(cmixID int, privateIdentity []byte, reject(utils.JsTrace(err)) } dmPath := base64.RawStdEncoding.EncodeToString(pi.PubKey[:]) - model, err := indexDB.NewWASMEventModel(dmPath, cipher, - messageReceivedCB) + model, err := indexDB.NewWASMEventModel( + dmPath, wasmJsPath, cipher, messageReceivedCB) if err != nil { reject(utils.JsTrace(err)) } diff --git a/worker/manager.go b/worker/manager.go index d6cceaf1..9a0ac78a 100644 --- a/worker/manager.go +++ b/worker/manager.go @@ -20,8 +20,7 @@ import ( ) // TODO: -// 1. Get path to JS file from bindings -// 2. Add tests for manager.go and thread.go +// 1. Add tests for manager.go and thread.go // initID is the ID for the first item in the callback list. If the list only // contains one callback, then this is the ID of that callback. If the list has -- GitLab