diff --git a/storage/purge.go b/storage/purge.go index e87d8da1cdf9b66583b2c31e56bafbb3c725a49e..b0d8fc889641530a56a9fd8c554100f2a3df4a7a 100644 --- a/storage/purge.go +++ b/storage/purge.go @@ -12,51 +12,66 @@ package storage import ( "github.com/hack-pad/go-indexeddb/idb" "github.com/pkg/errors" + "gitlab.com/elixxir/client/storage/utility" "gitlab.com/elixxir/xxdk-wasm/utils" "sync/atomic" "syscall/js" ) -// NumClientsRunning is an atomic that tracks the current number of Cmix +// numClientsRunning is an atomic that tracks the current number of Cmix // followers that have been started. Every time one is started, this counter // must be incremented and every time one is stopped, it must be decremented. // // This variable is an atomic. Only access it with atomic functions -var NumClientsRunning uint64 +var numClientsRunning uint64 + +// IncrementNumClientsRunning increments the number client tracker. This should +// be called when starting the network follower. +func IncrementNumClientsRunning() { + atomic.AddUint64(&numClientsRunning, 1) +} + +// DecrementNumClientsRunning decrements the number client tracker. This should +// be called when stopping the network follower. +func DecrementNumClientsRunning() { + atomic.AddUint64(&numClientsRunning, ^uint64(0)) +} // Purge clears all local storage and indexedDb databases saved by this WASM -// binary. All Cmix followers must be closed and the user's password is -// required. -// -// Warning: This deletes all storage local to the webpage running this WASM. -// Only use if you want to destroy everything. +// binary. This can only occur when no cMix followers are running. The user's +// password is required. // // Parameters: -// - args[0] - The user-supplied password (string). +// - args[0] - Storage directory path (string). This is the same directory path +// passed into [wasm.NewCmix]. +// - args[1] - The user-supplied password (string). This is the same password +// passed into [wasm.NewCmix]. // // Returns: -// - Throws a TypeError if the password is incorrect or if not all Cmix +// - Throws a TypeError if the password is incorrect or if not all cMix // followers have been stopped. func Purge(_ js.Value, args []js.Value) interface{} { + storageDirectory := args[0].String() + userPassword := args[1].String() + // Check the password - if !verifyPassword(args[0].String()) { + if !verifyPassword(userPassword) { utils.Throw(utils.TypeError, errors.New("invalid password")) return nil } // Verify all Cmix followers are stopped - if n := atomic.LoadUint64(&NumClientsRunning); n != 0 { - utils.Throw( - utils.TypeError, errors.Errorf("%d Cmix followers running", n)) + if n := atomic.LoadUint64(&numClientsRunning); n != 0 { + utils.Throw(utils.TypeError, errors.Errorf( + "%d cMix followers running; all need to be stopped", n)) return nil } // Get all indexedDb database names databaseList, err := GetIndexedDbList() if err != nil { - utils.Throw( - utils.TypeError, errors.Errorf( - "failed to get list of indexedDb database names: %+v", err)) + utils.Throw(utils.TypeError, errors.Errorf( + "failed to get list of indexedDb database names: %+v", err)) return nil } @@ -64,16 +79,23 @@ func Purge(_ js.Value, args []js.Value) interface{} { for dbName := range databaseList { _, err = idb.Global().DeleteDatabase(dbName) if err != nil { - utils.Throw( - utils.TypeError, errors.Errorf( - "failed to delete indexedDb database %q: %+v", dbName, err)) + utils.Throw(utils.TypeError, errors.Errorf( + "failed to delete indexedDb database %q: %+v", dbName, err)) return nil } } - // Clear WASM local storage + // Get local storage ls := GetLocalStorage() + + // Clear all local storage saved by this WASM project ls.ClearWASM() + // Clear all EKV from local storage + ls.ClearPrefix(storageDirectory) + + // Clear all NDFs saved to local storage + ls.ClearPrefix(utility.NdfStorageKeyNamePrefix) + return nil } diff --git a/wasm/follow.go b/wasm/follow.go index ed98bfbf4e54b92105f92cde0618e31cb674be77..1ce3a46522741666e052d0be53acc2148ee6249e 100644 --- a/wasm/follow.go +++ b/wasm/follow.go @@ -12,7 +12,6 @@ package wasm import ( "gitlab.com/elixxir/xxdk-wasm/storage" "gitlab.com/elixxir/xxdk-wasm/utils" - "sync/atomic" "syscall/js" ) @@ -62,8 +61,7 @@ func (c *Cmix) StartNetworkFollower(_ js.Value, args []js.Value) interface{} { return nil } - atomic.AddUint64(&storage.NumClientsRunning, 1) - + storage.IncrementNumClientsRunning() return nil } @@ -81,8 +79,8 @@ func (c *Cmix) StopNetworkFollower(js.Value, []js.Value) interface{} { utils.Throw(utils.TypeError, err) return nil } - atomic.AddUint64(&storage.NumClientsRunning, ^uint64(0)) + storage.DecrementNumClientsRunning() return nil }