ssh2-sftp-client 10.0.3 → 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.
Files changed (5) hide show
  1. package/README.md +177 -142
  2. package/README.org +152 -50
  3. package/package.json +9 -10
  4. package/src/index.js +76 -147
  5. package/src/utils.js +83 -38
package/README.md CHANGED
@@ -1,92 +1,131 @@
1
- - [Overview](#orga044be2)
2
- - [Version 10.0.0 Changes](#org5a95165)
3
- - [Installation](#org4295c8d)
4
- - [Basic Usage](#org7c7ac64)
5
- - [Documentation](#org47c8184)
6
- - [Specifying Paths](#org3436700)
7
- - [Methods](#org8effeda)
8
- - [new SftpClient(name) ===> SFTP client object](#orgb641cd7)
9
- - [connect(config) ===> SFTP object](#org054724c)
10
- - [list(path, filter) ==> Array[object]](#org241650c)
11
- - [exists(path) ==> boolean](#orgec95901)
12
- - [stat(path) ==> object](#org1cb80c9)
13
- - [get(path, dst, options) ==> String|Stream|Buffer](#org5248523)
14
- - [fastGet(remotePath, localPath, options) ===> string](#org49ee6bb)
15
- - [put(src, remotePath, options) ==> string](#org90c60ca)
16
- - [fastPut(localPath, remotePath, options) ==> string](#org3f737cf)
17
- - [append(input, remotePath, options) ==> string](#orgec4dff5)
18
- - [mkdir(path, recursive) ==> string](#orgc0728b6)
19
- - [rmdir(path, recursive) ==> string](#org6927e29)
20
- - [delete(path, noErrorOK) ==> string](#orgcf3a9b7)
21
- - [rename(fromPath, toPath) ==> string](#org9a3f834)
22
- - [posixRename(fromPath, toPath) ==> string](#orgf428194)
23
- - [chmod(path, mode) ==> string](#orgcc1d70a)
24
- - [realPath(path) ===> string](#org0b200a7)
25
- - [cwd() ==> string](#orgae0fe4e)
26
- - [uploadDir(srcDir, dstDir, options) ==> string](#org451886d)
27
- - [downloadDir(srcDir, dstDir, options) ==> string](#org49e357f)
28
- - [createReadStream(remotePath, options)) ==> stream object](#orga25bbd5)
29
- - [createWriteStream(remotePath, options) ==> stream object](#orgc593764)
30
- - [rcopy(srcPath, dstPath) ==> string](#orgfaaf765)
31
- - [end() ==> boolean](#orgd1c464a)
32
- - [Add and Remove Listeners](#orgccff405)
33
- - [Platform Quirks & Warnings](#org17e30f1)
34
- - [Server Capabilities](#org25b6c01)
35
- - [Issues with `fastPut()` and `fastGet()` Methods](#orgae85e0b)
36
- - [Promises, Events & Managing Exceptions](#org2511fe9)
37
- - [Adding Custom Handlers](#orgb041e8e)
38
- - [Windows Based Servers](#org279eef7)
39
- - [Don't Re-use SftpClient Objects](#org1d1a2ce)
40
- - [FAQ](#orgcc893b5)
41
- - [Remote server drops connections with only an end event](#orgb5071b8)
42
- - [How can I pass writeable stream as dst for get method?](#org85632d7)
43
- - [How can I upload files without having to specify a password?](#org783398b)
44
- - [How can I connect through a Socks Proxy](#org1ade32e)
45
- - [Timeout while waiting for handshake or handshake errors](#org98434f2)
46
- - [How can I limit upload/download speed](#orgb24e42e)
47
- - [Connection hangs or fails for larger files](#org9603b10)
48
- - [Typescript definition file out of date](#orgc04a5c5)
49
- - [Examples](#orgd9ac19e)
50
- - [Troubleshooting](#org80d0f58)
51
- - [Common Errors](#orgd5409ee)
52
- - [Not returning the promise in a `then()` block](#org1087cbf)
53
- - [Mixing Promise Chains and Async/Await](#org2bab537)
54
- - [Try/catch and Error Handlers](#org0a6ff84)
55
- - [Server Differences](#orgd2993cc)
56
- - [Avoid Concurrent Operations](#org4db9137)
57
- - [Debugging Support](#org04b7c4c)
58
- - [Logging Issues](#orgfa0c326)
59
- - [Pull Requests](#org455cc3e)
60
- - [Contributors](#org0408f3a)
61
-
62
-
63
-
64
- <a id="orga044be2"></a>
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>
65
66
 
66
67
  # Overview
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
+ 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.
69
70
 
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
+ 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.
71
72
 
72
- Current stable release is \*v10.0.3
73
+ Current stable release is \*v12.0.0.
73
74
 
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
-
76
- Node versions < 16.x are not supported.
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.
77
76
 
78
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).
79
78
 
80
79
 
81
- <a id="org5a95165"></a>
80
+ <a id="orgaf728c6"></a>
81
+
82
+ ## Version 12.0.0 Changes
83
+
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
+
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
+
88
+
89
+ <a id="org6f33d22"></a>
90
+
91
+ ## Background
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` 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
+
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
+
97
+ - All direct interactions with the `ssh2` API are wrapped in promise objects. When the API call succeeds, the assoiated promise will be sucessfully resolved. When an error occurs, the promise is rejected.
98
+
99
+ - An error can either be due to a low level network error, such as a lost connection to the remote server or due to an operational error, such as a file not existing or not having the appropriate permissions for access.
100
+
101
+ - Each of the available `SftpClient` methods wrap the method call inside a promise. In crwating eacvh promise, the class adds temporary event listenrs for the error, end and close events and links those liseners to the method's promise via it's `reject()` method.
82
102
 
83
- ## Version 10.0.0 Changes
103
+ - If a promise is waiting to be fulfilled when either of the two types of errors occurs, the error will be communicated back to client code via a rejected promise.
84
104
 
85
- - 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.
86
- - Various minor documentation fixes and some minor fixes for typos in option property names.
105
+ - When the `ssh2` emitter raises an event outside the context of any promise, that event will be handled by global event handlers. By default, these event handlers will log the event and will invalidate any existing socket connection objects, preventing any further API calls until a new connection is extablished.
87
106
 
107
+ - The `SftpClient` class constructor supports an optoinal second argument which is an objedct whi8ch can have any of three properties representing event callbaqck funct5ions which will be executed for each of the possible events error, end and close.
88
108
 
89
- <a id="org4295c8d"></a>
109
+ The need for both global listeners and temporary promise listeners is because network end, close or error events can occur at any time, including in-betwseen API calls. During an API call, a promise is active and can be used to communicate event information back to the calling code via normal promise communication means i.e. async/await with try/catch or promise chain's then/catch mechanism. However, outside API calls, no promise exists and there is no reliable mechanism to return error and other event information back to calling code. You cannot reliably use try/cdatch to catch errorsx thrown inside event listenrs as you lack control over when the listener code runs. Your try/catch block can easily complete before the error is raised as there is no equivalent *await* type functionality in this situation.
110
+
111
+ As there is no simple default way to return error and other event information back to the calling code, `ssh2-sftp-client` doesn't try to. Instead, the default action is to just log the event information and invalidate any existing sftp connections. This strategy is often sufficient for many use cases. For those cases where it isn't, client code can pass in end, close and/or error listener functions when instantiating the `SftpClient` object. If provided, these listners will be executed whenever the default global listeners are executed, which is whenever the `ssh2` event emitter raises an end, close or error event which is not handled by one of the temporary promise linked event listeners.
112
+
113
+ Version 11 of `ssh2-sftp-client` also changes the behaviour of the temporary promise linked *end* and *close* listeners. Prior to versioln 11, these listeners did not reject promises. They would only invalidate the underlying `ssh` connection object. Only the *error* listener would actually reject the associated promise. This was done because you cannot guarantee the order in which events are responded to.
114
+
115
+ In most cases, events will occur in the order *error*, *end* and then *close*. The error event would contain details about the cause of the error while the end and close events just communicvate that these events have been raised. The normal flow would be
116
+
117
+ - Error event occurs including error cause description. Listener catches event, creates an error object and calls the associated promises reject function to reject the promise. The calling process receives a rejected promise object.
118
+
119
+ - End event occurs. The end listener catches the event and marks the connection object as invalid as the socket connection has been ended. There is no need to call the reject method of the associated promise as it has already been called by the error listener and you can only call one promise resolution function.
120
+
121
+ - Close event occurs. This event means the socket connection has been closed. The listener will ensure any connection information has been invalidated. Aga8in, ther is no need to call the reject method of the associated promise as it has already been called by the error listener.
122
+
123
+ In some cases, no ende event is raised and you only get an error event followed by a close event. In versions of `ssh2-sftp-client` prior to version 11, neither the end or the close listeners attempted to call the reject method of the associated promise. It was assumed that all sftp servers would raise an error event whenever a connection was unexpectedly ended or closed. Unfortunately, it turns out some sftp servers are not well behaved and will terminate the connection without providing any error information or raising an error event. When this occurred in versions prior to version 11, it could result in either an API call hanging because its associated promise never gets rejected or resolved or the call gets rejected with a timeout error aftrer a significant delay.
124
+
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
+
127
+
128
+ <a id="org8439307"></a>
90
129
 
91
130
  # Installation
92
131
 
@@ -95,7 +134,7 @@ npm install ssh2-sftp-client
95
134
  ```
96
135
 
97
136
 
98
- <a id="org7c7ac64"></a>
137
+ <a id="orgaf41082"></a>
99
138
 
100
139
  # Basic Usage
101
140
 
@@ -118,18 +157,18 @@ sftp.connect({
118
157
  ```
119
158
 
120
159
 
121
- <a id="org47c8184"></a>
160
+ <a id="org5108c1b"></a>
122
161
 
123
162
  # Documentation
124
163
 
125
- 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.
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.
126
165
 
127
166
  All the methods will return a Promise, except for `on(), ~removeListener()`, `createReadStream` and `createWriteStream`, which are typically only used in special use cases.
128
167
 
129
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.
130
169
 
131
170
 
132
- <a id="org3436700"></a>
171
+ <a id="org72e8719"></a>
133
172
 
134
173
  ## Specifying Paths
135
174
 
@@ -162,20 +201,21 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
162
201
  This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
163
202
 
164
203
 
165
- <a id="org8effeda"></a>
204
+ <a id="orgfe65544"></a>
166
205
 
167
206
  ## Methods
168
207
 
169
208
 
170
- <a id="orgb641cd7"></a>
209
+ <a id="orgc6511be"></a>
171
210
 
172
- ### new SftpClient(name) ===> SFTP client object
211
+ ### new SftpClient(name, callbacks) ===> SFTP client object
173
212
 
174
213
  Constructor to create a new `ssh2-sftp-client` object. An optional `name` string can be provided, which will be used in error messages to help identify which client has thrown the error.
175
214
 
176
215
  1. Constructor Arguments
177
216
 
178
- - **name:** string. An optional name string used in error messages
217
+ - **name:** string. An optional name string used in error messages. Defaults to the string 'sftp'
218
+ - **callbacks:** object. An object with the properties error, end and close. Associted with each property is a function that will be executed whenever the associated global event listener for error, end or close is executed. The error function should accept one argument. The end and close functions have no arguments. Default functions just print a message to `console.log()`.
179
219
 
180
220
  2. Example Use
181
221
 
@@ -206,7 +246,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
206
246
  ```
207
247
 
208
248
 
209
- <a id="org054724c"></a>
249
+ <a id="org9fbbaaa"></a>
210
250
 
211
251
  ### connect(config) ===> SFTP object
212
252
 
@@ -216,9 +256,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
216
256
 
217
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.
218
258
 
219
- 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.
220
-
221
- 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.
222
260
 
223
261
  ```javascript
224
262
  // common options
@@ -237,9 +275,6 @@ Connect to an sftp server. Full documentation for connection options is availabl
237
275
  strictVendor: true, // boolean - Performs a strict server vendor check
238
276
  debug: myDebug,// function - Set this to a function that receives a single
239
277
  // string argument to get detailed (local) debug information.
240
- retries: 2, // integer. Number of times to retry connecting
241
- retry_factor: 2, // integer. Time factor used to calculate time between retries
242
- retry_minTimeout: 2000, // integer. Minimum timeout between attempts
243
278
  promiseLimit: 10, // max concurrent promises for downloadDir/uploadDir
244
279
  };
245
280
 
@@ -275,7 +310,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
275
310
  ```
276
311
 
277
312
 
278
- <a id="org241650c"></a>
313
+ <a id="org73f17ce"></a>
279
314
 
280
315
  ### list(path, filter) ==> Array[object]
281
316
 
@@ -336,7 +371,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
336
371
  ```
337
372
 
338
373
 
339
- <a id="orgec95901"></a>
374
+ <a id="org09b5d66"></a>
340
375
 
341
376
  ### exists(path) ==> boolean
342
377
 
@@ -372,7 +407,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
372
407
  ```
373
408
 
374
409
 
375
- <a id="org1cb80c9"></a>
410
+ <a id="org91f6b04"></a>
376
411
 
377
412
  ### stat(path) ==> object
378
413
 
@@ -423,7 +458,7 @@ Returns the attributes associated with the object pointed to by `path`.
423
458
  ```
424
459
 
425
460
 
426
- <a id="org5248523"></a>
461
+ <a id="org78860d7"></a>
427
462
 
428
463
  ### get(path, dst, options) ==> String|Stream|Buffer
429
464
 
@@ -479,7 +514,7 @@ In general, if you're going to pass in a string as the destination, you are bett
479
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'.
480
515
 
481
516
 
482
- <a id="org49ee6bb"></a>
517
+ <a id="org075afe7"></a>
483
518
 
484
519
  ### fastGet(remotePath, localPath, options) ===> string
485
520
 
@@ -524,7 +559,7 @@ Bottom line, when it works, it tends to work reliably. However, for many servers
524
559
  ```
525
560
 
526
561
 
527
- <a id="org90c60ca"></a>
562
+ <a id="org3570f3c"></a>
528
563
 
529
564
  ### put(src, remotePath, options) ==> string
530
565
 
@@ -574,7 +609,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
574
609
  - **Tip:** If the src argument is a path string, consider just using `fastPut()`.
575
610
 
576
611
 
577
- <a id="org3f737cf"></a>
612
+ <a id="org23d30ed"></a>
578
613
 
579
614
  ### fastPut(localPath, remotePath, options) ==> string
580
615
 
@@ -620,7 +655,7 @@ Bottom line, when it works, it tends to work well. However, when it doesn't work
620
655
  ```
621
656
 
622
657
 
623
- <a id="orgec4dff5"></a>
658
+ <a id="org4d3d523"></a>
624
659
 
625
660
  ### append(input, remotePath, options) ==> string
626
661
 
@@ -664,7 +699,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
664
699
  ```
665
700
 
666
701
 
667
- <a id="orgc0728b6"></a>
702
+ <a id="org7ff954e"></a>
668
703
 
669
704
  ### mkdir(path, recursive) ==> string
670
705
 
@@ -692,7 +727,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
692
727
  ```
693
728
 
694
729
 
695
- <a id="org6927e29"></a>
730
+ <a id="orga241b96"></a>
696
731
 
697
732
  ### rmdir(path, recursive) ==> string
698
733
 
@@ -722,7 +757,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
722
757
  ```
723
758
 
724
759
 
725
- <a id="orgcf3a9b7"></a>
760
+ <a id="org07b0cde"></a>
726
761
 
727
762
  ### delete(path, noErrorOK) ==> string
728
763
 
@@ -751,7 +786,7 @@ Delete a file on the remote server.
751
786
  ```
752
787
 
753
788
 
754
- <a id="org9a3f834"></a>
789
+ <a id="org2d695fa"></a>
755
790
 
756
791
  ### rename(fromPath, toPath) ==> string
757
792
 
@@ -780,7 +815,7 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
780
815
  ```
781
816
 
782
817
 
783
- <a id="orgf428194"></a>
818
+ <a id="org54eef84"></a>
784
819
 
785
820
  ### posixRename(fromPath, toPath) ==> string
786
821
 
@@ -807,7 +842,7 @@ client.connect(config)
807
842
  ```
808
843
 
809
844
 
810
- <a id="orgcc1d70a"></a>
845
+ <a id="org34c2a04"></a>
811
846
 
812
847
  ### chmod(path, mode) ==> string
813
848
 
@@ -836,7 +871,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
836
871
  ```
837
872
 
838
873
 
839
- <a id="org0b200a7"></a>
874
+ <a id="org8e2be15"></a>
840
875
 
841
876
  ### realPath(path) ===> string
842
877
 
@@ -847,14 +882,14 @@ Converts a relative path to an absolute path on the remote server. This method i
847
882
  - **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
848
883
 
849
884
 
850
- <a id="orgae0fe4e"></a>
885
+ <a id="org2d235d7"></a>
851
886
 
852
887
  ### cwd() ==> string
853
888
 
854
889
  Returns what the server believes is the current remote working directory.
855
890
 
856
891
 
857
- <a id="org451886d"></a>
892
+ <a id="orgb4585c4"></a>
858
893
 
859
894
  ### uploadDir(srcDir, dstDir, options) ==> string
860
895
 
@@ -923,11 +958,11 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
923
958
  ```
924
959
 
925
960
 
926
- <a id="org49e357f"></a>
961
+ <a id="orgd2d4371"></a>
927
962
 
928
963
  ### downloadDir(srcDir, dstDir, options) ==> string
929
964
 
930
- Download the remote directory specified by `srcDir` to the local file system directory specified by `dstDir`. The `dstDir` directory will be created if required. All sub directories within `srcDir` will also be copied. Any existing files in the local path will be overwritten. No files in the local path will be deleted.
965
+ Download the content of the remote directory specified by `srcDir` to the local file system directory specified by `dstDir`. The `dstDir` directory will be created if required. All sub directories within `srcDir` will also be copied. Any existing files in the local path will be overwritten. No files in the local path will be deleted.
931
966
 
932
967
  The method also emits `download` events to provide a way to monitor download progress. The download event listener is called with one argument, an object with two properties, source and destination. The source property is the path to the remote file that has been downloaded and the destination is the local path to where the file was downloaded to. You can add a listener for this event using the `on()` method.
933
968
 
@@ -988,7 +1023,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
988
1023
  ```
989
1024
 
990
1025
 
991
- <a id="orga25bbd5"></a>
1026
+ <a id="orgfd916cd"></a>
992
1027
 
993
1028
  ### createReadStream(remotePath, options)) ==> stream object
994
1029
 
@@ -1005,7 +1040,7 @@ Returns a read stream object which is attached to the remote file specified by t
1005
1040
  - **end:** Position to stop reading bytes (inclusive).
1006
1041
 
1007
1042
 
1008
- <a id="orgc593764"></a>
1043
+ <a id="orgbba804d"></a>
1009
1044
 
1010
1045
  ### createWriteStream(remotePath, options) ==> stream object
1011
1046
 
@@ -1020,7 +1055,7 @@ Returns a write stream object which is attached to the remote file specified in
1020
1055
  - **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
1021
1056
 
1022
1057
 
1023
- <a id="orgfaaf765"></a>
1058
+ <a id="org96b35cc"></a>
1024
1059
 
1025
1060
  ### rcopy(srcPath, dstPath) ==> string
1026
1061
 
@@ -1030,7 +1065,7 @@ Perform a remote file copy. The file identified by the `srcPath` argument will b
1030
1065
  - **dstPath:** Path to where the copy will be created specified as a string
1031
1066
 
1032
1067
 
1033
- <a id="orgd1c464a"></a>
1068
+ <a id="org7600349"></a>
1034
1069
 
1035
1070
  ### end() ==> boolean
1036
1071
 
@@ -1054,7 +1089,7 @@ Ends the current client session, releasing the client socket and associated reso
1054
1089
  ```
1055
1090
 
1056
1091
 
1057
- <a id="orgccff405"></a>
1092
+ <a id="orgff68122"></a>
1058
1093
 
1059
1094
  ### Add and Remove Listeners
1060
1095
 
@@ -1075,12 +1110,12 @@ Although normally not required, you can add and remove custom listeners on the s
1075
1110
  Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
1076
1111
 
1077
1112
 
1078
- <a id="org17e30f1"></a>
1113
+ <a id="orgcd3e52c"></a>
1079
1114
 
1080
1115
  # Platform Quirks & Warnings
1081
1116
 
1082
1117
 
1083
- <a id="org25b6c01"></a>
1118
+ <a id="org44699cb"></a>
1084
1119
 
1085
1120
  ## Server Capabilities
1086
1121
 
@@ -1089,7 +1124,7 @@ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-
1089
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.
1090
1125
 
1091
1126
 
1092
- <a id="orgae85e0b"></a>
1127
+ <a id="org219edb9"></a>
1093
1128
 
1094
1129
  ## Issues with `fastPut()` and `fastGet()` Methods
1095
1130
 
@@ -1098,7 +1133,7 @@ The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SF
1098
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.
1099
1134
 
1100
1135
 
1101
- <a id="org2511fe9"></a>
1136
+ <a id="orgdb50974"></a>
1102
1137
 
1103
1138
  ## Promises, Events & Managing Exceptions
1104
1139
 
@@ -1117,14 +1152,14 @@ The other area where additional events are fired is during the end() call. To de
1117
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.
1118
1153
 
1119
1154
 
1120
- <a id="orgb041e8e"></a>
1155
+ <a id="orge9ed516"></a>
1121
1156
 
1122
1157
  ### Adding Custom Handlers
1123
1158
 
1124
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.
1125
1160
 
1126
1161
 
1127
- <a id="org279eef7"></a>
1162
+ <a id="org04134c2"></a>
1128
1163
 
1129
1164
  ## Windows Based Servers
1130
1165
 
@@ -1133,7 +1168,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
1133
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.
1134
1169
 
1135
1170
 
1136
- <a id="org1d1a2ce"></a>
1171
+ <a id="orgb8a3ba1"></a>
1137
1172
 
1138
1173
  ## Don't Re-use SftpClient Objects
1139
1174
 
@@ -1142,12 +1177,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
1142
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.
1143
1178
 
1144
1179
 
1145
- <a id="orgcc893b5"></a>
1180
+ <a id="org8a8b24b"></a>
1146
1181
 
1147
1182
  # FAQ
1148
1183
 
1149
1184
 
1150
- <a id="orgb5071b8"></a>
1185
+ <a id="org5de7a15"></a>
1151
1186
 
1152
1187
  ## Remote server drops connections with only an end event
1153
1188
 
@@ -1158,7 +1193,7 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
1158
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.
1159
1194
 
1160
1195
 
1161
- <a id="org85632d7"></a>
1196
+ <a id="org21efa6c"></a>
1162
1197
 
1163
1198
  ## How can I pass writeable stream as dst for get method?
1164
1199
 
@@ -1217,7 +1252,7 @@ sftp
1217
1252
  ```
1218
1253
 
1219
1254
 
1220
- <a id="org783398b"></a>
1255
+ <a id="orgee1cf34"></a>
1221
1256
 
1222
1257
  ## How can I upload files without having to specify a password?
1223
1258
 
@@ -1252,7 +1287,7 @@ sftp.connect({
1252
1287
  ```
1253
1288
 
1254
1289
 
1255
- <a id="org1ade32e"></a>
1290
+ <a id="orga6297a7"></a>
1256
1291
 
1257
1292
  ## How can I connect through a Socks Proxy
1258
1293
 
@@ -1289,7 +1324,7 @@ client.connect({
1289
1324
  ```
1290
1325
 
1291
1326
 
1292
- <a id="org98434f2"></a>
1327
+ <a id="org628c88e"></a>
1293
1328
 
1294
1329
  ## Timeout while waiting for handshake or handshake errors
1295
1330
 
@@ -1298,7 +1333,7 @@ Some users have encountered the error 'Timeout while waiting for handshake' or '
1298
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.
1299
1334
 
1300
1335
 
1301
- <a id="orgb24e42e"></a>
1336
+ <a id="org1116140"></a>
1302
1337
 
1303
1338
  ## How can I limit upload/download speed
1304
1339
 
@@ -1338,7 +1373,7 @@ try {
1338
1373
  ```
1339
1374
 
1340
1375
 
1341
- <a id="org9603b10"></a>
1376
+ <a id="orgefe495c"></a>
1342
1377
 
1343
1378
  ## Connection hangs or fails for larger files
1344
1379
 
@@ -1349,21 +1384,21 @@ A symptom of this issue is that you are able to upload small files, but uploadin
1349
1384
  For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
1350
1385
 
1351
1386
 
1352
- <a id="orgc04a5c5"></a>
1387
+ <a id="orgedcb385"></a>
1353
1388
 
1354
1389
  ## Typescript definition file out of date
1355
1390
 
1356
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.
1357
1392
 
1358
1393
 
1359
- <a id="orgd9ac19e"></a>
1394
+ <a id="org3a23c04"></a>
1360
1395
 
1361
1396
  # Examples
1362
1397
 
1363
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.
1364
1399
 
1365
1400
 
1366
- <a id="org80d0f58"></a>
1401
+ <a id="org888858f"></a>
1367
1402
 
1368
1403
  # Troubleshooting
1369
1404
 
@@ -1378,14 +1413,14 @@ Note also that in the repository there are two useful directories. The first is
1378
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.
1379
1414
 
1380
1415
 
1381
- <a id="orgd5409ee"></a>
1416
+ <a id="orge820faa"></a>
1382
1417
 
1383
1418
  ## Common Errors
1384
1419
 
1385
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.
1386
1421
 
1387
1422
 
1388
- <a id="org1087cbf"></a>
1423
+ <a id="org52515e7"></a>
1389
1424
 
1390
1425
  ### Not returning the promise in a `then()` block
1391
1426
 
@@ -1422,7 +1457,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
1422
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.
1423
1458
 
1424
1459
 
1425
- <a id="org2bab537"></a>
1460
+ <a id="org8e86c97"></a>
1426
1461
 
1427
1462
  ### Mixing Promise Chains and Async/Await
1428
1463
 
@@ -1482,7 +1517,7 @@ async function doSftp() {
1482
1517
  ```
1483
1518
 
1484
1519
 
1485
- <a id="org0a6ff84"></a>
1520
+ <a id="org55f382c"></a>
1486
1521
 
1487
1522
  ### Try/catch and Error Handlers
1488
1523
 
@@ -1493,14 +1528,14 @@ The basic problem is that the try/catch block will have completed execution befo
1493
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.
1494
1529
 
1495
1530
 
1496
- <a id="orgd2993cc"></a>
1531
+ <a id="org76494fa"></a>
1497
1532
 
1498
1533
  ### Server Differences
1499
1534
 
1500
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.
1501
1536
 
1502
1537
 
1503
- <a id="org4db9137"></a>
1538
+ <a id="orgaedcad8"></a>
1504
1539
 
1505
1540
  ### Avoid Concurrent Operations
1506
1541
 
@@ -1509,7 +1544,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
1509
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.
1510
1545
 
1511
1546
 
1512
- <a id="org04b7c4c"></a>
1547
+ <a id="orgf7b85d3"></a>
1513
1548
 
1514
1549
  ## Debugging Support
1515
1550
 
@@ -1542,7 +1577,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
1542
1577
  ```
1543
1578
 
1544
1579
 
1545
- <a id="orgfa0c326"></a>
1580
+ <a id="orgb243d19"></a>
1546
1581
 
1547
1582
  # Logging Issues
1548
1583
 
@@ -1559,7 +1594,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
1559
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.
1560
1595
 
1561
1596
 
1562
- <a id="org455cc3e"></a>
1597
+ <a id="org3d32e62"></a>
1563
1598
 
1564
1599
  # Pull Requests
1565
1600
 
@@ -1576,7 +1611,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
1576
1611
  - **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
1577
1612
 
1578
1613
 
1579
- <a id="org0408f3a"></a>
1614
+ <a id="org99ba3a6"></a>
1580
1615
 
1581
1616
  # Contributors
1582
1617