-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
61 lines (53 loc) · 1.45 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
function empty() {}
const emptyLogger = {
info: empty,
debug: empty,
warn: empty,
error: empty,
log: empty
};
let uuidV4;
const crypto = require('crypto');
if (typeof crypto.randomUUID === 'function') {
uuidV4 = () => crypto.randomUUID();
} else {
// eslint-disable-next-line global-require
const uuid = require('uuid');
uuidV4 = uuid.v4;
}
module.exports = {
/**
* Default transport to prevent any printing in the terminal
* @type {Object} - empty logger overwriting the console object methods
*/
emptyLogger,
/**
* @deprecated
* For backwards compatibility with the `transport` configuration.
*/
emptyTransport: emptyLogger,
/**
* A function to generate a pause in promise chaining
* @param {number} timer How much ws to wait
* @return {Promise} A Promise that will resolve when timer is expired
*/
timeoutPromise: (timer) => new Promise((resolve) => {
setTimeout(resolve, timer);
}),
/**
* A function that allows to emit warnings for a specified code. The idea is to
* limit a warning emission to one per a specific code.
* @param warning - {code: string, detail: string, message: string}
*/
emitWarn: function emitWarn(warning) {
const { code, message, detail } = warning;
if (!emitWarn.warned) {
emitWarn.warned = {};
}
if (!emitWarn.warned[code]) {
emitWarn.warned[code] = true;
process.emitWarning(message, { code, detail });
}
},
uuidV4
};