ssh2-sftp-client 9.1.0 → 10.0.0
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.md +149 -140
- package/README.org +69 -20
- package/package.json +10 -10
- package/src/index.js +167 -139
- package/src/utils.js +58 -53
package/README.md
CHANGED
|
@@ -1,91 +1,90 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [Version
|
|
3
|
-
- [Installation](#
|
|
4
|
-
- [Basic Usage](#
|
|
5
|
-
- [Documentation](#
|
|
6
|
-
- [Specifying Paths](#
|
|
7
|
-
- [Methods](#
|
|
8
|
-
- [new SftpClient(name) ===> SFTP client object](#
|
|
9
|
-
- [connect(config) ===> SFTP object](#
|
|
10
|
-
- [list(path, filter) ==> Array[object]](#
|
|
11
|
-
- [exists(path) ==> boolean](#
|
|
12
|
-
- [stat(path) ==> object](#
|
|
13
|
-
- [get(path, dst, options) ==> String|Stream|Buffer](#
|
|
14
|
-
- [fastGet(remotePath, localPath, options) ===> string](#
|
|
15
|
-
- [put(src, remotePath, options) ==> string](#
|
|
16
|
-
- [fastPut(localPath, remotePath, options) ==> string](#
|
|
17
|
-
- [append(input, remotePath, options) ==> string](#
|
|
18
|
-
- [mkdir(path, recursive) ==> string](#
|
|
19
|
-
- [rmdir(path, recursive) ==> string](#
|
|
20
|
-
- [delete(path, noErrorOK) ==> string](#
|
|
21
|
-
- [rename(fromPath, toPath) ==> string](#
|
|
22
|
-
- [posixRename(fromPath, toPath) ==> string](#
|
|
23
|
-
- [chmod(path, mode) ==> string](#
|
|
24
|
-
- [realPath(path) ===> string](#
|
|
25
|
-
- [cwd() ==> string](#
|
|
26
|
-
- [uploadDir(srcDir, dstDir, options) ==> string](#
|
|
27
|
-
- [downloadDir(srcDir, dstDir, options) ==> string](#
|
|
28
|
-
- [createReadStream(remotePath, options)) ==> stream object](#
|
|
29
|
-
- [createWriteStream(remotePath, options) ==> stream object](#
|
|
30
|
-
- [rcopy(srcPath, dstPath) ==> string](#
|
|
31
|
-
- [end() ==> boolean](#
|
|
32
|
-
- [Add and Remove Listeners](#
|
|
33
|
-
- [Platform Quirks & Warnings](#
|
|
34
|
-
- [Server Capabilities](#
|
|
35
|
-
- [Issues with `fastPut()` and `fastGet()` Methods](#
|
|
36
|
-
- [Promises, Events & Managing Exceptions](#
|
|
37
|
-
- [Adding Custom Handlers](#
|
|
38
|
-
- [Windows Based Servers](#
|
|
39
|
-
- [Don't Re-use SftpClient Objects](#
|
|
40
|
-
- [FAQ](#
|
|
41
|
-
- [Remote server drops connections with only an end event](#
|
|
42
|
-
- [How can I pass writeable stream as dst for get method?](#
|
|
43
|
-
- [How can I upload files without having to specify a password?](#
|
|
44
|
-
- [How can I connect through a Socks Proxy](#
|
|
45
|
-
- [Timeout while waiting for handshake or handshake errors](#
|
|
46
|
-
- [How can I limit upload/download speed](#
|
|
47
|
-
- [Connection hangs or fails for larger files](#
|
|
48
|
-
- [Typescript definition file out of date](#
|
|
49
|
-
- [Examples](#
|
|
50
|
-
- [Troubleshooting](#
|
|
51
|
-
- [Common Errors](#
|
|
52
|
-
- [Not returning the promise in a `then()` block](#
|
|
53
|
-
- [Mixing Promise Chains and Async/Await](#
|
|
54
|
-
- [Try/catch and Error Handlers](#
|
|
55
|
-
- [Server Differences](#
|
|
56
|
-
- [Avoid Concurrent Operations](#
|
|
57
|
-
- [Debugging Support](#
|
|
58
|
-
- [Logging Issues](#
|
|
59
|
-
- [Pull Requests](#
|
|
60
|
-
- [Contributors](#
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
<a id="
|
|
1
|
+
- [Overview](#org4821cd9)
|
|
2
|
+
- [Version 10.0.0 Changes](#org196e8b6)
|
|
3
|
+
- [Installation](#orgeac23df)
|
|
4
|
+
- [Basic Usage](#orgda0ed58)
|
|
5
|
+
- [Documentation](#orgd0ca540)
|
|
6
|
+
- [Specifying Paths](#orgabfbdff)
|
|
7
|
+
- [Methods](#org35528cc)
|
|
8
|
+
- [new SftpClient(name) ===> SFTP client object](#org6d917af)
|
|
9
|
+
- [connect(config) ===> SFTP object](#org3ca98bb)
|
|
10
|
+
- [list(path, filter) ==> Array[object]](#org067b5c0)
|
|
11
|
+
- [exists(path) ==> boolean](#orgfde631c)
|
|
12
|
+
- [stat(path) ==> object](#orge6ccca9)
|
|
13
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#org63cf95f)
|
|
14
|
+
- [fastGet(remotePath, localPath, options) ===> string](#orga32496c)
|
|
15
|
+
- [put(src, remotePath, options) ==> string](#org6bdfd59)
|
|
16
|
+
- [fastPut(localPath, remotePath, options) ==> string](#org4296214)
|
|
17
|
+
- [append(input, remotePath, options) ==> string](#org01aa427)
|
|
18
|
+
- [mkdir(path, recursive) ==> string](#orge74ab72)
|
|
19
|
+
- [rmdir(path, recursive) ==> string](#org2692b91)
|
|
20
|
+
- [delete(path, noErrorOK) ==> string](#org59254a5)
|
|
21
|
+
- [rename(fromPath, toPath) ==> string](#org287873a)
|
|
22
|
+
- [posixRename(fromPath, toPath) ==> string](#orgb7290ba)
|
|
23
|
+
- [chmod(path, mode) ==> string](#org0e57abe)
|
|
24
|
+
- [realPath(path) ===> string](#org52444ca)
|
|
25
|
+
- [cwd() ==> string](#org34966e3)
|
|
26
|
+
- [uploadDir(srcDir, dstDir, options) ==> string](#org5c58fad)
|
|
27
|
+
- [downloadDir(srcDir, dstDir, options) ==> string](#orgb346779)
|
|
28
|
+
- [createReadStream(remotePath, options)) ==> stream object](#orga0edaaf)
|
|
29
|
+
- [createWriteStream(remotePath, options) ==> stream object](#org7287ad7)
|
|
30
|
+
- [rcopy(srcPath, dstPath) ==> string](#org4b84e95)
|
|
31
|
+
- [end() ==> boolean](#org6af9411)
|
|
32
|
+
- [Add and Remove Listeners](#org79d7176)
|
|
33
|
+
- [Platform Quirks & Warnings](#org8815934)
|
|
34
|
+
- [Server Capabilities](#orgcf08239)
|
|
35
|
+
- [Issues with `fastPut()` and `fastGet()` Methods](#org92f1dc4)
|
|
36
|
+
- [Promises, Events & Managing Exceptions](#org27f104a)
|
|
37
|
+
- [Adding Custom Handlers](#orgd903953)
|
|
38
|
+
- [Windows Based Servers](#orged5ee01)
|
|
39
|
+
- [Don't Re-use SftpClient Objects](#orgfafec91)
|
|
40
|
+
- [FAQ](#orgbe306ce)
|
|
41
|
+
- [Remote server drops connections with only an end event](#org9377d69)
|
|
42
|
+
- [How can I pass writeable stream as dst for get method?](#org0cf3ed0)
|
|
43
|
+
- [How can I upload files without having to specify a password?](#org14d1415)
|
|
44
|
+
- [How can I connect through a Socks Proxy](#org118bb9f)
|
|
45
|
+
- [Timeout while waiting for handshake or handshake errors](#orgc720a77)
|
|
46
|
+
- [How can I limit upload/download speed](#orge322728)
|
|
47
|
+
- [Connection hangs or fails for larger files](#org4164c6c)
|
|
48
|
+
- [Typescript definition file out of date](#orgd28c1e8)
|
|
49
|
+
- [Examples](#orgfab3156)
|
|
50
|
+
- [Troubleshooting](#orga11079b)
|
|
51
|
+
- [Common Errors](#org0372c41)
|
|
52
|
+
- [Not returning the promise in a `then()` block](#org0d8dc85)
|
|
53
|
+
- [Mixing Promise Chains and Async/Await](#org199b814)
|
|
54
|
+
- [Try/catch and Error Handlers](#org46e5412)
|
|
55
|
+
- [Server Differences](#org8a2ebba)
|
|
56
|
+
- [Avoid Concurrent Operations](#org24c4235)
|
|
57
|
+
- [Debugging Support](#org1d067af)
|
|
58
|
+
- [Logging Issues](#orgdc52f71)
|
|
59
|
+
- [Pull Requests](#orge4e0c24)
|
|
60
|
+
- [Contributors](#org7f7c9a1)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
<a id="org4821cd9"></a>
|
|
65
65
|
|
|
66
66
|
# Overview
|
|
67
67
|
|
|
68
68
|
an SFTP client for node.js, a wrapper around [SSH2](https://github.com/mscdex/ssh2) which provides a high level convenience abstraction as well as a Promise based API.
|
|
69
69
|
|
|
70
|
-
Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) project pages.
|
|
70
|
+
Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) project pages. As this module is really just a wrapper around the `ssh2` module, you will find lots of useful information, tips and examples in the `ssh2` repository.
|
|
71
71
|
|
|
72
|
-
Current stable release is \*
|
|
72
|
+
Current stable release is \*v10.0.0.
|
|
73
73
|
|
|
74
|
-
Code has been tested against Node versions
|
|
74
|
+
Code has been tested against Node versions 16.20.2, 18.18.2, 20.10.0 and 21.5.0. However, only versions from v18 are actively supported. It should also be noted that a significant performance improvement has been observed with versions >= 18. Version v16 is significantly slower.
|
|
75
75
|
|
|
76
|
-
Node versions <
|
|
76
|
+
Node versions < 16.x are not supported.
|
|
77
77
|
|
|
78
78
|
|
|
79
|
-
<a id="
|
|
79
|
+
<a id="org196e8b6"></a>
|
|
80
80
|
|
|
81
|
-
## Version
|
|
81
|
+
## Version 10.0.0 Changes
|
|
82
82
|
|
|
83
|
-
-
|
|
84
|
-
-
|
|
85
|
-
- Fixed bug where a loss of network connections between establishment of the connection and calling various sftp methods was not handled and could result in an event causing the node process to exit with an error.
|
|
83
|
+
- The main change in this version is adding of limits on the number of promises which can be active at the same time. Version 9.1.0 extended the use of multiple promises to improve performance with downloadDir() and uploadDir(). However, for directories with really large numbers of files, this often resulted in an error because the methods would try to create more concurrent promises than was possible given available resources. This issue has been fixed by adding a new property called `promiseLimit`, which is limited to 10 by default. A new configuration property, `promiseLimit`, is now available for setting the maximum number of concurrent promises the downloadDir()/uploadDir() methods will create when downloading or uploading directory trees.
|
|
84
|
+
- Various minor documentation fixes and some minor fixes for typos in option property names.
|
|
86
85
|
|
|
87
86
|
|
|
88
|
-
<a id="
|
|
87
|
+
<a id="orgeac23df"></a>
|
|
89
88
|
|
|
90
89
|
# Installation
|
|
91
90
|
|
|
@@ -94,7 +93,7 @@ npm install ssh2-sftp-client
|
|
|
94
93
|
```
|
|
95
94
|
|
|
96
95
|
|
|
97
|
-
<a id="
|
|
96
|
+
<a id="orgda0ed58"></a>
|
|
98
97
|
|
|
99
98
|
# Basic Usage
|
|
100
99
|
|
|
@@ -117,16 +116,18 @@ sftp.connect({
|
|
|
117
116
|
```
|
|
118
117
|
|
|
119
118
|
|
|
120
|
-
<a id="
|
|
119
|
+
<a id="orgd0ca540"></a>
|
|
121
120
|
|
|
122
121
|
# Documentation
|
|
123
122
|
|
|
124
|
-
The connection options are the same as those offered by the underlying SSH2 module. For full details, please see [SSH2 client methods](https://github.com/mscdex/ssh2#user-content-client-methods)
|
|
123
|
+
The connection options are the same as those offered by the underlying SSH2 module, with just a couple of additional properties added to tweak the `retry` parameters, add a `debug` function and set the `promiseLimit` property. For full details on the other properties, please see [SSH2 client methods](https://github.com/mscdex/ssh2#user-content-client-methods). In particular, see the `ssh2` documentation for details relating to setting various key exchange and encryption/signing algorithms used as part of the ssh2 protocol.
|
|
125
124
|
|
|
126
|
-
All the methods will return a Promise, except for `on()` and `
|
|
125
|
+
All the methods will return a Promise, except for `on(), ~removeListener()`, `createReadStream` and `createWriteStream`, which are typically only used in special use cases.
|
|
127
126
|
|
|
127
|
+
Note that I don't use Typescript and I don't maintain any typescript definition files. There are some typescript type definition files for this module, but they are maintained separately and have nothing to do with this project. Therefore, please do not log any issues arising from the use of these definition files with this project. Instead, refer your issues to the maintainers of those modules.
|
|
128
128
|
|
|
129
|
-
|
|
129
|
+
|
|
130
|
+
<a id="orgabfbdff"></a>
|
|
130
131
|
|
|
131
132
|
## Specifying Paths
|
|
132
133
|
|
|
@@ -159,12 +160,12 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
159
160
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
160
161
|
|
|
161
162
|
|
|
162
|
-
<a id="
|
|
163
|
+
<a id="org35528cc"></a>
|
|
163
164
|
|
|
164
165
|
## Methods
|
|
165
166
|
|
|
166
167
|
|
|
167
|
-
<a id="
|
|
168
|
+
<a id="org6d917af"></a>
|
|
168
169
|
|
|
169
170
|
### new SftpClient(name) ===> SFTP client object
|
|
170
171
|
|
|
@@ -203,7 +204,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
203
204
|
```
|
|
204
205
|
|
|
205
206
|
|
|
206
|
-
<a id="
|
|
207
|
+
<a id="org3ca98bb"></a>
|
|
207
208
|
|
|
208
209
|
### connect(config) ===> SFTP object
|
|
209
210
|
|
|
@@ -215,6 +216,8 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
215
216
|
|
|
216
217
|
The `retries`, `retry_factor` and `retry_minTimeout` options are not part of the SSH2 module. These are part of the configuration for the [retry](https://www.npmjs.com/package/retry) package and what is used to enable retrying of sftp connection attempts. See the documentation for that package for an explanation of these values.
|
|
217
218
|
|
|
219
|
+
The `promiseLimit` is another option which is not part of the `ssh2` module and is specific to `ssh2-sftp-client`. It is a property used to limit the maximum number of concurrent promises possible when either downloading or uploading a directory tree using the `downloadDir()` or `uploadDir()` methods. The default setting for this property is 10. **NOTE**: bigger doe snot mean better. Many factors can affect what is the ideal setting for `promiseLimit`. If it is too large, any benefits are lost while node spends time switching contexts and/or withi the overheads associated with creating and cleaning up promises. Lots of factors can affect what the setting should be, including size of files, number of files, speed of network, version of node, capabilities of remote sftp server etc. A setting of 10 seems to be a reasonably good default and should be adequate for most use cases. However, if you feel it needs to be changed, I highly recommend that you benchmark different values to work out what is the best maximum size before you begin to see a performance drop off.
|
|
220
|
+
|
|
218
221
|
```javascript
|
|
219
222
|
// common options
|
|
220
223
|
|
|
@@ -235,6 +238,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
235
238
|
retries: 2, // integer. Number of times to retry connecting
|
|
236
239
|
retry_factor: 2, // integer. Time factor used to calculate time between retries
|
|
237
240
|
retry_minTimeout: 2000, // integer. Minimum timeout between attempts
|
|
241
|
+
promiseLimit: 10, // max concurrent promises for downloadDir/uploadDir
|
|
238
242
|
};
|
|
239
243
|
|
|
240
244
|
// rarely used options
|
|
@@ -269,7 +273,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
269
273
|
```
|
|
270
274
|
|
|
271
275
|
|
|
272
|
-
<a id="
|
|
276
|
+
<a id="org067b5c0"></a>
|
|
273
277
|
|
|
274
278
|
### list(path, filter) ==> Array[object]
|
|
275
279
|
|
|
@@ -330,7 +334,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
330
334
|
```
|
|
331
335
|
|
|
332
336
|
|
|
333
|
-
<a id="
|
|
337
|
+
<a id="orgfde631c"></a>
|
|
334
338
|
|
|
335
339
|
### exists(path) ==> boolean
|
|
336
340
|
|
|
@@ -366,7 +370,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
366
370
|
```
|
|
367
371
|
|
|
368
372
|
|
|
369
|
-
<a id="
|
|
373
|
+
<a id="orge6ccca9"></a>
|
|
370
374
|
|
|
371
375
|
### stat(path) ==> object
|
|
372
376
|
|
|
@@ -417,7 +421,7 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
417
421
|
```
|
|
418
422
|
|
|
419
423
|
|
|
420
|
-
<a id="
|
|
424
|
+
<a id="org63cf95f"></a>
|
|
421
425
|
|
|
422
426
|
### get(path, dst, options) ==> String|Stream|Buffer
|
|
423
427
|
|
|
@@ -473,11 +477,13 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
473
477
|
- **Tip:** See examples file in the Git repository for more examples. You can pass any writeable stream in as the destination. For example, if you pass in `zlib.createGunzip()` writeable stream, you can both download and decompress a gzip file 'on the fly'.
|
|
474
478
|
|
|
475
479
|
|
|
476
|
-
<a id="
|
|
480
|
+
<a id="orga32496c"></a>
|
|
477
481
|
|
|
478
482
|
### fastGet(remotePath, localPath, options) ===> string
|
|
479
483
|
|
|
480
|
-
Downloads a file at remotePath to localPath using parallel reads for faster throughput. This is the simplest method if you just want to download a file.
|
|
484
|
+
Downloads a file at remotePath to localPath using parallel reads for faster throughput. This is the simplest method if you just want to download a file. However, fastGet functionality depends heavily on remote sftp server capabilities and not all servers have the concurrency support required. See the Platform Quirks & Warnings section of this README.
|
|
485
|
+
|
|
486
|
+
Bottom line, when it works, it tends to work reliably. However, for many servers, it simply won't work or will result in truncated/corrupted data.
|
|
481
487
|
|
|
482
488
|
- **remotePath:** String. Path to the remote file to download
|
|
483
489
|
- **localPath:** String. Path on local file system for the downloaded file. The local path should include the filename to use for saving the file.
|
|
@@ -516,7 +522,7 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
|
|
|
516
522
|
```
|
|
517
523
|
|
|
518
524
|
|
|
519
|
-
<a id="
|
|
525
|
+
<a id="org6bdfd59"></a>
|
|
520
526
|
|
|
521
527
|
### put(src, remotePath, options) ==> string
|
|
522
528
|
|
|
@@ -566,11 +572,13 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
566
572
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
567
573
|
|
|
568
574
|
|
|
569
|
-
<a id="
|
|
575
|
+
<a id="org4296214"></a>
|
|
570
576
|
|
|
571
577
|
### fastPut(localPath, remotePath, options) ==> string
|
|
572
578
|
|
|
573
|
-
Uploads the data in file at `localPath` to a new file on remote server at `remotePath` using concurrency. The options object allows tweaking of the fast put process.
|
|
579
|
+
Uploads the data in file at `localPath` to a new file on remote server at `remotePath` using concurrency. The options object allows tweaking of the fast put process. Note that this functionality is heavily dependent on the capabilities of the remote sftp server, which must support the concurrency operations used by this method. This is not part of the standard and therefore is not available in all sftp servers. See the Platform Quirks & Warnings for more details.
|
|
580
|
+
|
|
581
|
+
Bottom line, when it works, it tends to work well. However, when it doesn't work, it may fail completely or it may result in truncated or corrupted data transfers.
|
|
574
582
|
|
|
575
583
|
- **localPath:** string. Path to local file to upload
|
|
576
584
|
- **remotePath:** string. Path to remote file to create
|
|
@@ -610,7 +618,7 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
|
|
|
610
618
|
```
|
|
611
619
|
|
|
612
620
|
|
|
613
|
-
<a id="
|
|
621
|
+
<a id="org01aa427"></a>
|
|
614
622
|
|
|
615
623
|
### append(input, remotePath, options) ==> string
|
|
616
624
|
|
|
@@ -654,7 +662,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
654
662
|
```
|
|
655
663
|
|
|
656
664
|
|
|
657
|
-
<a id="
|
|
665
|
+
<a id="orge74ab72"></a>
|
|
658
666
|
|
|
659
667
|
### mkdir(path, recursive) ==> string
|
|
660
668
|
|
|
@@ -682,7 +690,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
682
690
|
```
|
|
683
691
|
|
|
684
692
|
|
|
685
|
-
<a id="
|
|
693
|
+
<a id="org2692b91"></a>
|
|
686
694
|
|
|
687
695
|
### rmdir(path, recursive) ==> string
|
|
688
696
|
|
|
@@ -712,7 +720,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
712
720
|
```
|
|
713
721
|
|
|
714
722
|
|
|
715
|
-
<a id="
|
|
723
|
+
<a id="org59254a5"></a>
|
|
716
724
|
|
|
717
725
|
### delete(path, noErrorOK) ==> string
|
|
718
726
|
|
|
@@ -741,7 +749,7 @@ Delete a file on the remote server.
|
|
|
741
749
|
```
|
|
742
750
|
|
|
743
751
|
|
|
744
|
-
<a id="
|
|
752
|
+
<a id="org287873a"></a>
|
|
745
753
|
|
|
746
754
|
### rename(fromPath, toPath) ==> string
|
|
747
755
|
|
|
@@ -770,7 +778,7 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
770
778
|
```
|
|
771
779
|
|
|
772
780
|
|
|
773
|
-
<a id="
|
|
781
|
+
<a id="orgb7290ba"></a>
|
|
774
782
|
|
|
775
783
|
### posixRename(fromPath, toPath) ==> string
|
|
776
784
|
|
|
@@ -797,7 +805,7 @@ client.connect(config)
|
|
|
797
805
|
```
|
|
798
806
|
|
|
799
807
|
|
|
800
|
-
<a id="
|
|
808
|
+
<a id="org0e57abe"></a>
|
|
801
809
|
|
|
802
810
|
### chmod(path, mode) ==> string
|
|
803
811
|
|
|
@@ -826,7 +834,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
826
834
|
```
|
|
827
835
|
|
|
828
836
|
|
|
829
|
-
<a id="
|
|
837
|
+
<a id="org52444ca"></a>
|
|
830
838
|
|
|
831
839
|
### realPath(path) ===> string
|
|
832
840
|
|
|
@@ -837,14 +845,14 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
837
845
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
838
846
|
|
|
839
847
|
|
|
840
|
-
<a id="
|
|
848
|
+
<a id="org34966e3"></a>
|
|
841
849
|
|
|
842
850
|
### cwd() ==> string
|
|
843
851
|
|
|
844
852
|
Returns what the server believes is the current remote working directory.
|
|
845
853
|
|
|
846
854
|
|
|
847
|
-
<a id="
|
|
855
|
+
<a id="org5c58fad"></a>
|
|
848
856
|
|
|
849
857
|
### uploadDir(srcDir, dstDir, options) ==> string
|
|
850
858
|
|
|
@@ -913,7 +921,7 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
|
|
|
913
921
|
```
|
|
914
922
|
|
|
915
923
|
|
|
916
|
-
<a id="
|
|
924
|
+
<a id="orgb346779"></a>
|
|
917
925
|
|
|
918
926
|
### downloadDir(srcDir, dstDir, options) ==> string
|
|
919
927
|
|
|
@@ -978,7 +986,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
|
|
|
978
986
|
```
|
|
979
987
|
|
|
980
988
|
|
|
981
|
-
<a id="
|
|
989
|
+
<a id="orga0edaaf"></a>
|
|
982
990
|
|
|
983
991
|
### createReadStream(remotePath, options)) ==> stream object
|
|
984
992
|
|
|
@@ -995,7 +1003,7 @@ Returns a read stream object which is attached to the remote file specified by t
|
|
|
995
1003
|
- **end:** Position to stop reading bytes (inclusive).
|
|
996
1004
|
|
|
997
1005
|
|
|
998
|
-
<a id="
|
|
1006
|
+
<a id="org7287ad7"></a>
|
|
999
1007
|
|
|
1000
1008
|
### createWriteStream(remotePath, options) ==> stream object
|
|
1001
1009
|
|
|
@@ -1010,7 +1018,7 @@ Returns a write stream object which is attached to the remote file specified in
|
|
|
1010
1018
|
- **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
|
|
1011
1019
|
|
|
1012
1020
|
|
|
1013
|
-
<a id="
|
|
1021
|
+
<a id="org4b84e95"></a>
|
|
1014
1022
|
|
|
1015
1023
|
### rcopy(srcPath, dstPath) ==> string
|
|
1016
1024
|
|
|
@@ -1020,7 +1028,7 @@ Perform a remote file copy. The file identified by the `srcPath` argument will b
|
|
|
1020
1028
|
- **dstPath:** Path to where the copy will be created specified as a string
|
|
1021
1029
|
|
|
1022
1030
|
|
|
1023
|
-
<a id="
|
|
1031
|
+
<a id="org6af9411"></a>
|
|
1024
1032
|
|
|
1025
1033
|
### end() ==> boolean
|
|
1026
1034
|
|
|
@@ -1044,7 +1052,7 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
1044
1052
|
```
|
|
1045
1053
|
|
|
1046
1054
|
|
|
1047
|
-
<a id="
|
|
1055
|
+
<a id="org79d7176"></a>
|
|
1048
1056
|
|
|
1049
1057
|
### Add and Remove Listeners
|
|
1050
1058
|
|
|
@@ -1065,12 +1073,12 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
1065
1073
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
1066
1074
|
|
|
1067
1075
|
|
|
1068
|
-
<a id="
|
|
1076
|
+
<a id="org8815934"></a>
|
|
1069
1077
|
|
|
1070
1078
|
# Platform Quirks & Warnings
|
|
1071
1079
|
|
|
1072
1080
|
|
|
1073
|
-
<a id="
|
|
1081
|
+
<a id="orgcf08239"></a>
|
|
1074
1082
|
|
|
1075
1083
|
## Server Capabilities
|
|
1076
1084
|
|
|
@@ -1079,7 +1087,7 @@ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-
|
|
|
1079
1087
|
One way to determine whether an issue you are encountering is due to `ssh2-sftp-client` or due to the remote server or server platform is to use a simple CLI sftp program, such as openSSH's sftp command. If you observe the same behaviour using plain `sftp` on the command line, the issue is likely due to server or remote platform limitations. Note that you should not use a GUI sftp client, like `Filezilla` or `winSCP` as such GUI programs often attempt to hide these server and platform incompatibilities and will take additional steps to simulate missing functionality etc. You want to use a CLI program which does as little as possible.
|
|
1080
1088
|
|
|
1081
1089
|
|
|
1082
|
-
<a id="
|
|
1090
|
+
<a id="org92f1dc4"></a>
|
|
1083
1091
|
|
|
1084
1092
|
## Issues with `fastPut()` and `fastGet()` Methods
|
|
1085
1093
|
|
|
@@ -1088,7 +1096,7 @@ The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SF
|
|
|
1088
1096
|
To see an example of the type of issues you can observe with `fastPut()` or `fastGet()`, have a look at [issue 407](https://github.com/theophilusx/ssh2-sftp-client/issues/407), which describes the experiences of one user. Bottom line, when it works, it tends to work well and be significantly faster than using just `get()` or `put()`. However, when developing code to run against different SFTP servers, especially where you are unable to test against each server, you are likely better off just using `get()` and `put()` or structuring your code so that users can select which method to use (this is what `ssh2-sftp-client` does - for example, see the `!downloadDir()` and `uploadDir()` methods.
|
|
1089
1097
|
|
|
1090
1098
|
|
|
1091
|
-
<a id="
|
|
1099
|
+
<a id="org27f104a"></a>
|
|
1092
1100
|
|
|
1093
1101
|
## Promises, Events & Managing Exceptions
|
|
1094
1102
|
|
|
@@ -1107,14 +1115,14 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
1107
1115
|
In addition to the promise based event handlers, `ssh2-sftp-client` also implements global event handlers which will catch any `error`, `end` or `close` events. Essentially, these global handlers only reset the `sftp` property of the client object, effectively ensuring any subsequent calls are rejected and in the case of an error, send the error to the console.
|
|
1108
1116
|
|
|
1109
1117
|
|
|
1110
|
-
<a id="
|
|
1118
|
+
<a id="orgd903953"></a>
|
|
1111
1119
|
|
|
1112
1120
|
### Adding Custom Handlers
|
|
1113
1121
|
|
|
1114
1122
|
While the above strategies appear to work for the majority of use cases, there are always going to be edge cases which require more flexible or powerful event handling. To support this, the `on()` and `removeListener()` methods are provided. Any event listener added using the `on()` method will be added at the beginning of the list of handlers for that event, ensuring it will be called before any global or promise local events. See the documentation for the `on()` method for details.
|
|
1115
1123
|
|
|
1116
1124
|
|
|
1117
|
-
<a id="
|
|
1125
|
+
<a id="orged5ee01"></a>
|
|
1118
1126
|
|
|
1119
1127
|
## Windows Based Servers
|
|
1120
1128
|
|
|
@@ -1123,7 +1131,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
|
|
|
1123
1131
|
The best way to avoid this issue is to not re-use client objects. Always generate a new sftp client object for each new connection.
|
|
1124
1132
|
|
|
1125
1133
|
|
|
1126
|
-
<a id="
|
|
1134
|
+
<a id="orgfafec91"></a>
|
|
1127
1135
|
|
|
1128
1136
|
## Don't Re-use SftpClient Objects
|
|
1129
1137
|
|
|
@@ -1132,12 +1140,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
|
|
|
1132
1140
|
To avoid this problem, don't re-use SftpClient objects. Generate a new SftpClient object for each connection. You can perform multiple actions with a single connection e.g. upload multiple files, download multiple files etc, but after you have called end(), you should not try to re-use the object with a further connect() call. Create a new object instead.
|
|
1133
1141
|
|
|
1134
1142
|
|
|
1135
|
-
<a id="
|
|
1143
|
+
<a id="orgbe306ce"></a>
|
|
1136
1144
|
|
|
1137
1145
|
# FAQ
|
|
1138
1146
|
|
|
1139
1147
|
|
|
1140
|
-
<a id="
|
|
1148
|
+
<a id="org9377d69"></a>
|
|
1141
1149
|
|
|
1142
1150
|
## Remote server drops connections with only an end event
|
|
1143
1151
|
|
|
@@ -1148,7 +1156,7 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
1148
1156
|
One way to avoid this type of issue is to add a delay between connection attempts. It does not need to be a very long delay - just sufficient to permit the previous connection to be authenticated. In fact, the default setting for openSSH is `10:30:60`, so you really just need to have enough delay to ensure that the 1st connection has completed authentication before the 11th connection is attempted.
|
|
1149
1157
|
|
|
1150
1158
|
|
|
1151
|
-
<a id="
|
|
1159
|
+
<a id="org0cf3ed0"></a>
|
|
1152
1160
|
|
|
1153
1161
|
## How can I pass writeable stream as dst for get method?
|
|
1154
1162
|
|
|
@@ -1207,7 +1215,7 @@ sftp
|
|
|
1207
1215
|
```
|
|
1208
1216
|
|
|
1209
1217
|
|
|
1210
|
-
<a id="
|
|
1218
|
+
<a id="org14d1415"></a>
|
|
1211
1219
|
|
|
1212
1220
|
## How can I upload files without having to specify a password?
|
|
1213
1221
|
|
|
@@ -1242,12 +1250,14 @@ sftp.connect({
|
|
|
1242
1250
|
```
|
|
1243
1251
|
|
|
1244
1252
|
|
|
1245
|
-
<a id="
|
|
1253
|
+
<a id="org118bb9f"></a>
|
|
1246
1254
|
|
|
1247
1255
|
## How can I connect through a Socks Proxy
|
|
1248
1256
|
|
|
1249
1257
|
This solution was provided by @jmorino.
|
|
1250
1258
|
|
|
1259
|
+
When a SOCKS 5 client is connected it must be ingested by ssh2-sftp-client immediately, otherwise a timeout occurs.
|
|
1260
|
+
|
|
1251
1261
|
```javascript
|
|
1252
1262
|
import { SocksClient } from 'socks';
|
|
1253
1263
|
import SFTPClient from 'ssh2-sftp-client';
|
|
@@ -1256,7 +1266,7 @@ const host = 'my-sftp-server.net';
|
|
|
1256
1266
|
const port = 22; // default SSH/SFTP port on remote server
|
|
1257
1267
|
|
|
1258
1268
|
// connect to SOCKS 5 proxy
|
|
1259
|
-
const {
|
|
1269
|
+
const { socket } = await SocksClient.createConnection({
|
|
1260
1270
|
proxy: {
|
|
1261
1271
|
host: 'my.proxy', // proxy hostname
|
|
1262
1272
|
port: 1080, // proxy port
|
|
@@ -1269,16 +1279,15 @@ const { socks } = await SocksClient.createConnection({
|
|
|
1269
1279
|
const client = new SFTPClient();
|
|
1270
1280
|
client.connect({
|
|
1271
1281
|
host,
|
|
1272
|
-
sock:
|
|
1273
|
-
|
|
1274
|
-
privateKey: '.....'
|
|
1282
|
+
sock: socket, // pass the socket to proxy here (see ssh2 doc)
|
|
1283
|
+
// other config options
|
|
1275
1284
|
})
|
|
1276
1285
|
|
|
1277
1286
|
// client is connected
|
|
1278
1287
|
```
|
|
1279
1288
|
|
|
1280
1289
|
|
|
1281
|
-
<a id="
|
|
1290
|
+
<a id="orgc720a77"></a>
|
|
1282
1291
|
|
|
1283
1292
|
## Timeout while waiting for handshake or handshake errors
|
|
1284
1293
|
|
|
@@ -1287,7 +1296,7 @@ Some users have encountered the error 'Timeout while waiting for handshake' or '
|
|
|
1287
1296
|
When encountering this type of problem, one worthwhile approach is to use openSSH's CLI sftp program with the `-v` switch to raise logging levels. This will show you what algorithms the CLI is using. You can then use this information to match the names with the accepted algorithm names documented in the `ssh2` README to set the properties in the `algorithms` object.
|
|
1288
1297
|
|
|
1289
1298
|
|
|
1290
|
-
<a id="
|
|
1299
|
+
<a id="orge322728"></a>
|
|
1291
1300
|
|
|
1292
1301
|
## How can I limit upload/download speed
|
|
1293
1302
|
|
|
@@ -1327,7 +1336,7 @@ try {
|
|
|
1327
1336
|
```
|
|
1328
1337
|
|
|
1329
1338
|
|
|
1330
|
-
<a id="
|
|
1339
|
+
<a id="org4164c6c"></a>
|
|
1331
1340
|
|
|
1332
1341
|
## Connection hangs or fails for larger files
|
|
1333
1342
|
|
|
@@ -1338,21 +1347,21 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1338
1347
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1339
1348
|
|
|
1340
1349
|
|
|
1341
|
-
<a id="
|
|
1350
|
+
<a id="orgd28c1e8"></a>
|
|
1342
1351
|
|
|
1343
1352
|
## Typescript definition file out of date
|
|
1344
1353
|
|
|
1345
1354
|
This project does not use Typescript. However, typescript definition files are provided by other 3rd parties. Sometimes, these definition files have not stayed up-to-date with the current version of this module. If you encounter this issue, you need to report it to the party responsible for the definition file, not this project.
|
|
1346
1355
|
|
|
1347
1356
|
|
|
1348
|
-
<a id="
|
|
1357
|
+
<a id="orgfab3156"></a>
|
|
1349
1358
|
|
|
1350
1359
|
# Examples
|
|
1351
1360
|
|
|
1352
1361
|
I have started collecting example scripts in the example directory of the repository. These are mainly scripts I have put together in order to investigate issues or provide samples for users. They are not robust, lack adequate error handling and may contain errors. However, I think they are still useful for helping developers see how the module and API can be used.
|
|
1353
1362
|
|
|
1354
1363
|
|
|
1355
|
-
<a id="
|
|
1364
|
+
<a id="orga11079b"></a>
|
|
1356
1365
|
|
|
1357
1366
|
# Troubleshooting
|
|
1358
1367
|
|
|
@@ -1367,14 +1376,14 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1367
1376
|
The second directory is the validation directory. I have some very simple scripts in this directory which perform basic tasks using only the `ssh2` modules (no `ssh2-sftp-client` module). These can be useful when trying to determine if the issue is with the underlying `ssh2` module or the `ssh2-sftp-client` wrapper module.
|
|
1368
1377
|
|
|
1369
1378
|
|
|
1370
|
-
<a id="
|
|
1379
|
+
<a id="org0372c41"></a>
|
|
1371
1380
|
|
|
1372
1381
|
## Common Errors
|
|
1373
1382
|
|
|
1374
1383
|
There are some common errors people tend to make when using Promises or Async/Await. These are by far the most common problem found in issues logged against this module. Please check for some of these before logging your issue.
|
|
1375
1384
|
|
|
1376
1385
|
|
|
1377
|
-
<a id="
|
|
1386
|
+
<a id="org0d8dc85"></a>
|
|
1378
1387
|
|
|
1379
1388
|
### Not returning the promise in a `then()` block
|
|
1380
1389
|
|
|
@@ -1411,7 +1420,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1411
1420
|
A common symptom of this type of error is for file uploads or download to fail to complete or for data in those files to be truncated. What is happening is that the connection is being ended before the transfer has completed.
|
|
1412
1421
|
|
|
1413
1422
|
|
|
1414
|
-
<a id="
|
|
1423
|
+
<a id="org199b814"></a>
|
|
1415
1424
|
|
|
1416
1425
|
### Mixing Promise Chains and Async/Await
|
|
1417
1426
|
|
|
@@ -1471,7 +1480,7 @@ async function doSftp() {
|
|
|
1471
1480
|
```
|
|
1472
1481
|
|
|
1473
1482
|
|
|
1474
|
-
<a id="
|
|
1483
|
+
<a id="org46e5412"></a>
|
|
1475
1484
|
|
|
1476
1485
|
### Try/catch and Error Handlers
|
|
1477
1486
|
|
|
@@ -1482,14 +1491,14 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1482
1491
|
Error events are essentially asynchronous code. You don't know when such events will fire. Therefore, you cannot use a try/catch block to catch such event errors. Even creating an error handler which then throws an exception won't help as the key problem is that your try/catch block has already executed. There are a number of alternative ways to deal with this situation. However, the key symptom is that you see occasional uncaught error exceptions that cause your script to exit abnormally despite having try/catch blocks in your script. What you need to do is look at your code and find where errors are raised asynchronously and use an event handler or some other mechanism to manage any errors raised.
|
|
1483
1492
|
|
|
1484
1493
|
|
|
1485
|
-
<a id="
|
|
1494
|
+
<a id="org8a2ebba"></a>
|
|
1486
1495
|
|
|
1487
1496
|
### Server Differences
|
|
1488
1497
|
|
|
1489
1498
|
Not all SFTP servers are the same. Like most standards, the SFTP protocol has some level of interpretation and allows different levels of compliance. This means there can be differences in behaviour between different servers and code which works with one server will not work the same with another. For example, the value returned by *realpath* for non-existent objects can differ significantly. Some servers will throw an error for a particular operation while others will just return null, some servers support concurrent operations (such as used by fastGet/fastPut) while others will not and of course, the text of error messages can vary significantly. In particular, we have noticed significant differences across different platforms. It is therefore advisable to do comprehensive testing when the SFTP server is moved to a new platform. This includes moving from to a cloud based service even if the underlying platform remains the same. I have noticed that some cloud platforms can generate unexpected events, possibly related to additional functionality or features associated with the cloud implementation. For example, it appears SFTP servers running under Azure will generate an error event when the connection is closed even when the client has requested the connection be terminated. The same SFTP server running natively on Windows does not appear to exhibit such behaviour.
|
|
1490
1499
|
|
|
1491
1500
|
|
|
1492
|
-
<a id="
|
|
1501
|
+
<a id="org24c4235"></a>
|
|
1493
1502
|
|
|
1494
1503
|
### Avoid Concurrent Operations
|
|
1495
1504
|
|
|
@@ -1498,7 +1507,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
|
|
|
1498
1507
|
If you are going to try and perform concurrent operations, you need to test extensively and ensure you are using data which is large enough that context switching does occur (i.e. the request is not completed in a single run). Some SFTP servers will handle concurrent operations better than others.
|
|
1499
1508
|
|
|
1500
1509
|
|
|
1501
|
-
<a id="
|
|
1510
|
+
<a id="org1d067af"></a>
|
|
1502
1511
|
|
|
1503
1512
|
## Debugging Support
|
|
1504
1513
|
|
|
@@ -1531,7 +1540,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1531
1540
|
```
|
|
1532
1541
|
|
|
1533
1542
|
|
|
1534
|
-
<a id="
|
|
1543
|
+
<a id="orgdc52f71"></a>
|
|
1535
1544
|
|
|
1536
1545
|
# Logging Issues
|
|
1537
1546
|
|
|
@@ -1548,7 +1557,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1548
1557
|
Perhaps the best assistance is a minimal reproducible example of the issue. Once the issue can be readily reproduced, it can usually be fixed very quickly.
|
|
1549
1558
|
|
|
1550
1559
|
|
|
1551
|
-
<a id="
|
|
1560
|
+
<a id="orge4e0c24"></a>
|
|
1552
1561
|
|
|
1553
1562
|
# Pull Requests
|
|
1554
1563
|
|
|
@@ -1565,7 +1574,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
|
|
|
1565
1574
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1566
1575
|
|
|
1567
1576
|
|
|
1568
|
-
<a id="
|
|
1577
|
+
<a id="org7f7c9a1"></a>
|
|
1569
1578
|
|
|
1570
1579
|
# Contributors
|
|
1571
1580
|
|
|
@@ -1586,4 +1595,4 @@ Thanks to the following for their contributions -
|
|
|
1586
1595
|
- **Emma Milner:** Contributed fix for put() bug
|
|
1587
1596
|
- **Witni Davis:** Contributed PR to fix put() RCE when using 'finish' rather than 'close' to resolve promise
|
|
1588
1597
|
- **Maik Marschner:** Contributed fix for connect() not returning sftp object. Also included test to check for this regression in future.
|
|
1589
|
-
- **cakemasher:** Contributed fix for removeTempListeners().
|
|
1598
|
+
- **cakemasher:** Contributed fix for removeTempListeners().
|