clavarium 0.2.0-next.11 → 0.2.0-next.12
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/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/server/bridge.mjs +29 -5
- package/server/talk.mjs +5 -2
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clavarium",
|
|
3
|
-
"version": "0.2.0-next.
|
|
3
|
+
"version": "0.2.0-next.12",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "clavarium",
|
|
9
|
-
"version": "0.2.0-next.
|
|
9
|
+
"version": "0.2.0-next.12",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@xterm/addon-fit": "^0.11.0",
|
package/package.json
CHANGED
package/server/bridge.mjs
CHANGED
|
@@ -117,6 +117,7 @@ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () =>
|
|
|
117
117
|
*/
|
|
118
118
|
const live = new Map(); /* name -> { terminal, sink, cols, rows, prefixAt } */
|
|
119
119
|
const reopened = new Map(); /* name -> times of recent automatic reopenings */
|
|
120
|
+
const opening = new Map(); /* name -> the opening under way */
|
|
120
121
|
/* name -> when its terminal was last closed for its machine's sake (put
|
|
121
122
|
to sleep, stopped, deleted) or ended by itself: what was known of its
|
|
122
123
|
machine before that is not known any more (talk.mjs). */
|
|
@@ -138,7 +139,20 @@ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () =>
|
|
|
138
139
|
|
|
139
140
|
/* Opens the terminal of `name` and keeps it; `sink` is the screen that
|
|
140
141
|
receives its output, if there is one. */
|
|
141
|
-
|
|
142
|
+
/* ONE OPENING AT A TIME. Opening takes seconds (into the machine, then
|
|
143
|
+
tmux); a second one asked meanwhile -- the server's own `warm` after a
|
|
144
|
+
restart, a line to type, a screen -- used to open another terminal,
|
|
145
|
+
and the newest closed the first: a line typed into the first lost its
|
|
146
|
+
Enter, and was typed again (seen on a real deployment). The one being
|
|
147
|
+
opened is waited for instead (`opening`). */
|
|
148
|
+
function open(name, cols, rows, sink, say = () => {}) {
|
|
149
|
+
const run = openNow(name, cols, rows, sink, say);
|
|
150
|
+
opening.set(name, run);
|
|
151
|
+
const done = () => { if (opening.get(name) === run) opening.delete(name); };
|
|
152
|
+
run.then(done, done);
|
|
153
|
+
return run;
|
|
154
|
+
}
|
|
155
|
+
async function openNow(name, cols, rows, sink, say) {
|
|
142
156
|
const terminal = await spawn(name, cols, rows, say);
|
|
143
157
|
/* While it was being opened another one may have been: the newest wins. */
|
|
144
158
|
if (live.has(name)) close(name);
|
|
@@ -189,6 +203,11 @@ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () =>
|
|
|
189
203
|
const say = (text, progress) => { if (viewer.readyState === 1) viewer.send(JSON.stringify({ type: 'status', ...(text ? { text } : {}), ...(progress ? { progress } : {}) })); };
|
|
190
204
|
|
|
191
205
|
let kept = live.get(name);
|
|
206
|
+
/* Being opened already (by the server, before any screen): that one is taken. */
|
|
207
|
+
if (!kept && opening.has(name)) {
|
|
208
|
+
kept = await opening.get(name).then(() => live.get(name), () => undefined);
|
|
209
|
+
if (gone) return;
|
|
210
|
+
}
|
|
192
211
|
if (kept) {
|
|
193
212
|
/* Taken as it is. The size is set a line short, then right: that
|
|
194
213
|
makes the tool draw its whole screen for the newcomer. */
|
|
@@ -240,6 +259,7 @@ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () =>
|
|
|
240
259
|
first screen then shows it at once. Does nothing if it is open already. */
|
|
241
260
|
async function warm(name) {
|
|
242
261
|
if (live.has(name)) return;
|
|
262
|
+
if (opening.has(name)) { await opening.get(name).catch(() => {}); return; }
|
|
243
263
|
try { await open(name, 120, 40, null); onLeft(name); } catch { /* it will be opened when a screen asks */ }
|
|
244
264
|
}
|
|
245
265
|
|
|
@@ -249,15 +269,19 @@ export function attachBridge(server, { spawn, onLeft = () => {}, onEnded = () =>
|
|
|
249
269
|
The line, then Enter `enterAfterMs` later (apart: see ENTER_AFTER_MS in
|
|
250
270
|
talk.mjs). Resolves false when there is no such channel, or when a
|
|
251
271
|
screen pressed tmux's prefix key lately; the caller then uses its
|
|
252
|
-
other way
|
|
272
|
+
other way; 'enter' when the line went but its Enter could not. Nothing
|
|
273
|
+
of it is told to `onTyped`: nobody typed. */
|
|
253
274
|
async function type(name, line, enterAfterMs = 0) {
|
|
254
275
|
const kept = live.get(name);
|
|
255
276
|
if (!kept || Date.now() - (kept.prefixAt ?? -Infinity) < PREFIX_QUIET_MS) return false;
|
|
256
277
|
try { kept.terminal.write(String(line)); } catch { return false; }
|
|
257
278
|
if (enterAfterMs > 0) await new Promise((r) => { setTimeout(r, enterAfterMs).unref?.(); });
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
279
|
+
/* The line is in the tool's input now: its Enter goes down whichever
|
|
280
|
+
terminal is open on the same pane by then. None: 'enter', the
|
|
281
|
+
caller sends the Enter alone -- never the line again. */
|
|
282
|
+
const now = live.get(name);
|
|
283
|
+
try { if (now) { now.terminal.write('\r'); return true; } } catch { /* closed meanwhile */ }
|
|
284
|
+
return 'enter';
|
|
261
285
|
}
|
|
262
286
|
|
|
263
287
|
server.on('close', () => { for (const name of [...live.keys()]) close(name); });
|
package/server/talk.mjs
CHANGED
|
@@ -85,6 +85,7 @@ export const HEALTH_KNOWN_MS = 150_000;
|
|
|
85
85
|
argument vector, no shell. tmux reads an argument that ENDS with ";" as the
|
|
86
86
|
end of a command: such a last ";" is written "\;", which tmux gives back
|
|
87
87
|
as ";" (tried). */
|
|
88
|
+
export const enterCommand = () => ['tmux', '-L', SOCKET, 'send-keys', '-t', PANE, 'Enter'];
|
|
88
89
|
export function typeCommand(line) {
|
|
89
90
|
const text = line.endsWith(';') ? `${line.slice(0, -1)}\\;` : line;
|
|
90
91
|
return ['tmux', '-L', SOCKET, 'send-keys', '-t', PANE, '-l', '--', text, ';', 'run-shell', '-d', String(ENTER_AFTER_MS / 1000), ';', 'send-keys', '-t', PANE, 'Enter'];
|
|
@@ -287,8 +288,10 @@ export class Talk {
|
|
|
287
288
|
else one command in the machine. */
|
|
288
289
|
async #type(name, line) {
|
|
289
290
|
await this.#warm(name);
|
|
290
|
-
|
|
291
|
-
|
|
291
|
+
const down = this.#terminal?.open(name) ? await Promise.resolve(this.#terminal.type(name, line, ENTER_AFTER_MS)).catch(() => false) : false;
|
|
292
|
+
if (down === true) { this.#ranAt.set(name, this.#now()); return true; }
|
|
293
|
+
/* The line went, its Enter did not (the channel closed meanwhile): the Enter alone, never the line twice. */
|
|
294
|
+
const ok = (await this.#vms.exec(name, down === 'enter' ? enterCommand() : typeCommand(line), { timeoutMs: 30_000 })).code === 0;
|
|
292
295
|
if (ok) this.#ranAt.set(name, this.#now());
|
|
293
296
|
return ok;
|
|
294
297
|
}
|