-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
98 lines (83 loc) · 3 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const { v4: uuidv4 } = require('uuid');
const EJSON = require('ejson');
const fs = require('fs');
const nrl = require('n-readlines');
const http = require('http');
const https = require('https');
let randomId = () => uuidv4();
let randomString = (chars, len) => {
var out = '';
for (var i = 0; i < len; ++i) {
out += chars[Math.floor(Math.random() * chars.length)];
}
return out;
};
let randomDigits = (len) => randomString('0123456789', len);
let randomLetters = (len) => randomString('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', len);
let errlog = (level, ...args) => {
if (module.exports.VERBOSITY >= level) {
console.error(...args);
}
};
let errlog0 = (...args) => errlog(0, ...args);
let errlog1 = (...args) => errlog(1, ...args);
let errlog2 = (...args) => errlog(2, ...args);
let errlog3 = (...args) => errlog(3, ...args);
let ejsonClone = (o) => EJSON.parse(EJSON.stringify(o));
const parsePkgAndUrl = (url) => {
if (url.indexOf('https://') === 0) {
return {url: url, pkg: https};
} else if (url.indexOf('http://') === 0) {
return {url: url, pkg: http};
}
let httpResult = {url: 'http://' + url, pkg: http};
let httpsResult = {url: 'https://' + url, pkg: https};
let hostPart = url.split('/')[0];
if (hostPart.indexOf(':') === -1) {
httpResult;
}
let port = parseInt(hostPart.split(':')[1]);
if (port === 80 || port === 8080 || port === 8888 || port === 8000) {
httpResult;
}
return httpsResult;
};
const getUrl = (inputUrl) => {
let {url, pkg} = parsePkgAndUrl(inputUrl);
return new Promise((resolve, reject) => {
//console.log("Have promise", url);
pkg.get(url, (response) => {
let body = '';
response.on('data', (part) => body += part);
response.on('end', () => { resolve(body); });
response.on('error', (e) => { console.error(e); reject(e); });
});
});
};
const heading = (str) => {
return str + "\n" + "-".repeat(str.length);
};
//todo
/**
* i want this class so I can have multiple probemanagers reading from the same
* wordlist where (1) we only read the entire wordlist from disk once (compare
* the behavior of multiple FileProbeManagers on the same file with the same
* lineDelta), and (2) lines are removed from RAM after every one of the
* probemanagers has already used that line. Current name references a comic
* book store because we discard an "issue" (i.e., a line from the file) after
* all of our "readers" (managers) are past that point in the "series" (file).
* Yeah, silly, but you come up with a better name.
**/
/*
class FileComicBookStore {
constructor(fn) {
this.filename = fn;
this.file = new nReadLines(this.filename);
this.lines = {}
this.nextIndex = 0;
this.
}
}
*/
module.exports = {randomId, randomString, randomDigits, randomLetters, errlog, errlog0, errlog1, errlog2, errlog3, ejsonClone, parsePkgAndUrl, getUrl, heading};
module.exports.VERBOSITY = 0;