ssh2-sftp-client 11.0.0 → 12.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 +135 -140
- package/README.org +35 -46
- package/package.json +3 -4
- package/src/index.js +40 -102
package/README.md
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
- [Overview](#
|
|
2
|
-
- [Version
|
|
3
|
-
- [Background](#
|
|
4
|
-
- [Installation](#
|
|
5
|
-
- [Basic Usage](#
|
|
6
|
-
- [Documentation](#
|
|
7
|
-
- [Specifying Paths](#
|
|
8
|
-
- [Methods](#
|
|
9
|
-
- [new SftpClient(name, callbacks) ===> SFTP client object](#
|
|
10
|
-
- [connect(config) ===> SFTP object](#
|
|
11
|
-
- [list(path, filter) ==> Array[object]](#
|
|
12
|
-
- [exists(path) ==> boolean](#
|
|
13
|
-
- [stat(path) ==> object](#
|
|
14
|
-
- [get(path, dst, options) ==> String|Stream|Buffer](#
|
|
15
|
-
- [fastGet(remotePath, localPath, options) ===> string](#
|
|
16
|
-
- [put(src, remotePath, options) ==> string](#
|
|
17
|
-
- [fastPut(localPath, remotePath, options) ==> string](#
|
|
18
|
-
- [append(input, remotePath, options) ==> string](#
|
|
19
|
-
- [mkdir(path, recursive) ==> string](#
|
|
20
|
-
- [rmdir(path, recursive) ==> string](#
|
|
21
|
-
- [delete(path, noErrorOK) ==> string](#
|
|
22
|
-
- [rename(fromPath, toPath) ==> string](#
|
|
23
|
-
- [posixRename(fromPath, toPath) ==> string](#
|
|
24
|
-
- [chmod(path, mode) ==> string](#
|
|
25
|
-
- [realPath(path) ===> string](#
|
|
26
|
-
- [cwd() ==> string](#
|
|
27
|
-
- [uploadDir(srcDir, dstDir, options) ==> string](#
|
|
28
|
-
- [downloadDir(srcDir, dstDir, options) ==> string](#
|
|
29
|
-
- [createReadStream(remotePath, options)) ==> stream object](#
|
|
30
|
-
- [createWriteStream(remotePath, options) ==> stream object](#
|
|
31
|
-
- [rcopy(srcPath, dstPath) ==> string](#
|
|
32
|
-
- [end() ==> boolean](#
|
|
33
|
-
- [Add and Remove Listeners](#
|
|
34
|
-
- [Platform Quirks & Warnings](#
|
|
35
|
-
- [Server Capabilities](#
|
|
36
|
-
- [Issues with `fastPut()` and `fastGet()` Methods](#
|
|
37
|
-
- [Promises, Events & Managing Exceptions](#
|
|
38
|
-
- [Adding Custom Handlers](#
|
|
39
|
-
- [Windows Based Servers](#
|
|
40
|
-
- [Don't Re-use SftpClient Objects](#
|
|
41
|
-
- [FAQ](#
|
|
42
|
-
- [Remote server drops connections with only an end event](#
|
|
43
|
-
- [How can I pass writeable stream as dst for get method?](#
|
|
44
|
-
- [How can I upload files without having to specify a password?](#
|
|
45
|
-
- [How can I connect through a Socks Proxy](#
|
|
46
|
-
- [Timeout while waiting for handshake or handshake errors](#
|
|
47
|
-
- [How can I limit upload/download speed](#
|
|
48
|
-
- [Connection hangs or fails for larger files](#
|
|
49
|
-
- [Typescript definition file out of date](#
|
|
50
|
-
- [Examples](#
|
|
51
|
-
- [Troubleshooting](#
|
|
52
|
-
- [Common Errors](#
|
|
53
|
-
- [Not returning the promise in a `then()` block](#
|
|
54
|
-
- [Mixing Promise Chains and Async/Await](#
|
|
55
|
-
- [Try/catch and Error Handlers](#
|
|
56
|
-
- [Server Differences](#
|
|
57
|
-
- [Avoid Concurrent Operations](#
|
|
58
|
-
- [Debugging Support](#
|
|
59
|
-
- [Logging Issues](#
|
|
60
|
-
- [Pull Requests](#
|
|
61
|
-
- [Contributors](#
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
<a id="
|
|
1
|
+
- [Overview](#org38d065d)
|
|
2
|
+
- [Version 12.0.0 Changes](#orgaf728c6)
|
|
3
|
+
- [Background](#org6f33d22)
|
|
4
|
+
- [Installation](#org8439307)
|
|
5
|
+
- [Basic Usage](#orgaf41082)
|
|
6
|
+
- [Documentation](#org5108c1b)
|
|
7
|
+
- [Specifying Paths](#org72e8719)
|
|
8
|
+
- [Methods](#orgfe65544)
|
|
9
|
+
- [new SftpClient(name, callbacks) ===> SFTP client object](#orgc6511be)
|
|
10
|
+
- [connect(config) ===> SFTP object](#org9fbbaaa)
|
|
11
|
+
- [list(path, filter) ==> Array[object]](#org73f17ce)
|
|
12
|
+
- [exists(path) ==> boolean](#org09b5d66)
|
|
13
|
+
- [stat(path) ==> object](#org91f6b04)
|
|
14
|
+
- [get(path, dst, options) ==> String|Stream|Buffer](#org78860d7)
|
|
15
|
+
- [fastGet(remotePath, localPath, options) ===> string](#org075afe7)
|
|
16
|
+
- [put(src, remotePath, options) ==> string](#org3570f3c)
|
|
17
|
+
- [fastPut(localPath, remotePath, options) ==> string](#org23d30ed)
|
|
18
|
+
- [append(input, remotePath, options) ==> string](#org4d3d523)
|
|
19
|
+
- [mkdir(path, recursive) ==> string](#org7ff954e)
|
|
20
|
+
- [rmdir(path, recursive) ==> string](#orga241b96)
|
|
21
|
+
- [delete(path, noErrorOK) ==> string](#org07b0cde)
|
|
22
|
+
- [rename(fromPath, toPath) ==> string](#org2d695fa)
|
|
23
|
+
- [posixRename(fromPath, toPath) ==> string](#org54eef84)
|
|
24
|
+
- [chmod(path, mode) ==> string](#org34c2a04)
|
|
25
|
+
- [realPath(path) ===> string](#org8e2be15)
|
|
26
|
+
- [cwd() ==> string](#org2d235d7)
|
|
27
|
+
- [uploadDir(srcDir, dstDir, options) ==> string](#orgb4585c4)
|
|
28
|
+
- [downloadDir(srcDir, dstDir, options) ==> string](#orgd2d4371)
|
|
29
|
+
- [createReadStream(remotePath, options)) ==> stream object](#orgfd916cd)
|
|
30
|
+
- [createWriteStream(remotePath, options) ==> stream object](#orgbba804d)
|
|
31
|
+
- [rcopy(srcPath, dstPath) ==> string](#org96b35cc)
|
|
32
|
+
- [end() ==> boolean](#org7600349)
|
|
33
|
+
- [Add and Remove Listeners](#orgff68122)
|
|
34
|
+
- [Platform Quirks & Warnings](#orgcd3e52c)
|
|
35
|
+
- [Server Capabilities](#org44699cb)
|
|
36
|
+
- [Issues with `fastPut()` and `fastGet()` Methods](#org219edb9)
|
|
37
|
+
- [Promises, Events & Managing Exceptions](#orgdb50974)
|
|
38
|
+
- [Adding Custom Handlers](#orge9ed516)
|
|
39
|
+
- [Windows Based Servers](#org04134c2)
|
|
40
|
+
- [Don't Re-use SftpClient Objects](#orgb8a3ba1)
|
|
41
|
+
- [FAQ](#org8a8b24b)
|
|
42
|
+
- [Remote server drops connections with only an end event](#org5de7a15)
|
|
43
|
+
- [How can I pass writeable stream as dst for get method?](#org21efa6c)
|
|
44
|
+
- [How can I upload files without having to specify a password?](#orgee1cf34)
|
|
45
|
+
- [How can I connect through a Socks Proxy](#orga6297a7)
|
|
46
|
+
- [Timeout while waiting for handshake or handshake errors](#org628c88e)
|
|
47
|
+
- [How can I limit upload/download speed](#org1116140)
|
|
48
|
+
- [Connection hangs or fails for larger files](#orgefe495c)
|
|
49
|
+
- [Typescript definition file out of date](#orgedcb385)
|
|
50
|
+
- [Examples](#org3a23c04)
|
|
51
|
+
- [Troubleshooting](#org888858f)
|
|
52
|
+
- [Common Errors](#orge820faa)
|
|
53
|
+
- [Not returning the promise in a `then()` block](#org52515e7)
|
|
54
|
+
- [Mixing Promise Chains and Async/Await](#org8e86c97)
|
|
55
|
+
- [Try/catch and Error Handlers](#org55f382c)
|
|
56
|
+
- [Server Differences](#org76494fa)
|
|
57
|
+
- [Avoid Concurrent Operations](#orgaedcad8)
|
|
58
|
+
- [Debugging Support](#orgf7b85d3)
|
|
59
|
+
- [Logging Issues](#orgb243d19)
|
|
60
|
+
- [Pull Requests](#org3d32e62)
|
|
61
|
+
- [Contributors](#org99ba3a6)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
<a id="org38d065d"></a>
|
|
66
66
|
|
|
67
67
|
# Overview
|
|
68
68
|
|
|
69
|
-
This
|
|
69
|
+
This package provides the class SftpClient, an SFTP client for node.js. It is a promise based decorator class around the excellent [SSH2](https://github.com/mscdex/ssh2) package, which provides a pure node Javascript event based ssh2 implementation.
|
|
70
70
|
|
|
71
|
-
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 the ssh2-sftp-client package just a wrapper around the `ssh2` module, you will find lots of useful information, tips and examples in the `ssh2` repository.
|
|
71
|
+
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 the ssh2-sftp-client package is just a wrapper around the `ssh2` module, you will find lots of useful information, tips and examples in the `ssh2` repository.
|
|
72
72
|
|
|
73
|
-
Current stable release is \*
|
|
73
|
+
Current stable release is \*v12.0.0.
|
|
74
74
|
|
|
75
|
-
Code has been tested against Node versions 18.
|
|
75
|
+
Code has been tested against Node versions 20.18.3, 22.14.0 and 23.8.0. Node versions prior to v20.x are not supported.
|
|
76
76
|
|
|
77
77
|
If you find this module useful and you would like to support the on-going maintenance and support of users, please consider making a small [donation](https://square.link/u/gB2kSdkY?src=embed).
|
|
78
78
|
|
|
79
79
|
|
|
80
|
-
<a id="
|
|
80
|
+
<a id="orgaf728c6"></a>
|
|
81
81
|
|
|
82
|
-
## Version
|
|
82
|
+
## Version 12.0.0 Changes
|
|
83
83
|
|
|
84
|
-
The
|
|
84
|
+
The big and breaking change in this version is the removal of the connection retry code. This module no longer supports automatic connection retries when establishing the initial connection. This functionality was removed for a number of reasons. The main reasons are that it significantly complicated the connection handling code and error handling in particular and it was seldom useful. In most cases, if the cvonnection failed on thye first attempt, it would also fail in all subsequent attempts and often resulted in just extending the time to failure as multiple retries were attempted.
|
|
85
85
|
|
|
86
|
-
The
|
|
86
|
+
The other reason this functionality was removed is because if you really want it, it is fairly straight forward to create your own wrapper around the connect call which uses one of the mnay promise retry packages available. Doing this is significantly easier when you don't need to cater for all the variables available when making a connection i.e. strict host verification, ssh agents, keys, proxies etc.
|
|
87
87
|
|
|
88
88
|
|
|
89
|
-
<a id="
|
|
89
|
+
<a id="org6f33d22"></a>
|
|
90
90
|
|
|
91
91
|
## Background
|
|
92
92
|
|
|
93
|
-
In basic terms `ssh2-sftp-client` is a simple wrapper around the `ssh2` package which provides a promise base API for interacting with a remote SFTP server . The `ssh2` package provides an event based API for interacting with the `ssh`
|
|
93
|
+
In basic terms `ssh2-sftp-client` is a simple wrapper around the `ssh2` package which provides a promise base API for interacting with a remote SFTP server . The `ssh2` package provides an event based API for interacting with the `ssh` protocol. The `ssh2-sftp-client` package uses the `sftp` subsystem of this protocol to implement the basic operations typically associated with an `sftp` client.
|
|
94
94
|
|
|
95
95
|
Wrapping an event based API with a promised based API comes with a number of challenges. In particular, efficiently and reliably managing events within the context of asynchrounous code execution. This package uses the following strategies;
|
|
96
96
|
|
|
@@ -125,7 +125,7 @@ In some cases, no ende event is raised and you only get an error event followed
|
|
|
125
125
|
In order to handle the possible hanging issue in version 11, the temporary promise linked end and close listeners have been updated to always call the promise's reject function if they fire. While this works, it can cause a minor issue. As wse cannot gurantee the order in which events are resonded to by listeners, it is possible that either the end or close listener may be executed before the error listener. When this occurs, the promise is rejected, but the only information wse have at that point is that the promise wsas reject due to either an end or close event. We don't yet have any details regarding what error has caused the unexpected end or close event. Furthermore, because only the first promise resolution function call has any effect, calling reject within the error listener (assuming an error event does eventually arrive) has no effect and does not communicate error information back to the caller. This means that in some circumstances, especially when working with some poorly behaved sftp servers, an sftp connection will be lost/closed with no indication as to reason. This can make diagnosis and bug tracking frustrating.
|
|
126
126
|
|
|
127
127
|
|
|
128
|
-
<a id="
|
|
128
|
+
<a id="org8439307"></a>
|
|
129
129
|
|
|
130
130
|
# Installation
|
|
131
131
|
|
|
@@ -134,7 +134,7 @@ npm install ssh2-sftp-client
|
|
|
134
134
|
```
|
|
135
135
|
|
|
136
136
|
|
|
137
|
-
<a id="
|
|
137
|
+
<a id="orgaf41082"></a>
|
|
138
138
|
|
|
139
139
|
# Basic Usage
|
|
140
140
|
|
|
@@ -157,18 +157,18 @@ sftp.connect({
|
|
|
157
157
|
```
|
|
158
158
|
|
|
159
159
|
|
|
160
|
-
<a id="
|
|
160
|
+
<a id="org5108c1b"></a>
|
|
161
161
|
|
|
162
162
|
# Documentation
|
|
163
163
|
|
|
164
|
-
The connection options are the same as those offered by the underlying SSH2 module, with just a couple of additional properties added to
|
|
164
|
+
The connection options are the same as those offered by the underlying SSH2 module, with just a couple of additional properties added to 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.
|
|
165
165
|
|
|
166
166
|
All the methods will return a Promise, except for `on(), ~removeListener()`, `createReadStream` and `createWriteStream`, which are typically only used in special use cases.
|
|
167
167
|
|
|
168
168
|
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.
|
|
169
169
|
|
|
170
170
|
|
|
171
|
-
<a id="
|
|
171
|
+
<a id="org72e8719"></a>
|
|
172
172
|
|
|
173
173
|
## Specifying Paths
|
|
174
174
|
|
|
@@ -201,12 +201,12 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
|
|
|
201
201
|
This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
|
|
202
202
|
|
|
203
203
|
|
|
204
|
-
<a id="
|
|
204
|
+
<a id="orgfe65544"></a>
|
|
205
205
|
|
|
206
206
|
## Methods
|
|
207
207
|
|
|
208
208
|
|
|
209
|
-
<a id="
|
|
209
|
+
<a id="orgc6511be"></a>
|
|
210
210
|
|
|
211
211
|
### new SftpClient(name, callbacks) ===> SFTP client object
|
|
212
212
|
|
|
@@ -246,7 +246,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
|
|
|
246
246
|
```
|
|
247
247
|
|
|
248
248
|
|
|
249
|
-
<a id="
|
|
249
|
+
<a id="org9fbbaaa"></a>
|
|
250
250
|
|
|
251
251
|
### connect(config) ===> SFTP object
|
|
252
252
|
|
|
@@ -256,9 +256,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
256
256
|
|
|
257
257
|
This module is based on the excellent [SSH2](https://github.com/mscdex/ssh2#client) module. That module is a general SSH2 client and server library and provides much more functionality than just SFTP connectivity. Many of the connect options provided by that module are less relevant for SFTP connections. It is recommended you keep the config options to the minimum needed and stick to the options listed in the `commonOpts` below.
|
|
258
258
|
|
|
259
|
-
The `
|
|
260
|
-
|
|
261
|
-
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.
|
|
259
|
+
The `promiseLimit` is an 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 does not 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 with 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.
|
|
262
260
|
|
|
263
261
|
```javascript
|
|
264
262
|
// common options
|
|
@@ -277,9 +275,6 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
277
275
|
strictVendor: true, // boolean - Performs a strict server vendor check
|
|
278
276
|
debug: myDebug,// function - Set this to a function that receives a single
|
|
279
277
|
// string argument to get detailed (local) debug information.
|
|
280
|
-
retries: 2, // integer. Number of times to retry connecting
|
|
281
|
-
retry_factor: 2, // integer. Time factor used to calculate time between retries
|
|
282
|
-
retry_minTimeout: 2000, // integer. Minimum timeout between attempts
|
|
283
278
|
promiseLimit: 10, // max concurrent promises for downloadDir/uploadDir
|
|
284
279
|
};
|
|
285
280
|
|
|
@@ -315,7 +310,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
|
|
|
315
310
|
```
|
|
316
311
|
|
|
317
312
|
|
|
318
|
-
<a id="
|
|
313
|
+
<a id="org73f17ce"></a>
|
|
319
314
|
|
|
320
315
|
### list(path, filter) ==> Array[object]
|
|
321
316
|
|
|
@@ -376,7 +371,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
|
|
|
376
371
|
```
|
|
377
372
|
|
|
378
373
|
|
|
379
|
-
<a id="
|
|
374
|
+
<a id="org09b5d66"></a>
|
|
380
375
|
|
|
381
376
|
### exists(path) ==> boolean
|
|
382
377
|
|
|
@@ -412,7 +407,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
|
|
|
412
407
|
```
|
|
413
408
|
|
|
414
409
|
|
|
415
|
-
<a id="
|
|
410
|
+
<a id="org91f6b04"></a>
|
|
416
411
|
|
|
417
412
|
### stat(path) ==> object
|
|
418
413
|
|
|
@@ -463,7 +458,7 @@ Returns the attributes associated with the object pointed to by `path`.
|
|
|
463
458
|
```
|
|
464
459
|
|
|
465
460
|
|
|
466
|
-
<a id="
|
|
461
|
+
<a id="org78860d7"></a>
|
|
467
462
|
|
|
468
463
|
### get(path, dst, options) ==> String|Stream|Buffer
|
|
469
464
|
|
|
@@ -519,7 +514,7 @@ In general, if you're going to pass in a string as the destination, you are bett
|
|
|
519
514
|
- **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'.
|
|
520
515
|
|
|
521
516
|
|
|
522
|
-
<a id="
|
|
517
|
+
<a id="org075afe7"></a>
|
|
523
518
|
|
|
524
519
|
### fastGet(remotePath, localPath, options) ===> string
|
|
525
520
|
|
|
@@ -564,7 +559,7 @@ Bottom line, when it works, it tends to work reliably. However, for many servers
|
|
|
564
559
|
```
|
|
565
560
|
|
|
566
561
|
|
|
567
|
-
<a id="
|
|
562
|
+
<a id="org3570f3c"></a>
|
|
568
563
|
|
|
569
564
|
### put(src, remotePath, options) ==> string
|
|
570
565
|
|
|
@@ -614,7 +609,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
|
|
|
614
609
|
- **Tip:** If the src argument is a path string, consider just using `fastPut()`.
|
|
615
610
|
|
|
616
611
|
|
|
617
|
-
<a id="
|
|
612
|
+
<a id="org23d30ed"></a>
|
|
618
613
|
|
|
619
614
|
### fastPut(localPath, remotePath, options) ==> string
|
|
620
615
|
|
|
@@ -660,7 +655,7 @@ Bottom line, when it works, it tends to work well. However, when it doesn't work
|
|
|
660
655
|
```
|
|
661
656
|
|
|
662
657
|
|
|
663
|
-
<a id="
|
|
658
|
+
<a id="org4d3d523"></a>
|
|
664
659
|
|
|
665
660
|
### append(input, remotePath, options) ==> string
|
|
666
661
|
|
|
@@ -704,7 +699,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
|
|
|
704
699
|
```
|
|
705
700
|
|
|
706
701
|
|
|
707
|
-
<a id="
|
|
702
|
+
<a id="org7ff954e"></a>
|
|
708
703
|
|
|
709
704
|
### mkdir(path, recursive) ==> string
|
|
710
705
|
|
|
@@ -732,7 +727,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
|
|
|
732
727
|
```
|
|
733
728
|
|
|
734
729
|
|
|
735
|
-
<a id="
|
|
730
|
+
<a id="orga241b96"></a>
|
|
736
731
|
|
|
737
732
|
### rmdir(path, recursive) ==> string
|
|
738
733
|
|
|
@@ -762,7 +757,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
|
|
|
762
757
|
```
|
|
763
758
|
|
|
764
759
|
|
|
765
|
-
<a id="
|
|
760
|
+
<a id="org07b0cde"></a>
|
|
766
761
|
|
|
767
762
|
### delete(path, noErrorOK) ==> string
|
|
768
763
|
|
|
@@ -791,7 +786,7 @@ Delete a file on the remote server.
|
|
|
791
786
|
```
|
|
792
787
|
|
|
793
788
|
|
|
794
|
-
<a id="
|
|
789
|
+
<a id="org2d695fa"></a>
|
|
795
790
|
|
|
796
791
|
### rename(fromPath, toPath) ==> string
|
|
797
792
|
|
|
@@ -820,7 +815,7 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
|
|
|
820
815
|
```
|
|
821
816
|
|
|
822
817
|
|
|
823
|
-
<a id="
|
|
818
|
+
<a id="org54eef84"></a>
|
|
824
819
|
|
|
825
820
|
### posixRename(fromPath, toPath) ==> string
|
|
826
821
|
|
|
@@ -847,7 +842,7 @@ client.connect(config)
|
|
|
847
842
|
```
|
|
848
843
|
|
|
849
844
|
|
|
850
|
-
<a id="
|
|
845
|
+
<a id="org34c2a04"></a>
|
|
851
846
|
|
|
852
847
|
### chmod(path, mode) ==> string
|
|
853
848
|
|
|
@@ -876,7 +871,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
|
|
|
876
871
|
```
|
|
877
872
|
|
|
878
873
|
|
|
879
|
-
<a id="
|
|
874
|
+
<a id="org8e2be15"></a>
|
|
880
875
|
|
|
881
876
|
### realPath(path) ===> string
|
|
882
877
|
|
|
@@ -887,14 +882,14 @@ Converts a relative path to an absolute path on the remote server. This method i
|
|
|
887
882
|
- **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
|
|
888
883
|
|
|
889
884
|
|
|
890
|
-
<a id="
|
|
885
|
+
<a id="org2d235d7"></a>
|
|
891
886
|
|
|
892
887
|
### cwd() ==> string
|
|
893
888
|
|
|
894
889
|
Returns what the server believes is the current remote working directory.
|
|
895
890
|
|
|
896
891
|
|
|
897
|
-
<a id="
|
|
892
|
+
<a id="orgb4585c4"></a>
|
|
898
893
|
|
|
899
894
|
### uploadDir(srcDir, dstDir, options) ==> string
|
|
900
895
|
|
|
@@ -963,7 +958,7 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
|
|
|
963
958
|
```
|
|
964
959
|
|
|
965
960
|
|
|
966
|
-
<a id="
|
|
961
|
+
<a id="orgd2d4371"></a>
|
|
967
962
|
|
|
968
963
|
### downloadDir(srcDir, dstDir, options) ==> string
|
|
969
964
|
|
|
@@ -1028,7 +1023,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
|
|
|
1028
1023
|
```
|
|
1029
1024
|
|
|
1030
1025
|
|
|
1031
|
-
<a id="
|
|
1026
|
+
<a id="orgfd916cd"></a>
|
|
1032
1027
|
|
|
1033
1028
|
### createReadStream(remotePath, options)) ==> stream object
|
|
1034
1029
|
|
|
@@ -1045,7 +1040,7 @@ Returns a read stream object which is attached to the remote file specified by t
|
|
|
1045
1040
|
- **end:** Position to stop reading bytes (inclusive).
|
|
1046
1041
|
|
|
1047
1042
|
|
|
1048
|
-
<a id="
|
|
1043
|
+
<a id="orgbba804d"></a>
|
|
1049
1044
|
|
|
1050
1045
|
### createWriteStream(remotePath, options) ==> stream object
|
|
1051
1046
|
|
|
@@ -1060,7 +1055,7 @@ Returns a write stream object which is attached to the remote file specified in
|
|
|
1060
1055
|
- **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
|
|
1061
1056
|
|
|
1062
1057
|
|
|
1063
|
-
<a id="
|
|
1058
|
+
<a id="org96b35cc"></a>
|
|
1064
1059
|
|
|
1065
1060
|
### rcopy(srcPath, dstPath) ==> string
|
|
1066
1061
|
|
|
@@ -1070,7 +1065,7 @@ Perform a remote file copy. The file identified by the `srcPath` argument will b
|
|
|
1070
1065
|
- **dstPath:** Path to where the copy will be created specified as a string
|
|
1071
1066
|
|
|
1072
1067
|
|
|
1073
|
-
<a id="
|
|
1068
|
+
<a id="org7600349"></a>
|
|
1074
1069
|
|
|
1075
1070
|
### end() ==> boolean
|
|
1076
1071
|
|
|
@@ -1094,7 +1089,7 @@ Ends the current client session, releasing the client socket and associated reso
|
|
|
1094
1089
|
```
|
|
1095
1090
|
|
|
1096
1091
|
|
|
1097
|
-
<a id="
|
|
1092
|
+
<a id="orgff68122"></a>
|
|
1098
1093
|
|
|
1099
1094
|
### Add and Remove Listeners
|
|
1100
1095
|
|
|
@@ -1115,12 +1110,12 @@ Although normally not required, you can add and remove custom listeners on the s
|
|
|
1115
1110
|
Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
|
|
1116
1111
|
|
|
1117
1112
|
|
|
1118
|
-
<a id="
|
|
1113
|
+
<a id="orgcd3e52c"></a>
|
|
1119
1114
|
|
|
1120
1115
|
# Platform Quirks & Warnings
|
|
1121
1116
|
|
|
1122
1117
|
|
|
1123
|
-
<a id="
|
|
1118
|
+
<a id="org44699cb"></a>
|
|
1124
1119
|
|
|
1125
1120
|
## Server Capabilities
|
|
1126
1121
|
|
|
@@ -1129,7 +1124,7 @@ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-
|
|
|
1129
1124
|
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.
|
|
1130
1125
|
|
|
1131
1126
|
|
|
1132
|
-
<a id="
|
|
1127
|
+
<a id="org219edb9"></a>
|
|
1133
1128
|
|
|
1134
1129
|
## Issues with `fastPut()` and `fastGet()` Methods
|
|
1135
1130
|
|
|
@@ -1138,7 +1133,7 @@ The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SF
|
|
|
1138
1133
|
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.
|
|
1139
1134
|
|
|
1140
1135
|
|
|
1141
|
-
<a id="
|
|
1136
|
+
<a id="orgdb50974"></a>
|
|
1142
1137
|
|
|
1143
1138
|
## Promises, Events & Managing Exceptions
|
|
1144
1139
|
|
|
@@ -1157,14 +1152,14 @@ The other area where additional events are fired is during the end() call. To de
|
|
|
1157
1152
|
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.
|
|
1158
1153
|
|
|
1159
1154
|
|
|
1160
|
-
<a id="
|
|
1155
|
+
<a id="orge9ed516"></a>
|
|
1161
1156
|
|
|
1162
1157
|
### Adding Custom Handlers
|
|
1163
1158
|
|
|
1164
1159
|
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.
|
|
1165
1160
|
|
|
1166
1161
|
|
|
1167
|
-
<a id="
|
|
1162
|
+
<a id="org04134c2"></a>
|
|
1168
1163
|
|
|
1169
1164
|
## Windows Based Servers
|
|
1170
1165
|
|
|
@@ -1173,7 +1168,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
|
|
|
1173
1168
|
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.
|
|
1174
1169
|
|
|
1175
1170
|
|
|
1176
|
-
<a id="
|
|
1171
|
+
<a id="orgb8a3ba1"></a>
|
|
1177
1172
|
|
|
1178
1173
|
## Don't Re-use SftpClient Objects
|
|
1179
1174
|
|
|
@@ -1182,12 +1177,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
|
|
|
1182
1177
|
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.
|
|
1183
1178
|
|
|
1184
1179
|
|
|
1185
|
-
<a id="
|
|
1180
|
+
<a id="org8a8b24b"></a>
|
|
1186
1181
|
|
|
1187
1182
|
# FAQ
|
|
1188
1183
|
|
|
1189
1184
|
|
|
1190
|
-
<a id="
|
|
1185
|
+
<a id="org5de7a15"></a>
|
|
1191
1186
|
|
|
1192
1187
|
## Remote server drops connections with only an end event
|
|
1193
1188
|
|
|
@@ -1198,7 +1193,7 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
|
|
|
1198
1193
|
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.
|
|
1199
1194
|
|
|
1200
1195
|
|
|
1201
|
-
<a id="
|
|
1196
|
+
<a id="org21efa6c"></a>
|
|
1202
1197
|
|
|
1203
1198
|
## How can I pass writeable stream as dst for get method?
|
|
1204
1199
|
|
|
@@ -1257,7 +1252,7 @@ sftp
|
|
|
1257
1252
|
```
|
|
1258
1253
|
|
|
1259
1254
|
|
|
1260
|
-
<a id="
|
|
1255
|
+
<a id="orgee1cf34"></a>
|
|
1261
1256
|
|
|
1262
1257
|
## How can I upload files without having to specify a password?
|
|
1263
1258
|
|
|
@@ -1292,7 +1287,7 @@ sftp.connect({
|
|
|
1292
1287
|
```
|
|
1293
1288
|
|
|
1294
1289
|
|
|
1295
|
-
<a id="
|
|
1290
|
+
<a id="orga6297a7"></a>
|
|
1296
1291
|
|
|
1297
1292
|
## How can I connect through a Socks Proxy
|
|
1298
1293
|
|
|
@@ -1329,7 +1324,7 @@ client.connect({
|
|
|
1329
1324
|
```
|
|
1330
1325
|
|
|
1331
1326
|
|
|
1332
|
-
<a id="
|
|
1327
|
+
<a id="org628c88e"></a>
|
|
1333
1328
|
|
|
1334
1329
|
## Timeout while waiting for handshake or handshake errors
|
|
1335
1330
|
|
|
@@ -1338,7 +1333,7 @@ Some users have encountered the error 'Timeout while waiting for handshake' or '
|
|
|
1338
1333
|
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.
|
|
1339
1334
|
|
|
1340
1335
|
|
|
1341
|
-
<a id="
|
|
1336
|
+
<a id="org1116140"></a>
|
|
1342
1337
|
|
|
1343
1338
|
## How can I limit upload/download speed
|
|
1344
1339
|
|
|
@@ -1378,7 +1373,7 @@ try {
|
|
|
1378
1373
|
```
|
|
1379
1374
|
|
|
1380
1375
|
|
|
1381
|
-
<a id="
|
|
1376
|
+
<a id="orgefe495c"></a>
|
|
1382
1377
|
|
|
1383
1378
|
## Connection hangs or fails for larger files
|
|
1384
1379
|
|
|
@@ -1389,21 +1384,21 @@ A symptom of this issue is that you are able to upload small files, but uploadin
|
|
|
1389
1384
|
For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
|
|
1390
1385
|
|
|
1391
1386
|
|
|
1392
|
-
<a id="
|
|
1387
|
+
<a id="orgedcb385"></a>
|
|
1393
1388
|
|
|
1394
1389
|
## Typescript definition file out of date
|
|
1395
1390
|
|
|
1396
1391
|
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.
|
|
1397
1392
|
|
|
1398
1393
|
|
|
1399
|
-
<a id="
|
|
1394
|
+
<a id="org3a23c04"></a>
|
|
1400
1395
|
|
|
1401
1396
|
# Examples
|
|
1402
1397
|
|
|
1403
1398
|
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.
|
|
1404
1399
|
|
|
1405
1400
|
|
|
1406
|
-
<a id="
|
|
1401
|
+
<a id="org888858f"></a>
|
|
1407
1402
|
|
|
1408
1403
|
# Troubleshooting
|
|
1409
1404
|
|
|
@@ -1418,14 +1413,14 @@ Note also that in the repository there are two useful directories. The first is
|
|
|
1418
1413
|
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.
|
|
1419
1414
|
|
|
1420
1415
|
|
|
1421
|
-
<a id="
|
|
1416
|
+
<a id="orge820faa"></a>
|
|
1422
1417
|
|
|
1423
1418
|
## Common Errors
|
|
1424
1419
|
|
|
1425
1420
|
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.
|
|
1426
1421
|
|
|
1427
1422
|
|
|
1428
|
-
<a id="
|
|
1423
|
+
<a id="org52515e7"></a>
|
|
1429
1424
|
|
|
1430
1425
|
### Not returning the promise in a `then()` block
|
|
1431
1426
|
|
|
@@ -1462,7 +1457,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
|
|
|
1462
1457
|
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.
|
|
1463
1458
|
|
|
1464
1459
|
|
|
1465
|
-
<a id="
|
|
1460
|
+
<a id="org8e86c97"></a>
|
|
1466
1461
|
|
|
1467
1462
|
### Mixing Promise Chains and Async/Await
|
|
1468
1463
|
|
|
@@ -1522,7 +1517,7 @@ async function doSftp() {
|
|
|
1522
1517
|
```
|
|
1523
1518
|
|
|
1524
1519
|
|
|
1525
|
-
<a id="
|
|
1520
|
+
<a id="org55f382c"></a>
|
|
1526
1521
|
|
|
1527
1522
|
### Try/catch and Error Handlers
|
|
1528
1523
|
|
|
@@ -1533,14 +1528,14 @@ The basic problem is that the try/catch block will have completed execution befo
|
|
|
1533
1528
|
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.
|
|
1534
1529
|
|
|
1535
1530
|
|
|
1536
|
-
<a id="
|
|
1531
|
+
<a id="org76494fa"></a>
|
|
1537
1532
|
|
|
1538
1533
|
### Server Differences
|
|
1539
1534
|
|
|
1540
1535
|
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.
|
|
1541
1536
|
|
|
1542
1537
|
|
|
1543
|
-
<a id="
|
|
1538
|
+
<a id="orgaedcad8"></a>
|
|
1544
1539
|
|
|
1545
1540
|
### Avoid Concurrent Operations
|
|
1546
1541
|
|
|
@@ -1549,7 +1544,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
|
|
|
1549
1544
|
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.
|
|
1550
1545
|
|
|
1551
1546
|
|
|
1552
|
-
<a id="
|
|
1547
|
+
<a id="orgf7b85d3"></a>
|
|
1553
1548
|
|
|
1554
1549
|
## Debugging Support
|
|
1555
1550
|
|
|
@@ -1582,7 +1577,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
|
|
|
1582
1577
|
```
|
|
1583
1578
|
|
|
1584
1579
|
|
|
1585
|
-
<a id="
|
|
1580
|
+
<a id="orgb243d19"></a>
|
|
1586
1581
|
|
|
1587
1582
|
# Logging Issues
|
|
1588
1583
|
|
|
@@ -1599,7 +1594,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
|
|
|
1599
1594
|
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.
|
|
1600
1595
|
|
|
1601
1596
|
|
|
1602
|
-
<a id="
|
|
1597
|
+
<a id="org3d32e62"></a>
|
|
1603
1598
|
|
|
1604
1599
|
# Pull Requests
|
|
1605
1600
|
|
|
@@ -1616,7 +1611,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
|
|
|
1616
1611
|
- **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
|
|
1617
1612
|
|
|
1618
1613
|
|
|
1619
|
-
<a id="
|
|
1614
|
+
<a id="org99ba3a6"></a>
|
|
1620
1615
|
|
|
1621
1616
|
# Contributors
|
|
1622
1617
|
|
package/README.org
CHANGED
|
@@ -3,47 +3,44 @@
|
|
|
3
3
|
|
|
4
4
|
* Overview
|
|
5
5
|
|
|
6
|
-
This
|
|
7
|
-
based decorator class around the
|
|
8
|
-
Javascript event based ssh2 implementation.
|
|
6
|
+
This package provides the class SftpClient, an SFTP client for node.js. It is a promise
|
|
7
|
+
based decorator class around the excellent [[https://github.com/mscdex/ssh2][SSH2]] package, which provides a pure node
|
|
8
|
+
Javascript event based ssh2 implementation.
|
|
9
9
|
|
|
10
10
|
Documentation on the methods and available options in the underlying modules can
|
|
11
|
-
be found on the [[https://github.com/mscdex/ssh2][SSH2]] project pages. As the ssh2-sftp-client package just a wrapper around the
|
|
11
|
+
be found on the [[https://github.com/mscdex/ssh2][SSH2]] project pages. As the ssh2-sftp-client package is just a wrapper around the
|
|
12
12
|
~ssh2~ module, you will find lots of useful information, tips and examples in the ~ssh2~
|
|
13
13
|
repository.
|
|
14
14
|
|
|
15
|
-
Current stable release is *
|
|
15
|
+
Current stable release is *v12.0.0.
|
|
16
16
|
|
|
17
|
-
Code has been tested against Node versions 18.
|
|
18
|
-
prior to
|
|
19
|
-
using node v22+. This is from the ~ssh2~ package and outside control of this package.
|
|
17
|
+
Code has been tested against Node versions 20.18.3, 22.14.0 and 23.8.0. Node versions
|
|
18
|
+
prior to v20.x are not supported.
|
|
20
19
|
|
|
21
20
|
If you find this module useful and you would like to support the on-going maintenance and
|
|
22
21
|
support of users, please consider making a small [[https://square.link/u/gB2kSdkY?src=embed][donation]].
|
|
23
22
|
|
|
24
|
-
** Version
|
|
23
|
+
** Version 12.0.0 Changes
|
|
25
24
|
|
|
26
|
-
The
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
The big and breaking change in this version is the removal of the connection retry
|
|
26
|
+
code. This module no longer supports automatic connection retries when establishing the
|
|
27
|
+
initial connection. This functionality was removed for a number of reasons. The main
|
|
28
|
+
reasons are that it significantly complicated the connection handling code and error
|
|
29
|
+
handling in particular and it was seldom useful. In most cases, if the cvonnection failed
|
|
30
|
+
on thye first attempt, it would also fail in all subsequent attempts and often resulted in
|
|
31
|
+
just extending the time to failure as multiple retries were attempted.
|
|
31
32
|
|
|
32
|
-
The
|
|
33
|
-
|
|
34
|
-
the
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
this approach has been replaced by a mechanism whereby the client code can pass in
|
|
38
|
-
application specific global handlers if desired. If no handlers are defined, default
|
|
39
|
-
handlers will log the event and when necessary, invalidate any existing connection
|
|
40
|
-
objects. See the [[Background]] section for details.
|
|
33
|
+
The other reason this functionality was removed is because if you really want it, it is
|
|
34
|
+
fairly straight forward to create your own wrapper around the connect call which uses one
|
|
35
|
+
of the mnay promise retry packages available. Doing this is significantly easier when you
|
|
36
|
+
don't need to cater for all the variables available when making a connection i.e. strict
|
|
37
|
+
host verification, ssh agents, keys, proxies etc.
|
|
41
38
|
|
|
42
39
|
** Background
|
|
43
40
|
|
|
44
41
|
In basic terms =ssh2-sftp-client= is a simple wrapper around the =ssh2= package which provides
|
|
45
42
|
a promise base API for interacting with a remote SFTP server . The =ssh2= package provides
|
|
46
|
-
an event based API for interacting with the ~ssh~
|
|
43
|
+
an event based API for interacting with the ~ssh~ protocol. The ~ssh2-sftp-client~ package
|
|
47
44
|
uses the ~sftp~ subsystem of this protocol to implement the basic operations typically
|
|
48
45
|
associated with an ~sftp~ client.
|
|
49
46
|
|
|
@@ -173,12 +170,12 @@ with no indication as to reason. This can make diagnosis and bug tracking frustr
|
|
|
173
170
|
|
|
174
171
|
* Documentation
|
|
175
172
|
|
|
176
|
-
The connection options are the same as those offered by the underlying SSH2
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
details relating to setting various key
|
|
181
|
-
part of the ssh2 protocol.
|
|
173
|
+
The connection options are the same as those offered by the underlying SSH2 module, with
|
|
174
|
+
just a couple of additional properties added to add a ~debug~ function and set the
|
|
175
|
+
~promiseLimit~ property. For full details on the other properties, please see
|
|
176
|
+
[[https://github.com/mscdex/ssh2#user-content-client-methods][SSH2 client methods]]. In
|
|
177
|
+
particular, see the ~ssh2~ documentation for details relating to setting various key
|
|
178
|
+
exchange and encryption/signing algorithms used as part of the ssh2 protocol.
|
|
182
179
|
|
|
183
180
|
All the methods will return a Promise, except for ~on(), ~removeListener()~, ~createReadStream~
|
|
184
181
|
and ~createWriteStream~, which are typically only used in special use cases.
|
|
@@ -302,23 +299,18 @@ available [[https://github.com/mscdex/ssh2#user-content-client-methods][here]]
|
|
|
302
299
|
|
|
303
300
|
**** Connection Options
|
|
304
301
|
|
|
305
|
-
This module is based on the excellent [[https://github.com/mscdex/ssh2#client][SSH2]]
|
|
306
|
-
client and server library and provides much more
|
|
307
|
-
connectivity. Many of the connect options provided by that
|
|
308
|
-
relevant for SFTP connections. It is recommended you keep the config
|
|
309
|
-
the minimum needed and stick to the options listed in the ~commonOpts~ below.
|
|
302
|
+
This module is based on the excellent [[https://github.com/mscdex/ssh2#client][SSH2]]
|
|
303
|
+
module. That module is a general SSH2 client and server library and provides much more
|
|
304
|
+
functionality than just SFTP connectivity. Many of the connect options provided by that
|
|
305
|
+
module are less relevant for SFTP connections. It is recommended you keep the config
|
|
306
|
+
options to the minimum needed and stick to the options listed in the ~commonOpts~ below.
|
|
310
307
|
|
|
311
|
-
The ~
|
|
312
|
-
SSH2 module. These are part of the configuration for the [[https://www.npmjs.com/package/retry][retry]] package and what
|
|
313
|
-
is used to enable retrying of sftp connection attempts. See the documentation
|
|
314
|
-
for that package for an explanation of these values.
|
|
315
|
-
|
|
316
|
-
The ~promiseLimit~ is another option which is not part of the ~ssh2~ module and is specific to
|
|
308
|
+
The ~promiseLimit~ is an option which is not part of the ~ssh2~ module and is specific to
|
|
317
309
|
~ssh2-sftp-client~. It is a property used to limit the maximum number of concurrent promises
|
|
318
310
|
possible when either downloading or uploading a directory tree using the ~downloadDir()~ or
|
|
319
|
-
~uploadDir()~ methods. The default setting for this property is 10. *NOTE*: bigger
|
|
311
|
+
~uploadDir()~ methods. The default setting for this property is 10. *NOTE*: bigger does not
|
|
320
312
|
mean better. Many factors can affect what is the ideal setting for ~promiseLimit~. If it is
|
|
321
|
-
too large, any benefits are lost while node spends time switching contexts and/or
|
|
313
|
+
too large, any benefits are lost while node spends time switching contexts and/or with
|
|
322
314
|
the overheads associated with creating and cleaning up promises. Lots of factors can
|
|
323
315
|
affect what the setting should be, including size of files, number of files, speed of
|
|
324
316
|
network, version of node, capabilities of remote sftp server etc. A setting of 10 seems to
|
|
@@ -343,9 +335,6 @@ work out what is the best maximum size before you begin to see a performance dro
|
|
|
343
335
|
strictVendor: true, // boolean - Performs a strict server vendor check
|
|
344
336
|
debug: myDebug,// function - Set this to a function that receives a single
|
|
345
337
|
// string argument to get detailed (local) debug information.
|
|
346
|
-
retries: 2, // integer. Number of times to retry connecting
|
|
347
|
-
retry_factor: 2, // integer. Time factor used to calculate time between retries
|
|
348
|
-
retry_minTimeout: 2000, // integer. Minimum timeout between attempts
|
|
349
338
|
promiseLimit: 10, // max concurrent promises for downloadDir/uploadDir
|
|
350
339
|
};
|
|
351
340
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh2-sftp-client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.0",
|
|
4
4
|
"description": "ssh2 sftp client for node",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/theophilusx/ssh2-sftp-client"
|
|
8
|
+
"url": "git+https://github.com/theophilusx/ssh2-sftp-client.git"
|
|
9
9
|
},
|
|
10
10
|
"keywords": [
|
|
11
11
|
"sftp",
|
|
@@ -54,7 +54,6 @@
|
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"concat-stream": "^2.0.0",
|
|
57
|
-
"
|
|
58
|
-
"ssh2": "^1.15.0"
|
|
57
|
+
"ssh2": "^1.16.0"
|
|
59
58
|
}
|
|
60
59
|
}
|
package/src/index.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
const { Client } = require('ssh2');
|
|
3
3
|
const fs = require('node:fs');
|
|
4
4
|
const concat = require('concat-stream');
|
|
5
|
-
const promiseRetry = require('promise-retry');
|
|
6
5
|
const { join, parse } = require('node:path');
|
|
7
6
|
const {
|
|
8
7
|
globalListener,
|
|
@@ -26,7 +25,7 @@ class SftpClient {
|
|
|
26
25
|
close: () => console.log('Global close listener: close event raised'),
|
|
27
26
|
},
|
|
28
27
|
) {
|
|
29
|
-
this.version = '
|
|
28
|
+
this.version = '12.0.0';
|
|
30
29
|
this.client = new Client();
|
|
31
30
|
this.sftp = undefined;
|
|
32
31
|
this.clientName = clientName;
|
|
@@ -58,35 +57,32 @@ class SftpClient {
|
|
|
58
57
|
fmtError(err, name = 'sftp', eCode, retryCount) {
|
|
59
58
|
let msg = '';
|
|
60
59
|
let code = '';
|
|
61
|
-
const retry = retryCount
|
|
62
|
-
? ` after ${retryCount} ${retryCount > 1 ? 'attempts' : 'attempt'}`
|
|
63
|
-
: '';
|
|
64
60
|
|
|
65
61
|
if (err === undefined) {
|
|
66
62
|
msg = `${name}: Undefined error - probably a bug!`;
|
|
67
63
|
code = errorCode.generic;
|
|
68
64
|
} else if (typeof err === 'string') {
|
|
69
|
-
msg = `${name}: ${err}
|
|
65
|
+
msg = `${name}: ${err}`;
|
|
70
66
|
code = eCode || errorCode.generic;
|
|
71
67
|
} else if (err.custom) {
|
|
72
|
-
msg = `${name}->${err.message}
|
|
68
|
+
msg = `${name}->${err.message}`;
|
|
73
69
|
code = err.code;
|
|
74
70
|
} else {
|
|
75
71
|
switch (err.code) {
|
|
76
72
|
case 'ENOTFOUND': {
|
|
77
|
-
msg = `${name}: Address lookup failed for host
|
|
73
|
+
msg = `${name}: Address lookup failed for host`;
|
|
78
74
|
break;
|
|
79
75
|
}
|
|
80
76
|
case 'ECONNREFUSED': {
|
|
81
|
-
msg = `${name}: Remote host refused connection
|
|
77
|
+
msg = `${name}: Remote host refused connection`;
|
|
82
78
|
break;
|
|
83
79
|
}
|
|
84
80
|
case 'ECONNRESET': {
|
|
85
|
-
msg = `${name}: Remote host has reset the connection: ${err.message}
|
|
81
|
+
msg = `${name}: Remote host has reset the connection: ${err.message}`;
|
|
86
82
|
break;
|
|
87
83
|
}
|
|
88
84
|
default: {
|
|
89
|
-
msg = `${name}: ${err.message}
|
|
85
|
+
msg = `${name}: ${err.message}`;
|
|
90
86
|
}
|
|
91
87
|
}
|
|
92
88
|
code = err.code || errorCode.generic;
|
|
@@ -122,25 +118,52 @@ class SftpClient {
|
|
|
122
118
|
}
|
|
123
119
|
|
|
124
120
|
/**
|
|
125
|
-
* @async
|
|
126
121
|
*
|
|
127
|
-
* Create a new SFTP connection to a remote SFTP server
|
|
122
|
+
* Create a new SFTP connection to a remote SFTP server.
|
|
123
|
+
* The connection options are the same as those offered
|
|
124
|
+
* by the underlying SSH2 module.
|
|
128
125
|
*
|
|
129
126
|
* @param {Object} config - an SFTP configuration object
|
|
130
127
|
*
|
|
131
128
|
* @return {Promise<Object>} which will resolve to an sftp client object
|
|
132
129
|
*/
|
|
133
|
-
|
|
130
|
+
connect(config) {
|
|
134
131
|
let doReady, listeners;
|
|
135
132
|
return new Promise((resolve, reject) => {
|
|
136
133
|
listeners = addTempListeners(this, 'getConnection', reject);
|
|
134
|
+
if (config.debug) {
|
|
135
|
+
this.debug = config.debug;
|
|
136
|
+
this.debugMsg('connect: Debugging turned on');
|
|
137
|
+
this.debugMsg(`ssh2-sftp-client Version: ${this.version} `, process.versions);
|
|
138
|
+
}
|
|
139
|
+
this.promiseLimit = config.promiseLimit ?? 10;
|
|
140
|
+
|
|
137
141
|
doReady = () => {
|
|
138
|
-
|
|
142
|
+
this.client.sftp((err, sftp) => {
|
|
143
|
+
if (err) {
|
|
144
|
+
reject(this.fmtError(err));
|
|
145
|
+
} else {
|
|
146
|
+
this.sftp = sftp;
|
|
147
|
+
resolve(sftp);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
139
150
|
};
|
|
140
151
|
this.on('ready', doReady);
|
|
152
|
+
|
|
141
153
|
try {
|
|
142
|
-
this.
|
|
154
|
+
if (this.sftp) {
|
|
155
|
+
reject(
|
|
156
|
+
this.fmtError(
|
|
157
|
+
'An existing SFTP connection is already defined',
|
|
158
|
+
'connect',
|
|
159
|
+
errorCode.connect,
|
|
160
|
+
),
|
|
161
|
+
);
|
|
162
|
+
} else {
|
|
163
|
+
this.client.connect(config);
|
|
164
|
+
}
|
|
143
165
|
} catch (err) {
|
|
166
|
+
this.end();
|
|
144
167
|
reject(err);
|
|
145
168
|
}
|
|
146
169
|
}).finally(() => {
|
|
@@ -149,91 +172,6 @@ class SftpClient {
|
|
|
149
172
|
});
|
|
150
173
|
}
|
|
151
174
|
|
|
152
|
-
getSftpChannel() {
|
|
153
|
-
return new Promise((resolve, reject) => {
|
|
154
|
-
this.client.sftp((err, sftp) => {
|
|
155
|
-
if (err) {
|
|
156
|
-
reject(this.fmtError(err, 'getSftpChannel', err.code));
|
|
157
|
-
} else {
|
|
158
|
-
this.sftp = sftp;
|
|
159
|
-
resolve(sftp);
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* @async
|
|
167
|
-
*
|
|
168
|
-
* Create a new SFTP connection to a remote SFTP server.
|
|
169
|
-
* The connection options are the same as those offered
|
|
170
|
-
* by the underlying SSH2 module.
|
|
171
|
-
*
|
|
172
|
-
* @param {Object} config - an SFTP configuration object
|
|
173
|
-
*
|
|
174
|
-
* @return {Promise<Object>} which will resolve to an sftp client object
|
|
175
|
-
*/
|
|
176
|
-
async connect(config) {
|
|
177
|
-
let listeners;
|
|
178
|
-
|
|
179
|
-
try {
|
|
180
|
-
listeners = addTempListeners(this, 'connect');
|
|
181
|
-
if (config.debug) {
|
|
182
|
-
this.debug = config.debug;
|
|
183
|
-
this.debugMsg('connect: Debugging turned on');
|
|
184
|
-
this.debugMsg(`ssh2-sftp-client Version: ${this.version} `, process.versions);
|
|
185
|
-
}
|
|
186
|
-
this.promiseLimit = config.promiseLimit ?? 10;
|
|
187
|
-
if (this.sftp) {
|
|
188
|
-
throw this.fmtError(
|
|
189
|
-
'An existing SFTP connection is already defined',
|
|
190
|
-
'connect',
|
|
191
|
-
errorCode.connect,
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
const retryOpts = {
|
|
195
|
-
retries: config.retries ?? 1,
|
|
196
|
-
factor: config.retry_factor ?? 2,
|
|
197
|
-
minTimeout: config.retry_minTimeout ?? 25000,
|
|
198
|
-
};
|
|
199
|
-
await promiseRetry(retryOpts, async (retry, attempt) => {
|
|
200
|
-
try {
|
|
201
|
-
this.debugMsg(`connect: Connect attempt ${attempt}`);
|
|
202
|
-
await this.getConnection(config);
|
|
203
|
-
} catch (err) {
|
|
204
|
-
switch (err.code) {
|
|
205
|
-
case 'ENOTFOUND':
|
|
206
|
-
case 'ECONNREFUSED':
|
|
207
|
-
case 'ERR_SOCKET_BAD_PORT': {
|
|
208
|
-
throw err;
|
|
209
|
-
}
|
|
210
|
-
case undefined: {
|
|
211
|
-
if (
|
|
212
|
-
err.message.endsWith('All configured authentication methods failed') ||
|
|
213
|
-
err.message.startsWith('Cannot parse privateKey')
|
|
214
|
-
) {
|
|
215
|
-
throw err;
|
|
216
|
-
}
|
|
217
|
-
retry(err);
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
default: {
|
|
221
|
-
retry(err);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
const sftp = await this.getSftpChannel();
|
|
227
|
-
this.endCalled = false;
|
|
228
|
-
return sftp;
|
|
229
|
-
} catch (err) {
|
|
230
|
-
this.end();
|
|
231
|
-
throw err.custom ? err : this.fmtError(err, 'connect');
|
|
232
|
-
} finally {
|
|
233
|
-
removeTempListeners(this, listeners, 'connect');
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
175
|
/**
|
|
238
176
|
* @async
|
|
239
177
|
*
|
|
@@ -1408,7 +1346,7 @@ class SftpClient {
|
|
|
1408
1346
|
} catch (err) {
|
|
1409
1347
|
throw err.custom ? err : this.fmtError(err.message, 'createReadStream', err.code);
|
|
1410
1348
|
} finally {
|
|
1411
|
-
removeTempListeners(this, listeners, '
|
|
1349
|
+
removeTempListeners(this, listeners, 'createReadStream');
|
|
1412
1350
|
}
|
|
1413
1351
|
}
|
|
1414
1352
|
|