1 import Configuration from
'./Configuration';
2 import { WebSocketCloseEventStatusString
} from
'../types/WebSocket';
3 import { WorkerProcessType
} from
'../types/Worker';
4 import { v4
as uuid
} from
'uuid';
6 export default class Utils
{
7 static logPrefix(prefixString
= ''): string {
8 return new Date().toLocaleString() + prefixString
;
11 static generateUUID(): string {
15 static async sleep(milliSeconds
: number): Promise
<NodeJS
.Timeout
> {
16 return new Promise((resolve
) => setTimeout(resolve
, milliSeconds
));
19 static secondsToHHMMSS(seconds
: number): string {
20 return Utils
.milliSecondsToHHMMSS(seconds
* 1000);
23 static milliSecondsToHHMMSS(milliSeconds
: number): string {
24 return new Date(milliSeconds
).toISOString().substr(11, 8);
27 static removeExtraEmptyLines(tab
: string[]): void {
29 for (let i
= tab
.length
- 1; i
> 0; i
--) {
30 // Two consecutive empty lines?
31 if (tab
[i
].length
=== 0 && tab
[i
- 1].length
=== 0) {
32 // Remove the last one
36 if (i
=== 1 && tab
[i
- 1].length
=== 0) {
37 // Remove the first one
43 static convertToDate(value
: any): Date {
49 if (!(value
instanceof Date)) {
50 return new Date(value
);
55 static convertToInt(value
: any): number {
56 let changedValue
= value
;
60 if (Number.isSafeInteger(value
)) {
64 if (typeof value
=== 'string') {
66 changedValue
= parseInt(value
);
71 static convertToFloat(value
: any): number {
72 let changedValue
= value
;
77 if (typeof value
=== 'string') {
79 changedValue
= parseFloat(value
);
84 static convertToBoolean(value
: any): boolean {
89 if (typeof value
=== 'boolean') {
94 result
= (value
=== 'true');
100 static getRandomFloat(max
: number, min
= 0): number {
101 return Math.random() < 0.5 ? (1 - Math.random()) * (max
- min
) + min
: Math.random() * (max
- min
) + min
;
104 static getRandomInt(max
: number, min
= 0): number {
106 return Math.floor(Math.random() * (max
- min
+ 1) + min
);
108 return Math.floor(Math.random() * max
+ 1);
111 static roundTo(numberValue
: number, scale
: number): number {
112 const roundPower
= Math.pow(10, scale
);
113 return Math.round(numberValue
* roundPower
) / roundPower
;
116 static truncTo(numberValue
: number, scale
: number): number {
117 const truncPower
= Math.pow(10, scale
);
118 return Math.trunc(numberValue
* truncPower
) / truncPower
;
121 static getRandomFloatRounded(max
: number, min
= 0, scale
= 2): number {
123 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
125 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
128 static getRandomFloatFluctuatedRounded(staticValue
: number, fluctuationPercent
: number, scale
= 2): number {
129 if (fluctuationPercent
=== 0) {
130 return Utils
.roundTo(staticValue
, scale
);
132 const fluctuationRatio
= fluctuationPercent
/ 100;
133 return Utils
.getRandomFloatRounded(staticValue
* (1 + fluctuationRatio
), staticValue
* (1 - fluctuationRatio
), scale
);
136 static cloneObject
<T
>(object
: T
): T
{
137 return JSON
.parse(JSON
.stringify(object
)) as T
;
140 static isIterable
<T
>(obj
: T
): boolean {
142 return typeof obj
[Symbol
.iterator
] === 'function';
147 static isEmptyJSon(document
: any): boolean {
153 if (typeof document
!== 'object') {
157 return Object.keys(document
).length
=== 0;
160 static isString(value
: any): boolean {
161 return typeof value
=== 'string';
164 static isUndefined(value
: any): boolean {
165 return typeof value
=== 'undefined';
168 static isNullOrUndefined(value
: any): boolean {
169 // eslint-disable-next-line no-eq-null, eqeqeq
176 static isEmptyArray(object
: any): boolean {
180 if (Array.isArray(object
) && object
.length
> 0) {
186 static isEmptyObject(obj
: any): boolean {
187 return !Object.keys(obj
).length
;
190 static insertAt
= (str
: string, subStr
: string, pos
: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
193 * @param {number} [retryNumber=0]
194 * @returns {number} delay in milliseconds
196 static exponentialDelay(retryNumber
= 0): number {
197 const delay
= Math.pow(2, retryNumber
) * 100;
198 const randomSum
= delay
* 0.2 * Math.random(); // 0-20% of the delay
199 return delay
+ randomSum
;
203 * Convert websocket error code to human readable string message
205 * @param {number} code websocket error code
206 * @returns {string} human readable string message
208 static getWebSocketCloseEventStatusString(code
: number): string {
209 if (code
>= 0 && code
<= 999) {
211 } else if (code
>= 1016) {
213 return '(For WebSocket standard)';
214 } else if (code
<= 2999) {
215 return '(For WebSocket extensions)';
216 } else if (code
<= 3999) {
217 return '(For libraries and frameworks)';
218 } else if (code
<= 4999) {
219 return '(For applications)';
222 if (!Utils
.isUndefined(WebSocketCloseEventStatusString
[code
])) {
223 return WebSocketCloseEventStatusString
[code
] as string;
228 static workerPoolInUse(): boolean {
229 return [WorkerProcessType
.DYNAMIC_POOL
, WorkerProcessType
.STATIC_POOL
].includes(Configuration
.getWorkerProcess());
232 static workerDynamicPoolInUse(): boolean {
233 return Configuration
.getWorkerProcess() === WorkerProcessType
.DYNAMIC_POOL
;