shellway 0.6.3 → 0.6.4
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/shellway.js +22 -2
package/package.json
CHANGED
package/shellway.js
CHANGED
|
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
7
7
|
import { execFileSync, execFile } from 'node:child_process';
|
|
8
8
|
import { readFile, writeFile, appendFile, readdir, stat, lstat, mkdir, rename, cp, rm } from 'node:fs/promises';
|
|
9
9
|
|
|
10
|
-
const AGENT_VERSION = '0.6.
|
|
10
|
+
const AGENT_VERSION = '0.6.4'; // keep equal to agent/package.json
|
|
11
11
|
if (Number(process.versions.node.split('.')[0]) < 22) {
|
|
12
12
|
console.error(`Shellway needs Node.js 22 or newer (you have ${process.versions.node}). Get it at https://nodejs.org`);
|
|
13
13
|
process.exit(1);
|
|
@@ -882,9 +882,29 @@ function start() {
|
|
|
882
882
|
setInterval(keepAwakeTick, 60_000);
|
|
883
883
|
const wsUrl = conf.server.replace(/^http/, 'ws') + '/agent';
|
|
884
884
|
let delay = 1000;
|
|
885
|
+
// 0.6.4 (owner PC 15 Nov 09:13): after sleep or a network change the socket can stay "open" on this side while the
|
|
886
|
+
// server has already dropped it (heartbeat timeout) — the agent then never reconnects. Watchdog: a tiny keepalive
|
|
887
|
+
// every 15 s (the relay ignores messages without an id) makes a dead connection fail fast, a clock jump means the
|
|
888
|
+
// computer slept, and data stuck in the send buffer for ~1 minute means the link is gone. Any of these: reconnect now.
|
|
889
|
+
let current = null, lastTick = Date.now();
|
|
890
|
+
const reconnectNow = (why) => {
|
|
891
|
+
const w = current; if (!w) return;
|
|
892
|
+
current = null; w.onclose = null; w.onmessage = null;
|
|
893
|
+
try { w.close(); } catch {}
|
|
894
|
+
console.log(`${why} — reconnecting…`);
|
|
895
|
+
delay = 1000; setTimeout(connect, 500);
|
|
896
|
+
};
|
|
897
|
+
setInterval(() => {
|
|
898
|
+
const t = Date.now(), slept = t - lastTick > 45_000; lastTick = t;
|
|
899
|
+
const w = current; if (!w || w.readyState !== 1) return;
|
|
900
|
+
if (slept) return reconnectNow('Computer woke up');
|
|
901
|
+
try { w.send('{"type":"keepalive"}'); } catch { return reconnectNow('Connection lost'); }
|
|
902
|
+
if (w.bufferedAmount > 0) { w._stuck = (w._stuck || 0) + 1; if (w._stuck >= 4) reconnectNow('Connection stuck'); } else w._stuck = 0;
|
|
903
|
+
}, 15_000);
|
|
885
904
|
const connect = async () => {
|
|
886
905
|
if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(78); }
|
|
887
906
|
const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}`, 'x-shellway-version': AGENT_VERSION } });
|
|
907
|
+
current = ws;
|
|
888
908
|
ws.onopen = () => { delay = 1000; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
|
|
889
909
|
ws.onmessage = async (ev) => {
|
|
890
910
|
let m; try { m = JSON.parse(String(ev.data)); } catch { return; }
|
|
@@ -895,7 +915,7 @@ function start() {
|
|
|
895
915
|
ws.send(JSON.stringify({ id: m.id, error: e.message }));
|
|
896
916
|
}
|
|
897
917
|
};
|
|
898
|
-
ws.onclose = () => { console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
|
|
918
|
+
ws.onclose = () => { if (current === ws) current = null; console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
|
|
899
919
|
ws.onerror = () => {};
|
|
900
920
|
};
|
|
901
921
|
connect();
|