@lethevimlet/sshift 1.7.4 → 1.7.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lethevimlet/sshift",
3
- "version": "1.7.4",
3
+ "version": "1.7.5",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -1190,8 +1190,10 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1190
1190
  if (session.terminal && !session.webglAddon && (session.webglContextLossCount || 0) < 3) {
1191
1191
  console.log('[SSHIFT] Attempting WebGL addon recreation after context loss');
1192
1192
  this._initWebGLAddon(session, false);
1193
- if (session.fitAddon && !document.hidden) {
1194
- try { session.fitAddon.fit(); } catch (_) {}
1193
+ if (!document.hidden) {
1194
+ // Guarded refit — see _refreshAllWebGLSessions for why a bare
1195
+ // fitAddon.fit() on a hidden container is destructive.
1196
+ this._fitTerminal(session);
1195
1197
  }
1196
1198
  // After context loss the local terminal's rasterised
1197
1199
  // cells may have been cleared; reconcile with the
@@ -1245,9 +1247,9 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1245
1247
  } else {
1246
1248
  try { webglAddon.clearTextureAtlas(); } catch (_) {}
1247
1249
  session.terminal.refresh(0, session.terminal.rows - 1);
1248
- if (session.fitAddon) {
1249
- try { session.fitAddon.fit(); } catch (_) {}
1250
- }
1250
+ // Guarded refit — a bare fitAddon.fit() here would resize a hidden
1251
+ // session to 10x5 (see _refreshAllWebGLSessions).
1252
+ this._fitTerminal(session);
1251
1253
  }
1252
1254
 
1253
1255
  return true;
@@ -1276,9 +1278,14 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1276
1278
  session.terminal.refresh(0, session.terminal.rows - 1);
1277
1279
  } else if ((session.webglContextLossCount || 0) < 3 && typeof window.WebglAddon === 'function') {
1278
1280
  this._initWebGLAddon(session, false);
1279
- if (session.fitAddon) {
1280
- try { session.fitAddon.fit(); } catch (_) {}
1281
- }
1281
+ // Refit through _fitTerminal, never fitAddon.fit() directly: a raw
1282
+ // fit() on a session whose wrapper is hidden reads the *computed*
1283
+ // (not used) style of the container, so `height: 100%` parses as
1284
+ // 100px and the terminal is resized to a bogus 10x5 — which the
1285
+ // controller then pushes to the remote PTY, wrecking whatever is
1286
+ // running in that background tab. _fitTerminal refuses to fit a
1287
+ // hidden or not-yet-laid-out container.
1288
+ this._fitTerminal(session);
1282
1289
  }
1283
1290
  };
1284
1291
  this.sessions.forEach(refresh);
@@ -1333,6 +1340,14 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1333
1340
  _fitTerminal(session, retryCount = 0) {
1334
1341
  if (!session || !session.fitAddon || !session.terminal) return false;
1335
1342
 
1343
+ // Non-controllers must mirror the server's dimensions (they receive them
1344
+ // via ssh-resize-sync); fitting them to the local container makes the
1345
+ // shared session diverge and can start a resize war between clients.
1346
+ if (session.isController === false) {
1347
+ session.needsResize = true;
1348
+ return false;
1349
+ }
1350
+
1336
1351
  const wrapper = document.getElementById(`terminal-wrapper-${session.id}`);
1337
1352
  const container = document.getElementById(`terminal-${session.id}`);
1338
1353
  if (!wrapper || !container) return false;
@@ -1407,9 +1422,44 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1407
1422
  // bottom-row garbage and garbled status lines after resize.
1408
1423
  this._forceRendererDimensionRecompute(session);
1409
1424
 
1425
+ // Make sure the remote PTY actually learns about this size. See
1426
+ // syncRemoteTerminalSize — xterm only fires onResize when cols/rows
1427
+ // change, so a fit that lands back on the dimensions the client already
1428
+ // had never notifies the server, even when the server is still on the
1429
+ // size it was given for the previous layout.
1430
+ this.syncRemoteTerminalSize(session);
1431
+
1410
1432
  return true;
1411
1433
  }
1412
1434
 
1435
+ // Push the terminal's current dimensions to the remote PTY when the server
1436
+ // is not already known to be at that size.
1437
+ //
1438
+ // The only automatic notification path is xterm's onResize event, which
1439
+ // fires ONLY when cols/rows actually change. That is not enough:
1440
+ // - a refit after a layout change can land on the same cols/rows the
1441
+ // client already had while the server is still sized for the previous
1442
+ // layout (nothing is emitted, the remote program keeps its old shape);
1443
+ // - the onResize emit is debounced and can be lost if another resize
1444
+ // event cancels the timer before it fires.
1445
+ // `remoteCols`/`remoteRows` track the last size the server is known to
1446
+ // have, so this is a no-op in the common case and cheap when it is not.
1447
+ syncRemoteTerminalSize(session) {
1448
+ if (!session || !session.terminal || !session.connected) return;
1449
+ if (!session.isController || session.isResyncing) return;
1450
+ if (!this.socket || !this.socket.connected) return;
1451
+
1452
+ const cols = session.terminal.cols;
1453
+ const rows = session.terminal.rows;
1454
+ if (!cols || !rows) return;
1455
+ if (session.remoteCols === cols && session.remoteRows === rows) return;
1456
+
1457
+ session.remoteCols = cols;
1458
+ session.remoteRows = rows;
1459
+ this.socket.emit('ssh-resize', { sessionId: session.id, cols, rows });
1460
+ console.log('[SSHIFT] Synced remote PTY size for', session.id, `${cols}x${rows}`);
1461
+ }
1462
+
1413
1463
  forceResizeTerminal(sessionId) {
1414
1464
  if (!sessionId) return;
1415
1465
  const session = this.sessions.get(sessionId) || this.sftpSessions.get(sessionId);
@@ -1471,6 +1521,19 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1471
1521
  void container.offsetWidth;
1472
1522
  void container.offsetHeight;
1473
1523
 
1524
+ // Same guard as _fitTerminal: a hidden pane reports its percentage CSS
1525
+ // verbatim ("100%" → 100px) and a fit against it produces a ~10x5
1526
+ // terminal that would be pushed straight to the remote PTY.
1527
+ if (!wrapper.classList.contains('active')) {
1528
+ session.needsResize = true;
1529
+ return false;
1530
+ }
1531
+ const rect = container.getBoundingClientRect();
1532
+ if (rect.width < 50 || rect.height < 50) {
1533
+ session.needsResize = true;
1534
+ return false;
1535
+ }
1536
+
1474
1537
  // (b) Commit the renderer's cell metrics before fit reads them.
1475
1538
  this._syncCharSizeThenClearAtlas(session);
1476
1539
 
@@ -1509,6 +1572,8 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1509
1572
  }
1510
1573
  // Emit synchronously (we already debounced upstream by only calling
1511
1574
  // this on discrete events: take-control / panel grab settlement).
1575
+ session.remoteCols = cols;
1576
+ session.remoteRows = rows;
1512
1577
  this.socket.emit('ssh-resize', { sessionId: sid, cols, rows });
1513
1578
  } catch (_) {}
1514
1579
  }
@@ -2183,7 +2248,12 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
2183
2248
  this.currentLayout = layout;
2184
2249
  // Pass syncedTabs to applyLayout which will handle distribution
2185
2250
  this.applyLayout(layout, syncedTabs);
2186
-
2251
+
2252
+ // Same staggered refit the local (dropdown) path gets — a client that
2253
+ // receives the layout from another browser tab has to re-measure its
2254
+ // panes just as thoroughly, or its terminals keep the previous shape.
2255
+ this.refitAllTerminals();
2256
+
2187
2257
  setTimeout(() => this.handleResize(), 50);
2188
2258
  }
2189
2259
  }
@@ -4876,6 +4946,10 @@ const wheelHandler = (e) => {
4876
4946
  // 3. This can cause resize feedback loops between clients
4877
4947
  // The controller is responsible for determining terminal dimensions
4878
4948
  session.terminal.resize(data.cols, data.rows);
4949
+ // The server just told us the size it applied — remember it so we
4950
+ // don't bounce it straight back via syncRemoteTerminalSize.
4951
+ session.remoteCols = data.cols;
4952
+ session.remoteRows = data.rows;
4879
4953
  console.log('[SSHIFT] Terminal resized to match server dimensions');
4880
4954
 
4881
4955
  // Clear the syncing flag after a delay to ensure resize events settle
@@ -8815,6 +8889,8 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8815
8889
  clearTimeout(sess.resizeTimeout);
8816
8890
  }
8817
8891
  sess.resizeTimeout = setTimeout(() => {
8892
+ sess.remoteCols = cols;
8893
+ sess.remoteRows = rows;
8818
8894
  this.socket.emit('ssh-resize', { sessionId, cols, rows });
8819
8895
  }, 100); // 100ms debounce
8820
8896
  }
@@ -9047,14 +9123,23 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
9047
9123
  // Setup ResizeObserver to handle container size changes
9048
9124
  // This ensures the terminal is refitted when the container is resized
9049
9125
  // We observe the wrapper (parent) because it persists across layout changes
9126
+ //
9127
+ // NOTE: this debounce MUST use its own timer field. It used to share
9128
+ // `session.resizeTimeout` with the onResize handler above, which
9129
+ // schedules the `ssh-resize` emit that tells the remote PTY about the
9130
+ // new size. fit() changes the wrapper's inner geometry, so it
9131
+ // re-triggers this observer, which then cleared the *pending emit* and
9132
+ // replaced it with another (no-op) fit — the server never learned the
9133
+ // new size and the remote program kept drawing at the old width. That
9134
+ // is the "layout changed but the terminal keeps its previous shape" bug.
9050
9135
  const resizeObserver = new ResizeObserver((entries) => {
9051
9136
  // Only fit if this is the controller and terminal is visible
9052
9137
  if (session.isController && session.terminal && wrapper.classList.contains('active')) {
9053
9138
  try {
9054
- if (session.resizeTimeout) {
9055
- clearTimeout(session.resizeTimeout);
9139
+ if (session.roFitTimeout) {
9140
+ clearTimeout(session.roFitTimeout);
9056
9141
  }
9057
- session.resizeTimeout = setTimeout(() => {
9142
+ session.roFitTimeout = setTimeout(() => {
9058
9143
  try {
9059
9144
  if (session.fitAddon && session.terminal && wrapper.classList.contains('active')) {
9060
9145
  this._fitTerminal(session);
@@ -10497,11 +10582,15 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
10497
10582
  session.resizeObserver.disconnect();
10498
10583
  session.resizeObserver = null;
10499
10584
  }
10500
- // Clean up resize timeout
10585
+ // Clean up resize timeouts (emit debounce + ResizeObserver fit debounce)
10501
10586
  if (session.resizeTimeout) {
10502
10587
  clearTimeout(session.resizeTimeout);
10503
10588
  session.resizeTimeout = null;
10504
10589
  }
10590
+ if (session.roFitTimeout) {
10591
+ clearTimeout(session.roFitTimeout);
10592
+ session.roFitTimeout = null;
10593
+ }
10505
10594
  // Clean up scrollbar MutationObserver (mobile-only)
10506
10595
  if (session._scrollbarObserver) {
10507
10596
  session._scrollbarObserver.disconnect();
@@ -10633,6 +10722,10 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
10633
10722
  clearTimeout(session.resizeTimeout);
10634
10723
  session.resizeTimeout = null;
10635
10724
  }
10725
+ if (session.roFitTimeout) {
10726
+ clearTimeout(session.roFitTimeout);
10727
+ session.roFitTimeout = null;
10728
+ }
10636
10729
  if (session.wheelHandler && session.wheelElement) {
10637
10730
  session.wheelElement.removeEventListener('wheel', session.wheelHandler);
10638
10731
  }
@@ -1618,11 +1618,17 @@ class MobileTerminalHandler {
1618
1618
  this.terminalElement.style.height = '';
1619
1619
  }
1620
1620
 
1621
- // Refit terminal
1621
+ // Refit terminal. Prefer the app's guarded refit (it refuses to fit a
1622
+ // hidden/unlaid-out container, which a bare fit() would resize to a
1623
+ // bogus 10x5) and falls back to fit() when the app isn't reachable.
1622
1624
  if (this.session.fitAddon) {
1623
1625
  requestAnimationFrame(() => {
1624
1626
  try {
1625
- this.session.fitAddon.fit();
1627
+ if (this.app && typeof this.app._fitTerminal === 'function') {
1628
+ this.app._fitTerminal(this.session);
1629
+ } else {
1630
+ this.session.fitAddon.fit();
1631
+ }
1626
1632
  } catch (err) {
1627
1633
  console.warn('[MobileTerminal] Failed to fit terminal:', err);
1628
1634
  }