-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.js
66 lines (55 loc) · 1.41 KB
/
cache.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
60
61
62
63
64
65
66
"use strict";
//////
// Modules
//////
const FS = require("fs");
const path = require("path");
//////
// Const and vars
//////
const secFilename = process.env.VAULT_CACHE_FILENAME || ".sec.json";
//////
// Exported functionality(ies)
//////
/**
* Tries to load the cache from disk. The cache is decoded with Base64.
*
* @param {String} appName
*
* @returns Cached configurations
*/
const load = (appName) => {
let configurations;
try {
const pathToConfiguration = path.resolve(secFilename);
configurations = require(pathToConfiguration);
configurations = Buffer.from(configurations[appName].sec, "base64").toString("ascii");
configurations = JSON.parse(configurations);
} catch (error) {
throw new Error(`Failed to load cached configurations! Error: ${error}`);
}
return configurations;
};
/**
* Tries to cache the configurations. The configurations will be encoded with Base64.
*
* @param {String} appName
* @param {Object} configurations
*/
const create = (appName, configurations) => {
const configurationsStringified = JSON.stringify(configurations);
const configurationsBase64 = Buffer.from(configurationsStringified).toString("base64");
try {
FS.writeFileSync(path.resolve(secFilename), JSON.stringify({
[appName]: {
"sec": configurationsBase64
}
}));
} catch (error) {
throw new Error(`Failed to create cache! Error: ${error.message}`);
}
};
module.exports = {
load,
create
};