ssh2-sftp-client 9.0.3 → 9.0.4

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 (4) hide show
  1. package/README.md +362 -432
  2. package/README.org +34 -31
  3. package/package.json +1 -1
  4. package/src/index.js +13 -5
package/README.md CHANGED
@@ -1,115 +1,110 @@
1
- - [Overview](#orgd38e3db)
2
- - [Version 9.x Changes](#orgbc6dbf7)
3
- - [Installation](#org8538ac8)
4
- - [Basic Usage](#org0e586d6)
5
- - [Documentation](#org8b956c0)
6
- - [Specifying Paths](#orge4d511d)
7
- - [Methods](#org54a5a5f)
8
- - [new SftpClient(name) ===> SFTP client object](#org7e1be88)
9
- - [connect(config) ===> SFTP object](#org4d439ac)
10
- - [list(path, filter) ==> Array[object]](#org1cdf979)
11
- - [exists(path) ==> boolean](#org27dfbbb)
12
- - [stat(path) ==> object](#org989df3b)
13
- - [get(path, dst, options) ==> String|Stream|Buffer](#orgc4730e8)
14
- - [fastGet(remotePath, localPath, options) ===> string](#org2f58141)
15
- - [put(src, remotePath, options) ==> string](#org6859ddc)
16
- - [fastPut(localPath, remotePath, options) ==> string](#org62993ea)
17
- - [append(input, remotePath, options) ==> string](#orgbac60b5)
18
- - [mkdir(path, recursive) ==> string](#orgf266033)
19
- - [rmdir(path, recursive) ==> string](#orgcb249fd)
20
- - [delete(path, noErrorOK) ==> string](#orgfef0bf5)
21
- - [rename(fromPath, toPath) ==> string](#org916cc1f)
22
- - [posixRename(fromPath, toPath) ==> string](#org590854d)
23
- - [chmod(path, mode) ==> string](#orgf437cd4)
24
- - [realPath(path) ===> string](#org22161d6)
25
- - [cwd() ==> string](#orgdba7a3a)
26
- - [uploadDir(srcDir, dstDir, options) ==> string](#org583c51e)
27
- - [downloadDir(srcDir, dstDir, options) ==> string](#orgc06694a)
28
- - [createReadStream(remotePath, options)) ==> stream object](#org5163593)
29
- - [createWriteStream(remotePath, options) ==> stream object](#orgbeba46f)
30
- - [rcopy(srcPath, dstPath) ==> string](#orgfab48e5)
31
- - [end() ==> boolean](#org9ffad87)
32
- - [Add and Remove Listeners](#org5c294b3)
33
- - [Platform Quirks & Warnings](#org6e33ff1)
34
- - [Server Capabilities](#org0b56fde)
35
- - [Issues with `fastPut()` and `fastGet()` Methods](#org7c6eba4)
36
- - [Promises, Events & Managing Exceptions](#org38e15f9)
37
- - [Adding Custom Handlers](#org10f1f65)
38
- - [Windows Based Servers](#org7057905)
39
- - [Don't Re-use SftpClient Objects](#org1775ff2)
40
- - [FAQ](#org683ae73)
41
- - [Remote server drops connections with only an end event](#org7e12a90)
42
- - [How can I pass writable stream as dst for get method?](#org7f93e7e)
43
- - [How can I upload files without having to specify a password?](#org6f24d1a)
44
- - [How can I connect through a Socks Proxy](#org59a22c0)
45
- - [Timeout while waiting for handshake or handshake errors](#org0439a3c)
46
- - [How can I limit upload/download speed](#orge773153)
47
- - [Connection hangs or fails for larger files](#orgf14374e)
48
- - [Examples](#orge58bfb0)
49
- - [Troubleshooting](#org5c3ee29)
50
- - [Common Errors](#orgd46a3a1)
51
- - [Not returning the promise in a `then()` block](#org41486a0)
52
- - [Mixing Promise Chains and Async/Await](#org36f96d6)
53
- - [Try/catch and Error Handlers](#org367d856)
54
- - [Server Differences](#org180dea0)
55
- - [Avoid Concurrent Operations](#org2bfd976)
56
- - [Debugging Support](#org9590bf5)
57
- - [Logging Issues](#orga4f395e)
58
- - [Pull Requests](#orgbed06d9)
59
- - [Contributors](#org7b575e4)
60
-
61
-
62
-
63
- <a id="orgd38e3db"></a>
64
-
65
- # Overview
1
+ - [Overview](#sec-1)
2
+ - [Version 9.x Changes](#sec-1-1)
3
+ - [Installation](#sec-2)
4
+ - [Basic Usage](#sec-3)
5
+ - [Documentation](#sec-4)
6
+ - [Specifying Paths](#sec-4-1)
7
+ - [Methods](#sec-4-2)
8
+ - [new SftpClient(name) ===> SFTP client object](#sec-4-2-1)
9
+ - [connect(config) ===> SFTP object](#sec-4-2-2)
10
+ - [list(path, filter) ==> Array[object]](#sec-4-2-3)
11
+ - [exists(path) ==> boolean](#sec-4-2-4)
12
+ - [stat(path) ==> object](#sec-4-2-5)
13
+ - [get(path, dst, options) ==> String|Stream|Buffer](#sec-4-2-6)
14
+ - [fastGet(remotePath, localPath, options) ===> string](#sec-4-2-7)
15
+ - [put(src, remotePath, options) ==> string](#sec-4-2-8)
16
+ - [fastPut(localPath, remotePath, options) ==> string](#sec-4-2-9)
17
+ - [append(input, remotePath, options) ==> string](#sec-4-2-10)
18
+ - [mkdir(path, recursive) ==> string](#sec-4-2-11)
19
+ - [rmdir(path, recursive) ==> string](#sec-4-2-12)
20
+ - [delete(path, noErrorOK) ==> string](#sec-4-2-13)
21
+ - [rename(fromPath, toPath) ==> string](#sec-4-2-14)
22
+ - [posixRename(fromPath, toPath) ==> string](#sec-4-2-15)
23
+ - [chmod(path, mode) ==> string](#sec-4-2-16)
24
+ - [realPath(path) ===> string](#sec-4-2-17)
25
+ - [cwd() ==> string](#sec-4-2-18)
26
+ - [uploadDir(srcDir, dstDir, options) ==> string](#sec-4-2-19)
27
+ - [downloadDir(srcDir, dstDir, options) ==> string](#sec-4-2-20)
28
+ - [createReadStream(remotePath, options)) ==> stream object](#sec-4-2-21)
29
+ - [createWriteStream(remotePath, options) ==> stream object](#sec-4-2-22)
30
+ - [rcopy(srcPath, dstPath) ==> string](#sec-4-2-23)
31
+ - [end() ==> boolean](#sec-4-2-24)
32
+ - [Add and Remove Listeners](#sec-4-2-25)
33
+ - [Platform Quirks & Warnings](#sec-5)
34
+ - [Server Capabilities](#sec-5-1)
35
+ - [Issues with `fastPut()` and `fastGet()` Methods](#sec-5-2)
36
+ - [Promises, Events & Managing Exceptions](#sec-5-3)
37
+ - [Adding Custom Handlers](#sec-5-3-1)
38
+ - [Windows Based Servers](#sec-5-4)
39
+ - [Don't Re-use SftpClient Objects](#sec-5-5)
40
+ - [FAQ](#sec-6)
41
+ - [Remote server drops connections with only an end event](#sec-6-1)
42
+ - [How can I pass writeable stream as dst for get method?](#sec-6-2)
43
+ - [How can I upload files without having to specify a password?](#sec-6-3)
44
+ - [How can I connect through a Socks Proxy](#sec-6-4)
45
+ - [Timeout while waiting for handshake or handshake errors](#sec-6-5)
46
+ - [How can I limit upload/download speed](#sec-6-6)
47
+ - [Connection hangs or fails for larger files](#sec-6-7)
48
+ - [Examples](#sec-7)
49
+ - [Troubleshooting](#sec-8)
50
+ - [Common Errors](#sec-8-1)
51
+ - [Not returning the promise in a `then()` block](#sec-8-1-1)
52
+ - [Mixing Promise Chains and Async/Await](#sec-8-1-2)
53
+ - [Try/catch and Error Handlers](#sec-8-1-3)
54
+ - [Server Differences](#sec-8-1-4)
55
+ - [Avoid Concurrent Operations](#sec-8-1-5)
56
+ - [Debugging Support](#sec-8-2)
57
+ - [Logging Issues](#sec-9)
58
+ - [Pull Requests](#sec-10)
59
+ - [Contributors](#sec-11)
60
+
61
+
62
+ # Overview<a id="sec-1"></a>
66
63
 
67
64
  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.
68
65
 
69
66
  Documentation on the methods and available options in the underlying modules can be found on the [SSH2](https://github.com/mscdex/ssh2) project pages.
70
67
 
71
- Current stable release is **v9.0.2**.
68
+ Current stable release is **v9.0.4**.
72
69
 
73
- Code has been tested against Node versions 14.19.1, 16.15.0 and 18.1.0
70
+ Code has been tested against Node versions 14.20.0, 16.17.2 and 18.8.0
74
71
 
75
72
  Node versions < 14.x are not supported.
76
73
 
74
+ ## Version 9.x Changes<a id="sec-1-1"></a>
77
75
 
78
- <a id="orgbc6dbf7"></a>
79
-
80
- ## Version 9.x Changes
81
-
76
+ - Fix bug in `connect()` method when private key data was corrupted. The method was not handling errors fro corrupted ssh private keys and would hang indefinitely without reporting any error. Now reports that it was unable to parse the private key.
82
77
  - Fix bug in `end()` method where it was possible for the module to attempt calling the underlying ssh2 `end()` method when ssh2 has not been initialised. This could lead to undefined reference errors.
83
78
  - Fix bug in `get()` method where supplied destination streams were not close, creating a possible resource leak. If the remote file did not exist, the method would return an error, but failed to close any passed in stream supplied as the destination for the data in the `get()` call.
84
- - Change the default end and close handlers not to throw error or reject promises. Previously, an end or close event would cause an error to be raised or a promise to be rejected if the event was deemed to be *unexpected*. However, classification of events as being unexpected was unreliable and didn't add much real value. Both these handlers will now invalidate the sftp connection object and log that the event fired and nothing else.
79
+ - Change the default end and close handlers not to throw error or reject promises. Previously, an end or close event would cause an error to be raised or a promise to be rejected if the event was deemed to be *unexpected*. However, classification of events as being unexpected was unreliable and didn't add much real value. Both these handlers will now invalidate the SFTP connection object and log that the event fired and nothing else.
85
80
  - Changed when event handled flags are reset. Now they are reset after a new set of temporary listeners are added.
86
- - Don't throw an error when calling end() if there is no active sftp connection. It does no harm to call end() when there is no connection, so no need to raise an error.
81
+ - Don't throw an error when calling end() if there is no active SFTP connection. It does no harm to call end() when there is no connection, so no need to raise an error.
87
82
  - Use nullish coalescing when setting retry parameters instead of or'ing with defaults. Allows setting values to 0.
88
83
  - **Breaking Change**: This version uses syntax not supported in node versions prior to v14. Therefore, node versions less than v14 will not work.
89
84
  - **Breaking Change**: This `list()` method no longer accepts a regular expression for filtering the entries to be returned. You can now specify a filter function instead. The function is called for each item in the list of items to be returned, passing in the item object as its only argument. Essentially, this is just a call to `Array.filter()`, so the filter function should behave in the same way i.e. return true for items to be retained and false for those to be dropped.
90
85
  - **Breaking Change**: The ability to set `autoClose` on read and write streams and the ability to set `end` on `pipe` operations has been removed. These options caused confusion for users and were too easy to get wrong, plus it made the methods overly complicated. For those use-cases where you want to control streams at a low level, two new methods have been added, `createReadStream()` and `createWriteStream()`. However, it should be noted that client code is 100% responsible for managing streams obtained using these methods. Use at your own risk!
91
- - **Breaking Change**: The 3rd argument to `uploadDir()` and `downloadDir()` methods has been change. Previously, the argument was a filter function used to select which directories and files to be transferred. The 3rd argument is now an options object with two supported properties, `filter` and `useFastput` (for `uploadDir()`) or `useFastget` (for `downloadDir()`). If `useFastput` is true, the `fastPut()` method will be pused to upload files. If `false` or missing, the slower, but better supported, `put()` method will be used. Likewise, the `useFastget` options can be set to `true` to use the `fastGet()` method for donwloading files, otherwise the slower, but more reliable, `get()` method will be used.
92
- - The `uploadDir()` and `downloadDir()` methods now use asynchrounous processes to upload/download files. This should result in improved performance for these two methods.
93
- - New Methods: Two new methods, `createWriteStream()` and `createReadStream()` have been added. These methods will return a stream object connected to a remote file on the `sftp` server. Client code is responsible for managing these stream objects. This includes adding any necessary event listeners and disposing of the objects once finished with them.
94
- - Refactoring of Listeners: The library manages temporary listeners in order to provide a way to catch events and processes them inside a `Promise` context. Previously, every method added its own set of temporary listeners. However, this could result in multiple sets of listeners being added, especially for methods which call other methods as part of their processing e.g. `rmdir(),` `uploadDir()` and `dowqnloadDir()`. To avoid this, *internal only* versions of each method have been created. These internal methods use an *underscore* `_` prefix. Client code should not use these methods directly.
86
+ - **Breaking Change**: The 3rd argument to `uploadDir()` and `downloadDir()` methods has been change. Previously, the argument was a filter function used to select which directories and files to be transferred. The 3rd argument is now an options object with two supported properties, `filter` and `useFastput` (for `uploadDir()`) or `useFastget` (for `downloadDir()`). If `useFastput` is true, the `fastPut()` method will be used to upload files. If `false` or missing, the slower, but better supported, `put()` method will be used. Likewise, the `useFastget` options can be set to `true` to use the `fastGet()` method for downloading files, otherwise the slower, but more reliable, `get()` method will be used.
87
+ - The `uploadDir()` and `downloadDir()` methods now use asynchronous processes to upload/download files. This should result in improved performance for these two methods.
88
+ - New Methods: Two new methods, `createWriteStream()` and `createReadStream()` have been added. These methods will return a stream object connected to a remote file on the `SFTP` server. Client code is responsible for managing these stream objects. This includes adding any necessary event listeners and disposing of the objects once finished with them.
89
+ - Re-factoring of Listeners: The library manages temporary listeners in order to provide a way to catch events and processes them inside a `Promise` context. Previously, every method added its own set of temporary listeners. However, this could result in multiple sets of listeners being added, especially for methods which call other methods as part of their processing e.g. `rmdir(),` `uploadDir()` and `dowqnloadDir()`. To avoid this, *internal only* versions of each method have been created. These internal methods use an *underscore* `_` prefix. Client code should not use these methods directly.
95
90
  - New method: Added `rcopy()` method to perform a remote copy of a file on the remote SFTP server.
96
91
  - Bumped ssh2 version to 1.11.0
97
92
 
93
+ # Installation<a id="sec-2"></a>
98
94
 
99
- <a id="org8538ac8"></a>
95
+ ```shell
100
96
 
101
- # Installation
97
+ npm
102
98
 
103
- ```shell
104
99
  npm install ssh2-sftp-client
105
100
  ```
106
101
 
102
+ # Basic Usage<a id="sec-3"></a>
107
103
 
108
- <a id="org0e586d6"></a>
104
+ ```javascript
109
105
 
110
- # Basic Usage
106
+ let
111
107
 
112
- ```javascript
113
108
  let Client = require('ssh2-sftp-client');
114
109
  let sftp = new Client();
115
110
 
@@ -127,19 +122,13 @@ sftp.connect({
127
122
  });
128
123
  ```
129
124
 
130
-
131
- <a id="org8b956c0"></a>
132
-
133
- # Documentation
125
+ # Documentation<a id="sec-4"></a>
134
126
 
135
127
  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)
136
128
 
137
129
  All the methods will return a Promise, except for `on()` and `removeListener()`, which are typically only used in special use cases.
138
130
 
139
-
140
- <a id="orge4d511d"></a>
141
-
142
- ## Specifying Paths
131
+ ## Specifying Paths<a id="sec-4-1"></a>
143
132
 
144
133
  The convention with both FTP and SFTP is that paths are specified using a 'nix' style i.e. use `/` as the path separator. This means that even if your SFTP server is running on a win32 platform, you should use `/` instead of `\` as the path separator. For example, for a win32 path of `C:\Users\fred` you would actually use `/C:/Users/fred`. If your win32 server does not support the 'nix' path convention, you can try setting the `remotePathSep` property of the `SftpClient` object to the path separator of your remote server. This **might** work, but has not been tested. Please let me know if you need to do this and provide details of the SFTP server so that I can try to create an appropriate environment and adjust things as necessary. At this point, I'm not aware of any win32 based SFTP servers which do not support the 'nix' path convention.
145
134
 
@@ -149,35 +138,38 @@ It is important to recognise that the current remote directory may not always be
149
138
 
150
139
  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.
151
140
 
152
- 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
141
+ When specifying file paths, ensure to include a full path i.e. include the remote file name. Don't expect the module to append the local file name to the path you provide. For example, the following will not work
153
142
 
154
143
  ```javascript
144
+
145
+
146
+
155
147
  client.put('/home/fred/test.txt', '/remote/dir');
156
148
  ```
157
149
 
158
- 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.
150
+ will not result in the file `test.txt` being copied to `/remote/dir/test.txt`. You need to specify the target file name as well e.g.
159
151
 
160
152
  ```javascript
153
+
154
+
155
+
161
156
  client.put('/home/fred/test.txt', '/remote/dir/test.txt');
162
157
  ```
163
158
 
164
159
  Note that the remote file name does not have to be the same as the local file name. The following works fine;
165
160
 
166
161
  ```javascript
167
- client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
168
- ```
169
-
170
- This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
171
162
 
172
163
 
173
- <a id="org54a5a5f"></a>
174
164
 
175
- ## Methods
165
+ client.put('/home/fred/test.txt', '/remote/dir/test-copy.txt');
166
+ ```
176
167
 
168
+ This will copy the local file `test.txt` to the remote file `test-copy.txt` in the directory `/remote/dir`.
177
169
 
178
- <a id="org7e1be88"></a>
170
+ ## Methods<a id="sec-4-2"></a>
179
171
 
180
- ### new SftpClient(name) ===> SFTP client object
172
+ ### new SftpClient(name) ===> SFTP client object<a id="sec-4-2-1"></a>
181
173
 
182
174
  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.
183
175
 
@@ -188,6 +180,9 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
188
180
  2. Example Use
189
181
 
190
182
  ```javascript
183
+
184
+ 'use
185
+
191
186
  'use strict';
192
187
 
193
188
  const Client = require('ssh2-sftp-client');
@@ -213,10 +208,7 @@ Constructor to create a new `ssh2-sftp-client` object. An optional `name` string
213
208
  });
214
209
  ```
215
210
 
216
-
217
- <a id="org4d439ac"></a>
218
-
219
- ### connect(config) ===> SFTP object
211
+ ### connect(config) ===> SFTP object<a id="sec-4-2-2"></a>
220
212
 
221
213
  Connect to an sftp server. Full documentation for connection options is available [here](https://github.com/mscdex/ssh2#user-content-client-methods)
222
214
 
@@ -227,6 +219,9 @@ Connect to an sftp server. Full documentation for connection options is availabl
227
219
  The `retries`, `retry_factor` and `retry_minTimeout` options are not part of the SSH2 module. These are part of the configuration for the [retry](https://www.npmjs.com/package/retry) package and what is used to enable retrying of sftp connection attempts. See the documentation for that package for an explanation of these values.
228
220
 
229
221
  ```javascript
222
+
223
+ //
224
+
230
225
  // common options
231
226
 
232
227
  let commonOpts {
@@ -271,6 +266,10 @@ Connect to an sftp server. Full documentation for connection options is availabl
271
266
  2. Example Use
272
267
 
273
268
  ```javascript
269
+
270
+ sftp.connect({
271
+
272
+
274
273
  sftp.connect({
275
274
  host: example.com,
276
275
  port: 22,
@@ -279,10 +278,7 @@ Connect to an sftp server. Full documentation for connection options is availabl
279
278
  });
280
279
  ```
281
280
 
282
-
283
- <a id="org1cdf979"></a>
284
-
285
- ### list(path, filter) ==> Array[object]
281
+ ### list(path, filter) ==> Array[object]<a id="sec-4-2-3"></a>
286
282
 
287
283
  Retrieves a directory listing. This method returns a Promise, which once realised, returns an array of objects representing items in the remote directory.
288
284
 
@@ -292,6 +288,9 @@ Retrieves a directory listing. This method returns a Promise, which once realise
292
288
  1. Example Use
293
289
 
294
290
  ```javascript
291
+
292
+ const
293
+
295
294
  const Client = require('ssh2-sftp-client');
296
295
 
297
296
  const config = {
@@ -322,34 +321,24 @@ Retrieves a directory listing. This method returns a Promise, which once realise
322
321
 
323
322
  The objects in the array returned by `list()` have the following properties;
324
323
 
325
- ```javascript
326
- {
327
- type: // file type(-, d, l)
328
- name: // file name
329
- size: // file size
330
- modifyTime: // file timestamp of modified time
331
- accessTime: // file timestamp of access time
332
- rights: {
333
- user:
334
- group:
335
- other:
336
- },
337
- owner: // user ID
338
- group: // group ID
339
- longname: // like ls -l line
340
- }
324
+ ```nillangnilswitchesnilflags
325
+ nilbody
326
+ #+END_SRC
327
+
328
+ ***
329
+ nilbody
341
330
  ```
342
331
 
343
-
344
- <a id="org27dfbbb"></a>
345
-
346
- ### exists(path) ==> boolean
332
+ ### exists(path) ==> boolean<a id="sec-4-2-4"></a>
347
333
 
348
334
  Tests to see if remote file or directory exists. Returns type of remote object if it exists or false if it does not.
349
335
 
350
336
  1. Example Use
351
337
 
352
338
  ```javascript
339
+
340
+ const
341
+
353
342
  const Client = require('ssh2-sftp-client');
354
343
 
355
344
  const config = {
@@ -376,10 +365,7 @@ Tests to see if remote file or directory exists. Returns type of remote object i
376
365
  });
377
366
  ```
378
367
 
379
-
380
- <a id="org989df3b"></a>
381
-
382
- ### stat(path) ==> object
368
+ ### stat(path) ==> object<a id="sec-4-2-5"></a>
383
369
 
384
370
  Returns the attributes associated with the object pointed to by `path`.
385
371
 
@@ -390,6 +376,9 @@ Returns the attributes associated with the object pointed to by `path`.
390
376
  The `stat()` method returns an object with the following properties;
391
377
 
392
378
  ```javascript
379
+
380
+ let
381
+
393
382
  let stats = {
394
383
  mode: 33279, // integer representing type and permissions
395
384
  uid: 1000, // user ID
@@ -410,6 +399,9 @@ Returns the attributes associated with the object pointed to by `path`.
410
399
  2. Example Use
411
400
 
412
401
  ```javascript
402
+
403
+ let
404
+
413
405
  let client = new Client();
414
406
 
415
407
  client.connect(config)
@@ -427,10 +419,7 @@ Returns the attributes associated with the object pointed to by `path`.
427
419
  });
428
420
  ```
429
421
 
430
-
431
- <a id="orgc4730e8"></a>
432
-
433
- ### get(path, dst, options) ==> String|Stream|Buffer
422
+ ### get(path, dst, options) ==> String|Stream|Buffer<a id="sec-4-2-6"></a>
434
423
 
435
424
  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.
436
425
 
@@ -445,6 +434,9 @@ In general, if you're going to pass in a string as the destination, you are bett
445
434
  The `options` argument can be used to pass options to the underlying streams and pipe call used by this method. The argument is an object with three possible properties, `readStreamOptions`, `writeStreamOptions` and `pipeOptions`. The values for each of these properties should be an object containing the required options. For example, possible read stream and pipe options could be defined as
446
435
 
447
436
  ```javascript
437
+
438
+ let
439
+
448
440
  let options = {
449
441
  readStreamOptions: {
450
442
  flags: 'r',
@@ -464,6 +456,9 @@ In general, if you're going to pass in a string as the destination, you are bett
464
456
  2. Example Use
465
457
 
466
458
  ```javascript
459
+
460
+ let
461
+
467
462
  let client = new Client();
468
463
 
469
464
  let remotePath = '/remote/server/path/file.txt';
@@ -483,10 +478,7 @@ In general, if you're going to pass in a string as the destination, you are bett
483
478
 
484
479
  - **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'.
485
480
 
486
-
487
- <a id="org2f58141"></a>
488
-
489
- ### fastGet(remotePath, localPath, options) ===> string
481
+ ### fastGet(remotePath, localPath, options) ===> string<a id="sec-4-2-7"></a>
490
482
 
491
483
  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.
492
484
 
@@ -496,13 +488,12 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
496
488
 
497
489
  1. Options
498
490
 
499
- ```javascript
500
- {
501
- concurrency: 64, // integer. Number of concurrent reads to use
502
- chunkSize: 32768, // integer. Size of each read in bytes
503
- step: function(total_transferred, chunk, total) // callback called each time a
504
- // chunk is transferred
505
- }
491
+ ```nillangnilswitchesnilflags
492
+ nilbody
493
+ #+END_SRC
494
+
495
+ -
496
+ nilbody
506
497
  ```
507
498
 
508
499
  - **Warning:** Some servers do not respond correctly to requests to alter chunk size. This can result in lost or corrupted data.
@@ -510,6 +501,9 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
510
501
  2. Sample Use
511
502
 
512
503
  ```javascript
504
+
505
+ let
506
+
513
507
  let client = new Client();
514
508
  let remotePath = '/server/path/file.txt';
515
509
  let localPath = '/local/path/file.txt';
@@ -526,10 +520,7 @@ Downloads a file at remotePath to localPath using parallel reads for faster thro
526
520
  });
527
521
  ```
528
522
 
529
-
530
- <a id="org6859ddc"></a>
531
-
532
- ### put(src, remotePath, options) ==> string
523
+ ### put(src, remotePath, options) ==> string<a id="sec-4-2-8"></a>
533
524
 
534
525
  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.
535
526
 
@@ -541,13 +532,12 @@ Upload data from local system to remote server. If the `src` argument is a strin
541
532
 
542
533
  The options object supports three properties, `readStreamOptions`, `writeStreamOptions` and `pipeOptions`. The value for each property should be an object with options as properties and their associated values representing the option value. For example, you might use the following to set `writeStream` options.
543
534
 
544
- ```javascript
545
- {
546
- writeStreamOptions: {
547
- flags: 'w', // w - write and a - append
548
- encoding: null, // use null for binary files
549
- mode: 0o666, // mode to use for created file (rwx)
550
- }}
535
+ ```nillangnilswitchesnilflags
536
+ nilbody
537
+ #+END_SRC
538
+
539
+ The
540
+ nilbody
551
541
  ```
552
542
 
553
543
  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.
@@ -557,6 +547,9 @@ Upload data from local system to remote server. If the `src` argument is a strin
557
547
  2. Example Use
558
548
 
559
549
  ```javascript
550
+
551
+ let
552
+
560
553
  let client = new Client();
561
554
 
562
555
  let data = fs.createReadStream('/path/to/local/file.txt');
@@ -576,10 +569,7 @@ Upload data from local system to remote server. If the `src` argument is a strin
576
569
 
577
570
  - **Tip:** If the src argument is a path string, consider just using `fastPut()`.
578
571
 
579
-
580
- <a id="org62993ea"></a>
581
-
582
- ### fastPut(localPath, remotePath, options) ==> string
572
+ ### fastPut(localPath, remotePath, options) ==> string<a id="sec-4-2-9"></a>
583
573
 
584
574
  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.
585
575
 
@@ -589,14 +579,12 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
589
579
 
590
580
  1. Options
591
581
 
592
- ```javascript
593
- {
594
- concurrency: 64, // integer. Number of concurrent reads
595
- chunkSize: 32768, // integer. Size of each read in bytes
596
- mode: 0o755, // mixed. Integer or string representing the file mode to set
597
- step: function(total_transferred, chunk, total) // function. Called every time
598
- // a part of a file was transferred
599
- }
582
+ ```nillangnilswitchesnilflags
583
+ nilbody
584
+ #+END_SRC
585
+
586
+ -
587
+ nilbody
600
588
  ```
601
589
 
602
590
  - **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.
@@ -604,6 +592,9 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
604
592
  2. Example Use
605
593
 
606
594
  ```javascript
595
+
596
+ let
597
+
607
598
  let localFile = '/path/to/file.txt';
608
599
  let remoteFile = '/path/to/remote/file.txt';
609
600
  let client = new Client();
@@ -620,10 +611,7 @@ Uploads the data in file at `localPath` to a new file on remote server at `remot
620
611
  });
621
612
  ```
622
613
 
623
-
624
- <a id="orgbac60b5"></a>
625
-
626
- ### append(input, remotePath, options) ==> string
614
+ ### append(input, remotePath, options) ==> string<a id="sec-4-2-10"></a>
627
615
 
628
616
  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.
629
617
 
@@ -635,13 +623,12 @@ Append the `input` data to an existing remote file. There is no integrity checki
635
623
 
636
624
  The following options are supported;
637
625
 
638
- ```javascript
639
- {
640
- flags: 'a', // w - write and a - append
641
- encoding: null, // use null for binary files
642
- mode: 0o666, // mode to use for created file (rwx)
643
- autoClose: true // automatically close the write stream when finished
644
- }
626
+ ```nillangnilswitchesnilflags
627
+ nilbody
628
+ #+END_SRC
629
+
630
+ The
631
+ nilbody
645
632
  ```
646
633
 
647
634
  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.
@@ -649,6 +636,9 @@ Append the `input` data to an existing remote file. There is no integrity checki
649
636
  2. Example Use
650
637
 
651
638
  ```javascript
639
+
640
+ let
641
+
652
642
  let remotePath = '/path/to/remote/file.txt';
653
643
  let client = new Client();
654
644
 
@@ -664,10 +654,7 @@ Append the `input` data to an existing remote file. There is no integrity checki
664
654
  });
665
655
  ```
666
656
 
667
-
668
- <a id="orgf266033"></a>
669
-
670
- ### mkdir(path, recursive) ==> string
657
+ ### mkdir(path, recursive) ==> string<a id="sec-4-2-11"></a>
671
658
 
672
659
  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.
673
660
 
@@ -677,6 +664,9 @@ Create a new directory. If the recursive flag is set to true, the method will cr
677
664
  1. Example Use
678
665
 
679
666
  ```javascript
667
+
668
+ let
669
+
680
670
  let remoteDir = '/path/to/new/dir';
681
671
  let client = new Client();
682
672
 
@@ -692,10 +682,7 @@ Create a new directory. If the recursive flag is set to true, the method will cr
692
682
  });
693
683
  ```
694
684
 
695
-
696
- <a id="orgcb249fd"></a>
697
-
698
- ### rmdir(path, recursive) ==> string
685
+ ### rmdir(path, recursive) ==> string<a id="sec-4-2-12"></a>
699
686
 
700
687
  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.
701
688
 
@@ -707,6 +694,9 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
707
694
  1. Example Use
708
695
 
709
696
  ```javascript
697
+
698
+ let
699
+
710
700
  let remoteDir = '/path/to/remote/dir';
711
701
  let client = new Client();
712
702
 
@@ -722,10 +712,7 @@ Remove a directory. If removing a directory and recursive flag is set to `true`,
722
712
  });
723
713
  ```
724
714
 
725
-
726
- <a id="orgfef0bf5"></a>
727
-
728
- ### delete(path, noErrorOK) ==> string
715
+ ### delete(path, noErrorOK) ==> string<a id="sec-4-2-13"></a>
729
716
 
730
717
  Delete a file on the remote server.
731
718
 
@@ -736,6 +723,9 @@ Delete a file on the remote server.
736
723
  1. Example Use
737
724
 
738
725
  ```javascript
726
+
727
+ let
728
+
739
729
  let remoteFile = '/path/to/remote/file.txt';
740
730
  let client = new Client();
741
731
 
@@ -751,10 +741,7 @@ Delete a file on the remote server.
751
741
  });
752
742
  ```
753
743
 
754
-
755
- <a id="org916cc1f"></a>
756
-
757
- ### rename(fromPath, toPath) ==> string
744
+ ### rename(fromPath, toPath) ==> string<a id="sec-4-2-14"></a>
758
745
 
759
746
  Rename a file or directory from `fromPath` to `toPath`. You must have the necessary permissions to modify the remote file.
760
747
 
@@ -764,6 +751,9 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
764
751
  1. Example Use
765
752
 
766
753
  ```javascript
754
+
755
+ let
756
+
767
757
  let from = '/remote/path/to/old.txt';
768
758
  let to = '/remote/path/to/new.txt';
769
759
  let client = new Client();
@@ -780,17 +770,17 @@ Rename a file or directory from `fromPath` to `toPath`. You must have the necess
780
770
  });
781
771
  ```
782
772
 
773
+ ### posixRename(fromPath, toPath) ==> string<a id="sec-4-2-15"></a>
783
774
 
784
- <a id="org590854d"></a>
785
-
786
- ### posixRename(fromPath, toPath) ==> string
787
-
788
- 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.
775
+ 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 file systems 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 sftp servers.
789
776
 
790
777
  - **fromPath:** string. Path to existing file to be renamed.
791
778
  - **toPath:** string. Path for new name. If it already exists, it will be replaced by file specified in fromPath
792
779
 
793
780
  ```javascript
781
+
782
+ let
783
+
794
784
  let from = '/remote/path/to/old.txt';
795
785
  let to = '/remote/path/to/new.txt';
796
786
  let client = new Client();
@@ -807,10 +797,7 @@ client.connect(config)
807
797
  });
808
798
  ```
809
799
 
810
-
811
- <a id="orgf437cd4"></a>
812
-
813
- ### chmod(path, mode) ==> string
800
+ ### chmod(path, mode) ==> string<a id="sec-4-2-16"></a>
814
801
 
815
802
  Change the mode (read, write or execute permissions) of a remote file or directory.
816
803
 
@@ -820,6 +807,9 @@ Change the mode (read, write or execute permissions) of a remote file or directo
820
807
  1. Example Use
821
808
 
822
809
  ```javascript
810
+
811
+ let
812
+
823
813
  let path = '/path/to/remote/file.txt';
824
814
  let newMode = 0o644; // rw-r-r
825
815
  let client = new Client();
@@ -836,10 +826,7 @@ Change the mode (read, write or execute permissions) of a remote file or directo
836
826
  });
837
827
  ```
838
828
 
839
-
840
- <a id="org22161d6"></a>
841
-
842
- ### realPath(path) ===> string
829
+ ### realPath(path) ===> string<a id="sec-4-2-17"></a>
843
830
 
844
831
  Converts a relative path to an absolute path on the remote server. This method is mainly used internally to resolve remote path names.
845
832
 
@@ -847,17 +834,11 @@ Converts a relative path to an absolute path on the remote server. This method i
847
834
 
848
835
  - **path:** A file path, either relative or absolute. Can handle '.' and '..', but does not expand '~'.
849
836
 
850
-
851
- <a id="orgdba7a3a"></a>
852
-
853
- ### cwd() ==> string
837
+ ### cwd() ==> string<a id="sec-4-2-18"></a>
854
838
 
855
839
  Returns what the server believes is the current remote working directory.
856
840
 
857
-
858
- <a id="org583c51e"></a>
859
-
860
- ### uploadDir(srcDir, dstDir, options) ==> string
841
+ ### uploadDir(srcDir, dstDir, options) ==> string<a id="sec-4-2-19"></a>
861
842
 
862
843
  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.
863
844
 
@@ -876,61 +857,61 @@ The `useFastput` option is a boolean option. If `true`, the method will use the
876
857
  1. Example
877
858
 
878
859
  ```javascript
879
- 'use strict';
880
860
 
881
- // Example of using the uploadDir() method to upload a directory
882
- // to a remote SFTP server
883
861
 
884
- const path = require('path');
885
- const SftpClient = require('../src/index');
886
862
 
887
- const dotenvPath = path.join(__dirname, '..', '.env');
888
- require('dotenv').config({path: dotenvPath});
863
+ 'use strict';
889
864
 
890
- const config = {
891
- host: process.env.SFTP_SERVER,
892
- username: process.env.SFTP_USER,
893
- password: process.env.SFTP_PASSWORD,
894
- port: process.env.SFTP_PORT || 22
895
- };
865
+ // Example of using the uploadDir() method to upload a directory
866
+ // to a remote SFTP server
896
867
 
897
- async function main() {
898
- const client = new SftpClient('upload-test');
899
- const src = path.join(__dirname, '..', 'test', 'testData', 'upload-src');
900
- const dst = '/home/tim/upload-test';
868
+ const path = require('path');
869
+ const SftpClient = require('../src/index');
901
870
 
902
- try {
903
- await client.connect(config);
904
- client.on('upload', info => {
905
- console.log(`Listener: Uploaded ${info.source}`);
906
- });
907
- let rslt = await client.uploadDir(src, dst);
908
- return rslt;
909
- } catch (err) {
910
- console.error(err);
911
- } finally {
912
- client.end();
913
- }
914
- }
871
+ const dotenvPath = path.join(__dirname, '..', '.env');
872
+ require('dotenv').config({path: dotenvPath});
915
873
 
916
- main()
917
- .then(msg => {
918
- console.log(msg);
919
- })
920
- .catch(err => {
921
- console.log(`main error: ${err.message}`);
922
- });
874
+ const config = {
875
+ host: process.env.SFTP_SERVER,
876
+ username: process.env.SFTP_USER,
877
+ password: process.env.SFTP_PASSWORD,
878
+ port: process.env.SFTP_PORT || 22
879
+ };
880
+
881
+ async function main() {
882
+ const client = new SftpClient('upload-test');
883
+ const src = path.join(__dirname, '..', 'test', 'testData', 'upload-src');
884
+ const dst = '/home/tim/upload-test';
885
+
886
+ try {
887
+ await client.connect(config);
888
+ client.on('upload', info => {
889
+ console.log(`Listener: Uploaded ${info.source}`);
890
+ });
891
+ let rslt = await client.uploadDir(src, dst);
892
+ return rslt;
893
+ } catch (err) {
894
+ console.error(err);
895
+ } finally {
896
+ client.end();
897
+ }
898
+ }
899
+
900
+ main()
901
+ .then(msg => {
902
+ console.log(msg);
903
+ })
904
+ .catch(err => {
905
+ console.log(`main error: ${err.message}`);
906
+ });
923
907
 
924
908
  ```
925
909
 
926
-
927
- <a id="orgc06694a"></a>
928
-
929
- ### downloadDir(srcDir, dstDir, options) ==> string
910
+ ### downloadDir(srcDir, dstDir, options) ==> string<a id="sec-4-2-20"></a>
930
911
 
931
912
  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.
932
913
 
933
- 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.
914
+ The method also emits `download` events to provide a way to monitor download progress. The download event listener is called with one argument, an object with two properties, source and destination. The source property is the path to the remote file that has been downloaded and the destination is the local path to where the file was downloaded to. You can add a listener for this event using the `on()` method.
934
915
 
935
916
  The `options` argument is an options object with two supported properties, `filter` and `useFastget`. The `filter` argument is a predicate function which will be called with two arguments for each potential item to be downloaded. The first argument is the full path of the item and the second argument is a boolean, which will be true if the item is a directory. If the function returns true, the item will be included in the download. If it returns false, it will be filtered and ignored. The filter function is called via the `Array.filter` method. These array comprehension methods are known to be unsafe for asynchronous functions. Therefore, only synchronous filter functions are supported at this time.
936
917
 
@@ -938,11 +919,14 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
938
919
 
939
920
  - **srcDir:** A remote file path specified as a string
940
921
  - **dstDir:** A local file path specified as a string
941
- - **options:** An object with two supported properties, `filter` and `useFastget`. The filter property is a function accepting two arguments, the full path to an item and a boolean value which will be true if the item is a directory. The function is called for each item in the download path and should return true to include the item and false to exclude it in the download. The `useFastget` property is a boolean. If true, the `fastGet()` method will be used to transfer files. If `false` (the default), the slower but better supported `get()` mehtod is used. .
922
+ - **options:** An object with two supported properties, `filter` and `useFastget`. The filter property is a function accepting two arguments, the full path to an item and a boolean value which will be true if the item is a directory. The function is called for each item in the download path and should return true to include the item and false to exclude it in the download. The `useFastget` property is a boolean. If true, the `fastGet()` method will be used to transfer files. If `false` (the default), the slower but better supported `get()` method is used. .
942
923
 
943
924
  1. Example
944
925
 
945
926
  ```javascript
927
+
928
+ 'use
929
+
946
930
  'use strict';
947
931
 
948
932
  // Example of using the downloadDir() method to upload a directory
@@ -988,10 +972,7 @@ If the `useFastget` property is set to `true`, the method will use `fastGet()` t
988
972
 
989
973
  ```
990
974
 
991
-
992
- <a id="org5163593"></a>
993
-
994
- ### createReadStream(remotePath, options)) ==> stream object
975
+ ### createReadStream(remotePath, options)) ==> stream object<a id="sec-4-2-21"></a>
995
976
 
996
977
  Returns a read stream object which is attached to the remote file specified by the `remotePath` argument. This is a low level method which just returns a read stream object. Client code is fully responsible for managing and releasing the resources associated with the stream once finished i.e. closing files, removing listeners etc.
997
978
 
@@ -1003,43 +984,37 @@ Returns a read stream object which is attached to the remote file specified by t
1003
984
  - **mode:** 0o666
1004
985
  - **autoClose:** defaults to true. If set to false, client code is responsible for closing file descriptors when finished
1005
986
  - **start:** Default 0. Position to start reading bytes from (inclusive)
1006
- - **end:** Postion to stop reading bytes (inclusive).
987
+ - **end:** Position to stop reading bytes (inclusive).
1007
988
 
989
+ ### createWriteStream(remotePath, options) ==> stream object<a id="sec-4-2-22"></a>
1008
990
 
1009
- <a id="orgbeba46f"></a>
1010
-
1011
- ### createWriteStream(remotePath, options) ==> stream object
1012
-
1013
- Returns a write stream object which is attached to the remote file specified in the `remotePath` argument. This is a low legvel function which just returns the stream object. Client code is fully responsible for managing that object, including closing any file descriptiors and removing listeners etc.
991
+ Returns a write stream object which is attached to the remote file specified in the `remotePath` argument. This is a low level function which just returns the stream object. Client code is fully responsible for managing that object, including closing any file descriptors and removing listeners etc.
1014
992
 
1015
993
  - **remotePath:** Path to the remote file specified as a string
1016
994
  - **options:** An object containing stream options. Supported properties include
1017
995
  - **flags:** default 'w'
1018
- - **encoding:** defulat null
996
+ - **encoding:** default null
1019
997
  - **mode:** 0o666
1020
998
  - **autoClose:** true
1021
999
  - **start:** Byte position to start writing from (inclusive). May require changing flag to 'r+'.
1022
1000
 
1001
+ ### rcopy(srcPath, dstPath) ==> string<a id="sec-4-2-23"></a>
1023
1002
 
1024
- <a id="orgfab48e5"></a>
1025
-
1026
- ### rcopy(srcPath, dstPath) ==> string
1027
-
1028
- Perfrom a remote file copy. The file identified by the `srcPath` argument will be copied to the file specified as the `dstPath` argument. The directory where `dstPath` will be placed must exist, but the actual file must not i.e. no overwrites allowed.
1003
+ Perform a remote file copy. The file identified by the `srcPath` argument will be copied to the file specified as the `dstPath` argument. The directory where `dstPath` will be placed must exist, but the actual file must not i.e. no overwrites allowed.
1029
1004
 
1030
1005
  - **srcPath:** Path to remote file to be copied specified as a string
1031
- - **dstPath:** Path to where the copy will be creaeted specified as a string
1006
+ - **dstPath:** Path to where the copy will be created specified as a string
1032
1007
 
1033
-
1034
- <a id="org9ffad87"></a>
1035
-
1036
- ### end() ==> boolean
1008
+ ### end() ==> boolean<a id="sec-4-2-24"></a>
1037
1009
 
1038
1010
  Ends the current client session, releasing the client socket and associated resources. This function also removes all listeners associated with the client.
1039
1011
 
1040
1012
  1. Example Use
1041
1013
 
1042
1014
  ```javascript
1015
+
1016
+ let
1017
+
1043
1018
  let client = new Client();
1044
1019
 
1045
1020
  client.connect(config)
@@ -1054,10 +1029,7 @@ Ends the current client session, releasing the client socket and associated reso
1054
1029
  });
1055
1030
  ```
1056
1031
 
1057
-
1058
- <a id="org5c294b3"></a>
1059
-
1060
- ### Add and Remove Listeners
1032
+ ### Add and Remove Listeners<a id="sec-4-2-25"></a>
1061
1033
 
1062
1034
  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
1063
1035
 
@@ -1075,82 +1047,55 @@ Although normally not required, you can add and remove custom listeners on the s
1075
1047
 
1076
1048
  Removes the specified listener from the event specified in eventType. Note that the `end()` method automatically removes all listeners from the client object.
1077
1049
 
1050
+ # Platform Quirks & Warnings<a id="sec-5"></a>
1078
1051
 
1079
- <a id="org6e33ff1"></a>
1080
-
1081
- # Platform Quirks & Warnings
1082
-
1052
+ ## Server Capabilities<a id="sec-5-1"></a>
1083
1053
 
1084
- <a id="org0b56fde"></a>
1085
-
1086
- ## Server Capabilities
1087
-
1088
- All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-sftp-client` either depend on capabilities of the remote server or the underlying capabilities of the remote server platform. As an example, consider `chmod()`. This command depends on a remote filesystem which implements the 'nix' concept of users and groups. The *win32* platform does not have the same concept of users and groups, so `chmod()` will not behave in the same way.
1054
+ All SFTP servers and platforms are not equal. Some facilities provided by `ssh2-sftp-client` either depend on capabilities of the remote server or the underlying capabilities of the remote server platform. As an example, consider `chmod()`. This command depends on a remote file system which implements the 'nix' concept of users and groups. The *win32* platform does not have the same concept of users and groups, so `chmod()` will not behave in the same way.
1089
1055
 
1090
1056
  One way to determine whether an issue you are encountering is due to `ssh2-sftp-client` or due to the remote server or server platform is to use a simple CLI sftp program, such as openSSH's sftp command. If you observe the same behaviour using plain `sftp` on the command line, the issue is likely due to server or remote platform limitations. Note that you should not use a GUI sftp client, like `Filezilla` or `winSCP` as such GUI programs often attempt to hide these server and platform incompatibilities and will take additional steps to simulate missing functionality etc. You want to use a CLI program which does as little as possible.
1091
1057
 
1058
+ ## Issues with `fastPut()` and `fastGet()` Methods<a id="sec-5-2"></a>
1092
1059
 
1093
- <a id="org7c6eba4"></a>
1094
-
1095
- ## Issues with `fastPut()` and `fastGet()` Methods
1096
-
1097
- The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SFTP server capabilities. Some SFTP servers just do not work correctly with concurrent connections and some are known to have issues with negotiating packet sizes. These issues can sometimes be resolved by tweaking the options supplied to the methods, such as setting number of concurrent connections or a psecific packet size.
1060
+ The `fastPut()` and `fastGet()` methods are known to be somewhat dependent on SFTP server capabilities. Some SFTP servers just do not work correctly with concurrent connections and some are known to have issues with negotiating packet sizes. These issues can sometimes be resolved by tweaking the options supplied to the methods, such as setting number of concurrent connections or a specific packet size.
1098
1061
 
1099
1062
  To see an example of the type of issues you can observe with `fastPut()` or `fastGet()`, have a look at [issue 407](https://github.com/theophilusx/ssh2-sftp-client/issues/407), which describes the experiences of one user. Bottom line, when it works, it tends to work well and be significantly faster than using just `get()` or `put()`. However, when developing code to run against different SFTP servers, especially where you are unable to test against each server, you are likely better off just using `get()` and `put()` or structuring your code so that users can select which method to use (this is what `ssh2-sftp-client` does - for example, see the `!downloadDir()` and `uploadDir()` methods.
1100
1063
 
1101
-
1102
- <a id="org38e15f9"></a>
1103
-
1104
- ## Promises, Events & Managing Exceptions
1064
+ ## Promises, Events & Managing Exceptions<a id="sec-5-3"></a>
1105
1065
 
1106
1066
  One of the challenges in providing a Promise based API over a module like SSH2, which is event based is how to ensure events are handled appropriately. The challenge is due to the synchronous nature of events. You cannot use `try/catch` for events because you have no way of knowing when the event might fire. For example, it could easily fire after your `try/catch` block as completed execution.
1107
1067
 
1108
- Things become even more complicated once you mix in Promises. When you define a promise, you have to methods which can be called to fulfil a promise, `resolve` and `reject`. Only one can be called - once you call `resolve`, you cannot call `reject` (well, you can call it, but it won't have any impact on the fulfilment status of the promise). The problem arises when an event, for exmaple an `error` event is fired either after you have resolved a promise or possibly in-between promises. If you don't catch the `error` event, your script will likely crash with an `uncaught exception` error.
1068
+ Things become even more complicated once you mix in Promises. When you define a promise, you have to methods which can be called to fulfil a promise, `resolve` and `reject`. Only one can be called - once you call `resolve`, you cannot call `reject` (well, you can call it, but it won't have any impact on the fulfilment status of the promise). The problem arises when an event, for example an `error` event is fired either after you have resolved a promise or possibly in-between promises. If you don't catch the `error` event, your script will likely crash with an `uncaught exception` error.
1109
1069
 
1110
1070
  To make matters worse, some servers, particularly servers running on a Windows platform, will raise multiple errors for the same error *event*. For example, when you attempt to connect with a bad username or password, you will get a `All authentication methods have failed` exception. However, under Windows, you will also get a `Connection reset by peer` exception. If we reject the connect promise based on the authentication failure exception, what do we do with the `reset by peer` exception? More critically, what will handle that exception given the promise has already been fulfilled and completed? To make matters worse, it seems that Windows based servers also raise an error event for *non-errors*. For example, when you call the `end()` method, the connection is closed. On windows, this also results in a *connection reset by peer* error. While it could be argued that the remote server resetting the connection after receiving a disconnect request is not an error, it doesn't change the fact that one is raised and we need to somehow deal with it.
1111
1071
 
1112
- To handle this, `ssh2-sftp-client` implements a couple of strategies. Firstly, when you call one of the module's methods, it adds `error`, `end` and `close` event listeners which will call the `reject` moethod on the enclosing promise. It also keeps track of whether an error has been handled and if it has, it ignores any subsequent errors until the promise ends. Typically, the first error caught has the most relevant information and any subsequent error events are less critical or informative, so ignoring them has no negative impact. Provided one of the events is raised before the promise is fulfilled, these handlers will consume the event and deal with it appropriately.
1072
+ To handle this, `ssh2-sftp-client` implements a couple of strategies. Firstly, when you call one of the module's methods, it adds `error`, `end` and `close` event listeners which will call the `reject` method on the enclosing promise. It also keeps track of whether an error has been handled and if it has, it ignores any subsequent errors until the promise ends. Typically, the first error caught has the most relevant information and any subsequent error events are less critical or informative, so ignoring them has no negative impact. Provided one of the events is raised before the promise is fulfilled, these handlers will consume the event and deal with it appropriately.
1113
1073
 
1114
1074
  In testing, it was found that in some situations, particularly during connect operations, subsequent errors fired with a small delay. This prevents the errors from being handled by the event handlers associated with the connect promise. To deal with this, a small 500ms delay has been added to the connect() method, which effectively delays the removal of the event handlers until all events have been caught.
1115
1075
 
1116
- The other area where additional events are fired is during the end() call. To deal with these events, the `end()` method setus up listeners which will simply ignore additional `error`, `end` and `close` events. It is assumed that once you have called `end()` you really only care about any main error which occurs and no longer care about other errors that may be raised as the connection is terminated.
1076
+ The other area where additional events are fired is during the end() call. To deal with these events, the `end()` method sets up listeners which will simply ignore additional `error`, `end` and `close` events. It is assumed that once you have called `end()` you really only care about any main error which occurs and no longer care about other errors that may be raised as the connection is terminated.
1117
1077
 
1118
1078
  In addition to the promise based event handlers, `ssh2-sftp-client` also implements global event handlers which will catch any `error`, `end` or `close` events. Essentially, these global handlers only reset the `sftp` property of the client object, effectively ensuring any subsequent calls are rejected and in the case of an error, send the error to the console.
1119
1079
 
1120
-
1121
- <a id="org10f1f65"></a>
1122
-
1123
- ### Adding Custom Handlers
1080
+ ### Adding Custom Handlers<a id="sec-5-3-1"></a>
1124
1081
 
1125
1082
  While the above strategies appear to work for the majority of use cases, there are always going to be edge cases which require more flexible or powerful event handling. To support this, the `on()` and `removeListener()` methods are provided. Any event listener added using the `on()` method will be added at the beginning of the list of handlers for that event, ensuring it will be called before any global or promise local events. See the documentation for the `on()` method for details.
1126
1083
 
1127
-
1128
- <a id="org7057905"></a>
1129
-
1130
- ## Windows Based Servers
1084
+ ## Windows Based Servers<a id="sec-5-4"></a>
1131
1085
 
1132
1086
  It appears that when the sftp server is running on Windows, a *ECONNRESET* error signal is raised when the end() method is called. Unfortunately, this signal is raised after a considerable delay. This means we cannot remove the error handler used in the end() promise as otherwise you will get an uncaught exception error. Leaving the handler in place, even though we will ignore this error, solves that issue, but unfortunately introduces a new problem. Because we are not removing the listener, if you re-use the client object for subsequent connections, an additional error handler will be added. If this happens more than 11 times, you will eventually see the Node warning about a possible memory leak. This is because node monitors the number of error handlers and if it sees more than 11 added to an object, it assumes there is a problem and generates the warning.
1133
1087
 
1134
1088
  The best way to avoid this issue is to not re-use client objects. Always generate a new sftp client object for each new connection.
1135
1089
 
1136
-
1137
- <a id="org1775ff2"></a>
1138
-
1139
- ## Don't Re-use SftpClient Objects
1090
+ ## Don't Re-use SftpClient Objects<a id="sec-5-5"></a>
1140
1091
 
1141
1092
  Due to an issue with *ECONNRESET* error signals when connecting to Windows based SFTP servers, it is not possible to remove the error handler in the end() method. This means that if you re-use the SftpClient object for multiple connections e.g. calling connect(), then end(), then connect() etc, you run the risk of multiple error handlers being added to the SftpClient object. After 11 handlers have been added, Node will generate a possible memory leak warning.
1142
1093
 
1143
1094
  To avoid this problem, don't re-use SftpClient objects. Generate a new SftpClient object for each connection. You can perform multiple actions with a single connection e.g. upload multiple files, download multiple files etc, but after you have called end(), you should not try to re-use the object with a further connect() call. Create a new object instead.
1144
1095
 
1096
+ # FAQ<a id="sec-6"></a>
1145
1097
 
1146
- <a id="org683ae73"></a>
1147
-
1148
- # FAQ
1149
-
1150
-
1151
- <a id="org7e12a90"></a>
1152
-
1153
- ## Remote server drops connections with only an end event
1098
+ ## Remote server drops connections with only an end event<a id="sec-6-1"></a>
1154
1099
 
1155
1100
  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.
1156
1101
 
@@ -1158,16 +1103,16 @@ Clients first make an unauthenticated connection to the SFTP server to begin neg
1158
1103
 
1159
1104
  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.
1160
1105
 
1161
-
1162
- <a id="org7f93e7e"></a>
1163
-
1164
- ## How can I pass writable stream as dst for get method?
1106
+ ## How can I pass writeable stream as dst for get method?<a id="sec-6-2"></a>
1165
1107
 
1166
1108
  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()`.
1167
1109
 
1168
1110
  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.
1169
1111
 
1170
1112
  ```javascript
1113
+
1114
+ 'use
1115
+
1171
1116
  'use strict';
1172
1117
 
1173
1118
  // Example of using a writeable with get to retrieve a file.
@@ -1217,16 +1162,16 @@ sftp
1217
1162
  });
1218
1163
  ```
1219
1164
 
1220
-
1221
- <a id="org6f24d1a"></a>
1222
-
1223
- ## How can I upload files without having to specify a password?
1165
+ ## How can I upload files without having to specify a password?<a id="sec-6-3"></a>
1224
1166
 
1225
1167
  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.
1226
1168
 
1227
1169
  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.
1228
1170
 
1229
1171
  ```javascript
1172
+
1173
+ let
1174
+
1230
1175
  let sftp = new Client();
1231
1176
  sftp.connect({
1232
1177
  host: 'YOUR-HOST',
@@ -1241,6 +1186,9 @@ sftp.connect({
1241
1186
  Another alternative is to just pass in the SSH key directly as part of the configuration.
1242
1187
 
1243
1188
  ```javascript
1189
+
1190
+ let
1191
+
1244
1192
  let sftp = new Client();
1245
1193
  sftp.connect({
1246
1194
  host: 'YOUR-HOST',
@@ -1252,14 +1200,14 @@ sftp.connect({
1252
1200
  }
1253
1201
  ```
1254
1202
 
1255
-
1256
- <a id="org59a22c0"></a>
1257
-
1258
- ## How can I connect through a Socks Proxy
1203
+ ## How can I connect through a Socks Proxy<a id="sec-6-4"></a>
1259
1204
 
1260
1205
  This solution was provided by @jmorino.
1261
1206
 
1262
1207
  ```javascript
1208
+
1209
+ import
1210
+
1263
1211
  import { SocksClient } from 'socks';
1264
1212
  import SFTPClient from 'ssh2-sftp-client';
1265
1213
 
@@ -1288,59 +1236,59 @@ client.connect({
1288
1236
  // client is connected
1289
1237
  ```
1290
1238
 
1239
+ ## Timeout while waiting for handshake or handshake errors<a id="sec-6-5"></a>
1291
1240
 
1292
- <a id="org0439a3c"></a>
1241
+ 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.
1293
1242
 
1294
- ## Timeout while waiting for handshake or handshake errors
1243
+ When encountering this type of problem, one worthwhile approach is to use openSSH's CLI sftp program with the `-v` switch to raise logging levels. This will show you what algorithms the CLI is using. You can then use this information to match the names with the accepted algorithm names documented in the `ssh2` README to set the properties in the `algorithms` object.
1295
1244
 
1296
- 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.
1245
+ ## How can I limit upload/download speed<a id="sec-6-6"></a>
1297
1246
 
1298
- When encountering this type of problem, one worthwhile approach is to use openSSH's CLI sftp program with the `-v` switch to raise loggin levels. This will show you what algorithms the CLI is using. You can then use this information to match the names with the accepted algorithm names documented in the `ssh2` README to set the properties in the `algorithms` object.
1247
+ If you want to limit the amount of bandwidth used during upload/download of data, you can use a stream to limit throughput. The following example was provided by *kennylbj*. Note that there is a caveat that we must set the `autoClose` flag to false to avoid calling an extra `_read()` on a closed stream that may cause \_get Permission Denied error in ssh2-streams.
1299
1248
 
1249
+ ```javascript
1300
1250
 
1301
- <a id="orge773153"></a>
1302
1251
 
1303
- ## How can I limit upload/download speed
1304
1252
 
1305
- If you want to limit the amount of bandwidth used during upload/download of data, you can use a stream to limit throughput. The following example was provided by *kennylbj*. Note that there is a caveat that we must set the `autoClose` flag to false to avoid calling an extra `_read()` on a closed stream that may cause \_get Permission Denied error in ssh2-streams.
1306
1253
 
1307
- ```javascript
1308
1254
 
1309
1255
 
1310
- const Throttle = require('throttle');
1311
- const progress = require('progress-stream');
1312
1256
 
1313
- // limit download speed
1314
- const throttleStream = new Throttle(config.throttle);
1315
1257
 
1316
- // download progress stream
1317
- const progressStream = progress({
1318
- length: fileSize,
1319
- time: 500,
1320
- });
1321
- progressStream.on('progress', (progress) => {
1322
- console.log(progress.percentage.toFixed(2));
1323
- });
1324
1258
 
1325
- const outStream = createWriteStream(localPath);
1326
1259
 
1327
- // pipe streams together
1328
- throttleStream.pipe(progressStream).pipe(outStream);
1329
1260
 
1330
- try {
1331
- // set autoClose to false
1332
- await client.get(remotePath, throttleStream, { autoClose: false });
1333
- } catch (e) {
1334
- console.log('sftp error', e);
1335
- } finally {
1336
- await client.end();
1337
- }
1338
- ```
1261
+ const Throttle = require('throttle');
1262
+ const progress = require('progress-stream');
1263
+
1264
+ // limit download speed
1265
+ const throttleStream = new Throttle(config.throttle);
1266
+
1267
+ // download progress stream
1268
+ const progressStream = progress({
1269
+ length: fileSize,
1270
+ time: 500,
1271
+ });
1272
+ progressStream.on('progress', (progress) => {
1273
+ console.log(progress.percentage.toFixed(2));
1274
+ });
1339
1275
 
1276
+ const outStream = createWriteStream(localPath);
1340
1277
 
1341
- <a id="orgf14374e"></a>
1278
+ // pipe streams together
1279
+ throttleStream.pipe(progressStream).pipe(outStream);
1342
1280
 
1343
- ## Connection hangs or fails for larger files
1281
+ try {
1282
+ // set autoClose to false
1283
+ await client.get(remotePath, throttleStream, { autoClose: false });
1284
+ } catch (e) {
1285
+ console.log('sftp error', e);
1286
+ } finally {
1287
+ await client.end();
1288
+ }
1289
+ ```
1290
+
1291
+ ## Connection hangs or fails for larger files<a id="sec-6-7"></a>
1344
1292
 
1345
1293
  This was contributed by Ladislav Jacho. Thanks.
1346
1294
 
@@ -1348,17 +1296,11 @@ A symptom of this issue is that you are able to upload small files, but uploadin
1348
1296
 
1349
1297
  For more explanation, see [issue #342](https://github.com/theophilusx/ssh2-sftp-client/issues/342).
1350
1298
 
1351
-
1352
- <a id="orge58bfb0"></a>
1353
-
1354
- # Examples
1299
+ # Examples<a id="sec-7"></a>
1355
1300
 
1356
1301
  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.
1357
1302
 
1358
-
1359
- <a id="org5c3ee29"></a>
1360
-
1361
- # Troubleshooting
1303
+ # Troubleshooting<a id="sec-8"></a>
1362
1304
 
1363
1305
  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.
1364
1306
 
@@ -1370,21 +1312,18 @@ Note also that in the repository there are two useful directories. The first is
1370
1312
 
1371
1313
  The second directory is the validation directory. I have some very simple scripts in this directory which perform basic tasks using only the `ssh2` modules (no `ssh2-sftp-client` module). These can be useful when trying to determine if the issue is with the underlying `ssh2` module or the `ssh2-sftp-client` wrapper module.
1372
1314
 
1315
+ ## Common Errors<a id="sec-8-1"></a>
1373
1316
 
1374
- <a id="orgd46a3a1"></a>
1317
+ There are some common errors people tend to make when using Promises or Async/Await. These are by far the most common problem found in issues logged against this module. Please check for some of these before logging your issue.
1375
1318
 
1376
- ## Common Errors
1377
-
1378
- 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.
1319
+ ### Not returning the promise in a `then()` block<a id="sec-8-1-1"></a>
1379
1320
 
1321
+ 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 example, the following will not do what you expect
1380
1322
 
1381
- <a id="org41486a0"></a>
1323
+ ```javascript
1382
1324
 
1383
- ### Not returning the promise in a `then()` block
1384
1325
 
1385
- 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 example, the following will not do what you expect
1386
1326
 
1387
- ```javascript
1388
1327
  sftp.connect(config)
1389
1328
  .then(() => {
1390
1329
  sftp.fastGet('foo.txt', 'bar.txt');
@@ -1399,6 +1338,9 @@ sftp.connect(config)
1399
1338
  In the above code, the `sftp.end()` method will almost certainly be called before `sftp.fastGet()` 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
1400
1339
 
1401
1340
  ```javascript
1341
+
1342
+
1343
+
1402
1344
  sftp.connect(config)
1403
1345
  .then(() => {
1404
1346
  return sftp.fastGet('foo.txt', 'bar.txt');
@@ -1414,14 +1356,14 @@ Note the `return` statements. These ensure that the Promise returned by the clie
1414
1356
 
1415
1357
  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.
1416
1358
 
1417
-
1418
- <a id="org36f96d6"></a>
1419
-
1420
- ### Mixing Promise Chains and Async/Await
1359
+ ### Mixing Promise Chains and Async/Await<a id="sec-8-1-2"></a>
1421
1360
 
1422
1361
  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!)
1423
1362
 
1424
1363
  ```javascript
1364
+
1365
+
1366
+
1425
1367
  sftp.connect(config)
1426
1368
  .then(() => {
1427
1369
  return sftp.cwd();
@@ -1444,6 +1386,9 @@ The main bug in the above code is the `then()` block is not returning the Promis
1444
1386
  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
1445
1387
 
1446
1388
  ```javascript
1389
+
1390
+
1391
+
1447
1392
  sftp.connect(config)
1448
1393
  .then(() => {
1449
1394
  return sftp.cwd();
@@ -1460,6 +1405,9 @@ sftp.connect(config)
1460
1405
  **or, using async/await**
1461
1406
 
1462
1407
  ```javascript
1408
+
1409
+
1410
+
1463
1411
  async function doSftp() {
1464
1412
  try {
1465
1413
  let sftp = await sftp.connect(conf);
@@ -1474,41 +1422,32 @@ async function doSftp() {
1474
1422
  }
1475
1423
  ```
1476
1424
 
1477
-
1478
- <a id="org367d856"></a>
1479
-
1480
- ### Try/catch and Error Handlers
1425
+ ### Try/catch and Error Handlers<a id="sec-8-1-3"></a>
1481
1426
 
1482
1427
  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.
1483
1428
 
1484
- 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.
1429
+ 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 completed, 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.
1485
1430
 
1486
1431
  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.
1487
1432
 
1488
-
1489
- <a id="org180dea0"></a>
1490
-
1491
- ### Server Differences
1433
+ ### Server Differences<a id="sec-8-1-4"></a>
1492
1434
 
1493
1435
  Not all SFTP servers are the same. Like most standards, the SFTP protocol has some level of interpretation and allows different levels of compliance. This means there can be differences in behaviour between different servers and code which works with one server will not work the same with another. For example, the value returned by *realpath* for non-existent objects can differ significantly. Some servers will throw an error for a particular operation while others will just return null, some servers support concurrent operations (such as used by fastGet/fastPut) while others will not and of course, the text of error messages can vary significantly. In particular, we have noticed significant differences across different platforms. It is therefore advisable to do comprehensive testing when the SFTP server is moved to a new platform. This includes moving from to a cloud based service even if the underlying platform remains the same. I have noticed that some cloud platforms can generate unexpected events, possibly related to additional functionality or features associated with the cloud implementation. For example, it appears SFTP servers running under Azure will generate an error event when the connection is closed even when the client has requested the connection be terminated. The same SFTP server running natively on Windows does not appear to exhibit such behaviour.
1494
1436
 
1495
-
1496
- <a id="org2bfd976"></a>
1497
-
1498
- ### Avoid Concurrent Operations
1437
+ ### Avoid Concurrent Operations<a id="sec-8-1-5"></a>
1499
1438
 
1500
1439
  Technically, SFTP should be able to perform multiple operations concurrently. As node is single threaded, what we a really talking about is running multiple execution contexts as a pool where node will switch contexts when each context is blocked due to things like waiting on network data etc. However, I have found this to be extremely unreliable and of very little benefit from a performance perspective. My recommendation is to therefore avoid executing multiple requests over the same connection in parallel (for example, generating multiple `get()` promises and using something like `Promise.all()` to resolve them.
1501
1440
 
1502
1441
  If you are going to try and perform concurrent operations, you need to test extensively and ensure you are using data which is large enough that context switching does occur (i.e. the request is not completed in a single run). Some SFTP servers will handle concurrent operations better than others.
1503
1442
 
1504
-
1505
- <a id="org9590bf5"></a>
1506
-
1507
- ## Debugging Support
1443
+ ## Debugging Support<a id="sec-8-2"></a>
1508
1444
 
1509
1445
  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;
1510
1446
 
1511
1447
  ```javascript
1448
+
1449
+ config.debug
1450
+
1512
1451
  config.debug = msg => {
1513
1452
  console.error(msg);
1514
1453
  };
@@ -1518,26 +1457,23 @@ config.debug = msg => {
1518
1457
  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.
1519
1458
 
1520
1459
  ```shell
1460
+
1461
+ node
1462
+
1521
1463
  node script.js 2> debug.log
1522
1464
 
1523
1465
  ```
1524
1466
 
1525
1467
  If you just want to see debug messages from `ssh2-sftp-client` and exclude debug messages from the underlying `ssh2` and `ssh2-streams` modules, you can filter based on messages which start with 'CLIENT' e.g.
1526
1468
 
1527
- ```javascript
1528
- {
1529
- debug: (msg) => {
1530
- if (msg.startsWith('CLIENT')) {
1531
- console.error(msg);
1532
- }
1533
- }
1534
- }
1469
+ ```nillangnilswitchesnilflags
1470
+ nilbody
1471
+ #+END_SRC
1472
+ *
1473
+ nilbody
1535
1474
  ```
1536
1475
 
1537
-
1538
- <a id="orga4f395e"></a>
1539
-
1540
- # Logging Issues
1476
+ # Logging Issues<a id="sec-9"></a>
1541
1477
 
1542
1478
  Please log an issue for all bugs, questions, feature and enhancement requests. Please ensure you include the module version, node version and platform.
1543
1479
 
@@ -1551,10 +1487,7 @@ I am happy to try and help diagnose and fix any issues you encounter while using
1551
1487
 
1552
1488
  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.
1553
1489
 
1554
-
1555
- <a id="orgbed06d9"></a>
1556
-
1557
- # Pull Requests
1490
+ # Pull Requests<a id="sec-10"></a>
1558
1491
 
1559
1492
  Pull requests are always welcomed. However, please ensure your changes pass all tests and if you're adding a new feature, that tests for that feature are included. Likewise, for new features or enhancements, please include any relevant documentation updates.
1560
1493
 
@@ -1566,10 +1499,7 @@ This module will adopt a standard semantic versioning policy. Please indicate in
1566
1499
  - **Minor:** Minor change, enhancement or new feature which does not change existing API and will not break existing client code.
1567
1500
  - **Bug Fix:** No change to functionality or features. Simple fix of an existing bug.
1568
1501
 
1569
-
1570
- <a id="org7b575e4"></a>
1571
-
1572
- # Contributors
1502
+ # Contributors<a id="sec-11"></a>
1573
1503
 
1574
1504
  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.
1575
1505
 
@@ -1588,4 +1518,4 @@ Thanks to the following for their contributions -
1588
1518
  - **Emma Milner:** Contributed fix for put() bug
1589
1519
  - **Witni Davis:** Contributed PR to fix put() RCE when using 'finish' rather than 'close' to resolve promise
1590
1520
  - **Maik Marschner:** Contributed fix for connect() not returning sftp object. Also included test to check for this regression in future.
1591
- - **cakemasher:** Contributed fix for removeTempListeners().
1521
+ - **cakemasher:** Contributed fix for removeTempListeners().