ssh2-sftp-client 5.2.0 → 5.2.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.
Files changed (3) hide show
  1. package/README.md +1043 -1643
  2. package/README.org +64 -59
  3. package/package.json +5 -5
package/README.md CHANGED
@@ -1,470 +1,266 @@
1
-
2
- # Table of Contents
3
-
4
- 1. [SSH2 SFTP Client](#org01ca044)
5
- 2. [Installation](#org2931228)
6
- 3. [Basic Usage](#orgaa4b035)
7
- 4. [Version 5.x](#org8896bb7)
8
- 1. [Breaking Changes in Version 5.x](#org7dd479a)
9
- 1. [Error Event Handling](#org5cb33ab)
10
- 2. [Technical Details](#org8e4dbfb)
11
- 2. [New Methods](#org948a6b7)
12
- 3. [Version 5.0.1](#org5cf9acc)
13
- 4. [Version 5.0.2](#org8bbba9f)
14
- 5. [Version 5.1.0](#org60ab99e)
15
- 6. [Version 5.1.1](#org55b82ed)
16
- 7. [Version 5.1.2](#orgf04af40)
17
- 8. [Version 5.1.3](#org27af3a9)
18
- 9. [Version 5.2.0](#org0a530e1)
19
- 5. [Documentation](#org87db731)
20
- 1. [Specifying Paths](#orgdb75138)
21
- 2. [Methods](#org6d72402)
22
- 1. [new SftpClient(name) ===> SFTP client object](#orgc685aec)
23
- 2. [connect(config) ===> SFTPstream](#orgfc82313)
24
- 3. [list(path, pattern) ==> Array[object]](#org423c72d)
25
- 4. [exists(path) ==> boolean](#orgea8fbfa)
26
- 5. [stat(path) ==> object](#orgeb61168)
27
- 6. [get(path, dst, options) ==> String|Stream|Buffer](#orga2d8770)
28
- 7. [fastGet(remotePath, localPath, options) ===> string](#orgd7fae2f)
29
- 8. [put(src, remotePath, options) ==> string](#orgfea8ec2)
30
- 9. [fastPut(localPath, remotePath, options) ==> string](#orgab8a35f)
31
- 10. [append(input, remotePath, options) ==> string](#org789e7fe)
32
- 11. [mkdir(path, recursive) ==> string](#org9e8c8c6)
33
- 12. [rmdir(path, recursive) ==> string](#org0edeb97)
34
- 13. [delete(path) ==> string](#org81d6ffe)
35
- 14. [rename(fromPath, toPath) ==> string](#org2e85ca1)
36
- 15. [posixRename(fromPath, toPath) ==> string](#org4d21fb7)
37
- 16. [chmod(path, mode) ==> string](#org91257cd)
38
- 17. [realPath(path) ===> string](#org6b2b3f8)
39
- 18. [cwd() ==> string](#orgae34ed3)
40
- 19. [uploadDir(srcDir, dstDir) ==> string](#org6e5e031)
41
- 20. [downloadDir(srcDir, dstDir) ==> string](#org751c9af)
42
- 21. [end() ==> boolean](#orgfbea0b9)
43
- 22. [Add and Remove Listeners](#orgcd064ac)
44
- 6. [FAQ](#org792a244)
45
- 1. [Remote server drops connections with only an end event](#org4a4dd6c)
46
- 2. [How can you pass writable stream as dst for get method?](#org735f9cc)
47
- 3. [How can I upload files without having to specify a password?](#orgce4077c)
48
- 4. [How can I connect through a Socks Proxy](#org945fc8f)
49
- 5. [Timeout while waiting for handshake or handshake errors](#org22b7853)
50
- 7. [Examples](#org14f6de1)
51
- 8. [Change Log](#orgf7b58df)
52
- 1. [v5.2.0 (Prod Version)](#org73f1344)
53
- 2. [v5.1.3](#orga6a3e40)
54
- 3. [v5.1.2](#org809d915)
55
- 4. [v5.1.1](#org27c7458)
56
- 5. [v5.1.0](#org5fb8ed9)
57
- 6. [v5.0.2](#org5c4a5da)
58
- 7. [v5.0.1](#org47a7bec)
59
- 8. [v5.0.0](#orge53577f)
60
- 9. [v4.3.1](#orgf4a7c60)
61
- 10. [v4.3.0](#org70ad882)
62
- 11. [v4.2.4](#org4d3d15c)
63
- 12. [v4.2.3](#org6ce1458)
64
- 13. [v4.2.2](#org7dafb79)
65
- 14. [v4.2.1](#org4e0749a)
66
- 15. [v4.2.0](#org3a386b5)
67
- 16. [v4.1.0](#org5bdeafc)
68
- 17. [v4.0.4](#orgf78aa34)
69
- 18. [v4.0.3](#org7ceeee9)
70
- 19. [v4.0.2](#org5fb5e3d)
71
- 20. [v4.0.0](#org83d4545)
72
- 21. [v2.5.2](#orgc6ca313)
73
- 22. [v2.5.1](#org2dafe31)
74
- 23. [v2.5.0](#orge233569)
75
- 24. [v2.4.3](#orgb05446f)
76
- 25. [v2.4.2](#orgad96dc1)
77
- 26. [v2.4.1](#org13a3465)
78
- 27. [v2.4.0](#org954540b)
79
- 28. [v2.3.0](#orgb50df74)
80
- 29. [v3.0.0 – deprecate this version](#org5d0ec00)
81
- 30. [v2.1.1](#org6b8440d)
82
- 31. [v2.0.1](#org3d55c20)
83
- 32. [v1.1.0](#org93598f6)
84
- 33. [v1.0.5:](#orgacc1b05)
85
- 9. [Troubleshooting](#orgeb31b33)
86
- 1. [Common Errors](#org97ed736)
87
- 1. [Not returning the promise in a `then()` block](#org415e864)
88
- 2. [Mixing Promise Chains and Async/Await](#org0212f67)
89
- 3. [Try/catch and Error Handlers](#orgfb2766e)
90
- 2. [Debugging Support](#orgb0a64b5)
91
- 10. [Logging Issues](#org63eba2b)
92
- 11. [Pull Requests](#org03a2ed4)
93
- 12. [Contributors](#org786c526)
94
-
95
-
96
- <a id="org01ca044"></a>
97
-
98
- # SSH2 SFTP Client
99
-
100
- an SFTP client for node.js, a wrapper around [SSH2](https://github.com/mscdex/ssh2) which provides a high level
101
- convenience abstraction as well as a Promise based API.
102
-
103
- Documentation on the methods and available options in the underlying modules can
104
- be found on the [SSH2](https://github.com/mscdex/ssh2) and [SSH2-STREAMS](https://github.com/mscdex/ssh2-streams/blob/master/SFTPStream.md) project pages.
105
-
106
- Current stable release is **v5.2.0**.
107
-
108
- Code has been tested against Node versions 12.18.0 and 13.14.0
1
+ - [SSH2 SFTP Client](#sec-1)
2
+ - [Installation](#sec-2)
3
+ - [Basic Usage](#sec-3)
4
+ - [Version 5.x](#sec-4)
5
+ - [Breaking Changes in Version 5.x](#sec-4-1)
6
+ - [Error Event Handling](#sec-4-1-1)
7
+ - [Technical Details](#sec-4-1-2)
8
+ - [New Methods](#sec-4-2)
9
+ - [Version 5.0.1](#sec-4-3)
10
+ - [Version 5.0.2](#sec-4-4)
11
+ - [Version 5.1.0](#sec-4-5)
12
+ - [Version 5.1.1](#sec-4-6)
13
+ - [Version 5.1.2](#sec-4-7)
14
+ - [Version 5.1.3](#sec-4-8)
15
+ - [Version 5.2.0](#sec-4-9)
16
+ - [Version 5.2.1](#sec-4-10)
17
+ - [Documentation](#sec-5)
18
+ - [Specifying Paths](#sec-5-1)
19
+ - [Methods](#sec-5-2)
20
+ - [new SftpClient(name) ===> SFTP client object](#sec-5-2-1)
21
+ - [connect(config) ===> SFTPstream](#sec-5-2-2)
22
+ - [list(path, pattern) ==> Array[object]](#sec-5-2-3)
23
+ - [exists(path) ==> boolean](#sec-5-2-4)
24
+ - [stat(path) ==> object](#sec-5-2-5)
25
+ - [get(path, dst, options) ==> String|Stream|Buffer](#sec-5-2-6)
26
+ - [fastGet(remotePath, localPath, options) ===> string](#sec-5-2-7)
27
+ - [put(src, remotePath, options) ==> string](#sec-5-2-8)
28
+ - [fastPut(localPath, remotePath, options) ==> string](#sec-5-2-9)
29
+ - [append(input, remotePath, options) ==> string](#sec-5-2-10)
30
+ - [mkdir(path, recursive) ==> string](#sec-5-2-11)
31
+ - [rmdir(path, recursive) ==> string](#sec-5-2-12)
32
+ - [delete(path) ==> string](#sec-5-2-13)
33
+ - [rename(fromPath, toPath) ==> string](#sec-5-2-14)
34
+ - [posixRename(fromPath, toPath) ==> string](#sec-5-2-15)
35
+ - [chmod(path, mode) ==> string](#sec-5-2-16)
36
+ - [realPath(path) ===> string](#sec-5-2-17)
37
+ - [cwd() ==> string](#sec-5-2-18)
38
+ - [uploadDir(srcDir, dstDir) ==> string](#sec-5-2-19)
39
+ - [downloadDir(srcDir, dstDir) ==> string](#sec-5-2-20)
40
+ - [end() ==> boolean](#sec-5-2-21)
41
+ - [Add and Remove Listeners](#sec-5-2-22)
42
+ - [FAQ](#sec-6)
43
+ - [Remote server drops connections with only an end event](#sec-6-1)
44
+ - [How can you pass writable stream as dst for get method?](#sec-6-2)
45
+ - [How can I upload files without having to specify a password?](#sec-6-3)
46
+ - [How can I connect through a Socks Proxy](#sec-6-4)
47
+ - [Timeout while waiting for handshake or handshake errors](#sec-6-5)
48
+ - [Examples](#sec-7)
49
+ - [Change Log](#sec-8)
50
+ - [v5.2.1 (Prod Version)](#sec-8-1)
51
+ - [v5.2.0](#sec-8-2)
52
+ - [v5.1.3](#sec-8-3)
53
+ - [v5.1.2](#sec-8-4)
54
+ - [v5.1.1](#sec-8-5)
55
+ - [v5.1.0](#sec-8-6)
56
+ - [v5.0.2](#sec-8-7)
57
+ - [v5.0.1](#sec-8-8)
58
+ - [v5.0.0](#sec-8-9)
59
+ - [v4.3.1](#sec-8-10)
60
+ - [v4.3.0](#sec-8-11)
61
+ - [v4.2.4](#sec-8-12)
62
+ - [v4.2.3](#sec-8-13)
63
+ - [v4.2.2](#sec-8-14)
64
+ - [v4.2.1](#sec-8-15)
65
+ - [v4.2.0](#sec-8-16)
66
+ - [v4.1.0](#sec-8-17)
67
+ - [v4.0.4](#sec-8-18)
68
+ - [v4.0.3](#sec-8-19)
69
+ - [v4.0.2](#sec-8-20)
70
+ - [v4.0.0](#sec-8-21)
71
+ - [Older Versions](#sec-8-22)
72
+ - [v2.5.2](#sec-8-22-1)
73
+ - [v2.5.1](#sec-8-22-2)
74
+ - [v2.5.0](#sec-8-22-3)
75
+ - [v2.4.3](#sec-8-22-4)
76
+ - [v2.4.2](#sec-8-22-5)
77
+ - [v2.4.1](#sec-8-22-6)
78
+ - [v2.4.0](#sec-8-22-7)
79
+ - [v2.3.0](#sec-8-22-8)
80
+ - [v3.0.0 &#x2013; deprecate this version](#sec-8-22-9)
81
+ - [v2.1.1](#sec-8-22-10)
82
+ - [v2.0.1](#sec-8-22-11)
83
+ - [v1.1.0](#sec-8-22-12)
84
+ - [v1.0.5:](#sec-8-22-13)
85
+ - [Troubleshooting](#sec-9)
86
+ - [Common Errors](#sec-9-1)
87
+ - [Not returning the promise in a `then()` block](#sec-9-1-1)
88
+ - [Mixing Promise Chains and Async/Await](#sec-9-1-2)
89
+ - [Try/catch and Error Handlers](#sec-9-1-3)
90
+ - [Debugging Support](#sec-9-2)
91
+ - [Logging Issues](#sec-10)
92
+ - [Pull Requests](#sec-11)
93
+ - [Contributors](#sec-12)
94
+
95
+ # SSH2 SFTP Client<a id="sec-1"></a>
96
+
97
+ an SFTP client for node.js, a wrapper around [SSH2](https://github.com/mscdex/ssh2) which provides a high level convenience abstraction as well as a Promise based API.
98
+
99
+ Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) and [SSH2-STREAMS](https://github.com/mscdex/ssh2-streams/blob/master/SFTPStream.md) project pages.
100
+
101
+ Current stable release is **v5.2.1**.
102
+
103
+ Code has been tested against Node versions 12.18.2 and 13.14.0
109
104
 
110
105
  Node versions < 10.x are not supported.
111
106
 
112
- <span class="underline">WARNING</span> There is currently an issue with both the fastPut() and fastGet()
113
- methods when using Node versions greater than 14.0.0. This is a bug in the
114
- underlying ssh2-streams library and needs to be fixed upstream. The issue
115
- appears to be related to the concurrency operations of these two functions. A
116
- workaround is to set concurrency to 1 using the options object. Alternatively,
117
- use get() or put(), which do not use concurrency and which will provide the same
118
- performance as fastGet() or fastPut() when they are set to use a concurrency
119
- of 1. A bug report has been logged against the ssh2-streams library as [issue 156](https://github.com/mscdex/ssh2-streams/issues/156).
107
+ <span class="underline">WARNING</span> There is currently an issue with both the fastPut() and fastGet() methods when using Node versions greater than 14.0.0. This is a bug in the underlying ssh2-streams library and needs to be fixed upstream. The issue appears to be related to the concurrency operations of these two functions. A workaround is to set concurrency to 1 using the options object. Alternatively, use get() or put(), which do not use concurrency and which will provide the same performance as fastGet() or fastPut() when they are set to use a concurrency of 1. A bug report has been logged against the ssh2-streams library as [issue 156](https://github.com/mscdex/ssh2-streams/issues/156).
120
108
 
109
+ # Installation<a id="sec-2"></a>
121
110
 
122
- <a id="org2931228"></a>
111
+ ```shell
112
+ npm install ssh2-sftp-client
113
+ ```
123
114
 
124
- # Installation
115
+ # Basic Usage<a id="sec-3"></a>
125
116
 
126
- npm install ssh2-sftp-client
117
+ ```javascript
118
+ let Client = require('ssh2-sftp-client');
119
+ let sftp = new Client();
127
120
 
121
+ sftp.connect({
122
+ host: '127.0.0.1',
123
+ port: '8080',
124
+ username: 'username',
125
+ password: '******'
126
+ }).then(() => {
127
+ return sftp.list('/pathname');
128
+ }).then(data => {
129
+ console.log(data, 'the data info');
130
+ }).catch(err => {
131
+ console.log(err, 'catch error');
132
+ });
133
+ ```
128
134
 
129
- <a id="orgaa4b035"></a>
135
+ # Version 5.x<a id="sec-4"></a>
130
136
 
131
- # Basic Usage
137
+ ## Breaking Changes in Version 5.x<a id="sec-4-1"></a>
132
138
 
133
- let Client = require('ssh2-sftp-client');
134
- let sftp = new Client();
135
-
136
- sftp.connect({
137
- host: '127.0.0.1',
138
- port: '8080',
139
- username: 'username',
140
- password: '******'
141
- }).then(() => {
142
- return sftp.list('/pathname');
143
- }).then(data => {
144
- console.log(data, 'the data info');
145
- }).catch(err => {
146
- console.log(err, 'catch error');
147
- });
139
+ - The auxList() method has been removed. This method was flagged as deprecated in version 4.x. The functionality provided by `auxList()` is available in `list()`, making `auxList()` unnecessary.
140
+ - The realPath() method now returns `''` if the path does not exist rather than throwing an exception.
141
+ - Improved error handling. The `ssh2` and `ssh2-streams` libraries use events to signal errors. Providing a clean Promise based API and managing these events can be challenging as an error event can fire at any time (including in-between the resolution of one promise and the commencement of another). As you cannot use `try/catch` blocks to reliably manage error events (for a similar reason - see Node's event documentation for details), a slightly more complex solution was required. See the section below on Error Event Handling for more details. In basic terms, a default handler is now used that will log the error and clear the SFTP connection if no Promise error handler has handled the error. This prevents the uncaughtException error and provides a reasonably clean way to deal with unexpected errors that fire in-between Promise execution activities.
142
+ - Ignore Errors during `end()` processing. At least one SFTP server (Azure SFTP) seems to generate an error in response to the `end()` call. As `end()` has been called, we don't really care if an error occurs provided the connection is closed. Therefore, a new default error listener for the `end()` method has been added that will simply ignore any errors which occur during a call to end the connection.
148
143
 
144
+ ### Error Event Handling<a id="sec-4-1-1"></a>
149
145
 
150
- <a id="org8896bb7"></a>
151
-
152
- # Version 5.x
153
-
154
-
155
- <a id="org7dd479a"></a>
156
-
157
- ## Breaking Changes in Version 5.x
158
-
159
- - The auxList() method has been removed. This method was flagged as deprecated
160
- in version 4.x. The functionality provided by `auxList()` is available in
161
- `list()`, making `auxList()` unnecessary.
162
- - The realPath() method now returns `''` if the path does not exist
163
- rather than throwing an exception.
164
- - Improved error handling. The `ssh2` and `ssh2-streams` libraries use events
165
- to signal errors. Providing a clean Promise based API and managing these
166
- events can be challenging as an error event can fire at any time (including
167
- in-between the resolution of one promise and the commencement of another). As
168
- you cannot use `try/catch` blocks to reliably manage error events (for a
169
- similar reason - see Node's event documentation for details), a slightly
170
- more complex solution was required. See the section below on Error Event
171
- Handling for more details. In basic terms, a default handler is now used
172
- that will log the error and clear the SFTP connection if no Promise error
173
- handler has handled the error. This prevents the uncaughtException error
174
- and provides a reasonably clean way to deal with unexpected errors that
175
- fire in-between Promise execution activities.
176
- - Ignore Errors during `end()` processing. At least one SFTP server (Azure
177
- SFTP) seems to generate an error in response to the `end()` call. As
178
- `end()` has been called, we don't really care if an error occurs provided
179
- the connection is closed. Therefore, a new default error listener for the
180
- `end()` method has been added that will simply ignore any errors which
181
- occur during a call to end the connection.
182
-
183
-
184
- <a id="org5cb33ab"></a>
185
-
186
- ### Error Event Handling
187
-
188
- Providing a clean Promise API for the SSH2 to manage basic SFTP functionality
189
- presents a couple of challenges for managing errors. The `SSH2` module uses
190
- events to communicate various state changes and error conditions. These
191
- events can fire at any time.
192
-
193
- On the client side, we wrap basic SFTP actions in Javascript Promises,
194
- allowing clients to use either the standard Promise API or async/await to
195
- model SFTP interactions. Creating an SFTP connection returns a promise, which
196
- resolves if a connection is successfully established and is rejected
197
- otherwise. Downloading a file using `get()` or `fastGet()` generates a new
198
- Promise which is either resolved, indicating file has been successfully
199
- downloaded or rejected, indicating the download failed. All pretty
200
- straight-forward.
201
-
202
- When the Promise is created, an error event handler is added to the SFTP
203
- object to catch any errors that fire during the execution of the promise. If
204
- an error event fires, the Promise is rejected and the error returned to the
205
- client as part of the rejection. After the Promise has resolved or rejected,
206
- the error listener is removed (the error listener is specific to each promise
207
- because it needs to call the reject method associated with that promise). As
208
- a promise can only be resolved or rejected once, after the Promise has
209
- completed, the error listener is of no further use.
210
-
211
- This all works fine when an error event fires during the execution of a
212
- Promise. However, what about outside promise execution? Consider the
213
- following flow;
146
+ Providing a clean Promise API for the SSH2 to manage basic SFTP functionality presents a couple of challenges for managing errors. The `SSH2` module uses events to communicate various state changes and error conditions. These events can fire at any time.
214
147
 
215
- 1. You have an active SFTP connection which you use to download a file
216
- 2. When you make the download request, a new Promise is created which will
217
- resolve when the file is downloaded or be rejected if the download fails
218
- for some reason. The promise resolves successfully.
219
- 3. You start processing the data downloaded. At this point, you still have an
220
- open connection to the SFTP server, but you are not actively interacting
221
- with it. There is no active Promise in play.
222
- 4. The remote SFTP server resets the connection for some reason, generating a
223
- ECONNRESET error that is emitted as an error event.
224
-
225
- What happens at this point? There is no active promise executing, so no
226
- Promise specific error handler in play. Your script is off processing the
227
- data from the previously downloaded file, so there is no currently executing
228
- try/catch block around the SFTP client object. Basically, there is nothing
229
- listening of any errors at this point. What will happen?
230
-
231
- Well, basically, the error event will bubble up to the top level of the node
232
- process context and cause an uncaughtException error, display the error and
233
- dump a stack trace and cause the node process to exit. In basic terms, your
234
- process will crash. Not a great outcome.
235
-
236
- There are a number of things we can do to improve the situation. However,
237
- nearly all of them have some drawbacks. We could -
238
-
239
- - Add our own error handler. The `client.on()` method would allow you to add
240
- your own error handler. This would provide a way to manage error events,
241
- but you want to make sure you only handle error events not handled already
242
- by the Promise error handlers. Worse yet, you cannot know before hand the
243
- processing context of your script at the point the error event fires. This
244
- means your error handling is likely to be complex and difficult to
245
- manage. Worse yet, these types of errors are quite rare in most situations
246
- and your now being required to add significant additional complexity to
247
- deal with a rare edge case. However, sometimes, you just need to deal with
248
- this sort of complexity and the `client.on()` method does give you that
249
- option.
250
- - Another alternative is to just add an uncaughtException handler to your Node
251
- process object. This would also prevent node from dumping the error and
252
- exiting abruptly. However, now you need to think about ALL the possible
253
- uncaughtExceptions which might happen, not just those associated with the
254
- SFTP client. Again, things are getting complicated for something which only
255
- occurs occasionally. .
256
-
257
- What we really want is a solution which will be simple for the majority of
258
- clients, but provide additional power when needed. What we have done is add a
259
- default error handler which will only take action if no Promise error handler
260
- has fired. All the default error handler does is log the error to
261
- console.error() and set the SFTP connection to undefined so that any further
262
- attempts to use the connection will throw an error inside the Promise which
263
- attempts to use it.
264
-
265
- The advantage of this approach is that it stops the abrupt exiting of the
266
- node script due to an uncaught exception error and provides a reasonable
267
- outcome for most use cases. For example, in the scenario outlined above, if
268
- an error event fires while your script is processing the data already
269
- downloaded, it will not impact on your script immediately. An error will be
270
- logged to console.error(), but your script will continue to run. Once you
271
- have completed processing your data, if you attempt another SFTP call, it
272
- will fail with an error about no available SFTP connections. As this will
273
- occur within the context of interacting with the SFTP server, your script can
274
- take appropriate action to resolve the issue (such as re-connecting to the
275
- server). On the other hand, if after processing the file your done and just
276
- want to end, then you can just ignore the error, perform any necessary
277
- cleanup work and exit successfully.
278
-
279
-
280
- <a id="org8e4dbfb"></a>
281
-
282
- ### Technical Details
283
-
284
- The event handlers added by each Promise are added using the `prependListener()`
285
- function. This ensures the handler is fired before any other error handlers
286
- which may be defined. As part of the processing, these error handler set a flag
287
- property `this.errorHandled` to true, indicating the error has been handled.
288
-
289
- In addition to the Promise error handlers, there is a default error handler
290
- which will fire after any Promise error handler. The default error handler looks
291
- to see if the `this.errorHandler` flag is true. If it is, it knows the error has
292
- been handled and it just resets it to false, taking no other action (so taht we
293
- are ready for the next error). If the flag is false, the default handler knows
294
- it must handle the error. In this case, the handler will log the error to
295
- `console.error()`, will set the SFTP connection to undefined to prevent any
296
- further attempts to use it and finally, ensure the `this.errorHandler` flag is
297
- reset to false in preparation for the next error.
298
-
299
-
300
- <a id="org948a6b7"></a>
301
-
302
- ## New Methods
303
-
304
- - Added the method uploadDir(). This method will upload a directory
305
- (including any subdirectories) to the remote server. Only directories and
306
- regular files are uploaded (no symbolic links, FIFOs, socket FDs etc). Will
307
- overwrite existing files or directories, but will not delete any remote
308
- files or directories.
309
- - Added the method downloadDir(). This method will download a directory
310
- (including any subdirectories) to the local file system. Only directories
311
- and regular files are downloaded (no symbolic links, FIFOs, socket FDs
312
- etc).. Will overwrite existing files or directories, but will not delete
313
- any local files in the directories.
314
- - Added the method posixRename(). This method will use the POSIX atomic
315
- rename openSSH extension. As this is an extension to the SFTP protocol, not
316
- all servers will support this operation.
317
-
318
-
319
- <a id="org5cf9acc"></a>
320
-
321
- ## Version 5.0.1
322
-
323
- - The error checking was a little too stringent. The use of exist() to test
324
- for file types had a problem when the user does not have read/execute
325
- rights on the directory. Replaced with stat() method, which should avoid
326
- this issue.
327
-
328
-
329
- <a id="org8bbba9f"></a>
330
-
331
- ## Version 5.0.2
148
+ On the client side, we wrap basic SFTP actions in Javascript Promises, allowing clients to use either the standard Promise API or async/await to model SFTP interactions. Creating an SFTP connection returns a promise, which resolves if a connection is successfully established and is rejected otherwise. Downloading a file using `get()` or `fastGet()` generates a new Promise which is either resolved, indicating file has been successfully downloaded or rejected, indicating the download failed. All pretty straight-forward.
332
149
 
333
- - Fix error in local directory tests due to missing await statement.
334
- - Fix path handling under win32. Paths were not being parsed correctly due to
335
- the use of path.posix.parse() instead of path.parse().
150
+ When the Promise is created, an error event handler is added to the SFTP object to catch any errors that fire during the execution of the promise. If an error event fires, the Promise is rejected and the error returned to the client as part of the rejection. After the Promise has resolved or rejected, the error listener is removed (the error listener is specific to each promise because it needs to call the reject method associated with that promise). As a promise can only be resolved or rejected once, after the Promise has completed, the error listener is of no further use.
336
151
 
152
+ This all works fine when an error event fires during the execution of a Promise. However, what about outside promise execution? Consider the following flow;
337
153
 
338
- <a id="org60ab99e"></a>
154
+ 1. You have an active SFTP connection which you use to download a file
155
+ 2. When you make the download request, a new Promise is created which will resolve when the file is downloaded or be rejected if the download fails for some reason. The promise resolves successfully.
156
+ 3. You start processing the data downloaded. At this point, you still have an open connection to the SFTP server, but you are not actively interacting with it. There is no active Promise in play.
157
+ 4. The remote SFTP server resets the connection for some reason, generating a ECONNRESET error that is emitted as an error event.
339
158
 
340
- ## Version 5.1.0
159
+ What happens at this point? There is no active promise executing, so no Promise specific error handler in play. Your script is off processing the data from the previously downloaded file, so there is no currently executing try/catch block around the SFTP client object. Basically, there is nothing listening of any errors at this point. What will happen?
341
160
 
342
- - Add missing connection check in end() method
343
- - Add debugging support. Now adding a debug property to the connection
344
- configuration object will enable debugging. The value of the debug property
345
- should be a function which accepts a single string argument. Typically,
346
- this function will send the value passed in to stderr or a file.
347
- - Fix bug in checkRemotePath() relating to poor path specifications where
348
- you cannot determine parent directory.
161
+ Well, basically, the error event will bubble up to the top level of the node process context and cause an uncaughtException error, display the error and dump a stack trace and cause the node process to exit. In basic terms, your process will crash. Not a great outcome.
349
162
 
163
+ There are a number of things we can do to improve the situation. However, nearly all of them have some drawbacks. We could -
350
164
 
351
- <a id="org55b82ed"></a>
165
+ - Add our own error handler. The `client.on()` method would allow you to add your own error handler. This would provide a way to manage error events, but you want to make sure you only handle error events not handled already by the Promise error handlers. Worse yet, you cannot know before hand the processing context of your script at the point the error event fires. This means your error handling is likely to be complex and difficult to manage. Worse yet, these types of errors are quite rare in most situations and your now being required to add significant additional complexity to deal with a rare edge case. However, sometimes, you just need to deal with this sort of complexity and the `client.on()` method does give you that option.
166
+ - Another alternative is to just add an uncaughtException handler to your Node process object. This would also prevent node from dumping the error and exiting abruptly. However, now you need to think about ALL the possible uncaughtExceptions which might happen, not just those associated with the SFTP client. Again, things are getting complicated for something which only occurs occasionally. .
352
167
 
353
- ## Version 5.1.1
168
+ What we really want is a solution which will be simple for the majority of clients, but provide additional power when needed. What we have done is add a default error handler which will only take action if no Promise error handler has fired. All the default error handler does is log the error to console.error() and set the SFTP connection to undefined so that any further attempts to use the connection will throw an error inside the Promise which attempts to use it.
354
169
 
355
- - Bug fix for unexpected close of connections. It would seem that a
356
- connections can be unexpectedly closed without an accompanying error event.
357
- As methods only looked for error events, the method promise wold never
358
- fulfil and the method would appear to hang. Have now added close event
359
- handlers to each method that will reject the promise if the connection is
360
- closed unexpectedly.
361
- - Missing return statement in connect method would result in the connect
362
- method attempting to re-connect again after it had reached maximum connect
363
- retries. Added the missing return statement.
364
- - Added some more troubleshooting documentation. Numerous issues have been
365
- raised that turn out to be due to client code failing to return Promises
366
- inside promise chains. Common symptom is what appears to be truncated file
367
- upload/download. What is really happening is that the end method is being
368
- called before the transfer has completed.
170
+ The advantage of this approach is that it stops the abrupt exiting of the node script due to an uncaught exception error and provides a reasonable outcome for most use cases. For example, in the scenario outlined above, if an error event fires while your script is processing the data already downloaded, it will not impact on your script immediately. An error will be logged to console.error(), but your script will continue to run. Once you have completed processing your data, if you attempt another SFTP call, it will fail with an error about no available SFTP connections. As this will occur within the context of interacting with the SFTP server, your script can take appropriate action to resolve the issue (such as re-connecting to the server). On the other hand, if after processing the file your done and just want to end, then you can just ignore the error, perform any necessary cleanup work and exit successfully.
369
171
 
172
+ ### Technical Details<a id="sec-4-1-2"></a>
370
173
 
371
- <a id="orgf04af40"></a>
174
+ The event handlers added by each Promise are added using the `prependListener()` function. This ensures the handler is fired before any other error handlers which may be defined. As part of the processing, these error handler set a flag property `this.errorHandled` to true, indicating the error has been handled.
372
175
 
373
- ## Version 5.1.2
176
+ In addition to the Promise error handlers, there is a default error handler which will fire after any Promise error handler. The default error handler looks to see if the `this.errorHandler` flag is true. If it is, it knows the error has been handled and it just resets it to false, taking no other action (so taht we are ready for the next error). If the flag is false, the default handler knows it must handle the error. In this case, the handler will log the error to `console.error()`, will set the SFTP connection to undefined to prevent any further attempts to use it and finally, ensure the `this.errorHandler` flag is reset to false in preparation for the next error.
374
177
 
375
- - Mainly a bug fix. We needed to add back a global close listener to ensure
376
- the sftp object is unset whenever a close event occurs. As close events can
377
- occur outside main method calls, only having method based listeners was not
378
- sufficient.
379
- - Also added a utils.dumpListeners() method, useful when debugging issues with
380
- listener 'leakage' due to failure to remove listeners when no longer required.
178
+ ## New Methods<a id="sec-4-2"></a>
381
179
 
180
+ - Added the method uploadDir(). This method will upload a directory (including any subdirectories) to the remote server. Only directories and regular files are uploaded (no symbolic links, FIFOs, socket FDs etc). Will overwrite existing files or directories, but will not delete any remote files or directories.
181
+ - Added the method downloadDir(). This method will download a directory (including any subdirectories) to the local file system. Only directories and regular files are downloaded (no symbolic links, FIFOs, socket FDs etc).. Will overwrite existing files or directories, but will not delete any local files in the directories.
182
+ - Added the method posixRename(). This method will use the POSIX atomic rename openSSH extension. As this is an extension to the SFTP protocol, not all servers will support this operation.
382
183
 
383
- <a id="org27af3a9"></a>
184
+ ## Version 5.0.1<a id="sec-4-3"></a>
384
185
 
385
- ## Version 5.1.3
186
+ - The error checking was a little too stringent. The use of exist() to test for file types had a problem when the user does not have read/execute rights on the directory. Replaced with stat() method, which should avoid this issue.
386
187
 
387
- - Fix issue with permissions for writing to root directory
388
- - Cleanup tests to use less connections and eliminate need for test delays
389
- - Bumped some dependencies to latest versions
188
+ ## Version 5.0.2<a id="sec-4-4"></a>
390
189
 
190
+ - Fix error in local directory tests due to missing await statement.
191
+ - Fix path handling under win32. Paths were not being parsed correctly due to the use of path.posix.parse() instead of path.parse().
391
192
 
392
- <a id="org0a530e1"></a>
193
+ ## Version 5.1.0<a id="sec-4-5"></a>
393
194
 
394
- ## Version 5.2.0
195
+ - Add missing connection check in end() method
196
+ - Add debugging support. Now adding a debug property to the connection configuration object will enable debugging. The value of the debug property should be a function which accepts a single string argument. Typically, this function will send the value passed in to stderr or a file.
197
+ - Fix bug in checkRemotePath() relating to poor path specifications where you cannot determine parent directory.
395
198
 
396
- - Add posixRename() method. This is an openssh extension added in openssh
397
- v4.8 and will only work on servers which support this extension.conflict
398
- - Bumped through2 dependency version to 4.0.2
199
+ ## Version 5.1.1<a id="sec-4-6"></a>
399
200
 
201
+ - Bug fix for unexpected close of connections. It would seem that a connections can be unexpectedly closed without an accompanying error event. As methods only looked for error events, the method promise wold never fulfil and the method would appear to hang. Have now added close event handlers to each method that will reject the promise if the connection is closed unexpectedly.
202
+ - Missing return statement in connect method would result in the connect method attempting to re-connect again after it had reached maximum connect retries. Added the missing return statement.
203
+ - Added some more troubleshooting documentation. Numerous issues have been raised that turn out to be due to client code failing to return Promises inside promise chains. Common symptom is what appears to be truncated file upload/download. What is really happening is that the end method is being called before the transfer has completed.
400
204
 
401
- <a id="org87db731"></a>
205
+ ## Version 5.1.2<a id="sec-4-7"></a>
402
206
 
403
- # Documentation
207
+ - Mainly a bug fix. We needed to add back a global close listener to ensure the sftp object is unset whenever a close event occurs. As close events can occur outside main method calls, only having method based listeners was not sufficient.
208
+ - Also added a utils.dumpListeners() method, useful when debugging issues with listener 'leakage' due to failure to remove listeners when no longer required.
404
209
 
405
- The connection options are the same as those offered by the underlying SSH2
406
- module. For full details, please see [SSH2 client methods](https://github.com/mscdex/ssh2#user-content-client-methods)
210
+ ## Version 5.1.3<a id="sec-4-8"></a>
407
211
 
408
- All the methods will return a Promise, except for `on()` and
409
- `removeListener()`, which are typically only used in special use cases.
212
+ - Fix issue with permissions for writing to root directory
213
+ - Cleanup tests to use less connections and eliminate need for test delays
214
+ - Bumped some dependencies to latest versions
410
215
 
216
+ ## Version 5.2.0<a id="sec-4-9"></a>
217
+
218
+ - Add posixRename() method. This is an openssh extension added in openssh v4.8 and will only work on servers which support this extension.conflict
219
+ - Bumped through2 dependency version to 4.0.2
411
220
 
412
- <a id="orgdb75138"></a>
221
+ ## Version 5.2.1<a id="sec-4-10"></a>
413
222
 
414
- ## Specifying Paths
223
+ - Move some dev dependencies from dependencies to devDependencies.
415
224
 
416
- All remote paths must either be absolute e.g. `/absolute/path/to/file` or they
417
- can be relative with a prefix of either `./` (relative to current remote
418
- directory) or `../` (relative to parent of current remote directory) e.g.
419
- `./relative/path/to/file` or `../relative/to/parent/file`. It is also possible
420
- to do things like `../../../file` to specify the parent of the parent of the
421
- parent of the current remote directory. The shell tilde (`~`) and common
422
- environment variables like `$HOME` are NOT supported.
225
+ # Documentation<a id="sec-5"></a>
423
226
 
424
- It is important to recognise that the current remote directory may not always be
425
- what you may expect. A lot will depend on the remote platform of the SFTP server
426
- and how the SFTP server has been configured. When things don't seem to be
427
- working as expected, it is often a good idea to verify your assumptions
428
- regarding the remote directory and remote paths. One way to do this is to login
429
- using a command line program like `sftp` or `lftp`.
227
+ The connection options are the same as those offered by the underlying SSH2 module. For full details, please see [SSH2 client methods](https://github.com/mscdex/ssh2#user-content-client-methods)
430
228
 
431
- There is a small performance hit for using `./` and `../` as the module must
432
- query the remote server to determine what the root path is and derive the
433
- absolute path. Using absolute paths are therefore more efficient and likely more
434
- robust.
229
+ All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
435
230
 
436
- When specifying file paths, ensure to include a full path i.e. include the
437
- remote filename. Don't expect the module to append the local file name to the
438
- path you provide. For example, the following will not work
231
+ ## Specifying Paths<a id="sec-5-1"></a>
439
232
 
440
- client.put('/home/fred/test.txt', '/remote/dir');
233
+ All remote paths must either be absolute e.g. `/absolute/path/to/file` or they can be relative with a prefix of either `./` (relative to current remote directory) or `../` (relative to parent of current remote directory) e.g. `./relative/path/to/file` or `../relative/to/parent/file`. It is also possible to do things like `../../../file` to specify the parent of the parent of the parent of the current remote directory. The shell tilde (`~`) and common environment variables like `$HOME` are NOT supported.
441
234
 
442
- will not result in the file `test.txt` being copied to
443
- `/remote/dir/test.txt`. You need to specify the target filename as well e.g.
235
+ It is important to recognise that the current remote directory may not always be what you may expect. A lot will depend on the remote platform of the SFTP server and how the SFTP server has been configured. When things don't seem to be working as expected, it is often a good idea to verify your assumptions regarding the remote directory and remote paths. One way to do this is to login using a command line program like `sftp` or `lftp`.
444
236
 
445
- client.put('/home/fred/test.txt', '/remote/dir/test.txt');
237
+ There is a small performance hit for using `./` and `../` as the module must query the remote server to determine what the root path is and derive the absolute path. Using absolute paths are therefore more efficient and likely more robust.
446
238
 
447
- Note that the remote file name does not have to be the same as the local file
448
- name. The following works fine;
239
+ When specifying file paths, ensure to include a full path i.e. include the remote filename. Don't expect the module to append the local file name to the path you provide. For example, the following will not work
449
240
 
450
- client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
241
+ ```javascript
242
+ client.put('/home/fred/test.txt', '/remote/dir');
243
+ ```
451
244
 
452
- This will copy the local file `test.txt` to the remote file `test-copy.txt` in
453
- the directory `/remote/dir`.
245
+ will not result in the file `test.txt` being copied to `/remote/dir/test.txt`. You need to specify the target filename as well e.g.
454
246
 
247
+ ```javascript
248
+ client.put('/home/fred/test.txt', '/remote/dir/test.txt');
249
+ ```
455
250
 
456
- <a id="org6d72402"></a>
251
+ Note that the remote file name does not have to be the same as the local file name. The following works fine;
457
252
 
458
- ## Methods
253
+ ```javascript
254
+ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
255
+ ```
459
256
 
257
+ This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
460
258
 
461
- <a id="orgc685aec"></a>
259
+ ## Methods<a id="sec-5-2"></a>
462
260
 
463
- ### new SftpClient(name) ===> SFTP client object
261
+ ### new SftpClient(name) ===> SFTP client object<a id="sec-5-2-1"></a>
464
262
 
465
- Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
466
- can be provided, which will be used in error messages to help identify which
467
- client has thrown the error.
263
+ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string can be provided, which will be used in error messages to help identify which client has thrown the error.
468
264
 
469
265
  1. Constructor Arguments
470
266
 
@@ -472,212 +268,196 @@ client has thrown the error.
472
268
 
473
269
  2. Example Use
474
270
 
475
- 'use strict';
476
-
477
- const Client = require('ssh2-sftp-client');
478
-
479
- const config = {
480
- host: 'example.com',
481
- username: 'donald',
482
- password: 'my-secret'
483
- };
484
-
485
- const sftp = new Client('example-client');
486
-
487
- sftp.connect(config)
488
- .then(() => {
489
- return sftp.cwd();
490
- })
491
- .then(p => {
492
- console.log(`Remote working directory is ${p}`);
493
- return sftp.end();
494
- })
495
- .catch(err => {
496
- console.log(`Error: ${err.message}`); // error message will include 'example-client'
497
- });
498
-
499
-
500
- <a id="orgfc82313"></a>
271
+ ```javascript
272
+ 'use strict';
273
+
274
+ const Client = require('ssh2-sftp-client');
275
+
276
+ const config = {
277
+ host: 'example.com',
278
+ username: 'donald',
279
+ password: 'my-secret'
280
+ };
281
+
282
+ const sftp = new Client('example-client');
283
+
284
+ sftp.connect(config)
285
+ .then(() => {
286
+ return sftp.cwd();
287
+ })
288
+ .then(p => {
289
+ console.log(`Remote working directory is ${p}`);
290
+ return sftp.end();
291
+ })
292
+ .catch(err => {
293
+ console.log(`Error: ${err.message}`); // error message will include 'example-client'
294
+ });
295
+ ```
501
296
 
502
- ### connect(config) ===> SFTPstream
297
+ ### connect(config) ===> SFTPstream<a id="sec-5-2-2"></a>
503
298
 
504
- Connect to an sftp server. Full documentation for connection options is
505
- available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
299
+ Connect to an sftp server. Full documentation for connection options is available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
506
300
 
507
301
  1. Connection Options
508
302
 
509
- This module is based on the excellent [SSH2](https://github.com/mscdex/ssh2#client) module. That module is a general SSH2
510
- client and server library and provides much more functionality than just SFTP
511
- connectivity. Many of the connect options provided by that module are less
512
- relevant for SFTP connections. It is recommended you keep the config options to
513
- the minimum needed and stick to the options listed in the `commonOpts` below.
303
+ This module is based on the excellent [SSH2](https://github.com/mscdex/ssh2#client) module. That module is a general SSH2 client and server library and provides much more functionality than just SFTP connectivity. Many of the connect options provided by that module are less relevant for SFTP connections. It is recommended you keep the config options to the minimum needed and stick to the options listed in the `commonOpts` below.
514
304
 
515
- The `retries`, `retry_factor` and `retry_minTimeout` options are not part of the
516
- SSH2 module. These are part of the configuration for the [retry](https://www.npmjs.com/package/retry) package and what
517
- is used to enable retrying of sftp connection attempts. See the documentation
518
- for that package for an explanation of these values.
305
+ The `retries`, `retry_factor` and `retry_minTimeout` options are not part of the SSH2 module. These are part of the configuration for the [retry](https://www.npmjs.com/package/retry) package and what is used to enable retrying of sftp connection attempts. See the documentation for that package for an explanation of these values.
519
306
 
520
- // common options
521
-
522
- let commonOpts {
523
- host: 'localhost', // string Hostname or IP of server.
524
- port: 22, // Port number of the server.
525
- forceIPv4: false, // boolean (optional) Only connect via IPv4 address
526
- forceIPv6: false, // boolean (optional) Only connect via IPv6 address
527
- username: 'donald', // string Username for authentication.
528
- password: 'borsch', // string Password for password-based user authentication
529
- agent: process.env.SSH_AGENT, // string - Path to ssh-agent's UNIX socket
530
- privateKey: fs.readFileSync('/path/to/key'), // Buffer or string that contains
531
- passphrase; 'a pass phrase', // string - For an encrypted private key
532
- readyTimeout: 20000, // integer How long (in ms) to wait for the SSH handshake
533
- strictVendor: true // boolean - Performs a strict server vendor check
534
- debug: myDebug // function - Set this to a function that receives a single
535
- // string argument to get detailed (local) debug information.
536
- retries: 2 // integer. Number of times to retry connecting
537
- retry_factor: 2 // integer. Time factor used to calculate time between retries
538
- retry_minTimeout: 2000 // integer. Minimum timeout between attempts
539
- };
540
-
541
- // rarely used options
542
-
543
- let advancedOpts {
544
- localAddress,
545
- localPort,
546
- hostHash,
547
- hostVerifier,
548
- agentForward,
549
- localHostname,
550
- localUsername,
551
- tryKeyboard,
552
- authHandler,
553
- keepaliveInterval,
554
- keepaliveCountMax,
555
- sock,
556
- algorithms,
557
- compress
558
- };
307
+ ```javascript
308
+ // common options
309
+
310
+ let commonOpts {
311
+ host: 'localhost', // string Hostname or IP of server.
312
+ port: 22, // Port number of the server.
313
+ forceIPv4: false, // boolean (optional) Only connect via IPv4 address
314
+ forceIPv6: false, // boolean (optional) Only connect via IPv6 address
315
+ username: 'donald', // string Username for authentication.
316
+ password: 'borsch', // string Password for password-based user authentication
317
+ agent: process.env.SSH_AGENT, // string - Path to ssh-agent's UNIX socket
318
+ privateKey: fs.readFileSync('/path/to/key'), // Buffer or string that contains
319
+ passphrase; 'a pass phrase', // string - For an encrypted private key
320
+ readyTimeout: 20000, // integer How long (in ms) to wait for the SSH handshake
321
+ strictVendor: true // boolean - Performs a strict server vendor check
322
+ debug: myDebug // function - Set this to a function that receives a single
323
+ // string argument to get detailed (local) debug information.
324
+ retries: 2 // integer. Number of times to retry connecting
325
+ retry_factor: 2 // integer. Time factor used to calculate time between retries
326
+ retry_minTimeout: 2000 // integer. Minimum timeout between attempts
327
+ };
328
+
329
+ // rarely used options
330
+
331
+ let advancedOpts {
332
+ localAddress,
333
+ localPort,
334
+ hostHash,
335
+ hostVerifier,
336
+ agentForward,
337
+ localHostname,
338
+ localUsername,
339
+ tryKeyboard,
340
+ authHandler,
341
+ keepaliveInterval,
342
+ keepaliveCountMax,
343
+ sock,
344
+ algorithms,
345
+ compress
346
+ };
347
+ ```
559
348
 
560
349
  2. Example Use
561
350
 
562
- sftp.connect({
563
- host: example.com,
564
- port: 22,
565
- username: 'donald',
566
- password: 'youarefired'
567
- });
568
-
569
-
570
- <a id="org423c72d"></a>
351
+ ```javascript
352
+ sftp.connect({
353
+ host: example.com,
354
+ port: 22,
355
+ username: 'donald',
356
+ password: 'youarefired'
357
+ });
358
+ ```
571
359
 
572
- ### list(path, pattern) ==> Array[object]
360
+ ### list(path, pattern) ==> Array[object]<a id="sec-5-2-3"></a>
573
361
 
574
- Retrieves a directory listing. This method returns a Promise, which once
575
- realised, returns an array of objects representing items in the remote
576
- directory.
362
+ Retrieves a directory listing. This method returns a Promise, which once realised, returns an array of objects representing items in the remote directory.
577
363
 
578
364
  - **path:** {String} Remote directory path
579
- - **pattern:** (optional) {string|RegExp} A pattern used to filter the items included in the returned
580
- array. Pattern can be a simple *glob*-style string or a regular
581
- expression. Defaults to `/.*/`.
365
+ - **pattern:** (optional) {string|RegExp} A pattern used to filter the items included in the returned array. Pattern can be a simple *glob*-style string or a regular expression. Defaults to `/.*/`.
582
366
 
583
367
  1. Example Use
584
368
 
585
- const Client = require('ssh2-sftp-client');
586
-
587
- const config = {
588
- host: 'example.com',
589
- port: 22,
590
- username: 'red-don',
591
- password: 'my-secret'
592
- };
593
-
594
- let sftp = new Client;
595
-
596
- sftp.connect(config)
597
- .then(() => {
598
- return sftp.list('/path/to/remote/dir');
599
- })
600
- .then(data => {
601
- console.log(data);
602
- })
603
- .then(() => {
604
- sftp.end();
605
- })
606
- .catch(err => {
607
- console.error(err.message);
608
- });
369
+ ```javascript
370
+ const Client = require('ssh2-sftp-client');
371
+
372
+ const config = {
373
+ host: 'example.com',
374
+ port: 22,
375
+ username: 'red-don',
376
+ password: 'my-secret'
377
+ };
378
+
379
+ let sftp = new Client;
380
+
381
+ sftp.connect(config)
382
+ .then(() => {
383
+ return sftp.list('/path/to/remote/dir');
384
+ })
385
+ .then(data => {
386
+ console.log(data);
387
+ })
388
+ .then(() => {
389
+ sftp.end();
390
+ })
391
+ .catch(err => {
392
+ console.error(err.message);
393
+ });
394
+ ```
609
395
 
610
396
  2. Return Objects
611
397
 
612
398
  The objects in the array returned by `list()` have the following properties;
613
399
 
614
- {
615
- type: // file type(-, d, l)
616
- name: // file name
617
- size: // file size
618
- modifyTime: // file timestamp of modified time
619
- accessTime: // file timestamp of access time
620
- rights: {
621
- user:
622
- group:
623
- other:
624
- },
625
- owner: // user ID
626
- group: // group ID
627
- }
400
+ ```javascript
401
+ {
402
+ type: // file type(-, d, l)
403
+ name: // file name
404
+ size: // file size
405
+ modifyTime: // file timestamp of modified time
406
+ accessTime: // file timestamp of access time
407
+ rights: {
408
+ user:
409
+ group:
410
+ other:
411
+ },
412
+ owner: // user ID
413
+ group: // group ID
414
+ }
415
+ ```
628
416
 
629
417
  3. Pattern Filter
630
418
 
631
- The filter options can be a regular expression (most powerful option) or a
632
- simple *glob*-like string where \* will match any number of characters, e.g.
419
+ The filter options can be a regular expression (most powerful option) or a simple *glob*-like string where \* will match any number of characters, e.g.
633
420
 
634
421
  foo* => foo, foobar, foobaz
635
422
  *bar => bar, foobar, tabbar
636
423
  *oo* => foo, foobar, look, book
637
424
 
638
- The *glob*-style matching is very simple. In most cases, you are best off using
639
- a real regular expression which will allow you to do more powerful matching and
640
- anchor matches to the beginning/end of the string etc.
641
-
425
+ The *glob*-style matching is very simple. In most cases, you are best off using a real regular expression which will allow you to do more powerful matching and anchor matches to the beginning/end of the string etc.
642
426
 
643
- <a id="orgea8fbfa"></a>
427
+ ### exists(path) ==> boolean<a id="sec-5-2-4"></a>
644
428
 
645
- ### exists(path) ==> boolean
646
-
647
- Tests to see if remote file or directory exists. Returns type of remote object
648
- if it exists or false if it does not.
429
+ Tests to see if remote file or directory exists. Returns type of remote object if it exists or false if it does not.
649
430
 
650
431
  1. Example Use
651
432
 
652
- const Client = require('ssh2-sftp-client');
653
-
654
- const config = {
655
- host: 'example.com',
656
- port: 22,
657
- username: 'red-don',
658
- password: 'my-secret'
659
- };
660
-
661
- let sftp = new Client;
662
-
663
- sftp.connect(config)
664
- .then(() => {
665
- return sftp.exists('/path/to/remote/dir');
666
- })
667
- .then(data => {
668
- console.log(data); // will be false or d, -, l (dir, file or link)
669
- })
670
- .then(() => {
671
- sftp.end();
672
- })
673
- .catch(err => {
674
- console.error(err.message);
675
- });
676
-
677
-
678
- <a id="orgeb61168"></a>
679
-
680
- ### stat(path) ==> object
433
+ ```javascript
434
+ const Client = require('ssh2-sftp-client');
435
+
436
+ const config = {
437
+ host: 'example.com',
438
+ port: 22,
439
+ username: 'red-don',
440
+ password: 'my-secret'
441
+ };
442
+
443
+ let sftp = new Client;
444
+
445
+ sftp.connect(config)
446
+ .then(() => {
447
+ return sftp.exists('/path/to/remote/dir');
448
+ })
449
+ .then(data => {
450
+ console.log(data); // will be false or d, -, l (dir, file or link)
451
+ })
452
+ .then(() => {
453
+ sftp.end();
454
+ })
455
+ .catch(err => {
456
+ console.error(err.message);
457
+ });
458
+ ```
459
+
460
+ ### stat(path) ==> object<a id="sec-5-2-5"></a>
681
461
 
682
462
  Returns the attributes associated with the object pointed to by `path`.
683
463
 
@@ -687,204 +467,180 @@ Returns the attributes associated with the object pointed to by `path`.
687
467
 
688
468
  The `stat()` method returns an object with the following properties;
689
469
 
690
- let stats = {
691
- mode: 33279, // integer representing type and permissions
692
- uid: 1000, // user ID
693
- gid: 985, // group ID
694
- size: 5, // file size
695
- accessTime: 1566868566000, // Last access time. milliseconds
696
- modifyTime: 1566868566000, // last modify time. milliseconds
697
- isDirectory: false, // true if object is a directory
698
- isFile: true, // true if object is a file
699
- isBlockDevice: false, // true if object is a block device
700
- isCharacterDevice: false, // true if object is a character device
701
- isSymbolicLink: false, // true if object is a symbolic link
702
- isFIFO: false, // true if object is a FIFO
703
- isSocket: false // true if object is a socket
704
- };
470
+ ```javascript
471
+ let stats = {
472
+ mode: 33279, // integer representing type and permissions
473
+ uid: 1000, // user ID
474
+ gid: 985, // group ID
475
+ size: 5, // file size
476
+ accessTime: 1566868566000, // Last access time. milliseconds
477
+ modifyTime: 1566868566000, // last modify time. milliseconds
478
+ isDirectory: false, // true if object is a directory
479
+ isFile: true, // true if object is a file
480
+ isBlockDevice: false, // true if object is a block device
481
+ isCharacterDevice: false, // true if object is a character device
482
+ isSymbolicLink: false, // true if object is a symbolic link
483
+ isFIFO: false, // true if object is a FIFO
484
+ isSocket: false // true if object is a socket
485
+ };
486
+ ```
705
487
 
706
488
  2. Example Use
707
489
 
708
- let client = new Client();
709
-
710
- client.connect(config)
711
- .then(() => {
712
- return client.stat('/path/to/remote/file');
713
- })
714
- .then(data => {
715
- // do something with data
716
- })
717
- .then(() => {
718
- client.end();
719
- })
720
- .catch(err => {
721
- console.error(err.message);
722
- });
723
-
724
-
725
- <a id="orga2d8770"></a>
726
-
727
- ### get(path, dst, options) ==> String|Stream|Buffer
728
-
729
- Retrieve a file from a remote SFTP server. The `dst` argument defines the
730
- destination and can be either a string, a stream object or undefined. If it is a
731
- string, it is interpreted as the path to a location on the local file system
732
- (path should include the file name). If it is a stream object, the remote data
733
- is passed to it via a call to pipe(). If `dst` is undefined, the method will put
734
- the data into a buffer and return that buffer when the Promise is resolved. If
735
- `dst` is defined, it is returned when the Promise is resolved.
736
-
737
- In general, if your going to pass in a string as the destination, you are
738
- better off using the `fastGet()` method.
490
+ ```javascript
491
+ let client = new Client();
492
+
493
+ client.connect(config)
494
+ .then(() => {
495
+ return client.stat('/path/to/remote/file');
496
+ })
497
+ .then(data => {
498
+ // do something with data
499
+ })
500
+ .then(() => {
501
+ client.end();
502
+ })
503
+ .catch(err => {
504
+ console.error(err.message);
505
+ });
506
+ ```
507
+
508
+ ### get(path, dst, options) ==> String|Stream|Buffer<a id="sec-5-2-6"></a>
509
+
510
+ Retrieve a file from a remote SFTP server. The `dst` argument defines the destination and can be either a string, a stream object or undefined. If it is a string, it is interpreted as the path to a location on the local file system (path should include the file name). If it is a stream object, the remote data is passed to it via a call to pipe(). If `dst` is undefined, the method will put the data into a buffer and return that buffer when the Promise is resolved. If `dst` is defined, it is returned when the Promise is resolved.
511
+
512
+ In general, if your going to pass in a string as the destination, you are better off using the `fastGet()` method.
739
513
 
740
514
  - **path:** String. Path to the remote file to download
741
- - **dst:** String|Stream. Destination for the data. If a string, it
742
- should be a local file path.
515
+ - **dst:** String|Stream. Destination for the data. If a string, it should be a local file path.
743
516
  - **options:** Options for the `get()` command (see below).
744
517
 
745
518
  1. Options
746
519
 
747
- The options object can be used to pass options to the underlying readStream used
748
- to read the data from the remote server.
520
+ The options object can be used to pass options to the underlying readStream used to read the data from the remote server.
749
521
 
750
- {
751
- flags: 'r',
752
- encoding: null,
753
- handle: null,
754
- mode: 0o666,
755
- autoClose: true
756
- }
522
+ ```javascript
523
+ {
524
+ flags: 'r',
525
+ encoding: null,
526
+ handle: null,
527
+ mode: 0o666,
528
+ autoClose: true
529
+ }
530
+ ```
757
531
 
758
- Most of the time, you won't want to use any options. Sometimes, it may be useful
759
- to set the encoding. For example, to 'utf-8'. However, it is important not to do
760
- this for binary files to avoid data corruption.
532
+ Most of the time, you won't want to use any options. Sometimes, it may be useful to set the encoding. For example, to 'utf-8'. However, it is important not to do this for binary files to avoid data corruption.
761
533
 
762
534
  2. Example Use
763
535
 
764
- let client = new Client();
765
-
766
- let remotePath = '/remote/server/path/file.txt';
767
- let dst = fs.createWriteStream('/local/file/path/copy.txt');
768
-
769
- client.connect(config)
770
- .then(() => {
771
- return client.get(remotePath, dst);
772
- })
773
- .then(() => {
774
- client.end();
775
- })
776
- .catch(err => {
777
- console.error(err.message);
778
- });
536
+ ```javascript
537
+ let client = new Client();
779
538
 
780
- - **Tip:** See examples file in the Git repository for more examples. You can pass
781
- any writeable stream in as the destination. For example, if you pass in
782
- `zlib.createGunzip()` writeable stream, you can both download and
783
- decompress a gzip file 'on the fly'.
784
-
785
-
786
- <a id="orgd7fae2f"></a>
539
+ let remotePath = '/remote/server/path/file.txt';
540
+ let dst = fs.createWriteStream('/local/file/path/copy.txt');
541
+
542
+ client.connect(config)
543
+ .then(() => {
544
+ return client.get(remotePath, dst);
545
+ })
546
+ .then(() => {
547
+ client.end();
548
+ })
549
+ .catch(err => {
550
+ console.error(err.message);
551
+ });
552
+ ```
553
+
554
+ - **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'.
787
555
 
788
- ### fastGet(remotePath, localPath, options) ===> string
556
+ ### fastGet(remotePath, localPath, options) ===> string<a id="sec-5-2-7"></a>
789
557
 
790
- Downloads a file at remotePath to localPath using parallel reads for faster
791
- throughput. This is the simplest method if you just want to download a file.
558
+ Downloads a file at remotePath to localPath using parallel reads for faster throughput. This is the simplest method if you just want to download a file.
792
559
 
793
560
  - **remotePath:** String. Path to the remote file to download
794
- - **localPath:** String. Path on local file system for the downloaded file. The
795
- local path should include the filename to use for saving the
796
- file.
561
+ - **localPath:** String. Path on local file system for the downloaded file. The local path should include the filename to use for saving the file.
797
562
  - **options:** Options for `fastGet()` (see below)
798
563
 
799
564
  1. Options
800
565
 
801
- {
802
- concurrency: 64, // integer. Number of concurrent reads to use
803
- chunkSize: 32768, // integer. Size of each read in bytes
804
- step: function(total_transferred, chunk, total) // callback called each time a
805
- // chunk is transferred
806
- }
566
+ ```javascript
567
+ {
568
+ concurrency: 64, // integer. Number of concurrent reads to use
569
+ chunkSize: 32768, // integer. Size of each read in bytes
570
+ step: function(total_transferred, chunk, total) // callback called each time a
571
+ // chunk is transferred
572
+ }
573
+ ```
807
574
 
808
- - **Warning:** Some servers do not respond correctly to requests to alter chunk
809
- size. This can result in lost or corrupted data.
575
+ - **Warning:** Some servers do not respond correctly to requests to alter chunk size. This can result in lost or corrupted data.
810
576
 
811
577
  2. Sample Use
812
578
 
813
- let client = new Client();
814
- let remotePath = '/server/path/file.txt';
815
- let localPath = '/local/path/file.txt';
816
-
817
- client.connect(config)
818
- .then(() => {
819
- client.fastGet(remotePath, localPath);
820
- })
821
- .then(() => {
822
- client.end();
823
- })
824
- .catch(err => {
825
- console.error(err.message);
826
- });
827
-
828
-
829
- <a id="orgfea8ec2"></a>
830
-
831
- ### put(src, remotePath, options) ==> string
832
-
833
- Upload data from local system to remote server. If the `src` argument is a
834
- string, it is interpreted as a local file path to be used for the data to
835
- transfer. If the `src` argument is a buffer, the contents of the buffer are
836
- copied to the remote file and if it is a readable stream, the contents of that
837
- stream are piped to the `remotePath` on the server.
838
-
839
- - **src:** string | buffer | readable stream. Data source for data to copy to the
840
- remote server.
579
+ ```javascript
580
+ let client = new Client();
581
+ let remotePath = '/server/path/file.txt';
582
+ let localPath = '/local/path/file.txt';
583
+
584
+ client.connect(config)
585
+ .then(() => {
586
+ client.fastGet(remotePath, localPath);
587
+ })
588
+ .then(() => {
589
+ client.end();
590
+ })
591
+ .catch(err => {
592
+ console.error(err.message);
593
+ });
594
+ ```
595
+
596
+ ### put(src, remotePath, options) ==> string<a id="sec-5-2-8"></a>
597
+
598
+ Upload data from local system to remote server. If the `src` argument is a string, it is interpreted as a local file path to be used for the data to transfer. If the `src` argument is a buffer, the contents of the buffer are copied to the remote file and if it is a readable stream, the contents of that stream are piped to the `remotePath` on the server.
599
+
600
+ - **src:** string | buffer | readable stream. Data source for data to copy to the remote server.
841
601
  - **remotePath:** string. Path to the remote file to be created on the server.
842
- - **options:** object. Options which can be passed to adjust the write stream used
843
- in sending the data to the remote server (see below).
602
+ - **options:** object. Options which can be passed to adjust the write stream used in sending the data to the remote server (see below).
844
603
 
845
604
  1. Options
846
605
 
847
606
  The following options are supported;
848
607
 
849
- {
850
- flags: 'w', // w - write and a - append
851
- encoding: null, // use null for binary files
852
- mode: 0o666, // mode to use for created file (rwx)
853
- autoClose: true // automatically close the write stream when finished
854
- }
608
+ ```javascript
609
+ {
610
+ flags: 'w', // w - write and a - append
611
+ encoding: null, // use null for binary files
612
+ mode: 0o666, // mode to use for created file (rwx)
613
+ autoClose: true // automatically close the write stream when finished
614
+ }
615
+ ```
855
616
 
856
- The most common options to use are mode and encoding. The values shown above are
857
- the defaults. You do not have to set encoding to utf-8 for text files, null is
858
- fine for all file types. However, using utf-8 encoding for binary files will
859
- often result in data corruption.
617
+ The most common options to use are mode and encoding. The values shown above are the defaults. You do not have to set encoding to utf-8 for text files, null is fine for all file types. However, using utf-8 encoding for binary files will often result in data corruption.
860
618
 
861
619
  2. Example Use
862
620
 
863
- let client = new Client();
864
-
865
- let data = fs.createReadStream('/path/to/local/file.txt');
866
- let remote = '/path/to/remote/file.txt';
867
-
868
- client.connect(config)
869
- .then(() => {
870
- return client.put(data, remote);
871
- })
872
- .then(() => {
873
- return client.end();
874
- })
875
- .catch(err => {
876
- console.error(err.message);
877
- });
621
+ ```javascript
622
+ let client = new Client();
623
+
624
+ let data = fs.createReadStream('/path/to/local/file.txt');
625
+ let remote = '/path/to/remote/file.txt';
626
+
627
+ client.connect(config)
628
+ .then(() => {
629
+ return client.put(data, remote);
630
+ })
631
+ .then(() => {
632
+ return client.end();
633
+ })
634
+ .catch(err => {
635
+ console.error(err.message);
636
+ });
637
+ ```
878
638
 
879
639
  - **Tip:** If the src argument is a path string, consider just using `fastPut()`.
880
640
 
641
+ ### fastPut(localPath, remotePath, options) ==> string<a id="sec-5-2-9"></a>
881
642
 
882
- <a id="orgab8a35f"></a>
883
-
884
- ### fastPut(localPath, remotePath, options) ==> string
885
-
886
- Uploads the data in file at `localPath` to a new file on remote server at
887
- `remotePath` using concurrency. The options object allows tweaking of the fast put process.
643
+ Uploads the data in file at `localPath` to a new file on remote server at `remotePath` using concurrency. The options object allows tweaking of the fast put process.
888
644
 
889
645
  - **localPath:** string. Path to local file to upload
890
646
  - **remotePath:** string. Path to remote file to create
@@ -892,44 +648,40 @@ Uploads the data in file at `localPath` to a new file on remote server at
892
648
 
893
649
  1. Options
894
650
 
895
- {
896
- concurrency: 64, // integer. Number of concurrent reads
897
- chunkSize: 32768, // integer. Size of each read in bytes
898
- mode: 0o755, // mixed. Integer or string representing the file mode to set
899
- step: function(total_transferred, chunk, total) // function. Called every time
900
- // a part of a file was transferred
901
- }
651
+ ```javascript
652
+ {
653
+ concurrency: 64, // integer. Number of concurrent reads
654
+ chunkSize: 32768, // integer. Size of each read in bytes
655
+ mode: 0o755, // mixed. Integer or string representing the file mode to set
656
+ step: function(total_transferred, chunk, total) // function. Called every time
657
+ // a part of a file was transferred
658
+ }
659
+ ```
902
660
 
903
- - **Warning:** There have been reports that some SFTP servers will not honour
904
- requests for non-default chunk sizes. This can result in data loss
905
- or corruption.
661
+ - **Warning:** There have been reports that some SFTP servers will not honour requests for non-default chunk sizes. This can result in data loss or corruption.
906
662
 
907
663
  2. Example Use
908
664
 
909
- let localFile = '/path/to/file.txt';
910
- let remoteFile = '/path/to/remote/file.txt';
911
- let client = new Client();
912
-
913
- client.connect(config)
914
- .then(() => {
915
- client.fastPut(localFile, remoteFile);
916
- })
917
- .then(() => {
918
- client.end();
919
- })
920
- .catch(err => {
921
- console.error(err.message);
922
- });
923
-
924
-
925
- <a id="org789e7fe"></a>
665
+ ```javascript
666
+ let localFile = '/path/to/file.txt';
667
+ let remoteFile = '/path/to/remote/file.txt';
668
+ let client = new Client();
669
+
670
+ client.connect(config)
671
+ .then(() => {
672
+ client.fastPut(localFile, remoteFile);
673
+ })
674
+ .then(() => {
675
+ client.end();
676
+ })
677
+ .catch(err => {
678
+ console.error(err.message);
679
+ });
680
+ ```
926
681
 
927
- ### append(input, remotePath, options) ==> string
682
+ ### append(input, remotePath, options) ==> string<a id="sec-5-2-10"></a>
928
683
 
929
- Append the `input` data to an existing remote file. There is no integrity
930
- checking performed apart from normal writeStream checks. This function simply
931
- opens a writeStream on the remote file in append mode and writes the data passed
932
- in to the file.
684
+ Append the `input` data to an existing remote file. There is no integrity checking performed apart from normal writeStream checks. This function simply opens a writeStream on the remote file in append mode and writes the data passed in to the file.
933
685
 
934
686
  - **input:** buffer | readStream. Data to append to remote file
935
687
  - **remotePath:** string. Path to remote file
@@ -939,96 +691,86 @@ in to the file.
939
691
 
940
692
  The following options are supported;
941
693
 
942
- {
943
- flags: 'a', // w - write and a - append
944
- encoding: null, // use null for binary files
945
- mode: 0o666, // mode to use for created file (rwx)
946
- autoClose: true // automatically close the write stream when finished
947
- }
694
+ ```javascript
695
+ {
696
+ flags: 'a', // w - write and a - append
697
+ encoding: null, // use null for binary files
698
+ mode: 0o666, // mode to use for created file (rwx)
699
+ autoClose: true // automatically close the write stream when finished
700
+ }
701
+ ```
948
702
 
949
- The most common options to use are mode and encoding. The values shown above are
950
- the defaults. You do not have to set encoding to utf-8 for text files, null is
951
- fine for all file types. Generally, I would not attempt to append binary files.
703
+ The most common options to use are mode and encoding. The values shown above are the defaults. You do not have to set encoding to utf-8 for text files, null is fine for all file types. Generally, I would not attempt to append binary files.
952
704
 
953
705
  2. Example Use
954
706
 
955
- let remotePath = '/path/to/remote/file.txt';
956
- let client = new Client();
957
-
958
- client.connect(config)
959
- .then(() => {
960
- return client.append(Buffer.from('Hello world'), remotePath);
961
- })
962
- .then(() => {
963
- return client.end();
964
- })
965
- .catch(err => {
966
- console.error(err.message);
967
- });
968
-
969
-
970
- <a id="org9e8c8c6"></a>
707
+ ```javascript
708
+ let remotePath = '/path/to/remote/file.txt';
709
+ let client = new Client();
710
+
711
+ client.connect(config)
712
+ .then(() => {
713
+ return client.append(Buffer.from('Hello world'), remotePath);
714
+ })
715
+ .then(() => {
716
+ return client.end();
717
+ })
718
+ .catch(err => {
719
+ console.error(err.message);
720
+ });
721
+ ```
971
722
 
972
- ### mkdir(path, recursive) ==> string
723
+ ### mkdir(path, recursive) ==> string<a id="sec-5-2-11"></a>
973
724
 
974
- Create a new directory. If the recursive flag is set to true, the method will
975
- create any directories in the path which do not already exist. Recursive flag
976
- defaults to false.
725
+ Create a new directory. If the recursive flag is set to true, the method will create any directories in the path which do not already exist. Recursive flag defaults to false.
977
726
 
978
727
  - **path:** string. Path to remote directory to create
979
- - **recursive:** boolean. If true, create any missing directories in the path as
980
- well
728
+ - **recursive:** boolean. If true, create any missing directories in the path as well
981
729
 
982
730
  1. Example Use
983
731
 
984
- let remoteDir = '/path/to/new/dir';
985
- let client = new Client();
986
-
987
- client.connect(config)
988
- .then(() => {
989
- return client.mkdir(remoteDir, true);
990
- })
991
- .then(() => {
992
- return client.end();
993
- })
994
- .catch(err => {
995
- console.error(err.message);
996
- });
997
-
998
-
999
- <a id="org0edeb97"></a>
732
+ ```javascript
733
+ let remoteDir = '/path/to/new/dir';
734
+ let client = new Client();
735
+
736
+ client.connect(config)
737
+ .then(() => {
738
+ return client.mkdir(remoteDir, true);
739
+ })
740
+ .then(() => {
741
+ return client.end();
742
+ })
743
+ .catch(err => {
744
+ console.error(err.message);
745
+ });
746
+ ```
1000
747
 
1001
- ### rmdir(path, recursive) ==> string
748
+ ### rmdir(path, recursive) ==> string<a id="sec-5-2-12"></a>
1002
749
 
1003
- Remove a directory. If removing a directory and recursive flag is set to
1004
- `true`, the specified directory and all sub-directories and files will be
1005
- deleted. If set to false and the directory has sub-directories or files, the
1006
- action will fail.
750
+ Remove a directory. If removing a directory and recursive flag is set to `true`, the specified directory and all sub-directories and files will be deleted. If set to false and the directory has sub-directories or files, the action will fail.
1007
751
 
1008
752
  - **path:** string. Path to remote directory
1009
- - **recursive:** boolean. If true, remove all files and directories in target
1010
- directory. Defaults to false
753
+ - **recursive:** boolean. If true, remove all files and directories in target directory. Defaults to false
1011
754
 
1012
755
  1. Example Use
1013
756
 
1014
- let remoteDir = '/path/to/remote/dir';
1015
- let client = new Client();
1016
-
1017
- client.connect(config)
1018
- .then(() => {
1019
- return client.rmdir(remoteDir, true);
1020
- })
1021
- .then(() => {
1022
- return client.end();
1023
- })
1024
- .catch(err => {
1025
- console.error(err.message);
1026
- });
1027
-
1028
-
1029
- <a id="org81d6ffe"></a>
757
+ ```javascript
758
+ let remoteDir = '/path/to/remote/dir';
759
+ let client = new Client();
760
+
761
+ client.connect(config)
762
+ .then(() => {
763
+ return client.rmdir(remoteDir, true);
764
+ })
765
+ .then(() => {
766
+ return client.end();
767
+ })
768
+ .catch(err => {
769
+ console.error(err.message);
770
+ });
771
+ ```
1030
772
 
1031
- ### delete(path) ==> string
773
+ ### delete(path) ==> string<a id="sec-5-2-13"></a>
1032
774
 
1033
775
  Delete a file on the remote server.
1034
776
 
@@ -1036,74 +778,39 @@ Delete a file on the remote server.
1036
778
 
1037
779
  1. Example Use
1038
780
 
1039
- let remoteFile = '/path/to/remote/file.txt';
1040
- let client = new Client();
1041
-
1042
- client.connect(config)
1043
- .then(() => {
1044
- return client.delete(remoteFile);
1045
- })
1046
- .then(() => {
1047
- return client.end();
1048
- })
1049
- .catch(err => {
1050
- console.error(err.message);
1051
- });
1052
-
1053
-
1054
- <a id="org2e85ca1"></a>
781
+ ```javascript
782
+ let remoteFile = '/path/to/remote/file.txt';
783
+ let client = new Client();
784
+
785
+ client.connect(config)
786
+ .then(() => {
787
+ return client.delete(remoteFile);
788
+ })
789
+ .then(() => {
790
+ return client.end();
791
+ })
792
+ .catch(err => {
793
+ console.error(err.message);
794
+ });
795
+ ```
1055
796
 
1056
- ### rename(fromPath, toPath) ==> string
797
+ ### rename(fromPath, toPath) ==> string<a id="sec-5-2-14"></a>
1057
798
 
1058
- Rename a file or directory from `fromPath` to `toPath`. You must have the
1059
- necessary permissions to modify the remote file.
799
+ Rename a file or directory from `fromPath` to `toPath`. You must have the necessary permissions to modify the remote file.
1060
800
 
1061
801
  - **fromPath:** string. Path to existing file to be renamed
1062
- - **toPath:** string. Path to new file existing file is to be renamed to. Should
1063
- not already exist.
802
+ - **toPath:** string. Path to new file existing file is to be renamed to. Should not already exist.
1064
803
 
1065
804
  1. Example Use
1066
805
 
1067
- let from = '/remote/path/to/old.txt';
1068
- let to = '/remote/path/to/new.txt';
1069
- let client = new Client();
1070
-
1071
- client.connect(config)
1072
- .then(() => {
1073
- return client.rename(from, to);
1074
- })
1075
- .then(() => {
1076
- return client.end();
1077
- })
1078
- .catch(err => {
1079
- console.error(err.message);
1080
- });
1081
-
1082
-
1083
- <a id="org4d21fb7"></a>
1084
-
1085
- ### posixRename(fromPath, toPath) ==> string
1086
-
1087
- This method uses the openssh POSIX rename extension introduced in OpenSSH 4.8.
1088
- The advantage of this version of rename over standard SFTP rename is that it is
1089
- an atomic operation and will allow renaming a resource where the destination
1090
- name exists. The POSIX rename will also work on some filesystems which do not
1091
- support standard SFTP rename because they don't support the system hardlink()
1092
- call. The POSIX rename extension is available on all openSSH servers from 4.8
1093
- and some other implementations. This is an extension to the standard SFTP
1094
- protocol and therefore is not supported on all sSFTP servers.
1095
-
1096
- - **fromPath:** string. Path to existing file to be renamed.
1097
- - **toPath:** string. Path for new name. If it already exists, it will be replaced
1098
- by file specified in fromPath
1099
-
806
+ ```javascript
1100
807
  let from = '/remote/path/to/old.txt';
1101
808
  let to = '/remote/path/to/new.txt';
1102
809
  let client = new Client();
1103
810
 
1104
811
  client.connect(config)
1105
812
  .then(() => {
1106
- return client.posixRename(from, to);
813
+ return client.rename(from, to);
1107
814
  })
1108
815
  .then(() => {
1109
816
  return client.end();
@@ -1111,595 +818,456 @@ protocol and therefore is not supported on all sSFTP servers.
1111
818
  .catch(err => {
1112
819
  console.error(err.message);
1113
820
  });
821
+ ```
1114
822
 
823
+ ### posixRename(fromPath, toPath) ==> string<a id="sec-5-2-15"></a>
1115
824
 
1116
- <a id="org91257cd"></a>
1117
-
1118
- ### chmod(path, mode) ==> string
825
+ This method uses the openssh POSIX rename extension introduced in OpenSSH 4.8. The advantage of this version of rename over standard SFTP rename is that it is an atomic operation and will allow renaming a resource where the destination name exists. The POSIX rename will also work on some filesystems which do not support standard SFTP rename because they don't support the system hardlink() call. The POSIX rename extension is available on all openSSH servers from 4.8 and some other implementations. This is an extension to the standard SFTP protocol and therefore is not supported on all sSFTP servers.
1119
826
 
1120
- Change the mode (read, write or execute permissions) of a remote file or
1121
- directory.
827
+ - **fromPath:** string. Path to existing file to be renamed.
828
+ - **toPath:** string. Path for new name. If it already exists, it will be replaced by file specified in fromPath
1122
829
 
1123
- - **path:** string. Path to the remote file or directory
1124
- - **mode:** octal. New mode to set for the remote file or directory
830
+ ```javascript
831
+ let from = '/remote/path/to/old.txt';
832
+ let to = '/remote/path/to/new.txt';
833
+ let client = new Client();
1125
834
 
1126
- 1. Example Use
835
+ client.connect(config)
836
+ .then(() => {
837
+ return client.posixRename(from, to);
838
+ })
839
+ .then(() => {
840
+ return client.end();
841
+ })
842
+ .catch(err => {
843
+ console.error(err.message);
844
+ });
845
+ ```
1127
846
 
1128
- let path = '/path/to/remote/file.txt';
1129
- let ndwMode = 0o644; // rw-r-r
1130
- let client = new Client();
1131
-
1132
- client.connect(config)
1133
- .then(() => {
1134
- return client.chmod(path, newMode);
1135
- })
1136
- .then(() => {
1137
- return client.end();
1138
- })
1139
- .catch(err => {
1140
- console.error(err.message);
1141
- });
847
+ ### chmod(path, mode) ==> string<a id="sec-5-2-16"></a>
1142
848
 
849
+ Change the mode (read, write or execute permissions) of a remote file or directory.
1143
850
 
1144
- <a id="org6b2b3f8"></a>
851
+ - **path:** string. Path to the remote file or directory
852
+ - **mode:** octal. New mode to set for the remote file or directory
1145
853
 
1146
- ### realPath(path) ===> string
854
+ 1. Example Use
1147
855
 
1148
- Converts a relative path to an absolute path on the remote server. This method
1149
- is mainly used internally to resolve remote path names. Returns '' if the
1150
- path is not valid.
856
+ ```javascript
857
+ let path = '/path/to/remote/file.txt';
858
+ let ndwMode = 0o644; // rw-r-r
859
+ let client = new Client();
860
+
861
+ client.connect(config)
862
+ .then(() => {
863
+ return client.chmod(path, newMode);
864
+ })
865
+ .then(() => {
866
+ return client.end();
867
+ })
868
+ .catch(err => {
869
+ console.error(err.message);
870
+ });
871
+ ```
1151
872
 
1152
- - **path:** A file path, either relative or absolute. Can handle '.' and '..', but
1153
- does not expand '~'.
873
+ ### realPath(path) ===> string<a id="sec-5-2-17"></a>
1154
874
 
875
+ Converts a relative path to an absolute path on the remote server. This method is mainly used internally to resolve remote path names. Returns '' if the path is not valid.
1155
876
 
1156
- <a id="orgae34ed3"></a>
877
+ - **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
1157
878
 
1158
- ### cwd() ==> string
879
+ ### cwd() ==> string<a id="sec-5-2-18"></a>
1159
880
 
1160
881
  Returns what the server believes is the current remote working directory.
1161
882
 
883
+ ### uploadDir(srcDir, dstDir) ==> string<a id="sec-5-2-19"></a>
1162
884
 
1163
- <a id="org6e5e031"></a>
1164
-
1165
- ### uploadDir(srcDir, dstDir) ==> string
1166
-
1167
- Upload the directory specified by `srcDir` to the remote directory specified by
1168
- `dstDir`. The `dstDir` will be created if necessary. Any sub directories within
1169
- `srcDir` will also be uploaded. Any existing files in the remote path will be
1170
- overwritten.
885
+ Upload the directory specified by `srcDir` to the remote directory specified by `dstDir`. The `dstDir` will be created if necessary. Any sub directories within `srcDir` will also be uploaded. Any existing files in the remote path will be overwritten.
1171
886
 
1172
- The upload process also emits 'upload' events. These events are fired for each
1173
- successfully uploaded file. The `upload` event calls listeners with 1 argument,
1174
- an object which has properties source and destination. The source property is
1175
- the path of the file uploaded and the destination property is the path to where
1176
- the file was uploaded to. The purpose of this event is to provide some way for
1177
- client code to get feedback on the upload progress. You can add your own lisener
1178
- using the `on()` method.
887
+ The upload process also emits 'upload' events. These events are fired for each successfully uploaded file. The `upload` event calls listeners with 1 argument, an object which has properties source and destination. The source property is the path of the file uploaded and the destination property is the path to where the file was uploaded to. The purpose of this event is to provide some way for client code to get feedback on the upload progress. You can add your own lisener using the `on()` method.
1179
888
 
1180
889
  - **srcDir:** A local file path specified as a string
1181
890
  - **dstDir:** A remote file path specified as a string
1182
891
 
1183
892
  1. Example
1184
893
 
1185
- 'use strict';
1186
-
1187
- // Example of using the uploadDir() method to upload a directory
1188
- // to a remote SFTP server
1189
-
1190
- const path = require('path');
1191
- const SftpClient = require('../src/index');
1192
-
1193
- const dotenvPath = path.join(__dirname, '..', '.env');
1194
- require('dotenv').config({path: dotenvPath});
1195
-
1196
- const config = {
1197
- host: process.env.SFTP_SERVER,
1198
- username: process.env.SFTP_USER,
1199
- password: process.env.SFTP_PASSWORD,
1200
- port: process.env.SFTP_PORT || 22
1201
- };
1202
-
1203
- async function main() {
1204
- const client = new SftpClient('upload-test');
1205
- const src = path.join(__dirname, '..', 'test', 'testData', 'upload-src');
1206
- const dst = '/home/tim/upload-test';
1207
-
1208
- try {
1209
- await client.connect(config);
1210
- client.on('upload', info => {
1211
- console.log(`Listener: Uploaded ${info.source}`);
1212
- });
1213
- let rslt = await client.uploadDir(src, dst);
1214
- return rslt;
1215
- } finally {
1216
- client.end();
1217
- }
1218
- }
1219
-
1220
- main()
1221
- .then(msg => {
1222
- console.log(msg);
1223
- })
1224
- .catch(err => {
1225
- console.log(`main error: ${err.message}`);
1226
- });
1227
-
1228
-
1229
- <a id="org751c9af"></a>
1230
-
1231
- ### downloadDir(srcDir, dstDir) ==> string
1232
-
1233
- Download the remote directory specified by `srcDir` to the local file system
1234
- directory specified by `dstDir`. The `dstDir` directory will be created if
1235
- required. All sub directories within `srcDir` will also be copied. Any existing
1236
- files in the local path will be overwritten. No files in the local path will be
1237
- deleted.
1238
-
1239
- The method also emites `download` events to provide a way to monitor download
1240
- progress. The download event listener is called with one argument, an object
1241
- with two properties, source and destination. The source property is the path to
1242
- the remote file that has been downloaded and the destination is the local path
1243
- to where the file was downloaded to. You can add a listener for this event using
1244
- the `on()` method.
1245
-
1246
- - **srcDir:** A remote file path specified as a string
1247
- - **dstDir:** A local file path specified as a string
1248
-
1249
- 1. Example
1250
-
894
+ ```javascript
1251
895
  'use strict';
1252
-
1253
- // Example of using the downloadDir() method to upload a directory
896
+
897
+ // Example of using the uploadDir() method to upload a directory
1254
898
  // to a remote SFTP server
1255
-
899
+
1256
900
  const path = require('path');
1257
901
  const SftpClient = require('../src/index');
1258
-
902
+
1259
903
  const dotenvPath = path.join(__dirname, '..', '.env');
1260
904
  require('dotenv').config({path: dotenvPath});
1261
-
905
+
1262
906
  const config = {
1263
- host: process.env.SFTP_SERVER,
1264
- username: process.env.SFTP_USER,
1265
- password: process.env.SFTP_PASSWORD,
1266
- port: process.env.SFTP_PORT || 22
907
+ host: process.env.SFTP_SERVER,
908
+ username: process.env.SFTP_USER,
909
+ password: process.env.SFTP_PASSWORD,
910
+ port: process.env.SFTP_PORT || 22
1267
911
  };
1268
-
912
+
1269
913
  async function main() {
1270
- const client = new SftpClient('upload-test');
1271
- const dst = '/tmp';
1272
- const src = '/home/tim/upload-test';
1273
-
1274
- try {
1275
- await client.connect(config);
1276
- client.on('download', info => {
1277
- console.log(`Listener: Download ${info.source}`);
1278
- });
1279
- let rslt = await client.downloadDir(src, dst);
1280
- return rslt;
1281
- } finally {
1282
- client.end();
1283
- }
914
+ const client = new SftpClient('upload-test');
915
+ const src = path.join(__dirname, '..', 'test', 'testData', 'upload-src');
916
+ const dst = '/home/tim/upload-test';
917
+
918
+ try {
919
+ await client.connect(config);
920
+ client.on('upload', info => {
921
+ console.log(`Listener: Uploaded ${info.source}`);
922
+ });
923
+ let rslt = await client.uploadDir(src, dst);
924
+ return rslt;
925
+ } finally {
926
+ client.end();
927
+ }
1284
928
  }
1285
-
929
+
1286
930
  main()
1287
- .then(msg => {
1288
- console.log(msg);
1289
- })
1290
- .catch(err => {
1291
- console.log(`main error: ${err.message}`);
1292
- });
1293
-
1294
-
1295
- <a id="orgfbea0b9"></a>
1296
-
1297
- ### end() ==> boolean
1298
-
1299
- Ends the current client session, releasing the client socket and associated
1300
- resources. This function also removes all listeners associated with the client.
1301
-
1302
- 1. Example Use
1303
-
1304
- let client = new Client();
1305
-
1306
- client.connect(config)
1307
- .then(() => {
1308
- // do some sftp stuff
1309
- })
1310
- .then(() => {
1311
- return client.end();
1312
- })
1313
- .catch(err => {
1314
- console.error(err.message);
1315
- });
1316
-
1317
-
1318
- <a id="orgcd064ac"></a>
1319
-
1320
- ### Add and Remove Listeners
1321
-
1322
- Although normally not required, you can add and remove custom listeners on the
1323
- ssh2 client object. This object supports a number of events, but only a few of
1324
- them have any meaning in the context of SFTP. These are
1325
-
1326
- - **error:** An error occurred. Calls listener with an error argument.
1327
- - **end:** The socket has been disconnected. No argument.
1328
- - **close:** The socket was closed. Boolean argument which is true when the socket
1329
- was closed due to errors.
1330
-
1331
- 1. on(eventType, listener)
1332
-
1333
- Adds the specified listener to the specified event type. It the event type is
1334
- `error`, the listener should accept 1 argument, which will be an Error object. If
1335
- the event type is `close`, the listener should accept one argument of a boolean
1336
- type, which will be true when the client connection was closed due to errors.
1337
-
1338
- 2. removeListener(eventType, listener)
1339
-
1340
- Removes the specified listener from the event specified in eventType. Note that
1341
- the `end()` method automatically removes all listeners from the client object.
1342
-
1343
-
1344
- <a id="org792a244"></a>
1345
-
1346
- # FAQ
1347
-
1348
-
1349
- <a id="org4a4dd6c"></a>
1350
-
1351
- ## Remote server drops connections with only an end event
1352
-
1353
- Many SFTP servers have rate limiting protection which will drop connections once
1354
- a limit has been reached. In particular, openSSH has the setting `MaxStartups`,
1355
- which can be a tuple of the form `max:drop:full` where `max` is the maximum
1356
- allowed unauthenticated connections, `drop` is a percentage value which
1357
- specifies percentage of connections to be dropped once `max` connections has
1358
- been reached and `full` is the number of connections at which point all
1359
- subsequent connections will be dropped. e.g. `10:30:60` means allow up to 10
1360
- unauthenticated connections after which drop 30% of connection attempts until
1361
- reaching 60 unauthenticated connections, at which time, drop all attempts.
1362
-
1363
- Clients first make an unauthenticated connection to the SFTP server to begin
1364
- negotiation of protocol settings (cipher, authentication method etc). If you are
1365
- creating multiple connections in a script, it is easy to exceed the limit,
1366
- resulting in some connections being dropped. As SSH2 only raises an 'end' event
1367
- for these dropped connections, no error is detected. The `ssh2-sftp-client` now
1368
- listens for `end` events during the connection process and if one is detected,
1369
- will reject the connection promise.
1370
-
1371
- One way to avoid this type of issue is to add a delay between connection
1372
- attempts. It does not need to be a very long delay - just sufficient to permit
1373
- the previous connection to be authenticated. In fact, the default setting for
1374
- openSSH is `10:30:60`, so you really just need to have enough delay to ensure
1375
- that the 1st connection has completed authentication before the 11th connection
1376
- is attempted.
931
+ .then(msg => {
932
+ console.log(msg);
933
+ })
934
+ .catch(err => {
935
+ console.log(`main error: ${err.message}`);
936
+ });
937
+
938
+ ```
1377
939
 
940
+ ### downloadDir(srcDir, dstDir) ==> string<a id="sec-5-2-20"></a>
1378
941
 
1379
- <a id="org735f9cc"></a>
942
+ Download the remote directory specified by `srcDir` to the local file system directory specified by `dstDir`. The `dstDir` directory will be created if required. All sub directories within `srcDir` will also be copied. Any existing files in the local path will be overwritten. No files in the local path will be deleted.
1380
943
 
1381
- ## How can you pass writable stream as dst for get method?
944
+ The method also emites `download` events to provide a way to monitor download progress. The download event listener is called with one argument, an object with two properties, source and destination. The source property is the path to the remote file that has been downloaded and the destination is the local path to where the file was downloaded to. You can add a listener for this event using the `on()` method.
1382
945
 
1383
- If the dst argument passed to the get method is a writeable stream, the remote
1384
- file will be piped into that writeable. If the writeable you pass in is a
1385
- writeable stream created with `fs.createWriteStream()`, the data will be written
1386
- to the file specified in the constructor call to `createWriteStream()`.
946
+ - **srcDir:** A remote file path specified as a string
947
+ - **dstDir:** A local file path specified as a string
1387
948
 
1388
- The writeable stream can be any type of write stream. For example, the below code
1389
- will convert all the characters in the remote file to upper case before it is
1390
- saved to the local file system. This could just as easily be something like a
1391
- gunzip stream from `zlib`, enabling you to decompress remote zipped files as you
1392
- bring them across before saving to local file system.
949
+ 1. Example
1393
950
 
951
+ ```javascript
1394
952
  'use strict';
1395
953
 
1396
- // Example of using a writeable with get to retrieve a file.
1397
- // This code will read the remote file, convert all characters to upper case
1398
- // and then save it to a local file
954
+ // Example of using the downloadDir() method to upload a directory
955
+ // to a remote SFTP server
1399
956
 
1400
- const Client = require('../src/index.js');
1401
957
  const path = require('path');
1402
- const fs = require('fs');
1403
- const through = require('through2');
958
+ const SftpClient = require('../src/index');
959
+
960
+ const dotenvPath = path.join(__dirname, '..', '.env');
961
+ require('dotenv').config({path: dotenvPath});
1404
962
 
1405
963
  const config = {
1406
- host: 'arch-vbox',
1407
- port: 22,
1408
- username: 'tim',
1409
- password: 'xxxx'
964
+ host: process.env.SFTP_SERVER,
965
+ username: process.env.SFTP_USER,
966
+ password: process.env.SFTP_PASSWORD,
967
+ port: process.env.SFTP_PORT || 22
1410
968
  };
1411
969
 
1412
- const sftp = new Client();
1413
- const remoteDir = '/home/tim/testServer';
970
+ async function main() {
971
+ const client = new SftpClient('upload-test');
972
+ const dst = '/tmp';
973
+ const src = '/home/tim/upload-test';
1414
974
 
1415
- function toupper() {
1416
- return through(function(buf, enc, next) {
1417
- next(null, buf.toString().toUpperCase());
1418
- });
975
+ try {
976
+ await client.connect(config);
977
+ client.on('download', info => {
978
+ console.log(`Listener: Download ${info.source}`);
979
+ });
980
+ let rslt = await client.downloadDir(src, dst);
981
+ return rslt;
982
+ } finally {
983
+ client.end();
984
+ }
1419
985
  }
1420
986
 
1421
- sftp
1422
- .connect(config)
1423
- .then(() => {
1424
- return sftp.list(remoteDir);
1425
- })
1426
- .then(data => {
1427
- // list of files in testServer
1428
- console.dir(data);
1429
- let remoteFile = path.join(remoteDir, 'test.txt');
1430
- let upperWtr = toupper();
1431
- let fileWtr = fs.createWriteStream(path.join(__dirname, 'loud-text.txt'));
1432
- upperWtr.pipe(fileWtr);
1433
- return sftp.get(remoteFile, upperWtr);
1434
- })
1435
- .then(() => {
1436
- return sftp.end();
987
+ main()
988
+ .then(msg => {
989
+ console.log(msg);
1437
990
  })
1438
991
  .catch(err => {
1439
- console.error(err.message);
992
+ console.log(`main error: ${err.message}`);
1440
993
  });
994
+
995
+ ```
1441
996
 
997
+ ### end() ==> boolean<a id="sec-5-2-21"></a>
1442
998
 
1443
- <a id="orgce4077c"></a>
999
+ Ends the current client session, releasing the client socket and associated resources. This function also removes all listeners associated with the client.
1444
1000
 
1445
- ## How can I upload files without having to specify a password?
1001
+ 1. Example Use
1446
1002
 
1447
- There are a couple of ways to do this. Essentially, you want to setup SSH keys
1448
- and use these for authentication to the remote server.
1003
+ ```javascript
1004
+ let client = new Client();
1005
+
1006
+ client.connect(config)
1007
+ .then(() => {
1008
+ // do some sftp stuff
1009
+ })
1010
+ .then(() => {
1011
+ return client.end();
1012
+ })
1013
+ .catch(err => {
1014
+ console.error(err.message);
1015
+ });
1016
+ ```
1449
1017
 
1450
- One solution, provided by @KalleVuorjoki is to use the SSH agent
1451
- process. **Note**: SSH<sub>AUTH</sub><sub>SOCK</sub> is normally created by your OS when you load the
1452
- ssh-agent as part of the login session.
1018
+ ### Add and Remove Listeners<a id="sec-5-2-22"></a>
1453
1019
 
1454
- let sftp = new Client();
1455
- sftp.connect({
1456
- host: 'YOUR-HOST',
1457
- port: 'YOUR-PORT',
1458
- username: 'YOUR-USERNAME',
1459
- agent: process.env.SSH_AUTH_SOCK
1460
- }).then(() => {
1461
- sftp.fastPut(/* ... */)
1462
- }
1020
+ Although normally not required, you can add and remove custom listeners on the ssh2 client object. This object supports a number of events, but only a few of them have any meaning in the context of SFTP. These are
1463
1021
 
1464
- Another alternative is to just pass in the SSH key directly as part of the
1465
- configuration.
1022
+ - **error:** An error occurred. Calls listener with an error argument.
1023
+ - **end:** The socket has been disconnected. No argument.
1024
+ - **close:** The socket was closed. Boolean argument which is true when the socket was closed due to errors.
1466
1025
 
1467
- let sftp = new Client();
1468
- sftp.connect({
1469
- host: 'YOUR-HOST',
1470
- port: 'YOUR-PORT',
1471
- username: 'YOUR-USERNAME',
1472
- privateKey: fs.readFileSync('/path/to/ssh/key')
1473
- }).then(() => {
1474
- sftp.fastPut(/* ... */)
1475
- }
1026
+ 1. on(eventType, listener)
1476
1027
 
1028
+ Adds the specified listener to the specified event type. It the event type is `error`, the listener should accept 1 argument, which will be an Error object. If the event type is `close`, the listener should accept one argument of a boolean type, which will be true when the client connection was closed due to errors.
1477
1029
 
1478
- <a id="org945fc8f"></a>
1030
+ 2. removeListener(eventType, listener)
1479
1031
 
1480
- ## How can I connect through a Socks Proxy
1032
+ Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
1033
+
1034
+ # FAQ<a id="sec-6"></a>
1035
+
1036
+ ## Remote server drops connections with only an end event<a id="sec-6-1"></a>
1037
+
1038
+ Many SFTP servers have rate limiting protection which will drop connections once a limit has been reached. In particular, openSSH has the setting `MaxStartups`, which can be a tuple of the form `max:drop:full` where `max` is the maximum allowed unauthenticated connections, `drop` is a percentage value which specifies percentage of connections to be dropped once `max` connections has been reached and `full` is the number of connections at which point all subsequent connections will be dropped. e.g. `10:30:60` means allow up to 10 unauthenticated connections after which drop 30% of connection attempts until reaching 60 unauthenticated connections, at which time, drop all attempts.
1039
+
1040
+ Clients first make an unauthenticated connection to the SFTP server to begin negotiation of protocol settings (cipher, authentication method etc). If you are creating multiple connections in a script, it is easy to exceed the limit, resulting in some connections being dropped. As SSH2 only raises an 'end' event for these dropped connections, no error is detected. The `ssh2-sftp-client` now listens for `end` events during the connection process and if one is detected, will reject the connection promise.
1041
+
1042
+ 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.
1043
+
1044
+ ## How can you pass writable stream as dst for get method?<a id="sec-6-2"></a>
1045
+
1046
+ If the dst argument passed to the get method is a writeable stream, the remote file will be piped into that writeable. If the writeable you pass in is a writeable stream created with `fs.createWriteStream()`, the data will be written to the file specified in the constructor call to `createWriteStream()`.
1047
+
1048
+ The writeable stream can be any type of write stream. For example, the below code will convert all the characters in the remote file to upper case before it is saved to the local file system. This could just as easily be something like a gunzip stream from `zlib`, enabling you to decompress remote zipped files as you bring them across before saving to local file system.
1049
+
1050
+ ```javascript
1051
+ 'use strict';
1052
+
1053
+ // Example of using a writeable with get to retrieve a file.
1054
+ // This code will read the remote file, convert all characters to upper case
1055
+ // and then save it to a local file
1056
+
1057
+ const Client = require('../src/index.js');
1058
+ const path = require('path');
1059
+ const fs = require('fs');
1060
+ const through = require('through2');
1061
+
1062
+ const config = {
1063
+ host: 'arch-vbox',
1064
+ port: 22,
1065
+ username: 'tim',
1066
+ password: 'xxxx'
1067
+ };
1068
+
1069
+ const sftp = new Client();
1070
+ const remoteDir = '/home/tim/testServer';
1071
+
1072
+ function toupper() {
1073
+ return through(function(buf, enc, next) {
1074
+ next(null, buf.toString().toUpperCase());
1075
+ });
1076
+ }
1077
+
1078
+ sftp
1079
+ .connect(config)
1080
+ .then(() => {
1081
+ return sftp.list(remoteDir);
1082
+ })
1083
+ .then(data => {
1084
+ // list of files in testServer
1085
+ console.dir(data);
1086
+ let remoteFile = path.join(remoteDir, 'test.txt');
1087
+ let upperWtr = toupper();
1088
+ let fileWtr = fs.createWriteStream(path.join(__dirname, 'loud-text.txt'));
1089
+ upperWtr.pipe(fileWtr);
1090
+ return sftp.get(remoteFile, upperWtr);
1091
+ })
1092
+ .then(() => {
1093
+ return sftp.end();
1094
+ })
1095
+ .catch(err => {
1096
+ console.error(err.message);
1097
+ });
1098
+ ```
1099
+
1100
+ ## How can I upload files without having to specify a password?<a id="sec-6-3"></a>
1101
+
1102
+ There are a couple of ways to do this. Essentially, you want to setup SSH keys and use these for authentication to the remote server.
1103
+
1104
+ One solution, provided by @KalleVuorjoki is to use the SSH agent process. **Note**: SSH<sub>AUTH</sub><sub>SOCK</sub> is normally created by your OS when you load the ssh-agent as part of the login session.
1105
+
1106
+ ```javascript
1107
+ let sftp = new Client();
1108
+ sftp.connect({
1109
+ host: 'YOUR-HOST',
1110
+ port: 'YOUR-PORT',
1111
+ username: 'YOUR-USERNAME',
1112
+ agent: process.env.SSH_AUTH_SOCK
1113
+ }).then(() => {
1114
+ sftp.fastPut(/* ... */)
1115
+ }
1116
+ ```
1117
+
1118
+ Another alternative is to just pass in the SSH key directly as part of the configuration.
1119
+
1120
+ ```javascript
1121
+ let sftp = new Client();
1122
+ sftp.connect({
1123
+ host: 'YOUR-HOST',
1124
+ port: 'YOUR-PORT',
1125
+ username: 'YOUR-USERNAME',
1126
+ privateKey: fs.readFileSync('/path/to/ssh/key')
1127
+ }).then(() => {
1128
+ sftp.fastPut(/* ... */)
1129
+ }
1130
+ ```
1131
+
1132
+ ## How can I connect through a Socks Proxy<a id="sec-6-4"></a>
1481
1133
 
1482
1134
  This solution was provided by @jmorino.
1483
1135
 
1484
- import { SocksClient } from 'socks';
1485
- import SFTPClient from 'ssh2-sftp-client';
1486
-
1487
- const host = 'my-sftp-server.net';
1488
- const port = 22; // default SSH/SFTP port on remote server
1489
-
1490
- // connect to SOCKS 5 proxy
1491
- const { socket } = await SocksClient.createConnection({
1492
- proxy: {
1493
- host: 'my.proxy', // proxy hostname
1494
- port: 1080, // proxy port
1495
- type: 5, // for SOCKS v5
1496
- },
1497
- command: 'connect',
1498
- destination: { host, port } // the remote SFTP server
1499
- });
1500
-
1501
- const client = new SFTPClient();
1502
- client.connect({
1503
- host,
1504
- sock: socket, // pass the socket to proxy here (see ssh2 doc)
1505
- username: '.....',
1506
- privateKey: '.....'
1507
- })
1508
-
1509
- // client is connected
1510
-
1136
+ ```javascript
1137
+ import { SocksClient } from 'socks';
1138
+ import SFTPClient from 'ssh2-sftp-client';
1511
1139
 
1512
- <a id="org22b7853"></a>
1140
+ const host = 'my-sftp-server.net';
1141
+ const port = 22; // default SSH/SFTP port on remote server
1513
1142
 
1514
- ## Timeout while waiting for handshake or handshake errors
1143
+ // connect to SOCKS 5 proxy
1144
+ const { socket } = await SocksClient.createConnection({
1145
+ proxy: {
1146
+ host: 'my.proxy', // proxy hostname
1147
+ port: 1080, // proxy port
1148
+ type: 5, // for SOCKS v5
1149
+ },
1150
+ command: 'connect',
1151
+ destination: { host, port } // the remote SFTP server
1152
+ });
1515
1153
 
1516
- Some users have encountered the error 'Timeout while waiting for handshake' or
1517
- 'Handshake failed, no matching client->server ciphers. This is often due to the
1518
- client not having the correct configuration for the transport layer algorithms
1519
- used by ssh2. One of the connect options provided by the ssh2 module is
1520
- `algorithm`, which is an object that allows you to explicitly set the key
1521
- exchange, ciphers, hmac and compression algorithms as well as server
1522
- host key used to establish the initial secure connection. See the SSH2
1523
- documentation for details. Getting these parameters correct usually resolves the
1524
- issue.
1154
+ const client = new SFTPClient();
1155
+ client.connect({
1156
+ host,
1157
+ sock: socket, // pass the socket to proxy here (see ssh2 doc)
1158
+ username: '.....',
1159
+ privateKey: '.....'
1160
+ })
1525
1161
 
1162
+ // client is connected
1163
+ ```
1526
1164
 
1527
- <a id="org14f6de1"></a>
1165
+ ## Timeout while waiting for handshake or handshake errors<a id="sec-6-5"></a>
1528
1166
 
1529
- # Examples
1167
+ Some users have encountered the error 'Timeout while waiting for handshake' or 'Handshake failed, no matching client->server ciphers. This is often due to the client not having the correct configuration for the transport layer algorithms used by ssh2. One of the connect options provided by the ssh2 module is `algorithm`, which is an object that allows you to explicitly set the key exchange, ciphers, hmac and compression algorithms as well as server host key used to establish the initial secure connection. See the SSH2 documentation for details. Getting these parameters correct usually resolves the issue.
1530
1168
 
1531
- I have started collecting example scripts in the example directory of the
1532
- repository. These are mainly scripts I have put together in order to investigate
1533
- issues or provide samples for users. They are not robust, lack adequate error
1534
- handling and may contain errors. However, I think they are still useful for
1535
- helping developers see how the module and API can be used.
1169
+ # Examples<a id="sec-7"></a>
1536
1170
 
1171
+ 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.
1537
1172
 
1538
- <a id="orgf7b58df"></a>
1173
+ # Change Log<a id="sec-8"></a>
1539
1174
 
1540
- # Change Log
1175
+ ## v5.2.1 (Prod Version)<a id="sec-8-1"></a>
1541
1176
 
1177
+ - Move some dependencies into dev-Dependencies
1542
1178
 
1543
- <a id="org73f1344"></a>
1544
-
1545
- ## v5.2.0 (Prod Version)
1179
+ ## v5.2.0<a id="sec-8-2"></a>
1546
1180
 
1547
1181
  - Add new method posixRename() which uses the openSSH POSIX rename extension.
1548
1182
 
1183
+ ## v5.1.3<a id="sec-8-3"></a>
1549
1184
 
1550
- <a id="orga6a3e40"></a>
1551
-
1552
- ## v5.1.3
1553
-
1554
- - Fix bug when writing to root directory and failure due to not being able to
1555
- determine parent
1556
- - Refactor some tests to eliminate need to have artificial delays between
1557
- tests
1185
+ - Fix bug when writing to root directory and failure due to not being able to determine parent
1186
+ - Refactor some tests to eliminate need to have artificial delays between tests
1558
1187
  - Bumped some dependency versions to latest version
1559
1188
 
1560
-
1561
- <a id="org809d915"></a>
1562
-
1563
- ## v5.1.2
1189
+ ## v5.1.2<a id="sec-8-4"></a>
1564
1190
 
1565
1191
  - Added back global close handler
1566
1192
  - Added dumpListeners() method
1567
1193
 
1568
-
1569
- <a id="org27c7458"></a>
1570
-
1571
- ## v5.1.1
1194
+ ## v5.1.1<a id="sec-8-5"></a>
1572
1195
 
1573
1196
  - Added separate close handlers to each method.
1574
1197
  - Added missing return statement in connect method
1575
- - Added additional troubleshooting documentation for
1576
- common errors.
1577
-
1578
-
1579
- <a id="org5fb8ed9"></a>
1198
+ - Added additional troubleshooting documentation for common errors.
1580
1199
 
1581
- ## v5.1.0
1200
+ ## v5.1.0<a id="sec-8-6"></a>
1582
1201
 
1583
- - Fix bug in checkRemotePath() relating to handling of badly
1584
- specified paths (issue #213)
1202
+ - Fix bug in checkRemotePath() relating to handling of badly specified paths (issue #213)
1585
1203
  - Added additional debugging support
1586
1204
  - Add missing test for valid connection in end() method.
1587
1205
  - Bump ssh2 version to v0.8.8
1588
1206
 
1589
-
1590
- <a id="org5c4a5da"></a>
1591
-
1592
- ## v5.0.2
1207
+ ## v5.0.2<a id="sec-8-7"></a>
1593
1208
 
1594
1209
  - Fix bugs related to win32 platform and local tests for valid directories
1595
1210
  - Fix problem with parsing of file paths
1596
1211
 
1212
+ ## v5.0.1<a id="sec-8-8"></a>
1597
1213
 
1598
- <a id="org47a7bec"></a>
1599
-
1600
- ## v5.0.1
1601
-
1602
- - Turn down error checking to be less stringent and handle situations
1603
- where user does not have read permission on parent directory.
1214
+ - Turn down error checking to be less stringent and handle situations where user does not have read permission on parent directory.
1604
1215
 
1605
-
1606
- <a id="orge53577f"></a>
1607
-
1608
- ## v5.0.0
1216
+ ## v5.0.0<a id="sec-8-9"></a>
1609
1217
 
1610
1218
  - Added two new methods `uploadDir()` and `downloadDir()`
1611
1219
  - Removed deprecated `auxList()` method
1612
1220
  - Improved error message consistency
1613
- - Added additional error checking to enable more accurate and useful error
1614
- messages.
1615
- - Added default error handler to deal with event errors which fire outside of
1616
- active SftpClient methods (i.e. connection unexpectedly reset by remote host).
1617
- - Modified event handlers to ensure that only event handlers added by the
1618
- module are removed by the module (users now responsible for removing any
1619
- custom event handlers they add).
1620
- - Module error handlers added using `prependListener` to ensure they are
1621
- called before any additional custom handlers added by client code.
1221
+ - Added additional error checking to enable more accurate and useful error messages.
1222
+ - Added default error handler to deal with event errors which fire outside of active SftpClient methods (i.e. connection unexpectedly reset by remote host).
1223
+ - Modified event handlers to ensure that only event handlers added by the module are removed by the module (users now responsible for removing any custom event handlers they add).
1224
+ - Module error handlers added using `prependListener` to ensure they are called before any additional custom handlers added by client code.
1622
1225
  - Any error events fired during an `end()` call are now ignored.
1623
1226
 
1624
-
1625
- <a id="orgf4a7c60"></a>
1626
-
1627
- ## v4.3.1
1227
+ ## v4.3.1<a id="sec-8-10"></a>
1628
1228
 
1629
1229
  - Updated end() method to resolve once close event fires
1630
- - Added errorListener to error event in each promise to catch error events
1631
- and reject the promise. This should resolve the issue of some error events
1632
- causing uncaughtException erros and causing the process to exit.
1633
-
1634
-
1635
- <a id="org70ad882"></a>
1636
-
1637
- ## v4.3.0
1230
+ - Added errorListener to error event in each promise to catch error events and reject the promise. This should resolve the issue of some error events causing uncaughtException erros and causing the process to exit.
1638
1231
 
1639
- - Ensure errors include an err.code property and pass through the error code
1640
- from the originating error
1641
- - Change tests for error type to use `error.code` instead of matching on
1642
- `error.message`.
1232
+ ## v4.3.0<a id="sec-8-11"></a>
1643
1233
 
1234
+ - Ensure errors include an err.code property and pass through the error code from the originating error
1235
+ - Change tests for error type to use `error.code` instead of matching on `error.message`.
1644
1236
 
1645
- <a id="org4d3d15c"></a>
1646
-
1647
- ## v4.2.4
1237
+ ## v4.2.4<a id="sec-8-12"></a>
1648
1238
 
1649
1239
  - Bumped ssh2 to v0.8.6
1650
1240
  - Added exists() usage example to examples directory
1651
1241
  - Clarify documentation on get() method
1652
1242
 
1653
-
1654
- <a id="org6ce1458"></a>
1655
-
1656
- ## v4.2.3
1243
+ ## v4.2.3<a id="sec-8-13"></a>
1657
1244
 
1658
1245
  - Fix bug in `exist()` where tests on root directory returned false
1659
1246
  - Minor documentation fixes
1660
1247
  - Clean up mkdir example
1661
1248
 
1662
-
1663
- <a id="org7dafb79"></a>
1664
-
1665
- ## v4.2.2
1249
+ ## v4.2.2<a id="sec-8-14"></a>
1666
1250
 
1667
1251
  - Minor documentation fixes
1668
1252
  - Added additional examples in the `example` directory
1669
1253
 
1254
+ ## v4.2.1<a id="sec-8-15"></a>
1670
1255
 
1671
- <a id="org4e0749a"></a>
1672
-
1673
- ## v4.2.1
1674
-
1675
- - Remove default close listener. changes in ssh2 API removed the utility of a
1676
- default close listener
1677
- - Fix path handling. Under mixed environments (where client platform and
1678
- server platform were different i.e. one windows the other unix), path
1679
- handling was broken due tot he use of path.join().
1680
- - Ensure error messages include path details. Instead of errors such as "No
1681
- such file" now report "No such file /path/to/missing/file" to help with
1682
- debugging
1256
+ - Remove default close listener. changes in ssh2 API removed the utility of a default close listener
1257
+ - Fix path handling. Under mixed environments (where client platform and server platform were different i.e. one windows the other unix), path handling was broken due tot he use of path.join().
1258
+ - Ensure error messages include path details. Instead of errors such as "No such file" now report "No such file /path/to/missing/file" to help with debugging
1683
1259
 
1684
-
1685
- <a id="org3a386b5"></a>
1686
-
1687
- ## v4.2.0
1260
+ ## v4.2.0<a id="sec-8-16"></a>
1688
1261
 
1689
1262
  - Work-around for SSH2 `end` event bug
1690
1263
  - Added ability to set client name in constructor method
1691
- - Added additional error checking to prevent `connect()` being called on
1692
- already connected client
1264
+ - Added additional error checking to prevent `connect()` being called on already connected client
1693
1265
  - Added additional examples in `example` directory
1694
1266
 
1695
-
1696
- <a id="org5bdeafc"></a>
1697
-
1698
- ## v4.1.0
1267
+ ## v4.1.0<a id="sec-8-17"></a>
1699
1268
 
1700
1269
  - move `end()` call to resolve into close hook
1701
- - Prevent `put()` and `get()` from creating empty files in destination when
1702
- unable to read source
1270
+ - Prevent `put()` and `get()` from creating empty files in destination when unable to read source
1703
1271
  - Expand tests for operations when lacking required permissions
1704
1272
  - Add additional data checks for `append()`
1705
1273
  - Verify file exists
@@ -1709,97 +1277,63 @@ helping developers see how the module and API can be used.
1709
1277
  - Add `realPath()` method
1710
1278
  - Add `cwd()` method
1711
1279
 
1712
-
1713
- <a id="orgf78aa34"></a>
1714
-
1715
- ## v4.0.4
1280
+ ## v4.0.4<a id="sec-8-18"></a>
1716
1281
 
1717
1282
  - Minor documentation fix
1718
1283
  - Fix return value from `get()`
1719
1284
 
1720
-
1721
- <a id="org7ceeee9"></a>
1722
-
1723
- ## v4.0.3
1285
+ ## v4.0.3<a id="sec-8-19"></a>
1724
1286
 
1725
1287
  - Fix bug in mkdir() relating to handling of relative paths
1726
1288
  - Modify exists() to always return 'd' if path is '.'
1727
1289
 
1728
-
1729
- <a id="org5fb5e3d"></a>
1730
-
1731
- ## v4.0.2
1290
+ ## v4.0.2<a id="sec-8-20"></a>
1732
1291
 
1733
1292
  - Fix some minor packaging issues
1734
1293
 
1735
-
1736
- <a id="org83d4545"></a>
1737
-
1738
- ## v4.0.0
1294
+ ## v4.0.0<a id="sec-8-21"></a>
1739
1295
 
1740
1296
  - Remove support for node < 8.x
1741
1297
  - Fix connection retry feature
1742
1298
  - sftp connection object set to null when 'end' signal is raised
1743
1299
  - Removed 'connectMethod' argument from connect method.
1744
- - Refined adding/removing of listeners in connect() and end() methods to enable
1745
- errors to be adequately caught and reported.
1300
+ - Refined adding/removing of listeners in connect() and end() methods to enable errors to be adequately caught and reported.
1746
1301
  - Deprecate auxList() and add pattern/regexp filter option to list()
1747
1302
  - Refactored handling of event signals to provide better feedback to clients
1748
- - Removed pointless 'permissions' property from objects returned by `stat()`
1749
- (same as mode property). Added additional properties describing the type of
1750
- object.
1303
+ - Removed pointless 'permissions' property from objects returned by `stat()` (same as mode property). Added additional properties describing the type of object.
1751
1304
  - Added the `removeListener()` method to compliment the existing `on()` method.
1752
1305
 
1306
+ ## Older Versions<a id="sec-8-22"></a>
1753
1307
 
1754
- <a id="orgc6ca313"></a>
1755
-
1756
- ## v2.5.2
1308
+ ### v2.5.2<a id="sec-8-22-1"></a>
1757
1309
 
1758
1310
  - Repository transferred to theophilusx
1759
1311
  - Fix error in package.json pointing to wrong repository
1760
1312
 
1761
-
1762
- <a id="org2dafe31"></a>
1763
-
1764
- ## v2.5.1
1313
+ ### v2.5.1<a id="sec-8-22-2"></a>
1765
1314
 
1766
1315
  - Apply 4 pull requests to address minor issues prior to transfer
1767
1316
 
1768
-
1769
- <a id="orge233569"></a>
1770
-
1771
- ## v2.5.0
1317
+ ### v2.5.0<a id="sec-8-22-3"></a>
1772
1318
 
1773
1319
  - ???
1774
1320
 
1775
-
1776
- <a id="orgb05446f"></a>
1777
-
1778
- ## v2.4.3
1321
+ ### v2.4.3<a id="sec-8-22-4"></a>
1779
1322
 
1780
1323
  - merge #108, #110
1781
1324
  - fix connect promise if connection ends
1782
1325
 
1783
-
1784
- <a id="orgad96dc1"></a>
1785
-
1786
- ## v2.4.2
1326
+ ### v2.4.2<a id="sec-8-22-5"></a>
1787
1327
 
1788
1328
  - merge #105
1789
1329
  - fix windows path
1790
1330
 
1791
-
1792
- <a id="org13a3465"></a>
1793
-
1794
- ## v2.4.1
1331
+ ### v2.4.1<a id="sec-8-22-6"></a>
1795
1332
 
1796
1333
  - merge pr #99, #100
1797
1334
  - bug fix
1798
1335
 
1799
-
1800
- <a id="org954540b"></a>
1801
-
1802
- ## v2.4.0
1336
+ ### v2.4.0<a id="sec-8-22-7"></a>
1803
1337
 
1804
1338
  - Requires node.js v7.5.0 or above.
1805
1339
  - merge pr #97, thanks for @theophilusx
@@ -1809,331 +1343,198 @@ helping developers see how the module and API can be used.
1809
1343
  - re-factored test
1810
1344
  - Added new 'exists' method and re-factored mkdir/rmdir
1811
1345
 
1812
-
1813
- <a id="orgb50df74"></a>
1814
-
1815
- ## v2.3.0
1346
+ ### v2.3.0<a id="sec-8-22-8"></a>
1816
1347
 
1817
1348
  - add: `stat` method
1818
1349
  - add `fastGet` and `fastPut` method.
1819
1350
  - fix: `mkdir` file exists decision logic
1820
1351
 
1821
-
1822
- <a id="org5d0ec00"></a>
1823
-
1824
- ## v3.0.0 &#x2013; deprecate this version
1352
+ ### v3.0.0 &#x2013; deprecate this version<a id="sec-8-22-9"></a>
1825
1353
 
1826
1354
  - change: `sftp.get` will return chunk not stream anymore
1827
1355
  - fix: get readable not emitting data events in node 10.0.0
1828
1356
 
1829
-
1830
- <a id="org6b8440d"></a>
1831
-
1832
- ## v2.1.1
1357
+ ### v2.1.1<a id="sec-8-22-10"></a>
1833
1358
 
1834
1359
  - add: event listener. [doc](https://github.com/jyu213/ssh2-sftp-client#Event)
1835
1360
  - add: `get` or `put` method add extra options [pr#52](https://github.com/jyu213/ssh2-sftp-client/pull/52)
1836
1361
 
1837
-
1838
- <a id="org3d55c20"></a>
1839
-
1840
- ## v2.0.1
1362
+ ### v2.0.1<a id="sec-8-22-11"></a>
1841
1363
 
1842
1364
  - add: `chmod` method [pr#33](https://github.com/jyu213/ssh2-sftp-client/pull/33)
1843
1365
  - update: upgrade ssh2 to V0.5.0 [pr#30](https://github.com/jyu213/ssh2-sftp-client/pull/30)
1844
1366
  - fix: get method stream error reject unwork [#22](https://github.com/jyu213/ssh2-sftp-client/issues/22)
1845
1367
  - fix: return Error object on promise rejection [pr#20](https://github.com/jyu213/ssh2-sftp-client/pull/20)
1846
1368
 
1847
-
1848
- <a id="org93598f6"></a>
1849
-
1850
- ## v1.1.0
1369
+ ### v1.1.0<a id="sec-8-22-12"></a>
1851
1370
 
1852
1371
  - fix: add encoding control support for binary stream
1853
1372
 
1854
-
1855
- <a id="orgacc1b05"></a>
1856
-
1857
- ## v1.0.5:
1373
+ ### v1.0.5:<a id="sec-8-22-13"></a>
1858
1374
 
1859
1375
  - fix: multi image upload
1860
1376
  - change: remove `this.client.sftp` to `connect` function
1861
1377
 
1378
+ # Troubleshooting<a id="sec-9"></a>
1862
1379
 
1863
- <a id="orgeb31b33"></a>
1864
-
1865
- # Troubleshooting
1866
-
1867
- The `ssh2-sftp-client` module is essentially a wrapper around the `ssh2` and
1868
- `ssh2-streams` modules, providing a higher level `promise` based API. When you
1869
- run into issues, it is important to try and determine where the issue lies -
1870
- either in the ssh2-sftp-client module or the underlying `ssh2` and
1871
- `ssh2-streams` modules. One way to do this is to first identify a minimal
1872
- reproducible example which reproduces the issue. Once you have that, try to
1873
- replicate the functionality just using the `ssh2` and `ssh2-streams` modules. If
1874
- the issue still occurs, then you can be fairly confident it is something related
1875
- to those later 2 modules and therefore and issue which should be referred to the
1876
- maintainer of that module.
1380
+ The `ssh2-sftp-client` module is essentially a wrapper around the `ssh2` and `ssh2-streams` modules, providing a higher level `promise` based API. When you run into issues, it is important to try and determine where the issue lies - either in the ssh2-sftp-client module or the underlying `ssh2` and `ssh2-streams` modules. One way to do this is to first identify a minimal reproducible example which reproduces the issue. Once you have that, try to replicate the functionality just using the `ssh2` and `ssh2-streams` modules. If the issue still occurs, then you can be fairly confident it is something related to those later 2 modules and therefore and issue which should be referred to the maintainer of that module.
1877
1381
 
1878
- The `ssh2` and `ssh2-streams` modules are very solid, high quality modules with
1879
- a large user base. Most of the time, issues with those modules are due to client
1880
- misconfiguration. It is therefore very important when trying to diagnose an
1881
- issue to also check the documentation for both `ssh2` and `ssh2-streams`. While
1882
- these modules have good defaults, the flexibility of the ssh2 protocol means
1883
- that not all options are available by default. You may need to tweak the
1884
- connection options, ssh2 algorithms and ciphers etc for some remote servers. The
1885
- documentation for both the `ssh2` and `ssh2-streams` module is quite
1886
- comprehensive and there is lots of valuable information in the issue logs.
1382
+ The `ssh2` and `ssh2-streams` modules are very solid, high quality modules with a large user base. Most of the time, issues with those modules are due to client misconfiguration. It is therefore very important when trying to diagnose an issue to also check the documentation for both `ssh2` and `ssh2-streams`. While these modules have good defaults, the flexibility of the ssh2 protocol means that not all options are available by default. You may need to tweak the connection options, ssh2 algorithms and ciphers etc for some remote servers. The documentation for both the `ssh2` and `ssh2-streams` module is quite comprehensive and there is lots of valuable information in the issue logs.
1887
1383
 
1888
- If you run into an issue which is not repeatable with just the `ssh2` and
1889
- `ssh2-streams` modules, then please log an issue against the `ssh2-sftp-client`
1890
- module and I will investigate. Please note the next section on logging issues.
1384
+ If you run into an issue which is not repeatable with just the `ssh2` and `ssh2-streams` modules, then please log an issue against the `ssh2-sftp-client` module and I will investigate. Please note the next section on logging issues.
1891
1385
 
1892
- Note also that in the repository there are two useful directories. The first is
1893
- the examples directory, which contain some examples of using `ssh2-sftp-client`
1894
- to perform common tasks. A few minutes reviewing these examples can provide that
1895
- additional bit of detail to help fix any problems you are encountering.
1386
+ Note also that in the repository there are two useful directories. The first is the examples directory, which contain some examples of using `ssh2-sftp-client` to perform common tasks. A few minutes reviewing these examples can provide that additional bit of detail to help fix any problems you are encountering.
1896
1387
 
1897
- The second directory is the tools directory. I have some very basic simple
1898
- scripts in this directory which perform basic tasks using only the `ssh2` and
1899
- `ssh2-streams` modules (no ssh2-sftp-client module). These can be useful when
1900
- trying to determine if the issue is with the underlying `ssh2` and
1901
- `ssh2-streams` modules.
1388
+ The second directory is the tools directory. I have some very basic simple scripts in this directory which perform basic tasks using only the `ssh2` and `ssh2-streams` modules (no ssh2-sftp-client module). These can be useful when trying to determine if the issue is with the underlying `ssh2` and `ssh2-streams` modules.
1902
1389
 
1390
+ ## Common Errors<a id="sec-9-1"></a>
1903
1391
 
1904
- <a id="org97ed736"></a>
1392
+ There are some common errors people tend to make when using Promises or Asyc/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.
1905
1393
 
1906
- ## Common Errors
1394
+ ### Not returning the promise in a `then()` block<a id="sec-9-1-1"></a>
1907
1395
 
1908
- There are some common errors people tend to make when using Promises or
1909
- Asyc/Await. These are by far the most common problem found in issues logged
1910
- against this module. Please check for some of these before logging your
1911
- issue.
1396
+ All methods in `ssh2-sftp-client` return a Promise. This means methods are executed *asynchrnously*. When you call a method inside the `then()` block of a promise chain, it is critical that you return the Promise that call generates. Failing to do this will result in the `then()` block completing and your code starting execution of the next `then()`, `catch()` or `finally()` block before your promise has been fulfilled. For exmaple, the following will not do what you expect
1912
1397
 
1398
+ ```javascript
1399
+ sftp.connect(config)
1400
+ .then(() => {
1401
+ sftp.fastGet('foo.txt', 'bar.txt');
1402
+ }).then(rslt => {
1403
+ console.log(rslt);
1404
+ sftp.end();
1405
+ }).catch(e => {
1406
+ console.error(e.message);
1407
+ });
1408
+ ```
1913
1409
 
1914
- <a id="org415e864"></a>
1915
-
1916
- ### Not returning the promise in a `then()` block
1917
-
1918
- All methods in `ssh2-sftp-client` return a Promise. This means methods are
1919
- executed *asynchrnously*. When you call a method inside the `then()` block
1920
- of a promise chain, it is critical that you return the Promise that call
1921
- generates. Failing to do this will result in the `then()` block completing
1922
- and your code starting execution of the next `then()`, `catch()` or
1923
- `finally()` block before your promise has been fulfilled. For exmaple, the
1924
- following will not do what you expect
1925
-
1926
- sftp.connect(config)
1927
- .then(() => {
1928
- sftp.fastGet('foo.txt', 'bar.txt');
1929
- }).then(rslt => {
1930
- console.log(rslt);
1931
- sftp.end();
1932
- }).catch(e => {
1933
- console.error(e.message);
1934
- });
1935
-
1936
- In the above code, the `sftp.end()` method will almost certainly be called
1937
- before `sftp.gastGet()` has been fulfilled (unless the *foo.txt* file is
1938
- really small!). In fact, the whole promise chain will complete and exit even
1939
- before the `sftp.end()` call has been fulfilled. The correct code would be
1940
- something like
1941
-
1942
- sftp.connect(config)
1943
- .then(() => {
1944
- return sftp.fastGet('foo.txt', 'bar.txt');
1945
- }).then(rslt => {
1946
- console.log(rslt);
1947
- return sftp.end();
1948
- }).catch(e => {
1949
- console.error(e.message);
1950
- });
1951
-
1952
- Note the `return` statements. These ensure that the Promise returned by the
1953
- client method is returned into the promise chain. It will be this promise
1954
- the next block in the chain will wait on to be fulfilled before the next
1955
- block is executed. Without the return statement, that block will return the
1956
- default promise for that block, which essentially says *this block has been
1957
- fulfilled*. What you really want is the promise which says *your sftp client
1958
- method call has been fulfilled*.
1410
+ In the above code, the `sftp.end()` method will almost certainly be called before `sftp.gastGet()` has been fulfilled (unless the *foo.txt* file is really small!). In fact, the whole promise chain will complete and exit even before the `sftp.end()` call has been fulfilled. The correct code would be something like
1959
1411
 
1960
- A common symptom of this type of error is for file uploads or download to
1961
- fail to complete or for data in those files to be truncated. What is
1962
- happening is that the connection is being ended before the transfer has
1963
- completed.
1412
+ ```javascript
1413
+ sftp.connect(config)
1414
+ .then(() => {
1415
+ return sftp.fastGet('foo.txt', 'bar.txt');
1416
+ }).then(rslt => {
1417
+ console.log(rslt);
1418
+ return sftp.end();
1419
+ }).catch(e => {
1420
+ console.error(e.message);
1421
+ });
1422
+ ```
1964
1423
 
1424
+ Note the `return` statements. These ensure that the Promise returned by the client method is returned into the promise chain. It will be this promise the next block in the chain will wait on to be fulfilled before the next block is executed. Without the return statement, that block will return the default promise for that block, which essentially says *this block has been fulfilled*. What you really want is the promise which says *your sftp client method call has been fulfilled*.
1965
1425
 
1966
- <a id="org0212f67"></a>
1426
+ 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.
1967
1427
 
1968
- ### Mixing Promise Chains and Async/Await
1428
+ ### Mixing Promise Chains and Async/Await<a id="sec-9-1-2"></a>
1969
1429
 
1970
- Another common error is to mix Promise chains and async/await calls. This is
1971
- rarely a great idea. While you can do this, it tends to create complicated
1972
- and difficult to maintain code. Select one approach and stick with it. Both
1973
- approaches are functionally equivalent, so there is no reason to mix up the
1974
- two paradigms. My personal preference would be to use async/await as I think
1975
- that is more *natural* for most developers. For example, the following is
1976
- more complex and difficult to follow than necessary (and has a bug!)
1430
+ Another common error is to mix Promise chains and async/await calls. This is rarely a great idea. While you can do this, it tends to create complicated and difficult to maintain code. Select one approach and stick with it. Both approaches are functionally equivalent, so there is no reason to mix up the two paradigms. My personal preference would be to use async/await as I think that is more *natural* for most developers. For example, the following is more complex and difficult to follow than necessary (and has a bug!)
1977
1431
 
1978
- sftp.connect(config)
1979
- .then(() => {
1980
- return sftp.cwd();
1981
- }).then(async (d) => {
1982
- console.log(`Remote directory is ${d}`);
1983
- try {
1984
- await sftp.fastGet(`${d}/foo.txt`, `./bar.txt`);
1985
- }.catch(e => {
1986
- console.error(e.message);
1987
- });
1988
- }).catch(e => {
1989
- console.error(e.message);
1990
- }).finally(() => {
1991
- sftp.end();
1992
- });
1993
-
1994
- The main bug in the above code is the `then()` block is not returning the
1995
- Promise generated by the call to `sftp.fastGet()`. What it is actually
1996
- returning is a fulfilled promise which says the `then()` block has been run
1997
- (note that the await'ed promise is not being returned and is therefore
1998
- outside the main Promise chain). As a result, the `finally()` block will be
1999
- executed before the await promise has been fulfilled.
2000
-
2001
- Using async/await inside the promise chain has created unnecessary
2002
- complexity and leads to incorrect assumptions regarding how the code will
2003
- execute. A quick glance at the code is likely to give the impression that
2004
- execution will wait for the `sftp.fastGet()` call to be fulfilled before
2005
- continuing. This is not the case. The code would be more clearly expressed
2006
- as either
2007
-
2008
- sftp.connect(config)
2009
- .then(() => {
2010
- return sftp.cwd();
2011
- }).then(d => {
2012
- console.log(`remote dir ${d}`);
2013
- return sftp.fastGet(`${d}/foot.txt`, 'bar.txt');
2014
- }).catch(e => {
2015
- console.error(e.message);
2016
- }).finally(() => {
2017
- return sftp.end();
2018
- });
1432
+ ```javascript
1433
+ sftp.connect(config)
1434
+ .then(() => {
1435
+ return sftp.cwd();
1436
+ }).then(async (d) => {
1437
+ console.log(`Remote directory is ${d}`);
1438
+ try {
1439
+ await sftp.fastGet(`${d}/foo.txt`, `./bar.txt`);
1440
+ }.catch(e => {
1441
+ console.error(e.message);
1442
+ });
1443
+ }).catch(e => {
1444
+ console.error(e.message);
1445
+ }).finally(() => {
1446
+ sftp.end();
1447
+ });
1448
+ ```
1449
+
1450
+ The main bug in the above code is the `then()` block is not returning the Promise generated by the call to `sftp.fastGet()`. What it is actually returning is a fulfilled promise which says the `then()` block has been run (note that the await'ed promise is not being returned and is therefore outside the main Promise chain). As a result, the `finally()` block will be executed before the await promise has been fulfilled.
1451
+
1452
+ Using async/await inside the promise chain has created unnecessary complexity and leads to incorrect assumptions regarding how the code will execute. A quick glance at the code is likely to give the impression that execution will wait for the `sftp.fastGet()` call to be fulfilled before continuing. This is not the case. The code would be more clearly expressed as either
1453
+
1454
+ ```javascript
1455
+ sftp.connect(config)
1456
+ .then(() => {
1457
+ return sftp.cwd();
1458
+ }).then(d => {
1459
+ console.log(`remote dir ${d}`);
1460
+ return sftp.fastGet(`${d}/foot.txt`, 'bar.txt');
1461
+ }).catch(e => {
1462
+ console.error(e.message);
1463
+ }).finally(() => {
1464
+ return sftp.end();
1465
+ });
1466
+ ```
2019
1467
 
2020
1468
  **or, using async/await**
2021
1469
 
2022
- async function doSftp() {
2023
- try {
2024
- let sftp = await sftp.connect(conf);
2025
- let d = await sftp.cwd();
2026
- console.log(`remote dir is ${d}`);
2027
- await sftp.fastGet(`${d}/foo.txt`, 'bat.txt');
2028
- } catch (e) {
2029
- console.error(e.message);
2030
- } finally () {
2031
- await sftp.end();
2032
- }
2033
- }
2034
-
2035
-
2036
- <a id="orgfb2766e"></a>
2037
-
2038
- ### Try/catch and Error Handlers
1470
+ ```javascript
1471
+ async function doSftp() {
1472
+ try {
1473
+ let sftp = await sftp.connect(conf);
1474
+ let d = await sftp.cwd();
1475
+ console.log(`remote dir is ${d}`);
1476
+ await sftp.fastGet(`${d}/foo.txt`, 'bat.txt');
1477
+ } catch (e) {
1478
+ console.error(e.message);
1479
+ } finally () {
1480
+ await sftp.end();
1481
+ }
1482
+ }
1483
+ ```
2039
1484
 
2040
- Another common error is to try and use a try/catch block to catch event
2041
- signals, such as an error event. In general, you cannot use try/catch blocks
2042
- for asynchronous code and expect errors to be caught by the `catch` block.
2043
- Handling errors in asynchronous code is one of the key reasons we now have
2044
- the Promise and async/await frameworks.
1485
+ ### Try/catch and Error Handlers<a id="sec-9-1-3"></a>
2045
1486
 
2046
- The basic problem is that the try/catch block will have completed execution
2047
- before the asynchronous code has completed. If the asynchronous code has not
2048
- compleed, then there is a potential for it to raise an error. However, as
2049
- the try/catch block has already completed, there is no *catch* waiting to
2050
- catch the error. It will bubble up and probably result in your script
2051
- exiting with an uncaught exception error.
1487
+ Another common error is to try and use a try/catch block to catch event signals, such as an error event. In general, you cannot use try/catch blocks for asynchronous code and expect errors to be caught by the `catch` block. Handling errors in asynchronous code is one of the key reasons we now have the Promise and async/await frameworks.
2052
1488
 
2053
- Error events are essentially asynchronous code. You don't know when such
2054
- events will fire. Therefore, you cannot use a try/catch block to catch such
2055
- event errors. Even creating an error handler which then throws an exception
2056
- won't help as the key problem is that your try/catch block has already
2057
- executed. There are a number of alternative ways to deal with this
2058
- situation. However, the key symptom is that you see occasional uncaught
2059
- error exceptions that cause your script to exit abnormally despite having
2060
- try/catch blocks in your script. What you need to do is look at your code
2061
- and find where errors are raised asynchronously and use an event handler or
2062
- some other mechanism to manage any errors raised.
1489
+ The basic problem is that the try/catch block will have completed execution before the asynchronous code has completed. If the asynchronous code has not compleed, then there is a potential for it to raise an error. However, as the try/catch block has already completed, there is no *catch* waiting to catch the error. It will bubble up and probably result in your script exiting with an uncaught exception error.
2063
1490
 
1491
+ Error events are essentially asynchronous code. You don't know when such events will fire. Therefore, you cannot use a try/catch block to catch such event errors. Even creating an error handler which then throws an exception won't help as the key problem is that your try/catch block has already executed. There are a number of alternative ways to deal with this situation. However, the key symptom is that you see occasional uncaught error exceptions that cause your script to exit abnormally despite having try/catch blocks in your script. What you need to do is look at your code and find where errors are raised asynchronously and use an event handler or some other mechanism to manage any errors raised.
2064
1492
 
2065
- <a id="orgb0a64b5"></a>
1493
+ ## Debugging Support<a id="sec-9-2"></a>
2066
1494
 
2067
- ## Debugging Support
1495
+ You can add a `debug` property to the config object passed in to `connect()` to turn on debugging. This will generate quite a lot of output. The value of the property should be a function which accepts a single string argument. For example;
2068
1496
 
2069
- You can add a `debug` property to the config object passed in to `connect()` to
2070
- turn on debugging. This will generate quite a lot of output. The value of the
2071
- property should be a function which accepts a single string argument. For example;
1497
+ ```javascript
1498
+ config.debug = msg => {
1499
+ console.error(msg);
1500
+ };
2072
1501
 
2073
- config.debug = msg => {
2074
- console.error(msg);
2075
- };
2076
-
2077
- Enabling debugging can generate a lot of output. If you use console.error() as
2078
- the output (as in the example above), you can redirect the output to a file
2079
- using shell redirection e.g.
1502
+ ```
2080
1503
 
2081
- node script.js 2> debug.log
1504
+ Enabling debugging can generate a lot of output. If you use console.error() as the output (as in the example above), you can redirect the output to a file using shell redirection e.g.
2082
1505
 
1506
+ ```shell
1507
+ node script.js 2> debug.log
2083
1508
 
2084
- <a id="org63eba2b"></a>
1509
+ ```
2085
1510
 
2086
- # Logging Issues
1511
+ # Logging Issues<a id="sec-10"></a>
2087
1512
 
2088
- Please log an issue for all bugs, questions, feature and enhancement
2089
- requests. Please ensure you include the module version, node version and
2090
- platform.
1513
+ Please log an issue for all bugs, questions, feature and enhancement requests. Please ensure you include the module version, node version and platform.
2091
1514
 
2092
- I am happy to try and help diagnose and fix any issues you encounter while using
2093
- the `ssh2-sftp-client` module. However, I will only put in effort if you are
2094
- prepared to put in the effort to provide the information necessary to reproduce
2095
- the issue. Things which will help
1515
+ I am happy to try and help diagnose and fix any issues you encounter while using the `ssh2-sftp-client` module. However, I will only put in effort if you are prepared to put in the effort to provide the information necessary to reproduce the issue. Things which will help
2096
1516
 
2097
1517
  - Node version you are using
2098
1518
  - Version of ssh2-sftp-client you are using
2099
1519
  - Platform your client is running on (Linux, macOS, Windows)
2100
1520
  - Platform and software for the remote SFTP server when possible
2101
- - Example of your code. By far, the most common issue is incorrect use of the
2102
- module API. Example code can usually result in such issues being resolved very
2103
- quickly.
2104
-
2105
- Perhaps the best assistance is a minimal reproducible example of the issue. Once
2106
- the issue can be readily reproduced, it can usually be fixed very quickly.
2107
-
1521
+ - Example of your code. By far, the most common issue is incorrect use of the module API. Example code can usually result in such issues being resolved very quickly.
2108
1522
 
2109
- <a id="org03a2ed4"></a>
1523
+ 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.
2110
1524
 
2111
- # Pull Requests
1525
+ # Pull Requests<a id="sec-11"></a>
2112
1526
 
2113
- Pull requests are always welcomed. However, please ensure your changes pass all
2114
- tests and if your adding a new feature, that tests for that feature are
2115
- included. Likewise, for new features or enhancements, please include any
2116
- relevant documentation updates.
1527
+ Pull requests are always welcomed. However, please ensure your changes pass all tests and if your adding a new feature, that tests for that feature are included. Likewise, for new features or enhancements, please include any relevant documentation updates.
2117
1528
 
2118
- This module will adopt a standard semantic versioning policy. Please indicate in
2119
- your pull request what level of change it represents i.e.
1529
+ This module will adopt a standard semantic versioning policy. Please indicate in your pull request what level of change it represents i.e.
2120
1530
 
2121
- - **Major:** Change to API or major change in functionality which will require an
2122
- increase in major version number.
2123
- - **Minor:** Minor change, enhancement or new feature which does not change
2124
- existing API and will not break existing client code.
2125
- - **Bug Fix:** No change to functionality or features. Simple fix of an existing
2126
- bug.
1531
+ - **Major:** Change to API or major change in functionality which will require an increase in major version number.
1532
+ - **Minor:** Minor change, enhancement or new feature which does not change existing API and will not break existing client code.
1533
+ - **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
2127
1534
 
1535
+ # Contributors<a id="sec-12"></a>
2128
1536
 
2129
- <a id="org786c526"></a>
2130
-
2131
- # Contributors
2132
-
2133
- This module was initially written by jyu213. On August 23rd, 2019, theophilusx
2134
- took over responsibility for maintaining this module. A number of other people
2135
- have contributed to this module, but until now, this was not tracked. My
2136
- intention is to credit anyone who contributes going forward.
1537
+ This module was initially written by jyu213. On August 23rd, 2019, theophilusx took over responsibility for maintaining this module. A number of other people have contributed to this module, but until now, this was not tracked. My intention is to credit anyone who contributes going forward.
2137
1538
 
2138
1539
  Thanks to the following for their contributions -
2139
1540
 
@@ -2143,4 +1544,3 @@ Thanks to the following for their contributions -
2143
1544
  - **waldyrious:** Documentation fixes
2144
1545
  - **james-pellow:** Cleanup and fix for connect method logic
2145
1546
  - **jhorbulyk:** Contributed posixRename() functionality
2146
-