mybase 1.1.13 → 1.1.15

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.
@@ -1,32 +1,31 @@
1
- const { isLoopbackIP } = require('./../mybase'); // adjust the path to mybase.js
1
+ const { isLoopbackIP } = require('./../mybase') // adjust the path to mybase.js
2
2
 
3
3
  test('should return true for local IPv4', () => {
4
- const ip = '127.0.0.1';
5
- expect(isLoopbackIP(ip)).toBe(true);
6
- });
4
+ const ip = '127.0.0.1'
5
+ expect(isLoopbackIP(ip)).toBe(true)
6
+ })
7
7
 
8
8
  test('should return true for local IPv4', () => {
9
- const ip = '127.127.127.127';
10
- expect(isLoopbackIP(ip)).toBe(true);
11
- });
12
-
9
+ const ip = '127.127.127.127'
10
+ expect(isLoopbackIP(ip)).toBe(true)
11
+ })
13
12
 
14
13
  test('should return true for local IPv6', () => {
15
- const ip = '::1';
16
- expect(isLoopbackIP(ip)).toBe(true);
17
- });
14
+ const ip = '::1'
15
+ expect(isLoopbackIP(ip)).toBe(true)
16
+ })
18
17
 
19
18
  test('should return null for non-local IPv4', () => {
20
- const ip = '8.8.8.8';
21
- expect(isLoopbackIP(ip)).toBe(null);
22
- });
19
+ const ip = '8.8.8.8'
20
+ expect(isLoopbackIP(ip)).toBe(null)
21
+ })
23
22
 
24
23
  test('should return null for non-local IPv6', () => {
25
- const ip = '2001:4860:4860::8888';
26
- expect(isLoopbackIP(ip)).toBe(null);
27
- });
24
+ const ip = '2001:4860:4860::8888'
25
+ expect(isLoopbackIP(ip)).toBe(null)
26
+ })
28
27
 
29
28
  test('should return false for invalid IP', () => {
30
- const ip = 'invalid';
31
- expect(isLoopbackIP(ip)).toBe(false);
32
- });
29
+ const ip = 'invalid'
30
+ expect(isLoopbackIP(ip)).toBe(false)
31
+ })
package/jest.config.js CHANGED
@@ -2,4 +2,5 @@
2
2
  module.exports = {
3
3
  preset: 'ts-jest',
4
4
  testEnvironment: 'node',
5
+ testTimeout: 15000,
5
6
  };
package/mybase.js CHANGED
@@ -6,7 +6,6 @@ const fs = require('fs')
6
6
  const path = require('path')
7
7
  const net = require('net')
8
8
  const chalk = require('chalk')
9
- const punycode = require('punycode');
10
9
  const _validURL = require('@7c/validurl')
11
10
  const validator = require('validator')
12
11
  const sha512 = require('js-sha512')
@@ -91,9 +90,8 @@ function randomHPassword(length=10) {
91
90
  let plain = randomString(length)
92
91
  return sha512(plain)
93
92
  }
94
- function validEmail_old(email) { return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) }
95
93
 
96
- function validEmail(email) {
94
+ function validEmail(email) { //! ported
97
95
  if (typeof email==='string')
98
96
  return validator.isEmail(email) // validator needs a string
99
97
  return false
@@ -637,11 +635,11 @@ function ensureProperty(target, propsString, defaultValue = false, debug = false
637
635
 
638
636
 
639
637
 
640
- function int2ip (ipInt) {
638
+ function int2ip (ipInt) { // ported into IPAddress
641
639
  return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
642
640
  }
643
641
 
644
- function ip2int(ip) {
642
+ function ip2int(ip) { // ported into IPAddress
645
643
  return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
646
644
  }
647
645
 
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.13",
3
+ "version": "1.1.15",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
7
- "test": "jest"
7
+ "test": "jest --forceExit --verbose"
8
8
  },
9
9
  "author": "",
10
10
  "license": "ISC",
@@ -25,8 +25,10 @@
25
25
  "devDependencies": {
26
26
  "@jest/globals": "^29.7.0",
27
27
  "@types/debug": "^4.1.12",
28
+ "@types/ip6addr": "^0.2.6",
28
29
  "@types/jest": "^29.5.11",
29
30
  "@types/node": "^20.11.5",
31
+ "@types/validator": "^13.11.8",
30
32
  "chai": "^4.2.0",
31
33
  "jest": "^29.7.0",
32
34
  "mocha": "^8.2.1",
@@ -0,0 +1 @@
1
+ export declare function isLoopbackIP(ip: string): boolean;
@@ -0,0 +1,40 @@
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 = void 0;
7
+ const ip6addr_1 = __importDefault(require("ip6addr"));
8
+ const net_1 = __importDefault(require("net"));
9
+ const local_network_cidrs = [ip6addr_1.default.createCIDR('127.0.0.0/8'),
10
+ ip6addr_1.default.createCIDR('::1/128')];
11
+ function normalizeIp(ip) {
12
+ // also support ipv6
13
+ if (net_1.default.isIP(ip) === 0)
14
+ return '';
15
+ if (net_1.default.isIPv6(ip))
16
+ return ip.toLowerCase().replace(/^::ffff:/, '');
17
+ return ip;
18
+ }
19
+ function isLoopbackIP(ip) {
20
+ ip = normalizeIp(ip);
21
+ if (net_1.default.isIP(ip) === 0)
22
+ return false;
23
+ try {
24
+ // speed optimized
25
+ let ipVersion = net_1.default.isIPv4(ip) ? 'ipv4' : 'ipv6';
26
+ for (let cidr of local_network_cidrs) {
27
+ let first = cidr.first();
28
+ if (first.kind() !== ipVersion)
29
+ continue;
30
+ if (cidr.contains(ip))
31
+ return true;
32
+ }
33
+ return false;
34
+ }
35
+ catch (err) {
36
+ console.log(err);
37
+ }
38
+ return false;
39
+ }
40
+ exports.isLoopbackIP = isLoopbackIP;
@@ -0,0 +1,69 @@
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 false for non-loopback IPv4 address', () => {
14
+ expect(isLoopbackIP('192.168.0.1')).toBe(false);
15
+ });
16
+
17
+ test('should return false for non-loopback IPv6 address', () => {
18
+ expect(isLoopbackIP('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(false);
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)).toBe(false)
53
+ })
54
+
55
+ test('should return null for non-local IPv6', () => {
56
+ const ip = '2001:4860:4860::8888'
57
+ expect(isLoopbackIP(ip)).toBe(false)
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<100;i++)
67
+ expect(isLoopbackIP(randomIP())).toBe(false)
68
+ })
69
+ });
@@ -0,0 +1,33 @@
1
+ import ip6addr from 'ip6addr'
2
+ import net from 'net'
3
+
4
+ const local_network_cidrs = [ip6addr.createCIDR('127.0.0.0/8')
5
+ ,ip6addr.createCIDR('::1/128')]
6
+
7
+
8
+ function normalizeIp(ip:string):string {
9
+ // also support ipv6
10
+ if (net.isIP(ip) === 0) return ''
11
+ if (net.isIPv6(ip)) return ip.toLowerCase().replace(/^::ffff:/, '')
12
+ return ip
13
+ }
14
+
15
+
16
+ export function isLoopbackIP(ip:string) : boolean {
17
+ ip=normalizeIp(ip)
18
+ if (net.isIP(ip) === 0) return false
19
+ try {
20
+ // speed optimized
21
+ let ipVersion = net.isIPv4(ip) ? 'ipv4' : 'ipv6'
22
+ for (let cidr of local_network_cidrs) {
23
+ let first = cidr.first()
24
+ if (first.kind() !== ipVersion) continue
25
+ if (cidr.contains(ip))
26
+ return true
27
+ }
28
+ return false
29
+ } catch (err) {
30
+ console.log(err)
31
+ }
32
+ return false
33
+ }
@@ -59,4 +59,4 @@ describe("randomTCPPort", () => {
59
59
  expect(port).toBe(false);
60
60
  });
61
61
 
62
- });
62
+ })
@@ -0,0 +1 @@
1
+ export declare function validEmail(email: string): boolean;
@@ -0,0 +1,20 @@
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.validEmail = void 0;
7
+ const validator_1 = __importDefault(require("validator"));
8
+ function validEmail(email) {
9
+ if (typeof email === 'string')
10
+ return validator_1.default.isEmail(email); // validator needs a string
11
+ return false;
12
+ // giving up old style, was not reliable
13
+ // taken from https://www.w3resource.com/javascript/form/email-validation.php
14
+ // strange looking emails might be indeed valid
15
+ // check https://www.w3resource.com/javascript/form/example-javascript-form-validation-email-REC-2822.html
16
+ if (typeof email === 'string')
17
+ email = email.toLowerCase().trim();
18
+ 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);
19
+ }
20
+ exports.validEmail = validEmail;
@@ -0,0 +1,31 @@
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
+ })
@@ -0,0 +1,13 @@
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/index.d.ts CHANGED
@@ -15,3 +15,5 @@ export * from "./funcs/ip2int";
15
15
  export * from "./funcs/int2ip";
16
16
  export * from "./funcs/randomIP6";
17
17
  export * from "./funcs/randomTCPPort";
18
+ export * from "./funcs/validEmail";
19
+ export * from "./funcs/isLoopbackIP";
package/ts/index.js CHANGED
@@ -31,3 +31,5 @@ __exportStar(require("./funcs/ip2int"), exports);
31
31
  __exportStar(require("./funcs/int2ip"), exports);
32
32
  __exportStar(require("./funcs/randomIP6"), exports);
33
33
  __exportStar(require("./funcs/randomTCPPort"), exports);
34
+ __exportStar(require("./funcs/validEmail"), exports);
35
+ __exportStar(require("./funcs/isLoopbackIP"), exports);
package/ts/index.ts CHANGED
@@ -15,4 +15,6 @@ export * from "./funcs/ip2int"
15
15
  export * from "./funcs/int2ip"
16
16
  export * from "./funcs/randomIP6"
17
17
  export * from "./funcs/randomTCPPort"
18
+ export * from "./funcs/validEmail"
19
+ export * from "./funcs/isLoopbackIP"
18
20