@lethevimlet/sshift 1.3.4 → 1.3.6

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 +225 -43
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lethevimlet/sshift",
3
- "version": "1.3.4",
3
+ "version": "1.3.6",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -980,6 +980,169 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
980
980
  // Don't persist - font size is session-only
981
981
  }
982
982
 
983
+ // Clear the WebGL glyph texture atlas and force a full repaint.
984
+ // The atlas caches rasterised glyphs; if it was built before the web font
985
+ // finished loading (or after a DPR / font-size change) those cached glyphs
986
+ // are blank or stale, producing the "black box" rendering artefacts.
987
+ // Clearing forces every cell to be re-rasterised with correct glyphs.
988
+ _resetWebGLAtlas(session) {
989
+ if (!session || !session.terminal) return;
990
+ if (session.webglAddon) {
991
+ try {
992
+ session.webglAddon.clearTextureAtlas();
993
+ } catch (_) {}
994
+ }
995
+ session.terminal.refresh(0, session.terminal.rows - 1);
996
+ }
997
+
998
+ // Initialize (or re-initialize) the WebGL renderer addon for a session.
999
+ // Handles context-loss tracking with a retry cap: after 3 losses the session
1000
+ // permanently falls back to the canvas renderer so we don't loop endlessly.
1001
+ // The `initialLoad` flag controls whether we wait for web fonts before
1002
+ // clearing the atlas (only needed on first creation, not on recreation).
1003
+ _initWebGLAddon(session, initialLoad = false) {
1004
+ if (!session || !session.terminal) return false;
1005
+ if (!this.webglRenderer || typeof window.WebglAddon !== 'function') return false;
1006
+
1007
+ if ((session.webglContextLossCount || 0) >= 3) {
1008
+ console.warn('[SSHIFT] Too many WebGL context losses for session, staying on canvas renderer');
1009
+ return false;
1010
+ }
1011
+
1012
+ if (session._webglInitPending) return false;
1013
+ session._webglInitPending = true;
1014
+
1015
+ if (session.webglAddon) {
1016
+ try { session.webglAddon.dispose(); } catch (_) {}
1017
+ session.webglAddon = null;
1018
+ }
1019
+
1020
+ try {
1021
+ const webglAddon = new window.WebglAddon();
1022
+
1023
+ webglAddon.onContextLoss(() => {
1024
+ console.warn('[SSHIFT] WebGL context lost');
1025
+ try { webglAddon.dispose(); } catch (_) {}
1026
+ session.webglAddon = null;
1027
+ session.webglContextLossCount = (session.webglContextLossCount || 0) + 1;
1028
+
1029
+ if (session.webglContextLossCount < 3) {
1030
+ console.log('[SSHIFT] Scheduling WebGL addon recreation via requestAnimationFrame');
1031
+ requestAnimationFrame(() => {
1032
+ if (session.terminal && !session.webglAddon && (session.webglContextLossCount || 0) < 3) {
1033
+ console.log('[SSHIFT] Attempting WebGL addon recreation after context loss');
1034
+ this._initWebGLAddon(session, false);
1035
+ if (session.fitAddon && !document.hidden) {
1036
+ try { session.fitAddon.fit(); } catch (_) {}
1037
+ }
1038
+ }
1039
+ });
1040
+ } else {
1041
+ console.warn('[SSHIFT] Repeated WebGL context loss, permanently falling back to canvas renderer');
1042
+ }
1043
+
1044
+ session.terminal.refresh(0, session.terminal.rows - 1);
1045
+ });
1046
+
1047
+ session.terminal.loadAddon(webglAddon);
1048
+ session.webglAddon = webglAddon;
1049
+
1050
+ const canvas = session.terminal.element?.querySelector('canvas');
1051
+ if (canvas) {
1052
+ const onCanvasContextLoss = () => {
1053
+ if (session.webglAddon) {
1054
+ console.warn('[SSHIFT] Raw canvas webglcontextlost event caught');
1055
+ try { session.webglAddon.dispose(); } catch (_) {}
1056
+ session.webglAddon = null;
1057
+ session.webglContextLossCount = (session.webglContextLossCount || 0) + 1;
1058
+ session.terminal.refresh(0, session.terminal.rows - 1);
1059
+ }
1060
+ canvas.removeEventListener('webglcontextlost', onCanvasContextLoss);
1061
+ };
1062
+ canvas.addEventListener('webglcontextlost', onCanvasContextLoss);
1063
+ }
1064
+
1065
+ session._webglInitPending = false;
1066
+ console.log('[SSHIFT] WebglAddon loaded');
1067
+
1068
+ if (initialLoad) {
1069
+ document.fonts.ready.then(() => {
1070
+ if (session.webglAddon) {
1071
+ try { session.webglAddon.clearTextureAtlas(); } catch (_) {}
1072
+ }
1073
+ if (session.terminal) {
1074
+ session.terminal.refresh(0, session.terminal.rows - 1);
1075
+ }
1076
+ console.log('[SSHIFT] WebGL atlas cleared after fonts ready');
1077
+ });
1078
+ } else {
1079
+ try { webglAddon.clearTextureAtlas(); } catch (_) {}
1080
+ session.terminal.refresh(0, session.terminal.rows - 1);
1081
+ if (session.fitAddon) {
1082
+ try { session.fitAddon.fit(); } catch (_) {}
1083
+ }
1084
+ }
1085
+
1086
+ return true;
1087
+ } catch (e) {
1088
+ session._webglInitPending = false;
1089
+ console.warn('[SSHIFT] Failed to load WebglAddon, falling back to canvas renderer:', e.message);
1090
+ return false;
1091
+ }
1092
+ }
1093
+
1094
+ // Called on visibilitychange (tab becomes visible). Browsers may reclaim
1095
+ // GPU memory from background tabs, which can invalidate the WebGL glyph
1096
+ // texture atlas or cause a full context loss. For sessions that still have
1097
+ // a WebGL addon we clear the stale atlas and force a repaint; for sessions
1098
+ // whose addon was lost while the tab was hidden we attempt to recreate it
1099
+ // (up to the 3-loss cap in _initWebGLAddon).
1100
+ // Wrapped in requestAnimationFrame to ensure layout is recalculated before
1101
+ // we touch the canvas — the container may have had zero dimensions while
1102
+ // the tab was hidden.
1103
+ _refreshAllWebGLSessions() {
1104
+ requestAnimationFrame(() => {
1105
+ const refresh = (session) => {
1106
+ if (!session || !session.terminal || !this.webglRenderer) return;
1107
+ if (session.webglAddon) {
1108
+ try { session.webglAddon.clearTextureAtlas(); } catch (_) {}
1109
+ session.terminal.refresh(0, session.terminal.rows - 1);
1110
+ } else if ((session.webglContextLossCount || 0) < 3 && typeof window.WebglAddon === 'function') {
1111
+ this._initWebGLAddon(session, false);
1112
+ if (session.fitAddon) {
1113
+ try { session.fitAddon.fit(); } catch (_) {}
1114
+ }
1115
+ }
1116
+ };
1117
+ this.sessions.forEach(refresh);
1118
+ this.sftpSessions.forEach(refresh);
1119
+ });
1120
+ }
1121
+
1122
+ // Watch for devicePixelRatio changes (zoom, monitor move, DPI switch).
1123
+ // When DPR changes the WebGL glyph atlas is built for the old pixel ratio
1124
+ // and every glyph becomes mis-scaled; clearing the atlas forces a rebuild.
1125
+ _setupDPRListener() {
1126
+ let currentDPR = window.devicePixelRatio;
1127
+ const query = matchMedia(`(resolution: ${currentDPR}dppx)`);
1128
+ const onChange = () => {
1129
+ const newDPR = window.devicePixelRatio;
1130
+ if (newDPR !== currentDPR) {
1131
+ console.log('[SSHIFT] devicePixelRatio changed from', currentDPR, 'to', newDPR);
1132
+ currentDPR = newDPR;
1133
+ this.sessions.forEach(session => this._resetWebGLAtlas(session));
1134
+ this.sftpSessions.forEach(session => this._resetWebGLAtlas(session));
1135
+ }
1136
+ // Re-register for the new DPR value
1137
+ this._setupDPRListener();
1138
+ };
1139
+ if (query.addEventListener) {
1140
+ query.addEventListener('change', onChange, { once: true });
1141
+ } else if (query.addListener) {
1142
+ query.addListener(onChange);
1143
+ }
1144
+ }
1145
+
983
1146
  // Robust terminal fit with validation and retry.
984
1147
  // Sometimes fit() is called before the browser finishes laying out a
985
1148
  // container that just became visible (display:none -> display:flex).
@@ -1027,6 +1190,11 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1027
1190
  }
1028
1191
 
1029
1192
  console.log('[SSHIFT] Terminal fitted, cols:', terminal.cols, 'rows:', terminal.rows);
1193
+
1194
+ // Resize changes cell metrics; clear the WebGL atlas so glyphs are
1195
+ // re-rasterised at the new size rather than stretched from the old cache.
1196
+ this._resetWebGLAtlas(session);
1197
+
1030
1198
  return true;
1031
1199
  }
1032
1200
 
@@ -1063,8 +1231,11 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1063
1231
 
1064
1232
  // Update the terminal
1065
1233
  session.terminal.options.fontSize = size;
1066
- session.terminal.refresh(0, session.terminal.rows - 1);
1067
-
1234
+
1235
+ // Font-size changes invalidate the glyph atlas — clear it so
1236
+ // glyphs are re-rasterised at the new size.
1237
+ this._resetWebGLAtlas(session);
1238
+
1068
1239
  if (session.fitAddon && session.isController) {
1069
1240
  this._fitTerminal(session);
1070
1241
  }
@@ -1092,7 +1263,7 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1092
1263
  this.sessions.forEach((session) => {
1093
1264
  if (session.terminal) {
1094
1265
  session.terminal.options.fontSize = size;
1095
- session.terminal.refresh(0, session.terminal.rows - 1);
1266
+ this._resetWebGLAtlas(session);
1096
1267
  if (session.fitAddon && session.isController) {
1097
1268
  this._fitTerminal(session);
1098
1269
  }
@@ -1105,7 +1276,7 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1105
1276
  this.sftpSessions.forEach((session) => {
1106
1277
  if (session.terminal) {
1107
1278
  session.terminal.options.fontSize = size;
1108
- session.terminal.refresh(0, session.terminal.rows - 1);
1279
+ this._resetWebGLAtlas(session);
1109
1280
  if (session.fitAddon) {
1110
1281
  this._fitTerminal(session);
1111
1282
  }
@@ -1299,8 +1470,7 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1299
1470
  const newTheme = this.getTerminalTheme(theme);
1300
1471
  console.log('[SSHIFT] Applying theme to terminal:', newTheme);
1301
1472
  session.terminal.options.theme = newTheme;
1302
- // Force terminal to redraw with new theme
1303
- session.terminal.refresh(0, session.terminal.rows - 1);
1473
+ this._resetWebGLAtlas(session);
1304
1474
 
1305
1475
  // Update wrapper background to match terminal background
1306
1476
  const wrapper = document.getElementById(`terminal-wrapper-${session.id}`);
@@ -1321,8 +1491,7 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1321
1491
  if (session.terminal) {
1322
1492
  const newTheme = this.getTerminalTheme(theme);
1323
1493
  session.terminal.options.theme = newTheme;
1324
- // Force terminal to redraw with new theme
1325
- session.terminal.refresh(0, session.terminal.rows - 1);
1494
+ this._resetWebGLAtlas(session);
1326
1495
 
1327
1496
  // Update wrapper background to match terminal background
1328
1497
  const wrapper = document.getElementById(`terminal-wrapper-${session.id}`);
@@ -2306,7 +2475,7 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
2306
2475
  // Set font size for this SFTP session
2307
2476
  sftpSession.fontSize = Math.max(this.minFontSize, Math.min(this.maxFontSize, newSize));
2308
2477
  sftpSession.terminal.options.fontSize = sftpSession.fontSize;
2309
- sftpSession.terminal.refresh(0, sftpSession.terminal.rows - 1);
2478
+ this._resetWebGLAtlas(sftpSession);
2310
2479
 
2311
2480
  if (sftpSession.fitAddon) {
2312
2481
  this._fitTerminal(sftpSession);
@@ -2430,6 +2599,12 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
2430
2599
  // Fix mobile viewport height issues
2431
2600
  this.fixMobileViewport();
2432
2601
 
2602
+ // Listen for devicePixelRatio changes (browser zoom, monitor move, etc.).
2603
+ // DPR changes invalidate the WebGL glyph atlas because cell metrics are
2604
+ // derived from CSS pixels * DPR — a different DPR means every cached
2605
+ // glyph is now the wrong size and must be re-rasterised.
2606
+ this._setupDPRListener();
2607
+
2433
2608
  // Load sticky config first
2434
2609
  await this.loadStickyConfig();
2435
2610
 
@@ -3493,12 +3668,16 @@ const wheelHandler = (e) => {
3493
3668
  }
3494
3669
  });
3495
3670
 
3496
- // When the tab becomes visible again, immediately try to reconnect if disconnected
3671
+ // When the tab becomes visible again, reconnect the socket if needed
3672
+ // and refresh WebGL terminal renderers (the browser may reclaim GPU
3673
+ // memory from background tabs, invalidating the glyph texture atlas).
3497
3674
  document.addEventListener('visibilitychange', () => {
3498
- if (!document.hidden && !this.socket.connected) {
3675
+ if (document.hidden) return;
3676
+ if (!this.socket.connected) {
3499
3677
  console.log('[SSHIFT] Tab visible and socket disconnected, triggering reconnect');
3500
3678
  this.socket.connect();
3501
3679
  }
3680
+ this._refreshAllWebGLSessions();
3502
3681
  });
3503
3682
 
3504
3683
  this.socket.on('connect_error', (error) => {
@@ -6535,19 +6714,22 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
6535
6714
  type: 'ssh',
6536
6715
  terminal: null,
6537
6716
  fitAddon: null,
6717
+ webglAddon: null,
6718
+ webglContextLossCount: 0,
6719
+ _webglInitPending: false,
6538
6720
  connecting: true,
6539
6721
  connected: false,
6540
- connectionData, // Store for sticky sessions
6541
- isRestoring: !!restoreSessionId, // Flag to indicate if this is a restored session
6542
- isAtBottom: true, // Auto-scroll by default when new data arrives
6543
- isController: false, // Default to observer - will be set to true for session creator or when taking control
6544
- syncing: false, // Flag to prevent data writes during screen sync
6545
- fontSize: this.terminalFontSize, // Initialize with default font size
6546
- mobileHandler: null, // Mobile terminal handler for touch interactions
6547
- writeChunks: [], // Array of chunks for batching terminal writes (avoids O(n^2) string concat)
6548
- writeRAF: null, // requestAnimationFrame ID for write flush
6549
- flushRemaining: null, // Remaining data from a frame that exceeded the write cap
6550
- originalScrollback: null, // Stores original scrollback before dynamic reduction
6722
+ connectionData,
6723
+ isRestoring: !!restoreSessionId,
6724
+ isAtBottom: true,
6725
+ isController: false,
6726
+ syncing: false,
6727
+ fontSize: this.terminalFontSize,
6728
+ mobileHandler: null,
6729
+ writeChunks: [],
6730
+ writeRAF: null,
6731
+ flushRemaining: null,
6732
+ originalScrollback: null,
6551
6733
  scrollbackRestoreTimer: null
6552
6734
  });
6553
6735
 
@@ -7006,23 +7188,7 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
7006
7188
  terminal.open(container);
7007
7189
 
7008
7190
  // Load WebGL renderer addon if enabled and available
7009
- if (this.webglRenderer && typeof window.WebglAddon === 'function') {
7010
- try {
7011
- const webglAddon = new window.WebglAddon();
7012
- webglAddon.onContextLoss(() => {
7013
- console.warn('[SSHIFT] WebGL context lost, disposing addon');
7014
- webglAddon.dispose();
7015
- });
7016
- terminal.loadAddon(webglAddon);
7017
- console.log('[SSHIFT] WebglAddon loaded');
7018
- } catch (e) {
7019
- console.warn('[SSHIFT] Failed to load WebglAddon, falling back to canvas renderer:', e.message);
7020
- }
7021
- } else if (this.webglRenderer) {
7022
- console.warn('[SSHIFT] WebglAddon not available (window.WebglAddon type:', typeof window.WebglAddon, '), using canvas renderer');
7023
- } else {
7024
- console.log('[SSHIFT] WebGL renderer disabled in settings, using canvas renderer');
7025
- }
7191
+ this._initWebGLAddon(session, true);
7026
7192
 
7027
7193
  // Load image addon if enabled and available (Sixel/iTerm2/Kitty image protocols)
7028
7194
  if (this.imageAddonEnabled && typeof window.ImageAddon === 'function') {
@@ -7291,6 +7457,9 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
7291
7457
  this.showToast('Failed to copy', 'error');
7292
7458
  }
7293
7459
  });
7460
+ if (session.webglAddon) {
7461
+ terminal.refresh(0, terminal.rows - 1);
7462
+ }
7294
7463
  return false; // Prevent default behavior
7295
7464
  }
7296
7465
  }
@@ -7325,6 +7494,9 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
7325
7494
  this.showToast('Failed to copy', 'error');
7326
7495
  }
7327
7496
  });
7497
+ if (session.webglAddon) {
7498
+ terminal.refresh(0, terminal.rows - 1);
7499
+ }
7328
7500
  return false;
7329
7501
  }
7330
7502
  }
@@ -7383,6 +7555,17 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
7383
7555
  this.showTerminalContextMenu(sessionId, terminal, e);
7384
7556
  });
7385
7557
 
7558
+ // When the selection is cleared (e.g. after copy / deselect) the
7559
+ // WebGL renderer can leave behind blank cells because the texture atlas
7560
+ // cached the "selected" version of the glyph and never re-rasterised
7561
+ // the deselected state. Force a full repaint on every selection change
7562
+ // so deselected cells are always redrawn with the correct glyph.
7563
+ terminal.onSelectionChange(() => {
7564
+ if (session.webglAddon) {
7565
+ session.terminal.refresh(0, session.terminal.rows - 1);
7566
+ }
7567
+ });
7568
+
7386
7569
  session.terminal = terminal;
7387
7570
  session.fitAddon = fitAddon;
7388
7571
 
@@ -8461,7 +8644,7 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8461
8644
  // Restore font size for this session
8462
8645
  if (session.fontSize && session.terminal) {
8463
8646
  session.terminal.options.fontSize = session.fontSize;
8464
- session.terminal.refresh(0, session.terminal.rows - 1);
8647
+ this._resetWebGLAtlas(session);
8465
8648
  console.log('[SSHIFT] Restored font size', session.fontSize, 'for session', sessionId);
8466
8649
  }
8467
8650
 
@@ -8494,7 +8677,7 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8494
8677
  // For non-controllers, just focus the terminal and restore font size
8495
8678
  if (session.fontSize) {
8496
8679
  session.terminal.options.fontSize = session.fontSize;
8497
- session.terminal.refresh(0, session.terminal.rows - 1);
8680
+ this._resetWebGLAtlas(session);
8498
8681
  }
8499
8682
  if (this.isMobile && session.mobileHandler && session.mobileHandler.hiddenTextarea) {
8500
8683
  session.mobileHandler._focusHiddenTextarea();
@@ -8506,10 +8689,9 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8506
8689
  // Handle SFTP sessions
8507
8690
  const sftpSession = this.sftpSessions.get(sessionId);
8508
8691
  if (sftpSession && sftpSession.terminal) {
8509
- // Restore font size for SFTP session
8510
8692
  if (sftpSession.fontSize) {
8511
8693
  sftpSession.terminal.options.fontSize = sftpSession.fontSize;
8512
- sftpSession.terminal.refresh(0, sftpSession.terminal.rows - 1);
8694
+ this._resetWebGLAtlas(sftpSession);
8513
8695
  }
8514
8696
  }
8515
8697