ssh2-sftp-client 12.0.1 → 12.1.1

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,69 +1,71 @@
1
- - [Overview](#org4015600)
2
- - [Version 12.0.0 Changes](#org9ba2cfa)
3
- - [Version 12.0.1 Changes](#org658a511)
4
- - [Background](#org4d07682)
5
- - [Installation](#orgb1c417b)
6
- - [Basic Usage](#orgbddd3fd)
7
- - [Documentation](#org8c374be)
8
- - [Specifying Paths](#org95c6689)
9
- - [Methods](#org8437ec6)
10
- - [new SftpClient(name, callbacks) ===> SFTP client object](#org210a6e7)
11
- - [connect(config) ===> SFTP object](#org9c48be5)
12
- - [list(path, filter) ==> Array[object]](#org190df3b)
13
- - [exists(path) ==> boolean](#org7d245b2)
14
- - [stat(path) ==> object](#orga6a58bd)
15
- - [get(path, dst, options) ==> String|Stream|Buffer](#orgc3ea293)
16
- - [fastGet(remotePath, localPath, options) ===> string](#org6261e44)
17
- - [put(src, remotePath, options) ==> string](#org6c23ce2)
18
- - [fastPut(localPath, remotePath, options) ==> string](#orgfda72d4)
19
- - [append(input, remotePath, options) ==> string](#org6c67ab9)
20
- - [mkdir(path, recursive) ==> string](#org99a7d1c)
21
- - [rmdir(path, recursive) ==> string](#org31ab7d2)
22
- - [delete(path, noErrorOK) ==> string](#org3a4a52a)
23
- - [rename(fromPath, toPath) ==> string](#org9fbf1b2)
24
- - [posixRename(fromPath, toPath) ==> string](#org0be5342)
25
- - [chmod(path, mode) ==> string](#org9227f61)
26
- - [realPath(path) ===> string](#orgdbe4247)
27
- - [cwd() ==> string](#orge856d52)
28
- - [uploadDir(srcDir, dstDir, options) ==> string](#org9337523)
29
- - [downloadDir(srcDir, dstDir, options) ==> string](#org30526ff)
30
- - [createReadStream(remotePath, options)) ==> stream object](#org98dacc6)
31
- - [createWriteStream(remotePath, options) ==> stream object](#orgf250341)
32
- - [rcopy(srcPath, dstPath) ==> string](#org16daa1a)
33
- - [end() ==> boolean](#orgb329f2e)
34
- - [Add and Remove Listeners](#org80dffcb)
35
- - [Platform Quirks & Warnings](#org6e83e30)
36
- - [Server Capabilities](#org75bad4f)
37
- - [Issues with `fastPut()` and `fastGet()` Methods](#org3fe5a31)
38
- - [Promises, Events & Managing Exceptions](#org9fd1871)
39
- - [Adding Custom Handlers](#org256ad96)
40
- - [Windows Based Servers](#org1eef7b1)
41
- - [Don't Re-use SftpClient Objects](#org37ac59e)
42
- - [FAQ](#orgc01e9f3)
43
- - [Remote server drops connections with only an end event](#orgcbb84bd)
44
- - [How can I pass writeable stream as dst for get method?](#orgb7ba758)
45
- - [How can I upload files without having to specify a password?](#orgf9d915c)
46
- - [How can I connect through a Socks Proxy](#orgc977636)
47
- - [Timeout while waiting for handshake or handshake errors](#org9caf1fc)
48
- - [How can I limit upload/download speed](#orgc99cfed)
49
- - [Connection hangs or fails for larger files](#orgff1cf60)
50
- - [Typescript definition file out of date](#org312104a)
51
- - [Examples](#org409c3ef)
52
- - [Troubleshooting](#orge118375)
53
- - [Common Errors](#org39ed676)
54
- - [Not returning the promise in a `then()` block](#orge4fdace)
55
- - [Mixing Promise Chains and Async/Await](#org613b5f0)
56
- - [Try/catch and Error Handlers](#org3df8f9d)
57
- - [Server Differences](#org295cfa3)
58
- - [Avoid Concurrent Operations](#org5ee2ce1)
59
- - [Debugging Support](#orgec0b62a)
60
- - [Logging Issues](#org41f81c7)
61
- - [Pull Requests](#orgd153e6d)
62
- - [Contributors](#org267dc2a)
63
-
64
-
65
-
66
- <a id="org4015600"></a>
1
+ - [Overview](#org6987c45)
2
+ - [Version 12.0.0 Changes](#org02987aa)
3
+ - [Version 12.0.1 Changes](#org7468421)
4
+ - [Version 12.1.0 Changes](#orge1b539d)
5
+ - [Version 12.1.1 Changes](#orged5d608)
6
+ - [Background](#orge45f641)
7
+ - [Installation](#org660793d)
8
+ - [Basic Usage](#org25ec48d)
9
+ - [Documentation](#org70c6bcd)
10
+ - [Specifying Paths](#org6e11821)
11
+ - [Methods](#orgac96e89)
12
+ - [new SftpClient(name, callbacks) ===> SFTP client object](#org5a43218)
13
+ - [connect(config) ===> SFTP object](#org2204c82)
14
+ - [list(path, filter) ==> Array[object]](#org6412c07)
15
+ - [exists(path) ==> boolean](#org538f718)
16
+ - [stat(path) ==> object](#org49ddb80)
17
+ - [get(path, dst, options) ==> String|Stream|Buffer](#org7a94a2b)
18
+ - [fastGet(remotePath, localPath, options) ===> string](#orgc9d1d0f)
19
+ - [put(src, remotePath, options) ==> string](#orgd1b5df0)
20
+ - [fastPut(localPath, remotePath, options) ==> string](#orga70daaf)
21
+ - [append(input, remotePath, options) ==> string](#org44bf28c)
22
+ - [mkdir(path, recursive) ==> string](#org79f8f19)
23
+ - [rmdir(path, recursive) ==> string](#org01678bc)
24
+ - [delete(path, noErrorOK) ==> string](#org7aa4867)
25
+ - [rename(fromPath, toPath) ==> string](#orgfeb9fe0)
26
+ - [posixRename(fromPath, toPath) ==> string](#orgcde5734)
27
+ - [chmod(path, mode) ==> string](#org9a2be14)
28
+ - [realPath(path) ===> string](#orgfa8caf6)
29
+ - [cwd() ==> string](#org712492d)
30
+ - [uploadDir(srcDir, dstDir, options) ==> string](#org4c58675)
31
+ - [downloadDir(srcDir, dstDir, options) ==> string](#orgd4cec94)
32
+ - [createReadStream(remotePath, options)) ==> stream object](#orgaf8232d)
33
+ - [createWriteStream(remotePath, options) ==> stream object](#orgb90e976)
34
+ - [rcopy(srcPath, dstPath) ==> string](#org8da7dd9)
35
+ - [end() ==> boolean](#orgefc8914)
36
+ - [Add and Remove Listeners](#org9bc7c20)
37
+ - [Platform Quirks & Warnings](#org141ea6c)
38
+ - [Server Capabilities](#org02bda70)
39
+ - [Issues with `fastPut()` and `fastGet()` Methods](#org7039cad)
40
+ - [Promises, Events & Managing Exceptions](#org6f0f2d3)
41
+ - [Adding Custom Handlers](#orga3bd20f)
42
+ - [Windows Based Servers](#orgd17159d)
43
+ - [Don't Re-use SftpClient Objects](#orgbabcbec)
44
+ - [FAQ](#orge56389f)
45
+ - [Remote server drops connections with only an end event](#orga1e4f59)
46
+ - [How can I pass writeable stream as dst for get method?](#org6bd3606)
47
+ - [How can I upload files without having to specify a password?](#orgec27c87)
48
+ - [How can I connect through a Socks Proxy](#org1f7a40b)
49
+ - [Timeout while waiting for handshake or handshake errors](#orge77b90c)
50
+ - [How can I limit upload/download speed](#org0a65ef0)
51
+ - [Connection hangs or fails for larger files](#org042cb9c)
52
+ - [Typescript definition file out of date](#orgd1fa8b1)
53
+ - [Examples](#org4a3c46b)
54
+ - [Troubleshooting](#org1c383d5)
55
+ - [Common Errors](#org3a2df1c)
56
+ - [Not returning the promise in a `then()` block](#orgd9b95f7)
57
+ - [Mixing Promise Chains and Async/Await](#orgd62dd8c)
58
+ - [Try/catch and Error Handlers](#orgf1cdec9)
59
+ - [Server Differences](#orgf717a07)
60
+ - [Avoid Concurrent Operations](#orge7917a5)
61
+ - [Debugging Support](#org5aa5efa)
62
+ - [Logging Issues](#org0475065)
63
+ - [Pull Requests](#org954dbfa)
64
+ - [Contributors](#orgaa3b2aa)
65
+
66
+
67
+
68
+ <a id="org6987c45"></a>
67
69
 
68
70
  # Overview
69
71
 
@@ -71,14 +73,14 @@ This package provides the class SftpClient, an SFTP client for node.js. It is a
71
73
 
72
74
  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.
73
75
 
74
- Current stable release is \*v12.0.1.
76
+ Current stable release is \*v12.1.1.
75
77
 
76
- Code has been tested against Node versions 20.19.2, 22.16.0, 23.11.1 and 24.2.0. Node versions prior to v20.x are not supported.
78
+ Code has been tested against Node versions 24.14.0. Node versions prior to v20.x are not supported.
77
79
 
78
80
  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
81
 
80
82
 
81
- <a id="org9ba2cfa"></a>
83
+ <a id="org02987aa"></a>
82
84
 
83
85
  ## Version 12.0.0 Changes
84
86
 
@@ -87,7 +89,7 @@ The big and breaking change in this version is the removal of the connection ret
87
89
  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.
88
90
 
89
91
 
90
- <a id="org658a511"></a>
92
+ <a id="org7468421"></a>
91
93
 
92
94
  ## Version 12.0.1 Changes
93
95
 
@@ -98,7 +100,23 @@ Bug Fixes
98
100
  - 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.
99
101
 
100
102
 
101
- <a id="org4d07682"></a>
103
+ <a id="orge1b539d"></a>
104
+
105
+ ## Version 12.1.0 Changes
106
+
107
+ 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.
108
+
109
+
110
+ <a id="orged5d608"></a>
111
+
112
+ ## Version 12.1.1 Changes
113
+
114
+ Just a minor bug fix for the error handlers. Ensure that the sftp object is invalidated after any ECONNRESET to ensure any further attempts to use the sftp connection fail immediately. Previously, there was an edge case where a connect abruptly closed by the remote server could result in either a long timeout error or a hanging promise which never fulfills or rejects. Now the internal sftp object is cleared whenever a ECONNRESET event occurs as this event indicates the remote server has closed the connection.
115
+
116
+ Note that as ECONNRESET events are triggered by the remote server closing the connection, there is little error information the client can report. It is generally necessary to check the remote SFTP server logs in order to determine why the remote server closed the connection.
117
+
118
+
119
+ <a id="orge45f641"></a>
102
120
 
103
121
  ## Background
104
122
 
@@ -116,7 +134,7 @@ Wrapping an event based API with a promise based API comes with a number of chal
116
134
 
117
135
  - 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.
118
136
 
119
- - The `SftpClient` class constructor supports an optoinal second argument which is an objedct 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?he 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.
137
+ - 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.
120
138
 
121
139
  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.
122
140
 
@@ -137,7 +155,7 @@ In some cases, no end event is raised and you only get an error event followed b
137
155
  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.
138
156
 
139
157
 
140
- <a id="orgb1c417b"></a>
158
+ <a id="org660793d"></a>
141
159
 
142
160
  # Installation
143
161
 
@@ -146,7 +164,7 @@ npm install ssh2-sftp-client
146
164
  ```
147
165
 
148
166
 
149
- <a id="orgbddd3fd"></a>
167
+ <a id="org25ec48d"></a>
150
168
 
151
169
  # Basic Usage
152
170
 
@@ -169,7 +187,7 @@ sftp.connect({
169
187
  ```
170
188
 
171
189
 
172
- <a id="org8c374be"></a>
190
+ <a id="org70c6bcd"></a>
173
191
 
174
192
  # Documentation
175
193
 
@@ -180,7 +198,7 @@ All the methods will return a Promise, except for `on(), ~removeListener()`, `cr
180
198
  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.
181
199
 
182
200
 
183
- <a id="org95c6689"></a>
201
+ <a id="org6e11821"></a>
184
202
 
185
203
  ## Specifying Paths
186
204
 
@@ -213,12 +231,12 @@ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
213
231
  This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
214
232
 
215
233
 
216
- <a id="org8437ec6"></a>
234
+ <a id="orgac96e89"></a>
217
235
 
218
236
  ## Methods
219
237
 
220
238
 
221
- <a id="org210a6e7"></a>
239
+ <a id="org5a43218"></a>
222
240
 
223
241
  ### new SftpClient(name, callbacks) ===> SFTP client object
224
242
 
@@ -258,7 +276,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
258
276
  ```
259
277
 
260
278
 
261
- <a id="org9c48be5"></a>
279
+ <a id="org2204c82"></a>
262
280
 
263
281
  ### connect(config) ===> SFTP object
264
282
 
@@ -322,7 +340,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
322
340
  ```
323
341
 
324
342
 
325
- <a id="org190df3b"></a>
343
+ <a id="org6412c07"></a>
326
344
 
327
345
  ### list(path, filter) ==> Array[object]
328
346
 
@@ -383,7 +401,7 @@ Retrieves a directory listing. This method returns a Promise, which once realise
383
401
  ```
384
402
 
385
403
 
386
- <a id="org7d245b2"></a>
404
+ <a id="org538f718"></a>
387
405
 
388
406
  ### exists(path) ==> boolean
389
407
 
@@ -419,7 +437,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
419
437
  ```
420
438
 
421
439
 
422
- <a id="orga6a58bd"></a>
440
+ <a id="org49ddb80"></a>
423
441
 
424
442
  ### stat(path) ==> object
425
443
 
@@ -470,7 +488,7 @@ Returns the attributes associated with the object pointed to by `path`.
470
488
  ```
471
489
 
472
490
 
473
- <a id="orgc3ea293"></a>
491
+ <a id="org7a94a2b"></a>
474
492
 
475
493
  ### get(path, dst, options) ==> String|Stream|Buffer
476
494
 
@@ -526,7 +544,7 @@ In general, if you're going to pass in a string as the destination, you are bett
526
544
  - **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'.
527
545
 
528
546
 
529
- <a id="org6261e44"></a>
547
+ <a id="orgc9d1d0f"></a>
530
548
 
531
549
  ### fastGet(remotePath, localPath, options) ===> string
532
550
 
@@ -571,7 +589,7 @@ Bottom line, when it works, it tends to work reliably. However, for many servers
571
589
  ```
572
590
 
573
591
 
574
- <a id="org6c23ce2"></a>
592
+ <a id="orgd1b5df0"></a>
575
593
 
576
594
  ### put(src, remotePath, options) ==> string
577
595
 
@@ -621,7 +639,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
621
639
  - **Tip:** If the src argument is a path string, consider just using `fastPut()`.
622
640
 
623
641
 
624
- <a id="orgfda72d4"></a>
642
+ <a id="orga70daaf"></a>
625
643
 
626
644
  ### fastPut(localPath, remotePath, options) ==> string
627
645
 
@@ -667,7 +685,7 @@ Bottom line, when it works, it tends to work well. However, when it doesn't work
667
685
  ```
668
686
 
669
687
 
670
- <a id="org6c67ab9"></a>
688
+ <a id="org44bf28c"></a>
671
689
 
672
690
  ### append(input, remotePath, options) ==> string
673
691
 
@@ -711,7 +729,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
711
729
  ```
712
730
 
713
731
 
714
- <a id="org99a7d1c"></a>
732
+ <a id="org79f8f19"></a>
715
733
 
716
734
  ### mkdir(path, recursive) ==> string
717
735
 
@@ -739,7 +757,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
739
757
  ```
740
758
 
741
759
 
742
- <a id="org31ab7d2"></a>
760
+ <a id="org01678bc"></a>
743
761
 
744
762
  ### rmdir(path, recursive) ==> string
745
763
 
@@ -769,7 +787,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
769
787
  ```
770
788
 
771
789
 
772
- <a id="org3a4a52a"></a>
790
+ <a id="org7aa4867"></a>
773
791
 
774
792
  ### delete(path, noErrorOK) ==> string
775
793
 
@@ -798,7 +816,7 @@ Delete a file on the remote server.
798
816
  ```
799
817
 
800
818
 
801
- <a id="org9fbf1b2"></a>
819
+ <a id="orgfeb9fe0"></a>
802
820
 
803
821
  ### rename(fromPath, toPath) ==> string
804
822
 
@@ -827,7 +845,7 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
827
845
  ```
828
846
 
829
847
 
830
- <a id="org0be5342"></a>
848
+ <a id="orgcde5734"></a>
831
849
 
832
850
  ### posixRename(fromPath, toPath) ==> string
833
851
 
@@ -854,7 +872,7 @@ client.connect(config)
854
872
  ```
855
873
 
856
874
 
857
- <a id="org9227f61"></a>
875
+ <a id="org9a2be14"></a>
858
876
 
859
877
  ### chmod(path, mode) ==> string
860
878
 
@@ -883,7 +901,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
883
901
  ```
884
902
 
885
903
 
886
- <a id="orgdbe4247"></a>
904
+ <a id="orgfa8caf6"></a>
887
905
 
888
906
  ### realPath(path) ===> string
889
907
 
@@ -894,14 +912,14 @@ Converts a relative path to an absolute path on the remote server. This method i
894
912
  - **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
895
913
 
896
914
 
897
- <a id="orge856d52"></a>
915
+ <a id="org712492d"></a>
898
916
 
899
917
  ### cwd() ==> string
900
918
 
901
919
  Returns what the server believes is the current remote working directory.
902
920
 
903
921
 
904
- <a id="org9337523"></a>
922
+ <a id="org4c58675"></a>
905
923
 
906
924
  ### uploadDir(srcDir, dstDir, options) ==> string
907
925
 
@@ -970,7 +988,7 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
970
988
  ```
971
989
 
972
990
 
973
- <a id="org30526ff"></a>
991
+ <a id="orgd4cec94"></a>
974
992
 
975
993
  ### downloadDir(srcDir, dstDir, options) ==> string
976
994
 
@@ -1035,7 +1053,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
1035
1053
  ```
1036
1054
 
1037
1055
 
1038
- <a id="org98dacc6"></a>
1056
+ <a id="orgaf8232d"></a>
1039
1057
 
1040
1058
  ### createReadStream(remotePath, options)) ==> stream object
1041
1059
 
@@ -1052,7 +1070,7 @@ Returns a read stream object which is attached to the remote file specified by t
1052
1070
  - **end:** Position to stop reading bytes (inclusive).
1053
1071
 
1054
1072
 
1055
- <a id="orgf250341"></a>
1073
+ <a id="orgb90e976"></a>
1056
1074
 
1057
1075
  ### createWriteStream(remotePath, options) ==> stream object
1058
1076
 
@@ -1067,7 +1085,7 @@ Returns a write stream object which is attached to the remote file specified in
1067
1085
  - **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
1068
1086
 
1069
1087
 
1070
- <a id="org16daa1a"></a>
1088
+ <a id="org8da7dd9"></a>
1071
1089
 
1072
1090
  ### rcopy(srcPath, dstPath) ==> string
1073
1091
 
@@ -1077,7 +1095,7 @@ Perform a remote file copy. The file identified by the `srcPath` argument will b
1077
1095
  - **dstPath:** Path to where the copy will be created specified as a string
1078
1096
 
1079
1097
 
1080
- <a id="orgb329f2e"></a>
1098
+ <a id="orgefc8914"></a>
1081
1099
 
1082
1100
  ### end() ==> boolean
1083
1101
 
@@ -1101,7 +1119,7 @@ Ends the current client session, releasing the client socket and associated reso
1101
1119
  ```
1102
1120
 
1103
1121
 
1104
- <a id="org80dffcb"></a>
1122
+ <a id="org9bc7c20"></a>
1105
1123
 
1106
1124
  ### Add and Remove Listeners
1107
1125
 
@@ -1122,12 +1140,12 @@ Although normally not required, you can add and remove custom listeners on the s
1122
1140
  Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
1123
1141
 
1124
1142
 
1125
- <a id="org6e83e30"></a>
1143
+ <a id="org141ea6c"></a>
1126
1144
 
1127
1145
  # Platform Quirks & Warnings
1128
1146
 
1129
1147
 
1130
- <a id="org75bad4f"></a>
1148
+ <a id="org02bda70"></a>
1131
1149
 
1132
1150
  ## Server Capabilities
1133
1151
 
@@ -1136,7 +1154,7 @@ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-
1136
1154
  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.
1137
1155
 
1138
1156
 
1139
- <a id="org3fe5a31"></a>
1157
+ <a id="org7039cad"></a>
1140
1158
 
1141
1159
  ## Issues with `fastPut()` and `fastGet()` Methods
1142
1160
 
@@ -1145,7 +1163,7 @@ The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SF
1145
1163
  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.
1146
1164
 
1147
1165
 
1148
- <a id="org9fd1871"></a>
1166
+ <a id="org6f0f2d3"></a>
1149
1167
 
1150
1168
  ## Promises, Events & Managing Exceptions
1151
1169
 
@@ -1164,14 +1182,14 @@ The other area where additional events are fired is during the end() call. To de
1164
1182
  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.
1165
1183
 
1166
1184
 
1167
- <a id="org256ad96"></a>
1185
+ <a id="orga3bd20f"></a>
1168
1186
 
1169
1187
  ### Adding Custom Handlers
1170
1188
 
1171
1189
  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.
1172
1190
 
1173
1191
 
1174
- <a id="org1eef7b1"></a>
1192
+ <a id="orgd17159d"></a>
1175
1193
 
1176
1194
  ## Windows Based Servers
1177
1195
 
@@ -1180,7 +1198,7 @@ It appears that when the sftp server is running on Windows, a *ECONNRESET* error
1180
1198
  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.
1181
1199
 
1182
1200
 
1183
- <a id="org37ac59e"></a>
1201
+ <a id="orgbabcbec"></a>
1184
1202
 
1185
1203
  ## Don't Re-use SftpClient Objects
1186
1204
 
@@ -1189,12 +1207,12 @@ Due to an issue with *ECONNRESET* error signals when connecting to Windows based
1189
1207
  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.
1190
1208
 
1191
1209
 
1192
- <a id="orgc01e9f3"></a>
1210
+ <a id="orge56389f"></a>
1193
1211
 
1194
1212
  # FAQ
1195
1213
 
1196
1214
 
1197
- <a id="orgcbb84bd"></a>
1215
+ <a id="orga1e4f59"></a>
1198
1216
 
1199
1217
  ## Remote server drops connections with only an end event
1200
1218
 
@@ -1205,7 +1223,7 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
1205
1223
  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.
1206
1224
 
1207
1225
 
1208
- <a id="orgb7ba758"></a>
1226
+ <a id="org6bd3606"></a>
1209
1227
 
1210
1228
  ## How can I pass writeable stream as dst for get method?
1211
1229
 
@@ -1264,7 +1282,7 @@ sftp
1264
1282
  ```
1265
1283
 
1266
1284
 
1267
- <a id="orgf9d915c"></a>
1285
+ <a id="orgec27c87"></a>
1268
1286
 
1269
1287
  ## How can I upload files without having to specify a password?
1270
1288
 
@@ -1299,7 +1317,7 @@ sftp.connect({
1299
1317
  ```
1300
1318
 
1301
1319
 
1302
- <a id="orgc977636"></a>
1320
+ <a id="org1f7a40b"></a>
1303
1321
 
1304
1322
  ## How can I connect through a Socks Proxy
1305
1323
 
@@ -1336,7 +1354,7 @@ client.connect({
1336
1354
  ```
1337
1355
 
1338
1356
 
1339
- <a id="org9caf1fc"></a>
1357
+ <a id="orge77b90c"></a>
1340
1358
 
1341
1359
  ## Timeout while waiting for handshake or handshake errors
1342
1360
 
@@ -1345,7 +1363,7 @@ Some users have encountered the error 'Timeout while waiting for handshake' or '
1345
1363
  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.
1346
1364
 
1347
1365
 
1348
- <a id="orgc99cfed"></a>
1366
+ <a id="org0a65ef0"></a>
1349
1367
 
1350
1368
  ## How can I limit upload/download speed
1351
1369
 
@@ -1385,7 +1403,7 @@ try {
1385
1403
  ```
1386
1404
 
1387
1405
 
1388
- <a id="orgff1cf60"></a>
1406
+ <a id="org042cb9c"></a>
1389
1407
 
1390
1408
  ## Connection hangs or fails for larger files
1391
1409
 
@@ -1396,21 +1414,21 @@ A symptom of this issue is that you are able to upload small files, but uploadin
1396
1414
  For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
1397
1415
 
1398
1416
 
1399
- <a id="org312104a"></a>
1417
+ <a id="orgd1fa8b1"></a>
1400
1418
 
1401
1419
  ## Typescript definition file out of date
1402
1420
 
1403
1421
  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.
1404
1422
 
1405
1423
 
1406
- <a id="org409c3ef"></a>
1424
+ <a id="org4a3c46b"></a>
1407
1425
 
1408
1426
  # Examples
1409
1427
 
1410
1428
  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.
1411
1429
 
1412
1430
 
1413
- <a id="orge118375"></a>
1431
+ <a id="org1c383d5"></a>
1414
1432
 
1415
1433
  # Troubleshooting
1416
1434
 
@@ -1425,14 +1443,14 @@ Note also that in the repository there are two useful directories. The first is
1425
1443
  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.
1426
1444
 
1427
1445
 
1428
- <a id="org39ed676"></a>
1446
+ <a id="org3a2df1c"></a>
1429
1447
 
1430
1448
  ## Common Errors
1431
1449
 
1432
1450
  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.
1433
1451
 
1434
1452
 
1435
- <a id="orge4fdace"></a>
1453
+ <a id="orgd9b95f7"></a>
1436
1454
 
1437
1455
  ### Not returning the promise in a `then()` block
1438
1456
 
@@ -1469,7 +1487,7 @@ Note the `return` statements. These ensure that the Promise returned by the clie
1469
1487
  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.
1470
1488
 
1471
1489
 
1472
- <a id="org613b5f0"></a>
1490
+ <a id="orgd62dd8c"></a>
1473
1491
 
1474
1492
  ### Mixing Promise Chains and Async/Await
1475
1493
 
@@ -1529,7 +1547,7 @@ async function doSftp() {
1529
1547
  ```
1530
1548
 
1531
1549
 
1532
- <a id="org3df8f9d"></a>
1550
+ <a id="orgf1cdec9"></a>
1533
1551
 
1534
1552
  ### Try/catch and Error Handlers
1535
1553
 
@@ -1540,14 +1558,14 @@ The basic problem is that the try/catch block will have completed execution befo
1540
1558
  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.
1541
1559
 
1542
1560
 
1543
- <a id="org295cfa3"></a>
1561
+ <a id="orgf717a07"></a>
1544
1562
 
1545
1563
  ### Server Differences
1546
1564
 
1547
1565
  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.
1548
1566
 
1549
1567
 
1550
- <a id="org5ee2ce1"></a>
1568
+ <a id="orge7917a5"></a>
1551
1569
 
1552
1570
  ### Avoid Concurrent Operations
1553
1571
 
@@ -1556,7 +1574,7 @@ Technically, SFTP should be able to perform multiple operations concurrently. As
1556
1574
  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.
1557
1575
 
1558
1576
 
1559
- <a id="orgec0b62a"></a>
1577
+ <a id="org5aa5efa"></a>
1560
1578
 
1561
1579
  ## Debugging Support
1562
1580
 
@@ -1589,7 +1607,7 @@ If you just want to see debug messages from `ssh2-sftp-client` and exclude debug
1589
1607
  ```
1590
1608
 
1591
1609
 
1592
- <a id="org41f81c7"></a>
1610
+ <a id="org0475065"></a>
1593
1611
 
1594
1612
  # Logging Issues
1595
1613
 
@@ -1606,7 +1624,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
1606
1624
  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.
1607
1625
 
1608
1626
 
1609
- <a id="orgd153e6d"></a>
1627
+ <a id="org954dbfa"></a>
1610
1628
 
1611
1629
  # Pull Requests
1612
1630
 
@@ -1623,7 +1641,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
1623
1641
  - **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
1624
1642
 
1625
1643
 
1626
- <a id="org267dc2a"></a>
1644
+ <a id="orgaa3b2aa"></a>
1627
1645
 
1628
1646
  # Contributors
1629
1647
 
@@ -1645,3 +1663,4 @@ Thanks to the following for their contributions -
1645
1663
  - **Witni Davis:** Contributed PR to fix put() RCE when using 'finish' rather than 'close' to resolve promise
1646
1664
  - **Maik Marschner:** Contributed fix for connect() not returning sftp object. Also included test to check for this regression in future.
1647
1665
  - **cakemasher:** Contributed fix for removeTempListeners().
1666
+ - **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.1.
15
+ Current stable release is *v12.1.1.
16
16
 
17
- Code has been tested against Node versions 20.19.2, 22.16.0, 23.11.1 and 24.2.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]].
@@ -52,7 +52,28 @@ possible combination of connection options, authentication methods, proxies etc.
52
52
  - Added calls to _resetEventFlags() to finally clauses to ensure event flags are reset
53
53
  when temp listeners are removed. Without this change, global listeners would not fire
54
54
  when they should after their has been an earlier handled error in the last API call.
55
-
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
+
63
+ ** Version 12.1.1 Changes
64
+
65
+ Just a minor bug fix for the error handlers. Ensure that the sftp object is invalidated
66
+ after any ECONNRESET to ensure any further attempts to use the sftp connection fail
67
+ immediately. Previously, there was an edge case where a connect abruptly closed by the
68
+ remote server could result in either a long timeout error or a hanging promise which never
69
+ fulfills or rejects. Now the internal sftp object is cleared whenever a ECONNRESET event
70
+ occurs as this event indicates the remote server has closed the connection.
71
+
72
+ Note that as ECONNRESET events are triggered by the remote server closing the connection,
73
+ there is little error information the client can report. It is generally necessary to
74
+ check the remote SFTP server logs in order to determine why the remote server closed the
75
+ connection.
76
+
56
77
  ** Background
57
78
 
58
79
  In basic terms =ssh2-sftp-client= is a simple wrapper around the =ssh2= package which provides
@@ -87,9 +108,9 @@ asynchronous code execution. This package uses the following strategies;
87
108
  further API calls until a new connection is established.
88
109
 
89
110
  - The ~SftpClient~ class constructor supports an optoinal second argument which is an
90
- objedct which can have any of the three properties error, end and close. The values
111
+ object which can have any of the three properties `error`, `end`, and `close`. The values
91
112
  associated with these properties are callback functions that will be run if an error
92
- event with?he same name as the property is raised and has not been handled by one of
113
+ event with the same name as the property is raised and has not been handled by one of
93
114
  the temporary event handlers. The error callback should have a single parameter, the
94
115
  error raised by the event. The other callbacks have no arguments.
95
116
 
@@ -2037,5 +2058,6 @@ Thanks to the following for their contributions -
2037
2058
  - Maik Marschner :: Contributed fix for connect() not returning sftp object.
2038
2059
  Also included test to check for this regression in future.
2039
2060
  - cakemasher :: Contributed fix for removeTempListeners().
2040
-
2061
+ - rhyswilliamsza :: Contributed RST-on-END handling fix
2062
+
2041
2063
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ssh2-sftp-client",
3
- "version": "12.0.1",
3
+ "version": "12.1.1",
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/utils.js CHANGED
@@ -64,6 +64,14 @@ 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
+ }
72
+ if (err.code === 'ECONNRESET') {
73
+ client.sftp = undefined;
74
+ }
67
75
  client.debugMsg(`${name} errorListener - handling error ${err.message}`);
68
76
  client.errorHandled = true;
69
77
  const newError = new Error(`${name}: ${err.message}`);