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 generateUUID(): string {
11 static async sleep(milliSeconds
: number): Promise
<NodeJS
.Timeout
> {
12 return new Promise((resolve
) => setTimeout(resolve
, milliSeconds
));
15 static secondsToHHMMSS(seconds
: number): string {
16 return Utils
.milliSecondsToHHMMSS(seconds
* 1000);
19 static milliSecondsToHHMMSS(milliSeconds
: number): string {
20 return new Date(milliSeconds
).toISOString().substr(11, 8);
23 static removeExtraEmptyLines(tab
: string[]): void {
25 for (let i
= tab
.length
- 1; i
> 0; i
--) {
26 // Two consecutive empty lines?
27 if (tab
[i
].length
=== 0 && tab
[i
- 1].length
=== 0) {
28 // Remove the last one
32 if (i
=== 1 && tab
[i
- 1].length
=== 0) {
33 // Remove the first one
39 static convertToDate(value
: any): Date {
45 if (!(value
instanceof Date)) {
46 return new Date(value
);
51 static convertToInt(value
: any): number {
52 let changedValue
= value
;
56 if (Number.isSafeInteger(value
)) {
60 if (typeof value
=== 'string') {
62 changedValue
= parseInt(value
);
67 static convertToFloat(value
: any): number {
68 let changedValue
= value
;
73 if (typeof value
=== 'string') {
75 changedValue
= parseFloat(value
);
80 static convertToBoolean(value
: any): boolean {
85 if (typeof value
=== 'boolean') {
90 result
= (value
=== 'true');
96 static getRandomFloat(max
: number, min
= 0): number {
97 return Math.random() < 0.5 ? (1 - Math.random()) * (max
- min
) + min
: Math.random() * (max
- min
) + min
;
100 static getRandomInt(max
: number, min
= 0): number {
102 return Math.floor(Math.random() * (max
- min
+ 1) + min
);
104 return Math.floor(Math.random() * max
+ 1);
107 static roundTo(numberValue
: number, scale
: number): number {
108 const roundPower
= Math.pow(10, scale
);
109 return Math.round(numberValue
* roundPower
) / roundPower
;
112 static truncTo(numberValue
: number, scale
: number): number {
113 const truncPower
= Math.pow(10, scale
);
114 return Math.trunc(numberValue
* truncPower
) / truncPower
;
117 static getRandomFloatRounded(max
: number, min
= 0, scale
= 2): number {
119 return Utils
.roundTo(Utils
.getRandomFloat(max
, min
), scale
);
121 return Utils
.roundTo(Utils
.getRandomFloat(max
), scale
);
124 static logPrefix(prefixString
= ''): string {
125 const date
= new Date();
126 return date
.toLocaleString() + prefixString
;
129 static cloneObject
<T
>(object
: T
): T
{
130 return JSON
.parse(JSON
.stringify(object
)) as T
;
133 static isIterable
<T
>(obj
: T
): boolean {
135 return typeof obj
[Symbol
.iterator
] === 'function';
140 static isEmptyJSon(document
: any): boolean {
146 if (typeof document
!== 'object') {
150 return Object.keys(document
).length
=== 0;
153 static isString(value
: any): boolean {
154 return typeof value
=== 'string';
157 static isUndefined(value
: any): boolean {
158 return typeof value
=== 'undefined';
161 static isNullOrUndefined(value
: any): boolean {
162 // eslint-disable-next-line no-eq-null, eqeqeq
169 static isEmptyArray(object
: any): boolean {
173 if (Array.isArray(object
) && object
.length
> 0) {
179 static isEmptyObject(obj
: any): boolean {
180 return !Object.keys(obj
).length
;
183 static insertAt
= (str
: string, subStr
: string, pos
: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`;
186 * @param {number} [retryNumber=0]
187 * @returns {number} delay in milliseconds
189 static exponentialDelay(retryNumber
= 0): number {
190 const delay
= Math.pow(2, retryNumber
) * 100;
191 const randomSum
= delay
* 0.2 * Math.random(); // 0-20% of the delay
192 return delay
+ randomSum
;
196 * Convert websocket error code to human readable string message
198 * @param {number} code websocket error code
199 * @returns {string} human readable string message
201 static getWebSocketCloseEventStatusString(code
: number): string {
202 if (code
>= 0 && code
<= 999) {
204 } else if (code
>= 1016) {
206 return '(For WebSocket standard)';
207 } else if (code
<= 2999) {
208 return '(For WebSocket extensions)';
209 } else if (code
<= 3999) {
210 return '(For libraries and frameworks)';
211 } else if (code
<= 4999) {
212 return '(For applications)';
215 if (!Utils
.isUndefined(WebSocketCloseEventStatusString
[code
])) {
216 return WebSocketCloseEventStatusString
[code
] as string;
221 static workerPoolInUse(): boolean {
222 return [WorkerProcessType
.DYNAMIC_POOL
, WorkerProcessType
.STATIC_POOL
].includes(Configuration
.getWorkerProcess());
225 static workerDynamicPoolInUse(): boolean {
226 return Configuration
.getWorkerProcess() === WorkerProcessType
.DYNAMIC_POOL
;