ssh2-sftp-client 12.0.0 → 12.1.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 CHANGED
@@ -1,68 +1,70 @@
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>
1
+ - [Overview](#orge272967)
2
+ - [Version 12.0.0 Changes](#org9688783)
3
+ - [Version 12.0.1 Changes](#org6c6cbc7)
4
+ - [Version 12.1.0 Changes](#org678bfad)
5
+ - [Background](#org41ff3b3)
6
+ - [Installation](#org64016b3)
7
+ - [Basic Usage](#org0cbc36d)
8
+ - [Documentation](#org161d25d)
9
+ - [Specifying Paths](#org569c530)
10
+ - [Methods](#orge938fe1)
11
+ - [new SftpClient(name, callbacks) ===> SFTP client object](#org87fb7cb)
12
+ - [connect(config) ===> SFTP object](#org00cd0ef)
13
+ - [list(path, filter) ==> Array[object]](#org2471bc8)
14
+ - [exists(path) ==> boolean](#org69f0979)
15
+ - [stat(path) ==> object](#org70605b0)
16
+ - [get(path, dst, options) ==> String|Stream|Buffer](#org6ef818c)
17
+ - [fastGet(remotePath, localPath, options) ===> string](#org4be5f02)
18
+ - [put(src, remotePath, options) ==> string](#org29892b8)
19
+ - [fastPut(localPath, remotePath, options) ==> string](#org8b51862)
20
+ - [append(input, remotePath, options) ==> string](#org60825d3)
21
+ - [mkdir(path, recursive) ==> string](#orga9278dd)
22
+ - [rmdir(path, recursive) ==> string](#org49dc87c)
23
+ - [delete(path, noErrorOK) ==> string](#orgbe8caee)
24
+ - [rename(fromPath, toPath) ==> string](#orgc525c46)
25
+ - [posixRename(fromPath, toPath) ==> string](#org72d1dcb)
26
+ - [chmod(path, mode) ==> string](#org0c71566)
27
+ - [realPath(path) ===> string](#org7dcfedc)
28
+ - [cwd() ==> string](#orgdaa63b2)
29
+ - [uploadDir(srcDir, dstDir, options) ==> string](#orgf47dba2)
30
+ - [downloadDir(srcDir, dstDir, options) ==> string](#orgc046861)
31
+ - [createReadStream(remotePath, options)) ==> stream object](#orgbb40821)
32
+ - [createWriteStream(remotePath, options) ==> stream object](#org5b94858)
33
+ - [rcopy(srcPath, dstPath) ==> string](#org352c030)
34
+ - [end() ==> boolean](#org9591ce4)
35
+ - [Add and Remove Listeners](#org5f4115d)
36
+ - [Platform Quirks & Warnings](#org99f279a)
37
+ - [Server Capabilities](#org5f13ec7)
38
+ - [Issues with `fastPut()` and `fastGet()` Methods](#org0580b2e)
39
+ - [Promises, Events & Managing Exceptions](#org836ad16)
40
+ - [Adding Custom Handlers](#org7eb04eb)
41
+ - [Windows Based Servers](#org64d4763)
42
+ - [Don't Re-use SftpClient Objects](#org4e39c0f)
43
+ - [FAQ](#org55e6f0b)
44
+ - [Remote server drops connections with only an end event](#orgbba8948)
45
+ - [How can I pass writeable stream as dst for get method?](#org3380bb0)
46
+ - [How can I upload files without having to specify a password?](#org144745f)
47
+ - [How can I connect through a Socks Proxy](#org65cf78c)
48
+ - [Timeout while waiting for handshake or handshake errors](#orgc1209ba)
49
+ - [How can I limit upload/download speed](#orgec6bc5b)
50
+ - [Connection hangs or fails for larger files](#org222771b)
51
+ - [Typescript definition file out of date](#org64453a9)
52
+ - [Examples](#orge079610)
53
+ - [Troubleshooting](#orgedc17fd)
54
+ - [Common Errors](#org7756613)
55
+ - [Not returning the promise in a `then()` block](#orgdf44163)
56
+ - [Mixing Promise Chains and Async/Await](#org643899f)
57
+ - [Try/catch and Error Handlers](#orgf1586cd)
58
+ - [Server Differences](#org57723b9)
59
+ - [Avoid Concurrent Operations](#org8f56d89)
60
+ - [Debugging Support](#org4c34184)
61
+ - [Logging Issues](#org63b74c6)
62
+ - [Pull Requests](#org308be7d)
63
+ - [Contributors](#org585b558)
64
+
65
+
66
+
67
+ <a id="orge272967"></a>
66
68
 
67
69
  # Overview
68
70
 
@@ -70,62 +72,80 @@ This package provides the class SftpClient, an SFTP client for node.js. It is a
70
72
 
71
73
  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
74
 
73
- Current stable release is \*v12.0.0.
75
+ Current stable release is \*v12.1.0.
74
76
 
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
+ Code has been tested against Node versions 24.14.0. Node versions prior to v20.x are not supported.
76
78
 
77
79
  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
80
 
79
81
 
80
- <a id="orgaf728c6"></a>
82
+ <a id="org9688783"></a>
81
83
 
82
84
  ## Version 12.0.0 Changes
83
85
 
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.
86
+ 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 connection failed on the 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
87
 
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.
88
+ Another reason this functionality was removed is because if you really want it, it is straight forward to create your own wrapper around the connect call which uses one of the mnay promise retry packages available. It is much simpler to add a retry mechanism for a specific application than it is to implement a flexible solution which supports all possible combination of connection options, authentication methods, proxies etc.
87
89
 
88
90
 
89
- <a id="org6f33d22"></a>
91
+ <a id="org6c6cbc7"></a>
92
+
93
+ ## Version 12.0.1 Changes
94
+
95
+ Bug Fixes
96
+
97
+ - Fix typo error in specification of generic error code in error handler. Typo was causing an undefined symbol error.
98
+ - Fix bug in stream error handling. Read and write streams had an error handler defined with 'once' rather than 'on'. This caused a bug when an API call caused more than one error. For example, a network connectivity error would cause a stream timeout error followed by a no response from remote host error when the error handler attempted to cleanup and destroy the remote stream. The second error whould not be handled and would bubble up to the parent node process, which would then exit with an unhandled exception error.
99
+ - Added calls to \_resetEventFlags() to finally clauses to ensure event flags are reset when temp listeners are removed. Without this change, global listeners would not fire when they should after their has been an earlier handled error in the last API call.
100
+
101
+
102
+ <a id="org678bfad"></a>
103
+
104
+ ## Version 12.1.0 Changes
105
+
106
+ The main change in this version is to address an issue with disconnect when accessing Microsoft's SFTP server. Due to the way the Microsoft server implements closing of connections, an exception is thrown even when there has been a legitimate request to end the connection. The fix is to ignore this specific *error* case. Thanks to @rhyswilliamsza for the patch.
107
+
108
+
109
+ <a id="org41ff3b3"></a>
90
110
 
91
111
  ## Background
92
112
 
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.
113
+ In basic terms `ssh2-sftp-client` is a simple wrapper around the `ssh2` package which provides a promise based 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
114
 
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;
115
+ Wrapping an event based API with a promise based API comes with a number of challenges. In particular, efficiently and reliably managing events within the context of asynchronous code execution. This package uses the following strategies;
96
116
 
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.
117
+ - All direct interactions with the `ssh2` API are wrapped in promise objects. When the API call succeeds, the associated promise will be successfully resolved. When an error occurs, the promise is rejected.
98
118
 
99
119
  - 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
120
 
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.
121
+ - Each of the available `SftpClient` methods wrap the method call inside a promise. In creating each promise, the class adds temporary event listeners for the error, end and close events and links those liseners to the method's promise via its `reject()` method.
102
122
 
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.
123
+ - If a promise is waiting to be fulfilled when an error occurs, the error will be communicated back to the client code via the rejected promise.
104
124
 
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.
125
+ - 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 established.
106
126
 
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.
127
+ - The `SftpClient` class constructor supports an optoinal second argument which is an object which can have any of the three properties \`error\`, \`end\`, and \`close\`. The values associated with these properties are callback functions that will be run if an error event with the same name as the property is raised and has not been handled by one of the temporary event handlers. The error callback should have a single parameter, the error raised by the event. The other callbacks have no arguments.
108
128
 
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.
129
+ 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-between 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 channels 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 errors 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
130
 
111
131
  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
132
 
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.
133
+ Version 11 of `ssh2-sftp-client` also changes the behaviour of the temporary promise linked *end* and *close* listeners. Prior to version 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
134
 
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
135
+ 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 communicate that these events have been raised. The normal flow would be
116
136
 
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.
137
+ - Error event occurs including error cause description. Listener catches event, creates an error object and calls the associated promise's reject function to reject the promise. The calling process receives a rejected promise object.
118
138
 
119
139
  - 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
140
 
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.
141
+ - Close event occurs. This event means the socket connection has been closed. The listener will ensure any connection information has been invalidated. Again, there is no need to call the reject method of the associated promise as it has already been called by the error listener.
122
142
 
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.
143
+ In some cases, no end 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
144
 
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.
145
+ 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 responded 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
146
 
127
147
 
128
- <a id="org8439307"></a>
148
+ <a id="org64016b3"></a>
129
149
 
130
150
  # Installation
131
151
 
@@ -134,7 +154,7 @@ npm install ssh2-sftp-client
134
154
  ```
135
155
 
136
156
 
137
- <a id="orgaf41082"></a>
157
+ <a id="org0cbc36d"></a>
138
158
 
139
159
  # Basic Usage
140
160
 
@@ -157,7 +177,7 @@ sftp.connect({
157
177
  ```
158
178
 
159
179
 
160
- <a id="org5108c1b"></a>
180
+ <a id="org161d25d"></a>
161
181
 
162
182
  # Documentation
163
183
 
@@ -168,7 +188,7 @@ All the methods will return a Promise, except for `on(), ~removeListener()`, `cr
168
188
  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
189
 
170
190
 
171
- <a id="org72e8719"></a>
191
+ <a id="org569c530"></a>
172
192
 
173
193
  ## Specifying Paths
174
194
 
@@ -201,12 +221,12 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
201
221
  This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
202
222
 
203
223
 
204
- <a id="orgfe65544"></a>
224
+ <a id="orge938fe1"></a>
205
225
 
206
226
  ## Methods
207
227
 
208
228
 
209
- <a id="orgc6511be"></a>
229
+ <a id="org87fb7cb"></a>
210
230
 
211
231
  ### new SftpClient(name, callbacks) ===> SFTP client object
212
232
 
@@ -246,7 +266,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
246
266
  ```
247
267
 
248
268
 
249
- <a id="org9fbbaaa"></a>
269
+ <a id="org00cd0ef"></a>
250
270
 
251
271
  ### connect(config) ===> SFTP object
252
272
 
@@ -310,7 +330,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
310
330
  ```
311
331
 
312
332
 
313
- <a id="org73f17ce"></a>
333
+ <a id="org2471bc8"></a>
314
334
 
315
335
  ### list(path, filter) ==> Array[object]
316
336
 
@@ -371,7 +391,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
371
391
  ```
372
392
 
373
393
 
374
- <a id="org09b5d66"></a>
394
+ <a id="org69f0979"></a>
375
395
 
376
396
  ### exists(path) ==> boolean
377
397
 
@@ -407,7 +427,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
407
427
  ```
408
428
 
409
429
 
410
- <a id="org91f6b04"></a>
430
+ <a id="org70605b0"></a>
411
431
 
412
432
  ### stat(path) ==> object
413
433
 
@@ -458,7 +478,7 @@ Returns the attributes associated with the object pointed to by `path`.
458
478
  ```
459
479
 
460
480
 
461
- <a id="org78860d7"></a>
481
+ <a id="org6ef818c"></a>
462
482
 
463
483
  ### get(path, dst, options) ==> String|Stream|Buffer
464
484
 
@@ -514,7 +534,7 @@ In general, if you're going to pass in a string as the destination, you are bett
514
534
  - **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'.
515
535
 
516
536
 
517
- <a id="org075afe7"></a>
537
+ <a id="org4be5f02"></a>
518
538
 
519
539
  ### fastGet(remotePath, localPath, options) ===> string
520
540
 
@@ -559,7 +579,7 @@ Bottom line, when it works, it tends to work reliably. However, for many servers
559
579
  ```
560
580
 
561
581
 
562
- <a id="org3570f3c"></a>
582
+ <a id="org29892b8"></a>
563
583
 
564
584
  ### put(src, remotePath, options) ==> string
565
585
 
@@ -609,7 +629,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
609
629
  - **Tip:** If the src argument is a path string, consider just using `fastPut()`.
610
630
 
611
631
 
612
- <a id="org23d30ed"></a>
632
+ <a id="org8b51862"></a>
613
633
 
614
634
  ### fastPut(localPath, remotePath, options) ==> string
615
635
 
@@ -655,7 +675,7 @@ Bottom line, when it works, it tends to work well. However, when it doesn't work
655
675
  ```
656
676
 
657
677
 
658
- <a id="org4d3d523"></a>
678
+ <a id="org60825d3"></a>
659
679
 
660
680
  ### append(input, remotePath, options) ==> string
661
681
 
@@ -699,7 +719,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
699
719
  ```
700
720
 
701
721
 
702
- <a id="org7ff954e"></a>
722
+ <a id="orga9278dd"></a>
703
723
 
704
724
  ### mkdir(path, recursive) ==> string
705
725
 
@@ -727,7 +747,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
727
747
  ```
728
748
 
729
749
 
730
- <a id="orga241b96"></a>
750
+ <a id="org49dc87c"></a>
731
751
 
732
752
  ### rmdir(path, recursive) ==> string
733
753
 
@@ -757,7 +777,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
757
777
  ```
758
778
 
759
779
 
760
- <a id="org07b0cde"></a>
780
+ <a id="orgbe8caee"></a>
761
781
 
762
782
  ### delete(path, noErrorOK) ==> string
763
783
 
@@ -786,7 +806,7 @@ Delete a file on the remote server.
786
806
  ```
787
807
 
788
808
 
789
- <a id="org2d695fa"></a>
809
+ <a id="orgc525c46"></a>
790
810
 
791
811
  ### rename(fromPath, toPath) ==> string
792
812
 
@@ -815,7 +835,7 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
815
835
  ```
816
836
 
817
837
 
818
- <a id="org54eef84"></a>
838
+ <a id="org72d1dcb"></a>
819
839
 
820
840
  ### posixRename(fromPath, toPath) ==> string
821
841
 
@@ -842,7 +862,7 @@ client.connect(config)
842
862
  ```
843
863
 
844
864
 
845
- <a id="org34c2a04"></a>
865
+ <a id="org0c71566"></a>
846
866
 
847
867
  ### chmod(path, mode) ==> string
848
868
 
@@ -871,7 +891,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
871
891
  ```
872
892
 
873
893
 
874
- <a id="org8e2be15"></a>
894
+ <a id="org7dcfedc"></a>
875
895
 
876
896
  ### realPath(path) ===> string
877
897
 
@@ -882,14 +902,14 @@ Converts a relative path to an absolute path on the remote server. This method i
882
902
  - **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
883
903
 
884
904
 
885
- <a id="org2d235d7"></a>
905
+ <a id="orgdaa63b2"></a>
886
906
 
887
907
  ### cwd() ==> string
888
908
 
889
909
  Returns what the server believes is the current remote working directory.
890
910
 
891
911
 
892
- <a id="orgb4585c4"></a>
912
+ <a id="orgf47dba2"></a>
893
913
 
894
914
  ### uploadDir(srcDir, dstDir, options) ==> string
895
915
 
@@ -958,7 +978,7 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
958
978
  ```
959
979
 
960
980
 
961
- <a id="orgd2d4371"></a>
981
+ <a id="orgc046861"></a>
962
982
 
963
983
  ### downloadDir(srcDir, dstDir, options) ==> string
964
984
 
@@ -1023,7 +1043,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
1023
1043
  ```
1024
1044
 
1025
1045
 
1026
- <a id="orgfd916cd"></a>
1046
+ <a id="orgbb40821"></a>
1027
1047
 
1028
1048
  ### createReadStream(remotePath, options)) ==> stream object
1029
1049
 
@@ -1040,7 +1060,7 @@ Returns a read stream object which is attached to the remote file specified by t
1040
1060
  - **end:** Position to stop reading bytes (inclusive).
1041
1061
 
1042
1062
 
1043
- <a id="orgbba804d"></a>
1063
+ <a id="org5b94858"></a>
1044
1064
 
1045
1065
  ### createWriteStream(remotePath, options) ==> stream object
1046
1066
 
@@ -1055,7 +1075,7 @@ Returns a write stream object which is attached to the remote file specified in
1055
1075
  - **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
1056
1076
 
1057
1077
 
1058
- <a id="org96b35cc"></a>
1078
+ <a id="org352c030"></a>
1059
1079
 
1060
1080
  ### rcopy(srcPath, dstPath) ==> string
1061
1081
 
@@ -1065,7 +1085,7 @@ Perform a remote file copy. The file identified by the `srcPath` argument will b
1065
1085
  - **dstPath:** Path to where the copy will be created specified as a string
1066
1086
 
1067
1087
 
1068
- <a id="org7600349"></a>
1088
+ <a id="org9591ce4"></a>
1069
1089
 
1070
1090
  ### end() ==> boolean
1071
1091
 
@@ -1089,7 +1109,7 @@ Ends the current client session, releasing the client socket and associated reso
1089
1109
  ```
1090
1110
 
1091
1111
 
1092
- <a id="orgff68122"></a>
1112
+ <a id="org5f4115d"></a>
1093
1113
 
1094
1114
  ### Add and Remove Listeners
1095
1115
 
@@ -1110,12 +1130,12 @@ Although normally not required, you can add and remove custom listeners on the s
1110
1130
  Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
1111
1131
 
1112
1132
 
1113
- <a id="orgcd3e52c"></a>
1133
+ <a id="org99f279a"></a>
1114
1134
 
1115
1135
  # Platform Quirks & Warnings
1116
1136
 
1117
1137
 
1118
- <a id="org44699cb"></a>
1138
+ <a id="org5f13ec7"></a>
1119
1139
 
1120
1140
  ## Server Capabilities
1121
1141
 
@@ -1124,7 +1144,7 @@ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-
1124
1144
  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.
1125
1145
 
1126
1146
 
1127
- <a id="org219edb9"></a>
1147
+ <a id="org0580b2e"></a>
1128
1148
 
1129
1149
  ## Issues with `fastPut()` and `fastGet()` Methods
1130
1150
 
@@ -1133,7 +1153,7 @@ The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SF
1133
1153
  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.
1134
1154
 
1135
1155
 
1136
- <a id="orgdb50974"></a>
1156
+ <a id="org836ad16"></a>
1137
1157
 
1138
1158
  ## Promises, Events & Managing Exceptions
1139
1159
 
@@ -1152,14 +1172,14 @@ The other area where additional events are fired is during the end() call. To de
1152
1172
  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.
1153
1173
 
1154
1174
 
1155
- <a id="orge9ed516"></a>
1175
+ <a id="org7eb04eb"></a>
1156
1176
 
1157
1177
  ### Adding Custom Handlers
1158
1178
 
1159
1179
  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.
1160
1180
 
1161
1181
 
1162
- <a id="org04134c2"></a>
1182
+ <a id="org64d4763"></a>
1163
1183
 
1164
1184
  ## Windows Based Servers
1165
1185
 
@@ -1168,7 +1188,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
1168
1188
  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.
1169
1189
 
1170
1190
 
1171
- <a id="orgb8a3ba1"></a>
1191
+ <a id="org4e39c0f"></a>
1172
1192
 
1173
1193
  ## Don't Re-use SftpClient Objects
1174
1194
 
@@ -1177,12 +1197,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
1177
1197
  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.
1178
1198
 
1179
1199
 
1180
- <a id="org8a8b24b"></a>
1200
+ <a id="org55e6f0b"></a>
1181
1201
 
1182
1202
  # FAQ
1183
1203
 
1184
1204
 
1185
- <a id="org5de7a15"></a>
1205
+ <a id="orgbba8948"></a>
1186
1206
 
1187
1207
  ## Remote server drops connections with only an end event
1188
1208
 
@@ -1193,7 +1213,7 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
1193
1213
  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.
1194
1214
 
1195
1215
 
1196
- <a id="org21efa6c"></a>
1216
+ <a id="org3380bb0"></a>
1197
1217
 
1198
1218
  ## How can I pass writeable stream as dst for get method?
1199
1219
 
@@ -1252,7 +1272,7 @@ sftp
1252
1272
  ```
1253
1273
 
1254
1274
 
1255
- <a id="orgee1cf34"></a>
1275
+ <a id="org144745f"></a>
1256
1276
 
1257
1277
  ## How can I upload files without having to specify a password?
1258
1278
 
@@ -1287,7 +1307,7 @@ sftp.connect({
1287
1307
  ```
1288
1308
 
1289
1309
 
1290
- <a id="orga6297a7"></a>
1310
+ <a id="org65cf78c"></a>
1291
1311
 
1292
1312
  ## How can I connect through a Socks Proxy
1293
1313
 
@@ -1324,7 +1344,7 @@ client.connect({
1324
1344
  ```
1325
1345
 
1326
1346
 
1327
- <a id="org628c88e"></a>
1347
+ <a id="orgc1209ba"></a>
1328
1348
 
1329
1349
  ## Timeout while waiting for handshake or handshake errors
1330
1350
 
@@ -1333,7 +1353,7 @@ Some users have encountered the error 'Timeout while waiting for handshake' or '
1333
1353
  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.
1334
1354
 
1335
1355
 
1336
- <a id="org1116140"></a>
1356
+ <a id="orgec6bc5b"></a>
1337
1357
 
1338
1358
  ## How can I limit upload/download speed
1339
1359
 
@@ -1373,7 +1393,7 @@ try {
1373
1393
  ```
1374
1394
 
1375
1395
 
1376
- <a id="orgefe495c"></a>
1396
+ <a id="org222771b"></a>
1377
1397
 
1378
1398
  ## Connection hangs or fails for larger files
1379
1399
 
@@ -1384,21 +1404,21 @@ A symptom of this issue is that you are able to upload small files, but uploadin
1384
1404
  For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
1385
1405
 
1386
1406
 
1387
- <a id="orgedcb385"></a>
1407
+ <a id="org64453a9"></a>
1388
1408
 
1389
1409
  ## Typescript definition file out of date
1390
1410
 
1391
1411
  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.
1392
1412
 
1393
1413
 
1394
- <a id="org3a23c04"></a>
1414
+ <a id="orge079610"></a>
1395
1415
 
1396
1416
  # Examples
1397
1417
 
1398
1418
  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.
1399
1419
 
1400
1420
 
1401
- <a id="org888858f"></a>
1421
+ <a id="orgedc17fd"></a>
1402
1422
 
1403
1423
  # Troubleshooting
1404
1424
 
@@ -1413,14 +1433,14 @@ Note also that in the repository there are two useful directories. The first is
1413
1433
  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.
1414
1434
 
1415
1435
 
1416
- <a id="orge820faa"></a>
1436
+ <a id="org7756613"></a>
1417
1437
 
1418
1438
  ## Common Errors
1419
1439
 
1420
1440
  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.
1421
1441
 
1422
1442
 
1423
- <a id="org52515e7"></a>
1443
+ <a id="orgdf44163"></a>
1424
1444
 
1425
1445
  ### Not returning the promise in a `then()` block
1426
1446
 
@@ -1457,7 +1477,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
1457
1477
  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.
1458
1478
 
1459
1479
 
1460
- <a id="org8e86c97"></a>
1480
+ <a id="org643899f"></a>
1461
1481
 
1462
1482
  ### Mixing Promise Chains and Async/Await
1463
1483
 
@@ -1517,7 +1537,7 @@ async function doSftp() {
1517
1537
  ```
1518
1538
 
1519
1539
 
1520
- <a id="org55f382c"></a>
1540
+ <a id="orgf1586cd"></a>
1521
1541
 
1522
1542
  ### Try/catch and Error Handlers
1523
1543
 
@@ -1528,14 +1548,14 @@ The basic problem is that the try/catch block will have completed execution befo
1528
1548
  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.
1529
1549
 
1530
1550
 
1531
- <a id="org76494fa"></a>
1551
+ <a id="org57723b9"></a>
1532
1552
 
1533
1553
  ### Server Differences
1534
1554
 
1535
1555
  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.
1536
1556
 
1537
1557
 
1538
- <a id="orgaedcad8"></a>
1558
+ <a id="org8f56d89"></a>
1539
1559
 
1540
1560
  ### Avoid Concurrent Operations
1541
1561
 
@@ -1544,7 +1564,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
1544
1564
  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.
1545
1565
 
1546
1566
 
1547
- <a id="orgf7b85d3"></a>
1567
+ <a id="org4c34184"></a>
1548
1568
 
1549
1569
  ## Debugging Support
1550
1570
 
@@ -1577,7 +1597,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
1577
1597
  ```
1578
1598
 
1579
1599
 
1580
- <a id="orgb243d19"></a>
1600
+ <a id="org63b74c6"></a>
1581
1601
 
1582
1602
  # Logging Issues
1583
1603
 
@@ -1594,7 +1614,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
1594
1614
  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.
1595
1615
 
1596
1616
 
1597
- <a id="org3d32e62"></a>
1617
+ <a id="org308be7d"></a>
1598
1618
 
1599
1619
  # Pull Requests
1600
1620
 
@@ -1611,7 +1631,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
1611
1631
  - **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
1612
1632
 
1613
1633
 
1614
- <a id="org99ba3a6"></a>
1634
+ <a id="org585b558"></a>
1615
1635
 
1616
1636
  # Contributors
1617
1637
 
@@ -1633,3 +1653,4 @@ Thanks to the following for their contributions -
1633
1653
  - **Witni Davis:** Contributed PR to fix put() RCE when using 'finish' rather than 'close' to resolve promise
1634
1654
  - **Maik Marschner:** Contributed fix for connect() not returning sftp object. Also included test to check for this regression in future.
1635
1655
  - **cakemasher:** Contributed fix for removeTempListeners().
1656
+ - **rhyswilliamsza:** Contributed RST-on-END handling fix
package/README.org CHANGED
@@ -12,10 +12,10 @@ be found on the [[https://github.com/mscdex/ssh2][SSH2]] project pages. As the s
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 *v12.0.0.
15
+ Current stable release is *v12.1.0.
16
16
 
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.
17
+ Code has been tested against Node versions 24.14.0. Node versions prior to v20.x are not
18
+ supported.
19
19
 
20
20
  If you find this module useful and you would like to support the on-going maintenance and
21
21
  support of users, please consider making a small [[https://square.link/u/gB2kSdkY?src=embed][donation]].
@@ -26,30 +26,54 @@ The big and breaking change in this version is the removal of the connection ret
26
26
  code. This module no longer supports automatic connection retries when establishing the
27
27
  initial connection. This functionality was removed for a number of reasons. The main
28
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
29
+ handling in particular and it was seldom useful. In most cases, if the connection failed
30
+ on the first attempt, it would also fail in all subsequent attempts and often resulted in
31
31
  just extending the time to failure as multiple retries were attempted.
32
32
 
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.
38
-
33
+ Another reason this functionality was removed is because if you really want it, it is
34
+ straight forward to create your own wrapper around the connect call which uses one
35
+ of the mnay promise retry packages available. It is much simpler to add a retry mechanism
36
+ for a specific application than it is to implement a flexible solution which supports all
37
+ possible combination of connection options, authentication methods, proxies etc.
38
+
39
+ ** Version 12.0.1 Changes
40
+
41
+ Bug Fixes
42
+
43
+ - Fix typo error in specification of generic error code in error handler. Typo was
44
+ causing an undefined symbol error.
45
+ - Fix bug in stream error handling. Read and write streams had an error handler defined
46
+ with 'once' rather than 'on'. This caused a bug when an API call caused more than one
47
+ error. For example, a network connectivity error would cause a stream timeout error
48
+ followed by a no response from remote host error when the error handler attempted to
49
+ cleanup and destroy the remote stream. The second error whould not be handled and
50
+ would bubble up to the parent node process, which would then exit with an unhandled
51
+ exception error.
52
+ - Added calls to _resetEventFlags() to finally clauses to ensure event flags are reset
53
+ when temp listeners are removed. Without this change, global listeners would not fire
54
+ when they should after their has been an earlier handled error in the last API call.
55
+ ** Version 12.1.0 Changes
56
+
57
+ The main change in this version is to address an issue with disconnect when accessing
58
+ Microsoft's SFTP server. Due to the way the Microsoft server implements closing of
59
+ connections, an exception is thrown even when there has been a legitimate request to end
60
+ the connection. The fix is to ignore this specific /error/ case. Thanks to @rhyswilliamsza
61
+ for the patch.
62
+
39
63
  ** Background
40
64
 
41
65
  In basic terms =ssh2-sftp-client= is a simple wrapper around the =ssh2= package which provides
42
- a promise base API for interacting with a remote SFTP server . The =ssh2= package provides
66
+ a promise based API for interacting with a remote SFTP server . The =ssh2= package provides
43
67
  an event based API for interacting with the ~ssh~ protocol. The ~ssh2-sftp-client~ package
44
68
  uses the ~sftp~ subsystem of this protocol to implement the basic operations typically
45
69
  associated with an ~sftp~ client.
46
70
 
47
- Wrapping an event based API with a promised based API comes with a number of
71
+ Wrapping an event based API with a promise based API comes with a number of
48
72
  challenges. In particular, efficiently and reliably managing events within the context of
49
- asynchrounous code execution. This package uses the following strategies;
73
+ asynchronous code execution. This package uses the following strategies;
50
74
 
51
75
  - All direct interactions with the ~ssh2~ API are wrapped in promise objects. When the
52
- API call succeeds, the assoiated promise will be sucessfully resolved. When an error occurs,
76
+ API call succeeds, the associated promise will be successfully resolved. When an error occurs,
53
77
  the promise is rejected.
54
78
 
55
79
  - An error can either be due to a low level network error, such as a lost connection
@@ -57,30 +81,32 @@ asynchrounous code execution. This package uses the following strategies;
57
81
  existing or not having the appropriate permissions for access.
58
82
 
59
83
  - Each of the available ~SftpClient~ methods wrap the method call inside a promise. In
60
- crwating eacvh promise, the class adds temporary event listenrs for the error, end
61
- and close events and links those liseners to the method's promise via it's ~reject()~
84
+ creating each promise, the class adds temporary event listeners for the error, end
85
+ and close events and links those liseners to the method's promise via its ~reject()~
62
86
  method.
63
87
 
64
- - If a promise is waiting to be fulfilled when either of the two types of errors
65
- occurs, the error will be communicated back to client code via a rejected promise.
88
+ - If a promise is waiting to be fulfilled when an error
89
+ occurs, the error will be communicated back to the client code via the rejected promise.
66
90
 
67
91
  - When the ~ssh2~ emitter raises an event outside the context of any promise, that event
68
92
  will be handled by global event handlers. By default, these event handlers will log
69
93
  the event and will invalidate any existing socket connection objects, preventing any
70
- further API calls until a new connection is extablished.
94
+ further API calls until a new connection is established.
71
95
 
72
96
  - The ~SftpClient~ class constructor supports an optoinal second argument which is an
73
- objedct whi8ch can have any of three properties representing event callbaqck
74
- funct5ions which will be executed for each of the possible events error, end and
75
- close.
97
+ object which can have any of the three properties `error`, `end`, and `close`. The values
98
+ associated with these properties are callback functions that will be run if an error
99
+ event with the same name as the property is raised and has not been handled by one of
100
+ the temporary event handlers. The error callback should have a single parameter, the
101
+ error raised by the event. The other callbacks have no arguments.
76
102
 
77
103
  The need for both global listeners and temporary promise listeners is because network end,
78
- close or error events can occur at any time, including in-betwseen API calls. During an
104
+ close or error events can occur at any time, including in-between API calls. During an
79
105
  API call, a promise is active and can be used to communicate event information back to the calling
80
- code via normal promise communication means i.e. async/await with try/catch or promise chain's then/catch
106
+ code via normal promise communication channels i.e. async/await with try/catch or promise chain's then/catch
81
107
  mechanism. However, outside API calls, no promise exists and there is no reliable
82
108
  mechanism to return error and other event information back to calling code. You cannot
83
- reliably use try/cdatch to catch errorsx thrown inside event listenrs as you lack control
109
+ reliably use try/cdatch to catch errors thrown inside event listenrs as you lack control
84
110
  over when the listener code runs. Your try/catch block can easily complete before the
85
111
  error is raised as there is no equivalent /await/ type functionality in this situation.
86
112
 
@@ -95,17 +121,17 @@ close or error event which is not handled by one of the temporary promise linked
95
121
  listeners.
96
122
 
97
123
  Version 11 of ~ssh2-sftp-client~ also changes the behaviour of the temporary promise linked
98
- /end/ and /close/ listeners. Prior to versioln 11, these listeners did not reject
124
+ /end/ and /close/ listeners. Prior to version 11, these listeners did not reject
99
125
  promises. They would only invalidate the underlying ~ssh~ connection object. Only the /error/
100
126
  listener would actually reject the associated promise. This was done because you cannot
101
127
  guarantee the order in which events are responded to.
102
128
 
103
129
  In most cases, events will occur in the order /error/, /end/ and then /close/. The error event
104
130
  would contain details about the cause of the error while the end and close events just
105
- communicvate that these events have been raised. The normal flow would be
131
+ communicate that these events have been raised. The normal flow would be
106
132
 
107
133
  - Error event occurs including error cause description. Listener catches event,
108
- creates an error object and calls the associated promises reject function to reject
134
+ creates an error object and calls the associated promise's reject function to reject
109
135
  the promise. The calling process receives a rejected promise object.
110
136
 
111
137
  - End event occurs. The end listener catches the event and marks the connection object
@@ -114,11 +140,11 @@ communicvate that these events have been raised. The normal flow would be
114
140
  listener and you can only call one promise resolution function.
115
141
 
116
142
  - Close event occurs. This event means the socket connection has been closed. The
117
- listener will ensure any connection information has been invalidated. Aga8in, ther
143
+ listener will ensure any connection information has been invalidated. Again, there
118
144
  is no need to call the reject method of the associated promise as it has already
119
145
  been called by the error listener.
120
146
 
121
- In some cases, no ende event is raised and you only get an error event followed by a close
147
+ In some cases, no end event is raised and you only get an error event followed by a close
122
148
  event. In versions of ~ssh2-sftp-client~ prior to version 11, neither the end or the close
123
149
  listeners attempted to call the reject method of the associated promise. It was assumed
124
150
  that all sftp servers would raise an error event whenever a connection was unexpectedly
@@ -131,7 +157,7 @@ call gets rejected with a timeout error aftrer a significant delay.
131
157
  In order to handle the possible hanging issue in version 11, the temporary promise linked
132
158
  end and close listeners have been updated to always call the promise's reject function if
133
159
  they fire. While this works, it can cause a minor issue. As wse cannot gurantee the order
134
- in which events are resonded to by listeners, it is possible that either the end or close
160
+ in which events are responded to by listeners, it is possible that either the end or close
135
161
  listener may be executed before the error listener. When this occurs, the promise is
136
162
  rejected, but the only information wse have at that point is that the promise wsas reject
137
163
  due to either an end or close event. We don't yet have any details regarding what error
@@ -2018,5 +2044,6 @@ Thanks to the following for their contributions -
2018
2044
  - Maik Marschner :: Contributed fix for connect() not returning sftp object.
2019
2045
  Also included test to check for this regression in future.
2020
2046
  - cakemasher :: Contributed fix for removeTempListeners().
2021
-
2047
+ - rhyswilliamsza :: Contributed RST-on-END handling fix
2048
+
2022
2049
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ssh2-sftp-client",
3
- "version": "12.0.0",
3
+ "version": "12.1.0",
4
4
  "description": "ssh2 sftp client for node",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -38,7 +38,7 @@
38
38
  "chai-as-promised": "^7.1.2",
39
39
  "chai-subset": "^1.6.0",
40
40
  "checksum": "^1.0.0",
41
- "dotenv": "^16.0.0",
41
+ "dotenv": "^17.3.1",
42
42
  "eslint": "^9.6.0",
43
43
  "eslint-config-prettier": "^9.0.0",
44
44
  "eslint-plugin-mocha": "^10.2.0",
package/src/index.js CHANGED
@@ -25,7 +25,7 @@ class SftpClient {
25
25
  close: () => console.log('Global close listener: close event raised'),
26
26
  },
27
27
  ) {
28
- this.version = '12.0.0';
28
+ this.version = '12.0.1';
29
29
  this.client = new Client();
30
30
  this.sftp = undefined;
31
31
  this.clientName = clientName;
@@ -169,6 +169,7 @@ class SftpClient {
169
169
  }).finally(() => {
170
170
  this.removeListener('ready', doReady);
171
171
  removeTempListeners(this, listeners, 'getConnection');
172
+ this._resetEventFlags();
172
173
  });
173
174
  }
174
175
 
@@ -203,6 +204,7 @@ class SftpClient {
203
204
  }).finally(() => {
204
205
  if (addListeners) {
205
206
  removeTempListeners(this, listeners, 'realPath');
207
+ this._resetEventFlags();
206
208
  }
207
209
  });
208
210
  }
@@ -267,6 +269,7 @@ class SftpClient {
267
269
  }).finally(() => {
268
270
  if (addListeners) {
269
271
  removeTempListeners(this, listeners, '_xstat');
272
+ this._resetEventFlags();
270
273
  }
271
274
  });
272
275
  }
@@ -397,6 +400,7 @@ class SftpClient {
397
400
  }).finally(() => {
398
401
  if (addListeners) {
399
402
  removeTempListeners(this, listeners, 'list');
403
+ this._resetEventFlags();
400
404
  }
401
405
  });
402
406
  }
@@ -437,7 +441,7 @@ class SftpClient {
437
441
  pipeOptions: { ...options?.pipeOptions, end: true },
438
442
  };
439
443
  rdr = this.sftp.createReadStream(remotePath, options.readStreamOptions);
440
- rdr.once('error', (err) => {
444
+ rdr.on('error', (err) => {
441
445
  if (dst && typeof dst === 'string' && wtr && !wtr.destroyed) {
442
446
  wtr.destroy();
443
447
  }
@@ -445,13 +449,11 @@ class SftpClient {
445
449
  });
446
450
  if (dst === undefined) {
447
451
  // no dst specified, return buffer of data
448
- this.debugMsg('get resolving with buffer of data');
449
452
  wtr = concat((buff) => {
450
453
  resolve(buff);
451
454
  });
452
455
  } else if (typeof dst === 'string') {
453
456
  // dst local file path
454
- this.debugMsg(`get called with file path destination ${dst}`);
455
457
  const localCheck = haveLocalCreate(dst);
456
458
  if (localCheck.status) {
457
459
  wtr = fs.createWriteStream(dst, options.writeStreamOptions);
@@ -465,10 +467,9 @@ class SftpClient {
465
467
  );
466
468
  }
467
469
  } else {
468
- this.debugMsg('get called with stream destination');
469
470
  wtr = dst;
470
471
  }
471
- wtr.once('error', (err) => {
472
+ wtr.on('error', (err) => {
472
473
  reject(
473
474
  this.fmtError(
474
475
  `${err.message} ${typeof dst === 'string' ? dst : '<stream>'}`,
@@ -492,6 +493,7 @@ class SftpClient {
492
493
  }
493
494
  if (addListeners) {
494
495
  removeTempListeners(this, listeners, 'get');
496
+ this._resetEventFlags();
495
497
  }
496
498
  });
497
499
  }
@@ -527,6 +529,7 @@ class SftpClient {
527
529
  }).finally(() => {
528
530
  if (addListeners) {
529
531
  removeTempListeners(this, listeners, '_fastGet');
532
+ this._resetEventFlags();
530
533
  }
531
534
  });
532
535
  }
@@ -593,6 +596,7 @@ class SftpClient {
593
596
  }).finally(() => {
594
597
  if (addListeners) {
595
598
  removeTempListeners(this, listeners, '_fastPut');
599
+ this._resetEventFlags();
596
600
  }
597
601
  });
598
602
  }
@@ -649,7 +653,7 @@ class SftpClient {
649
653
  };
650
654
  if (haveConnection(this, '_put', reject)) {
651
655
  wtr = this.sftp.createWriteStream(rPath, opts.writeStreamOptions);
652
- wtr.once('error', (err) => {
656
+ wtr.on('error', (err) => {
653
657
  if (typeof lPath === 'string' && rdr && !rdr.destroyed) {
654
658
  rdr.destroy();
655
659
  }
@@ -665,17 +669,14 @@ class SftpClient {
665
669
  resolve(`Uploaded data stream to ${rPath}`);
666
670
  });
667
671
  if (lPath instanceof Buffer) {
668
- this.debugMsg('put source is a buffer');
669
672
  wtr.end(lPath);
670
673
  } else {
671
674
  if (typeof lPath === 'string') {
672
- this.debugMsg('put source is string path');
673
675
  rdr = fs.createReadStream(lPath, opts.readStreamOptions);
674
676
  } else {
675
- this.debugMsg('put source is a stream');
676
677
  rdr = lPath;
677
678
  }
678
- rdr.once('error', (err) => {
679
+ rdr.on('error', (err) => {
679
680
  reject(
680
681
  this.fmtError(
681
682
  `Read stream error: ${err.message} ${
@@ -695,6 +696,7 @@ class SftpClient {
695
696
  }
696
697
  if (addListeners) {
697
698
  removeTempListeners(this, listeners, '_put');
699
+ this._resetEventFlags();
698
700
  }
699
701
  });
700
702
  }
@@ -750,6 +752,7 @@ class SftpClient {
750
752
  }).finally(() => {
751
753
  if (addListeners) {
752
754
  removeTempListeners(this, listeners, '_append');
755
+ this._resetEventFlags();
753
756
  }
754
757
  });
755
758
  }
@@ -821,6 +824,7 @@ class SftpClient {
821
824
  }).finally(() => {
822
825
  if (addListeners) {
823
826
  removeTempListeners(this, listeners, '_doMkdir');
827
+ this._resetEventFlags();
824
828
  }
825
829
  });
826
830
  }
@@ -894,6 +898,7 @@ class SftpClient {
894
898
  });
895
899
  }).finally(() => {
896
900
  removeTempListeners(this, listeners, '_rmdir');
901
+ this._resetEventFlags();
897
902
  });
898
903
  };
899
904
 
@@ -983,6 +988,7 @@ class SftpClient {
983
988
  }).finally(() => {
984
989
  if (addListeners) {
985
990
  removeTempListeners(this, listeners, 'delete');
991
+ this._resetEventFlags();
986
992
  }
987
993
  });
988
994
  }
@@ -1021,6 +1027,7 @@ class SftpClient {
1021
1027
  }).finally(() => {
1022
1028
  if (addListeners) {
1023
1029
  removeTempListeners(this, listeners, 'rename');
1030
+ this._resetEventFlags();
1024
1031
  }
1025
1032
  });
1026
1033
  }
@@ -1059,6 +1066,7 @@ class SftpClient {
1059
1066
  }
1060
1067
  }).finally(() => {
1061
1068
  removeTempListeners(this, listeners, 'posixRename');
1069
+ this._resetEventFlags();
1062
1070
  });
1063
1071
  }
1064
1072
 
@@ -1090,6 +1098,7 @@ class SftpClient {
1090
1098
  }).finally(() => {
1091
1099
  if (addListeners) {
1092
1100
  removeTempListeners(this, listeners, 'chmod');
1101
+ this._resetEventFlags();
1093
1102
  }
1094
1103
  });
1095
1104
  }
@@ -1175,6 +1184,7 @@ class SftpClient {
1175
1184
  throw this.fmtError(`${e.message} ${srcDir} to ${dstDir}`, 'uploadFiles', e.code);
1176
1185
  } finally {
1177
1186
  removeTempListeners(this, listeners, uploadFiles);
1187
+ this._resetEventFlags();
1178
1188
  }
1179
1189
  };
1180
1190
 
@@ -1300,6 +1310,7 @@ class SftpClient {
1300
1310
  );
1301
1311
  } finally {
1302
1312
  removeTempListeners(this, listeners, 'downloadFiles');
1313
+ this._resetEventFlags();
1303
1314
  }
1304
1315
  };
1305
1316
 
@@ -1347,6 +1358,7 @@ class SftpClient {
1347
1358
  throw err.custom ? err : this.fmtError(err.message, 'createReadStream', err.code);
1348
1359
  } finally {
1349
1360
  removeTempListeners(this, listeners, 'createReadStream');
1361
+ this._resetEventFlags();
1350
1362
  }
1351
1363
  }
1352
1364
 
@@ -1372,6 +1384,7 @@ class SftpClient {
1372
1384
  throw err.custom ? err : this.fmtError(err.message, 'createWriteStream', err.code);
1373
1385
  } finally {
1374
1386
  removeTempListeners(this, listeners, 'createWriteStream');
1387
+ this._resetEventFlags();
1375
1388
  }
1376
1389
  }
1377
1390
 
@@ -1435,6 +1448,7 @@ class SftpClient {
1435
1448
  throw err.custom ? err : this.fmtError(err, 'rcopy');
1436
1449
  } finally {
1437
1450
  removeTempListeners(this, listeners, 'rcopy');
1451
+ this._resetEventFlags();
1438
1452
  }
1439
1453
  }
1440
1454
  /**
@@ -1466,6 +1480,7 @@ class SftpClient {
1466
1480
  }).finally(() => {
1467
1481
  removeTempListeners(this, listeners, 'end');
1468
1482
  this.removeListener('close', endCloseHandler);
1483
+ this._resetEventFlags();
1469
1484
  });
1470
1485
  }
1471
1486
  }
package/src/utils.js CHANGED
@@ -64,6 +64,11 @@ function errorListener(client, name, reject) {
64
64
  client.debugMsg(`${name} errorListener - ignoring handled error ${err.message}`);
65
65
  return;
66
66
  }
67
+ // ignore ECONNRESET if end() has been called, as we can be confident that a reset connection is definitely dead
68
+ if (name === 'end' && client.endCalled && err.code === 'ECONNRESET') {
69
+ client.debugMsg(`${name} errorListener - ignoring ${err.message} on end`);
70
+ return;
71
+ }
67
72
  client.debugMsg(`${name} errorListener - handling error ${err.message}`);
68
73
  client.errorHandled = true;
69
74
  const newError = new Error(`${name}: ${err.message}`);
@@ -88,7 +93,7 @@ function endListener(client, name, reject) {
88
93
  client.endHandled = true;
89
94
  client.debugMsg(`${name} endListener - handling unexpected end event`);
90
95
  const newError = new Error(`${name}: Unexpected end event`);
91
- newError.code = errorCode.ERR_GENERIC_CLIENT;
96
+ newError.code = errorCode.generic;
92
97
  if (reject) {
93
98
  reject(newError);
94
99
  } else {
@@ -109,7 +114,7 @@ function closeListener(client, name, reject) {
109
114
  client.closeHandled = true;
110
115
  client.debugMsg(`${name} closeListener - handling unexpected close event`);
111
116
  const newError = new Error(`${name}: Unexpected close event`);
112
- newError.code = errorCode.ERR_GENERIC_CLIENT;
117
+ newError.code = errorCode.generic;
113
118
  if (reject) {
114
119
  reject(newError);
115
120
  } else {