ssh2-scp 1.1.0 → 2.0.4

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/README-CN.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ssh2-scp
2
2
 
3
- 通过 SSH2 会话进行远程文件系统操作(无需 SFTP)。
3
+ 通过 SSH2 会话进行远程文件系统操作/文件传输(无需 SFTP)。
4
4
 
5
5
  [English](./README.md)
6
6
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ssh2-scp
2
2
 
3
- Remote file system operations over SSH2 session without SFTP
3
+ Remote file system operations/file transfer over SSH2 session without SFTP
4
4
 
5
5
  [中文](./README-CN.md)
6
6
 
@@ -2,9 +2,17 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  class SshFs {
4
4
  session;
5
+ _chunkSize = null;
6
+ async getChunkSize() {
7
+ return this.detectChunkSize();
8
+ }
5
9
  constructor(session, _options) {
6
10
  this.session = session;
7
11
  }
12
+ async detectChunkSize() {
13
+ this._chunkSize = 4 * 1024;
14
+ return this._chunkSize;
15
+ }
8
16
  getExecOpts() {
9
17
  return {};
10
18
  }
@@ -12,6 +20,30 @@ class SshFs {
12
20
  const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
13
21
  return months.indexOf(month);
14
22
  }
23
+ parseMode(modeStr) {
24
+ const permMap = {
25
+ "---": 0,
26
+ "--x": 1,
27
+ "-w-": 2,
28
+ "-wx": 3,
29
+ "r--": 4,
30
+ "r-x": 5,
31
+ "rw-": 6,
32
+ "rwx": 7
33
+ };
34
+ const perms = modeStr.slice(1, 10);
35
+ const owner = permMap[perms.slice(0, 3)] || 0;
36
+ const group = permMap[perms.slice(3, 6)] || 0;
37
+ const other = permMap[perms.slice(6, 9)] || 0;
38
+ let fileType = 32768;
39
+ if (modeStr.startsWith("d")) fileType = 16384;
40
+ else if (modeStr.startsWith("l")) fileType = 40960;
41
+ else if (modeStr.startsWith("c")) fileType = 8192;
42
+ else if (modeStr.startsWith("b")) fileType = 24576;
43
+ else if (modeStr.startsWith("p")) fileType = 4096;
44
+ else if (modeStr.startsWith("s")) fileType = 49152;
45
+ return fileType | owner << 6 | group << 3 | other;
46
+ }
15
47
  runCmd(cmd, timeout = 3e4) {
16
48
  return new Promise((resolve, reject) => {
17
49
  let timeoutId;
@@ -115,11 +147,13 @@ class SshFs {
115
147
  });
116
148
  }
117
149
  async getFolderSize(folderPath) {
118
- const output = await this.runExec(`du -sh "${folderPath}" && find "${folderPath}" -type f | wc -l`);
119
- const lines = output.trim().split("\n");
120
- const size = lines[0]?.split(" ")[0] || "0";
121
- const count = parseInt(lines[1] || "0", 10);
122
- return { size, count };
150
+ try {
151
+ const output = await this.runCmd(`du -sh "${folderPath}"`);
152
+ const size = output.trim().split(" ")[0] || "0";
153
+ return { size, count: 0 };
154
+ } catch {
155
+ return { size: "0", count: 0 };
156
+ }
123
157
  }
124
158
  async listFiles(remotePath) {
125
159
  const output = await this.runCmd(`ls -la "${remotePath}"`);
@@ -176,9 +210,9 @@ class SshFs {
176
210
  }
177
211
  async stat(remotePath) {
178
212
  const isSymlink = await this.runCmd(`test -L "${remotePath}" && echo 1 || echo 0`).then((r) => r.trim() === "1");
179
- const output = await this.runCmd(`stat -c '%s %h %u %g %Y %Y %a' "${remotePath}"`);
213
+ const output = await this.runCmd(`ls -la "${remotePath}"`);
180
214
  const parts = output.trim().split(/\s+/);
181
- if (parts.length < 7) {
215
+ if (parts.length < 9) {
182
216
  return {
183
217
  size: 0,
184
218
  mode: 0,
@@ -195,36 +229,39 @@ class SshFs {
195
229
  isSocket: () => false
196
230
  };
197
231
  }
198
- const [size, _nlink, uid, gid, atime, mtime, modeOct] = parts;
199
- const mode = parseInt(modeOct, 8);
232
+ const size = parseInt(parts[4], 10);
233
+ const modeStr = parts[0];
234
+ const mode = this.parseMode(modeStr);
235
+ const uid = parseInt(parts[2], 10);
236
+ const gid = parseInt(parts[3], 10);
200
237
  return {
201
- size: parseInt(size, 10),
238
+ size,
202
239
  mode,
203
- uid: parseInt(uid, 10),
204
- gid: parseInt(gid, 10),
205
- atime: parseInt(atime, 10) * 1e3,
206
- mtime: parseInt(mtime, 10) * 1e3,
207
- isFile: () => (mode & 61440) === 32768,
208
- isDirectory: () => (mode & 61440) === 16384,
240
+ uid,
241
+ gid,
242
+ atime: 0,
243
+ mtime: 0,
244
+ isFile: () => modeStr.startsWith("-"),
245
+ isDirectory: () => modeStr.startsWith("d"),
209
246
  isSymbolicLink: () => isSymlink,
210
- isBlockDevice: () => (mode & 61440) === 24576,
211
- isCharacterDevice: () => (mode & 61440) === 8192,
212
- isFIFO: () => (mode & 61440) === 4096,
213
- isSocket: () => (mode & 61440) === 49152
247
+ isBlockDevice: () => modeStr.startsWith("b"),
248
+ isCharacterDevice: () => modeStr.startsWith("c"),
249
+ isFIFO: () => modeStr.startsWith("p"),
250
+ isSocket: () => modeStr.startsWith("s")
214
251
  };
215
252
  }
216
253
  readlink(remotePath) {
217
254
  return this.runCmd(`readlink "${remotePath}"`).then((output) => output.trim());
218
255
  }
219
256
  realpath(remotePath) {
220
- return this.runCmd(`realpath "${remotePath}"`).then((output) => output.trim());
257
+ return this.runCmd(`readlink -f "${remotePath}"`).then((output) => output.trim()).catch(() => this.runCmd(`cd "${remotePath}" && pwd`).then((output) => output.trim()));
221
258
  }
222
259
  async lstat(remotePath) {
223
260
  const output = await this.runCmd(`ls -ld "${remotePath}"`);
224
261
  const isSymlink = output.trim().startsWith("l");
225
- const statOutput = await this.runCmd(`stat -c '%s %h %u %g %Y %Y %a' "${remotePath}"`);
226
- const parts = statOutput.trim().split(/\s+/);
227
- if (parts.length < 7) {
262
+ const lsOutput = await this.runCmd(`ls -la "${remotePath}"`);
263
+ const parts = lsOutput.trim().split(/\s+/);
264
+ if (parts.length < 9) {
228
265
  return {
229
266
  size: 0,
230
267
  mode: 0,
@@ -241,22 +278,25 @@ class SshFs {
241
278
  isSocket: () => false
242
279
  };
243
280
  }
244
- const [size, _nlink, uid, gid, atime, mtime, modeOct] = parts;
245
- const mode = parseInt(modeOct, 8);
281
+ const size = parseInt(parts[4], 10);
282
+ const modeStr = parts[0];
283
+ const mode = this.parseMode(modeStr);
284
+ const uid = parseInt(parts[2], 10);
285
+ const gid = parseInt(parts[3], 10);
246
286
  return {
247
- size: parseInt(size, 10),
287
+ size,
248
288
  mode,
249
- uid: parseInt(uid, 10),
250
- gid: parseInt(gid, 10),
251
- atime: parseInt(atime, 10) * 1e3,
252
- mtime: parseInt(mtime, 10) * 1e3,
253
- isFile: () => !isSymlink && (mode & 61440) === 32768,
254
- isDirectory: () => !isSymlink && (mode & 61440) === 16384,
289
+ uid,
290
+ gid,
291
+ atime: 0,
292
+ mtime: 0,
293
+ isFile: () => !isSymlink && modeStr.startsWith("-"),
294
+ isDirectory: () => !isSymlink && modeStr.startsWith("d"),
255
295
  isSymbolicLink: () => isSymlink,
256
- isBlockDevice: () => (mode & 61440) === 24576,
257
- isCharacterDevice: () => (mode & 61440) === 8192,
258
- isFIFO: () => (mode & 61440) === 4096,
259
- isSocket: () => (mode & 61440) === 49152
296
+ isBlockDevice: () => modeStr.startsWith("b"),
297
+ isCharacterDevice: () => modeStr.startsWith("c"),
298
+ isFIFO: () => modeStr.startsWith("p"),
299
+ isSocket: () => modeStr.startsWith("s")
260
300
  };
261
301
  }
262
302
  chmod(remotePath, mode) {
@@ -272,26 +312,33 @@ class SshFs {
272
312
  return this.rmCmd(remotePath);
273
313
  }
274
314
  async readFile(remotePath, options) {
275
- const chunkSize = options?.chunkSize ?? 64 * 1024;
276
- const fileSizeOutput = await this.runCmd(`stat -c %s "${remotePath}"`);
277
- const fileSize = parseInt(fileSizeOutput.trim(), 10);
315
+ const defaultChunkSize = await this.detectChunkSize();
316
+ const chunkSize = options?.chunkSize ?? defaultChunkSize;
317
+ const lsOutput = await this.runCmd(`ls -la "${remotePath}"`);
318
+ const parts = lsOutput.trim().split(/\s+/);
319
+ const fileSize = parts.length >= 5 ? parseInt(parts[4], 10) : 0;
278
320
  if (fileSize <= chunkSize) {
279
321
  const output = await this.runCmd(`cat "${remotePath}"`);
280
322
  return output;
281
323
  }
282
324
  const chunks = [];
283
325
  for (let offset = 0; offset < fileSize; offset += chunkSize) {
284
- const cmd = `dd if="${remotePath}" bs=1K skip=${Math.floor(offset / 1024)} count=${Math.ceil(chunkSize / 1024)} 2>/dev/null`;
285
- const chunkOutput = await this.runCmd(cmd);
286
- if (chunkOutput) {
287
- chunks.push(chunkOutput);
326
+ try {
327
+ const cmd = `dd if="${remotePath}" bs=1K skip=${Math.floor(offset / 1024)} count=${Math.ceil(chunkSize / 1024)} 2>/dev/null`;
328
+ const chunkOutput = await this.runCmd(cmd);
329
+ if (chunkOutput) {
330
+ chunks.push(chunkOutput);
331
+ }
332
+ } catch {
333
+ return this.runCmd(`cat "${remotePath}"`);
288
334
  }
289
335
  }
290
336
  return chunks.join("");
291
337
  }
292
338
  async writeFile(remotePath, str, mode, _options) {
339
+ const defaultChunkSize = await this.detectChunkSize();
293
340
  const data = Buffer.from(str);
294
- const sizeThreshold = 64 * 1024;
341
+ const sizeThreshold = defaultChunkSize;
295
342
  if (data.length <= sizeThreshold) {
296
343
  const escapedContent = str.replace(/'/g, "'\\''");
297
344
  const cmd = `printf '%s' '${escapedContent}' > "${remotePath}"`;
@@ -300,7 +347,7 @@ class SshFs {
300
347
  const tempBase = `/tmp/ssh-fs-${Date.now()}`;
301
348
  await this.runCmd(`mkdir -p "${tempBase}"`);
302
349
  try {
303
- const chunkSize = 64 * 1024;
350
+ const chunkSize = defaultChunkSize;
304
351
  for (let i = 0; i < data.length; i += chunkSize) {
305
352
  const chunk = data.slice(i, i + chunkSize);
306
353
  const chunkFile = `${tempBase}/c${Math.floor(i / chunkSize)}`;
@@ -26,10 +26,12 @@ class Transfer {
26
26
  chunkSize;
27
27
  state;
28
28
  aborted = false;
29
+ sshFs;
29
30
  constructor(sshFs, options) {
31
+ this.sshFs = sshFs;
30
32
  this.session = sshFs.session;
31
33
  this.options = options;
32
- this.chunkSize = options.chunkSize || 32768;
34
+ this.chunkSize = options.chunkSize || 4096;
33
35
  this.state = {
34
36
  transferred: 0,
35
37
  total: 0,
@@ -41,6 +43,9 @@ class Transfer {
41
43
  if (this.state.completed || this.state.error || this.aborted) {
42
44
  return;
43
45
  }
46
+ if (!this.options.chunkSize) {
47
+ this.chunkSize = await this.sshFs.getChunkSize();
48
+ }
44
49
  const isDownload = this.options.type === "download";
45
50
  try {
46
51
  if (isDownload) {
@@ -20,9 +20,13 @@ export declare interface FileInfo {
20
20
 
21
21
  export declare class SshFs {
22
22
  private session;
23
+ private _chunkSize;
24
+ getChunkSize(): Promise<number>;
23
25
  constructor(session: Client, _options?: SshFsOptions);
26
+ private detectChunkSize;
24
27
  private getExecOpts;
25
28
  private getMonthIndex;
29
+ private parseMode;
26
30
  private runCmd;
27
31
  private rmFolderCmd;
28
32
  private rmCmd;
@@ -1,8 +1,16 @@
1
1
  class SshFs {
2
2
  session;
3
+ _chunkSize = null;
4
+ async getChunkSize() {
5
+ return this.detectChunkSize();
6
+ }
3
7
  constructor(session, _options) {
4
8
  this.session = session;
5
9
  }
10
+ async detectChunkSize() {
11
+ this._chunkSize = 4 * 1024;
12
+ return this._chunkSize;
13
+ }
6
14
  getExecOpts() {
7
15
  return {};
8
16
  }
@@ -10,6 +18,30 @@ class SshFs {
10
18
  const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
11
19
  return months.indexOf(month);
12
20
  }
21
+ parseMode(modeStr) {
22
+ const permMap = {
23
+ "---": 0,
24
+ "--x": 1,
25
+ "-w-": 2,
26
+ "-wx": 3,
27
+ "r--": 4,
28
+ "r-x": 5,
29
+ "rw-": 6,
30
+ "rwx": 7
31
+ };
32
+ const perms = modeStr.slice(1, 10);
33
+ const owner = permMap[perms.slice(0, 3)] || 0;
34
+ const group = permMap[perms.slice(3, 6)] || 0;
35
+ const other = permMap[perms.slice(6, 9)] || 0;
36
+ let fileType = 32768;
37
+ if (modeStr.startsWith("d")) fileType = 16384;
38
+ else if (modeStr.startsWith("l")) fileType = 40960;
39
+ else if (modeStr.startsWith("c")) fileType = 8192;
40
+ else if (modeStr.startsWith("b")) fileType = 24576;
41
+ else if (modeStr.startsWith("p")) fileType = 4096;
42
+ else if (modeStr.startsWith("s")) fileType = 49152;
43
+ return fileType | owner << 6 | group << 3 | other;
44
+ }
13
45
  runCmd(cmd, timeout = 3e4) {
14
46
  return new Promise((resolve, reject) => {
15
47
  let timeoutId;
@@ -113,11 +145,13 @@ class SshFs {
113
145
  });
114
146
  }
115
147
  async getFolderSize(folderPath) {
116
- const output = await this.runExec(`du -sh "${folderPath}" && find "${folderPath}" -type f | wc -l`);
117
- const lines = output.trim().split("\n");
118
- const size = lines[0]?.split(" ")[0] || "0";
119
- const count = parseInt(lines[1] || "0", 10);
120
- return { size, count };
148
+ try {
149
+ const output = await this.runCmd(`du -sh "${folderPath}"`);
150
+ const size = output.trim().split(" ")[0] || "0";
151
+ return { size, count: 0 };
152
+ } catch {
153
+ return { size: "0", count: 0 };
154
+ }
121
155
  }
122
156
  async listFiles(remotePath) {
123
157
  const output = await this.runCmd(`ls -la "${remotePath}"`);
@@ -174,9 +208,9 @@ class SshFs {
174
208
  }
175
209
  async stat(remotePath) {
176
210
  const isSymlink = await this.runCmd(`test -L "${remotePath}" && echo 1 || echo 0`).then((r) => r.trim() === "1");
177
- const output = await this.runCmd(`stat -c '%s %h %u %g %Y %Y %a' "${remotePath}"`);
211
+ const output = await this.runCmd(`ls -la "${remotePath}"`);
178
212
  const parts = output.trim().split(/\s+/);
179
- if (parts.length < 7) {
213
+ if (parts.length < 9) {
180
214
  return {
181
215
  size: 0,
182
216
  mode: 0,
@@ -193,36 +227,39 @@ class SshFs {
193
227
  isSocket: () => false
194
228
  };
195
229
  }
196
- const [size, _nlink, uid, gid, atime, mtime, modeOct] = parts;
197
- const mode = parseInt(modeOct, 8);
230
+ const size = parseInt(parts[4], 10);
231
+ const modeStr = parts[0];
232
+ const mode = this.parseMode(modeStr);
233
+ const uid = parseInt(parts[2], 10);
234
+ const gid = parseInt(parts[3], 10);
198
235
  return {
199
- size: parseInt(size, 10),
236
+ size,
200
237
  mode,
201
- uid: parseInt(uid, 10),
202
- gid: parseInt(gid, 10),
203
- atime: parseInt(atime, 10) * 1e3,
204
- mtime: parseInt(mtime, 10) * 1e3,
205
- isFile: () => (mode & 61440) === 32768,
206
- isDirectory: () => (mode & 61440) === 16384,
238
+ uid,
239
+ gid,
240
+ atime: 0,
241
+ mtime: 0,
242
+ isFile: () => modeStr.startsWith("-"),
243
+ isDirectory: () => modeStr.startsWith("d"),
207
244
  isSymbolicLink: () => isSymlink,
208
- isBlockDevice: () => (mode & 61440) === 24576,
209
- isCharacterDevice: () => (mode & 61440) === 8192,
210
- isFIFO: () => (mode & 61440) === 4096,
211
- isSocket: () => (mode & 61440) === 49152
245
+ isBlockDevice: () => modeStr.startsWith("b"),
246
+ isCharacterDevice: () => modeStr.startsWith("c"),
247
+ isFIFO: () => modeStr.startsWith("p"),
248
+ isSocket: () => modeStr.startsWith("s")
212
249
  };
213
250
  }
214
251
  readlink(remotePath) {
215
252
  return this.runCmd(`readlink "${remotePath}"`).then((output) => output.trim());
216
253
  }
217
254
  realpath(remotePath) {
218
- return this.runCmd(`realpath "${remotePath}"`).then((output) => output.trim());
255
+ return this.runCmd(`readlink -f "${remotePath}"`).then((output) => output.trim()).catch(() => this.runCmd(`cd "${remotePath}" && pwd`).then((output) => output.trim()));
219
256
  }
220
257
  async lstat(remotePath) {
221
258
  const output = await this.runCmd(`ls -ld "${remotePath}"`);
222
259
  const isSymlink = output.trim().startsWith("l");
223
- const statOutput = await this.runCmd(`stat -c '%s %h %u %g %Y %Y %a' "${remotePath}"`);
224
- const parts = statOutput.trim().split(/\s+/);
225
- if (parts.length < 7) {
260
+ const lsOutput = await this.runCmd(`ls -la "${remotePath}"`);
261
+ const parts = lsOutput.trim().split(/\s+/);
262
+ if (parts.length < 9) {
226
263
  return {
227
264
  size: 0,
228
265
  mode: 0,
@@ -239,22 +276,25 @@ class SshFs {
239
276
  isSocket: () => false
240
277
  };
241
278
  }
242
- const [size, _nlink, uid, gid, atime, mtime, modeOct] = parts;
243
- const mode = parseInt(modeOct, 8);
279
+ const size = parseInt(parts[4], 10);
280
+ const modeStr = parts[0];
281
+ const mode = this.parseMode(modeStr);
282
+ const uid = parseInt(parts[2], 10);
283
+ const gid = parseInt(parts[3], 10);
244
284
  return {
245
- size: parseInt(size, 10),
285
+ size,
246
286
  mode,
247
- uid: parseInt(uid, 10),
248
- gid: parseInt(gid, 10),
249
- atime: parseInt(atime, 10) * 1e3,
250
- mtime: parseInt(mtime, 10) * 1e3,
251
- isFile: () => !isSymlink && (mode & 61440) === 32768,
252
- isDirectory: () => !isSymlink && (mode & 61440) === 16384,
287
+ uid,
288
+ gid,
289
+ atime: 0,
290
+ mtime: 0,
291
+ isFile: () => !isSymlink && modeStr.startsWith("-"),
292
+ isDirectory: () => !isSymlink && modeStr.startsWith("d"),
253
293
  isSymbolicLink: () => isSymlink,
254
- isBlockDevice: () => (mode & 61440) === 24576,
255
- isCharacterDevice: () => (mode & 61440) === 8192,
256
- isFIFO: () => (mode & 61440) === 4096,
257
- isSocket: () => (mode & 61440) === 49152
294
+ isBlockDevice: () => modeStr.startsWith("b"),
295
+ isCharacterDevice: () => modeStr.startsWith("c"),
296
+ isFIFO: () => modeStr.startsWith("p"),
297
+ isSocket: () => modeStr.startsWith("s")
258
298
  };
259
299
  }
260
300
  chmod(remotePath, mode) {
@@ -270,26 +310,33 @@ class SshFs {
270
310
  return this.rmCmd(remotePath);
271
311
  }
272
312
  async readFile(remotePath, options) {
273
- const chunkSize = options?.chunkSize ?? 64 * 1024;
274
- const fileSizeOutput = await this.runCmd(`stat -c %s "${remotePath}"`);
275
- const fileSize = parseInt(fileSizeOutput.trim(), 10);
313
+ const defaultChunkSize = await this.detectChunkSize();
314
+ const chunkSize = options?.chunkSize ?? defaultChunkSize;
315
+ const lsOutput = await this.runCmd(`ls -la "${remotePath}"`);
316
+ const parts = lsOutput.trim().split(/\s+/);
317
+ const fileSize = parts.length >= 5 ? parseInt(parts[4], 10) : 0;
276
318
  if (fileSize <= chunkSize) {
277
319
  const output = await this.runCmd(`cat "${remotePath}"`);
278
320
  return output;
279
321
  }
280
322
  const chunks = [];
281
323
  for (let offset = 0; offset < fileSize; offset += chunkSize) {
282
- const cmd = `dd if="${remotePath}" bs=1K skip=${Math.floor(offset / 1024)} count=${Math.ceil(chunkSize / 1024)} 2>/dev/null`;
283
- const chunkOutput = await this.runCmd(cmd);
284
- if (chunkOutput) {
285
- chunks.push(chunkOutput);
324
+ try {
325
+ const cmd = `dd if="${remotePath}" bs=1K skip=${Math.floor(offset / 1024)} count=${Math.ceil(chunkSize / 1024)} 2>/dev/null`;
326
+ const chunkOutput = await this.runCmd(cmd);
327
+ if (chunkOutput) {
328
+ chunks.push(chunkOutput);
329
+ }
330
+ } catch {
331
+ return this.runCmd(`cat "${remotePath}"`);
286
332
  }
287
333
  }
288
334
  return chunks.join("");
289
335
  }
290
336
  async writeFile(remotePath, str, mode, _options) {
337
+ const defaultChunkSize = await this.detectChunkSize();
291
338
  const data = Buffer.from(str);
292
- const sizeThreshold = 64 * 1024;
339
+ const sizeThreshold = defaultChunkSize;
293
340
  if (data.length <= sizeThreshold) {
294
341
  const escapedContent = str.replace(/'/g, "'\\''");
295
342
  const cmd = `printf '%s' '${escapedContent}' > "${remotePath}"`;
@@ -298,7 +345,7 @@ class SshFs {
298
345
  const tempBase = `/tmp/ssh-fs-${Date.now()}`;
299
346
  await this.runCmd(`mkdir -p "${tempBase}"`);
300
347
  try {
301
- const chunkSize = 64 * 1024;
348
+ const chunkSize = defaultChunkSize;
302
349
  for (let i = 0; i < data.length; i += chunkSize) {
303
350
  const chunk = data.slice(i, i + chunkSize);
304
351
  const chunkFile = `${tempBase}/c${Math.floor(i / chunkSize)}`;
@@ -18,9 +18,13 @@ declare interface FileInfo {
18
18
 
19
19
  declare class SshFs {
20
20
  private session;
21
+ private _chunkSize;
22
+ getChunkSize(): Promise<number>;
21
23
  constructor(session: Client, _options?: SshFsOptions);
24
+ private detectChunkSize;
22
25
  private getExecOpts;
23
26
  private getMonthIndex;
27
+ private parseMode;
24
28
  private runCmd;
25
29
  private rmFolderCmd;
26
30
  private rmCmd;
@@ -82,6 +86,7 @@ export declare class Transfer {
82
86
  private chunkSize;
83
87
  private state;
84
88
  private aborted;
89
+ private sshFs;
85
90
  constructor(sshFs: SshFs, options: TransferOptions);
86
91
  startTransfer(): Promise<void>;
87
92
  private getRemoteFileSize;
@@ -6,10 +6,12 @@ class Transfer {
6
6
  chunkSize;
7
7
  state;
8
8
  aborted = false;
9
+ sshFs;
9
10
  constructor(sshFs, options) {
11
+ this.sshFs = sshFs;
10
12
  this.session = sshFs.session;
11
13
  this.options = options;
12
- this.chunkSize = options.chunkSize || 32768;
14
+ this.chunkSize = options.chunkSize || 4096;
13
15
  this.state = {
14
16
  transferred: 0,
15
17
  total: 0,
@@ -21,6 +23,9 @@ class Transfer {
21
23
  if (this.state.completed || this.state.error || this.aborted) {
22
24
  return;
23
25
  }
26
+ if (!this.options.chunkSize) {
27
+ this.chunkSize = await this.sshFs.getChunkSize();
28
+ }
24
29
  const isDownload = this.options.type === "download";
25
30
  try {
26
31
  if (isDownload) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ssh2-scp",
3
- "version": "1.1.0",
3
+ "version": "2.0.4",
4
4
  "description": "Remote file system operations over SSH2 session without SFTP",
5
5
  "scripts": {
6
6
  "build": "npm run build:esm && npm run build:cjs",
@@ -8,7 +8,8 @@
8
8
  "build:cjs": "vite build --config build/vite.config.cjs.ts --outDir dist/cjs",
9
9
  "test": "node test/ssh2-fs.test.mjs && node test/transfer.test.mjs",
10
10
  "lint": "echo \"Lint not configured\" && exit 0",
11
- "typecheck": "tsc --noEmit"
11
+ "typecheck": "tsc --noEmit",
12
+ "bench": "node benchmark/benchmark.js"
12
13
  },
13
14
  "homepage": "https://github.com/electerm/ssh2-scp#readme",
14
15
  "bugs": {
@@ -46,6 +47,7 @@
46
47
  "@types/node": "^20.10.0",
47
48
  "@types/ssh2": "^1.11.0",
48
49
  "@types/tar": "^6.1.0",
50
+ "dotenv": "^16.4.0",
49
51
  "ssh2": "^1.15.0",
50
52
  "tar": "^6.2.0",
51
53
  "typescript": "^5.3.0",
@@ -55,4 +57,4 @@
55
57
  "engines": {
56
58
  "node": ">=22.0.0"
57
59
  }
58
- }
60
+ }