mybase 1.1.1 → 1.1.2

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.
package/mybase.js CHANGED
@@ -116,7 +116,7 @@ function utcnow() { // ported
116
116
  return Math.floor(Date.now() / 1000)
117
117
  }
118
118
 
119
- function validIp(str) {
119
+ function validIp(str) { // ported
120
120
  if (str && typeof str==='string') {
121
121
  let splitted
122
122
  if (splitted=str.match(/^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
@@ -263,7 +263,7 @@ function vaultRead(vault, key, cache_in_minutes = 10) {
263
263
  }
264
264
 
265
265
 
266
- function vaultFill(vault, obj, ignoreError = false, keepCache=true) {
266
+ function vaultFill(vault, obj, ignoreError = false, keepCache=true) { // ported
267
267
  // v2.1
268
268
  // fills all strings with ^vault@/secret/... with proper vault values
269
269
  // if you call it again, it does reload the configuration
@@ -557,7 +557,7 @@ function sqlQuery(sqlHandle,query,values=[]) {
557
557
  })
558
558
  }
559
559
 
560
- function wait(seconds=1){
560
+ function wait(seconds=1){ // ported
561
561
  return new Promise((resolve,reject)=>{
562
562
  setTimeout(resolve,seconds*1000)
563
563
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -12,17 +12,19 @@
12
12
  "@7c/validurl": "0.0.3",
13
13
  "aes-js": "^3.1.2",
14
14
  "chalk": "^3.0.0",
15
- "debug": "^4.3.1",
15
+ "debug": "^4.3.4",
16
16
  "ip-range-check": "^0.2.0",
17
17
  "ip6": "=0.2.7",
18
18
  "ip6addr": "^0.2.5",
19
19
  "js-sha512": "^0.8.0",
20
+ "node-vault": "^0.10.2",
20
21
  "psl": "^1.9.0",
21
22
  "punycode": "^2.1.1",
22
23
  "validator": "^13.7.0"
23
24
  },
24
25
  "devDependencies": {
25
26
  "@jest/globals": "^29.7.0",
27
+ "@types/debug": "^4.1.12",
26
28
  "@types/jest": "^29.5.11",
27
29
  "@types/node": "^20.11.5",
28
30
  "chai": "^4.2.0",
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.fileCacheIsValid = void 0;
27
+ const fs = __importStar(require("fs"));
28
+ function fileCacheIsValid(cacheFileName, cache_in_minutes = 60) {
29
+ if (fs.existsSync(cacheFileName)) {
30
+ const stat = fs.statSync(cacheFileName);
31
+ const mtime = new Date(stat.mtime).getTime();
32
+ if (Date.now() - mtime < cache_in_minutes * 60 * 1000) {
33
+ // cache is still valid
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ }
39
+ exports.fileCacheIsValid = fileCacheIsValid;
@@ -0,0 +1,13 @@
1
+ import * as fs from 'fs'
2
+
3
+ export function fileCacheIsValid(cacheFileName : string, cache_in_minutes = 60) : boolean {
4
+ if (fs.existsSync(cacheFileName)) {
5
+ const stat = fs.statSync(cacheFileName)
6
+ const mtime = new Date(stat.mtime).getTime()
7
+ if (Date.now() - mtime < cache_in_minutes * 60 * 1000) {
8
+ // cache is still valid
9
+ return true
10
+ }
11
+ }
12
+ return false
13
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hash_sha512 = void 0;
4
+ const js_sha512_1 = require("js-sha512");
5
+ function hash_sha512(plainString) {
6
+ if (typeof plainString === 'string')
7
+ return (0, js_sha512_1.sha512)(plainString);
8
+ return "";
9
+ }
10
+ exports.hash_sha512 = hash_sha512;
@@ -0,0 +1,7 @@
1
+ import { sha512 } from 'js-sha512'
2
+
3
+ export function hash_sha512(plainString:string) : string {
4
+ if (typeof plainString==='string')
5
+ return sha512(plainString)
6
+ return ""
7
+ }
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.vaultFill = void 0;
13
+ const debug_1 = require("debug");
14
+ const vaultRead_1 = require("./vaultRead");
15
+ const dbg = (0, debug_1.debug)('vaultFill');
16
+ function vaultFill(vaultInstance, inputObject, ignoreError = false, keepCache = true) {
17
+ // v2.1
18
+ // fills all strings with ^vault@/secret/... with proper vault values
19
+ // if you call it again, it does reload the configuration
20
+ // v2.2
21
+ // supports keepCache - if vault fails, we will keep previous values instead of filling with false
22
+ return new Promise(function (resolve, reject) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ let vaultKeys = Array();
25
+ let vaultResults = {};
26
+ findVaultKeyMappings(inputObject, vaultKeys);
27
+ // read these keys from vault
28
+ for (let idx in vaultKeys) {
29
+ const val = vaultKeys[idx];
30
+ if (typeof val !== "string")
31
+ continue;
32
+ try {
33
+ let got = yield (0, vaultRead_1.vaultRead)(vaultInstance, vaultKeys[idx]);
34
+ got['__vaultkey'] = val;
35
+ got['__vaultfilled'] = Date.now();
36
+ vaultResults[val] = got;
37
+ }
38
+ catch (err) {
39
+ dbg(`Could not read vaultKey '${val}'`, err);
40
+ if (!ignoreError)
41
+ vaultResults[val] = false;
42
+ }
43
+ }
44
+ fillVaultKeyMappings(inputObject, vaultResults, keepCache);
45
+ resolve(true);
46
+ });
47
+ });
48
+ }
49
+ exports.vaultFill = vaultFill;
50
+ function findVaultKeyMappings(config, keys = Array()) {
51
+ for (var i in config) {
52
+ if (config[i] !== null) {
53
+ if (typeof config[i] === 'object' && (config[i].constructor.name === 'Array' || config[i].constructor.name === 'Object'))
54
+ findVaultKeyMappings(config[i], keys);
55
+ // detect previously read vaultKey
56
+ if (i === '__vaultkey') {
57
+ if (!keys.includes(vaultKey))
58
+ keys.push(config[i]);
59
+ continue;
60
+ }
61
+ // --
62
+ if (typeof config[i] === 'string' && config[i].search(/^vault@/) == 0) {
63
+ var splitted = config[i].split(/vault@/);
64
+ if (splitted.length == 2) {
65
+ var vaultKey = splitted[1];
66
+ if (!keys.includes(vaultKey))
67
+ keys.push(vaultKey);
68
+ }
69
+ }
70
+ }
71
+ }
72
+ }
73
+ function fillVaultKeyMappings(config, vaultResults, keepCache = true) {
74
+ for (var i in config) {
75
+ if (config[i] !== null) {
76
+ if (config[i] && config[i].constructor.name === 'Object' && config[i].hasOwnProperty('__vaultkey')) {
77
+ if (keepCache) {
78
+ if (vaultResults[config[i]['__vaultkey']])
79
+ config[i] = vaultResults[config[i]['__vaultkey']];
80
+ }
81
+ else
82
+ config[i] = vaultResults[config[i]['__vaultkey']];
83
+ continue;
84
+ }
85
+ if (typeof config[i] === 'object' && (config[i].constructor.name === 'Array' || config[i].constructor.name === 'Object'))
86
+ fillVaultKeyMappings(config[i], vaultResults);
87
+ if (typeof config[i] === 'string' && config[i].search(/^vault@/) == 0) {
88
+ var splitted = config[i].split(/vault@/);
89
+ if (splitted.length == 2) {
90
+ var vaultKey = splitted[1];
91
+ config[i] = vaultResults[vaultKey];
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
@@ -0,0 +1,89 @@
1
+ import { debug } from "debug"
2
+ import nodeVault from "node-vault";
3
+ import { vaultRead } from "./vaultRead";
4
+ const dbg = debug('vaultFill')
5
+
6
+ export function vaultFill(vaultInstance: nodeVault.client, inputObject: any, ignoreError = false, keepCache = true) {
7
+ // v2.1
8
+ // fills all strings with ^vault@/secret/... with proper vault values
9
+ // if you call it again, it does reload the configuration
10
+ // v2.2
11
+ // supports keepCache - if vault fails, we will keep previous values instead of filling with false
12
+ return new Promise(async function (resolve, reject) {
13
+ let vaultKeys = Array<string>()
14
+ let vaultResults: any = {}
15
+ findVaultKeyMappings(inputObject, vaultKeys)
16
+
17
+ // read these keys from vault
18
+ for (let idx in vaultKeys) {
19
+ const val = vaultKeys[idx]
20
+ if (typeof val !== "string") continue
21
+
22
+ try {
23
+ let got = await vaultRead(vaultInstance, vaultKeys[idx])
24
+ got['__vaultkey'] = val
25
+ got['__vaultfilled'] = Date.now()
26
+ vaultResults[val]=got
27
+ }
28
+ catch (err) {
29
+
30
+ dbg(`Could not read vaultKey '${val}'`, err)
31
+ if (!ignoreError) vaultResults[val]=false
32
+ }
33
+ }
34
+
35
+ fillVaultKeyMappings(inputObject, vaultResults, keepCache)
36
+
37
+ resolve(true)
38
+ })
39
+ }
40
+
41
+
42
+ function findVaultKeyMappings(config: any, keys = Array<string>()) {
43
+ for (var i in config) {
44
+ if (config[i] !== null) {
45
+ if (typeof config[i] === 'object' && (config[i].constructor.name === 'Array' || config[i].constructor.name === 'Object'))
46
+ findVaultKeyMappings(config[i], keys)
47
+
48
+ // detect previously read vaultKey
49
+ if (i === '__vaultkey') {
50
+ if (!keys.includes(vaultKey)) keys.push(config[i])
51
+ continue
52
+ }
53
+ // --
54
+ if (typeof config[i] === 'string' && config[i].search(/^vault@/) == 0) {
55
+ var splitted = config[i].split(/vault@/)
56
+ if (splitted.length == 2) {
57
+ var vaultKey = splitted[1]
58
+ if (!keys.includes(vaultKey)) keys.push(vaultKey)
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+
65
+ function fillVaultKeyMappings(config: any, vaultResults:any, keepCache = true) {
66
+ for (var i in config) {
67
+ if (config[i] !== null) {
68
+
69
+ if (config[i] && config[i].constructor.name === 'Object' && config[i].hasOwnProperty('__vaultkey')) {
70
+ if (keepCache) {
71
+ if (vaultResults[config[i]['__vaultkey']]) config[i] = vaultResults[config[i]['__vaultkey']]
72
+ }
73
+
74
+ else
75
+ config[i] = vaultResults[config[i]['__vaultkey']]
76
+ continue
77
+ }
78
+ if (typeof config[i] === 'object' && (config[i].constructor.name === 'Array' || config[i].constructor.name === 'Object'))
79
+ fillVaultKeyMappings(config[i], vaultResults)
80
+ if (typeof config[i] === 'string' && config[i].search(/^vault@/) == 0) {
81
+ var splitted = config[i].split(/vault@/)
82
+ if (splitted.length == 2) {
83
+ var vaultKey = splitted[1]
84
+ config[i] = vaultResults[vaultKey]
85
+ }
86
+ }
87
+ }
88
+ }
89
+ }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.vaultRead = void 0;
7
+ const debug_1 = require("debug");
8
+ const path_1 = __importDefault(require("path"));
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const node_fs_1 = require("node:fs");
11
+ const fileCacheIsValid_1 = require("./fileCacheIsValid");
12
+ const global_1 = require("../global");
13
+ const hash_sha512_1 = require("./hash_sha512");
14
+ const dbg = (0, debug_1.debug)('vaultRead');
15
+ // need to implement it better
16
+ function vaultRead(vaultInstance, vaultKey, cache_in_minutes = 10) {
17
+ // v2 - supports caching
18
+ return new Promise((resolve, reject) => {
19
+ let cache_file = path_1.default.join(global_1.vault_cache_folder, (0, hash_sha512_1.hash_sha512)(`${vaultInstance.endpoint}/${vaultKey}`));
20
+ if (cache_in_minutes > 0 && (0, fileCacheIsValid_1.fileCacheIsValid)(cache_file, cache_in_minutes))
21
+ return resolve(JSON.parse((0, node_fs_1.readFileSync)(cache_file).toString()));
22
+ vaultInstance.read(vaultKey).then((r) => {
23
+ if (r.data) {
24
+ dbg(`vault ${vaultKey} - success`);
25
+ if (cache_in_minutes > 0)
26
+ try {
27
+ fs_1.default.writeFileSync(cache_file, JSON.stringify(r.data));
28
+ fs_1.default.chmodSync(cache_file, 0o600);
29
+ }
30
+ catch (_) { }
31
+ return resolve(r.data);
32
+ }
33
+ dbg(`vault ${vaultKey} - failed`);
34
+ reject(r);
35
+ }).catch(e => {
36
+ dbg(`exception inside vaultRead`, e);
37
+ // we will still return latest information from cache
38
+ try {
39
+ dbg(`returning vault@${vaultKey} from cache due to error`);
40
+ if (fs_1.default.existsSync(cache_file))
41
+ return resolve(JSON.parse(fs_1.default.readFileSync(cache_file).toString()));
42
+ }
43
+ catch (e2) {
44
+ // content of the file is invalid
45
+ console.log(e2);
46
+ }
47
+ reject(e);
48
+ });
49
+ });
50
+ }
51
+ exports.vaultRead = vaultRead;
@@ -0,0 +1,45 @@
1
+ import { debug } from "debug"
2
+ import nodeVault from "node-vault";
3
+ import path from "path";
4
+ import fs from "fs";
5
+ import { readFileSync } from 'node:fs';
6
+ import { fileCacheIsValid } from "./fileCacheIsValid";
7
+ import { vault_cache_folder } from "../global";
8
+ import { hash_sha512 } from "./hash_sha512";
9
+ const dbg = debug('vaultRead')
10
+
11
+ // need to implement it better
12
+
13
+ export function vaultRead(vaultInstance: nodeVault.client, vaultKey: string, cache_in_minutes = 10) : Promise<any> {
14
+ // v2 - supports caching
15
+ return new Promise((resolve, reject) => {
16
+ let cache_file = path.join(vault_cache_folder, hash_sha512(`${vaultInstance.endpoint}/${vaultKey}`))
17
+
18
+ if (cache_in_minutes > 0 && fileCacheIsValid(cache_file, cache_in_minutes))
19
+ return resolve(JSON.parse(readFileSync(cache_file as string).toString()))
20
+
21
+ vaultInstance.read(vaultKey).then((r) => {
22
+ if (r.data) {
23
+ dbg(`vault ${vaultKey} - success`)
24
+ if (cache_in_minutes > 0)
25
+ try { fs.writeFileSync(cache_file, JSON.stringify(r.data)); fs.chmodSync(cache_file, 0o600); } catch (_) { }
26
+ return resolve(r.data);
27
+ }
28
+ dbg(`vault ${vaultKey} - failed`)
29
+ reject(r);
30
+ }).catch(e => {
31
+ dbg(`exception inside vaultRead`, e)
32
+ // we will still return latest information from cache
33
+ try {
34
+ dbg(`returning vault@${vaultKey} from cache due to error`)
35
+ if (fs.existsSync(cache_file))
36
+ return resolve(JSON.parse(fs.readFileSync(cache_file as string).toString()))
37
+ } catch (e2) {
38
+ // content of the file is invalid
39
+ console.log(e2)
40
+ }
41
+
42
+ reject(e);
43
+ })
44
+ })
45
+ }
@@ -0,0 +1 @@
1
+ export declare function wait(seconds?: number): Promise<void>;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wait = void 0;
4
+ function wait(seconds = 1) {
5
+ return new Promise((resolve, reject) => {
6
+ setTimeout(resolve, seconds * 1000);
7
+ });
8
+ }
9
+ exports.wait = wait;
@@ -0,0 +1,7 @@
1
+
2
+
3
+ export function wait(seconds=1) : Promise<void> {
4
+ return new Promise((resolve,reject)=>{
5
+ setTimeout(resolve,seconds*1000)
6
+ })
7
+ }
package/ts/global.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.vault_cache_folder = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ exports.vault_cache_folder = '/var/tmp/vault-cache';
9
+ try {
10
+ if (!fs_1.default.existsSync(exports.vault_cache_folder))
11
+ fs_1.default.mkdirSync(exports.vault_cache_folder);
12
+ }
13
+ catch (e) {
14
+ console.log(e);
15
+ }
package/ts/global.ts ADDED
@@ -0,0 +1,10 @@
1
+ import fs from 'fs'
2
+
3
+ export let vault_cache_folder = '/var/tmp/vault-cache'
4
+
5
+ try {
6
+ if (!fs.existsSync(vault_cache_folder))
7
+ fs.mkdirSync(vault_cache_folder)
8
+ } catch(e) {
9
+ console.log(e)
10
+ }
package/ts/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from "./funcs/isLocal";
3
3
  export * from "./funcs/randomString";
4
4
  export * from "./funcs/utcnow";
5
5
  export * from "./funcs/validIp";
6
+ export * from "./funcs/wait";
package/ts/index.js CHANGED
@@ -19,3 +19,8 @@ __exportStar(require("./funcs/isLocal"), exports);
19
19
  __exportStar(require("./funcs/randomString"), exports);
20
20
  __exportStar(require("./funcs/utcnow"), exports);
21
21
  __exportStar(require("./funcs/validIp"), exports);
22
+ __exportStar(require("./funcs/wait"), exports);
23
+ __exportStar(require("./funcs/fileCacheIsValid"), exports);
24
+ __exportStar(require("./funcs/hash_sha512"), exports);
25
+ __exportStar(require("./funcs/vaultRead"), exports);
26
+ __exportStar(require("./funcs/vaultFill"), exports);
package/ts/index.test.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import * as IMPORTS from "./index";
2
2
 
3
3
  test('exports', () => {
4
- expect(typeof IMPORTS.Geoip2Paths).toBe('function')
5
- expect(typeof IMPORTS.isLocal).toBe('function')
6
- expect(typeof IMPORTS.randomString).toBe('function')
7
- expect(typeof IMPORTS.utcnow).toBe('function')
8
- expect(typeof IMPORTS.validIp).toBe('function')
4
+ for(let key in IMPORTS) {
5
+ expect(typeof IMPORTS[key]).toBe('function')
6
+ }
9
7
  })
package/ts/index.ts CHANGED
@@ -3,4 +3,8 @@ export * from "./funcs/isLocal"
3
3
  export * from "./funcs/randomString"
4
4
  export * from "./funcs/utcnow"
5
5
  export * from "./funcs/validIp"
6
-
6
+ export * from "./funcs/wait"
7
+ export * from "./funcs/fileCacheIsValid"
8
+ export * from "./funcs/hash_sha512"
9
+ export * from "./funcs/vaultRead"
10
+ export * from "./funcs/vaultFill"