@lethevimlet/sshift 1.2.0 → 1.2.1

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.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -31,6 +31,8 @@ class SSHIFTClient {
31
31
  this.savedTabs = []; // Store tab information
32
32
  this.isRestoring = false; // Flag to prevent saving during restoration
33
33
  this.isSyncingTabs = false; // Flag to prevent saveTabs emission during tabs-sync
34
+ this._initialSyncDone = false; // Whether initial open-tabs sync from server has completed
35
+ this._serverSyncTimeout = null; // Timeout for fallback to localStorage restore
34
36
  this.sftpClipboard = null; // For cut/copy/paste: { action: 'cut'|'copy', path: string, name: string, sessionId: string }
35
37
  this.terminalClipboardContent = null; // Pre-read clipboard content for context menu paste
36
38
  this.osc52Buffer = null; // Buffered OSC 52 clipboard content pending write
@@ -2389,9 +2391,16 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
2389
2391
  // Update terminal color UI to reflect loaded settings
2390
2392
  this.updateTerminalColorOverrideUI();
2391
2393
 
2392
- // Restore tabs if sticky is enabled
2394
+ // In sticky mode, the server is the single source of truth for tabs.
2395
+ // Wait for the open-tabs event from the server instead of restoring from localStorage.
2396
+ // Set a fallback timeout in case the server is unreachable.
2393
2397
  if (this.sticky) {
2394
- await this.restoreTabs();
2398
+ this._serverSyncTimeout = setTimeout(() => {
2399
+ if (!this._initialSyncDone) {
2400
+ console.log('[SSHIFT] Server tabs not received within timeout, falling back to localStorage');
2401
+ this.restoreTabs();
2402
+ }
2403
+ }, 3000);
2395
2404
  }
2396
2405
 
2397
2406
  // Initialize mobile tabs dropdown
@@ -3255,6 +3264,13 @@ const wheelHandler = (e) => {
3255
3264
  }
3256
3265
 
3257
3266
  async restoreTabs() {
3267
+ // If server tabs have already been synced, skip localStorage restore
3268
+ // This prevents duplication when the server responded before the fallback timeout
3269
+ if (this._initialSyncDone) {
3270
+ console.log('[SSHIFT] Server tabs already synced, skipping localStorage restore');
3271
+ return;
3272
+ }
3273
+
3258
3274
  const savedData = this.loadTabs();
3259
3275
 
3260
3276
  // Handle both old format (array) and new format (object with tabs and layout)
@@ -3284,6 +3300,12 @@ const wheelHandler = (e) => {
3284
3300
 
3285
3301
  // Restore each session
3286
3302
  for (const savedTab of savedTabs) {
3303
+ // Stop restoring if server tabs have been synced (open-tabs arrived while we were restoring)
3304
+ if (this._initialSyncDone) {
3305
+ console.log('[SSHIFT] Server tabs received during restore, stopping localStorage restore');
3306
+ break;
3307
+ }
3308
+
3287
3309
  console.log('[SSHIFT] Restoring tab:', savedTab.name, savedTab.type);
3288
3310
 
3289
3311
  let sessionId;
@@ -3385,6 +3407,12 @@ const wheelHandler = (e) => {
3385
3407
  // Handle receiving open tabs from server (for cross-tab sync)
3386
3408
  this.socket.on('open-tabs', (data) => {
3387
3409
  console.log('[SSHIFT] Received open tabs from server:', data.tabs.length);
3410
+
3411
+ // Clear fallback timeout - server has responded
3412
+ if (this._serverSyncTimeout) {
3413
+ clearTimeout(this._serverSyncTimeout);
3414
+ this._serverSyncTimeout = null;
3415
+ }
3388
3416
 
3389
3417
  // Sync theme and accent from server
3390
3418
  if (data.theme) {
@@ -3411,8 +3439,19 @@ const wheelHandler = (e) => {
3411
3439
  }
3412
3440
 
3413
3441
  // Only sync if sticky is enabled
3414
- if (this.sticky && data.tabs.length > 0) {
3415
- this.syncTabsFromServer(data.tabs);
3442
+ // Server is the single source of truth for tabs in sticky mode.
3443
+ // On initial sync, reconcile local state to match server state.
3444
+ // On reconnection, only add missing tabs to avoid disrupting locally-created tabs.
3445
+ if (this.sticky) {
3446
+ if (data.tabs.length > 0) {
3447
+ const isInitialSync = !this._initialSyncDone;
3448
+ this.syncTabsFromServer(data.tabs, isInitialSync);
3449
+ } else if (!this._initialSyncDone) {
3450
+ // Server has no tabs and this is the first sync (e.g. server restart).
3451
+ // Fall back to localStorage restore to reconnect sessions.
3452
+ this.restoreTabs();
3453
+ }
3454
+ this._initialSyncDone = true;
3416
3455
  }
3417
3456
  // Sync layout if provided (but not on mobile - mobile always uses single panel)
3418
3457
  if (data.layout && this.sticky && !this.isMobile) {
@@ -8276,6 +8315,84 @@ const wheelHandler = (e) => {
8276
8315
  this.saveTabs();
8277
8316
  }
8278
8317
 
8318
+ // Remove a tab locally without notifying the server.
8319
+ // Used during initial sync to remove stale tabs from localStorage
8320
+ // that don't exist on the server (prevents cross-device duplication).
8321
+ removeTabLocally(sessionId) {
8322
+ const session = this.sessions.get(sessionId) || this.sftpSessions.get(sessionId);
8323
+ if (!session) return;
8324
+
8325
+ console.log('[SSHIFT] Removing tab locally:', sessionId);
8326
+
8327
+ const panelId = this.getPanelForSession(sessionId);
8328
+
8329
+ // Disconnect from server session (without emitting tab-close)
8330
+ if (session.type === 'ssh') {
8331
+ this.socket.emit('ssh-disconnect', { sessionId });
8332
+ if (session.writeRAF) cancelAnimationFrame(session.writeRAF);
8333
+ if (session.scrollbackRestoreTimer) clearTimeout(session.scrollbackRestoreTimer);
8334
+ if (session.originalScrollback && session.terminal) {
8335
+ session.terminal.options.scrollback = session.originalScrollback;
8336
+ }
8337
+ if (session.resizeObserver) {
8338
+ session.resizeObserver.disconnect();
8339
+ session.resizeObserver = null;
8340
+ }
8341
+ if (session.resizeTimeout) {
8342
+ clearTimeout(session.resizeTimeout);
8343
+ session.resizeTimeout = null;
8344
+ }
8345
+ if (session.wheelHandler && session.wheelElement) {
8346
+ session.wheelElement.removeEventListener('wheel', session.wheelHandler);
8347
+ }
8348
+ if (session.osc52FlushListener) {
8349
+ document.removeEventListener('click', session.osc52FlushListener, true);
8350
+ document.removeEventListener('keydown', session.osc52FlushListener, true);
8351
+ document.removeEventListener('touchstart', session.osc52FlushListener, true);
8352
+ }
8353
+ if (session.terminal) session.terminal.dispose();
8354
+ if (session.mobileHandler) {
8355
+ session.mobileHandler.destroy();
8356
+ session.mobileHandler = null;
8357
+ }
8358
+ this.sessions.delete(sessionId);
8359
+ } else {
8360
+ this.socket.emit('sftp-disconnect', { sessionId });
8361
+ this.sftpSessions.delete(sessionId);
8362
+ }
8363
+
8364
+ // Remove from DOM
8365
+ const tab = document.querySelector(`.tab[data-session-id="${sessionId}"]`);
8366
+ const wrapper = document.getElementById(`terminal-wrapper-${sessionId}`);
8367
+ if (tab) tab.remove();
8368
+ if (wrapper) wrapper.remove();
8369
+
8370
+ // Update scroll arrows
8371
+ this.updateTabsScrollArrows();
8372
+
8373
+ // Check if this was the active session
8374
+ if (this.activeSessionId === sessionId) {
8375
+ const remainingSessions = [...Array.from(this.sessions.keys()), ...Array.from(this.sftpSessions.keys())];
8376
+ if (remainingSessions.length > 0) {
8377
+ this.switchTab(remainingSessions[0]);
8378
+ } else {
8379
+ this.activeSessionId = null;
8380
+ this.updateMobileKeysBar();
8381
+ }
8382
+ }
8383
+
8384
+ // Check if this was the active session in its panel
8385
+ const activeInPanel = this.activeSessionsByPanel.get(panelId);
8386
+ if (activeInPanel === sessionId) {
8387
+ this.activeSessionsByPanel.delete(panelId);
8388
+ const tabsContainer = this.getTabsContainer(panelId);
8389
+ const remainingInPanel = tabsContainer ? Array.from(tabsContainer.children) : [];
8390
+ if (remainingInPanel.length === 0) {
8391
+ this.showEmptyState(panelId);
8392
+ }
8393
+ }
8394
+ }
8395
+
8279
8396
  // Handle tab opened by another client
8280
8397
  handleTabOpened(data) {
8281
8398
  // Check if we already have this session
@@ -8376,7 +8493,7 @@ const wheelHandler = (e) => {
8376
8493
  }
8377
8494
 
8378
8495
  // Sync tabs from server (called on connect when sticky is enabled)
8379
- async syncTabsFromServer(tabs) {
8496
+ async syncTabsFromServer(tabs, isInitialSync = false) {
8380
8497
  // Wait for any in-progress restoration to complete before syncing
8381
8498
  // This prevents the race condition where restoreTabs starts first,
8382
8499
  // sets isRestoring=true, and then this function returns early, missing
@@ -8387,9 +8504,27 @@ async syncTabsFromServer(tabs) {
8387
8504
  waitCount++;
8388
8505
  }
8389
8506
 
8390
- console.log('[SSHIFT] Syncing tabs from server:', tabs.length);
8507
+ console.log('[SSHIFT] Syncing tabs from server:', tabs.length, 'isInitialSync:', isInitialSync);
8391
8508
  this.isRestoring = true;
8392
8509
 
8510
+ // On initial sync, remove any client-side tabs that don't exist on the server.
8511
+ // This ensures the server is the single source of truth and prevents duplicates
8512
+ // from stale localStorage caches (e.g. when switching between devices).
8513
+ if (isInitialSync) {
8514
+ const serverSessionIds = new Set(tabs.map(t => t.sessionId));
8515
+ const clientSessionIds = [
8516
+ ...Array.from(this.sessions.keys()),
8517
+ ...Array.from(this.sftpSessions.keys())
8518
+ ];
8519
+
8520
+ for (const sessionId of clientSessionIds) {
8521
+ if (!serverSessionIds.has(sessionId)) {
8522
+ console.log('[SSHIFT] Removing local tab not on server during initial sync:', sessionId);
8523
+ this.removeTabLocally(sessionId);
8524
+ }
8525
+ }
8526
+ }
8527
+
8393
8528
  for (const tab of tabs) {
8394
8529
  // Check if we already have this session
8395
8530
  if (this.sessions.has(tab.sessionId) || this.sftpSessions.has(tab.sessionId)) {
@@ -8426,6 +8561,9 @@ async syncTabsFromServer(tabs) {
8426
8561
  this.applyFlashStates();
8427
8562
 
8428
8563
  this.isRestoring = false;
8564
+
8565
+ // Save the final tab state to localStorage after sync completes
8566
+ this.saveTabs();
8429
8567
  }
8430
8568
 
8431
8569
  // Special Keys