shellway 0.6.4 → 0.6.6
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 +19 -6
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.6'; // 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);
|
|
@@ -871,7 +871,8 @@ export { dispatch, writeMethods };
|
|
|
871
871
|
|
|
872
872
|
async function tokenValid(conf) {
|
|
873
873
|
try {
|
|
874
|
-
|
|
874
|
+
// 0.6.6: never wait forever (a hung check left the agent offline with no retry)
|
|
875
|
+
const r = await fetch(`${conf.server}/agent/check`, { headers: { authorization: `Bearer ${conf.device_token}` }, signal: AbortSignal.timeout(15_000) });
|
|
875
876
|
return r.status !== 401;
|
|
876
877
|
} catch { return true; } // network down: keep retrying
|
|
877
878
|
}
|
|
@@ -886,7 +887,10 @@ function start() {
|
|
|
886
887
|
// server has already dropped it (heartbeat timeout) — the agent then never reconnects. Watchdog: a tiny keepalive
|
|
887
888
|
// every 15 s (the relay ignores messages without an id) makes a dead connection fail fast, a clock jump means the
|
|
888
889
|
// computer slept, and data stuck in the send buffer for ~1 minute means the link is gone. Any of these: reconnect now.
|
|
889
|
-
|
|
890
|
+
// 0.6.6 (owner PC 16 Nov 21:56 and 23:03): a connect attempt sometimes neither opened nor closed (no socket, no
|
|
891
|
+
// event), so the agent sat offline forever. Every attempt now has a generation and a deadline: no open within 30 s
|
|
892
|
+
// -> drop it and start a fresh attempt. Events from an old attempt are ignored.
|
|
893
|
+
let current = null, lastTick = Date.now(), gen = 0, connectingSince = 0;
|
|
890
894
|
const reconnectNow = (why) => {
|
|
891
895
|
const w = current; if (!w) return;
|
|
892
896
|
current = null; w.onclose = null; w.onmessage = null;
|
|
@@ -896,16 +900,25 @@ function start() {
|
|
|
896
900
|
};
|
|
897
901
|
setInterval(() => {
|
|
898
902
|
const t = Date.now(), slept = t - lastTick > 45_000; lastTick = t;
|
|
903
|
+
if (connectingSince && t - connectingSince > 30_000) {
|
|
904
|
+
const w = current; current = null; connectingSince = 0;
|
|
905
|
+
if (w) { w.onopen = null; w.onclose = null; w.onmessage = null; try { w.close(); } catch {} }
|
|
906
|
+
console.log('Connecting took too long — retrying…');
|
|
907
|
+
delay = 1000; connect(); return;
|
|
908
|
+
}
|
|
899
909
|
const w = current; if (!w || w.readyState !== 1) return;
|
|
900
910
|
if (slept) return reconnectNow('Computer woke up');
|
|
911
|
+
// 0.6.5: check the buffer BEFORE sending (right after send() it always holds the new frame, so 0.6.4 reconnected every minute)
|
|
912
|
+
if (w.bufferedAmount > 0) { w._stuck = (w._stuck || 0) + 1; if (w._stuck >= 4) return reconnectNow('Connection stuck'); } else w._stuck = 0;
|
|
901
913
|
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
914
|
}, 15_000);
|
|
904
915
|
const connect = async () => {
|
|
916
|
+
const my = ++gen; connectingSince = Date.now();
|
|
905
917
|
if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(78); }
|
|
918
|
+
if (my !== gen) return; // a newer attempt took over while the check was running
|
|
906
919
|
const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}`, 'x-shellway-version': AGENT_VERSION } });
|
|
907
920
|
current = ws;
|
|
908
|
-
ws.onopen = () => { delay = 1000; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
|
|
921
|
+
ws.onopen = () => { if (my !== gen) { try { ws.close(); } catch {} return; } connectingSince = 0; delay = 1000; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
|
|
909
922
|
ws.onmessage = async (ev) => {
|
|
910
923
|
let m; try { m = JSON.parse(String(ev.data)); } catch { return; }
|
|
911
924
|
if (m.type === 'hello' || !m.id) return;
|
|
@@ -915,7 +928,7 @@ function start() {
|
|
|
915
928
|
ws.send(JSON.stringify({ id: m.id, error: e.message }));
|
|
916
929
|
}
|
|
917
930
|
};
|
|
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); };
|
|
931
|
+
ws.onclose = () => { if (my !== gen) return; if (current === ws) current = null; connectingSince = 0; console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
|
|
919
932
|
ws.onerror = () => {};
|
|
920
933
|
};
|
|
921
934
|
connect();
|