mybase 1.0.45 → 1.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/mybase.js +43 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -42,6 +42,7 @@ var { isLocal } = require('mybase')
42
42
  ## Encryption
43
43
  ### decryptAES_CBC_NOIV(encryptedHex, encryptionKey)
44
44
  ### encryptAES_CBC_NOIV(plainString, encryptionKey)
45
+ ### hash_sha512(plain)
45
46
  ```
46
47
  var handle = await maxmindOpen(config.geoip.country)
47
48
  ```
package/mybase.js CHANGED
@@ -11,6 +11,10 @@ const { match } = require('assert');
11
11
  const validator = require('validator')
12
12
  const sha512 = require('js-sha512')
13
13
 
14
+ // create cache folder if not exists
15
+ let vault_cache_folder = '/var/tmp/vault-cache'
16
+ if (!fs.existsSync(vault_cache_folder)) fs.mkdirSync(vault_cache_folder)
17
+
14
18
 
15
19
  Date.prototype.yyyymmdd = function() {
16
20
  var mm = this.getMonth() + 1; // getMonth() is zero-based
@@ -22,6 +26,11 @@ Date.prototype.yyyymmdd = function() {
22
26
  ].join('');
23
27
  };
24
28
 
29
+ function hash_sha512(plain) {
30
+ if (typeof plain==='string') return sha512(plain)
31
+ return false
32
+ }
33
+
25
34
  function validHPassword(hpassword) { return (hpassword && hpassword.length === 128 && hpassword.search(/^[a-f0-9]+$/) == 0) }
26
35
  function randomHPassword(length=10) {
27
36
  let plain = randomString(length)
@@ -152,22 +161,53 @@ async function getMysql(mysqlClass,config,keep_pinging=true) {
152
161
  })
153
162
  }
154
163
 
155
- function vaultRead(vault, key) {
164
+ function fileCacheIsValid(cacheFile, cache_in_minutes = 60) {
165
+ if (fs.existsSync(cacheFile)) {
166
+ const stat = fs.statSync(cacheFile)
167
+ const mtime = new Date(stat.mtime).getTime()
168
+ if (Date.now() - mtime < cache_in_minutes * 60 * 1000) {
169
+ // cache is still valid
170
+ return true
171
+ }
172
+ }
173
+ return false
174
+ }
175
+
176
+ function vaultRead(vault, key, cache_in_minutes = 10) {
177
+ // v2 - supports caching
156
178
  return new Promise((resolve, reject) => {
179
+ let cache_file = path.join(vault_cache_folder,hash_sha512(`${vault.endpoint}/${key}`))
180
+ if (cache_in_minutes>0 && fileCacheIsValid(cache_file,cache_in_minutes))
181
+ return resolve(JSON.parse(fs.readFileSync(cache_file)))
182
+
157
183
  vault.read(key).then((r) => {
158
184
  if (r.data) {
159
185
  debug(`vault ${key} - success`)
186
+ if (cache_in_minutes>0)
187
+ try { fs.writeFileSync(cache_file, JSON.stringify(r.data)) } catch(_) {}
160
188
  return resolve(r.data);
161
189
  }
162
190
  debug(`vault ${key} - failed`)
163
191
  reject(r);
164
192
  }).catch(e => {
193
+ console.log(e)
165
194
  debug(`exception inside vaultRead`, e)
195
+ // we will still return latest information from cache
196
+ try {
197
+ console.log(chalk.bgRed(`returning vault@${key} from cache due to error`))
198
+ if (fs.existsSync(cache_file))
199
+ return resolve(JSON.parse(fs.readFileSync(cache_file)))
200
+ }catch(e2) {
201
+ // content of the file is invalid
202
+ console.log(e2)
203
+ }
204
+
166
205
  reject(e);
167
206
  })
168
207
  })
169
208
  }
170
209
 
210
+
171
211
  function vaultFill(vault, obj, ignoreError = false, keepCache=true) {
172
212
  // v2.1
173
213
  // fills all strings with ^vault@/secret/... with proper vault values
@@ -616,5 +656,6 @@ module.exports = {
616
656
  utcnow,
617
657
  Geoip2Paths,
618
658
  randomHPassword,
619
- isURL
659
+ isURL,
660
+ hash_sha512
620
661
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.0.45",
3
+ "version": "1.0.47",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {