mybase 1.1.34 → 1.1.36

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 (124) hide show
  1. package/jest/isLANIp.test.js +36 -0
  2. package/jest/isLoopbackIP.test.js +31 -0
  3. package/jest.config.js +6 -0
  4. package/package.json +1 -5
  5. package/ts/funcs/Geoip2Paths.d.ts +7 -0
  6. package/ts/funcs/Geoip2Paths.js +26 -0
  7. package/ts/funcs/Geoip2Paths.test.ts +11 -0
  8. package/ts/funcs/Geoip2Paths.ts +26 -0
  9. package/ts/funcs/MaxRuntimeHours.d.ts +1 -0
  10. package/ts/funcs/MaxRuntimeHours.js +11 -0
  11. package/ts/funcs/MaxRuntimeHours.ts +7 -0
  12. package/ts/funcs/asJSON.d.ts +1 -0
  13. package/ts/funcs/asJSON.js +14 -0
  14. package/ts/funcs/asJSON.test.js +72 -0
  15. package/ts/funcs/asJSON.ts +6 -0
  16. package/ts/funcs/ensureFolder.d.ts +3 -0
  17. package/ts/funcs/ensureFolder.js +50 -0
  18. package/ts/funcs/ensureFolder.test.ts +79 -0
  19. package/ts/funcs/ensureFolder.ts +21 -0
  20. package/ts/funcs/fileCacheIsValid.d.ts +1 -0
  21. package/ts/funcs/fileCacheIsValid.js +40 -0
  22. package/ts/funcs/fileCacheIsValid.ts +13 -0
  23. package/ts/funcs/getMysql1.d.ts +8 -0
  24. package/ts/funcs/getMysql1.js +62 -0
  25. package/ts/funcs/getMysql1.ts +41 -0
  26. package/ts/funcs/getMysql2.d.ts +9 -0
  27. package/ts/funcs/getMysql2.js +62 -0
  28. package/ts/funcs/getMysql2.ts +42 -0
  29. package/ts/funcs/getWeekNumber.d.ts +1 -0
  30. package/ts/funcs/getWeekNumber.js +36 -0
  31. package/ts/funcs/getWeekNumber.test.ts +9 -0
  32. package/ts/funcs/getWeekNumber.ts +31 -0
  33. package/ts/funcs/hash_sha512.d.ts +1 -0
  34. package/ts/funcs/hash_sha512.js +11 -0
  35. package/ts/funcs/hash_sha512.ts +7 -0
  36. package/ts/funcs/initMysql2Pool.d.ts +3 -0
  37. package/ts/funcs/initMysql2Pool.js +39 -0
  38. package/ts/funcs/initMysql2Pool.ts +11 -0
  39. package/ts/funcs/int2ip.d.ts +1 -0
  40. package/ts/funcs/int2ip.js +18 -0
  41. package/ts/funcs/int2ip.test.js +33 -0
  42. package/ts/funcs/int2ip.ts +9 -0
  43. package/ts/funcs/ip2int.d.ts +1 -0
  44. package/ts/funcs/ip2int.js +18 -0
  45. package/ts/funcs/ip2int.test.js +30 -0
  46. package/ts/funcs/ip2int.ts +10 -0
  47. package/ts/funcs/isLANIp.d.ts +4 -0
  48. package/ts/funcs/isLANIp.js +47 -0
  49. package/ts/funcs/isLANIp.test.ts +59 -0
  50. package/ts/funcs/isLANIp.ts +37 -0
  51. package/ts/funcs/isLocal.d.ts +1 -0
  52. package/ts/funcs/isLocal.js +12 -0
  53. package/ts/funcs/isLocal.test.ts +16 -0
  54. package/ts/funcs/isLocal.ts +5 -0
  55. package/ts/funcs/isLoopbackIP.d.ts +4 -0
  56. package/ts/funcs/isLoopbackIP.js +42 -0
  57. package/ts/funcs/isLoopbackIP.test.ts +72 -0
  58. package/ts/funcs/isLoopbackIP.ts +32 -0
  59. package/ts/funcs/promiseTimeout.d.ts +1 -0
  60. package/ts/funcs/promiseTimeout.js +19 -0
  61. package/ts/funcs/promiseTimeout.test.js +104 -0
  62. package/ts/funcs/promiseTimeout.ts +19 -0
  63. package/ts/funcs/randomIP.d.ts +1 -0
  64. package/ts/funcs/randomIP.js +11 -0
  65. package/ts/funcs/randomIP.test.ts +16 -0
  66. package/ts/funcs/randomIP.ts +10 -0
  67. package/ts/funcs/randomIP6.d.ts +1 -0
  68. package/ts/funcs/randomIP6.js +17 -0
  69. package/ts/funcs/randomIP6.test.js +18 -0
  70. package/ts/funcs/randomIP6.ts +12 -0
  71. package/ts/funcs/randomString.d.ts +1 -0
  72. package/ts/funcs/randomString.js +8 -0
  73. package/ts/funcs/randomString.test.ts +15 -0
  74. package/ts/funcs/randomString.ts +5 -0
  75. package/ts/funcs/randomTCPPort.d.ts +12 -0
  76. package/ts/funcs/randomTCPPort.js +88 -0
  77. package/ts/funcs/randomTCPPort.test.ts +62 -0
  78. package/ts/funcs/randomTCPPort.ts +58 -0
  79. package/ts/funcs/randomUTFString.d.ts +1 -0
  80. package/ts/funcs/randomUTFString.js +31 -0
  81. package/ts/funcs/randomUTFString.test.ts +44 -0
  82. package/ts/funcs/randomUTFString.ts +33 -0
  83. package/ts/funcs/utcnow.d.ts +2 -0
  84. package/ts/funcs/utcnow.js +8 -0
  85. package/ts/funcs/utcnow.ts +5 -0
  86. package/ts/funcs/validEmail.d.ts +1 -0
  87. package/ts/funcs/validEmail.js +21 -0
  88. package/ts/funcs/validEmail.test.js +31 -0
  89. package/ts/funcs/validEmail.ts +13 -0
  90. package/ts/funcs/validIp.d.ts +1 -0
  91. package/ts/funcs/validIp.js +32 -0
  92. package/ts/funcs/validIp.test.ts +33 -0
  93. package/ts/funcs/validIp.ts +5 -0
  94. package/ts/funcs/vaultFill.d.ts +2 -0
  95. package/ts/funcs/vaultFill.js +86 -0
  96. package/ts/funcs/vaultFill.ts +89 -0
  97. package/ts/funcs/vaultRead.d.ts +2 -0
  98. package/ts/funcs/vaultRead.js +52 -0
  99. package/ts/funcs/vaultRead.ts +45 -0
  100. package/ts/funcs/wait.d.ts +1 -0
  101. package/ts/funcs/wait.js +10 -0
  102. package/ts/funcs/wait.ts +7 -0
  103. package/ts/global.d.ts +1 -0
  104. package/ts/global.js +16 -0
  105. package/ts/global.ts +10 -0
  106. package/ts/index.d.ts +30 -0
  107. package/ts/index.js +48 -0
  108. package/ts/index.test.ts +7 -0
  109. package/ts/index.ts +33 -0
  110. package/ts/models/IPAddress.d.ts +40 -0
  111. package/ts/models/IPAddress.js +255 -0
  112. package/ts/models/IPAddress.test.ts +393 -0
  113. package/ts/models/IPAddress.ts +272 -0
  114. package/ts/models/Timespan.d.ts +18 -0
  115. package/ts/models/Timespan.js +52 -0
  116. package/ts/models/Timespan.ts +63 -0
  117. package/ts/models/Unixtime.d.ts +32 -0
  118. package/ts/models/Unixtime.js +137 -0
  119. package/ts/models/Unixtime.test.ts +102 -0
  120. package/ts/models/Unixtime.ts +164 -0
  121. package/ts/types.d.ts +1 -0
  122. package/ts/types.js +3 -0
  123. package/ts/types.ts +1 -0
  124. package/tsconfig.json +36 -0
@@ -0,0 +1,62 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.getMysql2 = void 0;
30
+ const mysql2 = __importStar(require("mysql2"));
31
+ const debug_1 = __importDefault(require("debug"));
32
+ const dbg = (0, debug_1.default)("getMysql2");
33
+ async function getMysql2(config, pingInterval = 0) {
34
+ return new Promise((resolve, reject) => {
35
+ const mysql_client = mysql2.createConnection({
36
+ host: config.host,
37
+ port: config.port ? config.port : 3306,
38
+ user: config.login,
39
+ password: config.password,
40
+ database: config.db
41
+ });
42
+ mysql_client.connect((err) => {
43
+ if (err) {
44
+ dbg(`mysql connection has failed to ${config.host}@${config.db}`, err);
45
+ return reject(err);
46
+ }
47
+ dbg(`mysql connection has been established to ${config.host}@${config.db}`);
48
+ // install a pinger every 30 seconds to keep the connection alive
49
+ if (pingInterval > 0)
50
+ setInterval(() => {
51
+ mysql_client.ping(function (err) {
52
+ if (err)
53
+ return console.log(`could not ping mysql ${config.host}@${config.db}`, err.code);
54
+ dbg(`mysql ${config.host}@${config.db} is pinging properly`);
55
+ });
56
+ }, pingInterval);
57
+ resolve(mysql_client);
58
+ });
59
+ });
60
+ }
61
+ exports.getMysql2 = getMysql2;
62
+ //# sourceMappingURL=getMysql2.js.map
@@ -0,0 +1,42 @@
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
+ }
@@ -0,0 +1 @@
1
+ export declare function getWeekNumber(d?: Date): number;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getWeekNumber = void 0;
4
+ // credit: https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
5
+ /* For a given date, get the ISO week number
6
+
7
+ Based on information at:
8
+
9
+ THIS PAGE (DOMAIN EVEN) DOESN'T EXIST ANYMORE UNFORTUNATELY
10
+ http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
11
+
12
+ Algorithm is to find nearest thursday, it's year
13
+ is the year of the week number. Then get weeks
14
+ between that date and the first day of that year.
15
+
16
+ Note that dates in one year can be weeks of previous
17
+ or next year, overlap is up to 3 days.
18
+
19
+ e.g. 2014/12/29 is Monday in week 1 of 2015
20
+ 2012/1/1 is Sunday in week 52 of 2011
21
+ */
22
+ function getWeekNumber(d = new Date()) {
23
+ // Copy date so don't modify original
24
+ const date = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
25
+ // Set to nearest Thursday: current date + 4 - current day number
26
+ // Make Sunday's day number 7
27
+ date.setUTCDate(date.getUTCDate() + 4 - (date.getUTCDay() || 7));
28
+ // Get first day of year
29
+ const yearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
30
+ // Calculate full weeks to nearest Thursday
31
+ const weekNo = Math.ceil(((date.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
32
+ // Return the week number
33
+ return weekNo;
34
+ }
35
+ exports.getWeekNumber = getWeekNumber;
36
+ //# sourceMappingURL=getWeekNumber.js.map
@@ -0,0 +1,9 @@
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
+ })
@@ -0,0 +1,31 @@
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
+ }
@@ -0,0 +1 @@
1
+ export declare function hash_sha512(plainString: string): string;
@@ -0,0 +1,11 @@
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;
11
+ //# sourceMappingURL=hash_sha512.js.map
@@ -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,3 @@
1
+ import { Pool } from "mysql2";
2
+ import { Mysql2ServerConfiguration } from "./getMysql2";
3
+ export declare function initMysql2Pool(sqlServer: Mysql2ServerConfiguration): Pool;
@@ -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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.initMysql2Pool = void 0;
30
+ const mysql2 = __importStar(require("mysql2"));
31
+ const debug_1 = __importDefault(require("debug"));
32
+ const dbg = (0, debug_1.default)("initMysql2Pool");
33
+ function initMysql2Pool(sqlServer) {
34
+ const connectionUri = `mysql://${sqlServer.login}:${sqlServer.password}@${sqlServer.host}`;
35
+ dbg(`creating mysql pool to ${sqlServer.host}@${sqlServer.db} as connectionUri=${connectionUri}`);
36
+ return mysql2.createPool(connectionUri);
37
+ }
38
+ exports.initMysql2Pool = initMysql2Pool;
39
+ //# sourceMappingURL=initMysql2Pool.js.map
@@ -0,0 +1,11 @@
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
+ }
@@ -0,0 +1 @@
1
+ export declare function int2ip(ipInt: number): string;
@@ -0,0 +1,18 @@
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.int2ip = void 0;
7
+ const net_1 = __importDefault(require("net"));
8
+ //we only support ipv4
9
+ function int2ip(ipInt) {
10
+ if (typeof ipInt !== 'number' || ipInt < 0)
11
+ throw new Error(`require number as input`);
12
+ const ip4 = ((ipInt >>> 24) + '.' + (ipInt >> 16 & 255) + '.' + (ipInt >> 8 & 255) + '.' + (ipInt & 255));
13
+ if (net_1.default.isIP(ip4) !== 4)
14
+ throw new Error(`Invalid IP: ${ip4}`);
15
+ return ip4;
16
+ }
17
+ exports.int2ip = int2ip;
18
+ //# sourceMappingURL=int2ip.js.map
@@ -0,0 +1,33 @@
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
+ });
@@ -0,0 +1,9 @@
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
+ }
@@ -0,0 +1 @@
1
+ export declare function ip2int(ip: string): number;
@@ -0,0 +1,18 @@
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.ip2int = void 0;
7
+ const net_1 = __importDefault(require("net"));
8
+ // function int2ip (ipInt) {
9
+ // return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
10
+ // }
11
+ // only supports ipv4
12
+ function ip2int(ip) {
13
+ if (net_1.default.isIP(ip) !== 4)
14
+ throw new Error(`Invalid IP: ${ip}`);
15
+ return ip.split('.').reduce(function (ipInt, octet) { return (ipInt << 8) + parseInt(octet, 10); }, 0) >>> 0;
16
+ }
17
+ exports.ip2int = ip2int;
18
+ //# sourceMappingURL=ip2int.js.map
@@ -0,0 +1,30 @@
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
+ });
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,4 @@
1
+ /// <reference types="@root/ip6addr" />
2
+ import ip6addr from 'ip6addr';
3
+ export declare const private_network_cidrs: ip6addr.CIDR[];
4
+ export declare function isLANIp(ip: string): boolean;
@@ -0,0 +1,47 @@
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.isLANIp = exports.private_network_cidrs = void 0;
7
+ const ip6addr_1 = __importDefault(require("ip6addr"));
8
+ const net_1 = __importDefault(require("net"));
9
+ exports.private_network_cidrs = [
10
+ ip6addr_1.default.createCIDR('10.0.0.0/8'),
11
+ ip6addr_1.default.createCIDR('172.16.0.0/12'),
12
+ ip6addr_1.default.createCIDR('192.168.0.0/16'),
13
+ ip6addr_1.default.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
14
+ ip6addr_1.default.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
15
+ ];
16
+ function isLANIp(ip) {
17
+ ip = normalizeIp(ip);
18
+ if (net_1.default.isIP(ip) === 0)
19
+ return false;
20
+ try {
21
+ // speed optimized
22
+ let ipVersion = net_1.default.isIPv4(ip) ? 'ipv4' : 'ipv6';
23
+ for (let cidr of exports.private_network_cidrs) {
24
+ if (!cidr)
25
+ continue;
26
+ let first = cidr.first();
27
+ if (first.kind() !== ipVersion)
28
+ continue;
29
+ if (cidr.contains(ip))
30
+ return true;
31
+ }
32
+ }
33
+ catch (err) {
34
+ console.log(err);
35
+ }
36
+ return false;
37
+ }
38
+ exports.isLANIp = isLANIp;
39
+ function normalizeIp(ip) {
40
+ // also support ipv6
41
+ if (net_1.default.isIP(ip) === 0)
42
+ return '';
43
+ if (net_1.default.isIPv6(ip))
44
+ return ip.toLowerCase().replace(/^::ffff:/, '');
45
+ return ip;
46
+ }
47
+ //# sourceMappingURL=isLANIp.js.map
@@ -0,0 +1,59 @@
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 false for non-LAN IP addresses', () => {
13
+ expect(isLANIp('8.8.8.8')).toBe(false);
14
+ expect(isLANIp('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(false);
15
+ // Add more test cases for non-LAN IP addresses
16
+ });
17
+
18
+ it('should return false for invalid IP addresses', () => {
19
+ expect(isLANIp('')).toBe(false);
20
+ expect(isLANIp('192.168.0.')).toBe(false);
21
+ expect(isLANIp('256.0.0.1')).toBe(false);
22
+ // Add more test cases for invalid IP addresses
23
+ });
24
+
25
+ test('should return true for private IPv4', () => {
26
+ const ip = '192.168.1.1';
27
+ expect(isLANIp(ip)).toBe(true);
28
+ });
29
+
30
+ test('should return true for private IPv6', () => {
31
+ const ip = 'fd00::';
32
+ expect(isLANIp(ip)).toBe(true);
33
+ });
34
+
35
+ test('should return null for public IPv4', () => {
36
+ const ip = '8.8.8.8';
37
+ expect(isLANIp(ip)).toBe(false);
38
+ });
39
+
40
+ test('should return false for public IPv6', () => {
41
+ const ip = '2001:4860:4860::8888';
42
+ expect(isLANIp(ip)).toBe(false);
43
+ });
44
+
45
+ test('should return false for invalid IP', () => {
46
+ const ip = 'invalid';
47
+ expect(isLANIp(ip)).toBe(false);
48
+ });
49
+
50
+ test('should return false for invalid IP', () => {
51
+ const ip = '1.2.3';
52
+ expect(isLANIp(ip)).toBe(false);
53
+ });
54
+
55
+ test('should return null for non private IPv6', () => {
56
+ const ip = '::2';
57
+ expect(isLANIp(ip)).toBe(false);
58
+ });
59
+ });
@@ -0,0 +1,37 @@
1
+ import ip6addr from '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
+ export function isLANIp(ip:string) : boolean {
13
+ ip=normalizeIp(ip)
14
+ if (net.isIP(ip)===0) return false
15
+ try {
16
+ // speed optimized
17
+ let ipVersion = net.isIPv4(ip) ? 'ipv4' : 'ipv6'
18
+ for (let cidr of private_network_cidrs) {
19
+ if (!cidr) continue
20
+
21
+ let first = cidr.first()
22
+ if (first.kind() !== ipVersion) continue
23
+ if (cidr.contains(ip))
24
+ return true
25
+ }
26
+ } catch (err) {
27
+ console.log(err)
28
+ }
29
+ return false
30
+ }
31
+
32
+ function normalizeIp(ip:string):string {
33
+ // also support ipv6
34
+ if (net.isIP(ip) === 0) return ''
35
+ if (net.isIPv6(ip)) return ip.toLowerCase().replace(/^::ffff:/, '')
36
+ return ip
37
+ }
@@ -0,0 +1 @@
1
+ export declare function isLocal(): boolean;
@@ -0,0 +1,12 @@
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.isLocal = void 0;
7
+ const os_1 = __importDefault(require("os"));
8
+ function isLocal() {
9
+ return (os_1.default.type() === 'Darwin');
10
+ }
11
+ exports.isLocal = isLocal;
12
+ //# sourceMappingURL=isLocal.js.map
@@ -0,0 +1,16 @@
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
+ });
@@ -0,0 +1,5 @@
1
+ import os from 'os'
2
+
3
+ export function isLocal() : boolean {
4
+ return (os.type() === 'Darwin')
5
+ }
@@ -0,0 +1,4 @@
1
+ /// <reference types="@root/ip6addr" />
2
+ import ip6addr from 'ip6addr';
3
+ export declare const local_network_cidrs: ip6addr.CIDR[];
4
+ export declare function isLoopbackIP(ip: string): boolean;
@@ -0,0 +1,42 @@
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.isLoopbackIP = exports.local_network_cidrs = void 0;
7
+ const ip6addr_1 = __importDefault(require("ip6addr"));
8
+ const net_1 = __importDefault(require("net"));
9
+ exports.local_network_cidrs = [
10
+ ip6addr_1.default.createCIDR('127.0.0.0/8'),
11
+ ip6addr_1.default.createCIDR('::1/128')
12
+ ];
13
+ function isLoopbackIP(ip) {
14
+ ip = normalizeIp(ip);
15
+ if (net_1.default.isIP(ip) === 0)
16
+ return false;
17
+ try {
18
+ // speed optimized
19
+ let ipVersion = net_1.default.isIPv4(ip) ? 'ipv4' : 'ipv6';
20
+ for (let cidr of exports.local_network_cidrs) {
21
+ let first = cidr.first();
22
+ if (first.kind() !== ipVersion)
23
+ continue;
24
+ if (cidr.contains(ip))
25
+ return true;
26
+ }
27
+ }
28
+ catch (err) {
29
+ console.log(err);
30
+ }
31
+ return false;
32
+ }
33
+ exports.isLoopbackIP = isLoopbackIP;
34
+ function normalizeIp(ip) {
35
+ // also support ipv6
36
+ if (net_1.default.isIP(ip) === 0)
37
+ return '';
38
+ if (net_1.default.isIPv6(ip))
39
+ return ip.toLowerCase().replace(/^::ffff:/, '');
40
+ return ip;
41
+ }
42
+ //# sourceMappingURL=isLoopbackIP.js.map