@lethevimlet/sshift 1.0.3 → 1.0.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.
@@ -33,6 +33,7 @@ class SSHIFTClient {
33
33
  this.isSyncingTabs = false; // Flag to prevent saveTabs emission during tabs-sync
34
34
  this.sftpClipboard = null; // For cut/copy/paste: { action: 'cut'|'copy', path: string, name: string, sessionId: string }
35
35
  this.terminalClipboardContent = null; // Pre-read clipboard content for context menu paste
36
+ this.osc52Buffer = null; // Buffered OSC 52 clipboard content pending write
36
37
  this.terminalColorOverride = true; // Default to true, will be loaded in initThemeAndAccent
37
38
  this.terminalBgColor = '#0d1117';
38
39
  this.terminalFgColor = '#e6edf3';
@@ -71,7 +72,7 @@ class SSHIFTClient {
71
72
  };
72
73
 
73
74
  // Terminal font size (for pinch-to-zoom on mobile)
74
- this.terminalFontSize = 14; // Default font size
75
+ this.terminalFontSize = window.innerWidth <= 768 ? 11 : 14;
75
76
  this.minFontSize = 8; // Minimum font size
76
77
  this.maxFontSize = 32; // Maximum font size
77
78
 
@@ -713,28 +714,37 @@ class SSHIFTClient {
713
714
 
714
715
  // Helper method to copy text to clipboard with fallback
715
716
  async copyToClipboard(text) {
716
- try {
717
- // Try modern clipboard API first
718
- if (navigator.clipboard && navigator.clipboard.writeText) {
717
+ // Try modern clipboard API first (requires secure context + user gesture)
718
+ if (navigator.clipboard && navigator.clipboard.writeText) {
719
+ try {
719
720
  await navigator.clipboard.writeText(text);
721
+ console.log('[SSHIFT] Clipboard API write succeeded, length:', text.length);
720
722
  return true;
723
+ } catch (err) {
724
+ console.warn('[SSHIFT] Clipboard API write failed:', err.name, err.message);
721
725
  }
722
- } catch (err) {
723
- console.warn('[SSHIFT] Modern clipboard API failed, trying fallback:', err);
726
+ } else {
727
+ console.warn('[SSHIFT] Clipboard API not available');
724
728
  }
725
729
 
726
- // Fallback to execCommand
730
+ // Fallback to execCommand (also requires user gesture in modern browsers)
727
731
  try {
728
732
  const textarea = document.createElement('textarea');
729
733
  textarea.value = text;
730
734
  textarea.style.position = 'fixed';
731
735
  textarea.style.left = '-9999px';
732
736
  textarea.style.top = '-9999px';
737
+ textarea.style.opacity = '0';
733
738
  document.body.appendChild(textarea);
734
739
  textarea.focus();
735
740
  textarea.select();
736
741
  const success = document.execCommand('copy');
737
742
  document.body.removeChild(textarea);
743
+ if (success) {
744
+ console.log('[SSHIFT] execCommand copy succeeded, length:', text.length);
745
+ } else {
746
+ console.warn('[SSHIFT] execCommand copy returned false');
747
+ }
738
748
  return success;
739
749
  } catch (err) {
740
750
  console.error('[SSHIFT] Fallback clipboard copy failed:', err);
@@ -903,7 +913,7 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
903
913
  // Terminal Font Size Management (for pinch-to-zoom and buttons)
904
914
  loadTerminalFontSize() {
905
915
  // Don't persist - always start with default
906
- return 14; // Default font size
916
+ return window.innerWidth <= 768 ? 11 : 14;
907
917
  }
908
918
 
909
919
  saveTerminalFontSize(size) {
@@ -2437,6 +2447,8 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
2437
2447
  // If we switched between mobile and desktop, re-apply the layout
2438
2448
  if (wasMobile !== this.isMobile && this.currentLayout) {
2439
2449
  console.log('[SSHIFT] Viewport changed, re-applying layout for', this.isMobile ? 'mobile' : 'desktop');
2450
+ this.terminalFontSize = this.isMobile ? 11 : 14;
2451
+ this.setTerminalFontSize(this.terminalFontSize);
2440
2452
  this.applyLayout(this.currentLayout);
2441
2453
  this.updateMobileTabsDropdown();
2442
2454
  }
@@ -2644,7 +2656,7 @@ const wheelHandler = (e) => {
2644
2656
  // ydisp is the current scroll position (number of lines scrolled back from bottom)
2645
2657
  // The terminal is at bottom if ydisp is 0 or very close to 0
2646
2658
  const maxYDisp = buffer.length - terminal.rows;
2647
- const currentYDisp = terminal.buffer.ydisp;
2659
+ const currentYDisp = terminal.buffer.active.viewportY;
2648
2660
 
2649
2661
  // We're at the bottom if we're within 2 lines of the bottom
2650
2662
  const isAtBottom = currentYDisp >= maxYDisp - 2;
@@ -2668,7 +2680,7 @@ const wheelHandler = (e) => {
2668
2680
  // ydisp = 0 means we're at the bottom (most recent content)
2669
2681
  // ydisp > 0 means we've scrolled up into the scrollback buffer (older content)
2670
2682
  const buffer = terminal.buffer.active;
2671
- const scrollTop = buffer.ydisp;
2683
+ const scrollTop = buffer.viewportY;
2672
2684
 
2673
2685
  // Get scroll direction
2674
2686
  const scrollDiff = scrollTop - session.lastScrollTop;
@@ -3423,6 +3435,14 @@ const wheelHandler = (e) => {
3423
3435
  this.handleTabClosed(data.sessionId);
3424
3436
  });
3425
3437
 
3438
+ this.socket.on('sessions-updated', () => {
3439
+ const modal = document.getElementById('manageSessionsModal');
3440
+ if (modal && modal.classList.contains('active')) {
3441
+ if (this._sessionsUpdateTimer) clearTimeout(this._sessionsUpdateTimer);
3442
+ this._sessionsUpdateTimer = setTimeout(() => this.loadSessions(), 300);
3443
+ }
3444
+ });
3445
+
3426
3446
  // Handle tab order update from server
3427
3447
  this.socket.on('tab-order', (data) => {
3428
3448
  console.log('[SSHIFT] Tab order updated:', data.order);
@@ -4209,6 +4229,9 @@ const wheelHandler = (e) => {
4209
4229
  // Initialize password toggle
4210
4230
  this.initPasswordToggle();
4211
4231
 
4232
+ // Initialize debug info dialog
4233
+ this.initDebugInfoDialog();
4234
+
4212
4235
  // Initialize sessions modal handlers
4213
4236
  this.initSessionsModalHandlers();
4214
4237
 
@@ -4806,8 +4829,24 @@ const wheelHandler = (e) => {
4806
4829
  }
4807
4830
 
4808
4831
  const downloadCertBtn = document.getElementById('downloadCertBtn');
4832
+ const customCertWarning = document.getElementById('customCertWarning');
4833
+
4834
+ fetch('/api/security-info').then(r => r.json()).then(info => {
4835
+ if (info.usesCustomCert) {
4836
+ if (downloadCertBtn) {
4837
+ downloadCertBtn.disabled = true;
4838
+ downloadCertBtn.style.opacity = '0.5';
4839
+ downloadCertBtn.style.cursor = 'not-allowed';
4840
+ }
4841
+ if (customCertWarning) {
4842
+ customCertWarning.style.display = 'flex';
4843
+ }
4844
+ }
4845
+ }).catch(() => {});
4846
+
4809
4847
  if (downloadCertBtn) {
4810
4848
  downloadCertBtn.addEventListener('click', () => {
4849
+ if (downloadCertBtn.disabled) return;
4811
4850
  window.open('/api/cert', '_blank');
4812
4851
  });
4813
4852
  }
@@ -4839,26 +4878,69 @@ const wheelHandler = (e) => {
4839
4878
  }
4840
4879
 
4841
4880
  openSecurityInfoDialog() {
4881
+ const swStatusIcon = document.getElementById('swStatusIcon');
4882
+ const swStatusTitle = document.getElementById('swStatusTitle');
4842
4883
  const swStatusText = document.getElementById('swStatusText');
4843
- // Re-check actual SW state from navigator API
4884
+ const swStatusContainer = document.getElementById('swStatusContainer');
4885
+
4844
4886
  let swStatus = window._swStatus;
4845
4887
  if (navigator.serviceWorker && navigator.serviceWorker.controller) {
4846
4888
  swStatus = 'active';
4847
4889
  window._swStatus = 'active';
4848
4890
  }
4849
4891
 
4850
- if (swStatusText) {
4892
+ if (swStatusIcon && swStatusTitle && swStatusText && swStatusContainer) {
4893
+ swStatusContainer.classList.remove('sw-status-active', 'sw-status-failed', 'sw-status-unsupported');
4894
+
4851
4895
  if (swStatus === 'active') {
4852
- swStatusText.innerHTML = '<i class="fas fa-check-circle" style="color: var(--accent-success, #4caf50);"></i> Service Worker is <strong>active</strong>. Offline caching is available.';
4896
+ swStatusContainer.classList.add('sw-status-active');
4897
+ swStatusIcon.innerHTML = '<i class="fas fa-check-circle"></i>';
4898
+ swStatusTitle.textContent = 'Connected';
4899
+ swStatusText.innerHTML =
4900
+ '<ul class="sw-feature-list">' +
4901
+ '<li><i class="fas fa-check"></i> No browser security warnings</li>' +
4902
+ '<li><i class="fas fa-check"></i> Installable as app (PWA)</li>' +
4903
+ '<li><i class="fas fa-check"></i> Clipboard API access</li>' +
4904
+ '</ul>';
4853
4905
  } else if (swStatus === 'failed') {
4854
- swStatusText.innerHTML = '<i class="fas fa-exclamation-triangle" style="color: var(--accent-danger, #f44336);"></i> Service Worker <strong>failed to register</strong>: ' + (window._swError || 'Unknown error') + '. This is usually caused by an untrusted HTTPS certificate. Download and install the certificate below to fix this, then reload the page.';
4906
+ swStatusContainer.classList.add('sw-status-failed');
4907
+ swStatusIcon.innerHTML = '<i class="fas fa-exclamation-triangle"></i>';
4908
+ swStatusTitle.textContent = 'Connection Failed';
4909
+ swStatusText.innerHTML =
4910
+ '<p>' + (window._swError || 'Unknown error') + '</p>' +
4911
+ '<ul class="sw-feature-list">' +
4912
+ '<li><i class="fas fa-times"></i> No browser security warnings</li>' +
4913
+ '<li><i class="fas fa-times"></i> Installable as app (PWA)</li>' +
4914
+ '<li><i class="fas fa-times"></i> Clipboard API access</li>' +
4915
+ '</ul>' +
4916
+ '<p class="sw-hint">Trust the HTTPS certificate to enable all features.</p>';
4855
4917
  } else if (swStatus === 'redundant') {
4856
- swStatusText.innerHTML = '<i class="fas fa-exclamation-triangle" style="color: var(--accent-danger, #f44336);"></i> Service Worker became <strong>redundant</strong>. ' + (window._swError || 'The HTTPS certificate may not be trusted.') + ' Download and install the certificate below, then reload the page.';
4918
+ swStatusContainer.classList.add('sw-status-failed');
4919
+ swStatusIcon.innerHTML = '<i class="fas fa-exclamation-triangle"></i>';
4920
+ swStatusTitle.textContent = 'Connection Failed';
4921
+ swStatusText.innerHTML =
4922
+ '<p>' + (window._swError || 'The HTTPS certificate may not be trusted.') + '</p>' +
4923
+ '<ul class="sw-feature-list">' +
4924
+ '<li><i class="fas fa-times"></i> No browser security warnings</li>' +
4925
+ '<li><i class="fas fa-times"></i> Installable as app (PWA)</li>' +
4926
+ '<li><i class="fas fa-times"></i> Clipboard API access</li>' +
4927
+ '</ul>' +
4928
+ '<p class="sw-hint">Trust the HTTPS certificate to enable all features.</p>';
4857
4929
  } else if (swStatus === 'unsupported') {
4858
- swStatusText.innerHTML = '<i class="fas fa-info-circle" style="color: var(--accent-warning, #ff9800);"></i> Service Workers are <strong>not supported</strong> in this browser.';
4930
+ swStatusContainer.classList.add('sw-status-unsupported');
4931
+ swStatusIcon.innerHTML = '<i class="fas fa-info-circle"></i>';
4932
+ swStatusTitle.textContent = 'Not Supported';
4933
+ swStatusText.innerHTML =
4934
+ '<p>Service Workers are not available in this browser.</p>' +
4935
+ '<ul class="sw-feature-list">' +
4936
+ '<li><i class="fas fa-times"></i> No browser security warnings</li>' +
4937
+ '<li><i class="fas fa-times"></i> Installable as app (PWA)</li>' +
4938
+ '<li><i class="fas fa-times"></i> Clipboard API access</li>' +
4939
+ '</ul>';
4859
4940
  } else {
4860
- // Still loading, retry
4861
- swStatusText.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Checking...';
4941
+ swStatusIcon.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
4942
+ swStatusTitle.textContent = 'Checking...';
4943
+ swStatusText.textContent = 'Detecting service worker status';
4862
4944
  setTimeout(() => this.openSecurityInfoDialog(), 500);
4863
4945
  return;
4864
4946
  }
@@ -4867,6 +4949,63 @@ const wheelHandler = (e) => {
4867
4949
  this.openModal('securityInfoModal');
4868
4950
  }
4869
4951
 
4952
+ // Debug Info Dialog
4953
+ initDebugInfoDialog() {
4954
+ const showBtn = document.getElementById('showDebugInfoBtn');
4955
+ if (showBtn) {
4956
+ showBtn.addEventListener('click', () => {
4957
+ this.openDebugInfoDialog();
4958
+ });
4959
+ }
4960
+
4961
+ const closeBtn = document.getElementById('closeDebugInfoModal');
4962
+ if (closeBtn) {
4963
+ closeBtn.addEventListener('click', () => {
4964
+ this.closeModal('debugInfoModal');
4965
+ });
4966
+ }
4967
+
4968
+ const closeBtn2 = document.getElementById('closeDebugInfoBtn');
4969
+ if (closeBtn2) {
4970
+ closeBtn2.addEventListener('click', () => {
4971
+ this.closeModal('debugInfoModal');
4972
+ });
4973
+ }
4974
+
4975
+ const modal = document.getElementById('debugInfoModal');
4976
+ if (modal) {
4977
+ modal.addEventListener('click', (e) => {
4978
+ if (e.target === modal) {
4979
+ this.closeModal('debugInfoModal');
4980
+ }
4981
+ });
4982
+ }
4983
+ }
4984
+
4985
+ openDebugInfoDialog() {
4986
+ fetch('/api/debug-info').then(r => r.json()).then(info => {
4987
+ const configPathEl = document.getElementById('debugConfigPath');
4988
+ const dataDirEl = document.getElementById('debugDataDir');
4989
+ const certPathEl = document.getElementById('debugCertPath');
4990
+ const keyPathEl = document.getElementById('debugKeyPath');
4991
+ const certTypeEl = document.getElementById('debugCertType');
4992
+
4993
+ if (configPathEl) configPathEl.textContent = info.configPath || 'N/A';
4994
+ if (dataDirEl) dataDirEl.textContent = info.dataDir || 'N/A';
4995
+ if (certPathEl) certPathEl.textContent = info.certPath || 'N/A';
4996
+ if (keyPathEl) keyPathEl.textContent = info.keyPath || 'N/A';
4997
+ if (certTypeEl) certTypeEl.textContent = info.usesCustomCert ? 'Custom' : 'Self-signed';
4998
+ }).catch(() => {
4999
+ const ids = ['debugConfigPath', 'debugDataDir', 'debugCertPath', 'debugKeyPath', 'debugCertType'];
5000
+ ids.forEach(id => {
5001
+ const el = document.getElementById(id);
5002
+ if (el) el.textContent = 'Error loading';
5003
+ });
5004
+ });
5005
+
5006
+ this.openModal('debugInfoModal');
5007
+ }
5008
+
4870
5009
  // Sessions Modal
4871
5010
  async openSessionsModal() {
4872
5011
  this.openModal('manageSessionsModal');
@@ -4976,7 +5115,6 @@ const wheelHandler = (e) => {
4976
5115
  }
4977
5116
 
4978
5117
  initSessionsModalHandlers() {
4979
- // Close button
4980
5118
  const closeBtn = document.getElementById('closeManageSessionsModal');
4981
5119
  if (closeBtn) {
4982
5120
  closeBtn.addEventListener('click', () => {
@@ -4984,7 +5122,13 @@ const wheelHandler = (e) => {
4984
5122
  });
4985
5123
  }
4986
5124
 
4987
- // Close all button
5125
+ const closeFooterBtn = document.getElementById('closeManageSessions');
5126
+ if (closeFooterBtn) {
5127
+ closeFooterBtn.addEventListener('click', () => {
5128
+ this.closeModal('manageSessionsModal');
5129
+ });
5130
+ }
5131
+
4988
5132
  const closeAllBtn = document.getElementById('closeAllSessions');
4989
5133
  if (closeAllBtn) {
4990
5134
  closeAllBtn.addEventListener('click', () => {
@@ -6415,6 +6559,70 @@ const wheelHandler = (e) => {
6415
6559
  console.log('[SSHIFT] Image addon disabled in settings');
6416
6560
  }
6417
6561
 
6562
+ // Register OSC 52 handler for clipboard operations from remote terminals
6563
+ // This allows programs like opencode, tmux, vim, etc. to set the clipboard
6564
+ // via the OSC 52 escape sequence: \e]52;c;<base64-text>\007
6565
+ //
6566
+ // Browser Clipboard API requires a user gesture, but OSC 52 data arrives
6567
+ // asynchronously from the SSH stream. We buffer the content synchronously
6568
+ // and flush it on the next user interaction (click, keydown, touchstart).
6569
+ // Immediate writes are also attempted but may fail without a user gesture.
6570
+ try {
6571
+ const flushOsc52Buffer = () => {
6572
+ if (this.osc52Buffer !== null && this.osc52Buffer !== undefined) {
6573
+ const text = this.osc52Buffer;
6574
+ this.osc52Buffer = null;
6575
+ console.log('[SSHIFT] Flushing OSC 52 clipboard buffer, length:', text.length);
6576
+ this.copyToClipboard(text).then(success => {
6577
+ if (success) {
6578
+ console.log('[SSHIFT] OSC 52 clipboard write succeeded (flushed on user gesture)');
6579
+ this.showToast('Copied to clipboard', 'success');
6580
+ } else {
6581
+ console.warn('[SSHIFT] OSC 52 clipboard write failed after flush, re-buffering');
6582
+ this.osc52Buffer = text;
6583
+ }
6584
+ }).catch(err => {
6585
+ console.warn('[SSHIFT] OSC 52 clipboard write failed:', err);
6586
+ this.osc52Buffer = text;
6587
+ });
6588
+ }
6589
+ };
6590
+ document.addEventListener('click', flushOsc52Buffer, true);
6591
+ document.addEventListener('keydown', flushOsc52Buffer, true);
6592
+ document.addEventListener('touchstart', flushOsc52Buffer, true);
6593
+
6594
+ // Store for cleanup when the session is closed
6595
+ const sess = this.sessions.get(sessionId);
6596
+ if (sess) sess.osc52FlushListener = flushOsc52Buffer;
6597
+
6598
+ // Also register as fallback with xterm.js's OSC handler in case any
6599
+ // OSC 52 sequences slip through the data stream interceptor (e.g. from
6600
+ // screen sync restoration). The data stream interceptor is the primary handler.
6601
+ terminal.registerOscHandler(52, (data) => {
6602
+ console.log('[SSHIFT] OSC 52 handler fallback triggered (should be handled by stream interceptor)');
6603
+ const semicolonIndex = data.indexOf(';');
6604
+ if (semicolonIndex === -1) return true;
6605
+ const content = data.substring(semicolonIndex + 1);
6606
+ if (!content) {
6607
+ this.osc52Buffer = '';
6608
+ return true;
6609
+ }
6610
+ try {
6611
+ const decoded = atob(content);
6612
+ this.osc52Buffer = decoded;
6613
+ this.copyToClipboard(decoded).then(success => {
6614
+ if (success) this.osc52Buffer = null;
6615
+ }).catch(() => {});
6616
+ } catch (e) {
6617
+ console.warn('[SSHIFT] OSC 52 base64 decode failed:', e);
6618
+ }
6619
+ return true;
6620
+ });
6621
+ console.log('[SSHIFT] OSC 52 clipboard handler registered');
6622
+ } catch (e) {
6623
+ console.warn('[SSHIFT] Failed to register OSC 52 handler:', e.message);
6624
+ }
6625
+
6418
6626
  // Enable cursor blink after addons are loaded to avoid
6419
6627
  // repaint interference during initial render
6420
6628
  terminal.options.cursorBlink = true;
@@ -6609,6 +6817,7 @@ const wheelHandler = (e) => {
6609
6817
 
6610
6818
  // Ctrl+V - Paste
6611
6819
  if (event.ctrlKey && event.key === 'v' && event.type === 'keydown') {
6820
+ event.preventDefault(); // Prevent browser paste event to avoid double-paste
6612
6821
  this.readFromClipboard().then(text => {
6613
6822
  if (text) {
6614
6823
  const sess = this.sessions.get(sessionId);
@@ -6620,7 +6829,7 @@ const wheelHandler = (e) => {
6620
6829
  this.showToast('Clipboard is empty', 'info');
6621
6830
  }
6622
6831
  });
6623
- return false; // Prevent default behavior
6832
+ return false; // Prevent xterm key handling
6624
6833
  }
6625
6834
 
6626
6835
  // Ctrl+Shift+C - Force copy
@@ -6642,6 +6851,7 @@ const wheelHandler = (e) => {
6642
6851
 
6643
6852
  // Ctrl+Shift+V - Force paste
6644
6853
  if (event.ctrlKey && event.shiftKey && event.key === 'V') {
6854
+ event.preventDefault(); // Prevent browser paste event to avoid double-paste
6645
6855
  this.readFromClipboard().then(text => {
6646
6856
  if (text) {
6647
6857
  const sess = this.sessions.get(sessionId);
@@ -6693,29 +6903,6 @@ const wheelHandler = (e) => {
6693
6903
  this.showTerminalContextMenu(sessionId, terminal, e);
6694
6904
  });
6695
6905
 
6696
- // Handle native paste event from browser
6697
- container.addEventListener('paste', (e) => {
6698
- console.log('[SSHIFT] Native paste event triggered');
6699
- e.preventDefault();
6700
-
6701
- // Get pasted data from clipboard
6702
- let pastedText = '';
6703
- if (e.clipboardData && e.clipboardData.getData) {
6704
- pastedText = e.clipboardData.getData('text/plain');
6705
- }
6706
-
6707
- if (pastedText) {
6708
- console.log('[SSHIFT] Pasted text length:', pastedText.length);
6709
- const sess = this.sessions.get(sessionId);
6710
- if (sess && sess.connected) {
6711
- this.sendChunkedInput(sessionId, pastedText);
6712
- this.showToast('Pasted from clipboard', 'success');
6713
- }
6714
- } else {
6715
- this.showToast('Clipboard is empty', 'info');
6716
- }
6717
- });
6718
-
6719
6906
  session.terminal = terminal;
6720
6907
  session.fitAddon = fitAddon;
6721
6908
 
@@ -6875,6 +7062,119 @@ const wheelHandler = (e) => {
6875
7062
  }
6876
7063
  }
6877
7064
 
7065
+ // Parse and handle OSC 52 clipboard sequences from terminal data stream.
7066
+ // Extracts clipboard content, writes it to browser clipboard, and safely
7067
+ // handles sequences that span across data chunks.
7068
+ // Also handles tmux DCS passthrough: \x1bPtmux;\x1b\x1b]52;c;...\x07\x1b\\
7069
+ _handleOsc52(data) {
7070
+ if (!data || data.indexOf('52;') === -1) return data;
7071
+
7072
+ let result = data;
7073
+
7074
+ // Handle tmux/screen DCS passthrough wrapper:
7075
+ // \x1bPtmux;\x1b wraps an inner escape sequence, terminated by \x1b\\
7076
+ // \x1bP\x1b (screen) similarly
7077
+ // We unwrap these by extracting the inner OSC 52 sequence.
7078
+ while (true) {
7079
+ const tmuxStart = result.indexOf('\x1bPtmux;\x1b');
7080
+ const screenStart = result.indexOf('\x1bP\x1b');
7081
+ let dcsStart = -1;
7082
+ let dcsPrefixLen = 0;
7083
+
7084
+ if (tmuxStart !== -1 && (screenStart === -1 || tmuxStart < screenStart)) {
7085
+ dcsStart = tmuxStart;
7086
+ dcsPrefixLen = '\x1bPtmux;\x1b'.length;
7087
+ } else if (screenStart !== -1) {
7088
+ dcsStart = screenStart;
7089
+ dcsPrefixLen = '\x1bP\x1b'.length;
7090
+ }
7091
+
7092
+ if (dcsStart !== -1) {
7093
+ // Find the DCS terminator: \x1b\\ (ST)
7094
+ const dcsEnd = result.indexOf('\x1b\\', dcsStart + dcsPrefixLen);
7095
+ if (dcsEnd !== -1) {
7096
+ // Extract the inner sequence and replace the whole DCS block with just the inner sequence
7097
+ const inner = result.substring(dcsStart + dcsPrefixLen, dcsEnd);
7098
+ result = result.substring(0, dcsStart) + inner + result.substring(dcsEnd + 2);
7099
+ } else {
7100
+ break; // Incomplete DCS, leave for next chunk
7101
+ }
7102
+ } else {
7103
+ break;
7104
+ }
7105
+ }
7106
+
7107
+ // Now process plain OSC 52 sequences
7108
+ if (result.indexOf('\x1b]52;') === -1) return result;
7109
+
7110
+ let searchFrom = 0;
7111
+
7112
+ while (true) {
7113
+ const startIdx = result.indexOf('\x1b]52;', searchFrom);
7114
+ if (startIdx === -1) break;
7115
+
7116
+ // Find end of OSC sequence: BEL (\x07) or ST (\x1b\\)
7117
+ let endIdx = -1;
7118
+ let endLen = 1;
7119
+ const belIdx = result.indexOf('\x07', startIdx + 5);
7120
+ const stIdx = result.indexOf('\x1b\\', startIdx + 5);
7121
+
7122
+ if (belIdx !== -1 && (stIdx === -1 || belIdx < stIdx)) {
7123
+ endIdx = belIdx;
7124
+ endLen = 1;
7125
+ } else if (stIdx !== -1) {
7126
+ endIdx = stIdx;
7127
+ endLen = 2;
7128
+ }
7129
+
7130
+ if (endIdx === -1) {
7131
+ break;
7132
+ }
7133
+
7134
+ // Extract the payload: everything between "ESC]52;" and the terminator
7135
+ const payloadStart = startIdx + 5; // skip \x1b]52;
7136
+ const payload = result.substring(payloadStart, endIdx);
7137
+ console.log('[SSHIFT] OSC 52 intercepted from stream, payload length:', payload.length);
7138
+
7139
+ // Parse: <target>;<base64>
7140
+ const semiIdx = payload.indexOf(';');
7141
+ if (semiIdx !== -1) {
7142
+ const clipboardTarget = payload.substring(0, semiIdx);
7143
+ const content = payload.substring(semiIdx + 1);
7144
+ if (content) {
7145
+ try {
7146
+ const decoded = atob(content);
7147
+ console.log('[SSHIFT] OSC 52 decoded, target:', clipboardTarget, 'length:', decoded.length, 'preview:', decoded.substring(0, 60));
7148
+ // Buffer synchronously, then try immediate write
7149
+ this.osc52Buffer = decoded;
7150
+ this.copyToClipboard(decoded).then(success => {
7151
+ if (success) {
7152
+ this.osc52Buffer = null;
7153
+ console.log('[SSHIFT] OSC 52 immediate clipboard write succeeded');
7154
+ } else {
7155
+ console.log('[SSHIFT] OSC 52 immediate write failed, will flush on next user gesture');
7156
+ }
7157
+ }).catch(() => {
7158
+ console.log('[SSHIFT] OSC 52 immediate write rejected, will flush on next user gesture');
7159
+ });
7160
+ } catch (e) {
7161
+ console.warn('[SSHIFT] OSC 52 base64 decode failed:', e);
7162
+ }
7163
+ } else {
7164
+ // Clear clipboard request
7165
+ this.osc52Buffer = '';
7166
+ navigator.clipboard.writeText('').catch(() => {});
7167
+ }
7168
+ }
7169
+
7170
+ // Remove the OSC 52 sequence from the data so xterm.js doesn't render garbage
7171
+ result = result.substring(0, startIdx) + result.substring(endIdx + endLen);
7172
+ searchFrom = startIdx;
7173
+ }
7174
+
7175
+ return result;
7176
+ }
7177
+
6878
7178
  _flushWriteChunks(sessionId) {
6879
7179
  const session = this.sessions.get(sessionId);
6880
7180
  if (!session || !session.terminal) {
@@ -6940,6 +7240,11 @@ const wheelHandler = (e) => {
6940
7240
  }, 3000);
6941
7241
  }
6942
7242
 
7243
+ // Intercept OSC 52 clipboard sequences before writing to terminal.
7244
+ // OSC 52 format: ESC ] 52 ; <target> ; <base64> (BEL or ST)
7245
+ // This is more reliable than registerOscHandler which may not fire.
7246
+ combined = this._handleOsc52(combined);
7247
+
6943
7248
  // Cap write size per frame. terminal.write() is synchronous and blocks
6944
7249
  // the main thread. We spill overflow to the next frame via flushRemaining.
6945
7250
  const MAX_WRITE_PER_FRAME = 32768;
@@ -7556,6 +7861,13 @@ const wheelHandler = (e) => {
7556
7861
  session.wheelHandler = null;
7557
7862
  session.wheelElement = null;
7558
7863
  }
7864
+ // Clean up OSC 52 clipboard flush listeners
7865
+ if (session.osc52FlushListener) {
7866
+ document.removeEventListener('click', session.osc52FlushListener, true);
7867
+ document.removeEventListener('keydown', session.osc52FlushListener, true);
7868
+ document.removeEventListener('touchstart', session.osc52FlushListener, true);
7869
+ session.osc52FlushListener = null;
7870
+ }
7559
7871
  if (session.terminal) {
7560
7872
  session.terminal.dispose();
7561
7873
  }
@@ -7665,6 +7977,12 @@ const wheelHandler = (e) => {
7665
7977
  if (session.wheelHandler && session.wheelElement) {
7666
7978
  session.wheelElement.removeEventListener('wheel', session.wheelHandler);
7667
7979
  }
7980
+ if (session.osc52FlushListener) {
7981
+ document.removeEventListener('click', session.osc52FlushListener, true);
7982
+ document.removeEventListener('keydown', session.osc52FlushListener, true);
7983
+ document.removeEventListener('touchstart', session.osc52FlushListener, true);
7984
+ session.osc52FlushListener = null;
7985
+ }
7668
7986
  if (session.terminal) {
7669
7987
  session.terminal.dispose();
7670
7988
  }
@@ -10120,6 +10438,12 @@ async syncTabsFromServer(tabs) {
10120
10438
 
10121
10439
  const data = await response.json();
10122
10440
 
10441
+ // Check for update error from server
10442
+ if (data.updateError) {
10443
+ this.handleUpdateError(data.updateError);
10444
+ return;
10445
+ }
10446
+
10123
10447
  // Update progress based on status
10124
10448
  if (data.updating) {
10125
10449
  // Still updating
@@ -10157,23 +10481,25 @@ async syncTabsFromServer(tabs) {
10157
10481
  window.location.reload();
10158
10482
  }, 1000);
10159
10483
  } else if (data.ready && data.version === oldVersion) {
10160
- // Server restarted but version is the same
10161
- // This might mean the update failed or we need to wait longer
10162
- if (attempts < maxAttempts) {
10163
- const progress = Math.min(90, 70 + (attempts * 0.5));
10164
- if (progressBar) {
10165
- progressBar.style.width = `${progress}%`;
10166
- }
10167
-
10168
- if (statusEl) {
10169
- statusEl.textContent = `Waiting for new version... (${attempts}/${maxAttempts})`;
10170
- }
10171
-
10172
- setTimeout(poll, 1000);
10173
- } else {
10174
- // Same version after timeout - might be an error
10175
- this.handleUpdateError('Server restarted but version unchanged. Please check the update manually.');
10484
+ // Server is back and ready. Same version could mean:
10485
+ // 1. Already at the latest version (npm install succeeded but no newer version exists)
10486
+ // 2. Update failed silently
10487
+ // Either way, the server is back and functional — just reload.
10488
+ if (progressBar) {
10489
+ progressBar.style.width = '100%';
10176
10490
  }
10491
+
10492
+ if (messageEl) {
10493
+ messageEl.textContent = 'Server restarted. Reloading page...';
10494
+ }
10495
+
10496
+ if (statusEl) {
10497
+ statusEl.textContent = `Current version: ${data.version}`;
10498
+ }
10499
+
10500
+ setTimeout(() => {
10501
+ window.location.reload();
10502
+ }, 1000);
10177
10503
  } else {
10178
10504
  // Unexpected state
10179
10505
  if (attempts < maxAttempts) {