@wikicasa-dev/node-common 1.2.9 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/examples/callpool-demo.d.ts +1 -0
- package/dist/examples/callpool-demo.js +50 -0
- package/dist/examples/console-demo.d.ts +1 -0
- package/dist/examples/console-demo.js +44 -0
- package/dist/src/CallPool.d.ts +5 -0
- package/dist/src/CallPool.js +8 -0
- package/dist/src/Console.d.ts +4 -2
- package/dist/src/Console.js +20 -13
- package/package.json +4 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { CallPool, PROXY } from "../src/CallPool.js";
|
|
2
|
+
import { Console, COLORS } from "../src/Console.js";
|
|
3
|
+
async function demoCallPool() {
|
|
4
|
+
const pool = new CallPool("demo", PROXY.NONE, 1, 5, 10, 1000);
|
|
5
|
+
Console.appendSummaryLine(() => pool.print(), COLORS.YELLOW, "pool_status");
|
|
6
|
+
// Funzione che simula una chiamata API con timeout casuale
|
|
7
|
+
const simulateApiCall = (id) => {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
const delay = Math.random() * 4000 + 1000; // Ritardo casuale tra 1-5 secondi
|
|
10
|
+
const willSucceed = Math.random() > 0.2; // 80% di probabilità di successo
|
|
11
|
+
setTimeout(() => {
|
|
12
|
+
if (willSucceed) {
|
|
13
|
+
resolve(`✨ Operazione ${id} completata in ${(delay / 1000).toFixed(1)}s`);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
reject(new Error(`💥 Operazione ${id} fallita dopo ${(delay / 1000).toFixed(1)}s`));
|
|
17
|
+
}
|
|
18
|
+
}, delay);
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
// Creiamo 20 operazioni simulate
|
|
22
|
+
const operations = Array.from({ length: 20 }, (_, i) => i + 1);
|
|
23
|
+
// Aggiungiamo le operazioni al pool usando enqueue
|
|
24
|
+
const promises = operations.map(id => {
|
|
25
|
+
return pool.enqueue(() => simulateApiCall(id))
|
|
26
|
+
.then(result => {
|
|
27
|
+
Console.success(result);
|
|
28
|
+
return result;
|
|
29
|
+
})
|
|
30
|
+
.catch(error => {
|
|
31
|
+
Console.error(error.message);
|
|
32
|
+
throw error;
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
try {
|
|
36
|
+
await Promise.allSettled(promises);
|
|
37
|
+
Console.success("\n🎉 Demo completata! Tutte le operazioni sono state processate.");
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
Console.error("Errore generale nella demo:", error);
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
Console.clearSummaryLines();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Avviamo la demo
|
|
47
|
+
Console.log("🚀 Avvio demo CallPool...\n");
|
|
48
|
+
demoCallPool().catch(error => {
|
|
49
|
+
Console.error("Errore fatale nella demo:", error);
|
|
50
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Console, COLORS } from "../src/Console.js";
|
|
2
|
+
async function demoConsole() {
|
|
3
|
+
// Demo dei metodi di base
|
|
4
|
+
Console.log("Questo è un messaggio normale");
|
|
5
|
+
Console.error("Questo è un messaggio di errore");
|
|
6
|
+
Console.warn("Questo è un messaggio di avvertimento");
|
|
7
|
+
Console.success("Questo è un messaggio di successo");
|
|
8
|
+
// Demo del prefix e postfix
|
|
9
|
+
Console.setPrefix("PREFIX", COLORS.CYAN);
|
|
10
|
+
Console.log("Messaggio con prefix");
|
|
11
|
+
Console.setPostfix("POSTFIX", COLORS.MAGENTA);
|
|
12
|
+
Console.log("Messaggio con prefix e postfix");
|
|
13
|
+
// Demo dello spinner
|
|
14
|
+
await new Promise(resolve => {
|
|
15
|
+
Console.log("Caricamento in corso...", true);
|
|
16
|
+
setTimeout(() => {
|
|
17
|
+
resolve(true);
|
|
18
|
+
}, 3000);
|
|
19
|
+
});
|
|
20
|
+
// Demo delle linee di riepilogo con ID
|
|
21
|
+
Console.appendSummaryLine(() => "Stato: Attivo", COLORS.GREEN, "status");
|
|
22
|
+
Console.appendSummaryLine(() => "Errori: 0", COLORS.YELLOW, "errors");
|
|
23
|
+
Console.appendSummaryLine(() => "Tempo: " + new Date().toLocaleTimeString(), COLORS.BLUE, "time");
|
|
24
|
+
// Aggiorna i messaggi ogni secondo
|
|
25
|
+
let counter = 0;
|
|
26
|
+
const interval = setInterval(() => {
|
|
27
|
+
counter++;
|
|
28
|
+
Console.log(`Aggiornamento #${counter}`);
|
|
29
|
+
if (counter === 3) {
|
|
30
|
+
// Rimuove la linea degli errori dopo 3 secondi
|
|
31
|
+
Console.removeSummaryLine("errors");
|
|
32
|
+
}
|
|
33
|
+
if (counter >= 5) {
|
|
34
|
+
clearInterval(interval);
|
|
35
|
+
// Pulizia finale
|
|
36
|
+
Console.clearPrefix();
|
|
37
|
+
Console.clearPostfix();
|
|
38
|
+
Console.clearSummaryLines();
|
|
39
|
+
Console.success("Demo completata!");
|
|
40
|
+
}
|
|
41
|
+
}, 1000);
|
|
42
|
+
}
|
|
43
|
+
// Avvia la demo
|
|
44
|
+
demoConsole().catch(console.error);
|
package/dist/src/CallPool.d.ts
CHANGED
package/dist/src/CallPool.js
CHANGED
|
@@ -202,6 +202,7 @@ export class CallPool {
|
|
|
202
202
|
progress += Console.color(char, j < (max * 0.8) ? COLORS.GREEN : j < (max * 0.9) ? COLORS.YELLOW : COLORS.RED);
|
|
203
203
|
}
|
|
204
204
|
this._print = textStart + progress + textEnd;
|
|
205
|
+
Console.forceSummaryLinesRefresh();
|
|
205
206
|
}
|
|
206
207
|
async finish() {
|
|
207
208
|
while (this.activeCount) {
|
|
@@ -224,4 +225,11 @@ export class CallPool {
|
|
|
224
225
|
return this.fetchWithRetry(url, options, retries - 1);
|
|
225
226
|
}
|
|
226
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Restituisce il numero di chiamate attualmente in coda
|
|
230
|
+
* @returns {number} Il numero di chiamate in coda
|
|
231
|
+
*/
|
|
232
|
+
queueLength() {
|
|
233
|
+
return this.queue.length;
|
|
234
|
+
}
|
|
227
235
|
}
|
package/dist/src/Console.d.ts
CHANGED
|
@@ -19,8 +19,9 @@ export declare class Console {
|
|
|
19
19
|
private static summaryLines;
|
|
20
20
|
static setPrefix(prefix: string, color?: COLORS): void;
|
|
21
21
|
static setPostfix(postfix: string, color?: COLORS): void;
|
|
22
|
-
static appendSummaryLine(fn: () => string, color?: COLORS): void;
|
|
23
|
-
static
|
|
22
|
+
static appendSummaryLine(fn: () => string, color?: COLORS, id?: string): void;
|
|
23
|
+
static removeSummaryLine(id: string): void;
|
|
24
|
+
static clearSummaryLines(): void;
|
|
24
25
|
private static processSummaryLines;
|
|
25
26
|
static clearPostfix(): void;
|
|
26
27
|
static clearPrefix(): void;
|
|
@@ -33,4 +34,5 @@ export declare class Console {
|
|
|
33
34
|
private static getPostfix;
|
|
34
35
|
private static clearLines;
|
|
35
36
|
static write(str: string, color: COLORS, spinner: boolean, file?: string): void;
|
|
37
|
+
static forceSummaryLinesRefresh(): void;
|
|
36
38
|
}
|
package/dist/src/Console.js
CHANGED
|
@@ -27,12 +27,17 @@ export class Console {
|
|
|
27
27
|
static setPostfix(postfix, color = COLORS.WHITE) {
|
|
28
28
|
this.postfix = this.color(postfix, color);
|
|
29
29
|
}
|
|
30
|
-
static appendSummaryLine(fn, color = COLORS.WHITE) {
|
|
30
|
+
static appendSummaryLine(fn, color = COLORS.WHITE, id) {
|
|
31
31
|
if (process.env.NODE_ENV === "production")
|
|
32
32
|
return;
|
|
33
|
-
|
|
33
|
+
const uniqueId = id || `summary_${Math.random().toString(36).substr(2, 9)}`;
|
|
34
|
+
this.summaryLines.push({ id: uniqueId, fn, color });
|
|
34
35
|
}
|
|
35
|
-
static
|
|
36
|
+
static removeSummaryLine(id) {
|
|
37
|
+
this.summaryLines = this.summaryLines.filter(line => line.id !== id);
|
|
38
|
+
this.forceSummaryLinesRefresh();
|
|
39
|
+
}
|
|
40
|
+
static clearSummaryLines() {
|
|
36
41
|
this.summaryLines = [];
|
|
37
42
|
}
|
|
38
43
|
static processSummaryLines() {
|
|
@@ -84,9 +89,8 @@ export class Console {
|
|
|
84
89
|
readline.clearLine(log_stdout, 0);
|
|
85
90
|
log_stdout.write(`\r${this.getPrefix()}${P[x++]} ${this.color(str, color)}${this.getPostfix()}`);
|
|
86
91
|
if (this.summaryLines.length) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
readline.moveCursor(log_stdout, 0, -this.summaryLines.length);
|
|
92
|
+
log_stdout.write("\n");
|
|
93
|
+
this.forceSummaryLinesRefresh();
|
|
90
94
|
}
|
|
91
95
|
x &= 3;
|
|
92
96
|
this.loader = setInterval(() => {
|
|
@@ -95,19 +99,22 @@ export class Console {
|
|
|
95
99
|
}, 250);
|
|
96
100
|
}
|
|
97
101
|
else {
|
|
98
|
-
//this.clearLines(this.summaryLines.length + (this.loader ? 1 : 0));
|
|
99
|
-
//readline.moveCursor(log_stdout, 0, this.summaryLines.length);
|
|
100
|
-
//readline.cursorTo(log_stdout, 0);
|
|
101
102
|
readline.clearLine(log_stdout, 0);
|
|
102
103
|
log_stdout.write(`\r${this.getPrefix()}${this.color(str, color)}${this.getPostfix()}\n`);
|
|
103
|
-
if (file)
|
|
104
|
+
if (file) {
|
|
104
105
|
fs.appendFileSync(file, `\r${this.getPrefix()}${this.color(str, color)}${this.getPostfix()}\n`);
|
|
106
|
+
}
|
|
105
107
|
if (this.summaryLines.length && !this.first) {
|
|
106
|
-
|
|
107
|
-
log_stdout.write(`\r${this.processSummaryLines()}`);
|
|
108
|
-
readline.moveCursor(log_stdout, 0, -(this.summaryLines.length - 1));
|
|
108
|
+
this.forceSummaryLinesRefresh();
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
this.first = false;
|
|
112
112
|
}
|
|
113
|
+
static forceSummaryLinesRefresh() {
|
|
114
|
+
if (this.summaryLines.length) {
|
|
115
|
+
readline.clearScreenDown(log_stdout);
|
|
116
|
+
log_stdout.write(`\r${this.processSummaryLines()}`);
|
|
117
|
+
readline.moveCursor(log_stdout, 0, -(this.summaryLines.length - 1));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
113
120
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wikicasa-dev/node-common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Wikicasa node common",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -49,6 +49,8 @@
|
|
|
49
49
|
"scripts": {
|
|
50
50
|
"lint": "eslint --quiet --fix ./",
|
|
51
51
|
"compile": "tsc -p ./",
|
|
52
|
-
"build": "pnpm lint && pnpm compile"
|
|
52
|
+
"build": "pnpm lint && pnpm compile",
|
|
53
|
+
"demo:console": "pnpm build && node dist/examples/console-demo.js",
|
|
54
|
+
"demo:callpool": "pnpm build && node dist/examples/callpool-demo.js"
|
|
53
55
|
}
|
|
54
56
|
}
|