nano-wait-js 1.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/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/nano-wait.js +123 -0
- package/package.json +23 -0
- package/teste.js +16 -0
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Luiz Filipe Seabra de marco
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/nano-wait.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// nano-wait.js
|
|
3
|
+
const os = require('os');
|
|
4
|
+
const wifi = require('node-wifi');
|
|
5
|
+
|
|
6
|
+
// Inicializa node-wifi
|
|
7
|
+
wifi.init({ iface: null }); // null = qualquer interface
|
|
8
|
+
|
|
9
|
+
function clamp(value, min, max) {
|
|
10
|
+
return Math.max(min, Math.min(max, value));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Função para obter pontuação de Wi-Fi (0-1)
|
|
14
|
+
async function wifiScore(ssid) {
|
|
15
|
+
if (!ssid) return 0;
|
|
16
|
+
try {
|
|
17
|
+
const networks = await wifi.scan();
|
|
18
|
+
const network = networks.find(n => n.ssid === ssid);
|
|
19
|
+
if (!network) return 0;
|
|
20
|
+
// RSSI normalizado de -100 a 0 dBm → 0 a 1
|
|
21
|
+
const score = clamp((network.signal_level + 100)/100, 0, 1);
|
|
22
|
+
return score;
|
|
23
|
+
} catch (e) {
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Função principal de espera
|
|
29
|
+
async function wait(t = 1, options = {}) {
|
|
30
|
+
const {
|
|
31
|
+
smart = false,
|
|
32
|
+
profile = 'default',
|
|
33
|
+
explain = false,
|
|
34
|
+
verbose = false,
|
|
35
|
+
headless = false,
|
|
36
|
+
wifiSSID = null
|
|
37
|
+
} = options;
|
|
38
|
+
|
|
39
|
+
// Perfis de execução
|
|
40
|
+
const profiles = {
|
|
41
|
+
ci: { minFactor: 0.5, maxFactor: 1.5, verbose: true },
|
|
42
|
+
testing: { minFactor: 0.8, maxFactor: 1.2, verbose: false },
|
|
43
|
+
rpa: { minFactor: 0.9, maxFactor: 1.1, verbose: false },
|
|
44
|
+
default: { minFactor: 0.8, maxFactor: 1.2, verbose: false }
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const prof = profiles[profile] || profiles.default;
|
|
48
|
+
|
|
49
|
+
// CPU load
|
|
50
|
+
let cpuLoad = 0;
|
|
51
|
+
if (smart) {
|
|
52
|
+
const cpus = os.cpus();
|
|
53
|
+
const idle = cpus.map(c => c.times.idle);
|
|
54
|
+
const total = cpus.map(c => Object.values(c.times).reduce((a,b)=>a+b,0));
|
|
55
|
+
const avgIdle = idle.reduce((a,b)=>a+b,0)/idle.length;
|
|
56
|
+
const avgTotal = total.reduce((a,b)=>a+b,0)/total.length;
|
|
57
|
+
cpuLoad = 1 - (avgIdle / avgTotal);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Wi-Fi score
|
|
61
|
+
let wifi_score = 0;
|
|
62
|
+
if (smart && wifiSSID && !headless) {
|
|
63
|
+
wifi_score = await wifiScore(wifiSSID);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Fator adaptativo combinando CPU + Wi-Fi
|
|
67
|
+
let factor = 1;
|
|
68
|
+
if (smart) {
|
|
69
|
+
factor = clamp(1 - ((cpuLoad + wifi_score)/2), prof.minFactor, prof.maxFactor);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Tempo final (mínimo 50ms)
|
|
73
|
+
const finalTime = Math.max(0.05, t * factor);
|
|
74
|
+
|
|
75
|
+
if (verbose || prof.verbose) {
|
|
76
|
+
console.log(`[NanoWait] requested=${t}s factor=${factor.toFixed(2)} final=${finalTime.toFixed(2)}s cpuLoad=${cpuLoad.toFixed(2)} wifiScore=${wifi_score.toFixed(2)} profile=${profile}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (explain) {
|
|
80
|
+
return {
|
|
81
|
+
requested: t,
|
|
82
|
+
final: finalTime,
|
|
83
|
+
cpuLoad,
|
|
84
|
+
wifiScore: wifi_score,
|
|
85
|
+
factor,
|
|
86
|
+
profile
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return new Promise(res => setTimeout(res, finalTime * 1000));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ===================== CLI =====================
|
|
94
|
+
if (require.main === module) {
|
|
95
|
+
// Executado via terminal
|
|
96
|
+
const args = process.argv.slice(2); // remove node + arquivo
|
|
97
|
+
const t = parseFloat(args[0]) || 1;
|
|
98
|
+
|
|
99
|
+
const options = {
|
|
100
|
+
smart: args.includes('--smart'),
|
|
101
|
+
explain: args.includes('--explain'),
|
|
102
|
+
verbose: args.includes('--verbose'),
|
|
103
|
+
headless: args.includes('--headless'),
|
|
104
|
+
profile: 'default',
|
|
105
|
+
wifiSSID: null
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// Ver perfil
|
|
109
|
+
const profileIndex = args.findIndex(a => ['ci','testing','rpa','default'].includes(a));
|
|
110
|
+
if (profileIndex >= 0) options.profile = args[profileIndex];
|
|
111
|
+
|
|
112
|
+
// Wi-Fi
|
|
113
|
+
const wifiIndex = args.findIndex(a => a.startsWith('--wifi='));
|
|
114
|
+
if (wifiIndex >= 0) options.wifiSSID = args[wifiIndex].split('=')[1];
|
|
115
|
+
|
|
116
|
+
(async () => {
|
|
117
|
+
const result = await wait(t, options);
|
|
118
|
+
if (options.explain) console.log(result);
|
|
119
|
+
})();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Exporta para uso via require()
|
|
123
|
+
module.exports = { wait };
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nano-wait-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Adaptive wait engine for Node.js inspired by NanoWait Python",
|
|
5
|
+
"main": "nano-wait.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"nano-wait": "./nano-wait.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node teste.js"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/LuizSeabraDeMarco/Nano-Wait-JS.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": ["nano-wait", "adaptive wait", "automation", "rpa", "smart wait"],
|
|
17
|
+
"author": "Luiz Seabra",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"node-wifi": "^2.0.15"
|
|
21
|
+
},
|
|
22
|
+
"type": "commonjs"
|
|
23
|
+
}
|
package/teste.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// test.js
|
|
2
|
+
const { wait } = require('./nano-wait');
|
|
3
|
+
|
|
4
|
+
(async () => {
|
|
5
|
+
console.log('=== Teste do NanoWait ===\n');
|
|
6
|
+
|
|
7
|
+
console.log('1) Espera simples de 2 segundos adaptativa (smart + verbose):');
|
|
8
|
+
await wait(2, { smart: true, profile: 'testing', verbose: true });
|
|
9
|
+
console.log('Fim da espera!\n');
|
|
10
|
+
|
|
11
|
+
console.log('2) Espera com explain mode:');
|
|
12
|
+
const report = await wait(1.5, { smart: true, profile: 'ci', explain: true, verbose: true });
|
|
13
|
+
console.log('Relatório do Explain Mode:');
|
|
14
|
+
console.log(report);
|
|
15
|
+
console.log('\nTeste concluído!');
|
|
16
|
+
})();
|