mybase 1.2.2 → 2.0.0
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/dist/funcs/Geoip2Paths.js.map +1 -0
- package/dist/funcs/MaxRuntimeHours.js.map +1 -0
- package/dist/funcs/asJSON.js.map +1 -0
- package/dist/funcs/deepCopy.js.map +1 -0
- package/dist/funcs/ensureFolder.js.map +1 -0
- package/dist/funcs/fileCacheIsValid.js.map +1 -0
- package/dist/funcs/getMysql1.js.map +1 -0
- package/dist/funcs/getMysql2.js.map +1 -0
- package/dist/funcs/getWeekNumber.js.map +1 -0
- package/dist/funcs/hash_sha512.js.map +1 -0
- package/dist/funcs/initMysql2Pool.js.map +1 -0
- package/dist/funcs/int2ip.js.map +1 -0
- package/dist/funcs/ip2int.js.map +1 -0
- package/dist/funcs/isLANIp.d.ts +2 -0
- package/{ts → dist}/funcs/isLANIp.js +6 -6
- package/dist/funcs/isLANIp.js.map +1 -0
- package/dist/funcs/isLocal.js.map +1 -0
- package/dist/funcs/isLoopbackIP.d.ts +2 -0
- package/{ts → dist}/funcs/isLoopbackIP.js +3 -3
- package/dist/funcs/isLoopbackIP.js.map +1 -0
- package/dist/funcs/knexConnection.js.map +1 -0
- package/dist/funcs/promiseTimeout.js.map +1 -0
- package/dist/funcs/randomIP.js.map +1 -0
- package/dist/funcs/randomIP6.js.map +1 -0
- package/dist/funcs/randomString.js.map +1 -0
- package/dist/funcs/randomTCPPort.js.map +1 -0
- package/dist/funcs/randomUTFString.js.map +1 -0
- package/dist/funcs/utcnow.js.map +1 -0
- package/dist/funcs/validEmail.js.map +1 -0
- package/dist/funcs/validIp.js.map +1 -0
- package/dist/funcs/vaultFill.js.map +1 -0
- package/dist/funcs/vaultRead.js.map +1 -0
- package/dist/funcs/wait.js.map +1 -0
- package/dist/global.js.map +1 -0
- package/dist/index.d.ts +38 -0
- package/{ts → dist}/index.js +35 -27
- package/dist/index.js.map +1 -0
- package/dist/ip6addr.d.ts +69 -0
- package/dist/ip6addr.js +752 -0
- package/dist/ip6addr.js.map +1 -0
- package/dist/models/DateIterator.js.map +1 -0
- package/{ts → dist}/models/IPAddress.d.ts +2 -1
- package/{ts → dist}/models/IPAddress.js +11 -12
- package/dist/models/IPAddress.js.map +1 -0
- package/dist/models/Interfaces.js.map +1 -0
- package/dist/models/OTPGenerator.js.map +1 -0
- package/dist/models/Timespan.js.map +1 -0
- package/dist/models/Unixtime.js.map +1 -0
- package/{mybase.d.ts → dist/mybase.d.ts} +1 -20
- package/{mybase.js → dist/mybase.js} +1 -56
- package/dist/mybase.js.map +1 -0
- package/{ts → dist}/types.d.ts +10 -0
- package/dist/types.js.map +1 -0
- package/package.json +22 -9
- package/ip6addr.d.ts +0 -61
- package/jest/isLANIp.test.js +0 -36
- package/jest/isLoopbackIP.test.js +0 -31
- package/jest.config.js +0 -13
- package/mybase.test.ts +0 -647
- package/mybase.ts +0 -397
- package/ts/funcs/Geoip2Paths.test.ts +0 -11
- package/ts/funcs/Geoip2Paths.ts +0 -28
- package/ts/funcs/MaxRuntimeHours.ts +0 -7
- package/ts/funcs/asJSON.test.js +0 -72
- package/ts/funcs/asJSON.ts +0 -9
- 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 -6
- 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.d.ts +0 -2
- package/ts/funcs/isLANIp.test.ts +0 -58
- package/ts/funcs/isLANIp.ts +0 -34
- package/ts/funcs/isLocal.test.ts +0 -16
- package/ts/funcs/isLocal.ts +0 -5
- package/ts/funcs/isLoopbackIP.d.ts +0 -2
- package/ts/funcs/isLoopbackIP.test.ts +0 -72
- package/ts/funcs/isLoopbackIP.ts +0 -30
- 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 -11
- 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 -50
- package/ts/funcs/wait.ts +0 -7
- package/ts/global.ts +0 -10
- package/ts/index.d.ts +0 -35
- 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 -42
- package/tsconfig.jest.json +0 -11
- package/tsconfig.json +0 -37
- package/types/third-party.d.ts +0 -21
- /package/{ts → dist}/funcs/Geoip2Paths.d.ts +0 -0
- /package/{ts → dist}/funcs/Geoip2Paths.js +0 -0
- /package/{ts → dist}/funcs/MaxRuntimeHours.d.ts +0 -0
- /package/{ts → dist}/funcs/MaxRuntimeHours.js +0 -0
- /package/{ts → dist}/funcs/asJSON.d.ts +0 -0
- /package/{ts → dist}/funcs/asJSON.js +0 -0
- /package/{ts → dist}/funcs/deepCopy.d.ts +0 -0
- /package/{ts → dist}/funcs/deepCopy.js +0 -0
- /package/{ts → dist}/funcs/ensureFolder.d.ts +0 -0
- /package/{ts → dist}/funcs/ensureFolder.js +0 -0
- /package/{ts → dist}/funcs/fileCacheIsValid.d.ts +0 -0
- /package/{ts → dist}/funcs/fileCacheIsValid.js +0 -0
- /package/{ts → dist}/funcs/getMysql1.d.ts +0 -0
- /package/{ts → dist}/funcs/getMysql1.js +0 -0
- /package/{ts → dist}/funcs/getMysql2.d.ts +0 -0
- /package/{ts → dist}/funcs/getMysql2.js +0 -0
- /package/{ts → dist}/funcs/getWeekNumber.d.ts +0 -0
- /package/{ts → dist}/funcs/getWeekNumber.js +0 -0
- /package/{ts → dist}/funcs/hash_sha512.d.ts +0 -0
- /package/{ts → dist}/funcs/hash_sha512.js +0 -0
- /package/{ts → dist}/funcs/initMysql2Pool.d.ts +0 -0
- /package/{ts → dist}/funcs/initMysql2Pool.js +0 -0
- /package/{ts → dist}/funcs/int2ip.d.ts +0 -0
- /package/{ts → dist}/funcs/int2ip.js +0 -0
- /package/{ts → dist}/funcs/ip2int.d.ts +0 -0
- /package/{ts → dist}/funcs/ip2int.js +0 -0
- /package/{ts → dist}/funcs/isLocal.d.ts +0 -0
- /package/{ts → dist}/funcs/isLocal.js +0 -0
- /package/{ts → dist}/funcs/knexConnection.d.ts +0 -0
- /package/{ts → dist}/funcs/knexConnection.js +0 -0
- /package/{ts → dist}/funcs/promiseTimeout.d.ts +0 -0
- /package/{ts → dist}/funcs/promiseTimeout.js +0 -0
- /package/{ts → dist}/funcs/randomIP.d.ts +0 -0
- /package/{ts → dist}/funcs/randomIP.js +0 -0
- /package/{ts → dist}/funcs/randomIP6.d.ts +0 -0
- /package/{ts → dist}/funcs/randomIP6.js +0 -0
- /package/{ts → dist}/funcs/randomString.d.ts +0 -0
- /package/{ts → dist}/funcs/randomString.js +0 -0
- /package/{ts → dist}/funcs/randomTCPPort.d.ts +0 -0
- /package/{ts → dist}/funcs/randomTCPPort.js +0 -0
- /package/{ts → dist}/funcs/randomUTFString.d.ts +0 -0
- /package/{ts → dist}/funcs/randomUTFString.js +0 -0
- /package/{ts → dist}/funcs/utcnow.d.ts +0 -0
- /package/{ts → dist}/funcs/utcnow.js +0 -0
- /package/{ts → dist}/funcs/validEmail.d.ts +0 -0
- /package/{ts → dist}/funcs/validEmail.js +0 -0
- /package/{ts → dist}/funcs/validIp.d.ts +0 -0
- /package/{ts → dist}/funcs/validIp.js +0 -0
- /package/{ts → dist}/funcs/vaultFill.d.ts +0 -0
- /package/{ts → dist}/funcs/vaultFill.js +0 -0
- /package/{ts → dist}/funcs/vaultRead.d.ts +0 -0
- /package/{ts → dist}/funcs/vaultRead.js +0 -0
- /package/{ts → dist}/funcs/wait.d.ts +0 -0
- /package/{ts → dist}/funcs/wait.js +0 -0
- /package/{ts → dist}/global.d.ts +0 -0
- /package/{ts → dist}/global.js +0 -0
- /package/{ts → dist}/models/DateIterator.d.ts +0 -0
- /package/{ts → dist}/models/DateIterator.js +0 -0
- /package/{ts → dist}/models/Interfaces.d.ts +0 -0
- /package/{ts → dist}/models/Interfaces.js +0 -0
- /package/{ts → dist}/models/OTPGenerator.d.ts +0 -0
- /package/{ts → dist}/models/OTPGenerator.js +0 -0
- /package/{ts → dist}/models/Timespan.d.ts +0 -0
- /package/{ts → dist}/models/Timespan.js +0 -0
- /package/{ts → dist}/models/Unixtime.d.ts +0 -0
- /package/{ts → dist}/models/Unixtime.js +0 -0
- /package/{ts → dist}/types.js +0 -0
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { ensureFolder } from './ensureFolder'
|
|
2
|
-
import * as fs from 'fs'
|
|
3
|
-
import { wait } from '../index'
|
|
4
|
-
|
|
5
|
-
describe('ensureFolder', () => {
|
|
6
|
-
let folderPath: string
|
|
7
|
-
let filePath: string
|
|
8
|
-
|
|
9
|
-
beforeEach(async () => {
|
|
10
|
-
folderPath = `/tmp/ensureFolder.test.${Date.now()}`
|
|
11
|
-
filePath = `/tmp/ensureFolder.test.${Date.now()}.file`
|
|
12
|
-
await wait(1/1000)
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('should create a folder if it does not exist and return the folder path', () => {
|
|
16
|
-
const folder = ensureFolder(folderPath)
|
|
17
|
-
expect(folder).toBe(folderPath)
|
|
18
|
-
expect(fs.existsSync(folderPath)).toBe(true)
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
it('should throw an error if the folder path exists and is not a folder', () => {
|
|
22
|
-
fs.writeFileSync(filePath, 'test')
|
|
23
|
-
expect(() => ensureFolder(filePath)).toThrow('exists and is not a folder')
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
it('creating same folder twice should return the same folder path', () => {
|
|
27
|
-
expect(ensureFolder(folderPath)).toBe(folderPath)
|
|
28
|
-
expect(fs.existsSync(folderPath)).toBe(true)
|
|
29
|
-
expect(ensureFolder(folderPath)).toBe(folderPath)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it('we should be able to create recursively a folders and default to recursive mode', () => {
|
|
33
|
-
let sfolder= ensureFolder(`${folderPath}/subfolder/subsubfolder`)
|
|
34
|
-
expect(sfolder).toBe(`${folderPath}/subfolder/subsubfolder`)
|
|
35
|
-
if (sfolder) expect(fs.existsSync(sfolder)).toBe(true)
|
|
36
|
-
// test creating 10 recursive folders
|
|
37
|
-
for(let i=0;i<10;i++) {
|
|
38
|
-
sfolder= `${sfolder}/subsubsubfolder${i}`
|
|
39
|
-
expect(ensureFolder(sfolder)).toBe(sfolder)
|
|
40
|
-
if (sfolder) expect(fs.existsSync(sfolder)).toBe(true)
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('we should not be able to disable recursive mode', () => {
|
|
45
|
-
expect(ensureFolder(`${folderPath}/subfolder/subsubfolder`, { recursive: false })).toBe(`${folderPath}/subfolder/subsubfolder`)
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
it('we should be able to create a folder with a custom mode', () => {
|
|
49
|
-
let sfolder= ensureFolder(`${folderPath}/subfolder/subsubfolder`, { mode: 0o700 })
|
|
50
|
-
expect(sfolder).toBe(`${folderPath}/subfolder/subsubfolder`)
|
|
51
|
-
if (sfolder) expect(fs.existsSync(sfolder)).toBe(true)
|
|
52
|
-
expect(fs.statSync(sfolder).mode & 0o777).toBe(0o700);
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('try to create a file where we do not have write permissions should throw an error', () => {
|
|
56
|
-
expect(() => ensureFolder('/dev/ensureFolder.test.file')).toThrow()
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it('should throw an error if folder path is empty', () => {
|
|
60
|
-
expect(() => ensureFolder('')).toThrow();
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should throw an error if folder path is null or undefined or array or object', () => {
|
|
64
|
-
expect(() => ensureFolder(null as unknown as string)).toThrow();
|
|
65
|
-
expect(() => ensureFolder(undefined as unknown as string)).toThrow();
|
|
66
|
-
expect(() => ensureFolder([] as unknown as string)).toThrow();
|
|
67
|
-
expect(() => ensureFolder({} as unknown as string)).toThrow();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('should not change the permissions of an existing folder', () => {
|
|
71
|
-
fs.mkdirSync(folderPath, { mode: 0o755 });
|
|
72
|
-
ensureFolder(folderPath, { mode: 0o700 });
|
|
73
|
-
expect(fs.statSync(folderPath).mode & 0o777).toBe(0o755);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should throw error for invalid mode value', () => {
|
|
77
|
-
|
|
78
|
-
});
|
|
79
|
-
})
|
package/ts/funcs/ensureFolder.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import debug from 'debug'
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import { MakeDirectoryOptions } from 'fs';
|
|
4
|
-
const dbg = debug('_mybase:ensureFolder')
|
|
5
|
-
|
|
6
|
-
export function ensureFolder(folderPath: string, options?: MakeDirectoryOptions | null) : string {
|
|
7
|
-
options = options || {}
|
|
8
|
-
options.recursive = true
|
|
9
|
-
dbg('ensureFolder()', folderPath)
|
|
10
|
-
if (fs.existsSync(folderPath)) {
|
|
11
|
-
if (!fs.lstatSync(folderPath).isDirectory()) {
|
|
12
|
-
dbg(`${folderPath} exists and is not a folder`)
|
|
13
|
-
throw new Error(`${folderPath} exists and is not a folder`);
|
|
14
|
-
}
|
|
15
|
-
dbg(`${folderPath} exists and is a folder`)
|
|
16
|
-
return folderPath
|
|
17
|
-
}
|
|
18
|
-
dbg(`creating ${folderPath}`,options)
|
|
19
|
-
fs.mkdirSync(folderPath, options)
|
|
20
|
-
return folderPath
|
|
21
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
}
|
package/ts/funcs/getMysql1.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import * as mysql1 from "mysql"
|
|
2
|
-
import { MysqlError } from "mysql"
|
|
3
|
-
import debug from "debug"
|
|
4
|
-
const dbg = debug("getMysql1")
|
|
5
|
-
|
|
6
|
-
export type Mysql1ServerConfiguration = {
|
|
7
|
-
host: string,
|
|
8
|
-
port: number,
|
|
9
|
-
login: string,
|
|
10
|
-
password: string,
|
|
11
|
-
db: string
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export async function getMysql1(config: Mysql1ServerConfiguration, pingInterval: number = 0) {
|
|
15
|
-
return new Promise((resolve, reject) => {
|
|
16
|
-
const mysql_client = mysql1.createConnection({
|
|
17
|
-
host: config.host,
|
|
18
|
-
port: config.port ? config.port : 3306,
|
|
19
|
-
user: config.login,
|
|
20
|
-
password: config.password,
|
|
21
|
-
database: config.db
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
mysql_client.connect((err:MysqlError, res) => {
|
|
25
|
-
if (err) {
|
|
26
|
-
dbg(`mysql connection has failed to ${config.host}@${config.db}`, err)
|
|
27
|
-
return reject(err)
|
|
28
|
-
}
|
|
29
|
-
dbg(`mysql connection has been established to ${config.host}@${config.db}`)
|
|
30
|
-
// install a pinger every 30 seconds to keep the connection alive
|
|
31
|
-
if (pingInterval > 0)
|
|
32
|
-
setInterval(() => {
|
|
33
|
-
mysql_client.ping(function (err:MysqlError) {
|
|
34
|
-
if (err) return console.log(`could not ping mysql ${config.host}@${config.db}`, err.code)
|
|
35
|
-
dbg(`mysql ${config.host}@${config.db} is pinging properly`)
|
|
36
|
-
})
|
|
37
|
-
}, pingInterval)
|
|
38
|
-
resolve(mysql_client)
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
}
|
package/ts/funcs/getMysql2.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import * as mysql2 from "mysql2"
|
|
2
|
-
import { QueryError,Connection } from "mysql2"
|
|
3
|
-
import debug from "debug"
|
|
4
|
-
const dbg = debug("getMysql2")
|
|
5
|
-
|
|
6
|
-
export type Mysql2ServerConfiguration = {
|
|
7
|
-
host: string,
|
|
8
|
-
port: number,
|
|
9
|
-
login: string,
|
|
10
|
-
password: string,
|
|
11
|
-
db: string
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export async function getMysql2(config: Mysql2ServerConfiguration, pingInterval: number = 0):Promise<Connection> {
|
|
15
|
-
return new Promise((resolve, reject) => {
|
|
16
|
-
const mysql_client = mysql2.createConnection({
|
|
17
|
-
host: config.host,
|
|
18
|
-
port: config.port ? config.port : 3306,
|
|
19
|
-
user: config.login,
|
|
20
|
-
password: config.password,
|
|
21
|
-
database: config.db
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
mysql_client.connect((err: QueryError | null) => {
|
|
25
|
-
if (err) {
|
|
26
|
-
dbg(`mysql connection has failed to ${config.host}@${config.db}`, err)
|
|
27
|
-
return reject(err)
|
|
28
|
-
}
|
|
29
|
-
dbg(`mysql connection has been established to ${config.host}@${config.db}`)
|
|
30
|
-
// install a pinger every 30 seconds to keep the connection alive
|
|
31
|
-
if (pingInterval > 0)
|
|
32
|
-
setInterval(() => {
|
|
33
|
-
mysql_client.ping(function (err:QueryError | null) {
|
|
34
|
-
if (err)
|
|
35
|
-
return console.log(`could not ping mysql ${config.host}@${config.db}`, err.code)
|
|
36
|
-
dbg(`mysql ${config.host}@${config.db} is pinging properly`)
|
|
37
|
-
})
|
|
38
|
-
}, pingInterval)
|
|
39
|
-
resolve(mysql_client)
|
|
40
|
-
})
|
|
41
|
-
})
|
|
42
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { getWeekNumber } from './getWeekNumber'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
describe('getWeekNumber', () => {
|
|
5
|
-
it('should return the correct week number', () => {
|
|
6
|
-
expect(getWeekNumber(new Date('2012-01-01'))).toBe(52)
|
|
7
|
-
expect(getWeekNumber(new Date('2014-12-30'))).toBe(1)
|
|
8
|
-
})
|
|
9
|
-
})
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// credit: https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
|
|
2
|
-
/* For a given date, get the ISO week number
|
|
3
|
-
|
|
4
|
-
Based on information at:
|
|
5
|
-
|
|
6
|
-
THIS PAGE (DOMAIN EVEN) DOESN'T EXIST ANYMORE UNFORTUNATELY
|
|
7
|
-
http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
|
|
8
|
-
|
|
9
|
-
Algorithm is to find nearest thursday, it's year
|
|
10
|
-
is the year of the week number. Then get weeks
|
|
11
|
-
between that date and the first day of that year.
|
|
12
|
-
|
|
13
|
-
Note that dates in one year can be weeks of previous
|
|
14
|
-
or next year, overlap is up to 3 days.
|
|
15
|
-
|
|
16
|
-
e.g. 2014/12/29 is Monday in week 1 of 2015
|
|
17
|
-
2012/1/1 is Sunday in week 52 of 2011
|
|
18
|
-
*/
|
|
19
|
-
export function getWeekNumber(d: Date = new Date()): number {
|
|
20
|
-
// Copy date so don't modify original
|
|
21
|
-
const date = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
|
|
22
|
-
// Set to nearest Thursday: current date + 4 - current day number
|
|
23
|
-
// Make Sunday's day number 7
|
|
24
|
-
date.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7));
|
|
25
|
-
// Get first day of year
|
|
26
|
-
const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
|
|
27
|
-
// Calculate full weeks to nearest Thursday
|
|
28
|
-
const weekNo = Math.ceil(((date.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
|
|
29
|
-
// Return the week number
|
|
30
|
-
return weekNo;
|
|
31
|
-
}
|
package/ts/funcs/hash_sha512.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as mysql2 from "mysql2"
|
|
2
|
-
import { Pool } from "mysql2"
|
|
3
|
-
import debug from "debug"
|
|
4
|
-
import { Mysql2ServerConfiguration } from "./getMysql2"
|
|
5
|
-
const dbg = debug("initMysql2Pool")
|
|
6
|
-
|
|
7
|
-
export function initMysql2Pool(sqlServer: Mysql2ServerConfiguration): Pool {
|
|
8
|
-
const connectionUri = `mysql://${sqlServer.login}:${sqlServer.password}@${sqlServer.host}`
|
|
9
|
-
dbg(`creating mysql pool to ${sqlServer.host}@${sqlServer.db} as connectionUri=${connectionUri}`)
|
|
10
|
-
return mysql2.createPool(connectionUri)
|
|
11
|
-
}
|
package/ts/funcs/int2ip.test.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
const { int2ip } =require('./int2ip')
|
|
2
|
-
|
|
3
|
-
describe('int2ip', () => {
|
|
4
|
-
it('should convert integer to valid IPv4 address', () => {
|
|
5
|
-
const ipInt = 3232235521; // equivalent to 192.168.0.1
|
|
6
|
-
const result = int2ip(ipInt);
|
|
7
|
-
expect(result).toBe('192.168.0.1');
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('should throw an error for non-numeric input', () => {
|
|
11
|
-
const ipInt = '3232235521'; // string input instead of number
|
|
12
|
-
expect(() => int2ip(ipInt)).toThrow('require number as input');
|
|
13
|
-
expect(() => int2ip([])).toThrow('require number as input');
|
|
14
|
-
expect(() => int2ip(null)).toThrow('require number as input');
|
|
15
|
-
expect(() => int2ip({})).toThrow('require number as input');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('overflowing is allowed', () => {
|
|
19
|
-
const ipInt = 4294967297;
|
|
20
|
-
let ip = int2ip(ipInt)
|
|
21
|
-
expect(ip).toBe('0.0.0.1')
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should support 0', () => {
|
|
25
|
-
const ipInt = 0;
|
|
26
|
-
expect(int2ip(ipInt)).toBe('0.0.0.0')
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('negative numbers should throw', () => {
|
|
30
|
-
expect(() => int2ip(-1)).toThrow('require number as input');
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
});
|
package/ts/funcs/int2ip.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import net from 'net'
|
|
2
|
-
|
|
3
|
-
//we only support ipv4
|
|
4
|
-
export function int2ip (ipInt: number) : string {
|
|
5
|
-
if (typeof ipInt !== 'number' || ipInt<0) throw new Error(`require number as input`)
|
|
6
|
-
const ip4 = ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) )
|
|
7
|
-
if (net.isIP(ip4) !== 4) throw new Error(`Invalid IP: ${ip4}`)
|
|
8
|
-
return ip4
|
|
9
|
-
}
|
package/ts/funcs/ip2int.test.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const { ip2int } = require('./ip2int');
|
|
2
|
-
const { randomIP } = require('./randomIP');
|
|
3
|
-
|
|
4
|
-
describe('ip2int', () => {
|
|
5
|
-
it('should convert IP address to integer', () => {
|
|
6
|
-
const ipAddress = '192.168.0.1';
|
|
7
|
-
const result = ip2int(ipAddress);
|
|
8
|
-
expect(result).toBe(3232235521);
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
it('invalid inputs', () => {
|
|
12
|
-
expect(() => ip2int('')).toThrow()
|
|
13
|
-
expect(() => ip2int('192.168.0')).toThrow()
|
|
14
|
-
expect(() => ip2int(null)).toThrow()
|
|
15
|
-
expect(() => ip2int(undefined)).toThrow()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('should not support ipv6', () => {
|
|
19
|
-
const ipAddress = '::';
|
|
20
|
-
expect(() => ip2int(ipAddress)).toThrow()
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
it('test 100 random ips', () => {
|
|
24
|
-
for (let i = 0; i < 100; i++) {
|
|
25
|
-
const ipAddress = randomIP();
|
|
26
|
-
const result = ip2int(ipAddress);
|
|
27
|
-
expect(result).toBe(ip2int(ipAddress));
|
|
28
|
-
}
|
|
29
|
-
})
|
|
30
|
-
});
|
package/ts/funcs/ip2int.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import net from 'net'
|
|
2
|
-
// function int2ip (ipInt) {
|
|
3
|
-
// return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
|
|
4
|
-
// }
|
|
5
|
-
|
|
6
|
-
// only supports ipv4
|
|
7
|
-
export function ip2int(ip:string) {
|
|
8
|
-
if (net.isIP(ip) !== 4) throw new Error(`Invalid IP: ${ip}`)
|
|
9
|
-
return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
|
|
10
|
-
}
|
package/ts/funcs/isLANIp.d.ts
DELETED
package/ts/funcs/isLANIp.test.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { isLANIp } from './isLANIp';
|
|
2
|
-
|
|
3
|
-
describe('isLANIp', () => {
|
|
4
|
-
it('should return true for LAN IP addresses', () => {
|
|
5
|
-
expect(isLANIp('192.168.0.1')).toBe(true);
|
|
6
|
-
expect(isLANIp('10.0.0.1')).toBe(true);
|
|
7
|
-
expect(isLANIp('172.16.0.1')).toBe(true);
|
|
8
|
-
|
|
9
|
-
// Add more test cases for LAN IP addresses
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should return null for non-LAN IP addresses', () => {
|
|
13
|
-
expect(isLANIp('8.8.8.8')).toBeNull();
|
|
14
|
-
expect(isLANIp('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBeNull();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('should return false for invalid IP addresses', () => {
|
|
18
|
-
expect(isLANIp('')).toBe(false);
|
|
19
|
-
expect(isLANIp('192.168.0.')).toBe(false);
|
|
20
|
-
expect(isLANIp('256.0.0.1')).toBe(false);
|
|
21
|
-
// Add more test cases for invalid IP addresses
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('should return true for private IPv4', () => {
|
|
25
|
-
const ip = '192.168.1.1';
|
|
26
|
-
expect(isLANIp(ip)).toBe(true);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test('should return true for private IPv6', () => {
|
|
30
|
-
const ip = 'fd00::';
|
|
31
|
-
expect(isLANIp(ip)).toBe(true);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('should return null for public IPv4', () => {
|
|
35
|
-
const ip = '8.8.8.8';
|
|
36
|
-
expect(isLANIp(ip)).toBeNull();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('should return null for public IPv6', () => {
|
|
40
|
-
const ip = '2001:4860:4860::8888';
|
|
41
|
-
expect(isLANIp(ip)).toBeNull();
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('should return false for invalid IP', () => {
|
|
45
|
-
const ip = 'invalid';
|
|
46
|
-
expect(isLANIp(ip)).toBe(false);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test('should return false for invalid IP', () => {
|
|
50
|
-
const ip = '1.2.3';
|
|
51
|
-
expect(isLANIp(ip)).toBe(false);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('should return null for non private IPv6', () => {
|
|
55
|
-
const ip = '::2';
|
|
56
|
-
expect(isLANIp(ip)).toBeNull();
|
|
57
|
-
});
|
|
58
|
-
});
|
package/ts/funcs/isLANIp.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import ip6addr from '@7c/node-ip6addr'
|
|
2
|
-
import net from 'net'
|
|
3
|
-
|
|
4
|
-
export const private_network_cidrs = [
|
|
5
|
-
ip6addr.createCIDR('10.0.0.0/8'),
|
|
6
|
-
ip6addr.createCIDR('172.16.0.0/12'),
|
|
7
|
-
ip6addr.createCIDR('192.168.0.0/16'),
|
|
8
|
-
ip6addr.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
|
|
9
|
-
ip6addr.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
|
|
10
|
-
]
|
|
11
|
-
|
|
12
|
-
function normalizeIp(ip: string): string | false {
|
|
13
|
-
if (net.isIP(ip) === 0) return false
|
|
14
|
-
if (net.isIPv6(ip)) return ip.toLowerCase().replace(/^::ffff:/, '')
|
|
15
|
-
return ip
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function isLANIp(ip: string): boolean | null {
|
|
19
|
-
const n = normalizeIp(ip)
|
|
20
|
-
if (n === false || net.isIP(n) === 0) return false
|
|
21
|
-
try {
|
|
22
|
-
const ipVersion = net.isIPv4(n) ? 'ipv4' : 'ipv6'
|
|
23
|
-
for (const cidr of private_network_cidrs) {
|
|
24
|
-
if (!cidr) continue
|
|
25
|
-
const first = cidr.first()
|
|
26
|
-
if (first.kind() !== ipVersion) continue
|
|
27
|
-
if (cidr.contains(n)) return true
|
|
28
|
-
}
|
|
29
|
-
return null
|
|
30
|
-
} catch (err) {
|
|
31
|
-
console.log(err)
|
|
32
|
-
}
|
|
33
|
-
return false
|
|
34
|
-
}
|
package/ts/funcs/isLocal.test.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { isLocal } from './isLocal';
|
|
2
|
-
import os from 'os';
|
|
3
|
-
|
|
4
|
-
describe('isLocal', () => {
|
|
5
|
-
it('should return true on Darwin OS', () => {
|
|
6
|
-
jest.spyOn(os, 'type').mockReturnValue('Darwin');
|
|
7
|
-
const result = isLocal();
|
|
8
|
-
expect(result).toBe(true);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('should return false on non-Darwin OS', () => {
|
|
12
|
-
jest.spyOn(os, 'type').mockReturnValue('Linux');
|
|
13
|
-
const result = isLocal();
|
|
14
|
-
expect(result).toBe(false);
|
|
15
|
-
});
|
|
16
|
-
});
|
package/ts/funcs/isLocal.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { randomIP } from './randomIP';
|
|
2
|
-
import { isLoopbackIP } from './isLoopbackIP';
|
|
3
|
-
|
|
4
|
-
describe('isLoopbackIP', () => {
|
|
5
|
-
test('should return true for loopback IPv4 address', () => {
|
|
6
|
-
expect(isLoopbackIP('127.0.0.1')).toBe(true);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
test('should return true for loopback IPv6 address', () => {
|
|
10
|
-
expect(isLoopbackIP('::1')).toBe(true);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('should return null for non-loopback IPv4 address', () => {
|
|
14
|
-
expect(isLoopbackIP('192.168.0.1')).toBeNull();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('should return null for non-loopback IPv6 address', () => {
|
|
18
|
-
expect(isLoopbackIP('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBeNull();
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('should return false for invalid IP address', () => {
|
|
22
|
-
expect(isLoopbackIP('invalid')).toBe(false);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test('should return true for local IPv4', () => {
|
|
26
|
-
const ip = '127.0.0.1'
|
|
27
|
-
expect(isLoopbackIP(ip)).toBe(true)
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
test('should return true for local IPv4', () => {
|
|
31
|
-
const ip = '127.2.0.1'
|
|
32
|
-
expect(isLoopbackIP(ip)).toBe(true)
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
test('should return true for local IPv4', () => {
|
|
36
|
-
const ip = '127.255.0.1'
|
|
37
|
-
expect(isLoopbackIP(ip)).toBe(true)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test('should return true for local IPv4', () => {
|
|
41
|
-
const ip = '127.127.127.127'
|
|
42
|
-
expect(isLoopbackIP(ip)).toBe(true)
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
test('should return true for local IPv6', () => {
|
|
46
|
-
const ip = '::1'
|
|
47
|
-
expect(isLoopbackIP(ip)).toBe(true)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test('should return null for non-local IPv4', () => {
|
|
51
|
-
const ip = '8.8.8.8'
|
|
52
|
-
expect(isLoopbackIP(ip)).toBeNull()
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('should return null for non-local IPv6', () => {
|
|
56
|
-
const ip = '2001:4860:4860::8888'
|
|
57
|
-
expect(isLoopbackIP(ip)).toBeNull()
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('should return false for invalid IP', () => {
|
|
61
|
-
const ip = 'invalid'
|
|
62
|
-
expect(isLoopbackIP(ip)).toBe(false)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
test('random ips should not be Loopback', () => {
|
|
66
|
-
for(let i=0;i<10;i++){
|
|
67
|
-
let ip = randomIP()
|
|
68
|
-
expect(isLoopbackIP(ip)).toBeNull()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
})
|
|
72
|
-
});
|
package/ts/funcs/isLoopbackIP.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import ip6addr from '@7c/node-ip6addr'
|
|
2
|
-
import net from 'net'
|
|
3
|
-
|
|
4
|
-
export const local_network_cidrs = [
|
|
5
|
-
ip6addr.createCIDR('127.0.0.0/8'),
|
|
6
|
-
ip6addr.createCIDR('::1/128'),
|
|
7
|
-
]
|
|
8
|
-
|
|
9
|
-
function normalizeIp(ip: string): string | false {
|
|
10
|
-
if (net.isIP(ip) === 0) return false
|
|
11
|
-
if (net.isIPv6(ip)) return ip.toLowerCase().replace(/^::ffff:/, '')
|
|
12
|
-
return ip
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function isLoopbackIP(ip: string): boolean | null {
|
|
16
|
-
const n = normalizeIp(ip)
|
|
17
|
-
if (n === false || net.isIP(n) === 0) return false
|
|
18
|
-
try {
|
|
19
|
-
const ipVersion = net.isIPv4(n) ? 'ipv4' : 'ipv6'
|
|
20
|
-
for (const cidr of local_network_cidrs) {
|
|
21
|
-
const first = cidr.first()
|
|
22
|
-
if (first.kind() !== ipVersion) continue
|
|
23
|
-
if (cidr.contains(n)) return true
|
|
24
|
-
}
|
|
25
|
-
return null
|
|
26
|
-
} catch (err) {
|
|
27
|
-
console.log(err)
|
|
28
|
-
}
|
|
29
|
-
return false
|
|
30
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import debug from "debug"
|
|
2
|
-
import knex from "knex"
|
|
3
|
-
const dbg = debug('_knexConnection')
|
|
4
|
-
|
|
5
|
-
export function knexConnection(connection:{
|
|
6
|
-
host: string,
|
|
7
|
-
port: number,
|
|
8
|
-
password: string,
|
|
9
|
-
db: string,
|
|
10
|
-
login: string
|
|
11
|
-
}, poolConfiguration:knex.Knex.PoolConfig= {
|
|
12
|
-
min: 1,
|
|
13
|
-
max: 10
|
|
14
|
-
}, acquireConnectionTimeout:number=10000, debug: boolean = false) {
|
|
15
|
-
return knex({
|
|
16
|
-
client: 'mysql2',
|
|
17
|
-
debug,
|
|
18
|
-
connection:{
|
|
19
|
-
host: connection.host,
|
|
20
|
-
port: connection.port,
|
|
21
|
-
password: connection.password,
|
|
22
|
-
database: connection.db,
|
|
23
|
-
user: connection.login
|
|
24
|
-
},
|
|
25
|
-
pool: poolConfiguration,
|
|
26
|
-
acquireConnectionTimeout,
|
|
27
|
-
log: {
|
|
28
|
-
warn(message: string) {
|
|
29
|
-
dbg('knex warn', message)
|
|
30
|
-
},
|
|
31
|
-
error(message: string) {
|
|
32
|
-
dbg('knex error', message)
|
|
33
|
-
},
|
|
34
|
-
deprecate(message: string) {
|
|
35
|
-
dbg('knex deprecate', message)
|
|
36
|
-
},
|
|
37
|
-
debug(message: string) {
|
|
38
|
-
if (debug) console.log('knex debug', message)
|
|
39
|
-
},
|
|
40
|
-
}
|
|
41
|
-
} as knex.Knex.Config)
|
|
42
|
-
}
|