-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.js
87 lines (70 loc) · 2.17 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
'use strict';
const fs = require('fs');
const path = require('path');
const config = require('config');
const PKGJSON = JSON.parse(fs.readFileSync('package.json'));
const VERSION = PKGJSON.version;
const NAME = PKGJSON.name;
const ENV = process.env.NODE_ENV || 'dev';
const PREFIX = config.redis.prefixOverride || [NAME, ENV].join('-');
const CTCPVersion = config.irc.ctcpVersionOverride || `${config.irc.ctcpVersionPrefix} v${VERSION} <${config.irc.ctcpVersionUrl}>`;
// XXX axe
const scopedRedisClient = require('./lib/scopedRedisClient');
function runningInContainer () { return process.env.DRC_IN_CONTAINER; }
async function sizeAtPath (searchPath) {
let a = 0;
const curPathEles = await fs.promises.readdir(path.resolve(searchPath));
for (const curPathEle of curPathEles) {
const curPath = path.join(searchPath, curPathEle);
const curStat = await fs.promises.stat(curPath);
if (curStat.isDirectory()) {
a += await sizeAtPath(curPath);
} else if (curStat.isFile()) {
a += curStat.size;
}
}
return a;
}
function isObjPathExtant (obj, path) {
if (typeof path === 'string') {
if (path.search('.') === -1) {
throw new Error(`isObjPathExtant: malformed path "${path}"`);
}
path = path.split('.');
}
if (obj[path[0]]) {
const pathMut = Array.from(path);
return isObjPathExtant(obj[pathMut.shift()], pathMut);
}
return !path.length ? obj : null;
}
function fqUrlFromPath (path) {
return `${config.http.proto}://${config.http.fqdn}/${path}`;
}
Date.prototype.toDRCString = function () { // eslint-disable-line no-extend-native
return this.toString().replace(/\sGMT.*/, '');
};
module.exports = {
// XXX axe
...require('./lib/Errors'),
...require('./lib/Mappers'),
...require('./lib/channelsCount'),
...require('./lib/fmtDuration'),
...require('./lib/ipInfo'),
...require('./lib/matchNetwork'),
...require('./lib/nameResolvers'),
...require('./lib/replaceIrcEscapes'),
scopedRedisClient,
...require('./lib/searchLogs'),
...require('./lib/shodan'),
...require('./lib/userXSeen'),
ENV,
NAME,
PREFIX,
VERSION,
CTCPVersion,
sizeAtPath,
isObjPathExtant,
fqUrlFromPath,
runningInContainer
};