-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
59 lines (51 loc) · 1.92 KB
/
storage.js
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
const storage = require("node-persist");
const { logger } = require("./logger");
const globals = require("./globals");
let storageInitialized = false;
async function initializeStorage() {
if (!storageInitialized) {
try {
await storage.init({ logging: false });
logger.info("Storage initialized successfully");
storageInitialized = true;
} catch (error) {
logger.error(`Error initializing storage: ${error.message.slice(0, globals.MAX_CHARACTERS_TO_DISPLAY_IN_ERROR_MESSAGE)}`);
throw error;
}
}
}
async function getCurrentPastelIdAndPassphrase() {
try {
await initializeStorage();
const pastelID = await storage.getItem("MY_LOCAL_PASTELID");
const passphrase = await storage.getItem("MY_PASTELID_PASSPHRASE");
if (!pastelID || !passphrase) {
logger.warn("PastelID or passphrase not found in storage");
return { pastelID: null, passphrase: null };
}
logger.info(`Retrieved PastelID from storage: ${pastelID}`);
return { pastelID, passphrase };
} catch (error) {
logger.error(`Error retrieving PastelID and passphrase: ${error.message.slice(0, globals.MAX_CHARACTERS_TO_DISPLAY_IN_ERROR_MESSAGE)}`);
return { pastelID: null, passphrase: null };
}
}
async function setPastelIdAndPassphrase(pastelID, passphrase) {
if (!pastelID || !passphrase) {
logger.error("Attempted to set empty PastelID or passphrase");
throw new Error("PastelID and passphrase must not be empty");
}
try {
await initializeStorage();
await storage.setItem("MY_LOCAL_PASTELID", pastelID);
await storage.setItem("MY_PASTELID_PASSPHRASE", passphrase);
logger.info(`Set PastelID: ${pastelID}`);
} catch (error) {
logger.error(`Error setting PastelID and passphrase: ${error.message.slice(0, globals.MAX_CHARACTERS_TO_DISPLAY_IN_ERROR_MESSAGE)}`);
throw error;
}
}
module.exports = {
getCurrentPastelIdAndPassphrase,
setPastelIdAndPassphrase,
};