yaver-cli 1.99.304 → 1.99.306
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/package.json +1 -1
- package/src/discovery.js +29 -10
package/package.json
CHANGED
package/src/discovery.js
CHANGED
|
@@ -3,7 +3,9 @@ const dgram = require('dgram');
|
|
|
3
3
|
|
|
4
4
|
const YAVER_PORT = 8347;
|
|
5
5
|
const BEACON_PORT = 19837;
|
|
6
|
-
const DISCOVERY_TIMEOUT =
|
|
6
|
+
const DISCOVERY_TIMEOUT = 1500;
|
|
7
|
+
const HEALTH_TIMEOUT = 1200;
|
|
8
|
+
const LAN_SCAN_TIMEOUT = 350;
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* Discover a yaver.io device on the network.
|
|
@@ -66,10 +68,10 @@ function listenForBeacon(timeout) {
|
|
|
66
68
|
}
|
|
67
69
|
|
|
68
70
|
/** Fetch /health from a device */
|
|
69
|
-
function fetchHealth(device) {
|
|
71
|
+
function fetchHealth(device, timeout = HEALTH_TIMEOUT) {
|
|
70
72
|
return new Promise((resolve, reject) => {
|
|
71
73
|
const url = `http://${device.ip}:${device.port || YAVER_PORT}/health`;
|
|
72
|
-
const req = http.get(url, { timeout
|
|
74
|
+
const req = http.get(url, { timeout }, (res) => {
|
|
73
75
|
let data = '';
|
|
74
76
|
res.on('data', chunk => data += chunk);
|
|
75
77
|
res.on('end', () => {
|
|
@@ -93,27 +95,44 @@ async function scanLAN() {
|
|
|
93
95
|
const os = require('os');
|
|
94
96
|
const interfaces = os.networkInterfaces();
|
|
95
97
|
const found = [];
|
|
98
|
+
const seen = new Set();
|
|
96
99
|
|
|
97
100
|
for (const [, addrs] of Object.entries(interfaces)) {
|
|
98
101
|
for (const addr of addrs) {
|
|
99
102
|
if (addr.family !== 'IPv4' || addr.internal) continue;
|
|
100
103
|
// Try common IPs on this subnet
|
|
101
104
|
const subnet = addr.address.split('.').slice(0, 3).join('.');
|
|
102
|
-
const
|
|
105
|
+
const ips = [];
|
|
103
106
|
for (let i = 1; i <= 254; i++) {
|
|
104
107
|
const ip = `${subnet}.${i}`;
|
|
105
108
|
if (ip === addr.address) continue;
|
|
106
|
-
|
|
107
|
-
fetchHealth({ ip, port: YAVER_PORT })
|
|
108
|
-
.then(h => found.push({ ip, port: YAVER_PORT, name: h.deviceName, platform: h.platform }))
|
|
109
|
-
.catch(() => {})
|
|
110
|
-
);
|
|
109
|
+
ips.push(ip);
|
|
111
110
|
}
|
|
112
|
-
await
|
|
111
|
+
await runLimited(ips, 48, async (ip) => {
|
|
112
|
+
try {
|
|
113
|
+
const h = await fetchHealth({ ip, port: YAVER_PORT }, LAN_SCAN_TIMEOUT);
|
|
114
|
+
const key = h.deviceId || `${ip}:${YAVER_PORT}`;
|
|
115
|
+
if (seen.has(key)) return;
|
|
116
|
+
seen.add(key);
|
|
117
|
+
found.push({ ip, port: YAVER_PORT, name: h.deviceName, platform: h.platform });
|
|
118
|
+
} catch {}
|
|
119
|
+
});
|
|
113
120
|
}
|
|
114
121
|
}
|
|
115
122
|
|
|
116
123
|
return found;
|
|
117
124
|
}
|
|
118
125
|
|
|
126
|
+
async function runLimited(items, limit, fn) {
|
|
127
|
+
let next = 0;
|
|
128
|
+
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
129
|
+
for (;;) {
|
|
130
|
+
const idx = next++;
|
|
131
|
+
if (idx >= items.length) return;
|
|
132
|
+
await fn(items[idx]);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
await Promise.all(workers);
|
|
136
|
+
}
|
|
137
|
+
|
|
119
138
|
module.exports = { discoverDevice, fetchHealth, scanLAN, YAVER_PORT };
|