mybase 1.1.12 → 1.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/mybase.js +3 -5
- package/package.json +3 -2
- package/ts/funcs/randomTCPPort.js +47 -31
- package/ts/funcs/randomTCPPort.ts +43 -28
- package/ts/funcs/validEmail.d.ts +1 -0
- package/ts/funcs/validEmail.js +20 -0
- package/ts/funcs/validEmail.test.js +31 -0
- package/ts/funcs/validEmail.ts +13 -0
- package/ts/index.d.ts +1 -0
- package/ts/index.js +1 -0
- package/ts/index.ts +1 -0
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.
|
|
3
|
+
"version": "1.1.14",
|
|
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",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"@types/debug": "^4.1.12",
|
|
28
28
|
"@types/jest": "^29.5.11",
|
|
29
29
|
"@types/node": "^20.11.5",
|
|
30
|
+
"@types/validator": "^13.11.8",
|
|
30
31
|
"chai": "^4.2.0",
|
|
31
32
|
"jest": "^29.7.0",
|
|
32
33
|
"mocha": "^8.2.1",
|
|
@@ -47,38 +47,54 @@ const net = __importStar(require("net"));
|
|
|
47
47
|
*/
|
|
48
48
|
function randomTCPPort(timeout = 1000, fromPort = 1025, untilPort = 65534, tries = 1000, host = '127.0.0.1') {
|
|
49
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
50
|
+
return new Promise(function (resolve, reject) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
// check if the given port range is valid
|
|
53
|
+
if (fromPort > untilPort)
|
|
54
|
+
return resolve(false);
|
|
55
|
+
// check if the given port range is valid
|
|
56
|
+
if (fromPort < 0 || untilPort < 0)
|
|
57
|
+
return resolve(false);
|
|
58
|
+
// check if the given port range is valid
|
|
59
|
+
if (fromPort > 65535 || untilPort > 65535)
|
|
60
|
+
return resolve(false);
|
|
61
|
+
// 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
|
|
62
|
+
// this port number immediately
|
|
63
|
+
// this function should be used for testing purposes only
|
|
64
|
+
// this function is not suitable for production use
|
|
65
|
+
while (true && tries > 0) {
|
|
66
|
+
tries--;
|
|
67
|
+
let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort);
|
|
68
|
+
let server = net.createServer();
|
|
69
|
+
let checkPort = new Promise((resolveInner, reject) => {
|
|
70
|
+
server.on('error', (err) => {
|
|
71
|
+
// any error should raise problem
|
|
72
|
+
resolveInner(false);
|
|
73
|
+
});
|
|
74
|
+
server.on('listening', () => {
|
|
75
|
+
resolveInner(true);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
server.listen(port, host);
|
|
79
|
+
let result = yield checkPort;
|
|
80
|
+
if (result) {
|
|
81
|
+
let to = setTimeout(() => {
|
|
82
|
+
return resolve(port);
|
|
83
|
+
}, 1000);
|
|
84
|
+
server.on('close', () => {
|
|
85
|
+
clearTimeout(to);
|
|
86
|
+
return resolve(port);
|
|
87
|
+
});
|
|
88
|
+
server.close();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
else
|
|
92
|
+
server.close();
|
|
93
|
+
}
|
|
94
|
+
if (tries === 0)
|
|
95
|
+
return resolve(false);
|
|
75
96
|
});
|
|
76
|
-
|
|
77
|
-
let result = yield checkPort;
|
|
78
|
-
if (result)
|
|
79
|
-
return port;
|
|
80
|
-
}
|
|
81
|
-
return false;
|
|
97
|
+
});
|
|
82
98
|
});
|
|
83
99
|
}
|
|
84
100
|
exports.randomTCPPort = randomTCPPort;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { wait } from './wait'
|
|
1
2
|
import * as net from 'net'
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -11,34 +12,48 @@ import * as net from 'net'
|
|
|
11
12
|
* @param tries the number of tries to find a port number
|
|
12
13
|
* @param host the host to bind to
|
|
13
14
|
*/
|
|
14
|
-
export async function randomTCPPort(timeout:number=1000,fromPort:number=1025,untilPort:number=65534,tries=1000,host='127.0.0.1')
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
export async function randomTCPPort(timeout: number = 1000, fromPort: number = 1025, untilPort: number = 65534, tries = 1000, host = '127.0.0.1'): Promise<number | false> {
|
|
16
|
+
return new Promise(async function (resolve, reject) {
|
|
17
|
+
// check if the given port range is valid
|
|
18
|
+
if (fromPort > untilPort) return resolve(false)
|
|
19
|
+
// check if the given port range is valid
|
|
20
|
+
if (fromPort < 0 || untilPort < 0) return resolve(false)
|
|
21
|
+
// check if the given port range is valid
|
|
22
|
+
if (fromPort > 65535 || untilPort > 65535) return resolve(false)
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
// 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
|
|
25
|
+
// this port number immediately
|
|
26
|
+
// this function should be used for testing purposes only
|
|
27
|
+
// this function is not suitable for production use
|
|
28
|
+
while(true && tries > 0) {
|
|
29
|
+
tries--
|
|
30
|
+
let port = Math.floor(Math.random() * (untilPort - fromPort + 1) + fromPort)
|
|
31
|
+
let server = net.createServer()
|
|
32
|
+
let checkPort = new Promise((resolveInner, reject) => {
|
|
33
|
+
server.on('error', (err) => {
|
|
34
|
+
// any error should raise problem
|
|
35
|
+
resolveInner(false)
|
|
36
|
+
})
|
|
37
|
+
server.on('listening', () => {
|
|
38
|
+
resolveInner(true)
|
|
39
|
+
})
|
|
33
40
|
})
|
|
34
|
-
server.
|
|
41
|
+
server.listen(port, host)
|
|
42
|
+
let result = await checkPort
|
|
43
|
+
if (result) {
|
|
44
|
+
let to = setTimeout(() => {
|
|
45
|
+
return resolve(port)
|
|
46
|
+
},1000)
|
|
47
|
+
server.on('close', () => {
|
|
48
|
+
clearTimeout(to)
|
|
49
|
+
return resolve(port)
|
|
50
|
+
})
|
|
35
51
|
server.close()
|
|
36
|
-
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
52
|
+
return
|
|
53
|
+
} else server.close()
|
|
54
|
+
}
|
|
55
|
+
if (tries===0)
|
|
56
|
+
return resolve(false)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
}
|
|
@@ -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
package/ts/index.js
CHANGED
|
@@ -31,3 +31,4 @@ __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);
|