clavarium 0.2.464-next.1 → 0.2.465-next.1

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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.464-next.1",
3
+ "version": "0.2.465-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.464-next.1",
9
+ "version": "0.2.465-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.464-next.1",
3
+ "version": "0.2.465-next.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
package/server/bridge.mjs CHANGED
@@ -31,6 +31,9 @@ export const CLOSE_ENDED = 1000;
31
31
  export const CLOSE_TAKEN = 4001;
32
32
  /* A device whose right on this terminal was taken back (or its session ended): put out at once. */
33
33
  export const CLOSE_REVOKED = 4003;
34
+ /* A page that came back by itself (a dropped connection, this server restarted) to a machine put to sleep or
35
+ stopped: not woken -- only a person opening it wakes it (16-11: a tab left open woke a sleeper at each update). */
36
+ export const CLOSE_ASLEEP = 4004;
34
37
  /* A channel that dropped is reopened after a moment, a few times a minute at most. */
35
38
  export const REOPEN_AFTER_MS = 1000;
36
39
  export const REDRAW_AFTER_MS = 250;
@@ -87,7 +90,7 @@ export const sameOrigin = makeOriginCheck();
87
90
  const BY_ITSELF = /\x1b\[[IO]|\x1b\[\??[>=]?[\d;]*c|\x1b\[\d+;\d+R|\x1b\][\d;]*(?:rgb:[0-9a-fA-F/]+)?(?:\x07|\x1b\\)|\x1bP[^\x1b]*\x1b\\/g;
88
91
  export const byHand = (typed) => String(typed ?? '').replace(BY_ITSELF, '').length > 0;
89
92
 
90
- export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () => {}, originOk = sameOrigin, gate = () => 'local', deviceName = () => undefined, onRejoin = () => {}, onTyped = () => {} }) {
93
+ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () => {}, originOk = sameOrigin, gate = () => 'local', deviceName = () => undefined, onRejoin = () => {}, onTyped = () => {}, asleep = () => false }) {
91
94
  const sockets = new WebSocketServer({ noServer: true });
92
95
  const seats = new Seats();
93
96
  /* THE DEVICES AT A TERMINAL, each with what lets it stay: asked again when the owner changes
@@ -123,9 +126,11 @@ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () =>
123
126
  } else return;
124
127
  const cols = Number(url.searchParams.get('cols'));
125
128
  const rows = Number(url.searchParams.get('rows'));
129
+ /* `resume=1`: the page comes back by itself, nobody asked to open it. */
130
+ const resume = url.searchParams.get('resume') === '1';
126
131
  sockets.handleUpgrade(request, socket, head, (viewer) => {
127
132
  if (still) { deviceSeats.set(viewer, still); viewer.on('close', () => deviceSeats.delete(viewer)); }
128
- seat(viewer, name, within(cols, COLS) ? cols : 80, within(rows, ROWS) ? rows : 24);
133
+ seat(viewer, name, within(cols, COLS) ? cols : 80, within(rows, ROWS) ? rows : 24, resume);
129
134
  });
130
135
  });
131
136
 
@@ -211,7 +216,7 @@ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () =>
211
216
  return kept;
212
217
  }
213
218
 
214
- async function seat(viewer, name, cols, rows) {
219
+ async function seat(viewer, name, cols, rows, resume = false) {
215
220
  const previous = seats.take(name, viewer);
216
221
  if (previous) {
217
222
  previous.send(JSON.stringify({ type: 'taken' }));
@@ -249,6 +254,8 @@ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () =>
249
254
  resize(taken, cols, Math.max(2, rows - 1));
250
255
  setTimeout(() => { if (live.get(name) === taken && taken.sink === viewer) resize(taken, taken.cols, taken.rows); }, REDRAW_AFTER_MS);
251
256
  } else {
257
+ /* Asleep or stopped, and nobody asked: left so; the page offers to wake it. */
258
+ if (resume && (() => { try { return asleep(name) === true; } catch { return false; } })()) { viewer.close(CLOSE_ASLEEP, 'asleep'); return; }
252
259
  try { kept = await open(name, cols, rows, viewer, say); }
253
260
  catch (error) {
254
261
  /* Only a message written for the person is passed on. */
package/server/main.mjs CHANGED
@@ -917,6 +917,8 @@ export async function startHome({ port, host, dataDir, mntDir = dataDir, gitRese
917
917
  try { syncTargets(tethers.store(), boxes.all()); } catch (error) { console.error(`voice boxes in Tether: ${error.message}`); }
918
918
  bridge = attachBridge(server, {
919
919
  onRejoin: (name) => all.rejoin(name),
920
+ /* Put to sleep or stopped: a page that comes back by itself does not wake it (bridge.mjs, CLOSE_ASLEEP). */
921
+ asleep: (name) => { const c = all.get(name); return c?.asleep === true || c?.stopped === true; },
920
922
  onTyped: (name, data) => { answerer.typed(name, data); talk.typed(name); all.touch(name); typingWatch.typed(name, data); },
921
923
  spawn: async (name, cols, rows, say) => {
922
924
  const [program, ...argv] = await all.open(name, say);
package/web/app.js CHANGED
@@ -4,6 +4,7 @@
4
4
  (() => {
5
5
  const CLOSE_ENDED = 1000;
6
6
  const CLOSE_TAKEN = 4001;
7
+ const CLOSE_ASLEEP = 4004;
7
8
  const CLOSE_FAILED = 1011;
8
9
  const RETRY_MS = 1500;
9
10
  const NAME = /^[a-z](?:[a-z0-9-]{0,30}[a-z0-9])?$/;
@@ -821,11 +822,16 @@
821
822
  /* When this wait began (kept across the tries made by the page itself), and whether the server repaired it meanwhile. */
822
823
  let waitingSince = 0;
823
824
  let repairedFor = '';
825
+ let resumeWait = false;
824
826
  const disconnect = () => { wanted = false; clearSlow(); socket?.close(); socket = undefined; stopVoice?.(); };
825
827
 
826
828
  /* `again`: a try made by the page itself, within the same wait. */
829
+ /* `resume`: the page comes back by itself (a dropped connection, the server restarted): a machine asleep stays so. */
827
830
  function connect(options) {
828
831
  const again = options?.again === true;
832
+ /* Tries the page makes by itself within that wait keep it: a server still restarting refuses a few first. */
833
+ const resume = options?.resume === true || (again && resumeWait);
834
+ resumeWait = resume;
829
835
  disconnect();
830
836
  if (!NAME.test(current)) return;
831
837
  const name = current;
@@ -835,7 +841,7 @@
835
841
  say('connecting', 'connecting');
836
842
  fit.fit();
837
843
  const scheme = location.protocol === 'https:' ? 'wss' : 'ws';
838
- const where = DEVICE ? `/c/terminal?d=${pairing}&c=${encodeURIComponent(name)}&cols=${terminal.cols}&rows=${terminal.rows}` : `/terminal?c=${encodeURIComponent(name)}&cols=${terminal.cols}&rows=${terminal.rows}`;
844
+ const where = DEVICE ? `/c/terminal?d=${pairing}&c=${encodeURIComponent(name)}&cols=${terminal.cols}&rows=${terminal.rows}${resume ? '&resume=1' : ''}` : `/terminal?c=${encodeURIComponent(name)}&cols=${terminal.cols}&rows=${terminal.rows}${resume ? '&resume=1' : ''}`;
839
845
  const mine = new WebSocket(`${scheme}://${location.host}${where}`);
840
846
  socket = mine;
841
847
  mine.binaryType = 'arraybuffer';
@@ -904,7 +910,11 @@
904
910
  };
905
911
  mine.onclose = (event) => {
906
912
  if (socket !== mine || !wanted) return; /* we moved on, or closed it ourselves */
907
- if (event.code === CLOSE_TAKEN) {
913
+ if (event.code === CLOSE_ASLEEP) {
914
+ say('asleep', 'waiting');
915
+ show(uiSaid('set-asleep-page', 'Asleep. It wakes only when you wake it.'), uiSaid('set-wake-it', 'Wake it'), connect); mascot('asleep');
916
+ if (DEVICE) void refresh();
917
+ } else if (event.code === CLOSE_TAKEN) {
908
918
  say('open elsewhere', 'waiting');
909
919
  show('This conversation is now open on another screen.', 'Use it here', connect);
910
920
  } else if (event.code === CLOSE_ENDED) {
@@ -939,7 +949,7 @@
939
949
  } else {
940
950
  say('reconnecting', 'connecting');
941
951
  if (DEVICE) void refresh(); /* removed meanwhile? then say it, do not retry for ever */
942
- setTimeout(() => { if (socket === mine && wanted) connect(); }, RETRY_MS);
952
+ setTimeout(() => { if (socket === mine && wanted) connect({ resume: true }); }, RETRY_MS);
943
953
  }
944
954
  };
945
955
  }
@@ -951,7 +961,7 @@
951
961
  if (!current || !socket || !wanted) return;
952
962
  const dead = socket; socket = undefined;
953
963
  try { dead.close(); } catch { /* gone */ }
954
- connect();
964
+ connect({ resume: true });
955
965
  });
956
966
 
957
967
  terminal.onData(send);