@lethevimlet/sshift 1.3.8 → 1.3.9

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/webapp/js/app.js +134 -60
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lethevimlet/sshift",
3
- "version": "1.3.8",
3
+ "version": "1.3.9",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -1009,7 +1009,20 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1009
1009
  return false;
1010
1010
  }
1011
1011
 
1012
- if (session._webglInitPending) return false;
1012
+ // Guard: don't initialise WebGL while the terminal is hidden (display:none).
1013
+ // A hidden canvas has zero dimensions, producing corrupt glyph caches (Bug 3).
1014
+ const wrapper = document.getElementById(`terminal-wrapper-${session.id}`);
1015
+ if (wrapper && !wrapper.classList.contains('active')) {
1016
+ session._webglInitPending = true;
1017
+ console.log('[SSHIFT] Delaying WebGL init — wrapper is hidden');
1018
+ return false;
1019
+ }
1020
+
1021
+ if (session._webglInitPending && session.webglAddon) {
1022
+ // A previous call set the pending flag and we already have an addon;
1023
+ // don't double-initialise.
1024
+ return false;
1025
+ }
1013
1026
  session._webglInitPending = true;
1014
1027
 
1015
1028
  if (session.webglAddon) {
@@ -1162,6 +1175,19 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1162
1175
  return false;
1163
1176
  }
1164
1177
 
1178
+ // Guard: refuse to fit if the container has zero or near-zero dimensions.
1179
+ // A zero-size container produces bogus col/row counts that propagate to
1180
+ // the remote PTY and cause narrow-wrap / garbled output bugs.
1181
+ const rect = container.getBoundingClientRect();
1182
+ if (rect.width < 10 || rect.height < 10) {
1183
+ console.warn(`[SSHIFT] Container too small to fit (${rect.width.toFixed(1)}x${rect.height.toFixed(1)}px), deferring`);
1184
+ session.needsResize = true;
1185
+ if (retryCount < 3) {
1186
+ requestAnimationFrame(() => this._fitTerminal(session, retryCount + 1));
1187
+ }
1188
+ return false;
1189
+ }
1190
+
1165
1191
  void container.offsetHeight;
1166
1192
  void wrapper.offsetHeight;
1167
1193
 
@@ -1195,6 +1221,12 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1195
1221
  // re-rasterised at the new size rather than stretched from the old cache.
1196
1222
  this._resetWebGLAtlas(session);
1197
1223
 
1224
+ // Force a full repaint to flush stale cells from the previous dimensions.
1225
+ // This prevents bottom-row garbage and garbled status lines after resize.
1226
+ try {
1227
+ terminal.refresh(0, terminal.rows - 1);
1228
+ } catch (_) {}
1229
+
1198
1230
  return true;
1199
1231
  }
1200
1232
 
@@ -2031,16 +2063,19 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
2031
2063
  this.distributeTabsToPanels(syncedTabs);
2032
2064
  }
2033
2065
 
2034
- // Resize terminals after restoration
2035
- setTimeout(() => {
2066
+ // Resize terminals after restoration.
2067
+ // Use requestAnimationFrame so the browser has committed the new
2068
+ // flex/grid layout before we measure container widths (Bug 2 & 3).
2069
+ // Stagger a second pass for late fonts or animations.
2070
+ requestAnimationFrame(() => {
2071
+ this._refreshAllWebGLSessions();
2036
2072
  this.handleResize();
2037
- // Additional resize after animations complete
2038
2073
  requestAnimationFrame(() => {
2039
2074
  this.handleResize();
2040
2075
  });
2041
- }, 100);
2076
+ });
2042
2077
 
2043
- // Final resize attempt for slow rendering
2078
+ // Final resize attempt for slow rendering / font swaps
2044
2079
  setTimeout(() => this.handleResize(), 300);
2045
2080
  return;
2046
2081
  }
@@ -2089,16 +2124,18 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
2089
2124
  // Use syncedTabs if available (from another browser tab), otherwise distribute evenly
2090
2125
  this.distributeTabsToPanels(syncedTabs);
2091
2126
 
2092
- // Resize terminals after restoration
2093
- setTimeout(() => {
2127
+ // Resize terminals after restoration.
2128
+ // Use requestAnimationFrame so the browser has committed the new
2129
+ // flex/grid layout before we measure container widths (Bug 2 & 3).
2130
+ requestAnimationFrame(() => {
2131
+ this._refreshAllWebGLSessions();
2094
2132
  this.handleResize();
2095
- // Additional resize after animations complete
2096
2133
  requestAnimationFrame(() => {
2097
2134
  this.handleResize();
2098
2135
  });
2099
- }, 100);
2136
+ });
2100
2137
 
2101
- // Final resize attempt for slow rendering
2138
+ // Final resize attempt for slow rendering / font swaps
2102
2139
  setTimeout(() => this.handleResize(), 300);
2103
2140
 
2104
2141
  // Update scroll arrows for all panels
@@ -3678,6 +3715,9 @@ const wheelHandler = (e) => {
3678
3715
  this.socket.connect();
3679
3716
  }
3680
3717
  this._refreshAllWebGLSessions();
3718
+ // Refit visible terminals after tab becomes visible — the container
3719
+ // may have had zero dimensions while hidden (Bugs 1 & 2).
3720
+ requestAnimationFrame(() => this.handleResize());
3681
3721
  });
3682
3722
 
3683
3723
  this.socket.on('connect_error', (error) => {
@@ -7187,8 +7227,16 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
7187
7227
 
7188
7228
  terminal.open(container);
7189
7229
 
7190
- // Load WebGL renderer addon if enabled and available
7191
- this._initWebGLAddon(session, true);
7230
+ // Initialise the WebGL renderer only if the wrapper is currently
7231
+ // visible. Loading WebGL on a hidden (display:none) terminal
7232
+ // produces a zero-size canvas, corrupting the glyph texture atlas
7233
+ // and causing dim/invisible text (Bug 3).
7234
+ if (wrapper.classList.contains('active')) {
7235
+ this._initWebGLAddon(session, true);
7236
+ } else {
7237
+ session._webglInitPending = true;
7238
+ console.log('[SSHIFT] Deferring WebGL init — terminal not yet visible');
7239
+ }
7192
7240
 
7193
7241
  // Load image addon if enabled and available (Sixel/iTerm2/Kitty image protocols)
7194
7242
  if (this.imageAddonEnabled && typeof window.ImageAddon === 'function') {
@@ -7273,44 +7321,44 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
7273
7321
  // repaint interference during initial render
7274
7322
  terminal.options.cursorBlink = true;
7275
7323
 
7276
- console.log('[SSHIFT] Terminal opened, fitting...');
7277
-
7278
- // Fit after a small delay to ensure container is visible and rendered
7279
- // Use multiple attempts to ensure proper fitting
7280
- const attemptFit = (attempt = 1) => {
7281
- try {
7282
- // Force a reflow to ensure proper rendering
7283
- container.offsetHeight;
7284
- wrapper.offsetHeight;
7285
-
7286
- fitAddon.fit();
7287
- console.log('[SSHIFT] Terminal fitted successfully (attempt ' + attempt + '), cols:', terminal.cols, 'rows:', terminal.rows);
7288
-
7289
- // Verify the fit was successful by checking dimensions
7290
- if (terminal.cols > 0 && terminal.rows > 0) {
7291
- return true;
7292
- }
7293
- return false;
7294
- } catch (e) {
7295
- console.warn('[SSHIFT] Fit attempt ' + attempt + ' failed:', e.message);
7296
- return false;
7324
+ console.log('[SSHIFT] Terminal opened, waiting for fonts before fitting...');
7325
+
7326
+ // CRITICAL: Wait for custom monospace fonts (JetBrains Mono etc.) to
7327
+ // finish loading before calling fitAddon.fit(). If we fit before the
7328
+ // web font swaps in, the browser measures the fallback font's glyph
7329
+ // width which is typically narrower, producing far fewer columns than
7330
+ // the container can actually hold. Those bogus dimensions are then
7331
+ // sent to the remote PTY, wrapping every ~10 characters (Bug 1).
7332
+ // After fonts are ready we delay by one animation frame so the
7333
+ // browser has repainted with the correct metrics.
7334
+ const fontReady = document.fonts
7335
+ ? document.fonts.ready
7336
+ : Promise.resolve();
7337
+
7338
+ fontReady.then(() => {
7339
+ // Clear the WebGL texture atlas after the font swap — glyphs cached
7340
+ // before the swap were rasterised at the fallback font's metrics.
7341
+ if (session.webglAddon) {
7342
+ try { session.webglAddon.clearTextureAtlas(); } catch (_) {}
7297
7343
  }
7298
- };
7299
-
7300
- // Try fitting immediately
7301
- requestAnimationFrame(() => {
7302
- if (!attemptFit(1)) {
7303
- // If first attempt fails, try again after delays
7304
- setTimeout(() => {
7305
- if (!attemptFit(2)) {
7306
- setTimeout(() => {
7307
- if (!attemptFit(3)) {
7308
- console.error('[SSHIFT] All fit attempts failed');
7309
- }
7310
- }, 200);
7311
- }
7312
- }, 100);
7344
+
7345
+ // Also check if WebGL init was deferred due to hidden wrapper
7346
+ // (Bug 3) — the terminal may now be visible.
7347
+ if (session._webglInitPending && wrapper.classList.contains('active')) {
7348
+ session._webglInitPending = false;
7349
+ this._initWebGLAddon(session, true);
7313
7350
  }
7351
+
7352
+ requestAnimationFrame(() => {
7353
+ const fitted = this._fitTerminal(session);
7354
+ if (fitted) {
7355
+ console.log('[SSHIFT] Terminal fitted after fonts ready, cols:', terminal.cols, 'rows:', terminal.rows);
7356
+ } else {
7357
+ // If _fitTerminal couldn't fit (e.g. wrapper not visible yet),
7358
+ // it will have set needsResize = true so switchTab() picks it up.
7359
+ console.log('[SSHIFT] Could not fit after fonts ready — deferred to tab activation');
7360
+ }
7361
+ });
7314
7362
  });
7315
7363
 
7316
7364
  // Handle terminal input
@@ -7665,14 +7713,26 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
7665
7713
  // Clear the terminal to remove connecting messages
7666
7714
  session.terminal.clear();
7667
7715
 
7668
- // Send initial resize
7669
- if (session.fitAddon) {
7670
- this._fitTerminal(session);
7671
- this.socket.emit('ssh-resize', {
7672
- sessionId: data.sessionId,
7673
- cols: session.terminal.cols,
7674
- rows: session.terminal.rows
7675
- });
7716
+ // Send initial resize — but wait for fonts first so we send
7717
+ // the correct column/row count (Bug 1). If fonts haven't
7718
+ // loaded yet, the fallback font's narrower glyphs would make
7719
+ // fit() report far fewer cols than the container can actually
7720
+ // hold. The PTY on the server wraps at those bogus dimensions.
7721
+ const sendResize = () => {
7722
+ if (session.fitAddon) {
7723
+ this._fitTerminal(session);
7724
+ this.socket.emit('ssh-resize', {
7725
+ sessionId: data.sessionId,
7726
+ cols: session.terminal.cols,
7727
+ rows: session.terminal.rows
7728
+ });
7729
+ }
7730
+ };
7731
+
7732
+ if (document.fonts && document.fonts.status !== 'loaded') {
7733
+ document.fonts.ready.then(sendResize);
7734
+ } else {
7735
+ sendResize();
7676
7736
  }
7677
7737
 
7678
7738
  // Focus the terminal so user can type immediately
@@ -8641,10 +8701,23 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8641
8701
  // A second RAF ensures the paint is complete before we measure.
8642
8702
  requestAnimationFrame(() => {
8643
8703
  requestAnimationFrame(() => {
8704
+ // If WebGL init was deferred because this terminal was hidden
8705
+ // (Bug 3), initialise it now that the wrapper is visible.
8706
+ if (session._webglInitPending && this.webglRenderer && typeof window.WebglAddon === 'function') {
8707
+ session._webglInitPending = false;
8708
+ console.log('[SSHIFT] Initializing deferred WebGL for session:', sessionId);
8709
+ this._initWebGLAddon(session, true);
8710
+ }
8711
+
8712
+ // Always clear the WebGL texture atlas when a tab becomes visible.
8713
+ // While display:none, the terminal canvas has zero dimensions and
8714
+ // any cached glyphs are rendered at wrong colours/sizes (Bug 3).
8715
+ // Clearing + refreshing forces a clean redraw at the correct size.
8716
+ this._resetWebGLAtlas(session);
8717
+
8644
8718
  // Restore font size for this session
8645
8719
  if (session.fontSize && session.terminal) {
8646
8720
  session.terminal.options.fontSize = session.fontSize;
8647
- this._resetWebGLAtlas(session);
8648
8721
  console.log('[SSHIFT] Restored font size', session.fontSize, 'for session', sessionId);
8649
8722
  }
8650
8723
 
@@ -8674,10 +8747,11 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8674
8747
  });
8675
8748
  });
8676
8749
  } else if (session && session.terminal) {
8677
- // For non-controllers, just focus the terminal and restore font size
8750
+ // For non-controllers, just focus the terminal and restore font size.
8751
+ // Also clear the WebGL atlas since this tab was hidden.
8752
+ this._resetWebGLAtlas(session);
8678
8753
  if (session.fontSize) {
8679
8754
  session.terminal.options.fontSize = session.fontSize;
8680
- this._resetWebGLAtlas(session);
8681
8755
  }
8682
8756
  if (this.isMobile && session.mobileHandler && session.mobileHandler.hiddenTextarea) {
8683
8757
  session.mobileHandler._focusHiddenTextarea();