shellway 0.6.5 → 0.6.7
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 +40 -13
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.7'; // 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,37 +887,63 @@ 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
|
-
|
|
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
|
+
// 0.6.7 (17 Nov, customers reconnecting every 1-2 min on some Windows PCs): the link is judged by what the SERVER sends.
|
|
894
|
+
// The relay answers every keepalive ({"type":"ka"}); no message for KA_TIMEOUT means the link is gone. The old guesses
|
|
895
|
+
// (send-buffer size, timer gaps) are only a fallback for an old relay that never answers. After each new connection the
|
|
896
|
+
// agent reports its Node.js version, the reason, and the connection id it left, so the relay drops that one at once.
|
|
897
|
+
const TICK = Number(process.env.SHELLWAY_TICK_MS) || 15_000, KA_TIMEOUT = Number(process.env.SHELLWAY_KA_TIMEOUT_MS) || 75_000;
|
|
898
|
+
let current = null, lastTick = Date.now(), gen = 0, connectingSince = 0;
|
|
899
|
+
let lastMsg = Date.now(), kaSeen = false, cid = null, prevCid = null, why = 'start';
|
|
900
|
+
const reconnectNow = (reason) => {
|
|
891
901
|
const w = current; if (!w) return;
|
|
892
902
|
current = null; w.onclose = null; w.onmessage = null;
|
|
893
903
|
try { w.close(); } catch {}
|
|
894
|
-
console.log(`${
|
|
904
|
+
console.log(`${reason} — reconnecting…`);
|
|
905
|
+
why = reason; prevCid = cid; cid = null;
|
|
895
906
|
delay = 1000; setTimeout(connect, 500);
|
|
896
907
|
};
|
|
897
908
|
setInterval(() => {
|
|
898
|
-
const t = Date.now(), slept = t - lastTick > 45_000; lastTick = t;
|
|
909
|
+
const t = Date.now(), slept = t - lastTick > Math.max(45_000, TICK * 3); lastTick = t;
|
|
910
|
+
if (connectingSince && t - connectingSince > 30_000) {
|
|
911
|
+
const w = current; current = null; connectingSince = 0;
|
|
912
|
+
if (w) { w.onopen = null; w.onclose = null; w.onmessage = null; try { w.close(); } catch {} }
|
|
913
|
+
console.log('Connecting took too long — retrying…');
|
|
914
|
+
why = 'Connecting took too long';
|
|
915
|
+
delay = 1000; connect(); return;
|
|
916
|
+
}
|
|
899
917
|
const w = current; if (!w || w.readyState !== 1) return;
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
if (
|
|
918
|
+
const quiet = t - lastMsg;
|
|
919
|
+
if (slept && quiet > 30_000) return reconnectNow('Computer woke up');
|
|
920
|
+
if (kaSeen) { if (quiet > KA_TIMEOUT) return reconnectNow('No answer from Shellway'); }
|
|
921
|
+
// fallback for a relay without keepalive answers. 0.6.5: check the buffer BEFORE sending (right after send() it always holds the new frame)
|
|
922
|
+
else if (w.bufferedAmount > 0) { w._stuck = (w._stuck || 0) + 1; if (w._stuck >= 8) return reconnectNow('Connection stuck'); } else w._stuck = 0;
|
|
903
923
|
try { w.send('{"type":"keepalive"}'); } catch { return reconnectNow('Connection lost'); }
|
|
904
|
-
},
|
|
924
|
+
}, TICK);
|
|
905
925
|
const connect = async () => {
|
|
926
|
+
const my = ++gen; connectingSince = Date.now();
|
|
906
927
|
if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(78); }
|
|
928
|
+
if (my !== gen) return; // a newer attempt took over while the check was running
|
|
907
929
|
const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}`, 'x-shellway-version': AGENT_VERSION } });
|
|
908
930
|
current = ws;
|
|
909
|
-
ws.onopen = () => {
|
|
931
|
+
ws.onopen = () => { if (my !== gen) { try { ws.close(); } catch {} return; } connectingSince = 0; delay = 1000; lastMsg = Date.now();
|
|
932
|
+
try { ws.send(JSON.stringify({ type: 'info', v: AGENT_VERSION, node: process.versions.node, os: platform(), reason: why, ...(prevCid ? { replaced: prevCid } : {}) })); } catch {}
|
|
933
|
+
why = 'network'; prevCid = null; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
|
|
910
934
|
ws.onmessage = async (ev) => {
|
|
935
|
+
lastMsg = Date.now();
|
|
911
936
|
let m; try { m = JSON.parse(String(ev.data)); } catch { return; }
|
|
912
|
-
if (m.type === '
|
|
937
|
+
if (m.type === 'ka') { kaSeen = true; return; }
|
|
938
|
+
if (m.type === 'hello') { if (typeof m.cid === 'string') cid = m.cid; return; }
|
|
939
|
+
if (!m.id) return;
|
|
913
940
|
try {
|
|
914
941
|
ws.send(JSON.stringify({ id: m.id, result: await dispatch(m.method, m.params) }));
|
|
915
942
|
} catch (e) {
|
|
916
943
|
ws.send(JSON.stringify({ id: m.id, error: e.message }));
|
|
917
944
|
}
|
|
918
945
|
};
|
|
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); };
|
|
946
|
+
ws.onclose = () => { if (my !== gen) return; if (current === ws) current = null; connectingSince = 0; prevCid = cid; cid = null; console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
|
|
920
947
|
ws.onerror = () => {};
|
|
921
948
|
};
|
|
922
949
|
connect();
|