@dotrino/terminal-agent 0.27.0 → 0.29.0

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.
Files changed (3) hide show
  1. package/consoles.js +30 -2
  2. package/index.js +25 -1
  3. package/package.json +3 -3
package/consoles.js CHANGED
@@ -176,13 +176,14 @@ class Console {
176
176
  * de lo posterior.
177
177
  * @returns {Promise<string>} la pantalla serializada
178
178
  */
179
- attach (viewer) {
179
+ attach (viewer, { scrollback } = {}) {
180
180
  const pending = []
181
181
  const buffering = { onOut: (d) => pending.push(d), onExit: viewer.onExit }
182
182
  this.viewers.add(buffering)
183
183
  return new Promise((resolve) => {
184
184
  this.screen.write('', () => {
185
- const snapshot = this.serializer.serialize()
185
+ // `scrollback: 0` = solo lo que se ve ahora (ver `history`).
186
+ const snapshot = this.serializer.serialize(scrollback === undefined ? undefined : { scrollback })
186
187
  this.viewers.delete(buffering)
187
188
  this.viewers.add(viewer)
188
189
  this.doneAt = null // quien entra, la atiende (el `_meta` de abajo lo cuenta)
@@ -192,6 +193,33 @@ class Console {
192
193
  })
193
194
  }
194
195
 
196
+ /** ¿Hay líneas por encima de lo que se ve? Sin ellas, la pantalla sola ya es la foto entera. */
197
+ hasHistory () { return this.screen.buffer.normal.baseY > 0 }
198
+
199
+ /**
200
+ * LA FOTO ENTERA, PARA QUIEN YA ESTÁ MIRANDO. Quien se enganchó recibiendo solo la pantalla
201
+ * (`attach` con `scrollback: 0`) pide después el historial: lo que escribe la shell mientras
202
+ * se hace la foto se guarda y sale detrás, igual que en `attach`, así que no se pierde ni se
203
+ * repite nada. Empieza por un reinicio del terminal (`ESC c`): el cliente la pinta encima de
204
+ * lo que tenía sin saber que es una segunda foto.
205
+ * @returns {Promise<string|null>} `null` si ese mirón ya no está
206
+ */
207
+ history (viewer) {
208
+ if (!this.viewers.has(viewer)) return Promise.resolve(null)
209
+ const pending = []
210
+ const buffering = { onOut: (d) => pending.push(d), onExit: viewer.onExit }
211
+ this.viewers.delete(viewer)
212
+ this.viewers.add(buffering)
213
+ return new Promise((resolve) => {
214
+ this.screen.write('', () => {
215
+ const snapshot = this.serializer.serialize()
216
+ this.viewers.delete(buffering)
217
+ this.viewers.add(viewer)
218
+ resolve('\x1bc' + snapshot + pending.join(''))
219
+ })
220
+ })
221
+ }
222
+
195
223
  detach (viewer) {
196
224
  if (!this.viewers.has(viewer)) return
197
225
  // Quién decidía, ANTES de quitarlo: si era él, hay que pasar el tamaño a otro.
package/index.js CHANGED
@@ -37,6 +37,9 @@ import { makeGate } from './access.js'
37
37
 
38
38
  const require = createRequire(import.meta.url)
39
39
 
40
+ /** Cuánto se espera al canal directo antes de mandar el historial por el proxio. */
41
+ export const HISTORY_WAIT_MS = 8000
42
+
40
43
  export { LABEL }
41
44
 
42
45
  /** Cada cuánto mira un agente sin enlazar si ya lo enlazaron. */
@@ -121,7 +124,12 @@ export function serveSession (session, hub, { origin = 'remote', gate = null } =
121
124
  async function attachTo (c, { fresh }) {
122
125
  release()
123
126
  current = c
124
- const snapshot = await c.attach(viewer)
127
+ // SI EL CANAL DIRECTO AÚN NO ABRIÓ, PRIMERO SOLO LA PANTALLA. La foto con su historial
128
+ // son cientos de KB y, mandada ya, da toda la vuelta por el proxio (medido: 381 KB por el
129
+ // proxio en cada enganche). La pantalla sola son unos pocos KB y se ve igual de rápido; el
130
+ // historial sale en cuanto abre el directo (`sendHistory`). No se espera a nada para pintar.
131
+ const later = origin === 'remote' && typeof session.whenDirect === 'function' && !session.direct() && c.hasHistory()
132
+ const snapshot = await c.attach(viewer, later ? { scrollback: 0 } : {})
125
133
  // Mientras se hacía la foto la sesión se fue, soltó la consola o pidió otra: esta ya no es la
126
134
  // suya. Quedarse enganchado dejaba un mirón FANTASMA (la consola salía «abierta en otro
127
135
  // aparato» sin nadie mirando, y nadie la elegía como libre).
@@ -132,6 +140,22 @@ export function serveSession (session, hub, { origin = 'remote', gate = null } =
132
140
  await session.send({ type: 'replay', id: c.id, data: snapshot.slice(i, i + REPLAY_CHUNK), last: i + REPLAY_CHUNK >= snapshot.length })
133
141
  }
134
142
  await session.send({ type: 'attached', id: c.id, fresh, console: c.info() })
143
+ // Sin esperarlo (pintar no espera a nada), y un fallo aquí no puede tumbar el agente: se dice.
144
+ if (later) sendHistory(c).catch((e) => console.error('[terminal-agent] could not send the history of console', c.id, '-', e?.message || e))
145
+ }
146
+
147
+ /**
148
+ * El historial que `attachTo` dejó para después. Sale cuando abre el canal directo; si en
149
+ * `HISTORY_WAIT_MS` no abrió, sale igual por el proxio: llegar tarde es mejor que no llegar.
150
+ */
151
+ async function sendHistory (c) {
152
+ await session.whenDirect(HISTORY_WAIT_MS)
153
+ if (current !== c) return // ya mira otra, o se fue
154
+ const full = await c.history(viewer)
155
+ if (full == null || current !== c) return
156
+ for (let i = 0; i < full.length; i += REPLAY_CHUNK) {
157
+ await session.send({ type: 'replay', id: c.id, data: full.slice(i, i + REPLAY_CHUNK), last: i + REPLAY_CHUNK >= full.length })
158
+ }
135
159
  }
136
160
 
137
161
  session.on('message', (msg) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/terminal-agent",
3
- "version": "0.27.0",
3
+ "version": "0.29.0",
4
4
  "description": "Dotrino Terminal: las ventanas de esta máquina (dotrino-terminal) y su agente, que deja abrir esas mismas consolas desde los otros aparatos de tu cuenta, cifrado punto a punto.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -27,8 +27,8 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@dotrino/identity": "0.107.2",
30
- "@dotrino/proxy-client": "0.29.0",
31
- "@dotrino/remote-agent": "0.16.0",
30
+ "@dotrino/proxy-client": "0.30.3",
31
+ "@dotrino/remote-agent": "0.18.0",
32
32
  "@dotrino/update": "0.2.2",
33
33
  "@dotrino/vault": "0.79.0",
34
34
  "@xterm/addon-serialize": "0.13.0",