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

Fix purge

parent bd26d29c
No related branches found
No related tags found
1 merge request!60Revert "Fail a test to be sure it works"
......@@ -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
}
......@@ -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
}
......
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