This repository has been archived by the owner on Oct 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
80 lines (70 loc) · 2.18 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
75
76
77
78
79
80
const READLINE = require('readline');
const URL = require('url');
const FS = require('fs');
exports.mysqlRegex = /^[$a-zA-Z0-9_]+$/;
const hostnameRegex = exports.hostnameRegex = /^@$|^[0-9a-zA-Z]+([-0-9a-zA-Z.][0-9a-zA-Z]+)*$|^([0-9]{1,3}\.){2}[0-9]{1,3}$/; // eslint-disable-line max-len
exports.urlRegex = /https?:\/\/[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/;
const isBool = exports.isBool = line => ['y', 'n'].includes(line);
exports.existsFile = line => FS.existsSync(line) && FS.statSync(line).isFile();
exports.parseUUIDFormater = line => {
let parts = line.split('/');
return {
regex: parts[1],
flags: parts[2],
replacement: parts[3],
};
};
exports.parseEndpoint = line => {
const parts = line.split(',');
return {
hostname: parts[0],
allowUnsecure: parts[1] === 'y',
};
};
exports.isEndpoint = line => {
const parts = line.split(',');
if (!isBool(parts[1])) return false;
if (!parts[0].match(hostnameRegex)) return false;
return true;
};
const isUInt = exports.isUInt = line => !isNaN(line) && Number(line) > 0 && Number(line) === Math.floor(Number(line));
exports.isUIntList = line => !line.split(',').some(a => !isUInt(a));
exports.isClientLocation = line => {
const parts = line.split('@');
if (!parts[0].match(/^[A-Z]+$/)) return false;
const u = URL.parse(`https://${parts[1]}`);
if (!u.hostname) return false;
if (!u.path) return false;
return true;
};
exports.parseClientLocation = line => {
const parts = line.split('@');
const u = URL.parse(`https://${parts[1]}`);
const a = {
hostname: u.hostname,
port: u.port,
method: parts[0],
path: u.path,
};
if (!a.port) delete a.port;
if (!a.url || a.url === '/') delete a.url;
return a;
};
exports.readStdin = (q, validateFN, def) => new Promise(resolve => {
const rl = READLINE.createInterface(process.stdin, process.stdout);
rl.setPrompt(`${q}> `);
rl.prompt();
if (def !== undefined) rl.write(def);
rl.on('line', line => {
if (validateFN(line)) {
rl.close();
return resolve(line);
}
if (def !== undefined) {
rl.prompt();
return rl.write(def);
} else {
return rl.prompt();
}
});
});