This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
88 lines (76 loc) · 2.13 KB
/
utils.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
const death = require("death")({ uncaughtException: true });
const os = require("os");
const nanoid = require("nanoid");
const util = require("util");
const stream = require('stream');
const winston = require("winston");
const { format } = require("logform");
const { SPLAT } = require('triple-beam');
const alignedWithColorsAndTime = format.combine(
format((info, opts) => {
if (!info[SPLAT]) return info;
var s = info[SPLAT].map(s => util.inspect(s, { breakLength: Infinity })).join(" ");
info.message = util.format(info.message, s);
return info;
})(),
format((info, opts) => { info.level = info.level.toUpperCase(); return info; })(),
format.colorize(),
format.timestamp(),
format.align(),
format.printf(info => { return `[${info.timestamp}] ${info.level}: ${info.message}` })
)
const logger = winston.createLogger({
format: alignedWithColorsAndTime,
});
logger.add(new winston.transports.Console);
function to(promise) {
return promise.then(
data => [null, data],
err => [err, null]
);
}
function promisify(obj, ...args) {
}
function sleep(ms) {
return new Promise(resolve => {
if (ms === 0) resolve();
else setTimeout(resolve, ms);
})
}
function shortname() {
return os.hostname().split('.')[0];
}
function nodeid(nodeid) {
const pm_id = process.env.NODE_APP_INSTANCE;
const suffix = pm_id && `-${pm_id}` || '';
const name = shortname() + '-' + nodeid + suffix;
return name;
}
function exit(timeout) {
setTimeout(function () { process.exit(1); }, timeout);
}
function uuid() {
return nanoid() + new Date().getTime().toString(36);
}
const pipeline = util.promisify(stream.pipeline);
function streamToString(stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
})
}
module.exports = {
sleep,
death,
nodeid,
shortname,
exit,
uuid,
pipeline,
to,
logger,
promisify,
streamToString,
};