mybase 1.1.53 → 1.2.3
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/ip6addr.d.ts +10 -1
- package/mybase.d.ts +57 -0
- package/mybase.js +401 -684
- package/mybase.js.map +1 -0
- package/package.json +18 -2
- package/ts/funcs/Geoip2Paths.js +7 -5
- package/ts/funcs/Geoip2Paths.js.map +1 -0
- package/ts/funcs/MaxRuntimeHours.js.map +1 -0
- package/ts/funcs/asJSON.d.ts +1 -1
- package/ts/funcs/asJSON.js +6 -4
- package/ts/funcs/asJSON.js.map +1 -0
- package/ts/funcs/deepCopy.js.map +1 -0
- package/ts/funcs/ensureFolder.js.map +1 -0
- package/ts/funcs/fileCacheIsValid.js.map +1 -0
- package/ts/funcs/getMysql1.js.map +1 -0
- package/ts/funcs/getMysql2.js.map +1 -0
- package/ts/funcs/getWeekNumber.js.map +1 -0
- package/ts/funcs/hash_sha512.d.ts +1 -1
- package/ts/funcs/hash_sha512.js +4 -4
- package/ts/funcs/hash_sha512.js.map +1 -0
- package/ts/funcs/initMysql2Pool.js.map +1 -0
- package/ts/funcs/int2ip.js.map +1 -0
- package/ts/funcs/ip2int.js.map +1 -0
- package/ts/funcs/isLANIp.d.ts +2 -3
- package/ts/funcs/isLANIp.js +14 -15
- package/ts/funcs/isLANIp.js.map +1 -0
- package/ts/funcs/isLocal.js.map +1 -0
- package/ts/funcs/isLoopbackIP.d.ts +2 -3
- package/ts/funcs/isLoopbackIP.js +15 -16
- package/ts/funcs/isLoopbackIP.js.map +1 -0
- package/ts/funcs/knexConnection.js.map +1 -0
- package/ts/funcs/promiseTimeout.js.map +1 -0
- package/ts/funcs/randomIP.js.map +1 -0
- package/ts/funcs/randomIP6.js.map +1 -0
- package/ts/funcs/randomString.js.map +1 -0
- package/ts/funcs/randomTCPPort.js.map +1 -0
- package/ts/funcs/randomUTFString.js.map +1 -0
- package/ts/funcs/utcnow.js.map +1 -0
- package/ts/funcs/validEmail.d.ts +1 -1
- package/ts/funcs/validEmail.js +0 -3
- package/ts/funcs/validEmail.js.map +1 -0
- package/ts/funcs/validIp.js.map +1 -0
- package/ts/funcs/vaultFill.js +1 -1
- package/ts/funcs/vaultFill.js.map +1 -0
- package/ts/funcs/vaultRead.js +9 -3
- package/ts/funcs/vaultRead.js.map +1 -0
- package/ts/funcs/wait.js.map +1 -0
- package/ts/global.js.map +1 -0
- package/ts/index.js.map +1 -0
- package/ts/models/DateIterator.js.map +1 -0
- package/ts/models/IPAddress.d.ts +13 -13
- package/ts/models/IPAddress.js.map +1 -0
- package/ts/models/Interfaces.js.map +1 -0
- package/ts/models/OTPGenerator.js.map +1 -0
- package/ts/models/Timespan.js.map +1 -0
- package/ts/models/Unixtime.js.map +1 -0
- package/ts/types.d.ts +35 -0
- package/ts/types.js +1 -0
- package/ts/types.js.map +1 -0
- package/jest/isLANIp.test.js +0 -36
- package/jest/isLoopbackIP.test.js +0 -31
- package/jest.config.js +0 -7
- package/ts/funcs/Geoip2Paths.test.ts +0 -11
- package/ts/funcs/Geoip2Paths.ts +0 -26
- package/ts/funcs/MaxRuntimeHours.ts +0 -7
- package/ts/funcs/asJSON.test.js +0 -72
- package/ts/funcs/asJSON.ts +0 -6
- package/ts/funcs/deepCopy.test.ts +0 -108
- package/ts/funcs/deepCopy.ts +0 -34
- package/ts/funcs/ensureFolder.test.ts +0 -79
- package/ts/funcs/ensureFolder.ts +0 -21
- package/ts/funcs/fileCacheIsValid.ts +0 -13
- package/ts/funcs/getMysql1.ts +0 -41
- package/ts/funcs/getMysql2.ts +0 -42
- package/ts/funcs/getWeekNumber.test.ts +0 -9
- package/ts/funcs/getWeekNumber.ts +0 -31
- package/ts/funcs/hash_sha512.ts +0 -7
- package/ts/funcs/initMysql2Pool.ts +0 -11
- package/ts/funcs/int2ip.test.js +0 -33
- package/ts/funcs/int2ip.ts +0 -9
- package/ts/funcs/ip2int.test.js +0 -30
- package/ts/funcs/ip2int.ts +0 -10
- package/ts/funcs/isLANIp.test.ts +0 -59
- package/ts/funcs/isLANIp.ts +0 -37
- package/ts/funcs/isLocal.test.ts +0 -16
- package/ts/funcs/isLocal.ts +0 -5
- package/ts/funcs/isLoopbackIP.test.ts +0 -72
- package/ts/funcs/isLoopbackIP.ts +0 -32
- package/ts/funcs/knexConnection.ts +0 -42
- package/ts/funcs/promiseTimeout.test.js +0 -104
- package/ts/funcs/promiseTimeout.ts +0 -19
- package/ts/funcs/randomIP.test.ts +0 -16
- package/ts/funcs/randomIP.ts +0 -10
- package/ts/funcs/randomIP6.test.js +0 -18
- package/ts/funcs/randomIP6.ts +0 -12
- package/ts/funcs/randomString.test.ts +0 -15
- package/ts/funcs/randomString.ts +0 -5
- package/ts/funcs/randomTCPPort.test.ts +0 -62
- package/ts/funcs/randomTCPPort.ts +0 -58
- package/ts/funcs/randomUTFString.test.ts +0 -44
- package/ts/funcs/randomUTFString.ts +0 -33
- package/ts/funcs/utcnow.ts +0 -5
- package/ts/funcs/validEmail.test.js +0 -31
- package/ts/funcs/validEmail.ts +0 -13
- package/ts/funcs/validIp.test.ts +0 -33
- package/ts/funcs/validIp.ts +0 -5
- package/ts/funcs/vaultFill.ts +0 -89
- package/ts/funcs/vaultRead.ts +0 -45
- package/ts/funcs/wait.ts +0 -7
- package/ts/global.ts +0 -10
- package/ts/index.test.ts +0 -7
- package/ts/index.ts +0 -39
- package/ts/models/DateIterator.test.ts +0 -149
- package/ts/models/DateIterator.ts +0 -80
- package/ts/models/IPAddress.test.ts +0 -393
- package/ts/models/IPAddress.ts +0 -272
- package/ts/models/Interfaces.ts +0 -58
- package/ts/models/OTPGenerator.test.ts +0 -42
- package/ts/models/OTPGenerator.ts +0 -43
- package/ts/models/Timespan.ts +0 -80
- package/ts/models/Unixtime.test.ts +0 -168
- package/ts/models/Unixtime.ts +0 -208
- package/ts/types.ts +0 -1
- package/tsconfig.json +0 -36
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import * as net from 'net'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This function returns a random port number that is not in use and we should validate this port number by trying to listen to it and releasing
|
|
5
|
-
* this port number immediately
|
|
6
|
-
* This function should be used for testing purposes only
|
|
7
|
-
* This function is not suitable for production use
|
|
8
|
-
* @param timeout the timeout for each port check
|
|
9
|
-
* @param fromPort the first port number to check defaults to 1025
|
|
10
|
-
* @param untilPort the last port number to check defaults to 65534
|
|
11
|
-
* @param tries the number of tries to find a port number
|
|
12
|
-
* @param host the host to bind to
|
|
13
|
-
*/
|
|
14
|
-
export async function randomTCPPort(timeout: number = 1000, fromPort: number = 1025, untilPort: number = 65534, tries = 1000, host = '127.0.0.1'): Promise<number | false> {
|
|
15
|
-
return new Promise(async function (resolve, reject) {
|
|
16
|
-
// check if the given port range is valid
|
|
17
|
-
if (fromPort > untilPort) return resolve(false)
|
|
18
|
-
// check if the given port range is valid
|
|
19
|
-
if (fromPort < 0 || untilPort < 0) return resolve(false)
|
|
20
|
-
// check if the given port range is valid
|
|
21
|
-
if (fromPort > 65535 || untilPort > 65535) return resolve(false)
|
|
22
|
-
|
|
23
|
-
// this function shall return a random port number that is not in use and we should validate this port number by trying to listen to it and releasing
|
|
24
|
-
// this port number immediately
|
|
25
|
-
// this function should be used for testing purposes only
|
|
26
|
-
// this function is not suitable for production use
|
|
27
|
-
while(true && tries > 0) {
|
|
28
|
-
tries--
|
|
29
|
-
let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort)
|
|
30
|
-
let server = net.createServer()
|
|
31
|
-
let checkPort = new Promise((resolveInner, reject) => {
|
|
32
|
-
server.on('error', (err) => {
|
|
33
|
-
// any error should raise problem
|
|
34
|
-
resolveInner(false)
|
|
35
|
-
})
|
|
36
|
-
server.on('listening', () => {
|
|
37
|
-
resolveInner(true)
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
server.listen(port, host)
|
|
41
|
-
let result = await checkPort
|
|
42
|
-
if (result) {
|
|
43
|
-
let to = setTimeout(() => {
|
|
44
|
-
return resolve(port)
|
|
45
|
-
},1000)
|
|
46
|
-
server.on('close', () => {
|
|
47
|
-
clearTimeout(to)
|
|
48
|
-
return resolve(port)
|
|
49
|
-
})
|
|
50
|
-
server.close()
|
|
51
|
-
return
|
|
52
|
-
} else server.close()
|
|
53
|
-
}
|
|
54
|
-
if (tries===0)
|
|
55
|
-
return resolve(false)
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { randomUTFString } from './randomUTFString';
|
|
2
|
-
|
|
3
|
-
describe('randomUTFString', () => {
|
|
4
|
-
const asciiCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
5
|
-
let extendedCharset =
|
|
6
|
-
'àáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ' + // Accented Latin characters
|
|
7
|
-
'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρσςτυφχψω' + // Greek
|
|
8
|
-
'אבגדהוזחטיךכלםמןנסעפצקרשת' + // Hebrew
|
|
9
|
-
//Arabic
|
|
10
|
-
'ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ط ظ ع غ ف ق ك ل م ن ه و ي' +
|
|
11
|
-
// Persian
|
|
12
|
-
'آ ا ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی' +
|
|
13
|
-
// Turkish
|
|
14
|
-
'a b c ç d e f g ğ h ı i j k l m n o ö p r s ş t u ü v y z' +
|
|
15
|
-
'A B C Ç D E F G Ğ H I İ J K L M N O Ö P R S Ş T U Ü V Y Z' +
|
|
16
|
-
// More Emojis
|
|
17
|
-
'🙂🤔😍😂👍🚀❤️🎉' +
|
|
18
|
-
'абвгдежзийклмнопрстуфхцчшщъыьэюя' + // Cyrillic (Russian, etc.)
|
|
19
|
-
'日本語中文한국어' + // Japanese, Chinese, Korean characters
|
|
20
|
-
'🙂🤔😍😂👍🚀❤️🎉' // Emojis
|
|
21
|
-
it('should generate a random string within the specified length range', () => {
|
|
22
|
-
const minLength = 5;
|
|
23
|
-
const maxLength = 10;
|
|
24
|
-
const result = randomUTFString(minLength, maxLength);
|
|
25
|
-
expect(result.length).toBeGreaterThanOrEqual(minLength);
|
|
26
|
-
expect(result.length).toBeLessThanOrEqual(maxLength+1); // utf8 characters can be 1-4 bytes long
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should include ASCII characters by default', () => {
|
|
30
|
-
const result = randomUTFString(500, 400);
|
|
31
|
-
|
|
32
|
-
expect(result).toMatch(new RegExp(`[${asciiCharacters}]`));
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('should exclude ASCII characters when includeAscii is set to false', () => {
|
|
36
|
-
const result = randomUTFString(500, 1000, false);
|
|
37
|
-
expect(result).not.toMatch(new RegExp(`[${asciiCharacters}]`));
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('should include extended characters when includeAscii is set to true', () => {
|
|
41
|
-
const result = randomUTFString(500, 1000, true);
|
|
42
|
-
expect(result).toMatch(new RegExp(`[${extendedCharset}]`));
|
|
43
|
-
});
|
|
44
|
-
});
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export function randomUTFString(minLength: number, maxLength: number, includeAscii:boolean=true): string {
|
|
5
|
-
// Extended characters set including various languages and symbols
|
|
6
|
-
let characters =
|
|
7
|
-
'àáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ' + // Accented Latin characters
|
|
8
|
-
'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρσςτυφχψω' + // Greek
|
|
9
|
-
'אבגדהוזחטיךכלםמןנסעפצקרשת' + // Hebrew
|
|
10
|
-
//Arabic
|
|
11
|
-
'ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ط ظ ع غ ف ق ك ل م ن ه و ي' +
|
|
12
|
-
// Persian
|
|
13
|
-
'آ ا ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی' +
|
|
14
|
-
// Turkish
|
|
15
|
-
'ğıöşü' +
|
|
16
|
-
'ĞİÖŞÜ ' +
|
|
17
|
-
// More Emojis
|
|
18
|
-
'🙂🤔😍😂👍🚀❤️🎉' +
|
|
19
|
-
'бвгдежзийклмнопрстуфхцчшщъыьэюя' + // Cyrillic (Russian, etc.)
|
|
20
|
-
'日本語中文한국어' + // Japanese, Chinese, Korean characters
|
|
21
|
-
'🙂🤔😍😂👍🚀❤️🎉' // Emojis
|
|
22
|
-
|
|
23
|
-
if (includeAscii) characters += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
|
24
|
-
|
|
25
|
-
const charactersLength = Array.from(characters).length
|
|
26
|
-
const length = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength
|
|
27
|
-
let result = ''
|
|
28
|
-
|
|
29
|
-
for (let i = 0; i < length; i++)
|
|
30
|
-
result += Array.from(characters)[Math.floor(Math.random() * charactersLength)]
|
|
31
|
-
|
|
32
|
-
return result
|
|
33
|
-
}
|
package/ts/funcs/utcnow.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
const { validEmail } = require('./validEmail')
|
|
2
|
-
|
|
3
|
-
// since we use validator, we do not need to test it further
|
|
4
|
-
// detailed tests with millions of emails are in tests/ folder which is .gitignored
|
|
5
|
-
|
|
6
|
-
describe('validEmail', () => {
|
|
7
|
-
test('should return true for a valid email', () => {
|
|
8
|
-
const email = 'test@example.com'
|
|
9
|
-
expect(validEmail(email)).toBe(true)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
test('should return false for an invalid email', () => {
|
|
13
|
-
const email = 'invalidemail'
|
|
14
|
-
expect(validEmail(email)).toBe(false)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
test('should return false for an empty email', () => {
|
|
18
|
-
const email = ''
|
|
19
|
-
expect(validEmail(email)).toBe(false)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
test('should return false for a null email', () => {
|
|
23
|
-
const email = null
|
|
24
|
-
expect(validEmail(email)).toBe(false)
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
test('should return false for a non-string email', () => {
|
|
28
|
-
const email = 12345
|
|
29
|
-
expect(validEmail(email)).toBe(false)
|
|
30
|
-
})
|
|
31
|
-
})
|
package/ts/funcs/validEmail.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import validator from 'validator'
|
|
2
|
-
|
|
3
|
-
export function validEmail(email:string) {
|
|
4
|
-
if (typeof email==='string')
|
|
5
|
-
return validator.isEmail(email) // validator needs a string
|
|
6
|
-
return false
|
|
7
|
-
// giving up old style, was not reliable
|
|
8
|
-
// taken from https://www.w3resource.com/javascript/form/email-validation.php
|
|
9
|
-
// strange looking emails might be indeed valid
|
|
10
|
-
// check https://www.w3resource.com/javascript/form/example-javascript-form-validation-email-REC-2822.html
|
|
11
|
-
if (typeof email==='string') email=email.toLowerCase().trim()
|
|
12
|
-
return (/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/).test(email)
|
|
13
|
-
}
|
package/ts/funcs/validIp.test.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { validIp } from './validIp';
|
|
2
|
-
|
|
3
|
-
describe('validIp', () => {
|
|
4
|
-
it('should return true for a valid IP address', () => {
|
|
5
|
-
const ipAddress = '192.168.0.1';
|
|
6
|
-
const result = validIp(ipAddress);
|
|
7
|
-
expect(result).toBe(4);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('should return false for an invalid IP address', () => {
|
|
11
|
-
const ipAddress = '256.0.0.1';
|
|
12
|
-
const result = validIp(ipAddress);
|
|
13
|
-
expect(result).toBe(0);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('should support IPv6', () => {
|
|
17
|
-
const ipAddress = '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
|
|
18
|
-
const result = validIp(ipAddress);
|
|
19
|
-
expect(result).toBe(6);
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('invalid IPv6', () => {
|
|
23
|
-
const ipAddress = '2001:0db8:85a3:0000:0000:8a2e:0370:7334:0000';
|
|
24
|
-
const result = validIp(ipAddress);
|
|
25
|
-
expect(result).toBe(0);
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should support IPv6 shorthand', () => {
|
|
29
|
-
const ipAddress = '2001:db8:85a3::8a2e:370:7334';
|
|
30
|
-
const result = validIp(ipAddress);
|
|
31
|
-
expect(result).toBe(6);
|
|
32
|
-
})
|
|
33
|
-
})
|
package/ts/funcs/validIp.ts
DELETED
package/ts/funcs/vaultFill.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
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
|
-
}
|
package/ts/funcs/vaultRead.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
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 '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
|
-
}
|
package/ts/funcs/wait.ts
DELETED
package/ts/global.ts
DELETED
package/ts/index.test.ts
DELETED
package/ts/index.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
export * from "./funcs/getWeekNumber"
|
|
2
|
-
export * from "./funcs/Geoip2Paths";
|
|
3
|
-
export * from "./funcs/isLocal"
|
|
4
|
-
export * from "./funcs/randomString"
|
|
5
|
-
export * from "./funcs/utcnow"
|
|
6
|
-
export * from "./funcs/validIp"
|
|
7
|
-
export * from "./funcs/wait"
|
|
8
|
-
export * from "./funcs/fileCacheIsValid"
|
|
9
|
-
export * from "./funcs/hash_sha512"
|
|
10
|
-
export * from "./funcs/vaultRead"
|
|
11
|
-
export * from "./funcs/vaultFill"
|
|
12
|
-
export * from "./funcs/asJSON"
|
|
13
|
-
export * from "./funcs/promiseTimeout"
|
|
14
|
-
export * from "./funcs/randomIP"
|
|
15
|
-
export * from "./funcs/ip2int"
|
|
16
|
-
export * from "./funcs/int2ip"
|
|
17
|
-
export * from "./funcs/randomIP6"
|
|
18
|
-
export * from "./funcs/randomTCPPort"
|
|
19
|
-
export * from "./funcs/validEmail"
|
|
20
|
-
export * from "./funcs/isLoopbackIP"
|
|
21
|
-
export * from "./funcs/isLANIp"
|
|
22
|
-
export * from "./funcs/getMysql1"
|
|
23
|
-
export * from "./funcs/getMysql2"
|
|
24
|
-
export * from "./funcs/initMysql2Pool"
|
|
25
|
-
export * from "./funcs/randomUTFString"
|
|
26
|
-
export * from "./funcs/MaxRuntimeHours"
|
|
27
|
-
export * from "./funcs/ensureFolder"
|
|
28
|
-
export * from "./funcs/knexConnection"
|
|
29
|
-
export * from "./funcs/deepCopy"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
// models
|
|
34
|
-
export * from "./models/Unixtime"
|
|
35
|
-
export * from "./models/Timespan"
|
|
36
|
-
export * from "./models/IPAddress"
|
|
37
|
-
export * from "./models/OTPGenerator"
|
|
38
|
-
export * from "./models/Interfaces"
|
|
39
|
-
export * from "./models/DateIterator"
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { DateIterator } from './DateIterator';
|
|
2
|
-
import { Unixtime } from './Unixtime';
|
|
3
|
-
|
|
4
|
-
describe('DateIterator', () => {
|
|
5
|
-
const start = () => Unixtime.fromYYYYMMDD('20240110');
|
|
6
|
-
const end = () => Unixtime.fromYYYYMMDD('20240105');
|
|
7
|
-
|
|
8
|
-
it('should iterate backwards day-by-day (end-exclusive) with default separator', () => {
|
|
9
|
-
const it = new DateIterator(start(), end());
|
|
10
|
-
expect(it.toArray()).toEqual([
|
|
11
|
-
'20240110',
|
|
12
|
-
'20240109',
|
|
13
|
-
'20240108',
|
|
14
|
-
'20240107',
|
|
15
|
-
'20240106',
|
|
16
|
-
]);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should honor a custom separator', () => {
|
|
20
|
-
const it = new DateIterator(start(), end(), '-');
|
|
21
|
-
expect(it.toArray()).toEqual([
|
|
22
|
-
'2024-01-10',
|
|
23
|
-
'2024-01-09',
|
|
24
|
-
'2024-01-08',
|
|
25
|
-
'2024-01-07',
|
|
26
|
-
'2024-01-06',
|
|
27
|
-
]);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should honor a slash separator', () => {
|
|
31
|
-
const it = new DateIterator(start(), end(), '/');
|
|
32
|
-
expect(it.toArray()).toEqual([
|
|
33
|
-
'2024/01/10',
|
|
34
|
-
'2024/01/09',
|
|
35
|
-
'2024/01/08',
|
|
36
|
-
'2024/01/07',
|
|
37
|
-
'2024/01/06',
|
|
38
|
-
]);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('next() should return null when exhausted', () => {
|
|
42
|
-
const s = Unixtime.fromYYYYMMDD('20240103');
|
|
43
|
-
const e = Unixtime.fromYYYYMMDD('20240101');
|
|
44
|
-
const it = new DateIterator(s, e);
|
|
45
|
-
expect(it.next()).toEqual('20240103');
|
|
46
|
-
expect(it.next()).toEqual('20240102');
|
|
47
|
-
expect(it.next()).toBeNull();
|
|
48
|
-
expect(it.next()).toBeNull();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should return empty array when start equals end (end-exclusive)', () => {
|
|
52
|
-
const s = Unixtime.fromYYYYMMDD('20240105');
|
|
53
|
-
const e = Unixtime.fromYYYYMMDD('20240105');
|
|
54
|
-
const it = new DateIterator(s, e);
|
|
55
|
-
expect(it.toArray()).toEqual([]);
|
|
56
|
-
expect(it.next()).toBeNull();
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('should throw when start is older than end', () => {
|
|
60
|
-
const s = Unixtime.fromYYYYMMDD('20240101');
|
|
61
|
-
const e = Unixtime.fromYYYYMMDD('20240110');
|
|
62
|
-
expect(() => new DateIterator(s, e)).toThrow(
|
|
63
|
-
'DateIterator: `start` must be newer than or equal to `end`'
|
|
64
|
-
);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should not mutate the caller-supplied Unixtime instances', () => {
|
|
68
|
-
const s = Unixtime.fromYYYYMMDD('20240110');
|
|
69
|
-
const e = Unixtime.fromYYYYMMDD('20240105');
|
|
70
|
-
const sBefore = s.toLongUnixtime();
|
|
71
|
-
const eBefore = e.toLongUnixtime();
|
|
72
|
-
const it = new DateIterator(s, e);
|
|
73
|
-
it.toArray();
|
|
74
|
-
expect(s.toLongUnixtime()).toEqual(sBefore);
|
|
75
|
-
expect(e.toLongUnixtime()).toEqual(eBefore);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('reset() should allow the iterator to be consumed again', () => {
|
|
79
|
-
const it = new DateIterator(start(), end());
|
|
80
|
-
const first = it.toArray();
|
|
81
|
-
expect(it.next()).toBeNull();
|
|
82
|
-
it.reset();
|
|
83
|
-
const second = it.toArray();
|
|
84
|
-
expect(second).toEqual(first);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('reset() should return the iterator for chaining', () => {
|
|
88
|
-
const it = new DateIterator(start(), end());
|
|
89
|
-
expect(it.reset()).toBe(it);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should be iterable via for..of', () => {
|
|
93
|
-
const it = new DateIterator(start(), end(), '-');
|
|
94
|
-
const out: string[] = [];
|
|
95
|
-
for (const d of it) out.push(d);
|
|
96
|
-
expect(out).toEqual([
|
|
97
|
-
'2024-01-10',
|
|
98
|
-
'2024-01-09',
|
|
99
|
-
'2024-01-08',
|
|
100
|
-
'2024-01-07',
|
|
101
|
-
'2024-01-06',
|
|
102
|
-
]);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('for..of should be independent of next()/toArray() cursor', () => {
|
|
106
|
-
const it = new DateIterator(start(), end());
|
|
107
|
-
expect(it.next()).toEqual('20240110');
|
|
108
|
-
const collected: string[] = [];
|
|
109
|
-
for (const d of it) collected.push(d);
|
|
110
|
-
expect(collected).toEqual([
|
|
111
|
-
'20240110',
|
|
112
|
-
'20240109',
|
|
113
|
-
'20240108',
|
|
114
|
-
'20240107',
|
|
115
|
-
'20240106',
|
|
116
|
-
]);
|
|
117
|
-
expect(it.next()).toEqual('20240109');
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should support spread syntax', () => {
|
|
121
|
-
const it = new DateIterator(
|
|
122
|
-
Unixtime.fromYYYYMMDD('20240103'),
|
|
123
|
-
Unixtime.fromYYYYMMDD('20240101')
|
|
124
|
-
);
|
|
125
|
-
expect([...it]).toEqual(['20240103', '20240102']);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should cross month boundaries correctly', () => {
|
|
129
|
-
const s = Unixtime.fromYYYYMMDD('20240302');
|
|
130
|
-
const e = Unixtime.fromYYYYMMDD('20240227');
|
|
131
|
-
const it = new DateIterator(s, e, '-');
|
|
132
|
-
expect(it.toArray()).toEqual([
|
|
133
|
-
'2024-03-02',
|
|
134
|
-
'2024-03-01',
|
|
135
|
-
'2024-02-29',
|
|
136
|
-
'2024-02-28',
|
|
137
|
-
]);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('should produce N entries for a span of N+1 days (end-exclusive)', () => {
|
|
141
|
-
const s = Unixtime.fromYYYYMMDD('20240620');
|
|
142
|
-
const e = Unixtime.fromYYYYMMDD('20240610');
|
|
143
|
-
const it = new DateIterator(s, e);
|
|
144
|
-
const arr = it.toArray();
|
|
145
|
-
expect(arr.length).toEqual(10);
|
|
146
|
-
expect(arr[0]).toEqual('20240620');
|
|
147
|
-
expect(arr[arr.length - 1]).toEqual('20240611');
|
|
148
|
-
});
|
|
149
|
-
});
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Unixtime } from "./Unixtime"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Iterates day-by-day backwards from `start` toward `end` (end-exclusive),
|
|
5
|
-
* emitting each date as a `yyyy{sep}mm{sep}dd` string.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* const it = new DateIterator(Unixtime.now(), Unixtime.now().addDays(-5))
|
|
9
|
-
* it.next() // "20260417"
|
|
10
|
-
* it.next() // "20260416"
|
|
11
|
-
* // ...
|
|
12
|
-
* it.next() // null
|
|
13
|
-
*
|
|
14
|
-
* @example separator
|
|
15
|
-
* new DateIterator(start, end, "-").toArray()
|
|
16
|
-
* // ["2026-04-17", "2026-04-16", ...]
|
|
17
|
-
*
|
|
18
|
-
* @example for..of
|
|
19
|
-
* for (const d of new DateIterator(start, end, "/")) console.log(d)
|
|
20
|
-
*/
|
|
21
|
-
export class DateIterator implements Iterable<string> {
|
|
22
|
-
private readonly start: Unixtime
|
|
23
|
-
private readonly end: Unixtime
|
|
24
|
-
private readonly separator: string
|
|
25
|
-
private cursor: Unixtime | null
|
|
26
|
-
|
|
27
|
-
constructor(start: Unixtime, end: Unixtime, separator: string = "") {
|
|
28
|
-
if (start.lessThan(end)) {
|
|
29
|
-
throw new Error('DateIterator: `start` must be newer than or equal to `end`')
|
|
30
|
-
}
|
|
31
|
-
// Defensive clones: callers' instances must not be mutated, and
|
|
32
|
-
// Unixtime.addDays() mutates the underlying Date.
|
|
33
|
-
this.start = start.clone()
|
|
34
|
-
this.end = end.clone()
|
|
35
|
-
this.separator = separator
|
|
36
|
-
this.cursor = this.start.clone()
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Returns the next formatted date going backwards, or `null` when exhausted. */
|
|
40
|
-
next(): string | null {
|
|
41
|
-
if (this.cursor === null) return null
|
|
42
|
-
if (!this.cursor.greaterThan(this.end)) {
|
|
43
|
-
this.cursor = null
|
|
44
|
-
return null
|
|
45
|
-
}
|
|
46
|
-
const value = this.cursor.yyyymmdd(this.separator)
|
|
47
|
-
this.cursor.addDays(-1)
|
|
48
|
-
return value
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** Reset the iterator so it can be consumed again from `start`. */
|
|
52
|
-
reset(): this {
|
|
53
|
-
this.cursor = this.start.clone()
|
|
54
|
-
return this
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Collect all remaining dates into an array. */
|
|
58
|
-
toArray(): string[] {
|
|
59
|
-
const out: string[] = []
|
|
60
|
-
let d: string | null
|
|
61
|
-
while ((d = this.next()) !== null) out.push(d)
|
|
62
|
-
return out
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
[Symbol.iterator](): Iterator<string> {
|
|
66
|
-
let cursor: Unixtime | null = this.start.clone()
|
|
67
|
-
const end = this.end
|
|
68
|
-
const sep = this.separator
|
|
69
|
-
return {
|
|
70
|
-
next(): IteratorResult<string> {
|
|
71
|
-
if (cursor === null || !cursor.greaterThan(end)) {
|
|
72
|
-
return { value: undefined as unknown as string, done: true }
|
|
73
|
-
}
|
|
74
|
-
const value = cursor.yyyymmdd(sep)
|
|
75
|
-
cursor.addDays(-1)
|
|
76
|
-
return { value, done: false }
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|