Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
package utils
import (
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/bindings"
"os"
)
// SEMVER is the current semantic version of xxDK WASM.
const SEMVER = "0.0.0"
// Storage keys.
const (
semverKey = "xxdkWasmSemanticVersion"
clientVerKey = "xxdkClientSemanticVersion"
)
// CheckAndStoreVersions checks that the stored xxDK WASM version matches the
// current version and if not, upgrades it. It also stored the current xxDK
// client to storage.
//
// On first load, only the xxDK WASM and xxDK client versions are stored.
func CheckAndStoreVersions() error {
return checkAndStoreVersions(
SEMVER, bindings.GetVersion(), GetLocalStorage())
}
func checkAndStoreVersions(
currentWasmVer, currentClientVer string, ls *LocalStorage) error {
// Get the stored client and WASM versions, if they exists
storedClientVer, err := initOrLoadStoredSemver(
clientVerKey, currentClientVer, ls)
if err != nil {
return err
}
storedWasmVer, err := initOrLoadStoredSemver(semverKey, currentWasmVer, ls)
if err != nil {
return err
}
// Check if client needs an update
if storedClientVer != currentClientVer {
jww.INFO.Printf("xxDK client out of date; upgrading version: v%s → v%s",
storedClientVer, currentClientVer)
} else {
jww.INFO.Printf("xxDK client version is current: v%s", storedClientVer)
}
// Check if WASM needs an update
if storedWasmVer != currentWasmVer {
jww.INFO.Printf("xxDK WASM out of date; upgrading version: v%s → v%s",
storedWasmVer, currentWasmVer)
} else {
jww.INFO.Printf("xxDK WASM version is current: v%s", storedWasmVer)
}
// Upgrade path code goes here
// Save current versions
ls.SetItem(clientVerKey, []byte(currentClientVer))
ls.SetItem(semverKey, []byte(currentWasmVer))
return nil
}
// initOrLoadStoredSemver returns the semantic version stored at the key in
// local storage. If no version is stored, then the current version is stored
// and returned.
func initOrLoadStoredSemver(
key, currentVersion string, ls *LocalStorage) (string, error) {
storedVersion, err := ls.GetItem(key)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// Save the current version if this is the first run
jww.INFO.Printf("Initialising %s to v%s", key, currentVersion)
ls.SetItem(key, []byte(currentVersion))
return currentVersion, nil
} else {
// If the item exists, but cannot be loaded, return an error
return "", errors.Errorf(
"could not load %s from storage: %+v", key, err)
}
}
// Return the stored version
return string(storedVersion), nil
}