-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
187 lines (168 loc) · 6.11 KB
/
storage.js
File metadata and controls
187 lines (168 loc) · 6.11 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { createDefaultState, SCHEMA_VERSION } from "../data/defaults.js";
import { exportIndexedDB, importIndexedDB } from "./db.js";
import { encryptData, decryptData } from "./crypto.js";
const STORAGE_KEY = "greenlight";
export function loadState() {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return createDefaultState();
const parsed = JSON.parse(raw);
return migrateState(parsed);
} catch (err) {
console.error("[GreenLight] Failed to load state:", err);
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw) localStorage.setItem("greenlight_backup", raw);
} catch { /* best-effort backup */ }
return createDefaultState();
}
}
export function saveState(state) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
}
export function resetState() {
localStorage.removeItem(STORAGE_KEY);
return createDefaultState();
}
export async function exportState(state, password) {
if (!state) state = loadState();
const backup = { ...state };
try {
const idbData = await exportIndexedDB();
if (idbData) backup._indexedDB = idbData;
} catch (err) {
console.warn("[GreenLight] Could not include IndexedDB data in backup:", err.message);
}
const date = new Date().toISOString().slice(0, 10);
let blob, filename;
if (password) {
const envelope = await encryptData(backup, password);
blob = new Blob([JSON.stringify(envelope)], { type: "application/json" });
filename = `greenlight-backup-${date}.greenlight`;
} else {
blob = new Blob([JSON.stringify(backup, null, 2)], { type: "application/json" });
filename = `greenlight-backup-${date}.json`;
}
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
export async function importState(jsonString, password) {
let parsed;
try {
parsed = JSON.parse(jsonString);
} catch {
throw new Error("This file is not valid JSON. Make sure you selected a GreenLight backup file (.json or .greenlight).");
}
let backup;
if (parsed.format === "greenlight-encrypted-v1") {
if (!password) throw new Error("This file is encrypted. A password is required to import it.");
backup = await decryptData(parsed, password);
} else {
backup = parsed;
}
validateImport(backup);
const idbData = backup._indexedDB;
delete backup._indexedDB;
const migrated = migrateState(backup);
// Restore IndexedDB first — if it fails, localStorage is untouched
if (idbData) {
try {
await importIndexedDB(idbData);
} catch (err) {
console.warn("[GreenLight] IndexedDB import failed, continuing with localStorage only:", err.message);
}
}
saveState(migrated);
return migrated;
}
/**
* Validate imported JSON has the expected shape before migrating.
* Throws descriptive errors for common problems.
*/
export function validateImport(obj) {
if (obj == null || typeof obj !== "object" || Array.isArray(obj)) {
throw new Error("Import must be a JSON object, not " + (Array.isArray(obj) ? "an array" : typeof obj));
}
// Must have a schema version (any GreenLight export has one)
if (obj.schemaVersion == null) {
throw new Error("This doesn't look like a GreenLight backup (no schemaVersion found)");
}
// Schema version sanity check
if (typeof obj.schemaVersion !== "number" || obj.schemaVersion < 1) {
throw new Error(`Invalid schemaVersion: ${obj.schemaVersion}`);
}
if (obj.schemaVersion > SCHEMA_VERSION + 5) {
throw new Error(`This backup is from a newer version (v${obj.schemaVersion}). Update GreenLight first.`);
}
// Validate array fields are actually arrays
const arrayFields = ["assets", "cashAccounts", "lenders", "capitalSales"];
for (const field of arrayFields) {
if (obj[field] != null && !Array.isArray(obj[field])) {
throw new Error(`"${field}" must be an array, got ${typeof obj[field]}`);
}
}
// Validate assets have required fields
if (Array.isArray(obj.assets)) {
for (let i = 0; i < obj.assets.length; i++) {
const a = obj.assets[i];
if (typeof a !== "object" || a == null) {
throw new Error(`assets[${i}] is not an object`);
}
if (!a.name && !a.symbol) {
throw new Error(`assets[${i}] missing both name and symbol`);
}
if (a.quantity != null && typeof a.quantity !== "number") {
throw new Error(`assets[${i}] quantity must be a number`);
}
}
}
// Validate retirement accounts if present
const ret = obj.retirement;
if (ret != null && typeof ret === "object") {
if (ret.accounts != null && !Array.isArray(ret.accounts)) {
throw new Error(`retirement.accounts must be an array`);
}
}
}
export function migrateState(state) {
// Unrecognizable or empty state — reset to avoid partial state bugs
if (state.schemaVersion == null && !Array.isArray(state.assets) && !Array.isArray(state.cashAccounts)) {
return createDefaultState();
}
// Already current — return as-is (preserves object reference)
if (state.schemaVersion === SCHEMA_VERSION) return state;
// Future version — return as-is with warning
if (state.schemaVersion > SCHEMA_VERSION) {
console.warn(`[GreenLight] State has future schema v${state.schemaVersion} (current: ${SCHEMA_VERSION}). Returning as-is.`);
return state;
}
let data = { ...state };
// v1 → v2: add carMaintenanceAnnual to purchase
if ((data.schemaVersion ?? 0) < 2) {
if (data.purchase && data.purchase.carMaintenanceAnnual === undefined) {
data.purchase = { ...data.purchase, carMaintenanceAnnual: null };
}
data.schemaVersion = 2;
}
// v2 → v3: normalize platform fee fields (all three always present)
if (data.schemaVersion < 3) {
if (data.platforms) {
const migrated = {};
for (const [key, plat] of Object.entries(data.platforms)) {
migrated[key] = {
name: plat.name,
feePerShare: plat.feePerShare ?? 0,
flatFee: plat.flatFee ?? 0,
feePercent: plat.feePercent ?? 0,
};
}
data.platforms = migrated;
}
data.schemaVersion = 3;
}
return data;
}