@lethevimlet/sshift 1.3.9 → 1.4.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.3.9",
3
+ "version": "1.4.1",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -81,11 +81,60 @@ function registerSFTPHandlers(socket, io) {
81
81
  console.log('[SFTP] Emitting sftp-list-result for sessionId:', data.sessionId, 'files:', files.length);
82
82
  socket.emit('sftp-list-result', { path: data.path, files, sessionId: data.sessionId });
83
83
  } catch (err) {
84
- console.error('[SFTP] Error in sftp-list:', err.message);
85
- socket.emit('sftp-error', { message: err.message });
84
+ console.error('[SFTP] Error in sftp-list:', err.message, 'code:', err.code);
85
+
86
+ const homeDir = sftpManager.home(data.sessionId);
87
+ const isPermissionDenied = err.code === 3
88
+ || (err.message && (
89
+ err.message.includes('Permission denied')
90
+ || err.message.includes('permission denied')
91
+ || err.message.includes('EACCES')
92
+ ));
93
+
94
+ // If listing "/" fails with permission denied and we know the home
95
+ // directory, try falling back to it automatically. This handles the
96
+ // common case of chrooted SFTP servers that restrict root access.
97
+ if (isPermissionDenied && homeDir && data.path === '/') {
98
+ console.log('[SFTP] Permission denied on /, falling back to home directory:', homeDir);
99
+ try {
100
+ const homeFiles = await sftpManager.list(data.sessionId, homeDir);
101
+ socket.emit('sftp-list-result', {
102
+ path: homeDir,
103
+ files: homeFiles,
104
+ sessionId: data.sessionId,
105
+ redirectedFrom: '/',
106
+ homeDir
107
+ });
108
+ return;
109
+ } catch (homeErr) {
110
+ console.error('[SFTP] Home directory also failed:', homeErr.message);
111
+ socket.emit('sftp-error', {
112
+ message: homeErr.message,
113
+ sessionId: data.sessionId,
114
+ homeDir
115
+ });
116
+ return;
117
+ }
118
+ }
119
+
120
+ socket.emit('sftp-error', {
121
+ message: err.message,
122
+ sessionId: data.sessionId,
123
+ homeDir,
124
+ isPermissionDenied
125
+ });
86
126
  }
87
127
  });
88
128
 
129
+ // SFTP get home directory
130
+ socket.on('sftp-home', (data) => {
131
+ const homeDir = sftpManager.home(data.sessionId);
132
+ socket.emit('sftp-home-result', {
133
+ sessionId: data.sessionId,
134
+ homeDir
135
+ });
136
+ });
137
+
89
138
  // SFTP download (streamed)
90
139
  socket.on('sftp-download', async (data) => {
91
140
  try {
@@ -86,16 +86,30 @@ class SFTPManager {
86
86
  conn: conn,
87
87
  sftp: sftp,
88
88
  socket: socket,
89
- // Connection info for sessions API
90
89
  host: options.host,
91
90
  port: options.port || 22,
92
91
  username: options.username,
93
- connectedAt: Date.now()
92
+ connectedAt: Date.now(),
93
+ homeDir: null
94
94
  };
95
95
 
96
- this.sessions.set(sessionId, session);
97
- console.log(`[SFTP] SFTP session started: ${sessionId}`);
98
- resolve(sessionId);
96
+ // Resolve the user's home directory so we can fall back to it
97
+ // when listing "/" fails (e.g. chrooted SFTP or restricted
98
+ // server configs). realpath('.') returns the absolute path of
99
+ // the remote working directory, which defaults to the user's
100
+ // home on most SSH servers.
101
+ sftp.realpath('.', (realpathErr, resolvedPath) => {
102
+ if (!realpathErr && resolvedPath) {
103
+ session.homeDir = resolvedPath;
104
+ console.log(`[SFTP] Resolved home directory for ${options.username}: ${resolvedPath}`);
105
+ } else {
106
+ console.warn(`[SFTP] Could not resolve home directory: ${realpathErr?.message}`);
107
+ }
108
+
109
+ this.sessions.set(sessionId, session);
110
+ console.log(`[SFTP] SFTP session started: ${sessionId}`);
111
+ resolve(sessionId);
112
+ });
99
113
  });
100
114
  });
101
115
 
@@ -114,12 +128,36 @@ class SFTPManager {
114
128
  removeTab(sessionId);
115
129
  });
116
130
 
117
- try {
118
- conn.connect(config);
119
- } catch (err) {
120
- console.error(`[SFTP] Connect exception: ${err.message}`);
121
- reject(err);
131
+ try {
132
+ conn.connect(config);
133
+ } catch (err) {
134
+ console.error(`[SFTP] Connect exception: ${err.message}`);
135
+ reject(err);
136
+ }
137
+ });
138
+ }
139
+
140
+ home(sessionId) {
141
+ const session = this.sessions.get(sessionId);
142
+ if (!session) return null;
143
+ return session.homeDir || null;
144
+ }
145
+
146
+ realpath(sessionId, path) {
147
+ return new Promise((resolve, reject) => {
148
+ const session = this.sessions.get(sessionId);
149
+ if (!session) {
150
+ reject(new Error('Session not found'));
151
+ return;
122
152
  }
153
+
154
+ session.sftp.realpath(path, (err, resolvedPath) => {
155
+ if (err) {
156
+ reject(err);
157
+ return;
158
+ }
159
+ resolve(resolvedPath);
160
+ });
123
161
  });
124
162
  }
125
163
 
@@ -3000,12 +3000,16 @@ body.dragging-tabs .tabs-container {
3000
3000
  min-width: 200px;
3001
3001
  }
3002
3002
 
3003
+ .sftp-home-btn {
3004
+ border-radius: var(--radius-md) 0 0 var(--radius-md);
3005
+ padding: 8px 10px;
3006
+ }
3007
+
3003
3008
  .sftp-path-input {
3004
3009
  flex: 1;
3005
3010
  padding: 8px 12px;
3006
3011
  background: var(--bg-tertiary);
3007
3012
  border: 1px solid var(--border-color);
3008
- border-radius: var(--radius-md) 0 0 var(--radius-md);
3009
3013
  color: var(--text-primary);
3010
3014
  font-family: var(--font-mono);
3011
3015
  font-size: 13px;
@@ -1175,12 +1175,14 @@ sendChunkedInput(sessionId, data, chunkSize = 2048) {
1175
1175
  return false;
1176
1176
  }
1177
1177
 
1178
- // Guard: refuse to fit if the container has zero or near-zero dimensions.
1178
+ // Guard: refuse to fit if the container has near-zero dimensions.
1179
1179
  // A zero-size container produces bogus col/row counts that propagate to
1180
1180
  // the remote PTY and cause narrow-wrap / garbled output bugs.
1181
+ // We use 2px as the threshold to catch truly collapsed containers while
1182
+ // allowing containers that are mid-layout-transition but already have
1183
+ // meaningful size.
1181
1184
  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`);
1185
+ if (rect.width < 2 || rect.height < 2) {
1184
1186
  session.needsResize = true;
1185
1187
  if (retryCount < 3) {
1186
1188
  requestAnimationFrame(() => this._fitTerminal(session, retryCount + 1));
@@ -4158,10 +4160,11 @@ const wheelHandler = (e) => {
4158
4160
  if (!session.terminal || !session.fitAddon || !session.isController) return;
4159
4161
  try {
4160
4162
  session.isResyncing = true;
4161
- this._fitTerminal(session);
4163
+ const fitted = this._fitTerminal(session);
4162
4164
  session.isResyncing = false;
4163
4165
 
4164
- if (session.terminal && session.connected) {
4166
+ // Only send the resize if we actually fitted successfully.
4167
+ if (fitted && session.terminal && session.connected) {
4165
4168
  this.socket.emit('ssh-resize', {
4166
4169
  sessionId: data.sessionId,
4167
4170
  cols: session.terminal.cols,
@@ -4175,9 +4178,9 @@ const wheelHandler = (e) => {
4175
4178
  session.isResyncing = true;
4176
4179
  const prevCols = session.terminal.cols;
4177
4180
  const prevRows = session.terminal.rows;
4178
- this._fitTerminal(session);
4181
+ const corrected = this._fitTerminal(session);
4179
4182
  session.isResyncing = false;
4180
- if (session.terminal.cols !== prevCols || session.terminal.rows !== prevRows) {
4183
+ if (corrected && (session.terminal.cols !== prevCols || session.terminal.rows !== prevRows)) {
4181
4184
  this.socket.emit('ssh-resize', {
4182
4185
  sessionId: data.sessionId,
4183
4186
  cols: session.terminal.cols,
@@ -4250,10 +4253,15 @@ const wheelHandler = (e) => {
4250
4253
  if (!session.terminal || !session.fitAddon || !session.isController) return;
4251
4254
  try {
4252
4255
  session.isResyncing = true;
4253
- this._fitTerminal(session);
4256
+ const fitted = this._fitTerminal(session);
4254
4257
  session.isResyncing = false;
4255
4258
 
4256
- if (session.terminal && session.connected) {
4259
+ // Only send the resize to the server if we actually fitted the
4260
+ // terminal. If _fitTerminal returned false (container too small,
4261
+ // not visible, etc.), the cols/rows are stale and would confuse
4262
+ // the remote PTY. A successful retry will trigger onResize
4263
+ // which emits ssh-resize via the debounced handler.
4264
+ if (fitted && session.terminal && session.connected) {
4257
4265
  this.socket.emit('ssh-resize', {
4258
4266
  sessionId: data.sessionId,
4259
4267
  cols: session.terminal.cols,
@@ -4267,9 +4275,9 @@ const wheelHandler = (e) => {
4267
4275
  session.isResyncing = true;
4268
4276
  const prevCols = session.terminal.cols;
4269
4277
  const prevRows = session.terminal.rows;
4270
- this._fitTerminal(session);
4278
+ const corrected = this._fitTerminal(session);
4271
4279
  session.isResyncing = false;
4272
- if (session.terminal.cols !== prevCols || session.terminal.rows !== prevRows) {
4280
+ if (corrected && (session.terminal.cols !== prevCols || session.terminal.rows !== prevRows)) {
4273
4281
  this.socket.emit('ssh-resize', {
4274
4282
  sessionId: data.sessionId,
4275
4283
  cols: session.terminal.cols,
@@ -4432,11 +4440,47 @@ const wheelHandler = (e) => {
4432
4440
  }
4433
4441
  });
4434
4442
 
4443
+ this.socket.on('sftp-home-result', (data) => {
4444
+ console.log('[SSHIFT] sftp-home-result received:', data);
4445
+ const session = this.sftpSessions.get(data.sessionId);
4446
+ if (session && data.homeDir) {
4447
+ session.homeDir = data.homeDir;
4448
+ console.log('[SSHIFT] Stored home directory for session:', data.homeDir);
4449
+
4450
+ // If the session still shows "/" as current path (initial load),
4451
+ // and listing "/" hasn't succeeded yet, navigate to home instead.
4452
+ // This gives users with chrooted SFTP a working starting directory.
4453
+ if (session.currentPath === '/' && !session._rootListSucceeded) {
4454
+ console.log('[SSHIFT] Navigating to home directory:', data.homeDir);
4455
+ session.currentPath = data.homeDir;
4456
+ const pathInput = document.querySelector(`#sftp-${data.sessionId} .sftp-path-input`);
4457
+ if (pathInput) pathInput.value = data.homeDir;
4458
+ this.socket.emit('sftp-list', { sessionId: data.sessionId, path: data.homeDir });
4459
+ }
4460
+ }
4461
+ });
4462
+
4435
4463
  this.socket.on('sftp-list-result', (data) => {
4436
4464
  console.log('[SSHIFT] sftp-list-result received:', data);
4437
4465
  console.log('[SSHIFT] sessionId from server:', data.sessionId);
4438
4466
  console.log('[SSHIFT] path:', data.path);
4439
4467
  console.log('[SSHIFT] files count:', data.files?.length || 0);
4468
+
4469
+ // Track whether root listing has succeeded (used by sftp-home-result
4470
+ // to decide whether to redirect to home directory).
4471
+ const session = this.sftpSessions.get(data.sessionId);
4472
+ if (session) {
4473
+ if (data.path === '/' && !data.redirectedFrom) {
4474
+ session._rootListSucceeded = true;
4475
+ }
4476
+ session.currentPath = data.path;
4477
+ }
4478
+
4479
+ // If listing was auto-redirected from "/" to home, notify the user.
4480
+ if (data.redirectedFrom) {
4481
+ this.showToast(`Root directory (${data.redirectedFrom}) is not accessible. Showing home directory instead.`, 'info');
4482
+ }
4483
+
4440
4484
  this.renderSFTPFileList(data.path, data.files, data.sessionId);
4441
4485
  });
4442
4486
 
@@ -4476,6 +4520,22 @@ const wheelHandler = (e) => {
4476
4520
  } else {
4477
4521
  this.showToast(data.message, 'error');
4478
4522
  }
4523
+ } else if (data.isPermissionDenied && data.homeDir) {
4524
+ // Permission denied with a known home directory — show a helpful
4525
+ // error with a "Go Home" action instead of a bare error toast.
4526
+ this.showToast(`Permission denied: ${data.message}. Try navigating to your home directory.`, 'error');
4527
+ // Auto-navigate to home directory if we're stuck at an inaccessible path
4528
+ const session = data?.sessionId ? this.sftpSessions.get(data.sessionId) : null;
4529
+ if (session) {
4530
+ session.homeDir = data.homeDir;
4531
+ const currentPath = session.currentPath || '/';
4532
+ // Only auto-redirect if we haven't already navigated away
4533
+ const pathInput = document.querySelector(`#sftp-${data.sessionId} .sftp-path-input`);
4534
+ if (pathInput && pathInput.value === currentPath) {
4535
+ console.log('[SSHIFT] Auto-navigating SFTP to home directory:', data.homeDir);
4536
+ this.navigateSFTPPath(data.homeDir, data.sessionId);
4537
+ }
4538
+ }
4479
4539
  } else {
4480
4540
  this.showToast(data.message, 'error');
4481
4541
  }
@@ -7720,12 +7780,18 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
7720
7780
  // hold. The PTY on the server wraps at those bogus dimensions.
7721
7781
  const sendResize = () => {
7722
7782
  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
- });
7783
+ const fitted = this._fitTerminal(session);
7784
+ // Only send the resize to the server if we actually fitted
7785
+ // successfully — otherwise the remote PTY would get stale
7786
+ // dimensions. A deferred retry will trigger onResize which
7787
+ // sends ssh-resize via the debounced handler.
7788
+ if (fitted) {
7789
+ this.socket.emit('ssh-resize', {
7790
+ sessionId: data.sessionId,
7791
+ cols: session.terminal.cols,
7792
+ rows: session.terminal.rows
7793
+ });
7794
+ }
7729
7795
  }
7730
7796
  };
7731
7797
 
@@ -8035,6 +8101,8 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8035
8101
  name,
8036
8102
  type: 'sftp',
8037
8103
  currentPath: '/',
8104
+ homeDir: null, // Resolved from server via sftp-home
8105
+ _rootListSucceeded: false, // Whether listing "/" worked (used for auto-redirect)
8038
8106
  connectionData, // Store for sticky sessions
8039
8107
  fontSize: this.terminalFontSize, // Initialize with default font size
8040
8108
  isRestoring: !!restoreSessionId, // Flag to indicate if this is a restored session
@@ -8086,6 +8154,9 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8086
8154
  container.innerHTML = `
8087
8155
  <div class="sftp-toolbar">
8088
8156
  <div class="sftp-path">
8157
+ <button class="btn btn-sm sftp-home-btn" title="Home directory">
8158
+ <i class="fas fa-home"></i>
8159
+ </button>
8089
8160
  <input type="text" class="sftp-path-input" placeholder="/path/to/directory">
8090
8161
  <button class="btn btn-sm sftp-go-btn">
8091
8162
  <i class="fas fa-arrow-right"></i>
@@ -8108,11 +8179,18 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8108
8179
 
8109
8180
  // Add event listeners
8110
8181
  const pathInput = container.querySelector('.sftp-path-input');
8182
+ const homeBtn = container.querySelector('.sftp-home-btn');
8111
8183
  const goBtn = container.querySelector('.sftp-go-btn');
8112
8184
  const refreshBtn = container.querySelector('.sftp-refresh-btn');
8113
8185
  const mkdirBtn = container.querySelector('.sftp-mkdir-btn');
8114
8186
  const uploadBtn = container.querySelector('.sftp-upload-btn');
8115
8187
 
8188
+ homeBtn.addEventListener('click', () => {
8189
+ const session = this.sftpSessions.get(sessionId);
8190
+ const homePath = session?.homeDir || '~';
8191
+ this.navigateSFTPPath(homePath, sessionId);
8192
+ });
8193
+
8116
8194
  goBtn.addEventListener('click', () => {
8117
8195
  this.navigateSFTPPath(pathInput.value, sessionId);
8118
8196
  });
@@ -8187,7 +8265,12 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8187
8265
  console.log('[SSHIFT] SFTP container parent:', sftpContainer.parentElement?.id);
8188
8266
  }
8189
8267
 
8190
- // Update path input and list files
8268
+ // Request the server's home directory for this session so we can
8269
+ // start there instead of "/" (which may be denied by chroot / perms)
8270
+ this.socket.emit('sftp-home', { sessionId: data.sessionId });
8271
+
8272
+ // Also start listing the current path (or "/") immediately; if the
8273
+ // home-directory response arrives first we'll navigate there instead.
8191
8274
  const pathInput = document.querySelector(`#sftp-${data.sessionId} .sftp-path-input`);
8192
8275
  console.log('[SSHIFT] Path input found:', !!pathInput);
8193
8276
 
@@ -8197,7 +8280,6 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8197
8280
  console.error('[SSHIFT] Path input not found for session:', data.sessionId);
8198
8281
  }
8199
8282
 
8200
- // List files in the current directory
8201
8283
  console.log('[SSHIFT] Emitting sftp-list for path:', session.currentPath || '/');
8202
8284
  this.socket.emit('sftp-list', { sessionId: data.sessionId, path: session.currentPath || '/' });
8203
8285
  } else {
@@ -8330,6 +8412,16 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8330
8412
  navigateSFTPPath(path, sessionId) {
8331
8413
  const session = this.sftpSessions.get(sessionId);
8332
8414
  if (session) {
8415
+ // Resolve ~ to the user's home directory
8416
+ if (path === '~' || path.startsWith('~/')) {
8417
+ if (session.homeDir) {
8418
+ path = path === '~' ? session.homeDir : session.homeDir + path.slice(1);
8419
+ } else {
8420
+ // Ask the server to resolve ~ if we don't have the home dir yet
8421
+ this.socket.emit('sftp-home', { sessionId });
8422
+ }
8423
+ }
8424
+
8333
8425
  session.currentPath = path;
8334
8426
 
8335
8427
  const pathInput = document.querySelector(`#sftp-${sessionId} .sftp-path-input`);
@@ -8726,7 +8818,23 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8726
8818
  session.needsResize = false;
8727
8819
  }
8728
8820
 
8729
- this._fitTerminal(session);
8821
+ const fitted = this._fitTerminal(session);
8822
+
8823
+ // If fitting failed (e.g. container not yet laid out), retry
8824
+ // with delays until the browser commits the layout change.
8825
+ if (!fitted) {
8826
+ console.log('[SSHIFT] Initial fit failed during tab switch, scheduling retries');
8827
+ let retriesLeft = 4;
8828
+ const retryFit = () => {
8829
+ if (!session.terminal || !session.fitAddon || !session.isController) return;
8830
+ const ok = this._fitTerminal(session);
8831
+ if (!ok && retriesLeft > 0) {
8832
+ retriesLeft--;
8833
+ setTimeout(retryFit, 100);
8834
+ }
8835
+ };
8836
+ setTimeout(retryFit, 50);
8837
+ }
8730
8838
 
8731
8839
  // Focus the terminal so user can type
8732
8840
  if (session.terminal) {