shellway 0.6.5 → 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 +17 -5
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,6 +900,12 @@ 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');
|
|
901
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)
|
|
@@ -903,10 +913,12 @@ function start() {
|
|
|
903
913
|
try { w.send('{"type":"keepalive"}'); } catch { return reconnectNow('Connection lost'); }
|
|
904
914
|
}, 15_000);
|
|
905
915
|
const connect = async () => {
|
|
916
|
+
const my = ++gen; connectingSince = Date.now();
|
|
906
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
|
|
907
919
|
const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}`, 'x-shellway-version': AGENT_VERSION } });
|
|
908
920
|
current = ws;
|
|
909
|
-
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.`); };
|
|
910
922
|
ws.onmessage = async (ev) => {
|
|
911
923
|
let m; try { m = JSON.parse(String(ev.data)); } catch { return; }
|
|
912
924
|
if (m.type === 'hello' || !m.id) return;
|
|
@@ -916,7 +928,7 @@ function start() {
|
|
|
916
928
|
ws.send(JSON.stringify({ id: m.id, error: e.message }));
|
|
917
929
|
}
|
|
918
930
|
};
|
|
919
|
-
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); };
|
|
920
932
|
ws.onerror = () => {};
|
|
921
933
|
};
|
|
922
934
|
connect();
|